repl: add builtinModules

This adds an alias to `_builtinLibs` that is documented and should
as such also be accessed publicly. It does not contain any
underscored modules.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: https://github.com/nodejs/node/pull/33295
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
pull/33449/head
Ruben Bridgewater 2020-05-08 02:39:59 +02:00
parent a416692e93
commit 1acc14baf9
3 changed files with 28 additions and 3 deletions

View File

@ -540,6 +540,15 @@ by default. However, this is not the case when creating a REPL
programmatically. Use this method to initialize a history log file when working
with REPL instances programmatically.
## `repl.builtinModules`
<!-- YAML
added: REPLACEME
-->
* {string[]}
A list of the names of all Node.js modules, e.g., `'http'`.
## `repl.start([options])`
<!-- YAML
added: v0.1.91

View File

@ -86,7 +86,7 @@ const {
const { Console } = require('console');
const CJSModule = require('internal/modules/cjs/loader').Module;
let _builtinLibs = [...CJSModule.builtinModules]
.filter((e) => !e.startsWith('_'));
.filter((e) => !e.startsWith('_') && !e.includes('/'));
const domain = require('domain');
const debug = require('internal/util/debuglog').debuglog('repl');
const {
@ -1559,6 +1559,13 @@ module.exports = {
Recoverable
};
ObjectDefineProperty(module.exports, 'builtinModules', {
get: () => _builtinLibs,
set: (val) => _builtinLibs = val,
enumerable: true,
configurable: true
});
ObjectDefineProperty(module.exports, '_builtinLibs', {
get: pendingDeprecation ? deprecate(
() => _builtinLibs,

View File

@ -237,9 +237,18 @@ putIn.run(['.clear']);
testMe.complete('require(\'', common.mustCall(function(error, data) {
assert.strictEqual(error, null);
builtinModules.forEach((lib) => {
if (!lib.startsWith('_'))
assert(data[0].includes(lib), `${lib} not found`);
assert(
data[0].includes(lib) || lib.startsWith('_') || lib.includes('/'),
`${lib} not found`
);
});
const newModule = 'foobar';
assert(!builtinModules.includes(newModule));
repl.builtinModules.push(newModule);
testMe.complete('require(\'', common.mustCall((_, [modules]) => {
assert.strictEqual(data[0].length + 1, modules.length);
assert(modules.includes(newModule));
}));
}));
testMe.complete("require\t( 'n", common.mustCall(function(error, data) {