Skip to content

Commit bf2d9f2

Browse files
jasnelltargos
authored andcommittedMay 1, 2021
buffer: implement btoa and atob
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #37529 Fixes: #3462 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent fe0f6a5 commit bf2d9f2

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed
 

‎doc/api/buffer.md

+38
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,44 @@ While, the `Buffer` object is available as a global, there are additional
31123112
`Buffer`-related APIs that are available only via the `buffer` module
31133113
accessed using `require('buffer')`.
31143114

3115+
### `buffer.atob(data)`
3116+
<!-- YAML
3117+
added: REPLACEME
3118+
-->
3119+
3120+
* `data` {any} The Base64-encoded input string.
3121+
3122+
Decodes a string of Base64-encoded data into bytes, and encodes those bytes
3123+
into a string using Latin-1 (ISO-8859-1).
3124+
3125+
The `data` may be any JavaScript-value that can be coerced into a string.
3126+
3127+
**This function is only provided for compatibility with legacy web platform APIs
3128+
and should never be used in new code, because they use strings to represent
3129+
binary data and predate the introduction of typed arrays in JavaScript.
3130+
For code running using Node.js APIs, converting between base64-encoded strings
3131+
and binary data should be performed using `Buffer.from(str, 'base64')` and
3132+
`buf.toString('base64')`.**
3133+
3134+
### `buffer.btoa(data)`
3135+
<!-- YAML
3136+
added: REPLACEME
3137+
-->
3138+
3139+
* `data` {any} An ASCII (Latin1) string.
3140+
3141+
Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
3142+
into a string using Base64.
3143+
3144+
The `data` may be any JavaScript-value that can be coerced into a string.
3145+
3146+
**This function is only provided for compatibility with legacy web platform APIs
3147+
and should never be used in new code, because they use strings to represent
3148+
binary data and predate the introduction of typed arrays in JavaScript.
3149+
For code running using Node.js APIs, converting between base64-encoded strings
3150+
and binary data should be performed using `Buffer.from(str, 'base64')` and
3151+
`buf.toString('base64')`.**
3152+
31153153
### `buffer.INSPECT_MAX_BYTES`
31163154
<!-- YAML
31173155
added: v0.5.4

‎lib/buffer.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -1202,13 +1202,49 @@ if (internalBinding('config').hasIntl) {
12021202
};
12031203
}
12041204

1205+
let DOMException;
1206+
1207+
const lazyInvalidCharError = hideStackFrames((message, name) => {
1208+
if (DOMException === undefined)
1209+
DOMException = internalBinding('messaging').DOMException;
1210+
throw new DOMException('Invalid character', 'InvalidCharacterError');
1211+
});
1212+
1213+
function btoa(input) {
1214+
// TODO(@jasnell): The implementation here has not been performance
1215+
// optimized in any way.
1216+
input = `${input}`;
1217+
for (let n = 0; n < input.length; n++) {
1218+
if (input[n].charCodeAt(0) > 0xff)
1219+
lazyInvalidCharError();
1220+
}
1221+
const buf = Buffer.from(input, 'latin1');
1222+
return buf.toString('base64');
1223+
}
1224+
1225+
const kBase64Digits =
1226+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
1227+
1228+
function atob(input) {
1229+
// TODO(@jasnell): The implementation here has not been performance
1230+
// optimized in any way.
1231+
input = `${input}`;
1232+
for (let n = 0; n < input.length; n++) {
1233+
if (!kBase64Digits.includes(input[n]))
1234+
lazyInvalidCharError();
1235+
}
1236+
return Buffer.from(input, 'base64').toString('latin1');
1237+
}
1238+
12051239
module.exports = {
12061240
Buffer,
12071241
SlowBuffer,
12081242
transcode,
12091243
// Legacy
12101244
kMaxLength,
1211-
kStringMaxLength
1245+
kStringMaxLength,
1246+
btoa,
1247+
atob,
12121248
};
12131249

12141250
ObjectDefineProperties(module.exports, {

0 commit comments

Comments
 (0)
Please sign in to comment.