Skip to content

Commit

Permalink
buffer: expose btoa and atob as globals
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed Mar 18, 2021
1 parent c6855eb commit cef0a6d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,7 @@ module.exports = {
TextDecoder: 'readable',
queueMicrotask: 'readable',
globalThis: 'readable',
btoa: 'readable',
atob: 'readable',
},
};
16 changes: 16 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ This variable may appear to be global but is not. See [`__dirname`][].

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

## `atob(data)`
<!-- YAML
added: REPLACEME
-->

Global alias for [`buffer.atob()`][].

## `btoa(data)`
<!-- YAML
added: REPLACEME
-->

Global alias for [`buffer.btoa()`][].

## `clearImmediate(immediateObject)`
<!-- YAML
added: v0.9.1
Expand Down Expand Up @@ -402,6 +416,8 @@ The object that acts as the namespace for all W3C
[`URL`]: url.md#url_class_url
[`__dirname`]: modules.md#modules_dirname
[`__filename`]: modules.md#modules_filename
[`buffer.atob()`]: buffer.html#buffer_buffer_atob_data
[`buffer.btoa()`]: buffer.html#buffer_buffer_btoa_data
[`clearImmediate`]: timers.md#timers_clearimmediate_immediate
[`clearInterval`]: timers.md#timers_clearinterval_timeout
[`clearTimeout`]: timers.md#timers_cleartimeout_timeout
Expand Down
31 changes: 25 additions & 6 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
FunctionPrototypeCall,
JSONParse,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectGetPrototypeOf,
ObjectPreventExtensions,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -400,19 +401,37 @@ function setupGlobalProxy() {
}

function setupBuffer() {
const { Buffer } = require('buffer');
const {
Buffer,
atob,
btoa,
} = require('buffer');
const bufferBinding = internalBinding('buffer');

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

ObjectDefineProperty(global, 'Buffer', {
value: Buffer,
enumerable: false,
writable: true,
configurable: true
ObjectDefineProperties(global, {
'Buffer': {
value: Buffer,
enumerable: false,
writable: true,
configurable: true,
},
'atob': {
value: atob,
enumerable: false,
writable: true,
configurable: true,
},
'btoa': {
value: btoa,
enumerable: false,
writable: true,
configurable: true,
},
});
}

Expand Down
2 changes: 2 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ let knownGlobals = [
setInterval,
setTimeout,
queueMicrotask,
atob,
btoa,
];

// TODO(@jasnell): This check can be temporary. AbortController is
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-btoa-atob-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('../common');

const { strictEqual } = require('assert');
const buffer = require('buffer');

strictEqual(globalThis.atob, buffer.atob);
strictEqual(globalThis.btoa, buffer.btoa);

0 comments on commit cef0a6d

Please sign in to comment.