|
| 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(); |
0 commit comments