Skip to content

Commit c502795

Browse files
committed
worker: replace message types string by constants
This change can prevent typos and redundant strings in code.
1 parent 8132413 commit c502795

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

lib/internal/worker.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');
4747

4848
const debug = util.debuglog('worker');
4949

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+
5059
// A communication channel consisting of a handle (that wraps around an
5160
// uv_async_t) which can receive information from other threads and emits
5261
// .onmessage events, and a function used for sending data to a MessagePort
@@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
158167
}
159168

160169
this[kPort].postMessage({
161-
type: 'stdioWantsMoreData',
170+
type: messageTypes.STDIO_WANTS_MORE_DATA,
162171
stream: this[kName]
163172
});
164173
}
@@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {
174183

175184
_write(chunk, encoding, cb) {
176185
this[kPort].postMessage({
177-
type: 'stdioPayload',
186+
type: messageTypes.STDIO_PAYLOAD,
178187
stream: this[kName],
179188
chunk,
180189
encoding
@@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {
186195

187196
_final(cb) {
188197
this[kPort].postMessage({
189-
type: 'stdioPayload',
198+
type: messageTypes.STDIO_PAYLOAD,
190199
stream: this[kName],
191200
chunk: null
192201
});
@@ -252,7 +261,7 @@ class Worker extends EventEmitter {
252261
this[kPublicPort].on('message', (message) => this.emit('message', message));
253262
setupPortReferencing(this[kPublicPort], this, 'message');
254263
this[kPort].postMessage({
255-
type: 'loadScript',
264+
type: messageTypes.LOAD_SCRIPT,
256265
filename,
257266
doEval: !!options.eval,
258267
workerData: options.workerData,
@@ -283,18 +292,18 @@ class Worker extends EventEmitter {
283292

284293
[kOnMessage](message) {
285294
switch (message.type) {
286-
case 'upAndRunning':
295+
case messageTypes.UP_AND_RUNNING:
287296
return this.emit('online');
288-
case 'couldNotSerializeError':
297+
case messageTypes.COULD_NOT_SERIALIZE_ERROR:
289298
return this[kOnCouldNotSerializeErr]();
290-
case 'errorMessage':
299+
case messageTypes.ERROR_MESSAGE:
291300
return this[kOnErrorMessage](message.error);
292-
case 'stdioPayload':
301+
case messageTypes.STDIO_PAYLOAD:
293302
{
294303
const { stream, chunk, encoding } = message;
295304
return this[kParentSideStdio][stream].push(chunk, encoding);
296305
}
297-
case 'stdioWantsMoreData':
306+
case messageTypes.STDIO_WANTS_MORE_DATA:
298307
{
299308
const { stream } = message;
300309
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
@@ -390,7 +399,7 @@ function setupChild(evalScript) {
390399
const publicWorker = require('worker_threads');
391400

392401
port.on('message', (message) => {
393-
if (message.type === 'loadScript') {
402+
if (message.type === messageTypes.LOAD_SCRIPT) {
394403
const { filename, doEval, workerData, publicPort, hasStdin } = message;
395404
publicWorker.parentPort = publicPort;
396405
setupPortReferencing(publicPort, publicPort, 'message');
@@ -402,19 +411,19 @@ function setupChild(evalScript) {
402411
debug(`[${threadId}] starts worker script ${filename} ` +
403412
`(eval = ${eval}) at cwd = ${process.cwd()}`);
404413
port.unref();
405-
port.postMessage({ type: 'upAndRunning' });
414+
port.postMessage({ type: messageTypes.UP_AND_RUNNING });
406415
if (doEval) {
407416
evalScript('[worker eval]', filename);
408417
} else {
409418
process.argv[1] = filename; // script filename
410419
require('module').runMain();
411420
}
412421
return;
413-
} else if (message.type === 'stdioPayload') {
422+
} else if (message.type === messageTypes.STDIO_PAYLOAD) {
414423
const { stream, chunk, encoding } = message;
415424
workerStdio[stream].push(chunk, encoding);
416425
return;
417-
} else if (message.type === 'stdioWantsMoreData') {
426+
} else if (message.type === messageTypes.STDIO_WANTS_MORE_DATA) {
418427
const { stream } = message;
419428
workerStdio[stream][kStdioWantsMoreDataCallback]();
420429
return;
@@ -445,9 +454,12 @@ function setupChild(evalScript) {
445454
} catch {}
446455
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
447456
if (serialized)
448-
port.postMessage({ type: 'errorMessage', error: serialized });
457+
port.postMessage({
458+
type: messageTypes.ERROR_MESSAGE,
459+
error: serialized
460+
});
449461
else
450-
port.postMessage({ type: 'couldNotSerializeError' });
462+
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
451463
clearAsyncIdStack();
452464
}
453465
}

0 commit comments

Comments
 (0)