Skip to content

Commit 53ebd33

Browse files
committed
process: global.process, global.Buffer getters
PR-URL: #26882 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 2d5387e commit 53ebd33

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/internal/bootstrap/pre_execution.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { getOptionValue } = require('internal/options');
4+
const { Buffer } = require('buffer');
45

56
function prepareMainThreadExecution() {
67
// Patch the process object with legacy properties and normalizations
@@ -221,6 +222,33 @@ function initializeDeprecations() {
221222
'process.binding() is deprecated. ' +
222223
'Please use public APIs instead.', 'DEP0111');
223224
}
225+
226+
// Create global.process and global.Buffer as getters so that we have a
227+
// deprecation path for these in ES Modules.
228+
// See https://github.com/nodejs/node/pull/26334.
229+
let _process = process;
230+
Object.defineProperty(global, 'process', {
231+
get() {
232+
return _process;
233+
},
234+
set(value) {
235+
_process = value;
236+
},
237+
enumerable: false,
238+
configurable: true
239+
});
240+
241+
let _Buffer = Buffer;
242+
Object.defineProperty(global, 'Buffer', {
243+
get() {
244+
return _Buffer;
245+
},
246+
set(value) {
247+
_Buffer = value;
248+
},
249+
enumerable: false,
250+
configurable: true
251+
});
224252
}
225253

226254
function setupChildProcessIpcChannel() {

test/parallel/test-global-setters.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable strict */
2+
require('../common');
3+
const assert = require('assert');
4+
const _process = require('process');
5+
const { Buffer: _Buffer } = require('buffer');
6+
7+
assert.strictEqual(process, _process);
8+
// eslint-disable-next-line no-global-assign
9+
process = 'asdf';
10+
assert.strictEqual(process, 'asdf');
11+
assert.strictEqual(global.process, 'asdf');
12+
global.process = _process;
13+
assert.strictEqual(process, _process);
14+
assert.strictEqual(
15+
typeof Object.getOwnPropertyDescriptor(global, 'process').get,
16+
'function');
17+
18+
assert.strictEqual(Buffer, _Buffer);
19+
// eslint-disable-next-line no-global-assign
20+
Buffer = 'asdf';
21+
assert.strictEqual(Buffer, 'asdf');
22+
assert.strictEqual(global.Buffer, 'asdf');
23+
global.Buffer = _Buffer;
24+
assert.strictEqual(Buffer, _Buffer);
25+
assert.strictEqual(
26+
typeof Object.getOwnPropertyDescriptor(global, 'Buffer').get,
27+
'function');

0 commit comments

Comments
 (0)