Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib,repl: ignore canBeRequiredByUsers built-in #39942

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/main/eval_string.js
Expand Up @@ -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');
Expand Down
13 changes: 10 additions & 3 deletions lib/internal/modules/cjs/helpers.js
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Expand Up @@ -1098,7 +1098,7 @@ REPLServer.prototype.createContext = function() {
value: makeRequireFunction(replModule)
});

addBuiltinLibsToObject(context);
addBuiltinLibsToObject(context, '<REPL>');

return context;
};
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-repl-built-in-modules.js
@@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this function seems to emulate what execFile does (promisified if a Promise is really necessary here).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


// 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\] }/);