mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
lib,repl: ignore non-canBeRequiredByUsers built-in
e.g. `wasi` under no `--experimental-wasi-unstable-preview1` flag shouldn't be pre-required. PR-URL: https://github.com/nodejs/node/pull/39942 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
This commit is contained in:
parent
8d5ed107d6
commit
c7222b3589
@ -16,7 +16,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
|
||||
const { getOptionValue } = require('internal/options');
|
||||
|
||||
prepareMainThreadExecution();
|
||||
addBuiltinLibsToObject(globalThis);
|
||||
addBuiltinLibsToObject(globalThis, '<eval>');
|
||||
markBootstrapComplete();
|
||||
|
||||
const source = getOptionValue('--eval');
|
||||
|
@ -131,9 +131,16 @@ function stripBOM(content) {
|
||||
return content;
|
||||
}
|
||||
|
||||
function addBuiltinLibsToObject(object) {
|
||||
function addBuiltinLibsToObject(object, dummyModuleName) {
|
||||
// Make built-in modules available directly (loaded lazily).
|
||||
const { builtinModules } = require('internal/modules/cjs/loader').Module;
|
||||
const Module = require('internal/modules/cjs/loader').Module;
|
||||
const { builtinModules } = Module;
|
||||
|
||||
// To require built-in modules in user-land and ignore modules whose
|
||||
// `canBeRequiredByUsers` is false. So we create a dummy module object and not
|
||||
// use `require()` directly.
|
||||
const dummyModule = new Module(dummyModuleName);
|
||||
|
||||
ArrayPrototypeForEach(builtinModules, (name) => {
|
||||
// Neither add underscored modules, nor ones that contain slashes (e.g.,
|
||||
// 'fs/promises') or ones that are already defined.
|
||||
@ -157,7 +164,7 @@ function addBuiltinLibsToObject(object) {
|
||||
|
||||
ObjectDefineProperty(object, name, {
|
||||
get: () => {
|
||||
const lib = require(name);
|
||||
const lib = dummyModule.require(name);
|
||||
|
||||
// Disable the current getter/setter and set up a new
|
||||
// non-enumerable property.
|
||||
|
@ -1098,7 +1098,7 @@ REPLServer.prototype.createContext = function() {
|
||||
value: makeRequireFunction(replModule)
|
||||
});
|
||||
|
||||
addBuiltinLibsToObject(context);
|
||||
addBuiltinLibsToObject(context, '<REPL>');
|
||||
|
||||
return context;
|
||||
};
|
||||
|
32
test/parallel/test-repl-built-in-modules.js
Normal file
32
test/parallel/test-repl-built-in-modules.js
Normal file
@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const cp = require('child_process');
|
||||
|
||||
function runREPLWithAdditionalFlags(flags) {
|
||||
// Use -i to force node into interactive mode, despite stdout not being a TTY
|
||||
const args = ['-i'].concat(flags);
|
||||
const ret = cp.execFileSync(process.execPath, args, {
|
||||
input: 'require(\'events\');\nrequire(\'wasi\');',
|
||||
encoding: 'utf8',
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Run REPL in normal mode.
|
||||
let stdout = runREPLWithAdditionalFlags([]);
|
||||
assert.match(stdout, /\[Function: EventEmitter\] {/);
|
||||
assert.match(
|
||||
stdout,
|
||||
/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/);
|
||||
|
||||
// Run REPL with '--experimental-wasi-unstable-preview1'
|
||||
stdout = runREPLWithAdditionalFlags([
|
||||
'--experimental-wasi-unstable-preview1',
|
||||
]);
|
||||
assert.match(stdout, /\[Function: EventEmitter\] {/);
|
||||
assert.doesNotMatch(
|
||||
stdout,
|
||||
/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/);
|
||||
assert.match(stdout, /{ WASI: \[class WASI\] }/);
|
Loading…
Reference in New Issue
Block a user