| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 432d1b5 commit 3c4ee52
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,6 +44,7 @@ | |||
| 44 | 44 | /* global process, getLinkedBinding, getInternalBinding, primordials */ | |
| 45 | 45 | ||
| 46 | 46 | const { | |
| 47 | + ArrayFrom, | ||
| 47 | 48 | ArrayPrototypeMap, | |
| 48 | 49 | ArrayPrototypePush, | |
| 49 | 50 | ArrayPrototypeSlice, | |
@@ -119,6 +120,11 @@ const legacyWrapperList = new SafeSet([ | |||
| 119 | 120 | 'util', | |
| 120 | 121 | ]); | |
| 121 | 122 | ||
| 123 | + // Modules that can only be imported via the node: scheme. | ||
| 124 | + const schemelessBlockList = new SafeSet([ | ||
| 125 | + 'test', | ||
| 126 | + ]); | ||
| 127 | + | ||
| 122 | 128 | // Set up process.binding() and process._linkedBinding(). | |
| 123 | 129 | { | |
| 124 | 130 | const bindingObj = ObjectCreate(null); | |
@@ -242,6 +248,16 @@ class NativeModule { | |||
| 242 | 248 | return mod && mod.canBeRequiredByUsers; | |
| 243 | 249 | } | |
| 244 | 250 | ||
| 251 | + // Determine if a core module can be loaded without the node: prefix. This | ||
| 252 | + // function does not validate if the module actually exists. | ||
| 253 | + static canBeRequiredWithoutScheme(id) { | ||
| 254 | + return !schemelessBlockList.has(id); | ||
| 255 | + } | ||
| 256 | + | ||
| 257 | + static getSchemeOnlyModuleNames() { | ||
| 258 | + return ArrayFrom(schemelessBlockList); | ||
| 259 | + } | ||
| 260 | + | ||
| 245 | 261 | // Used by user-land module loaders to compile and load builtins. | |
| 246 | 262 | compileForPublicLoader() { | |
| 247 | 263 | if (!this.canBeRequiredByUsers) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -182,7 +182,8 @@ function Module(id = '', parent) { | |||
| 182 | 182 | ||
| 183 | 183 | const builtinModules = []; | |
| 184 | 184 | for (const { 0: id, 1: mod } of NativeModule.map) { | |
| 185 | - if (mod.canBeRequiredByUsers) { | ||
| 185 | + if (mod.canBeRequiredByUsers && | ||
| 186 | + NativeModule.canBeRequiredWithoutScheme(id)) { | ||
| 186 | 187 | ArrayPrototypePush(builtinModules, id); | |
| 187 | 188 | } | |
| 188 | 189 | } | |
@@ -802,7 +803,13 @@ Module._load = function(request, parent, isMain) { | |||
| 802 | 803 | } | |
| 803 | 804 | ||
| 804 | 805 | const mod = loadNativeModule(filename, request); | |
| 805 | - if (mod?.canBeRequiredByUsers) return mod.exports; | ||
| 806 | + if (mod?.canBeRequiredByUsers) { | ||
| 807 | + if (!NativeModule.canBeRequiredWithoutScheme(filename)) { | ||
| 808 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(filename); | ||
| 809 | + } | ||
| 810 | + | ||
| 811 | + return mod.exports; | ||
| 812 | + } | ||
| 806 | 813 | ||
| 807 | 814 | // Don't call updateChildren(), Module constructor already does. | |
| 808 | 815 | const module = cachedModule || new Module(filename, parent); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,7 @@ const { | |||
| 57 | 57 | ERR_PACKAGE_PATH_NOT_EXPORTED, | |
| 58 | 58 | ERR_UNSUPPORTED_DIR_IMPORT, | |
| 59 | 59 | ERR_NETWORK_IMPORT_DISALLOWED, | |
| 60 | + ERR_UNKNOWN_BUILTIN_MODULE, | ||
| 60 | 61 | ERR_UNSUPPORTED_ESM_URL_SCHEME, | |
| 61 | 62 | } = require('internal/errors').codes; | |
| 62 | 63 | const { Module: CJSModule } = require('internal/modules/cjs/loader'); | |
@@ -860,8 +861,13 @@ function parsePackageName(specifier, base) { | |||
| 860 | 861 | * @returns {resolved: URL, format? : string} | |
| 861 | 862 | */ | |
| 862 | 863 | function packageResolve(specifier, base, conditions) { | |
| 863 | - if (NativeModule.canBeRequiredByUsers(specifier)) | ||
| 864 | + if (NativeModule.canBeRequiredByUsers(specifier)) { | ||
| 865 | + if (!NativeModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 866 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); | ||
| 867 | + } | ||
| 868 | + | ||
| 864 | 869 | return new URL('node:' + specifier); | |
| 870 | + } | ||
| 865 | 871 | ||
| 866 | 872 | const { packageName, packageSubpath, isScoped } = | |
| 867 | 873 | parsePackageName(specifier, base); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,6 +100,7 @@ const { | |||
| 100 | 100 | globalThis, | |
| 101 | 101 | } = primordials; | |
| 102 | 102 | ||
| 103 | + const { NativeModule } = require('internal/bootstrap/loaders'); | ||
| 103 | 104 | const { | |
| 104 | 105 | makeRequireFunction, | |
| 105 | 106 | addBuiltinLibsToObject | |
@@ -129,6 +130,10 @@ let _builtinLibs = ArrayPrototypeFilter( | |||
| 129 | 130 | ); | |
| 130 | 131 | const nodeSchemeBuiltinLibs = ArrayPrototypeMap( | |
| 131 | 132 | _builtinLibs, (lib) => `node:${lib}`); | |
| 133 | + ArrayPrototypeForEach( | ||
| 134 | + NativeModule.getSchemeOnlyModuleNames(), | ||
| 135 | + (lib) => ArrayPrototypePush(nodeSchemeBuiltinLibs, `node:${lib}`), | ||
| 136 | + ); | ||
| 132 | 137 | const domain = require('domain'); | |
| 133 | 138 | let debug = require('internal/util/debuglog').debuglog('repl', (fn) => { | |
| 134 | 139 | debug = fn; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ const { | |||
| 16 | 16 | } = internalBinding('native_module'); | |
| 17 | 17 | ||
| 18 | 18 | for (const key of canBeRequired) { | |
| 19 | - require(key); | ||
| 19 | + require(`node:${key}`); | ||
| 20 | 20 | } | |
| 21 | 21 | ||
| 22 | 22 | // The computation has to be delayed until we have done loading modules | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,14 +53,18 @@ testMe.complete("import\t( 'n", common.mustCall((error, data) => { | |||
| 53 | 53 | assert.strictEqual(data[1], 'n'); | |
| 54 | 54 | const completions = data[0]; | |
| 55 | 55 | // import(...) completions include `node:` URL modules: | |
| 56 | - publicModules.forEach((lib, index) => | ||
| 57 | - assert.strictEqual(completions[index], `node:${lib}`)); | ||
| 58 | - assert.strictEqual(completions[publicModules.length], ''); | ||
| 56 | + let lastIndex = -1; | ||
| 57 | + | ||
| 58 | + publicModules.forEach((lib, index) => { | ||
| 59 | + lastIndex = completions.indexOf(`node:${lib}`); | ||
| 60 | + assert.notStrictEqual(lastIndex, -1); | ||
| 61 | + }); | ||
| 62 | + assert.strictEqual(completions[lastIndex + 1], ''); | ||
| 59 | 63 | // There is only one Node.js module that starts with n: | |
| 60 | - assert.strictEqual(completions[publicModules.length + 1], 'net'); | ||
| 61 | - assert.strictEqual(completions[publicModules.length + 2], ''); | ||
| 64 | + assert.strictEqual(completions[lastIndex + 2], 'net'); | ||
| 65 | + assert.strictEqual(completions[lastIndex + 3], ''); | ||
| 62 | 66 | // It's possible to pick up non-core modules too | |
| 63 | - completions.slice(publicModules.length + 3).forEach((completion) => { | ||
| 67 | + completions.slice(lastIndex + 4).forEach((completion) => { | ||
| 64 | 68 | assert.match(completion, /^n/); | |
| 65 | 69 | }); | |
| 66 | 70 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,14 +261,18 @@ testMe.complete("require\t( 'n", common.mustCall(function(error, data) { | |||
| 261 | 261 | assert.strictEqual(data.length, 2); | |
| 262 | 262 | assert.strictEqual(data[1], 'n'); | |
| 263 | 263 | // require(...) completions include `node:`-prefixed modules: | |
| 264 | - publicModules.forEach((lib, index) => | ||
| 265 | - assert.strictEqual(data[0][index], `node:${lib}`)); | ||
| 266 | - assert.strictEqual(data[0][publicModules.length], ''); | ||
| 264 | + let lastIndex = -1; | ||
| 265 | + | ||
| 266 | + publicModules.forEach((lib, index) => { | ||
| 267 | + lastIndex = data[0].indexOf(`node:${lib}`); | ||
| 268 | + assert.notStrictEqual(lastIndex, -1); | ||
| 269 | + }); | ||
| 270 | + assert.strictEqual(data[0][lastIndex + 1], ''); | ||
| 267 | 271 | // There is only one Node.js module that starts with n: | |
| 268 | - assert.strictEqual(data[0][publicModules.length + 1], 'net'); | ||
| 269 | - assert.strictEqual(data[0][publicModules.length + 2], ''); | ||
| 272 | + assert.strictEqual(data[0][lastIndex + 2], 'net'); | ||
| 273 | + assert.strictEqual(data[0][lastIndex + 3], ''); | ||
| 270 | 274 | // It's possible to pick up non-core modules too | |
| 271 | - data[0].slice(publicModules.length + 3).forEach((completion) => { | ||
| 275 | + data[0].slice(lastIndex + 4).forEach((completion) => { | ||
| 272 | 276 | assert.match(completion, /^n/); | |
| 273 | 277 | }); | |
| 274 | 278 | })); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + | ||
| 5 | + assert.throws( | ||
| 6 | + () => require('test'), | ||
| 7 | + common.expectsError({ code: 'ERR_UNKNOWN_BUILTIN_MODULE' }), | ||
| 8 | + ); | ||
| 9 | + | ||
| 10 | + (async () => { | ||
| 11 | + await assert.rejects( | ||
| 12 | + async () => import('test'), | ||
| 13 | + common.expectsError({ code: 'ERR_UNKNOWN_BUILTIN_MODULE' }), | ||
| 14 | + ); | ||
| 15 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments