-
-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
10 changed files
with
1,347 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export function addBuiltinLibsToObject(object: any): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.