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

bootstrap: consolidate global properties definition #43357

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
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,
},
legendecas marked this conversation as resolved.
Show resolved Hide resolved
'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