summaryrefslogtreecommitdiff
path: root/lib/internal/process/worker_thread_only.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/process/worker_thread_only.js')
-rw-r--r--lib/internal/process/worker_thread_only.js51
1 files changed, 41 insertions, 10 deletions
diff --git a/lib/internal/process/worker_thread_only.js b/lib/internal/process/worker_thread_only.js
index 834ba6078f..a9332fb427 100644
--- a/lib/internal/process/worker_thread_only.js
+++ b/lib/internal/process/worker_thread_only.js
@@ -2,23 +2,54 @@
// This file contains process bootstrappers that can only be
// run in the worker thread.
+const {
+ getEnvMessagePort,
+ threadId
+} = internalBinding('worker');
+
+const debug = require('util').debuglog('worker');
const {
- setupProcessStdio
-} = require('internal/process/stdio');
+ kWaitingStreams,
+ ReadableWorkerStdio,
+ WritableWorkerStdio
+} = require('internal/worker/io');
const {
- workerStdio
+ createMessageHandler,
+ createWorkerFatalExeception
} = require('internal/worker');
-function setupStdio() {
- setupProcessStdio({
- getStdout: () => workerStdio.stdout,
- getStderr: () => workerStdio.stderr,
- getStdin: () => workerStdio.stdin
- });
+const workerStdio = {};
+
+function initializeWorkerStdio() {
+ const port = getEnvMessagePort();
+ port[kWaitingStreams] = 0;
+ workerStdio.stdin = new ReadableWorkerStdio(port, 'stdin');
+ workerStdio.stdout = new WritableWorkerStdio(port, 'stdout');
+ workerStdio.stderr = new WritableWorkerStdio(port, 'stderr');
+
+ return {
+ getStdout() { return workerStdio.stdout; },
+ getStderr() { return workerStdio.stderr; },
+ getStdin() { return workerStdio.stdin; }
+ };
+}
+
+function setup() {
+ debug(`[${threadId}] is setting up worker child environment`);
+
+ const port = getEnvMessagePort();
+ const publicWorker = require('worker_threads');
+ port.on('message', createMessageHandler(publicWorker, port, workerStdio));
+ port.start();
+
+ return {
+ workerFatalExeception: createWorkerFatalExeception(port)
+ };
}
module.exports = {
- setupStdio
+ initializeWorkerStdio,
+ setup
};