From 9d6291ad46a608232da971bcf4554087da5e1f09 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 10 Feb 2019 17:58:41 +0800 Subject: [PATCH] process: refactor lib/internal/bootstrap/node.js - Use `deprecate` from `internal/util` instead of from `util` - Split `setupGlobalVariables()` into `setupGlobalProxy()` and `setupBuffer()`, and move out manipulation of the process object. PR-URL: https://github.com/nodejs/node/pull/26033 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Backport-PR-URL: https://github.com/nodejs/node/pull/26085 --- lib/internal/bootstrap/node.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 9ee28d24367457..ca9d6c60cf3868 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -40,6 +40,7 @@ const { internalBinding, NativeModule } = loaderExports; const { Object, Symbol } = primordials; const { getOptionValue } = NativeModule.require('internal/options'); const config = internalBinding('config'); +const { deprecate } = NativeModule.require('internal/util'); setupTraceCategoryState(); @@ -63,7 +64,11 @@ setupProcessObject(); hasUncaughtExceptionCaptureCallback; } -setupGlobalVariables(); +setupGlobalProxy(); +setupBuffer(); + +process.domain = null; +process._exiting = false; // Bootstrappers for all threads, including worker threads and main thread const perThreadSetup = NativeModule.require('internal/process/per_thread'); @@ -228,7 +233,6 @@ if (process._invalidDebug) { 'DeprecationWarning', 'DEP0062', undefined, true); } -const { deprecate } = NativeModule.require('internal/util'); // TODO(jasnell): The following have been globals since around 2012. // That's just silly. The underlying perfctr support has been removed // so these are now deprecated non-ops that can be removed after one @@ -388,6 +392,8 @@ function setupProcessObject() { const origProcProto = Object.getPrototypeOf(process); Object.setPrototypeOf(origProcProto, EventEmitter.prototype); EventEmitter.call(process); + // Make process globally available to users by putting it on the global proxy + global.process = process; } function setupProcessStdio(getStdout, getStdin, getStderr) { @@ -415,24 +421,22 @@ function setupProcessStdio(getStdout, getStdin, getStderr) { }; } -function setupGlobalVariables() { +function setupGlobalProxy() { Object.defineProperty(global, Symbol.toStringTag, { value: 'global', writable: false, enumerable: false, configurable: true }); - global.process = process; - const util = NativeModule.require('util'); function makeGetter(name) { - return util.deprecate(function() { + return deprecate(function() { return this; }, `'${name}' is deprecated, use 'global'`, 'DEP0016'); } function makeSetter(name) { - return util.deprecate(function(value) { + return deprecate(function(value) { Object.defineProperty(this, name, { configurable: true, writable: true, @@ -454,7 +458,9 @@ function setupGlobalVariables() { set: makeSetter('root') } }); +} +function setupBuffer() { const { Buffer } = NativeModule.require('buffer'); const bufferBinding = internalBinding('buffer'); @@ -464,8 +470,6 @@ function setupGlobalVariables() { delete bufferBinding.zeroFill; global.Buffer = Buffer; - process.domain = null; - process._exiting = false; } function setupGlobalTimeouts() {