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>

PR-URL: #37786
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jasnell committed Mar 25, 2021
1 parent abbd9d9 commit d1e2184
Show file tree
Hide file tree
Showing 6 changed files with 67 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',
},
};
4 changes: 4 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,8 @@ accessed using `require('buffer')`.
added: REPLACEME
-->

> Stability: 3 - Legacy. Use `Buffer.from(data, 'base64')` instead.
* `data` {any} The Base64-encoded input string.

Decodes a string of Base64-encoded data into bytes, and encodes those bytes
Expand All @@ -3301,6 +3303,8 @@ and binary data should be performed using `Buffer.from(str, 'base64')` and
added: REPLACEME
-->

> Stability: 3 - Legacy. Use `buf.toString('base64')` instead.
* `data` {any} An ASCII (Latin1) string.

Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
Expand Down
20 changes: 20 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ 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
-->

> Stability: 3 - Legacy. Use `Buffer.from(data, 'base64')` instead.
Global alias for [`buffer.atob()`][].

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

> Stability: 3 - Legacy. Use `buf.toString('base64')` instead.
Global alias for [`buffer.btoa()`][].

## `clearImmediate(immediateObject)`
<!-- YAML
added: v0.9.1
Expand Down Expand Up @@ -402,6 +420,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.md#buffer_buffer_atob_data
[`buffer.btoa()`]: buffer.md#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
7 changes: 7 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const bits = ['arm64', 'mips', 'mipsel', 'ppc64', 's390x', 'x64']
.includes(process.arch) ? 64 : 32;
const hasIntl = !!process.config.variables.v8_enable_i18n_support;

const {
atob,
btoa
} = require('buffer');

// Some tests assume a umask of 0o022 so set that up front. Tests that need a
// different umask will set it themselves.
//
Expand Down Expand Up @@ -257,6 +262,8 @@ function platformTimeout(ms) {
}

let knownGlobals = [
atob,
btoa,
clearImmediate,
clearInterval,
clearTimeout,
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 d1e2184

Please sign in to comment.