@@ -33,12 +33,26 @@ const {
3333 isBuildingSnapshot,
3434} = require ( 'v8' ) . startupSnapshot ;
3535
36- function prepareMainThreadExecution ( expandArgv1 = false ,
37- initialzeModules = true ) {
38- refreshRuntimeOptions ( ) ;
36+ function prepareMainThreadExecution ( expandArgv1 = false , initialzeModules = true ) {
37+ prepareExecution ( {
38+ expandArgv1,
39+ initialzeModules,
40+ isMainThread : true
41+ } ) ;
42+ }
43+
44+ function prepareWorkerThreadExecution ( ) {
45+ prepareExecution ( {
46+ expandArgv1 : false ,
47+ initialzeModules : false , // Will need to initialize it after policy setup
48+ isMainThread : false
49+ } ) ;
50+ }
3951
40- // TODO(joyeecheung): this is also necessary for workers when they deserialize
41- // this toggle from the snapshot.
52+ function prepareExecution ( options ) {
53+ const { expandArgv1, initialzeModules, isMainThread } = options ;
54+
55+ refreshRuntimeOptions ( ) ;
4256 reconnectZeroFillToggle ( ) ;
4357
4458 // Patch the process object with legacy properties and normalizations
@@ -60,48 +74,57 @@ function prepareMainThreadExecution(expandArgv1 = false,
6074 }
6175
6276 setupDebugEnv ( ) ;
63-
64- // Print stack trace on `SIGINT` if option `--trace-sigint` presents.
65- setupStacktracePrinterOnSigint ( ) ;
66-
6777 // Process initial diagnostic reporting configuration, if present.
6878 initializeReport ( ) ;
69- initializeReportSignalHandlers ( ) ; // Main-thread-only.
70-
71- initializeHeapSnapshotSignalHandlers ( ) ;
72-
73- // If the process is spawned with env NODE_CHANNEL_FD, it's probably
74- // spawned by our child_process module, then initialize IPC.
75- // This attaches some internal event listeners and creates:
76- // process.send(), process.channel, process.connected,
77- // process.disconnect().
78- setupChildProcessIpcChannel ( ) ;
79-
80- // Load policy from disk and parse it.
81- initializePolicy ( ) ;
82-
83- // If this is a worker in cluster mode, start up the communication
84- // channel. This needs to be done before any user code gets executed
85- // (including preload modules).
86- initializeClusterIPC ( ) ;
87-
8879 initializeSourceMapsHandlers ( ) ;
8980 initializeDeprecations ( ) ;
9081 initializeWASI ( ) ;
91-
9282 require ( 'internal/dns/utils' ) . initializeDns ( ) ;
9383
94- require ( 'internal/v8/startup_snapshot' ) . runDeserializeCallbacks ( ) ;
84+ if ( isMainThread ) {
85+ assert ( internalBinding ( 'worker' ) . isMainThread ) ;
86+ // Worker threads will get the manifest in the message handler.
87+ const policy = readPolicyFromDisk ( ) ;
88+ if ( policy ) {
89+ require ( 'internal/process/policy' )
90+ . setup ( policy . manifestSrc , policy . manifestURL ) ;
91+ }
9592
96- if ( ! initialzeModules ) {
97- return ;
93+ // Print stack trace on `SIGINT` if option `--trace-sigint` presents.
94+ setupStacktracePrinterOnSigint ( ) ;
95+ initializeReportSignalHandlers ( ) ; // Main-thread-only.
96+ initializeHeapSnapshotSignalHandlers ( ) ;
97+ // If the process is spawned with env NODE_CHANNEL_FD, it's probably
98+ // spawned by our child_process module, then initialize IPC.
99+ // This attaches some internal event listeners and creates:
100+ // process.send(), process.channel, process.connected,
101+ // process.disconnect().
102+ setupChildProcessIpcChannel ( ) ;
103+ // If this is a worker in cluster mode, start up the communication
104+ // channel. This needs to be done before any user code gets executed
105+ // (including preload modules).
106+ initializeClusterIPC ( ) ;
107+
108+ // TODO(joyeecheung): do this for worker threads as well.
109+ require ( 'internal/v8/startup_snapshot' ) . runDeserializeCallbacks ( ) ;
110+ } else {
111+ assert ( ! internalBinding ( 'worker' ) . isMainThread ) ;
112+ // The setup should be called in LOAD_SCRIPT message handler.
113+ assert ( ! initialzeModules ) ;
114+ }
115+
116+ if ( initialzeModules ) {
117+ setupUserModules ( ) ;
98118 }
119+ }
99120
121+ function setupUserModules ( ) {
100122 initializeCJSLoader ( ) ;
101123 initializeESMLoader ( ) ;
102124 const CJSLoader = require ( 'internal/modules/cjs/loader' ) ;
103125 assert ( ! CJSLoader . hasLoadedAnyUserCJSModule ) ;
104126 loadPreloadModules ( ) ;
127+ // Need to be done after --require setup.
105128 initializeFrozenIntrinsics ( ) ;
106129}
107130
@@ -476,7 +499,7 @@ function initializeClusterIPC() {
476499 }
477500}
478501
479- function initializePolicy ( ) {
502+ function readPolicyFromDisk ( ) {
480503 const experimentalPolicy = getOptionValue ( '--experimental-policy' ) ;
481504 if ( experimentalPolicy ) {
482505 process . emitWarning ( 'Policies are experimental.' ,
@@ -520,8 +543,9 @@ function initializePolicy() {
520543 throw new ERR_MANIFEST_ASSERT_INTEGRITY ( manifestURL , realIntegrities ) ;
521544 }
522545 }
523- require ( 'internal/process/policy' )
524- . setup ( src , manifestURL . href ) ;
546+ return {
547+ manifestSrc : src , manifestURL : manifestURL . href
548+ } ;
525549 }
526550}
527551
@@ -603,25 +627,8 @@ function markBootstrapComplete() {
603627}
604628
605629module . exports = {
606- refreshRuntimeOptions,
607- patchProcessObject,
608- setupCoverageHooks,
609- setupWarningHandler,
610- setupFetch,
611- setupWebCrypto,
612- setupCustomEvent,
613- setupDebugEnv,
614- setupPerfHooks,
630+ setupUserModules,
615631 prepareMainThreadExecution,
616- initializeDeprecations,
617- initializeESMLoader,
618- initializeFrozenIntrinsics,
619- initializeSourceMapsHandlers,
620- loadPreloadModules,
621- setupTraceCategoryState,
622- setupInspectorHooks,
623- initializeReport,
624- initializeCJSLoader,
625- initializeWASI,
632+ prepareWorkerThreadExecution,
626633 markBootstrapComplete
627634} ;
0 commit comments