Skip to content

Commit 6266ae2

Browse files
authored
Fix #1282: correct globals in [stdin], [eval], and <repl> contexts (#1333)
* add failing tests * remove log statement from test * add detailed tests for globals in <repl>, [stdin], and [eval] * WIP fixing * more WIP * update tests * WIP * update packagelock * fix tests * Fix and tests * fix test failure on windows * fix programmatic test to cleanup potentially-polluted env prior * modifying programmatic repl test to call out that `module` is unavailable before repl is started * Update tests for Windows env * Fix tests * add retries around npm install in tests on windows * lintfix
1 parent 4f16d1b commit 6266ae2

10 files changed

+1347
-666
lines changed

dist-raw/node-cjs-helpers.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function addBuiltinLibsToObject(object: any): void;

dist-raw/node-cjs-helpers.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const {ArrayPrototypeForEach, StringPrototypeStartsWith, ObjectPrototypeHasOwnProperty, StringPrototypeIncludes, ObjectDefineProperty} = require('./node-primordials');
2+
3+
exports.addBuiltinLibsToObject = addBuiltinLibsToObject;
4+
5+
// Copied from https://github.com/nodejs/node/blob/21f5a56914a3b24ad77535ef369b93c6b1c11d18/lib/internal/modules/cjs/helpers.js#L133-L178
6+
function addBuiltinLibsToObject(object) {
7+
// Make built-in modules available directly (loaded lazily).
8+
const { builtinModules } = require('module').Module;
9+
ArrayPrototypeForEach(builtinModules, (name) => {
10+
// Neither add underscored modules, nor ones that contain slashes (e.g.,
11+
// 'fs/promises') or ones that are already defined.
12+
if (StringPrototypeStartsWith(name, '_') ||
13+
StringPrototypeIncludes(name, '/') ||
14+
ObjectPrototypeHasOwnProperty(object, name)) {
15+
return;
16+
}
17+
// Goals of this mechanism are:
18+
// - Lazy loading of built-in modules
19+
// - Having all built-in modules available as non-enumerable properties
20+
// - Allowing the user to re-assign these variables as if there were no
21+
// pre-existing globals with the same name.
22+
23+
const setReal = (val) => {
24+
// Deleting the property before re-assigning it disables the
25+
// getter/setter mechanism.
26+
delete object[name];
27+
object[name] = val;
28+
};
29+
30+
ObjectDefineProperty(object, name, {
31+
get: () => {
32+
const lib = require(name);
33+
34+
// Disable the current getter/setter and set up a new
35+
// non-enumerable property.
36+
delete object[name];
37+
ObjectDefineProperty(object, name, {
38+
get: () => lib,
39+
set: setReal,
40+
configurable: true,
41+
enumerable: false
42+
});
43+
44+
return lib;
45+
},
46+
set: setReal,
47+
configurable: true,
48+
enumerable: false
49+
});
50+
});
51+
}

dist-raw/node-primordials.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module.exports = {
22
ArrayIsArray: Array.isArray,
33
ArrayPrototypeJoin: (obj, separator) => Array.prototype.join.call(obj, separator),
44
ArrayPrototypeShift: (obj) => Array.prototype.shift.call(obj),
5+
ArrayPrototypeForEach: (arr, ...rest) => Array.prototype.forEach.apply(arr, rest),
56
JSONParse: JSON.parse,
67
JSONStringify: JSON.stringify,
78
ObjectFreeze: Object.freeze,
89
ObjectGetOwnPropertyNames: Object.getOwnPropertyNames,
10+
ObjectDefineProperty: Object.defineProperty,
911
ObjectPrototypeHasOwnProperty: (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop),
1012
RegExpPrototypeTest: (obj, string) => RegExp.prototype.test.call(obj, string),
1113
SafeMap: Map,
@@ -18,5 +20,5 @@ module.exports = {
1820
StringPrototypeSlice: (str, ...rest) => String.prototype.slice.apply(str, rest),
1921
StringPrototypeSplit: (str, ...rest) => String.prototype.split.apply(str, rest),
2022
StringPrototypeStartsWith: (str, ...rest) => String.prototype.startsWith.apply(str, rest),
21-
StringPrototypeSubstr: (str, ...rest) => String.prototype.substr.apply(str, rest)
23+
StringPrototypeSubstr: (str, ...rest) => String.prototype.substr.apply(str, rest),
2224
};

0 commit comments

Comments
 (0)