@@ -34,12 +34,26 @@ const {
34
34
isBuildingSnapshot,
35
35
} = require ( 'v8' ) . startupSnapshot ;
36
36
37
- function prepareMainThreadExecution ( expandArgv1 = false ,
38
- initialzeModules = true ) {
39
- refreshRuntimeOptions ( ) ;
37
+ function prepareMainThreadExecution ( expandArgv1 = false , initialzeModules = true ) {
38
+ prepareExecution ( {
39
+ expandArgv1,
40
+ initialzeModules,
41
+ isMainThread : true
42
+ } ) ;
43
+ }
44
+
45
+ function prepareWorkerThreadExecution ( ) {
46
+ prepareExecution ( {
47
+ expandArgv1 : false ,
48
+ initialzeModules : false , // Will need to initialize it after policy setup
49
+ isMainThread : false
50
+ } ) ;
51
+ }
40
52
41
- // TODO(joyeecheung): this is also necessary for workers when they deserialize
42
- // this toggle from the snapshot.
53
+ function prepareExecution ( options ) {
54
+ const { expandArgv1, initialzeModules, isMainThread } = options ;
55
+
56
+ refreshRuntimeOptions ( ) ;
43
57
reconnectZeroFillToggle ( ) ;
44
58
45
59
// Patch the process object with legacy properties and normalizations
@@ -61,48 +75,57 @@ function prepareMainThreadExecution(expandArgv1 = false,
61
75
}
62
76
63
77
setupDebugEnv ( ) ;
64
-
65
- // Print stack trace on `SIGINT` if option `--trace-sigint` presents.
66
- setupStacktracePrinterOnSigint ( ) ;
67
-
68
78
// Process initial diagnostic reporting configuration, if present.
69
79
initializeReport ( ) ;
70
- initializeReportSignalHandlers ( ) ; // Main-thread-only.
71
-
72
- initializeHeapSnapshotSignalHandlers ( ) ;
73
-
74
- // If the process is spawned with env NODE_CHANNEL_FD, it's probably
75
- // spawned by our child_process module, then initialize IPC.
76
- // This attaches some internal event listeners and creates:
77
- // process.send(), process.channel, process.connected,
78
- // process.disconnect().
79
- setupChildProcessIpcChannel ( ) ;
80
-
81
- // Load policy from disk and parse it.
82
- initializePolicy ( ) ;
83
-
84
- // If this is a worker in cluster mode, start up the communication
85
- // channel. This needs to be done before any user code gets executed
86
- // (including preload modules).
87
- initializeClusterIPC ( ) ;
88
-
89
80
initializeSourceMapsHandlers ( ) ;
90
81
initializeDeprecations ( ) ;
91
82
initializeWASI ( ) ;
92
-
93
83
require ( 'internal/dns/utils' ) . initializeDns ( ) ;
94
84
95
- require ( 'internal/v8/startup_snapshot' ) . runDeserializeCallbacks ( ) ;
85
+ if ( isMainThread ) {
86
+ assert ( internalBinding ( 'worker' ) . isMainThread ) ;
87
+ // Worker threads will get the manifest in the message handler.
88
+ const policy = readPolicyFromDisk ( ) ;
89
+ if ( policy ) {
90
+ require ( 'internal/process/policy' )
91
+ . setup ( policy . manifestSrc , policy . manifestURL ) ;
92
+ }
96
93
97
- if ( ! initialzeModules ) {
98
- return ;
94
+ // Print stack trace on `SIGINT` if option `--trace-sigint` presents.
95
+ setupStacktracePrinterOnSigint ( ) ;
96
+ initializeReportSignalHandlers ( ) ; // Main-thread-only.
97
+ initializeHeapSnapshotSignalHandlers ( ) ;
98
+ // If the process is spawned with env NODE_CHANNEL_FD, it's probably
99
+ // spawned by our child_process module, then initialize IPC.
100
+ // This attaches some internal event listeners and creates:
101
+ // process.send(), process.channel, process.connected,
102
+ // process.disconnect().
103
+ setupChildProcessIpcChannel ( ) ;
104
+ // If this is a worker in cluster mode, start up the communication
105
+ // channel. This needs to be done before any user code gets executed
106
+ // (including preload modules).
107
+ initializeClusterIPC ( ) ;
108
+
109
+ // TODO(joyeecheung): do this for worker threads as well.
110
+ require ( 'internal/v8/startup_snapshot' ) . runDeserializeCallbacks ( ) ;
111
+ } else {
112
+ assert ( ! internalBinding ( 'worker' ) . isMainThread ) ;
113
+ // The setup should be called in LOAD_SCRIPT message handler.
114
+ assert ( ! initialzeModules ) ;
115
+ }
116
+
117
+ if ( initialzeModules ) {
118
+ setupUserModules ( ) ;
99
119
}
120
+ }
100
121
122
+ function setupUserModules ( ) {
101
123
initializeCJSLoader ( ) ;
102
124
initializeESMLoader ( ) ;
103
125
const CJSLoader = require ( 'internal/modules/cjs/loader' ) ;
104
126
assert ( ! CJSLoader . hasLoadedAnyUserCJSModule ) ;
105
127
loadPreloadModules ( ) ;
128
+ // Need to be done after --require setup.
106
129
initializeFrozenIntrinsics ( ) ;
107
130
}
108
131
@@ -482,7 +505,7 @@ function initializeClusterIPC() {
482
505
}
483
506
}
484
507
485
- function initializePolicy ( ) {
508
+ function readPolicyFromDisk ( ) {
486
509
const experimentalPolicy = getOptionValue ( '--experimental-policy' ) ;
487
510
if ( experimentalPolicy ) {
488
511
process . emitWarning ( 'Policies are experimental.' ,
@@ -526,8 +549,9 @@ function initializePolicy() {
526
549
throw new ERR_MANIFEST_ASSERT_INTEGRITY ( manifestURL , realIntegrities ) ;
527
550
}
528
551
}
529
- require ( 'internal/process/policy' )
530
- . setup ( src , manifestURL . href ) ;
552
+ return {
553
+ manifestSrc : src , manifestURL : manifestURL . href
554
+ } ;
531
555
}
532
556
}
533
557
@@ -609,25 +633,8 @@ function markBootstrapComplete() {
609
633
}
610
634
611
635
module . exports = {
612
- refreshRuntimeOptions,
613
- patchProcessObject,
614
- setupCoverageHooks,
615
- setupWarningHandler,
616
- setupFetch,
617
- setupWebCrypto,
618
- setupCustomEvent,
619
- setupDebugEnv,
620
- setupPerfHooks,
636
+ setupUserModules,
621
637
prepareMainThreadExecution,
622
- initializeDeprecations,
623
- initializeESMLoader,
624
- initializeFrozenIntrinsics,
625
- initializeSourceMapsHandlers,
626
- loadPreloadModules,
627
- setupTraceCategoryState,
628
- setupInspectorHooks,
629
- initializeReport,
630
- initializeCJSLoader,
631
- initializeWASI,
638
+ prepareWorkerThreadExecution,
632
639
markBootstrapComplete
633
640
} ;
0 commit comments