digitalbazaar / base64url-universal

Encode/decode "Base64url Encoding" format of JSON Web Signature (JWS) RFC7517.
Other
5 stars 4 forks source link

decode() returns a Buffer instead of Uint8Array when in Node.js #7

Open dmitrizagidulin opened 3 years ago

dmitrizagidulin commented 3 years ago

The decode() function is returning a different type based on environment.

In Node.js:

decode('eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19');
// <Buffer 7b 22 61 6c 67 22 3a 22 45 64 44 53 41 22 2c 22 62 36 34 22 3a 66 61 6c 73 65 2c 22 63 72 69 74 22 3a 5b 22 62 36 34 22 5d 7d>

In the browser:

decode('eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19');
// Uint8Array{0: 123, 1: 34, 2: 97, 3: 108, 4: 103, 5: 34, 6: 58, 7: 34, 8: 69, 9: 100, 10: 68, 11: 83, 12: 65, 13: 34, 14: 44, 15: 34, 16: 98, 17: 54, 18: 52, 19: 34, 20: 58, 21: 102, 22: 97, 23: 108, 24: 115, 25: 101, 26: 44, 27: 34, 28: 99, 29: 114, 30: 105, 31: 116, 32: 34, 33: 58, 34: 91, 35: 34, 36: 98, 37: 54, 38: 52, 39: 34, 40: 93, 41: 125}

Need to fix it so that it returns Uint8Array in Node.js too.

Also, I did NOT know that JSON.parse() works on a Buffer without throwing an error. (But not on a Uint8Array, turns out).

dmitrizagidulin commented 3 years ago

See also the performance discussion over at https://github.com/digitalbazaar/base64url-universal/issues/4

msporny commented 3 years ago

"Buffer instances are also JavaScript Uint8Array and TypedArray instances. All TypedArray methods are available on Buffers." -- https://nodejs.org/api/buffer.html#buffer_buffers_and_typedarrays

There are small incompatibilities... don't know if that affects this library or the tests? What concrete issue is this type difference resulting in?

dmitrizagidulin commented 3 years ago

@msporny - right, Buffers are instances. But there's still small differences in behavior that masked a bug downstream (in our jsigs libs). Now, I've fixed the bug downstream, so for the moment, we don't have to fix this issue.

However, I would at very least like to spell it out explicitly in the README - that in the browser, decode() returns a Uint8Array, and in Node.js, it returns a Buffer, which is technically also a Uint8Array, but there may be some behavior differences.