GeezerVison is showing github.com — build: add --node-snapshot-main configure option · ide/node@37aee80
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: configure.py +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 788 | 788 | default=False, | 789 | 789 | help='node will load builtin modules from disk instead of from binary') | 790 | 790 |
| | 791 | +parser.add_argument('--node-snapshot-main', | | 792 | + action='store', | | 793 | + dest='node_snapshot_main', | | 794 | + default=None, | | 795 | + help='Run a file when building the embedded snapshot. Currently ' + | | 796 | + 'experimental.') | | 797 | + | 791 | 798 | # Create compile_commands.json in out/Debug and out/Release. | 792 | 799 | parser.add_argument('-C', | 793 | 800 | action='store_true', | @@ -1216,6 +1223,18 @@ def configure_node(o): | 1216 | 1223 |
| 1217 | 1224 | o['variables']['want_separate_host_toolset'] = int(cross_compiling) | 1218 | 1225 |
| | 1226 | + if options.node_snapshot_main is not None: | | 1227 | + if options.shared: | | 1228 | + # This should be possible to fix, but we will need to refactor the | | 1229 | + # libnode target to avoid building it twice. | | 1230 | + error('--node-snapshot-main is incompatible with --shared') | | 1231 | + if options.without_node_snapshot: | | 1232 | + error('--node-snapshot-main is incompatible with ' + | | 1233 | + '--without-node-snapshot') | | 1234 | + if cross_compiling: | | 1235 | + error('--node-snapshot-main is incompatible with cross compilation') | | 1236 | + o['variables']['node_snapshot_main'] = options.node_snapshot_main | | 1237 | + | 1219 | 1238 | if options.without_node_snapshot or options.node_builtin_modules_path: | 1220 | 1239 | o['variables']['node_use_node_snapshot'] = 'false' | 1221 | 1240 | else: | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,8 @@ const { Buffer } = require('buffer'); | 27 | 27 | const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes; | 28 | 28 | const assert = require('internal/assert'); | 29 | 29 |
| 30 | | -function prepareMainThreadExecution(expandArgv1 = false) { | | 30 | +function prepareMainThreadExecution(expandArgv1 = false, | | 31 | + initialzeModules = true) { | 31 | 32 | refreshRuntimeOptions(); | 32 | 33 |
| 33 | 34 | // TODO(joyeecheung): this is also necessary for workers when they deserialize | @@ -81,9 +82,13 @@ function prepareMainThreadExecution(expandArgv1 = false) { | 81 | 82 | initializeSourceMapsHandlers(); | 82 | 83 | initializeDeprecations(); | 83 | 84 | initializeWASI(); | | 85 | + | | 86 | + if (!initialzeModules) { | | 87 | + return; | | 88 | + } | | 89 | + | 84 | 90 | initializeCJSLoader(); | 85 | 91 | initializeESMLoader(); | 86 | | - | 87 | 92 | const CJSLoader = require('internal/modules/cjs/loader'); | 88 | 93 | assert(!CJSLoader.hasLoadedAnyUserCJSModule); | 89 | 94 | loadPreloadModules(); | @@ -102,7 +107,8 @@ function patchProcessObject(expandArgv1) { | 102 | 107 |
| 103 | 108 | ObjectDefineProperty(process, 'argv0', { | 104 | 109 | enumerable: true, | 105 | | - configurable: false, | | 110 | + // Only set it to true during snapshot building. | | 111 | + configurable: getOptionValue('--build-snapshot'), | 106 | 112 | value: process.argv[0] | 107 | 113 | }); | 108 | 114 | process.argv[0] = process.execPath; | |
Collapse file Copy file name to clipboard +142Lines changed: 142 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const { | | 4 | + Error, | | 5 | + SafeSet, | | 6 | + SafeArrayIterator | | 7 | +} = primordials; | | 8 | + | | 9 | +const binding = internalBinding('mksnapshot'); | | 10 | +const { NativeModule } = require('internal/bootstrap/loaders'); | | 11 | +const { | | 12 | + compileSnapshotMain, | | 13 | +} = binding; | | 14 | + | | 15 | +const { | | 16 | + getOptionValue | | 17 | +} = require('internal/options'); | | 18 | + | | 19 | +const { | | 20 | + readFileSync | | 21 | +} = require('fs'); | | 22 | + | | 23 | +const supportedModules = new SafeSet(new SafeArrayIterator([ | | 24 | + // '_http_agent', | | 25 | + // '_http_client', | | 26 | + // '_http_common', | | 27 | + // '_http_incoming', | | 28 | + // '_http_outgoing', | | 29 | + // '_http_server', | | 30 | + '_stream_duplex', | | 31 | + '_stream_passthrough', | | 32 | + '_stream_readable', | | 33 | + '_stream_transform', | | 34 | + '_stream_wrap', | | 35 | + '_stream_writable', | | 36 | + // '_tls_common', | | 37 | + // '_tls_wrap', | | 38 | + 'assert', | | 39 | + 'assert/strict', | | 40 | + // 'async_hooks', | | 41 | + 'buffer', | | 42 | + // 'child_process', | | 43 | + // 'cluster', | | 44 | + 'console', | | 45 | + 'constants', | | 46 | + 'crypto', | | 47 | + // 'dgram', | | 48 | + // 'diagnostics_channel', | | 49 | + // 'dns', | | 50 | + // 'dns/promises', | | 51 | + // 'domain', | | 52 | + 'events', | | 53 | + 'fs', | | 54 | + 'fs/promises', | | 55 | + // 'http', | | 56 | + // 'http2', | | 57 | + // 'https', | | 58 | + // 'inspector', | | 59 | + // 'module', | | 60 | + // 'net', | | 61 | + 'os', | | 62 | + 'path', | | 63 | + 'path/posix', | | 64 | + 'path/win32', | | 65 | + // 'perf_hooks', | | 66 | + 'process', | | 67 | + 'punycode', | | 68 | + 'querystring', | | 69 | + // 'readline', | | 70 | + // 'repl', | | 71 | + 'stream', | | 72 | + 'stream/promises', | | 73 | + 'string_decoder', | | 74 | + 'sys', | | 75 | + 'timers', | | 76 | + 'timers/promises', | | 77 | + // 'tls', | | 78 | + // 'trace_events', | | 79 | + // 'tty', | | 80 | + 'url', | | 81 | + 'util', | | 82 | + 'util/types', | | 83 | + 'v8', | | 84 | + // 'vm', | | 85 | + // 'worker_threads', | | 86 | + // 'zlib', | | 87 | +])); | | 88 | + | | 89 | +const warnedModules = new SafeSet(); | | 90 | +function supportedInUserSnapshot(id) { | | 91 | + return supportedModules.has(id); | | 92 | +} | | 93 | + | | 94 | +function requireForUserSnapshot(id) { | | 95 | + if (!NativeModule.canBeRequiredByUsers(id)) { | | 96 | + // eslint-disable-next-line no-restricted-syntax | | 97 | + const err = new Error( | | 98 | + `Cannot find module '${id}'. ` | | 99 | + ); | | 100 | + err.code = 'MODULE_NOT_FOUND'; | | 101 | + throw err; | | 102 | + } | | 103 | + if (!supportedInUserSnapshot(id)) { | | 104 | + if (!warnedModules.has(id)) { | | 105 | + process.emitWarning( | | 106 | + `built-in module ${id} is not yet supported in user snapshots`); | | 107 | + warnedModules.add(id); | | 108 | + } | | 109 | + } | | 110 | + | | 111 | + return require(id); | | 112 | +} | | 113 | + | | 114 | +function main() { | | 115 | + const { | | 116 | + prepareMainThreadExecution | | 117 | + } = require('internal/bootstrap/pre_execution'); | | 118 | + | | 119 | + prepareMainThreadExecution(true, false); | | 120 | + process.once('beforeExit', function runCleanups() { | | 121 | + for (const cleanup of binding.cleanups) { | | 122 | + cleanup(); | | 123 | + } | | 124 | + }); | | 125 | + | | 126 | + const file = process.argv[1]; | | 127 | + const path = require('path'); | | 128 | + const filename = path.resolve(file); | | 129 | + const dirname = path.dirname(filename); | | 130 | + const source = readFileSync(file, 'utf-8'); | | 131 | + const snapshotMainFunction = compileSnapshotMain(filename, source); | | 132 | + | | 133 | + if (getOptionValue('--inspect-brk')) { | | 134 | + internalBinding('inspector').callAndPauseOnStart( | | 135 | + snapshotMainFunction, undefined, | | 136 | + requireForUserSnapshot, filename, dirname); | | 137 | + } else { | | 138 | + snapshotMainFunction(requireForUserSnapshot, filename, dirname); | | 139 | + } | | 140 | +} | | 141 | + | | 142 | +main(); |
Collapse file Copy file name to clipboardExpand all lines: node.gyp +39-14Lines changed: 39 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | 'node_use_dtrace%': 'false', | 8 | 8 | 'node_use_etw%': 'false', | 9 | 9 | 'node_no_browser_globals%': 'false', | | 10 | + 'node_snapshot_main%': '', | 10 | 11 | 'node_use_node_snapshot%': 'false', | 11 | 12 | 'node_use_v8_platform%': 'true', | 12 | 13 | 'node_use_bundled_v8%': 'true', | | 315 | 316 | 'dependencies': [ | 316 | 317 | 'node_mksnapshot', | 317 | 318 | ], | 318 | | - 'actions': [ | 319 | | - { | 320 | | - 'action_name': 'node_mksnapshot', | 321 | | - 'process_outputs_as_sources': 1, | 322 | | - 'inputs': [ | 323 | | - '<(node_mksnapshot_exec)', | 324 | | - ], | 325 | | - 'outputs': [ | 326 | | - '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', | | 319 | + 'conditions': [ | | 320 | + ['node_snapshot_main!=""', { | | 321 | + 'actions': [ | | 322 | + { | | 323 | + 'action_name': 'node_mksnapshot', | | 324 | + 'process_outputs_as_sources': 1, | | 325 | + 'inputs': [ | | 326 | + '<(node_mksnapshot_exec)', | | 327 | + '<(node_snapshot_main)', | | 328 | + ], | | 329 | + 'outputs': [ | | 330 | + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', | | 331 | + ], | | 332 | + 'action': [ | | 333 | + '<(node_mksnapshot_exec)', | | 334 | + '--build-snapshot', | | 335 | + '<(node_snapshot_main)', | | 336 | + '<@(_outputs)', | | 337 | + ], | | 338 | + }, | 327 | 339 | ], | 328 | | - 'action': [ | 329 | | - '<@(_inputs)', | 330 | | - '<@(_outputs)', | | 340 | + }, { | | 341 | + 'actions': [ | | 342 | + { | | 343 | + 'action_name': 'node_mksnapshot', | | 344 | + 'process_outputs_as_sources': 1, | | 345 | + 'inputs': [ | | 346 | + '<(node_mksnapshot_exec)', | | 347 | + ], | | 348 | + 'outputs': [ | | 349 | + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', | | 350 | + ], | | 351 | + 'action': [ | | 352 | + '<@(_inputs)', | | 353 | + '<@(_outputs)', | | 354 | + ], | | 355 | + }, | 331 | 356 | ], | 332 | | - }, | | 357 | + }], | 333 | 358 | ], | 334 | | - }, { | | 359 | + }, { | 335 | 360 | 'sources': [ | 336 | 361 | 'src/node_snapshot_stub.cc' | 337 | 362 | ], | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -495,6 +495,10 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { | 495 | 495 | return StartExecution(env, "internal/main/inspect"); | 496 | 496 | } | 497 | 497 |
| | 498 | + if (per_process::cli_options->build_snapshot) { | | 499 | + return StartExecution(env, "internal/main/mksnapshot"); | | 500 | + } | | 501 | + | 498 | 502 | if (per_process::cli_options->print_help) { | 499 | 503 | return StartExecution(env, "internal/main/print_help"); | 500 | 504 | } | @@ -1153,6 +1157,12 @@ int Start(int argc, char** argv) { | 1153 | 1157 | return result.exit_code; | 1154 | 1158 | } | 1155 | 1159 |
| | 1160 | + if (per_process::cli_options->build_snapshot) { | | 1161 | + fprintf(stderr, | | 1162 | + "--build-snapshot is not yet supported in the node binary\n"); | | 1163 | + return 1; | | 1164 | + } | | 1165 | + | 1156 | 1166 | { | 1157 | 1167 | bool use_node_snapshot = | 1158 | 1168 | per_process::cli_options->per_isolate->node_snapshot; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_binding.cc +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 59 | 59 | V(js_udp_wrap) \ | 60 | 60 | V(messaging) \ | 61 | 61 | V(module_wrap) \ | | 62 | + V(mksnapshot) \ | 62 | 63 | V(native_module) \ | 63 | 64 | V(options) \ | 64 | 65 | V(os) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_external_reference.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,7 @@ class ExternalReferenceRegistry { | 66 | 66 | V(handle_wrap) \ | 67 | 67 | V(heap_utils) \ | 68 | 68 | V(messaging) \ | | 69 | + V(mksnapshot) \ | 69 | 70 | V(native_module) \ | 70 | 71 | V(options) \ | 71 | 72 | V(os) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -725,6 +725,11 @@ PerProcessOptionsParser::PerProcessOptionsParser( | 725 | 725 | "disable Object.prototype.__proto__", | 726 | 726 | &PerProcessOptions::disable_proto, | 727 | 727 | kAllowedInEnvironment); | | 728 | + AddOption("--build-snapshot", | | 729 | + "Generate a snapshot blob when the process exits." | | 730 | + "Currently only supported in the node_mksnapshot binary.", | | 731 | + &PerProcessOptions::build_snapshot, | | 732 | + kDisallowedInEnvironment); | 728 | 733 |
| 729 | 734 | // 12.x renamed this inadvertently, so alias it for consistency within the | 730 | 735 | // release line, while using the original name for consistency with older | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -229,6 +229,7 @@ class PerProcessOptions : public Options { | 229 | 229 | bool zero_fill_all_buffers = false; | 230 | 230 | bool debug_arraybuffer_allocations = false; | 231 | 231 | std::string disable_proto; | | 232 | + bool build_snapshot; | 232 | 233 |
| 233 | 234 | std::vector<std::string> security_reverts; | 234 | 235 | bool print_bash_completion = false; | |
|