@@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');
47
47
48
48
const debug = util . debuglog ( 'worker' ) ;
49
49
50
+ const messageTypes = {
51
+ UP_AND_RUNNING : 'upAndRunning' ,
52
+ COULD_NOT_SERIALIZE_ERROR : 'couldNotSerializeError' ,
53
+ ERROR_MESSAGE : 'errorMessage' ,
54
+ STDIO_PAYLOAD : 'stdioPayload' ,
55
+ STDIO_WANTS_MORE_DATA : 'stdioWantsMoreData' ,
56
+ LOAD_SCRIPT : 'loadScript'
57
+ } ;
58
+
50
59
// A communication channel consisting of a handle (that wraps around an
51
60
// uv_async_t) which can receive information from other threads and emits
52
61
// .onmessage events, and a function used for sending data to a MessagePort
@@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
158
167
}
159
168
160
169
this [ kPort ] . postMessage ( {
161
- type : 'stdioWantsMoreData' ,
170
+ type : messageTypes . STDIO_WANTS_MORE_DATA ,
162
171
stream : this [ kName ]
163
172
} ) ;
164
173
}
@@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {
174
183
175
184
_write ( chunk , encoding , cb ) {
176
185
this [ kPort ] . postMessage ( {
177
- type : 'stdioPayload' ,
186
+ type : messageTypes . STDIO_PAYLOAD ,
178
187
stream : this [ kName ] ,
179
188
chunk,
180
189
encoding
@@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {
186
195
187
196
_final ( cb ) {
188
197
this [ kPort ] . postMessage ( {
189
- type : 'stdioPayload' ,
198
+ type : messageTypes . STDIO_PAYLOAD ,
190
199
stream : this [ kName ] ,
191
200
chunk : null
192
201
} ) ;
@@ -252,7 +261,7 @@ class Worker extends EventEmitter {
252
261
this [ kPublicPort ] . on ( 'message' , ( message ) => this . emit ( 'message' , message ) ) ;
253
262
setupPortReferencing ( this [ kPublicPort ] , this , 'message' ) ;
254
263
this [ kPort ] . postMessage ( {
255
- type : 'loadScript' ,
264
+ type : messageTypes . LOAD_SCRIPT ,
256
265
filename,
257
266
doEval : ! ! options . eval ,
258
267
workerData : options . workerData ,
@@ -283,18 +292,18 @@ class Worker extends EventEmitter {
283
292
284
293
[ kOnMessage ] ( message ) {
285
294
switch ( message . type ) {
286
- case 'upAndRunning' :
295
+ case messageTypes . UP_AND_RUNNING :
287
296
return this . emit ( 'online' ) ;
288
- case 'couldNotSerializeError' :
297
+ case messageTypes . COULD_NOT_SERIALIZE_ERROR :
289
298
return this [ kOnCouldNotSerializeErr ] ( ) ;
290
- case 'errorMessage' :
299
+ case messageTypes . ERROR_MESSAGE :
291
300
return this [ kOnErrorMessage ] ( message . error ) ;
292
- case 'stdioPayload' :
301
+ case messageTypes . STDIO_PAYLOAD :
293
302
{
294
303
const { stream, chunk, encoding } = message ;
295
304
return this [ kParentSideStdio ] [ stream ] . push ( chunk , encoding ) ;
296
305
}
297
- case 'stdioWantsMoreData' :
306
+ case messageTypes . STDIO_WANTS_MORE_DATA :
298
307
{
299
308
const { stream } = message ;
300
309
return this [ kParentSideStdio ] [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
@@ -390,7 +399,7 @@ function setupChild(evalScript) {
390
399
const publicWorker = require ( 'worker_threads' ) ;
391
400
392
401
port . on ( 'message' , ( message ) => {
393
- if ( message . type === 'loadScript' ) {
402
+ if ( message . type === messageTypes . LOAD_SCRIPT ) {
394
403
const { filename, doEval, workerData, publicPort, hasStdin } = message ;
395
404
publicWorker . parentPort = publicPort ;
396
405
setupPortReferencing ( publicPort , publicPort , 'message' ) ;
@@ -402,19 +411,19 @@ function setupChild(evalScript) {
402
411
debug ( `[${ threadId } ] starts worker script ${ filename } ` +
403
412
`(eval = ${ eval } ) at cwd = ${ process . cwd ( ) } ` ) ;
404
413
port . unref ( ) ;
405
- port . postMessage ( { type : 'upAndRunning' } ) ;
414
+ port . postMessage ( { type : messageTypes . UP_AND_RUNNING } ) ;
406
415
if ( doEval ) {
407
416
evalScript ( '[worker eval]' , filename ) ;
408
417
} else {
409
418
process . argv [ 1 ] = filename ; // script filename
410
419
require ( 'module' ) . runMain ( ) ;
411
420
}
412
421
return ;
413
- } else if ( message . type === 'stdioPayload' ) {
422
+ } else if ( message . type === messageTypes . STDIO_PAYLOAD ) {
414
423
const { stream, chunk, encoding } = message ;
415
424
workerStdio [ stream ] . push ( chunk , encoding ) ;
416
425
return ;
417
- } else if ( message . type === 'stdioWantsMoreData' ) {
426
+ } else if ( message . type === messageTypes . STDIO_WANTS_MORE_DATA ) {
418
427
const { stream } = message ;
419
428
workerStdio [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
420
429
return ;
@@ -445,9 +454,12 @@ function setupChild(evalScript) {
445
454
} catch { }
446
455
debug ( `[${ threadId } ] fatal exception serialized = ${ ! ! serialized } ` ) ;
447
456
if ( serialized )
448
- port . postMessage ( { type : 'errorMessage' , error : serialized } ) ;
457
+ port . postMessage ( {
458
+ type : messageTypes . ERROR_MESSAGE ,
459
+ error : serialized
460
+ } ) ;
449
461
else
450
- port . postMessage ( { type : 'couldNotSerializeError' } ) ;
462
+ port . postMessage ( { type : messageTypes . COULD_NOT_SERIALIZE_ERROR } ) ;
451
463
clearAsyncIdStack ( ) ;
452
464
}
453
465
}
0 commit comments