Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: replace message types string by constants #21537

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const kIncrementsPortRef = Symbol('kIncrementsPortRef');

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

const messageTypes = {
UP_AND_RUNNING: 'upAndRunning',
COULD_NOT_SERIALIZE_ERROR: 'couldNotSerializeError',
ERROR_MESSAGE: 'errorMessage',
STDIO_PAYLOAD: 'stdioPayload',
STDIO_WANTS_MORE_DATA: 'stdioWantsMoreData',
LOAD_SCRIPT: 'loadScript'
};

// A communication channel consisting of a handle (that wraps around an
// uv_async_t) which can receive information from other threads and emits
// .onmessage events, and a function used for sending data to a MessagePort
Expand Down Expand Up @@ -158,7 +167,7 @@ class ReadableWorkerStdio extends Readable {
}

this[kPort].postMessage({
type: 'stdioWantsMoreData',
type: messageTypes.STDIO_WANTS_MORE_DATA,
stream: this[kName]
});
}
Expand All @@ -174,7 +183,7 @@ class WritableWorkerStdio extends Writable {

_write(chunk, encoding, cb) {
this[kPort].postMessage({
type: 'stdioPayload',
type: messageTypes.STDIO_PAYLOAD,
stream: this[kName],
chunk,
encoding
Expand All @@ -186,7 +195,7 @@ class WritableWorkerStdio extends Writable {

_final(cb) {
this[kPort].postMessage({
type: 'stdioPayload',
type: messageTypes.STDIO_PAYLOAD,
stream: this[kName],
chunk: null
});
Expand Down Expand Up @@ -252,7 +261,7 @@ class Worker extends EventEmitter {
this[kPublicPort].on('message', (message) => this.emit('message', message));
setupPortReferencing(this[kPublicPort], this, 'message');
this[kPort].postMessage({
type: 'loadScript',
type: messageTypes.LOAD_SCRIPT,
filename,
doEval: !!options.eval,
workerData: options.workerData,
Expand Down Expand Up @@ -283,18 +292,18 @@ class Worker extends EventEmitter {

[kOnMessage](message) {
switch (message.type) {
case 'upAndRunning':
case messageTypes.UP_AND_RUNNING:
return this.emit('online');
case 'couldNotSerializeError':
case messageTypes.COULD_NOT_SERIALIZE_ERROR:
return this[kOnCouldNotSerializeErr]();
case 'errorMessage':
case messageTypes.ERROR_MESSAGE:
return this[kOnErrorMessage](message.error);
case 'stdioPayload':
case messageTypes.STDIO_PAYLOAD:
{
const { stream, chunk, encoding } = message;
return this[kParentSideStdio][stream].push(chunk, encoding);
}
case 'stdioWantsMoreData':
case messageTypes.STDIO_WANTS_MORE_DATA:
{
const { stream } = message;
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
Expand Down Expand Up @@ -390,7 +399,7 @@ function setupChild(evalScript) {
const publicWorker = require('worker_threads');

port.on('message', (message) => {
if (message.type === 'loadScript') {
if (message.type === messageTypes.LOAD_SCRIPT) {
const { filename, doEval, workerData, publicPort, hasStdin } = message;
publicWorker.parentPort = publicPort;
setupPortReferencing(publicPort, publicPort, 'message');
Expand All @@ -402,19 +411,19 @@ function setupChild(evalScript) {
debug(`[${threadId}] starts worker script ${filename} ` +
`(eval = ${eval}) at cwd = ${process.cwd()}`);
port.unref();
port.postMessage({ type: 'upAndRunning' });
port.postMessage({ type: messageTypes.UP_AND_RUNNING });
if (doEval) {
evalScript('[worker eval]', filename);
} else {
process.argv[1] = filename; // script filename
require('module').runMain();
}
return;
} else if (message.type === 'stdioPayload') {
} else if (message.type === messageTypes.STDIO_PAYLOAD) {
const { stream, chunk, encoding } = message;
workerStdio[stream].push(chunk, encoding);
return;
} else if (message.type === 'stdioWantsMoreData') {
} else if (message.type === messageTypes.STDIO_WANTS_MORE_DATA) {
const { stream } = message;
workerStdio[stream][kStdioWantsMoreDataCallback]();
return;
Expand Down Expand Up @@ -445,9 +454,12 @@ function setupChild(evalScript) {
} catch {}
debug(`[${threadId}] fatal exception serialized = ${!!serialized}`);
if (serialized)
port.postMessage({ type: 'errorMessage', error: serialized });
port.postMessage({
type: messageTypes.ERROR_MESSAGE,
error: serialized
});
else
port.postMessage({ type: 'couldNotSerializeError' });
port.postMessage({ type: messageTypes.COULD_NOT_SERIALIZE_ERROR });
clearAsyncIdStack();
}
}
Expand Down