Commit 489c41d3 authored by Romain Courteaud's avatar Romain Courteaud

[node] atob raises an Error if parameter is not a valid base64

Try to reproduce the browser specifications.
parent 101fd0ba
...@@ -190,11 +190,10 @@ ...@@ -190,11 +190,10 @@
try { try {
return window.atob(str); return window.atob(str);
} catch (err) { } catch (err) {
var buffer; var buffer = Buffer.from(str.toString(), 'base64');
if (str instanceof Buffer) { // Provide the same behaviour than the browser atob
buffer = str; if (buffer.toString('base64') !== str) {
} else { throw new Error('The string to be decoded is not correctly encoded.');
buffer = Buffer.from(str.toString(), 'base64');
} }
return buffer.toString('binary'); return buffer.toString('binary');
} }
...@@ -206,13 +205,7 @@ ...@@ -206,13 +205,7 @@
try { try {
return window.btoa(str); return window.btoa(str);
} catch (err) { } catch (err) {
var buffer; return Buffer.from(str.toString(), 'binary').toString('base64');
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = Buffer.from(str.toString(), 'binary');
}
return buffer.toString('base64');
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment