11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci NumberParseInt, 51cb0ef41Sopenharmony_ci ObjectDefineProperties, 61cb0ef41Sopenharmony_ci ObjectDefineProperty, 71cb0ef41Sopenharmony_ci SafeMap, 81cb0ef41Sopenharmony_ci StringPrototypeStartsWith, 91cb0ef41Sopenharmony_ci Symbol, 101cb0ef41Sopenharmony_ci SymbolDispose, 111cb0ef41Sopenharmony_ci SymbolAsyncDispose, 121cb0ef41Sopenharmony_ci globalThis, 131cb0ef41Sopenharmony_ci} = primordials; 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst { 161cb0ef41Sopenharmony_ci getOptionValue, 171cb0ef41Sopenharmony_ci refreshOptions, 181cb0ef41Sopenharmony_ci} = require('internal/options'); 191cb0ef41Sopenharmony_ciconst { reconnectZeroFillToggle } = require('internal/buffer'); 201cb0ef41Sopenharmony_ciconst { 211cb0ef41Sopenharmony_ci defineOperation, 221cb0ef41Sopenharmony_ci exposeInterface, 231cb0ef41Sopenharmony_ci exposeLazyInterfaces, 241cb0ef41Sopenharmony_ci defineReplaceableLazyAttribute, 251cb0ef41Sopenharmony_ci setupCoverageHooks, 261cb0ef41Sopenharmony_ci} = require('internal/util'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst { 291cb0ef41Sopenharmony_ci ERR_MANIFEST_ASSERT_INTEGRITY, 301cb0ef41Sopenharmony_ci} = require('internal/errors').codes; 311cb0ef41Sopenharmony_ciconst assert = require('internal/assert'); 321cb0ef41Sopenharmony_ciconst { 331cb0ef41Sopenharmony_ci namespace: { 341cb0ef41Sopenharmony_ci addSerializeCallback, 351cb0ef41Sopenharmony_ci isBuildingSnapshot, 361cb0ef41Sopenharmony_ci }, 371cb0ef41Sopenharmony_ci} = require('internal/v8/startup_snapshot'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cifunction prepareMainThreadExecution(expandArgv1 = false, initializeModules = true) { 401cb0ef41Sopenharmony_ci return prepareExecution({ 411cb0ef41Sopenharmony_ci expandArgv1, 421cb0ef41Sopenharmony_ci initializeModules, 431cb0ef41Sopenharmony_ci isMainThread: true, 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_cifunction prepareWorkerThreadExecution() { 481cb0ef41Sopenharmony_ci prepareExecution({ 491cb0ef41Sopenharmony_ci expandArgv1: false, 501cb0ef41Sopenharmony_ci initializeModules: false, // Will need to initialize it after policy setup 511cb0ef41Sopenharmony_ci isMainThread: false, 521cb0ef41Sopenharmony_ci }); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifunction prepareExecution(options) { 561cb0ef41Sopenharmony_ci const { expandArgv1, initializeModules, isMainThread } = options; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci refreshRuntimeOptions(); 591cb0ef41Sopenharmony_ci reconnectZeroFillToggle(); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci // Patch the process object and get the resolved main entry point. 621cb0ef41Sopenharmony_ci const mainEntry = patchProcessObject(expandArgv1); 631cb0ef41Sopenharmony_ci setupTraceCategoryState(); 641cb0ef41Sopenharmony_ci setupPerfHooks(); 651cb0ef41Sopenharmony_ci setupInspectorHooks(); 661cb0ef41Sopenharmony_ci setupWarningHandler(); 671cb0ef41Sopenharmony_ci setupFetch(); 681cb0ef41Sopenharmony_ci setupWebCrypto(); 691cb0ef41Sopenharmony_ci setupCustomEvent(); 701cb0ef41Sopenharmony_ci setupCodeCoverage(); 711cb0ef41Sopenharmony_ci setupDebugEnv(); 721cb0ef41Sopenharmony_ci // Process initial diagnostic reporting configuration, if present. 731cb0ef41Sopenharmony_ci initializeReport(); 741cb0ef41Sopenharmony_ci initializeSourceMapsHandlers(); 751cb0ef41Sopenharmony_ci initializeDeprecations(); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci require('internal/dns/utils').initializeDns(); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci setupSymbolDisposePolyfill(); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci if (isMainThread) { 821cb0ef41Sopenharmony_ci assert(internalBinding('worker').isMainThread); 831cb0ef41Sopenharmony_ci // Worker threads will get the manifest in the message handler. 841cb0ef41Sopenharmony_ci const policy = readPolicyFromDisk(); 851cb0ef41Sopenharmony_ci if (policy) { 861cb0ef41Sopenharmony_ci require('internal/process/policy') 871cb0ef41Sopenharmony_ci .setup(policy.manifestSrc, policy.manifestURL); 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci // Print stack trace on `SIGINT` if option `--trace-sigint` presents. 911cb0ef41Sopenharmony_ci setupStacktracePrinterOnSigint(); 921cb0ef41Sopenharmony_ci initializeReportSignalHandlers(); // Main-thread-only. 931cb0ef41Sopenharmony_ci initializeHeapSnapshotSignalHandlers(); 941cb0ef41Sopenharmony_ci // If the process is spawned with env NODE_CHANNEL_FD, it's probably 951cb0ef41Sopenharmony_ci // spawned by our child_process module, then initialize IPC. 961cb0ef41Sopenharmony_ci // This attaches some internal event listeners and creates: 971cb0ef41Sopenharmony_ci // process.send(), process.channel, process.connected, 981cb0ef41Sopenharmony_ci // process.disconnect(). 991cb0ef41Sopenharmony_ci setupChildProcessIpcChannel(); 1001cb0ef41Sopenharmony_ci // If this is a worker in cluster mode, start up the communication 1011cb0ef41Sopenharmony_ci // channel. This needs to be done before any user code gets executed 1021cb0ef41Sopenharmony_ci // (including preload modules). 1031cb0ef41Sopenharmony_ci initializeClusterIPC(); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci // TODO(joyeecheung): do this for worker threads as well. 1061cb0ef41Sopenharmony_ci require('internal/v8/startup_snapshot').runDeserializeCallbacks(); 1071cb0ef41Sopenharmony_ci } else { 1081cb0ef41Sopenharmony_ci assert(!internalBinding('worker').isMainThread); 1091cb0ef41Sopenharmony_ci // The setup should be called in LOAD_SCRIPT message handler. 1101cb0ef41Sopenharmony_ci assert(!initializeModules); 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci if (initializeModules) { 1141cb0ef41Sopenharmony_ci setupUserModules(); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci return mainEntry; 1181cb0ef41Sopenharmony_ci} 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_cifunction setupSymbolDisposePolyfill() { 1211cb0ef41Sopenharmony_ci // TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. 1221cb0ef41Sopenharmony_ci // eslint-disable-next-line node-core/prefer-primordials 1231cb0ef41Sopenharmony_ci if (typeof Symbol.dispose !== 'symbol') { 1241cb0ef41Sopenharmony_ci ObjectDefineProperty(Symbol, 'dispose', { 1251cb0ef41Sopenharmony_ci __proto__: null, 1261cb0ef41Sopenharmony_ci configurable: false, 1271cb0ef41Sopenharmony_ci enumerable: false, 1281cb0ef41Sopenharmony_ci value: SymbolDispose, 1291cb0ef41Sopenharmony_ci writable: false, 1301cb0ef41Sopenharmony_ci }); 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci // eslint-disable-next-line node-core/prefer-primordials 1341cb0ef41Sopenharmony_ci if (typeof Symbol.asyncDispose !== 'symbol') { 1351cb0ef41Sopenharmony_ci ObjectDefineProperty(Symbol, 'asyncDispose', { 1361cb0ef41Sopenharmony_ci __proto__: null, 1371cb0ef41Sopenharmony_ci configurable: false, 1381cb0ef41Sopenharmony_ci enumerable: false, 1391cb0ef41Sopenharmony_ci value: SymbolAsyncDispose, 1401cb0ef41Sopenharmony_ci writable: false, 1411cb0ef41Sopenharmony_ci }); 1421cb0ef41Sopenharmony_ci } 1431cb0ef41Sopenharmony_ci} 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_cifunction setupUserModules(isLoaderWorker = false) { 1461cb0ef41Sopenharmony_ci initializeCJSLoader(); 1471cb0ef41Sopenharmony_ci initializeESMLoader(isLoaderWorker); 1481cb0ef41Sopenharmony_ci const CJSLoader = require('internal/modules/cjs/loader'); 1491cb0ef41Sopenharmony_ci assert(!CJSLoader.hasLoadedAnyUserCJSModule); 1501cb0ef41Sopenharmony_ci // Loader workers are responsible for doing this themselves. 1511cb0ef41Sopenharmony_ci if (isLoaderWorker) { 1521cb0ef41Sopenharmony_ci return; 1531cb0ef41Sopenharmony_ci } 1541cb0ef41Sopenharmony_ci loadPreloadModules(); 1551cb0ef41Sopenharmony_ci // Need to be done after --require setup. 1561cb0ef41Sopenharmony_ci initializeFrozenIntrinsics(); 1571cb0ef41Sopenharmony_ci} 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_cifunction refreshRuntimeOptions() { 1601cb0ef41Sopenharmony_ci refreshOptions(); 1611cb0ef41Sopenharmony_ci} 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci/** 1641cb0ef41Sopenharmony_ci * Patch the process object with legacy properties and normalizations. 1651cb0ef41Sopenharmony_ci * Replace `process.argv[0]` with `process.execPath`, preserving the original `argv[0]` value as `process.argv0`. 1661cb0ef41Sopenharmony_ci * Replace `process.argv[1]` with the resolved absolute file path of the entry point, if found. 1671cb0ef41Sopenharmony_ci * @param {boolean} expandArgv1 - Whether to replace `process.argv[1]` with the resolved absolute file path of 1681cb0ef41Sopenharmony_ci * the main entry point. 1691cb0ef41Sopenharmony_ci */ 1701cb0ef41Sopenharmony_cifunction patchProcessObject(expandArgv1) { 1711cb0ef41Sopenharmony_ci const binding = internalBinding('process_methods'); 1721cb0ef41Sopenharmony_ci binding.patchProcessObject(process); 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci require('internal/process/per_thread').refreshHrtimeBuffer(); 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // Since we replace process.argv[0] below, preserve the original value in case the user needs it. 1771cb0ef41Sopenharmony_ci ObjectDefineProperty(process, 'argv0', { 1781cb0ef41Sopenharmony_ci __proto__: null, 1791cb0ef41Sopenharmony_ci enumerable: true, 1801cb0ef41Sopenharmony_ci // Only set it to true during snapshot building. 1811cb0ef41Sopenharmony_ci configurable: getOptionValue('--build-snapshot'), 1821cb0ef41Sopenharmony_ci value: process.argv[0], 1831cb0ef41Sopenharmony_ci }); 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci process.exitCode = undefined; 1861cb0ef41Sopenharmony_ci process._exiting = false; 1871cb0ef41Sopenharmony_ci process.argv[0] = process.execPath; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci /** @type {string} */ 1901cb0ef41Sopenharmony_ci let mainEntry; 1911cb0ef41Sopenharmony_ci // If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of 1921cb0ef41Sopenharmony_ci // the entry point. 1931cb0ef41Sopenharmony_ci if (expandArgv1 && process.argv[1] && 1941cb0ef41Sopenharmony_ci !StringPrototypeStartsWith(process.argv[1], '-')) { 1951cb0ef41Sopenharmony_ci // Expand process.argv[1] into a full path. 1961cb0ef41Sopenharmony_ci const path = require('path'); 1971cb0ef41Sopenharmony_ci try { 1981cb0ef41Sopenharmony_ci mainEntry = path.resolve(process.argv[1]); 1991cb0ef41Sopenharmony_ci process.argv[1] = mainEntry; 2001cb0ef41Sopenharmony_ci } catch { 2011cb0ef41Sopenharmony_ci // Continue regardless of error. 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci } 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci // We need to initialize the global console here again with process.stdout 2061cb0ef41Sopenharmony_ci // and friends for snapshot deserialization. 2071cb0ef41Sopenharmony_ci const globalConsole = require('internal/console/global'); 2081cb0ef41Sopenharmony_ci const { initializeGlobalConsole } = require('internal/console/constructor'); 2091cb0ef41Sopenharmony_ci initializeGlobalConsole(globalConsole); 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci // TODO(joyeecheung): most of these should be deprecated and removed, 2121cb0ef41Sopenharmony_ci // except some that we need to be able to mutate during run time. 2131cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_eval', '--eval'); 2141cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_print_eval', '--print'); 2151cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_syntax_check_only', '--check'); 2161cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_forceRepl', '--interactive'); 2171cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_preload_modules', '--require'); 2181cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('noDeprecation', '--no-deprecation'); 2191cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('noProcessWarnings', '--no-warnings'); 2201cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('traceProcessWarnings', '--trace-warnings'); 2211cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('throwDeprecation', '--throw-deprecation'); 2221cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('profProcess', '--prof-process'); 2231cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('traceDeprecation', '--trace-deprecation'); 2241cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_breakFirstLine', '--inspect-brk', false); 2251cb0ef41Sopenharmony_ci addReadOnlyProcessAlias('_breakNodeFirstLine', '--inspect-brk-node', false); 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci return mainEntry; 2281cb0ef41Sopenharmony_ci} 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_cifunction addReadOnlyProcessAlias(name, option, enumerable = true) { 2311cb0ef41Sopenharmony_ci const value = getOptionValue(option); 2321cb0ef41Sopenharmony_ci if (value) { 2331cb0ef41Sopenharmony_ci ObjectDefineProperty(process, name, { 2341cb0ef41Sopenharmony_ci __proto__: null, 2351cb0ef41Sopenharmony_ci writable: false, 2361cb0ef41Sopenharmony_ci configurable: true, 2371cb0ef41Sopenharmony_ci enumerable, 2381cb0ef41Sopenharmony_ci value, 2391cb0ef41Sopenharmony_ci }); 2401cb0ef41Sopenharmony_ci } 2411cb0ef41Sopenharmony_ci} 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_cifunction setupWarningHandler() { 2441cb0ef41Sopenharmony_ci const { 2451cb0ef41Sopenharmony_ci onWarning, 2461cb0ef41Sopenharmony_ci resetForSerialization, 2471cb0ef41Sopenharmony_ci } = require('internal/process/warning'); 2481cb0ef41Sopenharmony_ci if (getOptionValue('--warnings') && 2491cb0ef41Sopenharmony_ci process.env.NODE_NO_WARNINGS !== '1') { 2501cb0ef41Sopenharmony_ci process.on('warning', onWarning); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci // The code above would add the listener back during deserialization, 2531cb0ef41Sopenharmony_ci // if applicable. 2541cb0ef41Sopenharmony_ci if (isBuildingSnapshot()) { 2551cb0ef41Sopenharmony_ci addSerializeCallback(() => { 2561cb0ef41Sopenharmony_ci process.removeListener('warning', onWarning); 2571cb0ef41Sopenharmony_ci resetForSerialization(); 2581cb0ef41Sopenharmony_ci }); 2591cb0ef41Sopenharmony_ci } 2601cb0ef41Sopenharmony_ci } 2611cb0ef41Sopenharmony_ci} 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ci// https://fetch.spec.whatwg.org/ 2641cb0ef41Sopenharmony_cifunction setupFetch() { 2651cb0ef41Sopenharmony_ci if (process.config.variables.node_no_browser_globals || 2661cb0ef41Sopenharmony_ci getOptionValue('--no-experimental-fetch')) { 2671cb0ef41Sopenharmony_ci return; 2681cb0ef41Sopenharmony_ci } 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci let undici; 2711cb0ef41Sopenharmony_ci function lazyUndici() { 2721cb0ef41Sopenharmony_ci if (undici) { 2731cb0ef41Sopenharmony_ci return undici; 2741cb0ef41Sopenharmony_ci } 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci undici = require('internal/deps/undici/undici'); 2771cb0ef41Sopenharmony_ci return undici; 2781cb0ef41Sopenharmony_ci } 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci async function fetch(input, init = undefined) { 2811cb0ef41Sopenharmony_ci return lazyUndici().fetch(input, init); 2821cb0ef41Sopenharmony_ci } 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci defineOperation(globalThis, 'fetch', fetch); 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci function lazyInterface(name) { 2871cb0ef41Sopenharmony_ci return { 2881cb0ef41Sopenharmony_ci configurable: true, 2891cb0ef41Sopenharmony_ci enumerable: false, 2901cb0ef41Sopenharmony_ci get() { 2911cb0ef41Sopenharmony_ci return lazyUndici()[name]; 2921cb0ef41Sopenharmony_ci }, 2931cb0ef41Sopenharmony_ci set(value) { 2941cb0ef41Sopenharmony_ci exposeInterface(globalThis, name, value); 2951cb0ef41Sopenharmony_ci }, 2961cb0ef41Sopenharmony_ci }; 2971cb0ef41Sopenharmony_ci } 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ci ObjectDefineProperties(globalThis, { 3001cb0ef41Sopenharmony_ci FormData: lazyInterface('FormData'), 3011cb0ef41Sopenharmony_ci Headers: lazyInterface('Headers'), 3021cb0ef41Sopenharmony_ci Request: lazyInterface('Request'), 3031cb0ef41Sopenharmony_ci Response: lazyInterface('Response'), 3041cb0ef41Sopenharmony_ci }); 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci // The WebAssembly Web API: https://webassembly.github.io/spec/web-api 3071cb0ef41Sopenharmony_ci internalBinding('wasm_web_api').setImplementation((streamState, source) => { 3081cb0ef41Sopenharmony_ci require('internal/wasm_web_api').wasmStreamingCallback(streamState, source); 3091cb0ef41Sopenharmony_ci }); 3101cb0ef41Sopenharmony_ci} 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ci// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is 3131cb0ef41Sopenharmony_ci// removed. 3141cb0ef41Sopenharmony_cifunction setupWebCrypto() { 3151cb0ef41Sopenharmony_ci if (process.config.variables.node_no_browser_globals || 3161cb0ef41Sopenharmony_ci !getOptionValue('--experimental-global-webcrypto')) { 3171cb0ef41Sopenharmony_ci return; 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci if (internalBinding('config').hasOpenSSL) { 3211cb0ef41Sopenharmony_ci defineReplaceableLazyAttribute( 3221cb0ef41Sopenharmony_ci globalThis, 'internal/crypto/webcrypto', ['crypto'], false, 3231cb0ef41Sopenharmony_ci ); 3241cb0ef41Sopenharmony_ci exposeLazyInterfaces( 3251cb0ef41Sopenharmony_ci globalThis, 'internal/crypto/webcrypto', 3261cb0ef41Sopenharmony_ci ['Crypto', 'CryptoKey', 'SubtleCrypto'], 3271cb0ef41Sopenharmony_ci ); 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci} 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_cifunction setupCodeCoverage() { 3321cb0ef41Sopenharmony_ci // Resolve the coverage directory to an absolute path, and 3331cb0ef41Sopenharmony_ci // overwrite process.env so that the original path gets passed 3341cb0ef41Sopenharmony_ci // to child processes even when they switch cwd. Don't do anything if the 3351cb0ef41Sopenharmony_ci // --experimental-test-coverage flag is present, as the test runner will 3361cb0ef41Sopenharmony_ci // handle coverage. 3371cb0ef41Sopenharmony_ci if (process.env.NODE_V8_COVERAGE && 3381cb0ef41Sopenharmony_ci !getOptionValue('--experimental-test-coverage')) { 3391cb0ef41Sopenharmony_ci process.env.NODE_V8_COVERAGE = 3401cb0ef41Sopenharmony_ci setupCoverageHooks(process.env.NODE_V8_COVERAGE); 3411cb0ef41Sopenharmony_ci } 3421cb0ef41Sopenharmony_ci} 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ci// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is 3451cb0ef41Sopenharmony_ci// removed. 3461cb0ef41Sopenharmony_cifunction setupCustomEvent() { 3471cb0ef41Sopenharmony_ci if (process.config.variables.node_no_browser_globals || 3481cb0ef41Sopenharmony_ci !getOptionValue('--experimental-global-customevent')) { 3491cb0ef41Sopenharmony_ci return; 3501cb0ef41Sopenharmony_ci } 3511cb0ef41Sopenharmony_ci const { CustomEvent } = require('internal/event_target'); 3521cb0ef41Sopenharmony_ci exposeInterface(globalThis, 'CustomEvent', CustomEvent); 3531cb0ef41Sopenharmony_ci} 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_cifunction setupStacktracePrinterOnSigint() { 3561cb0ef41Sopenharmony_ci if (!getOptionValue('--trace-sigint')) { 3571cb0ef41Sopenharmony_ci return; 3581cb0ef41Sopenharmony_ci } 3591cb0ef41Sopenharmony_ci const { SigintWatchdog } = require('internal/watchdog'); 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci const watchdog = new SigintWatchdog(); 3621cb0ef41Sopenharmony_ci watchdog.start(); 3631cb0ef41Sopenharmony_ci} 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_cifunction initializeReport() { 3661cb0ef41Sopenharmony_ci ObjectDefineProperty(process, 'report', { 3671cb0ef41Sopenharmony_ci __proto__: null, 3681cb0ef41Sopenharmony_ci enumerable: true, 3691cb0ef41Sopenharmony_ci configurable: true, 3701cb0ef41Sopenharmony_ci get() { 3711cb0ef41Sopenharmony_ci const { report } = require('internal/process/report'); 3721cb0ef41Sopenharmony_ci return report; 3731cb0ef41Sopenharmony_ci }, 3741cb0ef41Sopenharmony_ci }); 3751cb0ef41Sopenharmony_ci} 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_cifunction setupDebugEnv() { 3781cb0ef41Sopenharmony_ci require('internal/util/debuglog').initializeDebugEnv(process.env.NODE_DEBUG); 3791cb0ef41Sopenharmony_ci if (getOptionValue('--expose-internals')) { 3801cb0ef41Sopenharmony_ci require('internal/bootstrap/realm').BuiltinModule.exposeInternals(); 3811cb0ef41Sopenharmony_ci } 3821cb0ef41Sopenharmony_ci} 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci// This has to be called after initializeReport() is called 3851cb0ef41Sopenharmony_cifunction initializeReportSignalHandlers() { 3861cb0ef41Sopenharmony_ci if (getOptionValue('--report-on-signal')) { 3871cb0ef41Sopenharmony_ci const { addSignalHandler } = require('internal/process/report'); 3881cb0ef41Sopenharmony_ci addSignalHandler(); 3891cb0ef41Sopenharmony_ci } 3901cb0ef41Sopenharmony_ci} 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_cifunction initializeHeapSnapshotSignalHandlers() { 3931cb0ef41Sopenharmony_ci const signal = getOptionValue('--heapsnapshot-signal'); 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci if (!signal) 3961cb0ef41Sopenharmony_ci return; 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ci require('internal/validators').validateSignalName(signal); 3991cb0ef41Sopenharmony_ci const { writeHeapSnapshot } = require('v8'); 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci function doWriteHeapSnapshot() { 4021cb0ef41Sopenharmony_ci writeHeapSnapshot(); 4031cb0ef41Sopenharmony_ci } 4041cb0ef41Sopenharmony_ci process.on(signal, doWriteHeapSnapshot); 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci // The code above would add the listener back during deserialization, 4071cb0ef41Sopenharmony_ci // if applicable. 4081cb0ef41Sopenharmony_ci if (isBuildingSnapshot()) { 4091cb0ef41Sopenharmony_ci addSerializeCallback(() => { 4101cb0ef41Sopenharmony_ci process.removeListener(signal, doWriteHeapSnapshot); 4111cb0ef41Sopenharmony_ci }); 4121cb0ef41Sopenharmony_ci } 4131cb0ef41Sopenharmony_ci} 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_cifunction setupTraceCategoryState() { 4161cb0ef41Sopenharmony_ci const { isTraceCategoryEnabled } = internalBinding('trace_events'); 4171cb0ef41Sopenharmony_ci const { toggleTraceCategoryState } = require('internal/process/per_thread'); 4181cb0ef41Sopenharmony_ci toggleTraceCategoryState(isTraceCategoryEnabled('node.async_hooks')); 4191cb0ef41Sopenharmony_ci} 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_cifunction setupPerfHooks() { 4221cb0ef41Sopenharmony_ci require('internal/perf/utils').refreshTimeOrigin(); 4231cb0ef41Sopenharmony_ci} 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_cifunction setupInspectorHooks() { 4261cb0ef41Sopenharmony_ci // If Debugger.setAsyncCallStackDepth is sent during bootstrap, 4271cb0ef41Sopenharmony_ci // we cannot immediately call into JS to enable the hooks, which could 4281cb0ef41Sopenharmony_ci // interrupt the JS execution of bootstrap. So instead we save the 4291cb0ef41Sopenharmony_ci // notification in the inspector agent if it's sent in the middle of 4301cb0ef41Sopenharmony_ci // bootstrap, and process the notification later here. 4311cb0ef41Sopenharmony_ci if (internalBinding('config').hasInspector) { 4321cb0ef41Sopenharmony_ci const { 4331cb0ef41Sopenharmony_ci enable, 4341cb0ef41Sopenharmony_ci disable, 4351cb0ef41Sopenharmony_ci } = require('internal/inspector_async_hook'); 4361cb0ef41Sopenharmony_ci internalBinding('inspector').registerAsyncHook(enable, disable); 4371cb0ef41Sopenharmony_ci } 4381cb0ef41Sopenharmony_ci} 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_ci// In general deprecations are initialized wherever the APIs are implemented, 4411cb0ef41Sopenharmony_ci// this is used to deprecate APIs implemented in C++ where the deprecation 4421cb0ef41Sopenharmony_ci// utilities are not easily accessible. 4431cb0ef41Sopenharmony_cifunction initializeDeprecations() { 4441cb0ef41Sopenharmony_ci const { deprecate } = require('internal/util'); 4451cb0ef41Sopenharmony_ci const pendingDeprecation = getOptionValue('--pending-deprecation'); 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci // DEP0103: access to `process.binding('util').isX` type checkers 4481cb0ef41Sopenharmony_ci // TODO(addaleax): Turn into a full runtime deprecation. 4491cb0ef41Sopenharmony_ci const utilBinding = internalBinding('util'); 4501cb0ef41Sopenharmony_ci const types = require('internal/util/types'); 4511cb0ef41Sopenharmony_ci for (const name of [ 4521cb0ef41Sopenharmony_ci 'isArrayBuffer', 4531cb0ef41Sopenharmony_ci 'isArrayBufferView', 4541cb0ef41Sopenharmony_ci 'isAsyncFunction', 4551cb0ef41Sopenharmony_ci 'isDataView', 4561cb0ef41Sopenharmony_ci 'isDate', 4571cb0ef41Sopenharmony_ci 'isExternal', 4581cb0ef41Sopenharmony_ci 'isMap', 4591cb0ef41Sopenharmony_ci 'isMapIterator', 4601cb0ef41Sopenharmony_ci 'isNativeError', 4611cb0ef41Sopenharmony_ci 'isPromise', 4621cb0ef41Sopenharmony_ci 'isRegExp', 4631cb0ef41Sopenharmony_ci 'isSet', 4641cb0ef41Sopenharmony_ci 'isSetIterator', 4651cb0ef41Sopenharmony_ci 'isTypedArray', 4661cb0ef41Sopenharmony_ci 'isUint8Array', 4671cb0ef41Sopenharmony_ci 'isAnyArrayBuffer', 4681cb0ef41Sopenharmony_ci ]) { 4691cb0ef41Sopenharmony_ci utilBinding[name] = pendingDeprecation ? 4701cb0ef41Sopenharmony_ci deprecate(types[name], 4711cb0ef41Sopenharmony_ci 'Accessing native typechecking bindings of Node ' + 4721cb0ef41Sopenharmony_ci 'directly is deprecated. ' + 4731cb0ef41Sopenharmony_ci `Please use \`util.types.${name}\` instead.`, 4741cb0ef41Sopenharmony_ci 'DEP0103') : 4751cb0ef41Sopenharmony_ci types[name]; 4761cb0ef41Sopenharmony_ci } 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci // TODO(joyeecheung): this is a legacy property exposed to process. 4791cb0ef41Sopenharmony_ci // Now that we use the config binding to carry this information, remove 4801cb0ef41Sopenharmony_ci // it from the process. We may consider exposing it properly in 4811cb0ef41Sopenharmony_ci // process.features. 4821cb0ef41Sopenharmony_ci const { noBrowserGlobals } = internalBinding('config'); 4831cb0ef41Sopenharmony_ci if (noBrowserGlobals) { 4841cb0ef41Sopenharmony_ci ObjectDefineProperty(process, '_noBrowserGlobals', { 4851cb0ef41Sopenharmony_ci __proto__: null, 4861cb0ef41Sopenharmony_ci writable: false, 4871cb0ef41Sopenharmony_ci enumerable: true, 4881cb0ef41Sopenharmony_ci configurable: true, 4891cb0ef41Sopenharmony_ci value: noBrowserGlobals, 4901cb0ef41Sopenharmony_ci }); 4911cb0ef41Sopenharmony_ci } 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci if (pendingDeprecation) { 4941cb0ef41Sopenharmony_ci process.binding = deprecate(process.binding, 4951cb0ef41Sopenharmony_ci 'process.binding() is deprecated. ' + 4961cb0ef41Sopenharmony_ci 'Please use public APIs instead.', 'DEP0111'); 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci process._tickCallback = deprecate(process._tickCallback, 4991cb0ef41Sopenharmony_ci 'process._tickCallback() is deprecated', 5001cb0ef41Sopenharmony_ci 'DEP0134'); 5011cb0ef41Sopenharmony_ci } 5021cb0ef41Sopenharmony_ci} 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_cifunction setupChildProcessIpcChannel() { 5051cb0ef41Sopenharmony_ci if (process.env.NODE_CHANNEL_FD) { 5061cb0ef41Sopenharmony_ci const assert = require('internal/assert'); 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ci const fd = NumberParseInt(process.env.NODE_CHANNEL_FD, 10); 5091cb0ef41Sopenharmony_ci assert(fd >= 0); 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci // Make sure it's not accidentally inherited by child processes. 5121cb0ef41Sopenharmony_ci delete process.env.NODE_CHANNEL_FD; 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci const serializationMode = 5151cb0ef41Sopenharmony_ci process.env.NODE_CHANNEL_SERIALIZATION_MODE || 'json'; 5161cb0ef41Sopenharmony_ci delete process.env.NODE_CHANNEL_SERIALIZATION_MODE; 5171cb0ef41Sopenharmony_ci 5181cb0ef41Sopenharmony_ci require('child_process')._forkChild(fd, serializationMode); 5191cb0ef41Sopenharmony_ci assert(process.send); 5201cb0ef41Sopenharmony_ci } 5211cb0ef41Sopenharmony_ci} 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_cifunction initializeClusterIPC() { 5241cb0ef41Sopenharmony_ci if (process.argv[1] && process.env.NODE_UNIQUE_ID) { 5251cb0ef41Sopenharmony_ci const cluster = require('cluster'); 5261cb0ef41Sopenharmony_ci cluster._setupWorker(); 5271cb0ef41Sopenharmony_ci // Make sure it's not accidentally inherited by child processes. 5281cb0ef41Sopenharmony_ci delete process.env.NODE_UNIQUE_ID; 5291cb0ef41Sopenharmony_ci } 5301cb0ef41Sopenharmony_ci} 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_cifunction readPolicyFromDisk() { 5331cb0ef41Sopenharmony_ci const experimentalPolicy = getOptionValue('--experimental-policy'); 5341cb0ef41Sopenharmony_ci if (experimentalPolicy) { 5351cb0ef41Sopenharmony_ci process.emitWarning('Policies are experimental.', 5361cb0ef41Sopenharmony_ci 'ExperimentalWarning'); 5371cb0ef41Sopenharmony_ci const { pathToFileURL, URL } = require('internal/url'); 5381cb0ef41Sopenharmony_ci // URL here as it is slightly different parsing 5391cb0ef41Sopenharmony_ci // no bare specifiers for now 5401cb0ef41Sopenharmony_ci let manifestURL; 5411cb0ef41Sopenharmony_ci if (require('path').isAbsolute(experimentalPolicy)) { 5421cb0ef41Sopenharmony_ci manifestURL = pathToFileURL(experimentalPolicy); 5431cb0ef41Sopenharmony_ci } else { 5441cb0ef41Sopenharmony_ci const cwdURL = pathToFileURL(process.cwd()); 5451cb0ef41Sopenharmony_ci cwdURL.pathname += '/'; 5461cb0ef41Sopenharmony_ci manifestURL = new URL(experimentalPolicy, cwdURL); 5471cb0ef41Sopenharmony_ci } 5481cb0ef41Sopenharmony_ci const fs = require('fs'); 5491cb0ef41Sopenharmony_ci const src = fs.readFileSync(manifestURL, 'utf8'); 5501cb0ef41Sopenharmony_ci const experimentalPolicyIntegrity = getOptionValue('--policy-integrity'); 5511cb0ef41Sopenharmony_ci if (experimentalPolicyIntegrity) { 5521cb0ef41Sopenharmony_ci const SRI = require('internal/policy/sri'); 5531cb0ef41Sopenharmony_ci const { createHash, timingSafeEqual } = require('crypto'); 5541cb0ef41Sopenharmony_ci const realIntegrities = new SafeMap(); 5551cb0ef41Sopenharmony_ci const integrityEntries = SRI.parse(experimentalPolicyIntegrity); 5561cb0ef41Sopenharmony_ci let foundMatch = false; 5571cb0ef41Sopenharmony_ci for (let i = 0; i < integrityEntries.length; i++) { 5581cb0ef41Sopenharmony_ci const { 5591cb0ef41Sopenharmony_ci algorithm, 5601cb0ef41Sopenharmony_ci value: expected, 5611cb0ef41Sopenharmony_ci } = integrityEntries[i]; 5621cb0ef41Sopenharmony_ci const hash = createHash(algorithm); 5631cb0ef41Sopenharmony_ci hash.update(src); 5641cb0ef41Sopenharmony_ci const digest = hash.digest(); 5651cb0ef41Sopenharmony_ci if (digest.length === expected.length && 5661cb0ef41Sopenharmony_ci timingSafeEqual(digest, expected)) { 5671cb0ef41Sopenharmony_ci foundMatch = true; 5681cb0ef41Sopenharmony_ci break; 5691cb0ef41Sopenharmony_ci } 5701cb0ef41Sopenharmony_ci realIntegrities.set(algorithm, digest.toString('base64')); 5711cb0ef41Sopenharmony_ci } 5721cb0ef41Sopenharmony_ci if (!foundMatch) { 5731cb0ef41Sopenharmony_ci throw new ERR_MANIFEST_ASSERT_INTEGRITY(manifestURL, realIntegrities); 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci } 5761cb0ef41Sopenharmony_ci return { 5771cb0ef41Sopenharmony_ci manifestSrc: src, manifestURL: manifestURL.href, 5781cb0ef41Sopenharmony_ci }; 5791cb0ef41Sopenharmony_ci } 5801cb0ef41Sopenharmony_ci} 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_cifunction initializeCJSLoader() { 5831cb0ef41Sopenharmony_ci const { initializeCJS } = require('internal/modules/cjs/loader'); 5841cb0ef41Sopenharmony_ci initializeCJS(); 5851cb0ef41Sopenharmony_ci} 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_cifunction initializeESMLoader(isLoaderWorker) { 5881cb0ef41Sopenharmony_ci const { initializeESM } = require('internal/modules/esm/utils'); 5891cb0ef41Sopenharmony_ci initializeESM(isLoaderWorker); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci // Patch the vm module when --experimental-vm-modules is on. 5921cb0ef41Sopenharmony_ci // Please update the comments in vm.js when this block changes. 5931cb0ef41Sopenharmony_ci if (getOptionValue('--experimental-vm-modules')) { 5941cb0ef41Sopenharmony_ci const { 5951cb0ef41Sopenharmony_ci Module, SourceTextModule, SyntheticModule, 5961cb0ef41Sopenharmony_ci } = require('internal/vm/module'); 5971cb0ef41Sopenharmony_ci const vm = require('vm'); 5981cb0ef41Sopenharmony_ci vm.Module = Module; 5991cb0ef41Sopenharmony_ci vm.SourceTextModule = SourceTextModule; 6001cb0ef41Sopenharmony_ci vm.SyntheticModule = SyntheticModule; 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci} 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_cifunction initializeSourceMapsHandlers() { 6051cb0ef41Sopenharmony_ci const { 6061cb0ef41Sopenharmony_ci setSourceMapsEnabled, 6071cb0ef41Sopenharmony_ci } = require('internal/source_map/source_map_cache'); 6081cb0ef41Sopenharmony_ci setSourceMapsEnabled(getOptionValue('--enable-source-maps')); 6091cb0ef41Sopenharmony_ci} 6101cb0ef41Sopenharmony_ci 6111cb0ef41Sopenharmony_cifunction initializeFrozenIntrinsics() { 6121cb0ef41Sopenharmony_ci if (getOptionValue('--frozen-intrinsics')) { 6131cb0ef41Sopenharmony_ci process.emitWarning('The --frozen-intrinsics flag is experimental', 6141cb0ef41Sopenharmony_ci 'ExperimentalWarning'); 6151cb0ef41Sopenharmony_ci require('internal/freeze_intrinsics')(); 6161cb0ef41Sopenharmony_ci } 6171cb0ef41Sopenharmony_ci} 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_cifunction loadPreloadModules() { 6201cb0ef41Sopenharmony_ci // For user code, we preload modules if `-r` is passed 6211cb0ef41Sopenharmony_ci const preloadModules = getOptionValue('--require'); 6221cb0ef41Sopenharmony_ci if (preloadModules && preloadModules.length > 0) { 6231cb0ef41Sopenharmony_ci const { 6241cb0ef41Sopenharmony_ci Module: { 6251cb0ef41Sopenharmony_ci _preloadModules, 6261cb0ef41Sopenharmony_ci }, 6271cb0ef41Sopenharmony_ci } = require('internal/modules/cjs/loader'); 6281cb0ef41Sopenharmony_ci _preloadModules(preloadModules); 6291cb0ef41Sopenharmony_ci } 6301cb0ef41Sopenharmony_ci} 6311cb0ef41Sopenharmony_ci 6321cb0ef41Sopenharmony_cifunction markBootstrapComplete() { 6331cb0ef41Sopenharmony_ci internalBinding('performance').markBootstrapComplete(); 6341cb0ef41Sopenharmony_ci} 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_cimodule.exports = { 6371cb0ef41Sopenharmony_ci setupUserModules, 6381cb0ef41Sopenharmony_ci prepareMainThreadExecution, 6391cb0ef41Sopenharmony_ci prepareWorkerThreadExecution, 6401cb0ef41Sopenharmony_ci markBootstrapComplete, 6411cb0ef41Sopenharmony_ci loadPreloadModules, 6421cb0ef41Sopenharmony_ci initializeFrozenIntrinsics, 6431cb0ef41Sopenharmony_ci}; 644