@@ -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
} ) ;
@@ -258,7 +267,7 @@ class Worker extends EventEmitter {
258
267
this [ kPublicPort ] . on ( 'message' , ( message ) => this . emit ( 'message' , message ) ) ;
259
268
setupPortReferencing ( this [ kPublicPort ] , this , 'message' ) ;
260
269
this [ kPort ] . postMessage ( {
261
- type : 'loadScript' ,
270
+ type : messageTypes . LOAD_SCRIPT ,
262
271
filename,
263
272
doEval : ! ! options . eval ,
264
273
workerData : options . workerData ,
@@ -289,18 +298,18 @@ class Worker extends EventEmitter {
289
298
290
299
[ kOnMessage ] ( message ) {
291
300
switch ( message . type ) {
292
- case 'upAndRunning' :
301
+ case messageTypes . UP_AND_RUNNING :
293
302
return this . emit ( 'online' ) ;
294
- case 'couldNotSerializeError' :
303
+ case messageTypes . COULD_NOT_SERIALIZE_ERROR :
295
304
return this [ kOnCouldNotSerializeErr ] ( ) ;
296
- case 'errorMessage' :
305
+ case messageTypes . ERROR_MESSAGE :
297
306
return this [ kOnErrorMessage ] ( message . error ) ;
298
- case 'stdioPayload' :
307
+ case messageTypes . STDIO_PAYLOAD :
299
308
{
300
309
const { stream, chunk, encoding } = message ;
301
310
return this [ kParentSideStdio ] [ stream ] . push ( chunk , encoding ) ;
302
311
}
303
- case 'stdioWantsMoreData' :
312
+ case messageTypes . STDIO_WANTS_MORE_DATA :
304
313
{
305
314
const { stream } = message ;
306
315
return this [ kParentSideStdio ] [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
@@ -396,7 +405,7 @@ function setupChild(evalScript) {
396
405
const publicWorker = require ( 'worker_threads' ) ;
397
406
398
407
port . on ( 'message' , ( message ) => {
399
- if ( message . type === 'loadScript' ) {
408
+ if ( message . type === messageTypes . LOAD_SCRIPT ) {
400
409
const { filename, doEval, workerData, publicPort, hasStdin } = message ;
401
410
publicWorker . parentPort = publicPort ;
402
411
setupPortReferencing ( publicPort , publicPort , 'message' ) ;
@@ -408,19 +417,19 @@ function setupChild(evalScript) {
408
417
debug ( `[${ threadId } ] starts worker script ${ filename } ` +
409
418
`(eval = ${ eval } ) at cwd = ${ process . cwd ( ) } ` ) ;
410
419
port . unref ( ) ;
411
- port . postMessage ( { type : 'upAndRunning' } ) ;
420
+ port . postMessage ( { type : messageTypes . UP_AND_RUNNING } ) ;
412
421
if ( doEval ) {
413
422
evalScript ( '[worker eval]' , filename ) ;
414
423
} else {
415
424
process . argv [ 1 ] = filename ; // script filename
416
425
require ( 'module' ) . runMain ( ) ;
417
426
}
418
427
return ;
419
- } else if ( message . type === 'stdioPayload' ) {
428
+ } else if ( message . type === messageTypes . STDIO_PAYLOAD ) {
420
429
const { stream, chunk, encoding } = message ;
421
430
workerStdio [ stream ] . push ( chunk , encoding ) ;
422
431
return ;
423
- } else if ( message . type === 'stdioWantsMoreData' ) {
432
+ } else if ( message . type === messageTypes . STDIO_WANTS_MORE_DATA ) {
424
433
const { stream } = message ;
425
434
workerStdio [ stream ] [ kStdioWantsMoreDataCallback ] ( ) ;
426
435
return ;
@@ -451,9 +460,12 @@ function setupChild(evalScript) {
451
460
} catch { }
452
461
debug ( `[${ threadId } ] fatal exception serialized = ${ ! ! serialized } ` ) ;
453
462
if ( serialized )
454
- port . postMessage ( { type : 'errorMessage' , error : serialized } ) ;
463
+ port . postMessage ( {
464
+ type : messageTypes . ERROR_MESSAGE ,
465
+ error : serialized
466
+ } ) ;
455
467
else
456
- port . postMessage ( { type : 'couldNotSerializeError' } ) ;
468
+ port . postMessage ( { type : messageTypes . COULD_NOT_SERIALIZE_ERROR } ) ;
457
469
clearAsyncIdStack ( ) ;
458
470
}
459
471
}
0 commit comments