Skip to content

Commit

Permalink
bootstrap: consolidate global properties definition
Browse files Browse the repository at this point in the history
`globalThis.process` and `globalThis.Buffer` has been re-defined with
a getter/setter pair.

`atob` and `bota` are defined as enumerable properties according to
WebIDL definition.

PR-URL: #43357
Refs: #26882
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
legendecas authored and danielleadams committed Jun 14, 2022
1 parent 41955e5 commit f7c4015
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 65 deletions.
7 changes: 7 additions & 0 deletions lib/internal/bootstrap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
defineOperation(globalThis, 'setInterval', timers.setInterval);
defineOperation(globalThis, 'setTimeout', timers.setTimeout);

const buffer = require('buffer');
defineOperation(globalThis, 'atob', buffer.atob);
defineOperation(globalThis, 'btoa', buffer.btoa);

// https://www.w3.org/TR/FileAPI/#dfn-Blob
exposeInterface(globalThis, 'Blob', buffer.Blob);

// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
defineReplacableAttribute(globalThis, 'performance',
require('perf_hooks').performance);
Expand Down
59 changes: 24 additions & 35 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const {
FunctionPrototypeCall,
JSONParse,
ObjectDefineProperty,
ObjectDefineProperties,
ObjectGetPrototypeOf,
ObjectPreventExtensions,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -377,13 +376,21 @@ function setupProcessObject() {
configurable: false,
value: 'process'
});
// Make process globally available to users by putting it on the global proxy

// Create global.process as getters so that we have a
// deprecation path for these in ES Modules.
// See https://github.com/nodejs/node/pull/26334.
let _process = process;
ObjectDefineProperty(globalThis, 'process', {
__proto__: null,
value: process,
get() {
return _process;
},
set(value) {
_process = value;
},
enumerable: false,
writable: true,
configurable: true
configurable: true,
});
}

Expand All @@ -399,10 +406,7 @@ function setupGlobalProxy() {

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

Expand All @@ -411,34 +415,19 @@ function setupBuffer() {
delete bufferBinding.setBufferPrototype;
delete bufferBinding.zeroFill;

ObjectDefineProperties(globalThis, {
'Blob': {
__proto__: null,
value: Blob,
enumerable: false,
writable: true,
configurable: true,
},
'Buffer': {
__proto__: null,
value: Buffer,
enumerable: false,
writable: true,
configurable: true,
},
'atob': {
__proto__: null,
value: atob,
enumerable: false,
writable: true,
configurable: true,
// Create global.Buffer as getters so that we have a
// deprecation path for these in ES Modules.
// See https://github.com/nodejs/node/pull/26334.
let _Buffer = Buffer;
ObjectDefineProperty(globalThis, 'Buffer', {
__proto__: null,
get() {
return _Buffer;
},
'btoa': {
__proto__: null,
value: btoa,
enumerable: false,
writable: true,
configurable: true,
set(value) {
_Buffer = value;
},
enumerable: false,
configurable: true,
});
}
30 changes: 0 additions & 30 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {
exposeInterface,
} = require('internal/util');

const { Buffer } = require('buffer');
const {
ERR_MANIFEST_ASSERT_INTEGRITY,
} = require('internal/errors').codes;
Expand Down Expand Up @@ -408,35 +407,6 @@ function initializeDeprecations() {
'process._tickCallback() is deprecated',
'DEP0134');
}

// Create global.process and global.Buffer as getters so that we have a
// deprecation path for these in ES Modules.
// See https://github.com/nodejs/node/pull/26334.
let _process = process;
ObjectDefineProperty(globalThis, 'process', {
__proto__: null,
get() {
return _process;
},
set(value) {
_process = value;
},
enumerable: false,
configurable: true
});

let _Buffer = Buffer;
ObjectDefineProperty(globalThis, 'Buffer', {
__proto__: null,
get() {
return _Buffer;
},
set(value) {
_Buffer = value;
},
enumerable: false,
configurable: true
});
}

function setupChildProcessIpcChannel() {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ builtinModules.forEach((moduleName) => {
'clearImmediate',
'clearInterval',
'clearTimeout',
'atob',
'btoa',
'performance',
'setImmediate',
'setInterval',
Expand Down

0 comments on commit f7c4015

Please sign in to comment.