From c7504883e48c0dbf7de99243503a378fb8a7592d Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 14 Oct 2019 17:33:31 -0400 Subject: [PATCH] move main functions into pre_execution --- lib/internal/bootstrap/pre_execution.js | 60 ++++++++++++++++++++++++ lib/internal/modules/cjs/loader.js | 2 +- lib/internal/process/main.js | 61 ------------------------- node.gyp | 1 - test/parallel/test-bootstrap-modules.js | 1 - 5 files changed, 61 insertions(+), 64 deletions(-) delete mode 100644 lib/internal/process/main.js diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index fd5aa1d1748a95..c2d72fb0baf272 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -5,6 +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'); function prepareMainThreadExecution(expandArgv1 = false) { // Patch the process object with legacy properties and normalizations @@ -437,11 +438,70 @@ 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 experimentalModules = getOptionValue('--experimental-modules'); + if (!experimentalModules) + return false; + 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() || Promise.resolve()).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, diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 67c5d1ecf49469..94690e87800a5e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -73,7 +73,7 @@ const { resolveMainPath, shouldUseESMLoader, runMainESM -} = require('internal/process/main'); +} = require('internal/bootstrap/pre_execution'); const pendingDeprecation = getOptionValue('--pending-deprecation'); module.exports = { wrapSafe, Module, toRealPath, readPackageScope }; diff --git a/lib/internal/process/main.js b/lib/internal/process/main.js deleted file mode 100644 index 2948040973b538..00000000000000 --- a/lib/internal/process/main.js +++ /dev/null @@ -1,61 +0,0 @@ -'use strict'; -const path = require('path'); -const { getOptionValue } = require('internal/options'); - -exports.resolveMainPath = resolveMainPath; -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; -} - -exports.shouldUseESMLoader = shouldUseESMLoader; -function shouldUseESMLoader(mainPath) { - const experimentalModules = getOptionValue('--experimental-modules'); - if (!experimentalModules) - return false; - 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'; -} - -exports.runMainESM = runMainESM; -function runMainESM(mainPath) { - const esmLoader = require('internal/process/esm_loader'); - const { pathToFileURL } = require('internal/url'); - const { hasUncaughtExceptionCaptureCallback } = - require('internal/process/execution'); - return (esmLoader.initializeLoader() || Promise.resolve()).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 */ - ); - }); - }); -} diff --git a/node.gyp b/node.gyp index 2cfb1db476fbf2..9bf8fa2b994da1 100644 --- a/node.gyp +++ b/node.gyp @@ -160,7 +160,6 @@ 'lib/internal/priority_queue.js', 'lib/internal/process/esm_loader.js', 'lib/internal/process/execution.js', - 'lib/internal/process/main.js', 'lib/internal/process/main_thread_only.js', 'lib/internal/process/per_thread.js', 'lib/internal/process/policy.js', diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index ec99bb1b091202..939658a3b0553f 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -51,7 +51,6 @@ const expectedModules = new Set([ 'NativeModule internal/options', 'NativeModule internal/priority_queue', 'NativeModule internal/process/execution', - 'NativeModule internal/process/main', 'NativeModule internal/process/per_thread', 'NativeModule internal/process/promises', 'NativeModule internal/process/task_queues',