summaryrefslogtreecommitdiff
path: root/lib/internal/bootstrap/pre_execution.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-11-10 16:59:16 +0800
committerAnna Henningsen <anna@addaleax.net>2019-11-19 13:43:59 +0100
commitefce655c0f1671d0e86b5c89092ac93db983ef94 (patch)
tree26af173e687a6c8ee0f26d2857742ffce01729f2 /lib/internal/bootstrap/pre_execution.js
parent0f78dcc86d9af8f742f76505c5a104c6dff17ca9 (diff)
downloadandroid-node-v8-efce655c0f1671d0e86b5c89092ac93db983ef94.tar.gz
android-node-v8-efce655c0f1671d0e86b5c89092ac93db983ef94.tar.bz2
android-node-v8-efce655c0f1671d0e86b5c89092ac93db983ef94.zip
module: reduce circular dependency of internal/modules/cjs/loader
Previously `internal/bootstrap/pre_execution.js` requires `internal/modules/cjs/loader.js` which in turn requires `internal/bootstrap/pre_execution.js`. This patch moves the entry point execution logic out of `pre_execution.js` and puts it into `internal/modules/run_main.js`. It also tests that `Module.runMain` can be monkey-patched before further deprecation/refactoring can be done. Also added an internal assertion `hasLoadedAnyUserCJSModule` for documentation purposes. PR-URL: https://github.com/nodejs/node/pull/30349 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/internal/bootstrap/pre_execution.js')
-rw-r--r--lib/internal/bootstrap/pre_execution.js67
1 files changed, 9 insertions, 58 deletions
diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js
index 793ee18142..8edec86a3e 100644
--- a/lib/internal/bootstrap/pre_execution.js
+++ b/lib/internal/bootstrap/pre_execution.js
@@ -5,7 +5,7 @@ const { Object, SafeWeakMap } = primordials;
const { getOptionValue } = require('internal/options');
const { Buffer } = require('buffer');
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
-const path = require('path');
+const assert = require('internal/assert');
function prepareMainThreadExecution(expandArgv1 = false) {
// Patch the process object with legacy properties and normalizations
@@ -60,6 +60,9 @@ function prepareMainThreadExecution(expandArgv1 = false) {
initializeDeprecations();
initializeCJSLoader();
initializeESMLoader();
+
+ const CJSLoader = require('internal/modules/cjs/loader');
+ assert(!CJSLoader.hasLoadedAnyUserCJSModule);
loadPreloadModules();
initializeFrozenIntrinsics();
}
@@ -394,7 +397,11 @@ function initializePolicy() {
}
function initializeCJSLoader() {
- require('internal/modules/cjs/loader').Module._initPaths();
+ const CJSLoader = require('internal/modules/cjs/loader');
+ CJSLoader.Module._initPaths();
+ // TODO(joyeecheung): deprecate this in favor of a proper hook?
+ CJSLoader.Module.runMain =
+ require('internal/modules/run_main').executeUserEntryPoint;
}
function initializeESMLoader() {
@@ -433,67 +440,11 @@ function loadPreloadModules() {
}
}
-function resolveMainPath(main) {
- const { toRealPath, Module: CJSModule } =
- require('internal/modules/cjs/loader');
-
- // Note extension resolution for the main entry point can be deprecated in a
- // future major.
- let mainPath = CJSModule._findPath(path.resolve(main), null, true);
- if (!mainPath)
- return;
-
- const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
- if (!preserveSymlinksMain)
- mainPath = toRealPath(mainPath);
-
- return mainPath;
-}
-
-function shouldUseESMLoader(mainPath) {
- const userLoader = getOptionValue('--experimental-loader');
- if (userLoader)
- return true;
- // Determine the module format of the main
- if (mainPath && mainPath.endsWith('.mjs'))
- return true;
- if (!mainPath || mainPath.endsWith('.cjs'))
- return false;
- const { readPackageScope } = require('internal/modules/cjs/loader');
- const pkg = readPackageScope(mainPath);
- return pkg && pkg.data.type === 'module';
-}
-
-function runMainESM(mainPath) {
- const esmLoader = require('internal/process/esm_loader');
- const { pathToFileURL } = require('internal/url');
- const { hasUncaughtExceptionCaptureCallback } =
- require('internal/process/execution');
- return esmLoader.initializeLoader().then(() => {
- const main = path.isAbsolute(mainPath) ?
- pathToFileURL(mainPath).href : mainPath;
- return esmLoader.ESMLoader.import(main);
- }).catch((e) => {
- if (hasUncaughtExceptionCaptureCallback()) {
- process._fatalException(e);
- return;
- }
- internalBinding('errors').triggerUncaughtException(
- e,
- true /* fromPromise */
- );
- });
-}
-
-
module.exports = {
patchProcessObject,
- resolveMainPath,
- runMainESM,
setupCoverageHooks,
setupWarningHandler,
setupDebugEnv,
- shouldUseESMLoader,
prepareMainThreadExecution,
initializeDeprecations,
initializeESMLoader,