Skip to content

Commit 69061dc

Browse files
BridgeARcodebytere
authored andcommitted
repl: replace hard coded core module list with actual list
This replaces the internally used hard coded Node.js core module list with the actual internal existent modules. That way all modules are automatically picked up instead of having to update the list manually. This currently only applies to the REPL and to the Node.js `eval` functionality (User passed `-e` or `--eval` arguments to Node without `-i` or `--interactive`). Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33282 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent d33dcf1 commit 69061dc

File tree

6 files changed

+22
-62
lines changed

6 files changed

+22
-62
lines changed

lib/internal/modules/cjs/helpers.js

+10-48
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectDefineProperty,
5+
ObjectPrototypeHasOwnProperty,
56
SafeMap,
67
} = primordials;
78
const {
@@ -109,55 +110,17 @@ function stripBOM(content) {
109110
return content;
110111
}
111112

112-
const builtinLibs = [
113-
'assert',
114-
'async_hooks',
115-
'buffer',
116-
'child_process',
117-
'cluster',
118-
'crypto',
119-
'dgram',
120-
'dns',
121-
'domain',
122-
'events',
123-
'fs',
124-
'http',
125-
'http2',
126-
'https',
127-
'net',
128-
'os',
129-
'path',
130-
'perf_hooks',
131-
'punycode',
132-
'querystring',
133-
'readline',
134-
'repl',
135-
'stream',
136-
'string_decoder',
137-
'tls',
138-
'trace_events',
139-
'tty',
140-
'url',
141-
'util',
142-
'v8',
143-
'vm',
144-
'worker_threads',
145-
'zlib',
146-
];
147-
148-
if (internalBinding('config').experimentalWasi) {
149-
builtinLibs.push('wasi');
150-
builtinLibs.sort();
151-
}
152-
153-
if (typeof internalBinding('inspector').open === 'function') {
154-
builtinLibs.push('inspector');
155-
builtinLibs.sort();
156-
}
157-
158113
function addBuiltinLibsToObject(object) {
159114
// Make built-in modules available directly (loaded lazily).
160-
builtinLibs.forEach((name) => {
115+
const { builtinModules } = require('internal/modules/cjs/loader').Module;
116+
builtinModules.forEach((name) => {
117+
// Neither add underscored modules, nor ones that contain slashes (e.g.,
118+
// 'fs/promises') or ones that are already defined.
119+
if (name.startsWith('_') ||
120+
name.includes('/') ||
121+
ObjectPrototypeHasOwnProperty(object, name)) {
122+
return;
123+
}
161124
// Goals of this mechanism are:
162125
// - Lazy loading of built-in modules
163126
// - Having all built-in modules available as non-enumerable properties
@@ -203,7 +166,6 @@ function normalizeReferrerURL(referrer) {
203166

204167
module.exports = {
205168
addBuiltinLibsToObject,
206-
builtinLibs,
207169
loadNativeModule,
208170
makeRequireFunction,
209171
normalizeReferrerURL,

lib/repl.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const {
6363
} = primordials;
6464

6565
const {
66-
builtinLibs,
6766
makeRequireFunction,
6867
addBuiltinLibsToObject
6968
} = require('internal/modules/cjs/helpers');
@@ -86,6 +85,8 @@ const {
8685
} = require('internal/readline/utils');
8786
const { Console } = require('console');
8887
const CJSModule = require('internal/modules/cjs/loader').Module;
88+
const builtinModules = [...CJSModule.builtinModules]
89+
.filter((e) => !e.startsWith('_'));
8990
const domain = require('domain');
9091
const debug = require('internal/util/debuglog').debuglog('repl');
9192
const {
@@ -158,7 +159,7 @@ module.paths = CJSModule._nodeModulePaths(module.filename);
158159
const writer = exports.writer = (obj) => inspect(obj, writer.options);
159160
writer.options = { ...inspect.defaultOptions, showProxy: true };
160161

161-
exports._builtinLibs = builtinLibs;
162+
exports._builtinLibs = builtinModules;
162163

163164
function REPLServer(prompt,
164165
stream,

test/parallel/test-bootstrap-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const expectedModules = new Set([
1717
'Internal Binding credentials',
1818
'Internal Binding fs',
1919
'Internal Binding fs_dir',
20-
'Internal Binding inspector',
2120
'Internal Binding module_wrap',
2221
'Internal Binding native_module',
2322
'Internal Binding options',
@@ -116,6 +115,7 @@ if (common.hasIntl) {
116115
}
117116

118117
if (process.features.inspector) {
118+
expectedModules.add('Internal Binding inspector');
119119
expectedModules.add('NativeModule internal/inspector_async_hook');
120120
expectedModules.add('NativeModule internal/util/inspector');
121121
}

test/parallel/test-module-cjs-helpers.js

-9
This file was deleted.

test/parallel/test-repl-mode.js

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function testStrictMode() {
3939
}
4040

4141
function testStrictModeTerminal() {
42+
if (!process.features.inspector) {
43+
console.warn('Test skipped: V8 inspector is disabled');
44+
return;
45+
}
4246
// Verify that ReferenceErrors are reported in strict mode previews.
4347
const cli = initRepl(repl.REPL_MODE_STRICT, {
4448
terminal: true

test/parallel/test-repl-tab-complete.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
const assert = require('assert');
3131
const path = require('path');
3232
const fixtures = require('../common/fixtures');
33+
const { builtinModules } = require('module');
3334
const hasInspector = process.features.inspector;
3435

3536
if (!common.isMainThread)
@@ -222,8 +223,9 @@ putIn.run(['.clear']);
222223

223224
testMe.complete('require(\'', common.mustCall(function(error, data) {
224225
assert.strictEqual(error, null);
225-
repl._builtinLibs.forEach(function(lib) {
226-
assert(data[0].includes(lib), `${lib} not found`);
226+
builtinModules.forEach((lib) => {
227+
if (!lib.startsWith('_'))
228+
assert(data[0].includes(lib), `${lib} not found`);
227229
});
228230
}));
229231

0 commit comments

Comments
 (0)