| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 947d066 commit 3d1ca78
25 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ const { | |||
| 9 | 9 | const { getOptionValue } = require('internal/options'); | |
| 10 | 10 | const { Buffer } = require('buffer'); | |
| 11 | 11 | const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes; | |
| 12 | - const path = require('path'); | ||
| 12 | + const assert = require('internal/assert'); | ||
| 13 | 13 | ||
| 14 | 14 | function prepareMainThreadExecution(expandArgv1 = false) { | |
| 15 | 15 | // Patch the process object with legacy properties and normalizations | |
@@ -64,6 +64,9 @@ function prepareMainThreadExecution(expandArgv1 = false) { | |||
| 64 | 64 | initializeDeprecations(); | |
| 65 | 65 | initializeCJSLoader(); | |
| 66 | 66 | initializeESMLoader(); | |
| 67 | + | ||
| 68 | + const CJSLoader = require('internal/modules/cjs/loader'); | ||
| 69 | + assert(!CJSLoader.hasLoadedAnyUserCJSModule); | ||
| 67 | 70 | loadPreloadModules(); | |
| 68 | 71 | initializeFrozenIntrinsics(); | |
| 69 | 72 | } | |
@@ -398,7 +401,11 @@ function initializePolicy() { | |||
| 398 | 401 | } | |
| 399 | 402 | ||
| 400 | 403 | function initializeCJSLoader() { | |
| 401 | - require('internal/modules/cjs/loader').Module._initPaths(); | ||
| 404 | + const CJSLoader = require('internal/modules/cjs/loader'); | ||
| 405 | + CJSLoader.Module._initPaths(); | ||
| 406 | + // TODO(joyeecheung): deprecate this in favor of a proper hook? | ||
| 407 | + CJSLoader.Module.runMain = | ||
| 408 | + require('internal/modules/run_main').executeUserEntryPoint; | ||
| 402 | 409 | } | |
| 403 | 410 | ||
| 404 | 411 | function initializeESMLoader() { | |
@@ -446,74 +453,11 @@ function loadPreloadModules() { | |||
| 446 | 453 | } | |
| 447 | 454 | } | |
| 448 | 455 | ||
| 449 | - function resolveMainPath(main) { | ||
| 450 | - const { toRealPath, Module: CJSModule } = | ||
| 451 | - require('internal/modules/cjs/loader'); | ||
| 452 | - | ||
| 453 | - // Note extension resolution for the main entry point can be deprecated in a | ||
| 454 | - // future major. | ||
| 455 | - let mainPath = CJSModule._findPath(path.resolve(main), null, true); | ||
| 456 | - if (!mainPath) | ||
| 457 | - return; | ||
| 458 | - | ||
| 459 | - const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | ||
| 460 | - if (!preserveSymlinksMain) | ||
| 461 | - mainPath = toRealPath(mainPath); | ||
| 462 | - | ||
| 463 | - return mainPath; | ||
| 464 | - } | ||
| 465 | - | ||
| 466 | - function shouldUseESMLoader(mainPath) { | ||
| 467 | - const experimentalModules = getOptionValue('--experimental-modules'); | ||
| 468 | - if (!experimentalModules) | ||
| 469 | - return false; | ||
| 470 | - const userLoader = getOptionValue('--experimental-loader'); | ||
| 471 | - if (userLoader) | ||
| 472 | - return true; | ||
| 473 | - const experimentalSpecifierResolution = | ||
| 474 | - getOptionValue('--experimental-specifier-resolution'); | ||
| 475 | - if (experimentalSpecifierResolution === 'node') | ||
| 476 | - return true; | ||
| 477 | - // Determine the module format of the main | ||
| 478 | - if (mainPath && mainPath.endsWith('.mjs')) | ||
| 479 | - return true; | ||
| 480 | - if (!mainPath || mainPath.endsWith('.cjs')) | ||
| 481 | - return false; | ||
| 482 | - const { readPackageScope } = require('internal/modules/cjs/loader'); | ||
| 483 | - const pkg = readPackageScope(mainPath); | ||
| 484 | - return pkg && pkg.data.type === 'module'; | ||
| 485 | - } | ||
| 486 | - | ||
| 487 | - function runMainESM(mainPath) { | ||
| 488 | - const esmLoader = require('internal/process/esm_loader'); | ||
| 489 | - const { pathToFileURL } = require('internal/url'); | ||
| 490 | - const { hasUncaughtExceptionCaptureCallback } = | ||
| 491 | - require('internal/process/execution'); | ||
| 492 | - return esmLoader.initializeLoader().then(() => { | ||
| 493 | - const main = path.isAbsolute(mainPath) ? | ||
| 494 | - pathToFileURL(mainPath).href : mainPath; | ||
| 495 | - return esmLoader.ESMLoader.import(main); | ||
| 496 | - }).catch((e) => { | ||
| 497 | - if (hasUncaughtExceptionCaptureCallback()) { | ||
| 498 | - process._fatalException(e); | ||
| 499 | - return; | ||
| 500 | - } | ||
| 501 | - internalBinding('errors').triggerUncaughtException( | ||
| 502 | - e, | ||
| 503 | - true /* fromPromise */ | ||
| 504 | - ); | ||
| 505 | - }); | ||
| 506 | - } | ||
| 507 | - | ||
| 508 | - | ||
| 509 | 456 | module.exports = { | |
| 510 | 457 | patchProcessObject, | |
| 511 | - resolveMainPath, | ||
| 512 | - runMainESM, | ||
| 513 | 458 | setupCoverageHooks, | |
| 514 | 459 | setupWarningHandler, | |
| 515 | 460 | setupDebugEnv, | |
| 516 | - shouldUseESMLoader, | ||
| 517 | 461 | prepareMainThreadExecution, | |
| 518 | 462 | initializeDeprecations, | |
| 519 | 463 | initializeESMLoader, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,11 +6,13 @@ const { | |||
| 6 | 6 | ||
| 7 | 7 | prepareMainThreadExecution(true); | |
| 8 | 8 | ||
| 9 | - const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 10 | - | ||
| 11 | 9 | markBootstrapComplete(); | |
| 12 | 10 | ||
| 13 | 11 | // Note: this loads the module through the ESM loader if | |
| 14 | 12 | // --experimental-loader is provided or --experimental-modules is on | |
| 15 | - // and the module is determined to be an ES module | ||
| 16 | - CJSModule.runMain(process.argv[1]); | ||
| 13 | + // and the module is determined to be an ES module. This hangs from the CJS | ||
| 14 | + // module loader because we currently allow monkey-patching of the module | ||
| 15 | + // loaders in the preloaded scripts through require('module'). | ||
| 16 | + // runMain here might be monkey-patched by users in --require. | ||
| 17 | + // XXX: the monkey-patchability here should probably be deprecated. | ||
| 18 | + require('internal/modules/cjs/loader').Module.runMain(process.argv[1]); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,6 +110,9 @@ port.on('message', (message) => { | |||
| 110 | 110 | initializeDeprecations(); | |
| 111 | 111 | initializeCJSLoader(); | |
| 112 | 112 | initializeESMLoader(); | |
| 113 | + | ||
| 114 | + const CJSLoader = require('internal/modules/cjs/loader'); | ||
| 115 | + assert(!CJSLoader.hasLoadedAnyUserCJSModule); | ||
| 113 | 116 | loadPreloadModules(); | |
| 114 | 117 | initializeFrozenIntrinsics(); | |
| 115 | 118 | publicWorker.parentPort = publicPort; | |
@@ -138,8 +141,9 @@ port.on('message', (message) => { | |||
| 138 | 141 | evalScript('[worker eval]', filename); | |
| 139 | 142 | } else { | |
| 140 | 143 | // script filename | |
| 141 | - const CJSModule = require('internal/modules/cjs/loader').Module; | ||
| 142 | - CJSModule.runMain(process.argv[1] = filename); | ||
| 144 | + // runMain here might be monkey-patched by users in --require. | ||
| 145 | + // XXX: the monkey-patchability here should probably be deprecated. | ||
| 146 | + CJSLoader.Module.runMain(process.argv[1] = filename); | ||
| 143 | 147 | } | |
| 144 | 148 | } else if (message.type === STDIO_PAYLOAD) { | |
| 145 | 149 | const { stream, chunk, encoding } = message; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,21 +74,23 @@ const manifest = getOptionValue('--experimental-policy') ? | |||
| 74 | 74 | null; | |
| 75 | 75 | const { compileFunction } = internalBinding('contextify'); | |
| 76 | 76 | ||
| 77 | + // Whether any user-provided CJS modules had been loaded (executed). | ||
| 78 | + // Used for internal assertions. | ||
| 79 | + let hasLoadedAnyUserCJSModule = false; | ||
| 80 | + | ||
| 77 | 81 | const { | |
| 78 | 82 | ERR_INVALID_ARG_VALUE, | |
| 79 | 83 | ERR_INVALID_OPT_VALUE, | |
| 80 | 84 | ERR_INVALID_PACKAGE_CONFIG, | |
| 81 | 85 | ERR_REQUIRE_ESM | |
| 82 | 86 | } = require('internal/errors').codes; | |
| 83 | 87 | const { validateString } = require('internal/validators'); | |
| 84 | - const { | ||
| 85 | - resolveMainPath, | ||
| 86 | - shouldUseESMLoader, | ||
| 87 | - runMainESM | ||
| 88 | - } = require('internal/bootstrap/pre_execution'); | ||
| 89 | 88 | const pendingDeprecation = getOptionValue('--pending-deprecation'); | |
| 90 | 89 | ||
| 91 | - module.exports = { wrapSafe, Module, toRealPath, readPackageScope }; | ||
| 90 | + module.exports = { | ||
| 91 | + wrapSafe, Module, toRealPath, readPackageScope, | ||
| 92 | + get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; } | ||
| 93 | + }; | ||
| 92 | 94 | ||
| 93 | 95 | let asyncESM, ModuleJob, ModuleWrap, kInstantiated; | |
| 94 | 96 | ||
@@ -1148,6 +1150,7 @@ Module.prototype._compile = function(content, filename) { | |||
| 1148 | 1150 | result = compiledWrapper.call(thisValue, exports, require, module, | |
| 1149 | 1151 | filename, dirname); | |
| 1150 | 1152 | } | |
| 1153 | + hasLoadedAnyUserCJSModule = true; | ||
| 1151 | 1154 | if (requireDepth === 0) statCache = null; | |
| 1152 | 1155 | return result; | |
| 1153 | 1156 | }; | |
@@ -1197,18 +1200,6 @@ Module._extensions['.node'] = function(module, filename) { | |||
| 1197 | 1200 | return process.dlopen(module, path.toNamespacedPath(filename)); | |
| 1198 | 1201 | }; | |
| 1199 | 1202 | ||
| 1200 | - // Bootstrap main module. | ||
| 1201 | - Module.runMain = function(main = process.argv[1]) { | ||
| 1202 | - const resolvedMain = resolveMainPath(main); | ||
| 1203 | - const useESMLoader = shouldUseESMLoader(resolvedMain); | ||
| 1204 | - module.exports.asyncRunMain = useESMLoader; | ||
| 1205 | - if (useESMLoader) { | ||
| 1206 | - runMainESM(resolvedMain || main); | ||
| 1207 | - } else { | ||
| 1208 | - Module._load(main, null, true); | ||
| 1209 | - } | ||
| 1210 | - }; | ||
| 1211 | - | ||
| 1212 | 1203 | function createRequireFromPath(filename) { | |
| 1213 | 1204 | // Allow a directory to be passed as the filename | |
| 1214 | 1205 | const trailingSlash = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,80 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const CJSLoader = require('internal/modules/cjs/loader'); | ||
| 4 | + const { Module, toRealPath, readPackageScope } = CJSLoader; | ||
| 5 | + const { getOptionValue } = require('internal/options'); | ||
| 6 | + const path = require('path'); | ||
| 7 | + | ||
| 8 | + function resolveMainPath(main) { | ||
| 9 | + // Note extension resolution for the main entry point can be deprecated in a | ||
| 10 | + // future major. | ||
| 11 | + // Module._findPath is monkey-patchable here. | ||
| 12 | + let mainPath = Module._findPath(path.resolve(main), null, true); | ||
| 13 | + if (!mainPath) | ||
| 14 | + return; | ||
| 15 | + | ||
| 16 | + const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); | ||
| 17 | + if (!preserveSymlinksMain) | ||
| 18 | + mainPath = toRealPath(mainPath); | ||
| 19 | + | ||
| 20 | + return mainPath; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + function shouldUseESMLoader(mainPath) { | ||
| 24 | + const experimentalModules = getOptionValue('--experimental-modules'); | ||
| 25 | + if (!experimentalModules) | ||
| 26 | + return false; | ||
| 27 | + const userLoader = getOptionValue('--experimental-loader'); | ||
| 28 | + if (userLoader) | ||
| 29 | + return true; | ||
| 30 | + const experimentalSpecifierResolution = | ||
| 31 | + getOptionValue('--experimental-specifier-resolution'); | ||
| 32 | + if (experimentalSpecifierResolution === 'node') | ||
| 33 | + return true; | ||
| 34 | + // Determine the module format of the main | ||
| 35 | + if (mainPath && mainPath.endsWith('.mjs')) | ||
| 36 | + return true; | ||
| 37 | + if (!mainPath || mainPath.endsWith('.cjs')) | ||
| 38 | + return false; | ||
| 39 | + const pkg = readPackageScope(mainPath); | ||
| 40 | + return pkg && pkg.data.type === 'module'; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + function runMainESM(mainPath) { | ||
| 44 | + const esmLoader = require('internal/process/esm_loader'); | ||
| 45 | + const { pathToFileURL } = require('internal/url'); | ||
| 46 | + const { hasUncaughtExceptionCaptureCallback } = | ||
| 47 | + require('internal/process/execution'); | ||
| 48 | + return esmLoader.initializeLoader().then(() => { | ||
| 49 | + const main = path.isAbsolute(mainPath) ? | ||
| 50 | + pathToFileURL(mainPath).href : mainPath; | ||
| 51 | + return esmLoader.ESMLoader.import(main); | ||
| 52 | + }).catch((e) => { | ||
| 53 | + if (hasUncaughtExceptionCaptureCallback()) { | ||
| 54 | + process._fatalException(e); | ||
| 55 | + return; | ||
| 56 | + } | ||
| 57 | + internalBinding('errors').triggerUncaughtException( | ||
| 58 | + e, | ||
| 59 | + true /* fromPromise */ | ||
| 60 | + ); | ||
| 61 | + }); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + // For backwards compatibility, we have to run a bunch of | ||
| 65 | + // monkey-patchable code that belongs to the CJS loader (exposed by | ||
| 66 | + // `require('module')`) even when the entry point is ESM. | ||
| 67 | + function executeUserEntryPoint(main = process.argv[1]) { | ||
| 68 | + const resolvedMain = resolveMainPath(main); | ||
| 69 | + const useESMLoader = shouldUseESMLoader(resolvedMain); | ||
| 70 | + if (useESMLoader) { | ||
| 71 | + runMainESM(resolvedMain || main); | ||
| 72 | + } else { | ||
| 73 | + // Module._load is the monkey-patchable CJS module loader. | ||
| 74 | + Module._load(main, null, true); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + module.exports = { | ||
| 79 | + executeUserEntryPoint | ||
| 80 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -145,6 +145,7 @@ | |||
| 145 | 145 | 'lib/internal/main/run_main_module.js', | |
| 146 | 146 | 'lib/internal/main/run_third_party_main.js', | |
| 147 | 147 | 'lib/internal/main/worker_thread.js', | |
| 148 | + 'lib/internal/modules/run_main.js', | ||
| 148 | 149 | 'lib/internal/modules/cjs/helpers.js', | |
| 149 | 150 | 'lib/internal/modules/cjs/loader.js', | |
| 150 | 151 | 'lib/internal/modules/esm/loader.js', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const oldRunMain = require('module').runMain; | ||
| 4 | + | ||
| 5 | + require('module').runMain = function(...args) { | ||
| 6 | + console.log('runMain is monkey patched!'); | ||
| 7 | + oldRunMain(...args); | ||
| 8 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,5 +10,5 @@ RangeError: Invalid input | |||
| 10 | 10 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) | |
| 11 | 11 | at Module.load (internal/modules/cjs/loader.js:*:*) | |
| 12 | 12 | at Function.Module._load (internal/modules/cjs/loader.js:*:*) | |
| 13 | - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) | ||
| 13 | + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) | ||
| 14 | 14 | at internal/main/run_main_module.js:*:* | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: | |||
| 12 | 12 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:*:*) | |
| 13 | 13 | at Module.load (internal/modules/cjs/loader.js:*:*) | |
| 14 | 14 | at Function.Module._load (internal/modules/cjs/loader.js:*:*) | |
| 15 | - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) | ||
| 15 | + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) | ||
| 16 | 16 | at internal/main/run_main_module.js:*:* { | |
| 17 | 17 | generatedMessage: true, | |
| 18 | 18 | code: 'ERR_ASSERTION', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,8 @@ Error: Cannot find package 'i-dont-exist' imported from * | |||
| 11 | 11 | at Loader.import (internal/modules/esm/loader.js:*:*) | |
| 12 | 12 | at internal/process/esm_loader.js:*:* | |
| 13 | 13 | at Object.initializeLoader (internal/process/esm_loader.js:*:*) | |
| 14 | - at runMainESM (internal/bootstrap/pre_execution.js:*:*) | ||
| 15 | - at Function.Module.runMain (internal/modules/cjs/loader.js:*:*) | ||
| 14 | + at runMainESM (internal/modules/run_main.js:*:*) | ||
| 15 | + at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) | ||
| 16 | 16 | at internal/main/run_main_module.js:*:* { | |
| 17 | 17 | code: 'ERR_MODULE_NOT_FOUND' | |
| 18 | 18 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments