| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fb047d6 commit 631c3ef
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,6 +63,7 @@ const { | |||
| 63 | 63 | SafeSet, | |
| 64 | 64 | String, | |
| 65 | 65 | StringPrototypeStartsWith, | |
| 66 | + StringPrototypeSlice, | ||
| 66 | 67 | TypeError, | |
| 67 | 68 | } = primordials; | |
| 68 | 69 | ||
@@ -126,10 +127,14 @@ const legacyWrapperList = new SafeSet([ | |||
| 126 | 127 | 'util', | |
| 127 | 128 | ]); | |
| 128 | 129 | ||
| 130 | + // The code bellow assumes that the two lists must not contain any modules | ||
| 131 | + // beginning with "internal/". | ||
| 129 | 132 | // Modules that can only be imported via the node: scheme. | |
| 130 | 133 | const schemelessBlockList = new SafeSet([ | |
| 131 | 134 | 'test', | |
| 132 | 135 | ]); | |
| 136 | + // Modules that will only be enabled at run time. | ||
| 137 | + const experimentalModuleList = new SafeSet(); | ||
| 133 | 138 | ||
| 134 | 139 | // Set up process.binding() and process._linkedBinding(). | |
| 135 | 140 | { | |
@@ -196,6 +201,20 @@ const getOwn = (target, property, receiver) => { | |||
| 196 | 201 | undefined; | |
| 197 | 202 | }; | |
| 198 | 203 | ||
| 204 | + const publicBuiltinIds = builtinIds | ||
| 205 | + .filter((id) => | ||
| 206 | + !StringPrototypeStartsWith(id, 'internal/') && | ||
| 207 | + !experimentalModuleList.has(id), | ||
| 208 | + ); | ||
| 209 | + // Do not expose the loaders to user land even with --expose-internals. | ||
| 210 | + const internalBuiltinIds = builtinIds | ||
| 211 | + .filter((id) => StringPrototypeStartsWith(id, 'internal/') && id !== selfId); | ||
| 212 | + | ||
| 213 | + // When --expose-internals is on we'll add the internal builtin ids to these. | ||
| 214 | + const canBeRequiredByUsersList = new SafeSet(publicBuiltinIds); | ||
| 215 | + const canBeRequiredByUsersWithoutSchemeList = | ||
| 216 | + new SafeSet(publicBuiltinIds.filter((id) => !schemelessBlockList.has(id))); | ||
| 217 | + | ||
| 199 | 218 | /** | |
| 200 | 219 | * An internal abstraction for the built-in JavaScript modules of Node.js. | |
| 201 | 220 | * Be careful not to expose this to user land unless --expose-internals is | |
@@ -213,7 +232,6 @@ class BuiltinModule { | |||
| 213 | 232 | constructor(id) { | |
| 214 | 233 | this.filename = `${id}.js`; | |
| 215 | 234 | this.id = id; | |
| 216 | - this.canBeRequiredByUsers = !StringPrototypeStartsWith(id, 'internal/'); | ||
| 217 | 235 | ||
| 218 | 236 | // The CJS exports object of the module. | |
| 219 | 237 | this.exports = {}; | |
@@ -235,14 +253,23 @@ class BuiltinModule { | |||
| 235 | 253 | this.exportKeys = undefined; | |
| 236 | 254 | } | |
| 237 | 255 | ||
| 256 | + static allowRequireByUsers(id) { | ||
| 257 | + if (id === selfId) { | ||
| 258 | + // No code because this is an assertion against bugs. | ||
| 259 | + // eslint-disable-next-line no-restricted-syntax | ||
| 260 | + throw new Error(`Should not allow ${id}`); | ||
| 261 | + } | ||
| 262 | + canBeRequiredByUsersList.add(id); | ||
| 263 | + if (!schemelessBlockList.has(id)) { | ||
| 264 | + canBeRequiredByUsersWithoutSchemeList.add(id); | ||
| 265 | + } | ||
| 266 | + } | ||
| 267 | + | ||
| 238 | 268 | // To be called during pre-execution when --expose-internals is on. | |
| 239 | 269 | // Enables the user-land module loader to access internal modules. | |
| 240 | 270 | static exposeInternals() { | |
| 241 | - for (const { 0: id, 1: mod } of BuiltinModule.map) { | ||
| 242 | - // Do not expose this to user land even with --expose-internals. | ||
| 243 | - if (id !== selfId) { | ||
| 244 | - mod.canBeRequiredByUsers = true; | ||
| 245 | - } | ||
| 271 | + for (let i = 0; i < internalBuiltinIds.length; ++i) { | ||
| 272 | + BuiltinModule.allowRequireByUsers(internalBuiltinIds[i]); | ||
| 246 | 273 | } | |
| 247 | 274 | } | |
| 248 | 275 | ||
@@ -251,14 +278,23 @@ class BuiltinModule { | |||
| 251 | 278 | } | |
| 252 | 279 | ||
| 253 | 280 | static canBeRequiredByUsers(id) { | |
| 254 | - const mod = BuiltinModule.map.get(id); | ||
| 255 | - return mod && mod.canBeRequiredByUsers; | ||
| 281 | + return canBeRequiredByUsersList.has(id); | ||
| 256 | 282 | } | |
| 257 | 283 | ||
| 258 | - // Determine if a core module can be loaded without the node: prefix. This | ||
| 259 | - // function does not validate if the module actually exists. | ||
| 260 | 284 | static canBeRequiredWithoutScheme(id) { | |
| 261 | - return !schemelessBlockList.has(id); | ||
| 285 | + return canBeRequiredByUsersWithoutSchemeList.has(id); | ||
| 286 | + } | ||
| 287 | + | ||
| 288 | + static isBuiltin(id) { | ||
| 289 | + return BuiltinModule.canBeRequiredWithoutScheme(id) || ( | ||
| 290 | + typeof id === 'string' && | ||
| 291 | + StringPrototypeStartsWith(id, 'node:') && | ||
| 292 | + BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(id, 5)) | ||
| 293 | + ); | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + static getCanBeRequiredByUsersWithoutSchemeList() { | ||
| 297 | + return ArrayFrom(canBeRequiredByUsersWithoutSchemeList); | ||
| 262 | 298 | } | |
| 263 | 299 | ||
| 264 | 300 | static getSchemeOnlyModuleNames() { | |
@@ -267,7 +303,7 @@ class BuiltinModule { | |||
| 267 | 303 | ||
| 268 | 304 | // Used by user-land module loaders to compile and load builtins. | |
| 269 | 305 | compileForPublicLoader() { | |
| 270 | - if (!this.canBeRequiredByUsers) { | ||
| 306 | + if (!BuiltinModule.canBeRequiredByUsers(this.id)) { | ||
| 271 | 307 | // No code because this is an assertion against bugs | |
| 272 | 308 | // eslint-disable-next-line no-restricted-syntax | |
| 273 | 309 | throw new Error(`Should not compile ${this.id} for public use`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,6 @@ const { | |||
| 34 | 34 | ArrayPrototypeSplice, | |
| 35 | 35 | ArrayPrototypeUnshift, | |
| 36 | 36 | ArrayPrototypeUnshiftApply, | |
| 37 | - ArrayPrototypeFlatMap, | ||
| 38 | 37 | Boolean, | |
| 39 | 38 | Error, | |
| 40 | 39 | JSONParse, | |
@@ -51,7 +50,6 @@ const { | |||
| 51 | 50 | ReflectSet, | |
| 52 | 51 | RegExpPrototypeExec, | |
| 53 | 52 | SafeMap, | |
| 54 | - SafeSet, | ||
| 55 | 53 | SafeWeakMap, | |
| 56 | 54 | String, | |
| 57 | 55 | StringPrototypeCharAt, | |
@@ -81,7 +79,7 @@ const { | |||
| 81 | 79 | } = require('internal/source_map/source_map_cache'); | |
| 82 | 80 | const { pathToFileURL, fileURLToPath, isURL } = require('internal/url'); | |
| 83 | 81 | const { | |
| 84 | - deprecate, | ||
| 82 | + pendingDeprecate, | ||
| 85 | 83 | emitExperimentalWarning, | |
| 86 | 84 | kEmptyObject, | |
| 87 | 85 | filterOwnProperties, | |
@@ -309,44 +307,29 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |||
| 309 | 307 | debug = fn; | |
| 310 | 308 | }); | |
| 311 | 309 | ||
| 312 | - const builtinModules = []; | ||
| 310 | + ObjectDefineProperty(Module.prototype, 'parent', { | ||
| 311 | + __proto__: null, | ||
| 312 | + get: pendingDeprecate( | ||
| 313 | + getModuleParent, | ||
| 314 | + 'module.parent is deprecated due to accuracy issues. Please use ' + | ||
| 315 | + 'require.main to find program entry point instead.', | ||
| 316 | + 'DEP0144', | ||
| 317 | + ), | ||
| 318 | + set: pendingDeprecate( | ||
| 319 | + setModuleParent, | ||
| 320 | + 'module.parent is deprecated due to accuracy issues. Please use ' + | ||
| 321 | + 'require.main to find program entry point instead.', | ||
| 322 | + 'DEP0144', | ||
| 323 | + ), | ||
| 324 | + }); | ||
| 325 | + Module._debug = pendingDeprecate(debug, 'Module._debug is deprecated.', 'DEP0077'); | ||
| 326 | + Module.isBuiltin = BuiltinModule.isBuiltin; | ||
| 327 | + | ||
| 313 | 328 | // This function is called during pre-execution, before any user code is run. | |
| 314 | 329 | function initializeCJS() { | |
| 315 | - const pendingDeprecation = getOptionValue('--pending-deprecation'); | ||
| 316 | - ObjectDefineProperty(Module.prototype, 'parent', { | ||
| 317 | - __proto__: null, | ||
| 318 | - get: pendingDeprecation ? deprecate( | ||
| 319 | - getModuleParent, | ||
| 320 | - 'module.parent is deprecated due to accuracy issues. Please use ' + | ||
| 321 | - 'require.main to find program entry point instead.', | ||
| 322 | - 'DEP0144', | ||
| 323 | - ) : getModuleParent, | ||
| 324 | - set: pendingDeprecation ? deprecate( | ||
| 325 | - setModuleParent, | ||
| 326 | - 'module.parent is deprecated due to accuracy issues. Please use ' + | ||
| 327 | - 'require.main to find program entry point instead.', | ||
| 328 | - 'DEP0144', | ||
| 329 | - ) : setModuleParent, | ||
| 330 | - }); | ||
| 331 | - Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077'); | ||
| 332 | - | ||
| 333 | - for (const { 0: id, 1: mod } of BuiltinModule.map) { | ||
| 334 | - if (mod.canBeRequiredByUsers && | ||
| 335 | - BuiltinModule.canBeRequiredWithoutScheme(id)) { | ||
| 336 | - ArrayPrototypePush(builtinModules, id); | ||
| 337 | - } | ||
| 338 | - } | ||
| 339 | - | ||
| 340 | - const allBuiltins = new SafeSet( | ||
| 341 | - ArrayPrototypeFlatMap(builtinModules, (bm) => [bm, `node:${bm}`]), | ||
| 342 | - ); | ||
| 343 | - BuiltinModule.getSchemeOnlyModuleNames().forEach((builtin) => allBuiltins.add(`node:${builtin}`)); | ||
| 344 | - ObjectFreeze(builtinModules); | ||
| 345 | - Module.builtinModules = builtinModules; | ||
| 346 | - | ||
| 347 | - Module.isBuiltin = function isBuiltin(moduleName) { | ||
| 348 | - return allBuiltins.has(moduleName); | ||
| 349 | - }; | ||
| 330 | + // This need to be done at runtime in case --expose-internals is set. | ||
| 331 | + const builtinModules = BuiltinModule.getCanBeRequiredByUsersWithoutSchemeList(); | ||
| 332 | + Module.builtinModules = ObjectFreeze(builtinModules); | ||
| 350 | 333 | ||
| 351 | 334 | initializeCjsConditions(); | |
| 352 | 335 | ||
@@ -813,7 +796,6 @@ Module._resolveLookupPaths = function(request, parent) { | |||
| 813 | 796 | StringPrototypeStartsWith(request, 'node:') && | |
| 814 | 797 | BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5)) | |
| 815 | 798 | ) || ( | |
| 816 | - BuiltinModule.canBeRequiredByUsers(request) && | ||
| 817 | 799 | BuiltinModule.canBeRequiredWithoutScheme(request) | |
| 818 | 800 | )) { | |
| 819 | 801 | debug('looking for %j in []', request); | |
@@ -935,11 +917,11 @@ Module._load = function(request, parent, isMain) { | |||
| 935 | 917 | // Slice 'node:' prefix | |
| 936 | 918 | const id = StringPrototypeSlice(request, 5); | |
| 937 | 919 | ||
| 938 | - const module = loadBuiltinModule(id, request); | ||
| 939 | - if (!module?.canBeRequiredByUsers) { | ||
| 920 | + if (!BuiltinModule.canBeRequiredByUsers(id)) { | ||
| 940 | 921 | throw new ERR_UNKNOWN_BUILTIN_MODULE(request); | |
| 941 | 922 | } | |
| 942 | 923 | ||
| 924 | + const module = loadBuiltinModule(id, request); | ||
| 943 | 925 | return module.exports; | |
| 944 | 926 | } | |
| 945 | 927 | ||
@@ -957,9 +939,8 @@ Module._load = function(request, parent, isMain) { | |||
| 957 | 939 | } | |
| 958 | 940 | } | |
| 959 | 941 | ||
| 960 | - const mod = loadBuiltinModule(filename, request); | ||
| 961 | - if (mod?.canBeRequiredByUsers && | ||
| 962 | - BuiltinModule.canBeRequiredWithoutScheme(filename)) { | ||
| 942 | + if (BuiltinModule.canBeRequiredWithoutScheme(filename)) { | ||
| 943 | + const mod = loadBuiltinModule(filename, request); | ||
| 963 | 944 | return mod.exports; | |
| 964 | 945 | } | |
| 965 | 946 | ||
@@ -1013,7 +994,6 @@ Module._resolveFilename = function(request, parent, isMain, options) { | |||
| 1013 | 994 | StringPrototypeStartsWith(request, 'node:') && | |
| 1014 | 995 | BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5)) | |
| 1015 | 996 | ) || ( | |
| 1016 | - BuiltinModule.canBeRequiredByUsers(request) && | ||
| 1017 | 997 | BuiltinModule.canBeRequiredWithoutScheme(request) | |
| 1018 | 998 | ) | |
| 1019 | 999 | ) { | |
@@ -1469,8 +1449,7 @@ Module._preloadModules = function(requests) { | |||
| 1469 | 1449 | ||
| 1470 | 1450 | Module.syncBuiltinESMExports = function syncBuiltinESMExports() { | |
| 1471 | 1451 | for (const mod of BuiltinModule.map.values()) { | |
| 1472 | - if (mod.canBeRequiredByUsers && | ||
| 1473 | - BuiltinModule.canBeRequiredWithoutScheme(mod.id)) { | ||
| 1452 | + if (BuiltinModule.canBeRequiredWithoutScheme(mod.id)) { | ||
| 1474 | 1453 | mod.syncExports(); | |
| 1475 | 1454 | } | |
| 1476 | 1455 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -207,8 +207,7 @@ class Hooks { | |||
| 207 | 207 | globalThis, | |
| 208 | 208 | // Param getBuiltin | |
| 209 | 209 | (builtinName) => { | |
| 210 | - if (BuiltinModule.canBeRequiredByUsers(builtinName) && | ||
| 211 | - BuiltinModule.canBeRequiredWithoutScheme(builtinName)) { | ||
| 210 | + if (BuiltinModule.canBeRequiredWithoutScheme(builtinName)) { | ||
| 212 | 211 | return require(builtinName); | |
| 213 | 212 | } | |
| 214 | 213 | throw new ERR_INVALID_ARG_VALUE('builtinName', builtinName); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -736,8 +736,7 @@ function parsePackageName(specifier, base) { | |||
| 736 | 736 | * @returns {resolved: URL, format? : string} | |
| 737 | 737 | */ | |
| 738 | 738 | function packageResolve(specifier, base, conditions) { | |
| 739 | - if (BuiltinModule.canBeRequiredByUsers(specifier) && | ||
| 740 | - BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 739 | + if (BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 741 | 740 | return new URL('node:' + specifier); | |
| 742 | 741 | } | |
| 743 | 742 | ||
@@ -919,8 +918,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { | |||
| 919 | 918 | ||
| 920 | 919 | return { url: parsed.href }; | |
| 921 | 920 | } | |
| 922 | - if (BuiltinModule.canBeRequiredByUsers(specifier) && | ||
| 923 | - BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 921 | + if (BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 924 | 922 | throw new ERR_NETWORK_IMPORT_DISALLOWED( | |
| 925 | 923 | specifier, | |
| 926 | 924 | parsedParentURL, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,13 +58,14 @@ function getCjsConditions() { | |||
| 58 | 58 | } | |
| 59 | 59 | ||
| 60 | 60 | function loadBuiltinModule(filename, request) { | |
| 61 | - const mod = BuiltinModule.map.get(filename); | ||
| 62 | - if (mod?.canBeRequiredByUsers) { | ||
| 63 | - debug('load built-in module %s', request); | ||
| 64 | - // compileForPublicLoader() throws if mod.canBeRequiredByUsers is false: | ||
| 65 | - mod.compileForPublicLoader(); | ||
| 66 | - return mod; | ||
| 61 | + if (!BuiltinModule.canBeRequiredByUsers(filename)) { | ||
| 62 | + return; | ||
| 67 | 63 | } | |
| 64 | + const mod = BuiltinModule.map.get(filename); | ||
| 65 | + debug('load built-in module %s', request); | ||
| 66 | + // compileForPublicLoader() throws if canBeRequiredByUsers is false: | ||
| 67 | + mod.compileForPublicLoader(); | ||
| 68 | + return mod; | ||
| 68 | 69 | } | |
| 69 | 70 | ||
| 70 | 71 | // Invoke with makeRequireFunction(module) where |module| is the Module object | |
@@ -88,8 +89,9 @@ function makeRequireFunction(mod, redirects) { | |||
| 88 | 89 | const href = destination.href; | |
| 89 | 90 | if (destination.protocol === 'node:') { | |
| 90 | 91 | const specifier = destination.pathname; | |
| 91 | - const mod = loadBuiltinModule(specifier, href); | ||
| 92 | - if (mod && mod.canBeRequiredByUsers) { | ||
| 92 | + | ||
| 93 | + if (BuiltinModule.canBeRequiredByUsers(specifier)) { | ||
| 94 | + const mod = loadBuiltinModule(specifier, href); | ||
| 93 | 95 | return mod.exports; | |
| 94 | 96 | } | |
| 95 | 97 | throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments