| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 257f322 commit 075c95f
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,7 +96,6 @@ | |||
| 96 | 96 | /doc/api/packages.md @nodejs/loaders | |
| 97 | 97 | /lib/internal/bootstrap/realm.js @nodejs/loaders | |
| 98 | 98 | /lib/internal/modules/* @nodejs/loaders | |
| 99 | - /lib/internal/process/esm_loader.js @nodejs/loaders | ||
| 100 | 99 | /lib/internal/process/execution.js @nodejs/loaders | |
| 101 | 100 | /lib/module.js @nodejs/loaders | |
| 102 | 101 | /src/module_wrap* @nodejs/loaders @nodejs/vm | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,8 +50,7 @@ function loadESMIfNeeded(cb) { | |||
| 50 | 50 | const hasModulePreImport = getOptionValue('--import').length > 0; | |
| 51 | 51 | ||
| 52 | 52 | if (hasModulePreImport) { | |
| 53 | - const { loadESM } = require('internal/process/esm_loader'); | ||
| 54 | - loadESM(cb); | ||
| 53 | + require('internal/modules/run_main').runEntryPointWithESMLoader(cb); | ||
| 55 | 54 | return; | |
| 56 | 55 | } | |
| 57 | 56 | cb(); | |
@@ -76,7 +75,5 @@ async function checkSyntax(source, filename) { | |||
| 76 | 75 | return; | |
| 77 | 76 | } | |
| 78 | 77 | ||
| 79 | - const { loadESM } = require('internal/process/esm_loader'); | ||
| 80 | - const { handleMainPromise } = require('internal/modules/run_main'); | ||
| 81 | - handleMainPromise(loadESM((loader) => wrapSafe(filename, source))); | ||
| 78 | + wrapSafe(filename, source); | ||
| 82 | 79 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ const { | |||
| 10 | 10 | const { getOptionValue } = require('internal/options'); | |
| 11 | 11 | ||
| 12 | 12 | const { | |
| 13 | - evalModule, | ||
| 13 | + evalModuleEntryPoint, | ||
| 14 | 14 | evalScript, | |
| 15 | 15 | readStdin, | |
| 16 | 16 | } = require('internal/process/execution'); | |
@@ -24,15 +24,15 @@ readStdin((code) => { | |||
| 24 | 24 | process._eval = code; | |
| 25 | 25 | ||
| 26 | 26 | const print = getOptionValue('--print'); | |
| 27 | - const loadESM = getOptionValue('--import').length > 0; | ||
| 27 | + const shouldLoadESM = getOptionValue('--import').length > 0; | ||
| 28 | 28 | if (getOptionValue('--input-type') === 'module' || | |
| 29 | 29 | (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { | |
| 30 | - evalModule(code, print); | ||
| 30 | + evalModuleEntryPoint(code, print); | ||
| 31 | 31 | } else { | |
| 32 | 32 | evalScript('[stdin]', | |
| 33 | 33 | code, | |
| 34 | 34 | getOptionValue('--inspect-brk'), | |
| 35 | 35 | print, | |
| 36 | - loadESM); | ||
| 36 | + shouldLoadESM); | ||
| 37 | 37 | } | |
| 38 | 38 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ const { | |||
| 13 | 13 | prepareMainThreadExecution, | |
| 14 | 14 | markBootstrapComplete, | |
| 15 | 15 | } = require('internal/process/pre_execution'); | |
| 16 | - const { evalModule, evalScript } = require('internal/process/execution'); | ||
| 16 | + const { evalModuleEntryPoint, evalScript } = require('internal/process/execution'); | ||
| 17 | 17 | const { addBuiltinLibsToObject } = require('internal/modules/helpers'); | |
| 18 | 18 | ||
| 19 | 19 | const { getOptionValue } = require('internal/options'); | |
@@ -24,10 +24,10 @@ markBootstrapComplete(); | |||
| 24 | 24 | ||
| 25 | 25 | const source = getOptionValue('--eval'); | |
| 26 | 26 | const print = getOptionValue('--print'); | |
| 27 | - const loadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0; | ||
| 27 | + const shouldLoadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0; | ||
| 28 | 28 | if (getOptionValue('--input-type') === 'module' || | |
| 29 | 29 | (getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) { | |
| 30 | - evalModule(source, print); | ||
| 30 | + evalModuleEntryPoint(source, print); | ||
| 31 | 31 | } else { | |
| 32 | 32 | // For backward compatibility, we want the identifier crypto to be the | |
| 33 | 33 | // `node:crypto` module rather than WebCrypto. | |
@@ -54,5 +54,5 @@ if (getOptionValue('--input-type') === 'module' || | |||
| 54 | 54 | ) : source, | |
| 55 | 55 | getOptionValue('--inspect-brk'), | |
| 56 | 56 | print, | |
| 57 | - loadESM); | ||
| 57 | + shouldLoadESM); | ||
| 58 | 58 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,8 +35,7 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) { | |||
| 35 | 35 | process.exit(kInvalidCommandLineArgument); | |
| 36 | 36 | } | |
| 37 | 37 | ||
| 38 | - const esmLoader = require('internal/process/esm_loader'); | ||
| 39 | - esmLoader.loadESM(() => { | ||
| 38 | + require('internal/modules/run_main').runEntryPointWithESMLoader(() => { | ||
| 40 | 39 | console.log(`Welcome to Node.js ${process.version}.\n` + | |
| 41 | 40 | 'Type ".help" for more information.'); | |
| 42 | 41 | ||
@@ -64,5 +63,7 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) { | |||
| 64 | 63 | getOptionValue('--inspect-brk'), | |
| 65 | 64 | getOptionValue('--print')); | |
| 66 | 65 | } | |
| 66 | + // The TLAs in the REPL are still run as scripts, just transformed as async | ||
| 67 | + // IIFEs for the REPL code itself to await on. | ||
| 67 | 68 | }); | |
| 68 | 69 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -170,8 +170,8 @@ port.on('message', (message) => { | |||
| 170 | 170 | } | |
| 171 | 171 | ||
| 172 | 172 | case 'module': { | |
| 173 | - const { evalModule } = require('internal/process/execution'); | ||
| 174 | - PromisePrototypeThen(evalModule(filename), undefined, (e) => { | ||
| 173 | + const { evalModuleEntryPoint } = require('internal/process/execution'); | ||
| 174 | + PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => { | ||
| 175 | 175 | workerOnGlobalUncaughtException(e, true); | |
| 176 | 176 | }); | |
| 177 | 177 | break; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -146,8 +146,8 @@ class Hooks { | |||
| 146 | 146 | * loader (user-land) to the worker. | |
| 147 | 147 | */ | |
| 148 | 148 | async register(urlOrSpecifier, parentURL, data) { | |
| 149 | - const moduleLoader = require('internal/process/esm_loader').esmLoader; | ||
| 150 | - const keyedExports = await moduleLoader.import( | ||
| 149 | + const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | ||
| 150 | + const keyedExports = await cascadedLoader.import( | ||
| 151 | 151 | urlOrSpecifier, | |
| 152 | 152 | parentURL, | |
| 153 | 153 | kEmptyObject, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ const { | |||
| 20 | 20 | ERR_UNKNOWN_MODULE_FORMAT, | |
| 21 | 21 | } = require('internal/errors').codes; | |
| 22 | 22 | const { getOptionValue } = require('internal/options'); | |
| 23 | - const { pathToFileURL, isURL } = require('internal/url'); | ||
| 23 | + const { isURL } = require('internal/url'); | ||
| 24 | 24 | const { emitExperimentalWarning } = require('internal/util'); | |
| 25 | 25 | const { | |
| 26 | 26 | getDefaultConditions, | |
@@ -85,11 +85,6 @@ class ModuleLoader { | |||
| 85 | 85 | */ | |
| 86 | 86 | #defaultConditions = getDefaultConditions(); | |
| 87 | 87 | ||
| 88 | - /** | ||
| 89 | - * The index for assigning unique URLs to anonymous module evaluation | ||
| 90 | - */ | ||
| 91 | - evalIndex = 0; | ||
| 92 | - | ||
| 93 | 88 | /** | |
| 94 | 89 | * Registry of resolved specifiers | |
| 95 | 90 | */ | |
@@ -187,10 +182,7 @@ class ModuleLoader { | |||
| 187 | 182 | } | |
| 188 | 183 | } | |
| 189 | 184 | ||
| 190 | - async eval( | ||
| 191 | - source, | ||
| 192 | - url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href, | ||
| 193 | - ) { | ||
| 185 | + async eval(source, url) { | ||
| 194 | 186 | const evalInstance = (url) => { | |
| 195 | 187 | const { ModuleWrap } = internalBinding('module_wrap'); | |
| 196 | 188 | const { registerModule } = require('internal/modules/esm/utils'); | |
@@ -214,6 +206,7 @@ class ModuleLoader { | |||
| 214 | 206 | return { | |
| 215 | 207 | __proto__: null, | |
| 216 | 208 | namespace: module.getNamespace(), | |
| 209 | + module, | ||
| 217 | 210 | }; | |
| 218 | 211 | } | |
| 219 | 212 | ||
@@ -568,6 +561,23 @@ function getHooksProxy() { | |||
| 568 | 561 | return hooksProxy; | |
| 569 | 562 | } | |
| 570 | 563 | ||
| 564 | + let cascadedLoader; | ||
| 565 | + | ||
| 566 | + /** | ||
| 567 | + * This is a singleton ESM loader that integrates the loader hooks, if any. | ||
| 568 | + * It it used by other internal built-ins when they need to load ESM code | ||
| 569 | + * while also respecting hooks. | ||
| 570 | + * When built-ins need access to this loader, they should do | ||
| 571 | + * require('internal/module/esm/loader').getOrInitializeCascadedLoader() | ||
| 572 | + * lazily only right before the loader is actually needed, and don't do it | ||
| 573 | + * in the top-level, to avoid circular dependencies. | ||
| 574 | + * @returns {ModuleLoader} | ||
| 575 | + */ | ||
| 576 | + function getOrInitializeCascadedLoader() { | ||
| 577 | + cascadedLoader ??= createModuleLoader(); | ||
| 578 | + return cascadedLoader; | ||
| 579 | + } | ||
| 580 | + | ||
| 571 | 581 | /** | |
| 572 | 582 | * Register a single loader programmatically. | |
| 573 | 583 | * @param {string|import('url').URL} specifier | |
@@ -598,12 +608,11 @@ function getHooksProxy() { | |||
| 598 | 608 | * ``` | |
| 599 | 609 | */ | |
| 600 | 610 | function register(specifier, parentURL = undefined, options) { | |
| 601 | - const moduleLoader = require('internal/process/esm_loader').esmLoader; | ||
| 602 | 611 | if (parentURL != null && typeof parentURL === 'object' && !isURL(parentURL)) { | |
| 603 | 612 | options = parentURL; | |
| 604 | 613 | parentURL = options.parentURL; | |
| 605 | 614 | } | |
| 606 | - moduleLoader.register( | ||
| 615 | + getOrInitializeCascadedLoader().register( | ||
| 607 | 616 | specifier, | |
| 608 | 617 | parentURL ?? 'data:', | |
| 609 | 618 | options?.data, | |
@@ -614,5 +623,6 @@ function register(specifier, parentURL = undefined, options) { | |||
| 614 | 623 | module.exports = { | |
| 615 | 624 | createModuleLoader, | |
| 616 | 625 | getHooksProxy, | |
| 626 | + getOrInitializeCascadedLoader, | ||
| 617 | 627 | register, | |
| 618 | 628 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,7 +55,6 @@ const { | |||
| 55 | 55 | const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); | |
| 56 | 56 | const moduleWrap = internalBinding('module_wrap'); | |
| 57 | 57 | const { ModuleWrap } = moduleWrap; | |
| 58 | - const asyncESM = require('internal/process/esm_loader'); | ||
| 59 | 58 | const { emitWarningSync } = require('internal/process/warning'); | |
| 60 | 59 | const { internalCompileFunction } = require('internal/vm'); | |
| 61 | 60 | const { | |
@@ -157,7 +156,8 @@ function errPath(url) { | |||
| 157 | 156 | * @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports>} The imported module. | |
| 158 | 157 | */ | |
| 159 | 158 | async function importModuleDynamically(specifier, { url }, attributes) { | |
| 160 | - return asyncESM.esmLoader.import(specifier, url, attributes); | ||
| 159 | + const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | ||
| 160 | + return cascadedLoader.import(specifier, url, attributes); | ||
| 161 | 161 | } | |
| 162 | 162 | ||
| 163 | 163 | // Strategy for loading a standard JavaScript module. | |
@@ -243,6 +243,7 @@ function loadCJSModule(module, source, url, filename) { | |||
| 243 | 243 | ||
| 244 | 244 | const compiledWrapper = compileResult.function; | |
| 245 | 245 | ||
| 246 | + const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | ||
| 246 | 247 | const __dirname = dirname(filename); | |
| 247 | 248 | // eslint-disable-next-line func-name-matching,func-style | |
| 248 | 249 | const requireFn = function require(specifier) { | |
@@ -261,7 +262,7 @@ function loadCJSModule(module, source, url, filename) { | |||
| 261 | 262 | } | |
| 262 | 263 | specifier = `${pathToFileURL(path)}`; | |
| 263 | 264 | } | |
| 264 | - const job = asyncESM.esmLoader.getModuleJobSync(specifier, url, importAttributes); | ||
| 265 | + const job = cascadedLoader.getModuleJobSync(specifier, url, importAttributes); | ||
| 265 | 266 | job.runSync(); | |
| 266 | 267 | return cjsCache.get(job.url).exports; | |
| 267 | 268 | }; | |
@@ -272,7 +273,7 @@ function loadCJSModule(module, source, url, filename) { | |||
| 272 | 273 | specifier = `${pathToFileURL(path)}`; | |
| 273 | 274 | } | |
| 274 | 275 | } | |
| 275 | - const { url: resolvedURL } = asyncESM.esmLoader.resolveSync(specifier, url, kEmptyObject); | ||
| 276 | + const { url: resolvedURL } = cascadedLoader.resolveSync(specifier, url, kEmptyObject); | ||
| 276 | 277 | return StringPrototypeStartsWith(resolvedURL, 'file://') ? fileURLToPath(resolvedURL) : resolvedURL; | |
| 277 | 278 | }); | |
| 278 | 279 | setOwnProperty(requireFn, 'main', process.mainModule); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments