buffer: expose btoa and atob as globals

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/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>
pull/37839/head
James M Snell 2021-03-17 13:11:05 -07:00
parent abbd9d9db3
commit d1e2184c8e
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC
6 changed files with 67 additions and 6 deletions

View File

@ -329,5 +329,7 @@ module.exports = {
TextDecoder: 'readable',
queueMicrotask: 'readable',
globalThis: 'readable',
btoa: 'readable',
atob: 'readable',
},
};

View File

@ -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
@ -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

View File

@ -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
@ -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

View File

@ -42,6 +42,7 @@ const {
FunctionPrototypeCall,
JSONParse,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectGetPrototypeOf,
ObjectPreventExtensions,
ObjectSetPrototypeOf,
@ -400,7 +401,11 @@ 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()
@ -408,11 +413,25 @@ function setupBuffer() {
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,
},
});
}

View File

@ -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.
//
@ -257,6 +262,8 @@ function platformTimeout(ms) {
}
let knownGlobals = [
atob,
btoa,
clearImmediate,
clearInterval,
clearTimeout,

View File

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