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

module: add isBuiltin method #43396

Merged
merged 24 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ const require = createRequire(import.meta.url);
const siblingModule = require('./sibling-module');
```

### `module.isBuiltin(moduleName)`

<!-- YAML
added: REPLACEME
-->

* `moduleName` {string} name of the module
* Returns: {boolean} returns true if the module is builtin else returns false

```mjs
import { isBuiltin } from 'node:module';
isBuiltin('node:fs'); // true
isBuiltin('fs'); // true
isBuiltin('wss'); // false
```

### `module.syncBuiltinESMExports()`

<!-- YAML
Expand Down
11 changes: 11 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
ArrayPrototypeSplice,
ArrayPrototypeUnshift,
ArrayPrototypeUnshiftApply,
ArrayPrototypeFlatMap,
Boolean,
Error,
JSONParse,
Expand All @@ -51,6 +52,7 @@ const {
RegExpPrototypeExec,
RegExpPrototypeTest,
SafeMap,
SafeSet,
SafeWeakMap,
String,
StringPrototypeCharAt,
Expand Down Expand Up @@ -188,6 +190,11 @@ for (const { 0: id, 1: mod } of NativeModule.map) {
}
}

const allBuiltins = new SafeSet(
hemanth marked this conversation as resolved.
Show resolved Hide resolved
ArrayPrototypeFlatMap(builtinModules, (bm) => [bm, `node:${bm}`])
);
NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`));

ObjectFreeze(builtinModules);
Module.builtinModules = builtinModules;

Expand Down Expand Up @@ -1293,5 +1300,9 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
}
};

Module.isBuiltin = function isBuiltin(moduleName) {
return allBuiltins.has(moduleName);
};

// Backwards compatibility
Module.Module = Module;
16 changes: 16 additions & 0 deletions test/parallel/test-module-isBuiltin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
require('../common');
const assert = require('assert');
const { isBuiltin } = require('module');

// Includes modules in lib/ (even deprecated ones)
assert(isBuiltin('http'));
assert(isBuiltin('sys'));
assert(isBuiltin('node:fs'));
assert(isBuiltin('node:test'));

// Does not include internal modules
assert(!isBuiltin('internal/errors'));
assert(!isBuiltin('test'));
assert(!isBuiltin(''));
assert(!isBuiltin(undefined));