@@ -7,18 +7,21 @@ const {
7
7
threadId
8
8
} = internalBinding ( 'worker' ) ;
9
9
10
- const debug = require ( 'util' ) . debuglog ( 'worker' ) ;
11
-
12
10
const {
11
+ messageTypes,
12
+ kStdioWantsMoreDataCallback,
13
13
kWaitingStreams,
14
14
ReadableWorkerStdio,
15
15
WritableWorkerStdio
16
16
} = require ( 'internal/worker/io' ) ;
17
17
18
- const {
19
- createMessageHandler,
20
- createWorkerFatalExeception
21
- } = require ( 'internal/worker' ) ;
18
+ let debuglog ;
19
+ function debug ( ...args ) {
20
+ if ( ! debuglog ) {
21
+ debuglog = require ( 'util' ) . debuglog ( 'worker' ) ;
22
+ }
23
+ return debuglog ( ...args ) ;
24
+ }
22
25
23
26
const workerStdio = { } ;
24
27
@@ -36,12 +39,90 @@ function initializeWorkerStdio() {
36
39
} ;
37
40
}
38
41
42
+ function createMessageHandler ( port ) {
43
+ const publicWorker = require ( 'worker_threads' ) ;
44
+
45
+ return function ( message ) {
46
+ if ( message . type === messageTypes . LOAD_SCRIPT ) {
47
+ const { filename, doEval, workerData, publicPort, hasStdin } = message ;
48
+ publicWorker . parentPort = publicPort ;
49
+ publicWorker . workerData = workerData ;
50
+
51
+ if ( ! hasStdin )
52
+ workerStdio . stdin . push ( null ) ;
53
+
54
+ debug ( `[${ threadId } ] starts worker script ${ filename } ` +
55
+ `(eval = ${ eval } ) at cwd = ${ process . cwd ( ) } ` ) ;
56
+ port . unref ( ) ;
57
+ port . postMessage ( { type : messageTypes . UP_AND_RUNNING } ) ;
58
+ if ( doEval ) {
59
+ const { evalScript } = require ( 'internal/process/execution' ) ;
60
+ evalScript ( '[worker eval]' , filename ) ;
61
+ } else {
62
+ process . argv [ 1 ] = filename ; // script filename
63
+ require ( 'module' ) . runMain ( ) ;
64
+ }
65
+ return ;
66
+ } else if ( message . type === messageTypes . STDIO_PAYLOAD ) {
67
+ const { stream, chunk, encoding } = message ;
68
+ workerStdio [ stream ] . push ( chunk , encoding ) ;
69
+ return ;
70
+ } else if ( message . type === messageTypes . STDIO_WANTS_MORE_DATA ) {
71
+ const { stream } = message ;
72
+ workerStdio [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
73
+ return ;
74
+ }
75
+
76
+ require ( 'assert' ) . fail ( `Unknown worker message type ${ message . type } ` ) ;
77
+ } ;
78
+ }
79
+
80
+ // XXX(joyeecheung): this has to be returned as an anonymous function
81
+ // wrapped in a closure, see the comment of the original
82
+ // process._fatalException in lib/internal/process/execution.js
83
+ function createWorkerFatalExeception ( port ) {
84
+ const {
85
+ fatalException : originalFatalException
86
+ } = require ( 'internal/process/execution' ) ;
87
+
88
+ return ( error ) => {
89
+ debug ( `[${ threadId } ] gets fatal exception` ) ;
90
+ let caught = false ;
91
+ try {
92
+ caught = originalFatalException . call ( this , error ) ;
93
+ } catch ( e ) {
94
+ error = e ;
95
+ }
96
+ debug ( `[${ threadId } ] fatal exception caught = ${ caught } ` ) ;
97
+
98
+ if ( ! caught ) {
99
+ let serialized ;
100
+ try {
101
+ const { serializeError } = require ( 'internal/error-serdes' ) ;
102
+ serialized = serializeError ( error ) ;
103
+ } catch { }
104
+ debug ( `[${ threadId } ] fatal exception serialized = ${ ! ! serialized } ` ) ;
105
+ if ( serialized )
106
+ port . postMessage ( {
107
+ type : messageTypes . ERROR_MESSAGE ,
108
+ error : serialized
109
+ } ) ;
110
+ else
111
+ port . postMessage ( { type : messageTypes . COULD_NOT_SERIALIZE_ERROR } ) ;
112
+
113
+ const { clearAsyncIdStack } = require ( 'internal/async_hooks' ) ;
114
+ clearAsyncIdStack ( ) ;
115
+
116
+ process . exit ( ) ;
117
+ }
118
+ } ;
119
+ }
120
+
39
121
function setup ( ) {
40
122
debug ( `[${ threadId } ] is setting up worker child environment` ) ;
41
123
42
124
const port = getEnvMessagePort ( ) ;
43
- const publicWorker = require ( 'worker_threads' ) ;
44
- port . on ( 'message' , createMessageHandler ( publicWorker , port , workerStdio ) ) ;
125
+ port . on ( 'message' , createMessageHandler ( port ) ) ;
45
126
port . start ( ) ;
46
127
47
128
return {
0 commit comments