From 01cd21973b26a2cbacbe143c5983cb4adf8e7681 Mon Sep 17 00:00:00 2001 From: Yael Hermon Date: Fri, 4 Jan 2019 20:02:25 +0200 Subject: [PATCH] worker: enable passing command line flags This PR adds the ability to provide Workers with their own execArgv flags in replacement of the main thread's execArgv. Only per-Isolate/per-Environment options are allowed. Per-Process options and V8 flags are not allowed. Passing an empty execArgv array will reset per-Isolate and per-Environment options of the Worker to their defaults. If execArgv option is not passed, the Worker will get the same flags as the main thread. Usage example: ``` const worker = new Worker(__filename, { execArgv: ['--trace-warnings'], }); ``` PR-URL: https://github.com/nodejs/node/pull/25467 Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Joyee Cheung --- doc/api/errors.md | 6 ++ doc/api/worker_threads.md | 9 ++- lib/internal/errors.js | 3 + lib/internal/worker.js | 13 +++- src/env-inl.h | 5 ++ src/env.h | 1 + src/node_worker.cc | 67 ++++++++++++++++++- src/node_worker.h | 5 +- test/parallel/test-internal-errors.js | 10 +++ test/parallel/test-worker-execargv-invalid.js | 35 ++++++++++ test/parallel/test-worker-execargv.js | 22 ++++++ 11 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-worker-execargv-invalid.js create mode 100644 test/parallel/test-worker-execargv.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 840ba9b85c12c2..96ba39dd486a29 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1909,6 +1909,12 @@ The fulfilled value of a linking promise is not a `vm.SourceTextModule` object. The current module's status does not allow for this operation. The specific meaning of the error depends on the specific function. + +### ERR_WORKER_INVALID_EXEC_ARGV + +The `execArgv` option passed to the `Worker` constructor contains +invalid flags. + ### ERR_WORKER_PATH diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 78f35412c32a65..05cafa9cb178e7 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -316,13 +316,16 @@ if (isMainThread) { occur as 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 + * `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 + * `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 + * `stderr` {boolean} If this is set to `true`, then `worker.stderr` will not automatically be piped through to `process.stderr` in the parent. + * `execArgv` {string[]} List of node CLI options passed to the worker. + V8 options (such as `--max-old-space-size`) and options that affect the + process (such as `--title`) are not supported. ### Event: 'error'