From ec1f95aa30323f0c5b232ebca799d42db9506bd4 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 00:32:00 +0000 Subject: [PATCH 01/24] module: add isBuiltIn method --- lib/internal/modules/cjs/loader.js | 5 +++++ test/parallel/test-module-isBuiltIn.js | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test/parallel/test-module-isBuiltIn.js diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b7fc3f9b2a1ea5..6b023c9f13fdd8 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1293,5 +1293,10 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { } }; +Module.isBuiltIn = function isBuiltIn(moduleName) { + moduleName = String(moduleName).replace('node:', ''); + return Module.builtinModules.includes(moduleName); +}; + // Backwards compatibility Module.Module = Module; diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js new file mode 100644 index 00000000000000..e2514161333c2c --- /dev/null +++ b/test/parallel/test-module-isBuiltIn.js @@ -0,0 +1,13 @@ +'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')); + +// Does not include internal modules +assert(!isBuiltIn('internal')); +assert(!isBuiltIn('')); +assert(!isBuiltIn(undefined)); From 2376045b496528dad93ec859d67bd096a8ea12b3 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 12:00:19 -0700 Subject: [PATCH 02/24] Use regex instead Co-authored-by: Jordan Harband --- lib/internal/modules/cjs/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 6b023c9f13fdd8..d3360501975931 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1294,7 +1294,7 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { }; Module.isBuiltIn = function isBuiltIn(moduleName) { - moduleName = String(moduleName).replace('node:', ''); + moduleName = StringPrototypeReplace(moduleName, /^node:/, ''); return Module.builtinModules.includes(moduleName); }; From 06c135aeb7e6ddb8780327319b2c5778a675b0d3 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 12:03:29 -0700 Subject: [PATCH 03/24] using primordial instead Co-authored-by: Jordan Harband --- lib/internal/modules/cjs/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index d3360501975931..c3d43e80edb04e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1295,7 +1295,7 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { Module.isBuiltIn = function isBuiltIn(moduleName) { moduleName = StringPrototypeReplace(moduleName, /^node:/, ''); - return Module.builtinModules.includes(moduleName); + return ArrayPrototypeIncludes(Module.builtinModules, moduleName); }; // Backwards compatibility From 62a42804e72addc289c05ce03d46af7aa1b9984c Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 22:24:38 +0000 Subject: [PATCH 04/24] importing StringPrototypeReplace --- lib/internal/modules/cjs/loader.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index c3d43e80edb04e..32ccc46768b8d1 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -60,6 +60,7 @@ const { StringPrototypeIndexOf, StringPrototypeMatch, StringPrototypeRepeat, + StringPrototypeReplace, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, From 57f56861a67a16c78dda50fa2f9a582c6f2f5fbf Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Tue, 14 Jun 2022 23:40:39 +0000 Subject: [PATCH 05/24] doc: add isBuiltIn --- doc/api/module.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/api/module.md b/doc/api/module.md index 85ba9a79e2291d..8ba7cc227376b5 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -62,6 +62,22 @@ const require = createRequire(import.meta.url); const siblingModule = require('./sibling-module'); ``` +### `module.isBuiltIn(moduleName)` + + + +* `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()` * `moduleName` {string} name of the module From 7a08b8f13ba4c5a93b92052ce0dc7a9febe032de Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 16 Jun 2022 04:28:51 -0700 Subject: [PATCH 08/24] prefix to tests Co-authored-by: mscdex --- test/parallel/test-module-isBuiltIn.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js index e2514161333c2c..ff8285b0f0147e 100644 --- a/test/parallel/test-module-isBuiltIn.js +++ b/test/parallel/test-module-isBuiltIn.js @@ -6,6 +6,8 @@ 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')); From 8c78587a80aa2820fd13546043e5f8ecbab85076 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 16 Jun 2022 04:29:19 -0700 Subject: [PATCH 09/24] avoid reassigning function args Co-authored-by: Jordan Harband --- lib/internal/modules/cjs/loader.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 072bcd4d174e3c..3f8f1d8fc786de 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1295,8 +1295,10 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { }; Module.isBuiltIn = function isBuiltIn(moduleName) { - moduleName = StringPrototypeReplace(String(moduleName), /^node:/, ''); - return ArrayPrototypeIncludes(Module.builtinModules, moduleName); + return ArrayPrototypeIncludes( + Module.builtinModules, + StringPrototypeReplace(String(moduleName), /^node:/, '') + ); }; // Backwards compatibility From cfc1a07173595ee8d7cbe32de058c0035d31b862 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 16 Jun 2022 20:14:02 +0000 Subject: [PATCH 10/24] fix: falsy value assertion --- test/parallel/test-module-isBuiltIn.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js index ff8285b0f0147e..30beae028a243c 100644 --- a/test/parallel/test-module-isBuiltIn.js +++ b/test/parallel/test-module-isBuiltIn.js @@ -7,7 +7,7 @@ const { isBuiltIn } = require('module'); assert(isBuiltIn('http')); assert(isBuiltIn('sys')); assert(isBuiltIn('node:fs')); -assert(isBuiltIn('node:test')); +assert(!isBuiltIn('node:test')); // Does not include internal modules assert(!isBuiltIn('internal')); From 8ab8589827919a998e531082f480afbc77f27663 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sun, 19 Jun 2022 00:31:14 +0000 Subject: [PATCH 11/24] fix: also support scheme only modules --- lib/internal/modules/cjs/loader.js | 3 ++- test/parallel/test-module-isBuiltIn.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 3f8f1d8fc786de..cf2684389cc3a1 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -140,6 +140,7 @@ const { packageExportsResolve, packageImportsResolve } = require('internal/modules/esm/resolve'); +const console = require('console'); const isWindows = process.platform === 'win32'; @@ -1296,7 +1297,7 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { Module.isBuiltIn = function isBuiltIn(moduleName) { return ArrayPrototypeIncludes( - Module.builtinModules, + [...Module.builtinModules,...NativeModule.getSchemeOnlyModuleNames()], StringPrototypeReplace(String(moduleName), /^node:/, '') ); }; diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js index 30beae028a243c..ff8285b0f0147e 100644 --- a/test/parallel/test-module-isBuiltIn.js +++ b/test/parallel/test-module-isBuiltIn.js @@ -7,7 +7,7 @@ const { isBuiltIn } = require('module'); assert(isBuiltIn('http')); assert(isBuiltIn('sys')); assert(isBuiltIn('node:fs')); -assert(!isBuiltIn('node:test')); +assert(isBuiltIn('node:test')); // Does not include internal modules assert(!isBuiltIn('internal')); From deb9010cffc0ec1528bfe8fb24bb5c24e1fb341e Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sat, 18 Jun 2022 17:44:44 -0700 Subject: [PATCH 12/24] removed auto imported console. Co-authored-by: mscdex --- lib/internal/modules/cjs/loader.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index cf2684389cc3a1..fd67ce50f45712 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -140,7 +140,6 @@ const { packageExportsResolve, packageImportsResolve } = require('internal/modules/esm/resolve'); -const console = require('console'); const isWindows = process.platform === 'win32'; From 420c8ebe24c8011d83b60b4f2202364c92e0d63c Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sun, 19 Jun 2022 01:18:59 +0000 Subject: [PATCH 13/24] fix: prefix only module checks --- lib/internal/modules/cjs/loader.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index fd67ce50f45712..85233bf1f8649c 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1295,10 +1295,13 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { }; Module.isBuiltIn = function isBuiltIn(moduleName) { - return ArrayPrototypeIncludes( - [...Module.builtinModules,...NativeModule.getSchemeOnlyModuleNames()], + return (ArrayPrototypeIncludes( + Module.builtinModules, StringPrototypeReplace(String(moduleName), /^node:/, '') - ); + )) || (StringPrototypeStartsWith(String(moduleName), 'node:') && ArrayPrototypeIncludes( + NativeModule.getSchemeOnlyModuleNames(), + StringPrototypeReplace(String(moduleName), /^node:/, '') + )); }; // Backwards compatibility From 4bab0853bfd628e1e75bea8dbbf980977f0840ca Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Sat, 18 Jun 2022 22:00:50 -0700 Subject: [PATCH 14/24] proper check for prefixes Co-authored-by: mscdex --- test/parallel/test-module-isBuiltIn.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js index ff8285b0f0147e..2005fd5aeb56c6 100644 --- a/test/parallel/test-module-isBuiltIn.js +++ b/test/parallel/test-module-isBuiltIn.js @@ -11,5 +11,6 @@ assert(isBuiltIn('node:test')); // Does not include internal modules assert(!isBuiltIn('internal')); +assert(!isBuiltIn('test')); assert(!isBuiltIn('')); assert(!isBuiltIn(undefined)); From 9a4fe4a5664aa860529274770b5db68105d789bf Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Mon, 20 Jun 2022 21:59:29 -0700 Subject: [PATCH 15/24] Update lib/internal/modules/cjs/loader.js Co-authored-by: Jordan Harband --- lib/internal/modules/cjs/loader.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 85233bf1f8649c..886a2943d2ac40 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1295,12 +1295,15 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { }; Module.isBuiltIn = function isBuiltIn(moduleName) { + if (typeof moduleName !== 'string') { + return false; + } return (ArrayPrototypeIncludes( Module.builtinModules, - StringPrototypeReplace(String(moduleName), /^node:/, '') - )) || (StringPrototypeStartsWith(String(moduleName), 'node:') && ArrayPrototypeIncludes( + StringPrototypeReplace(moduleName, /^node:/, '') + )) || (StringPrototypeStartsWith(moduleName, 'node:') && ArrayPrototypeIncludes( NativeModule.getSchemeOnlyModuleNames(), - StringPrototypeReplace(String(moduleName), /^node:/, '') + StringPrototypeReplace(moduleName, /^node:/, '') )); }; From 3b2cd570a7fca916675daa1adb748b330dbaeaea Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Wed, 22 Jun 2022 21:28:50 -0700 Subject: [PATCH 16/24] Update test/parallel/test-module-isBuiltIn.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michaƫl Zasso --- test/parallel/test-module-isBuiltIn.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js index 2005fd5aeb56c6..aceaae658afd75 100644 --- a/test/parallel/test-module-isBuiltIn.js +++ b/test/parallel/test-module-isBuiltIn.js @@ -10,7 +10,7 @@ assert(isBuiltIn('node:fs')); assert(isBuiltIn('node:test')); // Does not include internal modules -assert(!isBuiltIn('internal')); +assert(!isBuiltIn('internal/errors')); assert(!isBuiltIn('test')); assert(!isBuiltIn('')); assert(!isBuiltIn(undefined)); From eafd1316cd52df5b9efe8eb298ad2a03f94c376f Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 07:01:30 +0000 Subject: [PATCH 17/24] fix: using Set for better perf --- lib/internal/modules/cjs/loader.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 886a2943d2ac40..923a455ddf32b2 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -51,6 +51,7 @@ const { RegExpPrototypeExec, RegExpPrototypeTest, SafeMap, + SafeSet, SafeWeakMap, String, StringPrototypeCharAt, @@ -60,7 +61,6 @@ const { StringPrototypeIndexOf, StringPrototypeMatch, StringPrototypeRepeat, - StringPrototypeReplace, StringPrototypeSlice, StringPrototypeSplit, StringPrototypeStartsWith, @@ -189,6 +189,11 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } } +let allBuiltins = builtinModules; +builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); +allBuiltins = new SafeSet(allBuiltins); +NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); + ObjectFreeze(builtinModules); Module.builtinModules = builtinModules; @@ -1295,16 +1300,7 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { }; Module.isBuiltIn = function isBuiltIn(moduleName) { - if (typeof moduleName !== 'string') { - return false; - } - return (ArrayPrototypeIncludes( - Module.builtinModules, - StringPrototypeReplace(moduleName, /^node:/, '') - )) || (StringPrototypeStartsWith(moduleName, 'node:') && ArrayPrototypeIncludes( - NativeModule.getSchemeOnlyModuleNames(), - StringPrototypeReplace(moduleName, /^node:/, '') - )); + return allBuiltins.has(moduleName); }; // Backwards compatibility From 1e81d07310f50c858b3792b09d56899e892e0a72 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 11:50:49 +0000 Subject: [PATCH 18/24] fix: avoid mutation --- lib/internal/modules/cjs/loader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 923a455ddf32b2..764efa3b1c1f69 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -189,8 +189,8 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } } -let allBuiltins = builtinModules; -builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); +let allBuiltins = []; +builtinModules.forEach((builtin) => allBuiltins.push(builtin) && allBuiltins.push(`node:${builtin}`)); allBuiltins = new SafeSet(allBuiltins); NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); From 0910fe0f3ec848ef299189b3dd29d99759b0137f Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 05:49:26 -0700 Subject: [PATCH 19/24] use slice rather Co-authored-by: mscdex --- lib/internal/modules/cjs/loader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 764efa3b1c1f69..96b3555e6dde35 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -189,8 +189,8 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } } -let allBuiltins = []; -builtinModules.forEach((builtin) => allBuiltins.push(builtin) && allBuiltins.push(`node:${builtin}`)); +let allBuiltins = builtinModules.slice(); +allBuiltins = builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); allBuiltins = new SafeSet(allBuiltins); NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); From 1e7b74f7976c636f3ebe5e4af75f29ddf09377cb Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 12:12:31 -0700 Subject: [PATCH 20/24] fix: avoid assignment. Co-authored-by: mscdex --- lib/internal/modules/cjs/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 96b3555e6dde35..1569eff7aaf4b3 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -190,7 +190,7 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } let allBuiltins = builtinModules.slice(); -allBuiltins = builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); +builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); allBuiltins = new SafeSet(allBuiltins); NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); From 15b71f3c47b7dc4f95a53da3c0fe904a66dde8b4 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 20:40:58 -0700 Subject: [PATCH 21/24] allBuiltins as a Set Co-authored-by: Feng Yu --- lib/internal/modules/cjs/loader.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 1569eff7aaf4b3..60d876dae6ba7d 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -189,9 +189,8 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } } -let allBuiltins = builtinModules.slice(); -builtinModules.forEach((builtin) => allBuiltins.push(`node:${builtin}`)); -allBuiltins = new SafeSet(allBuiltins); +const allBuiltins = new SafeSet(builtinModules); +builtinModules.forEach((builtin) => allBuiltins.add(`node:${builtin}`)); NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); ObjectFreeze(builtinModules); From 70e823a9b3b18f95c096db5b03534cb375d03915 Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Fri, 24 Jun 2022 03:57:54 +0000 Subject: [PATCH 22/24] chore: naming it isBuiltin instead & using flatMap --- lib/internal/modules/cjs/loader.js | 8 +++++--- test/parallel/test-module-isBuiltIn.js | 16 ---------------- test/parallel/test-module-isBuiltin.js | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) delete mode 100644 test/parallel/test-module-isBuiltIn.js create mode 100644 test/parallel/test-module-isBuiltin.js diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 60d876dae6ba7d..1592c4e4cb395c 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -33,6 +33,7 @@ const { ArrayPrototypeSplice, ArrayPrototypeUnshift, ArrayPrototypeUnshiftApply, + ArrayPrototypeFlatMap, Boolean, Error, JSONParse, @@ -189,8 +190,9 @@ for (const { 0: id, 1: mod } of NativeModule.map) { } } -const allBuiltins = new SafeSet(builtinModules); -builtinModules.forEach((builtin) => allBuiltins.add(`node:${builtin}`)); +const allBuiltins = new SafeSet( + ArrayPrototypeFlatMap(builtinModules, (bm) => [bm, `node:${bm}`]) +); NativeModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); ObjectFreeze(builtinModules); @@ -1298,7 +1300,7 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { } }; -Module.isBuiltIn = function isBuiltIn(moduleName) { +Module.isBuiltin = function isBuiltin(moduleName) { return allBuiltins.has(moduleName); }; diff --git a/test/parallel/test-module-isBuiltIn.js b/test/parallel/test-module-isBuiltIn.js deleted file mode 100644 index aceaae658afd75..00000000000000 --- a/test/parallel/test-module-isBuiltIn.js +++ /dev/null @@ -1,16 +0,0 @@ -'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)); diff --git a/test/parallel/test-module-isBuiltin.js b/test/parallel/test-module-isBuiltin.js new file mode 100644 index 00000000000000..a7815a8dfc1c18 --- /dev/null +++ b/test/parallel/test-module-isBuiltin.js @@ -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)); From fc6311714422f0fcaf74eeb4c9a0e994edc4d3cb Mon Sep 17 00:00:00 2001 From: "hemanth.hm" Date: Thu, 23 Jun 2022 21:31:18 -0700 Subject: [PATCH 23/24] doc: isBuiltin Co-authored-by: mscdex --- doc/api/module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/module.md b/doc/api/module.md index 6f59ba38305ccd..7423e215c73748 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -62,7 +62,7 @@ const require = createRequire(import.meta.url); const siblingModule = require('./sibling-module'); ``` -### `module.isBuiltIn(moduleName)` +### `module.isBuiltin(moduleName)`