[FIXED] Base91 without Node
Closed this issue · 8 comments
Dear Brian,
could you be so kind and make (or tell me how to make) it working without Node?
I want it as standalone JS file to inject and use right away against array of bytes. Current implementation seems to be dependent on Node (which I neither use nor plan to install) and throws errors like ReferenceError: process is not defined
and ReferenceError: hasNode is not defined
.
Try the version in the master branch now, I've tweaked the node check.
Thank you for a quick reply, Brian. Alas, it's still cloudy.
Example console.log(base91.decode(base91.encode('node.js rules!')).toString());
throws [object Uint8Array]
, yet node.js rules!
is promised hence expected. It seems there should be some wrapper that converts like array2string(base91.decode(base91.encode(message)));
, which is missing so far.
Also I have doubts about proper UTF8 support (e.g. let's try to decode encoded word любовь which is love in Russian). Doesn't work, or does? Perhaps, Uint8Array might be used all the way since the very beginning (instead of string) with wrapper like base91.encode(stringToUTF8array(message));
.?
function stringToUTF8array(s) {
var i,
d = unescape(encodeURIComponent(s)),
b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
}
Ok, multibyte character encoding should be fixed in master. For the browser you can't call .toString()
on the return value because that's a Buffer thing for node.js environments. Instead you will need to use a function like this:
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
}
console.log(uintToString(base91.decode(base91.encode('любовь'))));
Brian, it seems you forget to update master with multibyte character encoding.
Offered function uintToString(uintArray))
works as expected, thanks.
Consider accepting byte array for input as well (e.g. base62js supports both strings and arrays).
After all base91 seems to be more suitable to encode binary data than short messages.
Apologies, I forgot to push. It's up there now.
Encoding a Uint8Array should already just work AFAIK. If not, can you provide an example of it not working?
@mscdex Both points work now as expected, great. Thanks for your time.
P.S. What do you usually use base91 for?
Backburner projects, nothing currently, but typically anywhere I need to exchange binary data between browser and server.