From 511e289ea868b4c5ea55d71a826a3fa012a6fe2e Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Thu, 9 Jun 2022 03:04:23 +0000 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/43357 Refs: https://github.com/nodejs/node/pull/26882 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Joyee Cheung --- lib/internal/bootstrap/browser.js | 7 +++ lib/internal/bootstrap/node.js | 59 ++++++++++--------------- lib/internal/bootstrap/pre_execution.js | 30 ------------- test/parallel/test-global.js | 2 + 4 files changed, 33 insertions(+), 65 deletions(-) diff --git a/lib/internal/bootstrap/browser.js b/lib/internal/bootstrap/browser.js index 92c98a7fa9cc55..4df2fa0a495749 100644 --- a/lib/internal/bootstrap/browser.js +++ b/lib/internal/bootstrap/browser.js @@ -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); diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index e58538b34cb17e..0f72e3e67af306 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -45,7 +45,6 @@ const { FunctionPrototypeCall, JSONParse, ObjectDefineProperty, - ObjectDefineProperties, ObjectGetPrototypeOf, ObjectPreventExtensions, ObjectSetPrototypeOf, @@ -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, }); } @@ -399,10 +406,7 @@ function setupGlobalProxy() { function setupBuffer() { const { - Blob, Buffer, - atob, - btoa, } = require('buffer'); const bufferBinding = internalBinding('buffer'); @@ -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, }); } diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index dc8cb637870744..d00c1fbd7864de 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -23,7 +23,6 @@ const { exposeInterface, } = require('internal/util'); -const { Buffer } = require('buffer'); const { ERR_MANIFEST_ASSERT_INTEGRITY, } = require('internal/errors').codes; @@ -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() { diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index 3585062bf310e5..5437751cbcc520 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -49,6 +49,8 @@ builtinModules.forEach((moduleName) => { 'clearImmediate', 'clearInterval', 'clearTimeout', + 'atob', + 'btoa', 'performance', 'setImmediate', 'setInterval',