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: make global.process, global.Buffer getters #26882

Closed
wants to merge 2 commits 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
28 changes: 28 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { getOptionValue } = require('internal/options');
const { Buffer } = require('buffer');

function prepareMainThreadExecution() {
// Patch the process object with legacy properties and normalizations
Expand Down Expand Up @@ -221,6 +222,33 @@ function initializeDeprecations() {
'process.binding() is deprecated. ' +
'Please use public APIs instead.', 'DEP0111');
}

// 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;
guybedford marked this conversation as resolved.
Show resolved Hide resolved
Object.defineProperty(global, 'process', {
get() {
return _process;
},
set(value) {
_process = value;
},
enumerable: false,
configurable: true
});

let _Buffer = Buffer;
Object.defineProperty(global, 'Buffer', {
get() {
return _Buffer;
},
set(value) {
_Buffer = value;
},
enumerable: false,
configurable: true
});
}

function setupChildProcessIpcChannel() {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-global-setters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable strict */
require('../common');
const assert = require('assert');
const _process = require('process');
const { Buffer: _Buffer } = require('buffer');

assert.strictEqual(process, _process);
// eslint-disable-next-line no-global-assign
process = 'asdf';
assert.strictEqual(process, 'asdf');
assert.strictEqual(global.process, 'asdf');
global.process = _process;
assert.strictEqual(process, _process);
assert.strictEqual(
typeof Object.getOwnPropertyDescriptor(global, 'process').get,
'function');

assert.strictEqual(Buffer, _Buffer);
// eslint-disable-next-line no-global-assign
Buffer = 'asdf';
assert.strictEqual(Buffer, 'asdf');
assert.strictEqual(global.Buffer, 'asdf');
global.Buffer = _Buffer;
assert.strictEqual(Buffer, _Buffer);
guybedford marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(
typeof Object.getOwnPropertyDescriptor(global, 'Buffer').get,
'function');