Skip to content

Commit cef0a6d

Browse files
committed
buffer: expose btoa and atob as globals
Signed-off-by: James M Snell <[email protected]>
1 parent c6855eb commit cef0a6d

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,7 @@ module.exports = {
329329
TextDecoder: 'readable',
330330
queueMicrotask: 'readable',
331331
globalThis: 'readable',
332+
btoa: 'readable',
333+
atob: 'readable',
332334
},
333335
};

doc/api/globals.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ This variable may appear to be global but is not. See [`__dirname`][].
146146

147147
This variable may appear to be global but is not. See [`__filename`][].
148148

149+
## `atob(data)`
150+
<!-- YAML
151+
added: REPLACEME
152+
-->
153+
154+
Global alias for [`buffer.atob()`][].
155+
156+
## `btoa(data)`
157+
<!-- YAML
158+
added: REPLACEME
159+
-->
160+
161+
Global alias for [`buffer.btoa()`][].
162+
149163
## `clearImmediate(immediateObject)`
150164
<!-- YAML
151165
added: v0.9.1
@@ -402,6 +416,8 @@ The object that acts as the namespace for all W3C
402416
[`URL`]: url.md#url_class_url
403417
[`__dirname`]: modules.md#modules_dirname
404418
[`__filename`]: modules.md#modules_filename
419+
[`buffer.atob()`]: buffer.html#buffer_buffer_atob_data
420+
[`buffer.btoa()`]: buffer.html#buffer_buffer_btoa_data
405421
[`clearImmediate`]: timers.md#timers_clearimmediate_immediate
406422
[`clearInterval`]: timers.md#timers_clearinterval_timeout
407423
[`clearTimeout`]: timers.md#timers_cleartimeout_timeout

lib/internal/bootstrap/node.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
FunctionPrototypeCall,
4343
JSONParse,
4444
ObjectDefineProperty,
45+
ObjectDefineProperties,
4546
ObjectGetPrototypeOf,
4647
ObjectPreventExtensions,
4748
ObjectSetPrototypeOf,
@@ -400,19 +401,37 @@ function setupGlobalProxy() {
400401
}
401402

402403
function setupBuffer() {
403-
const { Buffer } = require('buffer');
404+
const {
405+
Buffer,
406+
atob,
407+
btoa,
408+
} = require('buffer');
404409
const bufferBinding = internalBinding('buffer');
405410

406411
// Only after this point can C++ use Buffer::New()
407412
bufferBinding.setBufferPrototype(Buffer.prototype);
408413
delete bufferBinding.setBufferPrototype;
409414
delete bufferBinding.zeroFill;
410415

411-
ObjectDefineProperty(global, 'Buffer', {
412-
value: Buffer,
413-
enumerable: false,
414-
writable: true,
415-
configurable: true
416+
ObjectDefineProperties(global, {
417+
'Buffer': {
418+
value: Buffer,
419+
enumerable: false,
420+
writable: true,
421+
configurable: true,
422+
},
423+
'atob': {
424+
value: atob,
425+
enumerable: false,
426+
writable: true,
427+
configurable: true,
428+
},
429+
'btoa': {
430+
value: btoa,
431+
enumerable: false,
432+
writable: true,
433+
configurable: true,
434+
},
416435
});
417436
}
418437

test/common/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ let knownGlobals = [
263263
setInterval,
264264
setTimeout,
265265
queueMicrotask,
266+
atob,
267+
btoa,
266268
];
267269

268270
// TODO(@jasnell): This check can be temporary. AbortController is
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const { strictEqual } = require('assert');
6+
const buffer = require('buffer');
7+
8+
strictEqual(globalThis.atob, buffer.atob);
9+
strictEqual(globalThis.btoa, buffer.btoa);

0 commit comments

Comments
 (0)