-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
src: refactor embedded entrypoint loading #53573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,116 @@ | ||
| 'use strict'; | ||
|
|
||
| // This main script is currently only run when LoadEnvironment() | ||
| // is run with a non-null StartExecutionCallback or a UTF8 | ||
| // main script. Effectively there are two cases where this happens: | ||
| // 1. It's a single-executable application *loading* a main script | ||
| // bundled into the executable. This is currently done from | ||
| // NodeMainInstance::Run(). | ||
| // 2. It's an embedder application and LoadEnvironment() is invoked | ||
| // as described above. | ||
|
|
||
| const { | ||
| prepareMainThreadExecution, | ||
| } = require('internal/process/pre_execution'); | ||
| const { isExperimentalSeaWarningNeeded } = internalBinding('sea'); | ||
| const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); | ||
| const { emitExperimentalWarning } = require('internal/util'); | ||
| const { embedderRequire, embedderRunCjs } = require('internal/util/embedding'); | ||
| const { emitWarningSync } = require('internal/process/warning'); | ||
| const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); | ||
| const { Module } = require('internal/modules/cjs/loader'); | ||
| const { compileFunctionForCJSLoader } = internalBinding('contextify'); | ||
| const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); | ||
|
|
||
| const { codes: { | ||
| ERR_UNKNOWN_BUILTIN_MODULE, | ||
| } } = require('internal/errors'); | ||
|
|
||
| // Don't expand process.argv[1] because in a single-executable application or an | ||
| // embedder application, the user main script isn't necessarily provided via the | ||
| // command line (e.g. it could be provided via an API or bundled into the executable). | ||
| prepareMainThreadExecution(false, true); | ||
|
|
||
| const isLoadingSea = isSea(); | ||
| if (isExperimentalSeaWarningNeeded()) { | ||
| emitExperimentalWarning('Single executable application'); | ||
| } | ||
|
|
||
| // This is roughly the same as: | ||
| // | ||
| // const mod = new Module(filename); | ||
| // mod._compile(content, filename); | ||
| // | ||
| // but the code has been duplicated because currently there is no way to set the | ||
| // value of require.main to module. | ||
| // | ||
| // TODO(RaisinTen): Find a way to deduplicate this. | ||
| function embedderRunCjs(content) { | ||
| // The filename of the module (used for CJS module lookup) | ||
| // is always the same as the location of the executable itself | ||
| // at the time of the loading (which means it changes depending | ||
| // on where the executable is in the file system). | ||
| const filename = process.execPath; | ||
| const customModule = new Module(filename, null); | ||
|
|
||
| const { | ||
| function: compiledWrapper, | ||
| cachedDataRejected, | ||
| sourceMapURL, | ||
| } = compileFunctionForCJSLoader( | ||
| content, | ||
| filename, | ||
| isLoadingSea, // is_sea_main | ||
| false, // should_detect_module, ESM should be supported differently for embedded code | ||
| ); | ||
| // Cache the source map for the module if present. | ||
| if (sourceMapURL) { | ||
| maybeCacheSourceMap( | ||
| filename, | ||
| content, | ||
| customModule, | ||
| false, // isGeneratedSource | ||
| undefined, // sourceURL, TODO(joyeecheung): should be extracted by V8 | ||
| sourceMapURL, | ||
| ); | ||
| } | ||
|
|
||
| // cachedDataRejected is only set if cache from SEA is used. | ||
| if (cachedDataRejected !== false && isLoadingSea) { | ||
| emitWarningSync('Code cache data rejected.'); | ||
| } | ||
|
|
||
| // Patch the module to make it look almost like a regular CJS module | ||
| // instance. | ||
| customModule.filename = process.execPath; | ||
| customModule.paths = Module._nodeModulePaths(process.execPath); | ||
| embedderRequire.main = customModule; | ||
|
|
||
| return compiledWrapper( | ||
| customModule.exports, // exports | ||
| embedderRequire, // require | ||
| customModule, // module | ||
| process.execPath, // __filename | ||
| customModule.path, // __dirname | ||
| ); | ||
| } | ||
|
|
||
| let warnedAboutBuiltins = false; | ||
|
|
||
| function embedderRequire(id) { | ||
| const normalizedId = normalizeRequirableId(id); | ||
| if (!normalizedId) { | ||
| if (isLoadingSea && !warnedAboutBuiltins) { | ||
| emitWarningSync( | ||
| 'Currently the require() provided to the main script embedded into ' + | ||
| 'single-executable applications only supports loading built-in modules.\n' + | ||
| 'To load a module from disk after the single executable application is ' + | ||
| 'launched, use require("module").createRequire().\n' + | ||
| 'Support for bundled module loading or virtual file systems are under ' + | ||
| 'discussions in https://github.com/nodejs/single-executable'); | ||
| warnedAboutBuiltins = true; | ||
| } | ||
| throw new ERR_UNKNOWN_BUILTIN_MODULE(id); | ||
| } | ||
| return require(normalizedId); | ||
| } | ||
|
|
||
| return [process, embedderRequire, embedderRunCjs]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.