|
1 | 1 | 'use strict';
|
| 2 | + |
| 3 | +// This main script is currently only run when LoadEnvironment() |
| 4 | +// is run with a non-null StartExecutionCallback or a UTF8 |
| 5 | +// main script. Effectively there are two cases where this happens: |
| 6 | +// 1. It's a single-executable application *loading* a main script |
| 7 | +// bundled into the executable. This is currently done from |
| 8 | +// NodeMainInstance::Run(). |
| 9 | +// 2. It's an embedder application and LoadEnvironment() is invoked |
| 10 | +// as described above. |
| 11 | + |
2 | 12 | const {
|
3 | 13 | prepareMainThreadExecution,
|
4 | 14 | } = require('internal/process/pre_execution');
|
5 |
| -const { isExperimentalSeaWarningNeeded } = internalBinding('sea'); |
| 15 | +const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); |
6 | 16 | const { emitExperimentalWarning } = require('internal/util');
|
7 |
| -const { embedderRequire, embedderRunCjs } = require('internal/util/embedding'); |
| 17 | +const { emitWarningSync } = require('internal/process/warning'); |
| 18 | +const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm'); |
| 19 | +const { Module } = require('internal/modules/cjs/loader'); |
| 20 | +const { compileFunctionForCJSLoader } = internalBinding('contextify'); |
| 21 | +const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); |
| 22 | + |
| 23 | +const { codes: { |
| 24 | + ERR_UNKNOWN_BUILTIN_MODULE, |
| 25 | +} } = require('internal/errors'); |
8 | 26 |
|
| 27 | +// Don't expand process.argv[1] because in a single-executable application or an |
| 28 | +// embedder application, the user main script isn't necessarily provided via the |
| 29 | +// command line (e.g. it could be provided via an API or bundled into the executable). |
9 | 30 | prepareMainThreadExecution(false, true);
|
10 | 31 |
|
| 32 | +const isLoadingSea = isSea(); |
11 | 33 | if (isExperimentalSeaWarningNeeded()) {
|
12 | 34 | emitExperimentalWarning('Single executable application');
|
13 | 35 | }
|
14 | 36 |
|
| 37 | +// This is roughly the same as: |
| 38 | +// |
| 39 | +// const mod = new Module(filename); |
| 40 | +// mod._compile(content, filename); |
| 41 | +// |
| 42 | +// but the code has been duplicated because currently there is no way to set the |
| 43 | +// value of require.main to module. |
| 44 | +// |
| 45 | +// TODO(RaisinTen): Find a way to deduplicate this. |
| 46 | +function embedderRunCjs(content) { |
| 47 | + // The filename of the module (used for CJS module lookup) |
| 48 | + // is always the same as the location of the executable itself |
| 49 | + // at the time of the loading (which means it changes depending |
| 50 | + // on where the executable is in the file system). |
| 51 | + const filename = process.execPath; |
| 52 | + const customModule = new Module(filename, null); |
| 53 | + |
| 54 | + const { |
| 55 | + function: compiledWrapper, |
| 56 | + cachedDataRejected, |
| 57 | + sourceMapURL, |
| 58 | + } = compileFunctionForCJSLoader( |
| 59 | + content, |
| 60 | + filename, |
| 61 | + isLoadingSea, // is_sea_main |
| 62 | + false, // should_detect_module, ESM should be supported differently for embedded code |
| 63 | + ); |
| 64 | + // Cache the source map for the module if present. |
| 65 | + if (sourceMapURL) { |
| 66 | + maybeCacheSourceMap( |
| 67 | + filename, |
| 68 | + content, |
| 69 | + customModule, |
| 70 | + false, // isGeneratedSource |
| 71 | + undefined, // sourceURL, TODO(joyeecheung): should be extracted by V8 |
| 72 | + sourceMapURL, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // cachedDataRejected is only set if cache from SEA is used. |
| 77 | + if (cachedDataRejected !== false && isLoadingSea) { |
| 78 | + emitWarningSync('Code cache data rejected.'); |
| 79 | + } |
| 80 | + |
| 81 | + // Patch the module to make it look almost like a regular CJS module |
| 82 | + // instance. |
| 83 | + customModule.filename = process.execPath; |
| 84 | + customModule.paths = Module._nodeModulePaths(process.execPath); |
| 85 | + embedderRequire.main = customModule; |
| 86 | + |
| 87 | + return compiledWrapper( |
| 88 | + customModule.exports, // exports |
| 89 | + embedderRequire, // require |
| 90 | + customModule, // module |
| 91 | + process.execPath, // __filename |
| 92 | + customModule.path, // __dirname |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +let warnedAboutBuiltins = false; |
| 97 | + |
| 98 | +function embedderRequire(id) { |
| 99 | + const normalizedId = normalizeRequirableId(id); |
| 100 | + if (!normalizedId) { |
| 101 | + if (isLoadingSea && !warnedAboutBuiltins) { |
| 102 | + emitWarningSync( |
| 103 | + 'Currently the require() provided to the main script embedded into ' + |
| 104 | + 'single-executable applications only supports loading built-in modules.\n' + |
| 105 | + 'To load a module from disk after the single executable application is ' + |
| 106 | + 'launched, use require("module").createRequire().\n' + |
| 107 | + 'Support for bundled module loading or virtual file systems are under ' + |
| 108 | + 'discussions in https://github.com/nodejs/single-executable'); |
| 109 | + warnedAboutBuiltins = true; |
| 110 | + } |
| 111 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); |
| 112 | + } |
| 113 | + return require(normalizedId); |
| 114 | +} |
| 115 | + |
15 | 116 | return [process, embedderRequire, embedderRunCjs];
|
0 commit comments