@@ -33,12 +33,26 @@ const {
33
33
isBuildingSnapshot,
34
34
} = require ( 'v8' ) . startupSnapshot ;
35
35
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
+ }
39
51
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 ( ) ;
42
56
reconnectZeroFillToggle ( ) ;
43
57
44
58
// Patch the process object with legacy properties and normalizations
@@ -60,48 +74,57 @@ function prepareMainThreadExecution(expandArgv1 = false,
60
74
}
61
75
62
76
setupDebugEnv ( ) ;
63
-
64
- // Print stack trace on `SIGINT` if option `--trace-sigint` presents.
65
- setupStacktracePrinterOnSigint ( ) ;
66
-
67
77
// Process initial diagnostic reporting configuration, if present.
68
78
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
-
88
79
initializeSourceMapsHandlers ( ) ;
89
80
initializeDeprecations ( ) ;
90
81
initializeWASI ( ) ;
91
-
92
82
require ( 'internal/dns/utils' ) . initializeDns ( ) ;
93
83
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
+ }
95
92
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 ( ) ;
98
118
}
119
+ }
99
120
121
+ function setupUserModules ( ) {
100
122
initializeCJSLoader ( ) ;
101
123
initializeESMLoader ( ) ;
102
124
const CJSLoader = require ( 'internal/modules/cjs/loader' ) ;
103
125
assert ( ! CJSLoader . hasLoadedAnyUserCJSModule ) ;
104
126
loadPreloadModules ( ) ;
127
+ // Need to be done after --require setup.
105
128
initializeFrozenIntrinsics ( ) ;
106
129
}
107
130
@@ -476,7 +499,7 @@ function initializeClusterIPC() {
476
499
}
477
500
}
478
501
479
- function initializePolicy ( ) {
502
+ function readPolicyFromDisk ( ) {
480
503
const experimentalPolicy = getOptionValue ( '--experimental-policy' ) ;
481
504
if ( experimentalPolicy ) {
482
505
process . emitWarning ( 'Policies are experimental.' ,
@@ -520,8 +543,9 @@ function initializePolicy() {
520
543
throw new ERR_MANIFEST_ASSERT_INTEGRITY ( manifestURL , realIntegrities ) ;
521
544
}
522
545
}
523
- require ( 'internal/process/policy' )
524
- . setup ( src , manifestURL . href ) ;
546
+ return {
547
+ manifestSrc : src , manifestURL : manifestURL . href
548
+ } ;
525
549
}
526
550
}
527
551
@@ -603,25 +627,8 @@ function markBootstrapComplete() {
603
627
}
604
628
605
629
module . exports = {
606
- refreshRuntimeOptions,
607
- patchProcessObject,
608
- setupCoverageHooks,
609
- setupWarningHandler,
610
- setupFetch,
611
- setupWebCrypto,
612
- setupCustomEvent,
613
- setupDebugEnv,
614
- setupPerfHooks,
630
+ setupUserModules,
615
631
prepareMainThreadExecution,
616
- initializeDeprecations,
617
- initializeESMLoader,
618
- initializeFrozenIntrinsics,
619
- initializeSourceMapsHandlers,
620
- loadPreloadModules,
621
- setupTraceCategoryState,
622
- setupInspectorHooks,
623
- initializeReport,
624
- initializeCJSLoader,
625
- initializeWASI,
632
+ prepareWorkerThreadExecution,
626
633
markBootstrapComplete
627
634
} ;
0 commit comments