Skip to content

Commit

Permalink
Fix #1282: correct globals in [stdin], [eval], and <repl> contexts (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
cspotcode committed Jun 8, 2021
1 parent 4f16d1b commit 6266ae2
Show file tree
Hide file tree
Showing 10 changed files with 1,347 additions and 666 deletions.
1 change: 1 addition & 0 deletions dist-raw/node-cjs-helpers.d.ts
@@ -0,0 +1 @@
export function addBuiltinLibsToObject(object: any): void;
51 changes: 51 additions & 0 deletions dist-raw/node-cjs-helpers.js
@@ -0,0 +1,51 @@
const {ArrayPrototypeForEach, StringPrototypeStartsWith, ObjectPrototypeHasOwnProperty, StringPrototypeIncludes, ObjectDefineProperty} = require('./node-primordials');

exports.addBuiltinLibsToObject = addBuiltinLibsToObject;

// Copied from https://github.com/nodejs/node/blob/21f5a56914a3b24ad77535ef369b93c6b1c11d18/lib/internal/modules/cjs/helpers.js#L133-L178
function addBuiltinLibsToObject(object) {
// Make built-in modules available directly (loaded lazily).
const { builtinModules } = require('module').Module;
ArrayPrototypeForEach(builtinModules, (name) => {
// Neither add underscored modules, nor ones that contain slashes (e.g.,
// 'fs/promises') or ones that are already defined.
if (StringPrototypeStartsWith(name, '_') ||
StringPrototypeIncludes(name, '/') ||
ObjectPrototypeHasOwnProperty(object, name)) {
return;
}
// Goals of this mechanism are:
// - Lazy loading of built-in modules
// - Having all built-in modules available as non-enumerable properties
// - Allowing the user to re-assign these variables as if there were no
// pre-existing globals with the same name.

const setReal = (val) => {
// Deleting the property before re-assigning it disables the
// getter/setter mechanism.
delete object[name];
object[name] = val;
};

ObjectDefineProperty(object, name, {
get: () => {
const lib = require(name);

// Disable the current getter/setter and set up a new
// non-enumerable property.
delete object[name];
ObjectDefineProperty(object, name, {
get: () => lib,
set: setReal,
configurable: true,
enumerable: false
});

return lib;
},
set: setReal,
configurable: true,
enumerable: false
});
});
}
4 changes: 3 additions & 1 deletion dist-raw/node-primordials.js
Expand Up @@ -2,10 +2,12 @@ module.exports = {
ArrayIsArray: Array.isArray,
ArrayPrototypeJoin: (obj, separator) => Array.prototype.join.call(obj, separator),
ArrayPrototypeShift: (obj) => Array.prototype.shift.call(obj),
ArrayPrototypeForEach: (arr, ...rest) => Array.prototype.forEach.apply(arr, rest),
JSONParse: JSON.parse,
JSONStringify: JSON.stringify,
ObjectFreeze: Object.freeze,
ObjectGetOwnPropertyNames: Object.getOwnPropertyNames,
ObjectDefineProperty: Object.defineProperty,
ObjectPrototypeHasOwnProperty: (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop),
RegExpPrototypeTest: (obj, string) => RegExp.prototype.test.call(obj, string),
SafeMap: Map,
Expand All @@ -18,5 +20,5 @@ module.exports = {
StringPrototypeSlice: (str, ...rest) => String.prototype.slice.apply(str, rest),
StringPrototypeSplit: (str, ...rest) => String.prototype.split.apply(str, rest),
StringPrototypeStartsWith: (str, ...rest) => String.prototype.startsWith.apply(str, rest),
StringPrototypeSubstr: (str, ...rest) => String.prototype.substr.apply(str, rest)
StringPrototypeSubstr: (str, ...rest) => String.prototype.substr.apply(str, rest),
};

0 comments on commit 6266ae2

Please sign in to comment.