Skip to content

Commit

Permalink
process: global.process, global.Buffer getters
Browse files Browse the repository at this point in the history
PR-URL: #26882
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
guybedford committed Mar 26, 2019
1 parent 2d5387e commit 53ebd33
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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;
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);
assert.strictEqual(
typeof Object.getOwnPropertyDescriptor(global, 'Buffer').get,
'function');

0 comments on commit 53ebd33

Please sign in to comment.