From 6b1a887aa2fc7197fa787934188d24cd0dd574e7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 13 May 2018 23:25:14 +0200 Subject: [PATCH] worker: enable stdio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide `stdin`, `stdout` and `stderr` options for the `Worker` constructor, and make these available to the worker thread under their usual names. The default for `stdin` is an empty stream, the default for `stdout` and `stderr` is redirecting to the parent thread’s corresponding stdio streams. PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Shingo Inoue Reviewed-By: Matteo Collina Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: John-David Dalton Reviewed-By: Gus Caplan --- doc/api/worker.md | 44 +++++++- lib/internal/process/stdio.js | 14 +-- lib/internal/worker.js | 166 ++++++++++++++++++++++++++++- test/parallel/test-worker-stdio.js | 43 ++++++++ 4 files changed, 256 insertions(+), 11 deletions(-) create mode 100644 test/parallel/test-worker-stdio.js diff --git a/doc/api/worker.md b/doc/api/worker.md index 3517a4c86ac767..2fa55cfa2ddadf 100644 --- a/doc/api/worker.md +++ b/doc/api/worker.md @@ -240,7 +240,7 @@ Most Node.js APIs are available inside of it. Notable differences inside a Worker environment are: - The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][] - properties are set to `null`. + may be redirected by the parent thread. - The [`require('worker').isMainThread`][] property is set to `false`. - The [`require('worker').parentPort`][] message port is available, - [`process.exit()`][] does not stop the whole program, just the single thread, @@ -313,6 +313,13 @@ if (isMainThread) { described in the [HTML structured clone algorithm][], and an error will be thrown if the object cannot be cloned (e.g. because it contains `function`s). + * stdin {boolean} If this is set to `true`, then `worker.stdin` will + provide a writable stream whose contents will appear as `process.stdin` + inside the Worker. By default, no data is provided. + * stdout {boolean} If this is set to `true`, then `worker.stdout` will + not automatically be piped through to `process.stdout` in the parent. + * stderr {boolean} If this is set to `true`, then `worker.stderr` will + not automatically be piped through to `process.stderr` in the parent. ### Event: 'error' + +* {stream.Readable} + +This is a readable stream which contains data written to [`process.stderr`][] +inside the worker thread. If `stderr: true` was not passed to the +[`Worker`][] constructor, then data will be piped to the parent thread's +[`process.stderr`][] stream. + +### worker.stdin + + +* {null|stream.Writable} + +If `stdin: true` was passed to the [`Worker`][] constructor, this is a +writable stream. The data written to this stream will be made available in +the worker thread as [`process.stdin`][]. + +### worker.stdout + + +* {stream.Readable} + +This is a readable stream which contains data written to [`process.stdout`][] +inside the worker thread. If `stdout: true` was not passed to the +[`Worker`][] constructor, then data will be piped to the parent thread's +[`process.stdout`][] stream. + ### worker.terminate([callback])