bootstrap: make Buffer and process non-enumerable · nodejs/node@c992639 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -408,7 +408,12 @@ function setupGlobalVariables() {
408 408 enumerable: false,
409 409 configurable: true
410 410 });
411 -global.process = process;
411 +Object.defineProperty(global, 'process', {
412 +value: process,
413 +enumerable: false,
414 +writable: true,
415 +configurable: true
416 +});
412 417 const util = NativeModule.require('util');
413 418
414 419 function makeGetter(name) {
@@ -445,7 +450,12 @@ function setupGlobalVariables() {
445 450 // and exposes it on `internal/buffer`.
446 451 NativeModule.require('internal/buffer');
447 452
448 -global.Buffer = NativeModule.require('buffer').Buffer;
453 +Object.defineProperty(global, 'Buffer', {
454 +value: NativeModule.require('buffer').Buffer,
455 +enumerable: false,
456 +writable: true,
457 +configurable: true
458 +});
449 459 process.domain = null;
450 460 process._exiting = false;
451 461 }
Original file line number Diff line number Diff line change
@@ -217,12 +217,10 @@ function platformTimeout(ms) {
217 217 }
218 218
219 219 let knownGlobals = [
220 -Buffer,
221 220 clearImmediate,
222 221 clearInterval,
223 222 clearTimeout,
224 223 global,
225 -process,
226 224 setImmediate,
227 225 setInterval,
228 226 setTimeout
Original file line number Diff line number Diff line change
@@ -27,6 +27,41 @@ const common = require('../common');
27 27 const fixtures = require('../common/fixtures');
28 28
29 29 const assert = require('assert');
30 +const { builtinModules } = require('module');
31 +
32 +// Load all modules to actually cover most code parts.
33 +builtinModules.forEach((moduleName) => {
34 +if (!moduleName.includes('/')) {
35 +try {
36 +// This could throw for e.g., crypto if the binary is not compiled
37 +// accordingly.
38 +require(moduleName);
39 +} catch {}
40 +}
41 +});
42 +
43 +{
44 +const expected = [
45 +'global',
46 +'clearImmediate',
47 +'clearInterval',
48 +'clearTimeout',
49 +'setImmediate',
50 +'setInterval',
51 +'setTimeout'
52 +];
53 +if (global.DTRACE_HTTP_SERVER_RESPONSE) {
54 +expected.unshift(
55 +'DTRACE_HTTP_SERVER_RESPONSE',
56 +'DTRACE_HTTP_SERVER_REQUEST',
57 +'DTRACE_HTTP_CLIENT_RESPONSE',
58 +'DTRACE_HTTP_CLIENT_REQUEST',
59 +'DTRACE_NET_STREAM_END',
60 +'DTRACE_NET_SERVER_CONNECTION'
61 +);
62 +}
63 +assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
64 +}
30 65
31 66 common.allowGlobals('bar', 'foo');
32 67