Skip to content

Commit f7c4015

Browse files
legendecasdanielleadams
authored andcommitted
bootstrap: consolidate global properties definition
`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>
1 parent 41955e5 commit f7c4015

File tree

4 files changed

+33
-65
lines changed

4 files changed

+33
-65
lines changed

lib/internal/bootstrap/browser.js

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
6565
defineOperation(globalThis, 'setInterval', timers.setInterval);
6666
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
6767

68+
const buffer = require('buffer');
69+
defineOperation(globalThis, 'atob', buffer.atob);
70+
defineOperation(globalThis, 'btoa', buffer.btoa);
71+
72+
// https://www.w3.org/TR/FileAPI/#dfn-Blob
73+
exposeInterface(globalThis, 'Blob', buffer.Blob);
74+
6875
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
6976
defineReplacableAttribute(globalThis, 'performance',
7077
require('perf_hooks').performance);

lib/internal/bootstrap/node.js

+24-35
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const {
4545
FunctionPrototypeCall,
4646
JSONParse,
4747
ObjectDefineProperty,
48-
ObjectDefineProperties,
4948
ObjectGetPrototypeOf,
5049
ObjectPreventExtensions,
5150
ObjectSetPrototypeOf,
@@ -377,13 +376,21 @@ function setupProcessObject() {
377376
configurable: false,
378377
value: 'process'
379378
});
380-
// Make process globally available to users by putting it on the global proxy
379+
380+
// Create global.process as getters so that we have a
381+
// deprecation path for these in ES Modules.
382+
// See https://github.com/nodejs/node/pull/26334.
383+
let _process = process;
381384
ObjectDefineProperty(globalThis, 'process', {
382385
__proto__: null,
383-
value: process,
386+
get() {
387+
return _process;
388+
},
389+
set(value) {
390+
_process = value;
391+
},
384392
enumerable: false,
385-
writable: true,
386-
configurable: true
393+
configurable: true,
387394
});
388395
}
389396

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

400407
function setupBuffer() {
401408
const {
402-
Blob,
403409
Buffer,
404-
atob,
405-
btoa,
406410
} = require('buffer');
407411
const bufferBinding = internalBinding('buffer');
408412

@@ -411,34 +415,19 @@ function setupBuffer() {
411415
delete bufferBinding.setBufferPrototype;
412416
delete bufferBinding.zeroFill;
413417

414-
ObjectDefineProperties(globalThis, {
415-
'Blob': {
416-
__proto__: null,
417-
value: Blob,
418-
enumerable: false,
419-
writable: true,
420-
configurable: true,
421-
},
422-
'Buffer': {
423-
__proto__: null,
424-
value: Buffer,
425-
enumerable: false,
426-
writable: true,
427-
configurable: true,
428-
},
429-
'atob': {
430-
__proto__: null,
431-
value: atob,
432-
enumerable: false,
433-
writable: true,
434-
configurable: true,
418+
// Create global.Buffer as getters so that we have a
419+
// deprecation path for these in ES Modules.
420+
// See https://github.com/nodejs/node/pull/26334.
421+
let _Buffer = Buffer;
422+
ObjectDefineProperty(globalThis, 'Buffer', {
423+
__proto__: null,
424+
get() {
425+
return _Buffer;
435426
},
436-
'btoa': {
437-
__proto__: null,
438-
value: btoa,
439-
enumerable: false,
440-
writable: true,
441-
configurable: true,
427+
set(value) {
428+
_Buffer = value;
442429
},
430+
enumerable: false,
431+
configurable: true,
443432
});
444433
}

lib/internal/bootstrap/pre_execution.js

-30
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const {
2323
exposeInterface,
2424
} = require('internal/util');
2525

26-
const { Buffer } = require('buffer');
2726
const {
2827
ERR_MANIFEST_ASSERT_INTEGRITY,
2928
} = require('internal/errors').codes;
@@ -408,35 +407,6 @@ function initializeDeprecations() {
408407
'process._tickCallback() is deprecated',
409408
'DEP0134');
410409
}
411-
412-
// Create global.process and global.Buffer as getters so that we have a
413-
// deprecation path for these in ES Modules.
414-
// See https://github.com/nodejs/node/pull/26334.
415-
let _process = process;
416-
ObjectDefineProperty(globalThis, 'process', {
417-
__proto__: null,
418-
get() {
419-
return _process;
420-
},
421-
set(value) {
422-
_process = value;
423-
},
424-
enumerable: false,
425-
configurable: true
426-
});
427-
428-
let _Buffer = Buffer;
429-
ObjectDefineProperty(globalThis, 'Buffer', {
430-
__proto__: null,
431-
get() {
432-
return _Buffer;
433-
},
434-
set(value) {
435-
_Buffer = value;
436-
},
437-
enumerable: false,
438-
configurable: true
439-
});
440410
}
441411

442412
function setupChildProcessIpcChannel() {

test/parallel/test-global.js

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ builtinModules.forEach((moduleName) => {
4949
'clearImmediate',
5050
'clearInterval',
5151
'clearTimeout',
52+
'atob',
53+
'btoa',
5254
'performance',
5355
'setImmediate',
5456
'setInterval',

0 commit comments

Comments
 (0)