base64-js
base64-js copied to clipboard
Ignore padding on decode
Strictly speaking the '=' characters do not carry any information. Can decode ignore missing padding and not throw an error?
Hello downvoters, care to explain your reasoning?
From those lines in the code seems URL-safe base64 strings are supported, but if padding is required then those aren't supported as some variants don't use it.
Node supports decoding URL-safe base64 strings without padding. Example: Buffer.from('HI', 'base64')
return "<Buffer 1c>". base64js.toByteArray('HI')
throws "Invalid string. Length must be a multiple of 4".
As a workaround, I re-inject any missing padding before passing the text to this library, like so -
let b64 = "AAAAAAA"; // base64-encoded text that could be missing padding
if( (b64.length % 4) !== 0) b64 += "=".repeat(4 - (b64.length % 4))