Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: expose btoa and atob as globals #37786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()`][].
jasnell marked this conversation as resolved.
Show resolved Hide resolved

## `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,
},
Comment on lines +423 to +434
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that atob and btoa are enumerable in web browsers.

});
}

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);