@@ -125,6 +125,25 @@ if (isMainThread) {
125
125
}
126
126
```
127
127
128
+ ## worker.SHARE_ENV
129
+ <!-- YAML
130
+ added: REPLACEME
131
+ -->
132
+
133
+ * {symbol}
134
+
135
+ A special value that can be passed as the ` env ` option of the [ ` Worker ` ] [ ]
136
+ constructor, to indicate that the current thread and the Worker thread should
137
+ share read and write access to the same set of environment variables.
138
+
139
+ ``` js
140
+ const { Worker , SHARE_ENV } = require (' worker_threads' );
141
+ new Worker (' process.env.SET_IN_WORKER = "foo"' , { eval: true , env: SHARE_ENV })
142
+ .on (' exit' , () => {
143
+ console .log (process .env .SET_IN_WORKER ); // Prints 'foo'.
144
+ });
145
+ ```
146
+
128
147
## worker.threadId
129
148
<!-- YAML
130
149
added: v10.5.0
@@ -380,7 +399,11 @@ Notable differences inside a Worker environment are:
380
399
and [ ` process.abort() ` ] [ ] is not available.
381
400
- [ ` process.chdir() ` ] [ ] and ` process ` methods that set group or user ids
382
401
are not available.
383
- - [ ` process.env ` ] [ ] is a read-only reference to the environment variables.
402
+ - [ ` process.env ` ] [ ] is a copy of the parent thread's environment variables,
403
+ unless otherwise specified. Changes to one copy will not be visible in other
404
+ threads, and will not be visible to native add-ons (unless
405
+ [ ` worker.SHARE_ENV ` ] [ ] has been passed as the ` env ` option to the
406
+ [ ` Worker ` ] [ ] constructor).
384
407
- [ ` process.title ` ] [ ] cannot be modified.
385
408
- Signals will not be delivered through [ ` process.on('...') ` ] [ Signals events ] .
386
409
- Execution may stop at any point as a result of [ ` worker.terminate() ` ] [ ]
@@ -439,25 +462,30 @@ if (isMainThread) {
439
462
If ` options.eval ` is ` true ` , this is a string containing JavaScript code
440
463
rather than a path.
441
464
* ` options ` {Object}
465
+ * ` env ` {Object} If set, specifies the initial value of ` process.env ` inside
466
+ the Worker thread. As a special value, [ ` worker.SHARE_ENV ` ] [ ] may be used
467
+ to specify that the parent thread and the child thread should share their
468
+ environment variables; in that case, changes to one thread’s ` process.env `
469
+ object will affect the other thread as well. ** Default:** ` process.env ` .
442
470
* ` eval ` {boolean} If ` true ` , interpret the first argument to the constructor
443
471
as a script that is executed once the worker is online.
444
- * ` workerData ` {any} Any JavaScript value that will be cloned and made
445
- available as [ ` require('worker_threads').workerData ` ] [ ] . The cloning will
446
- occur as described in the [ HTML structured clone algorithm ] [ ] , and an error
447
- will be thrown if the object cannot be cloned (e.g. because it contains
448
- ` function ` s) .
472
+ * ` execArgv ` {string [ ] } List of node CLI options passed to the worker.
473
+ V8 options (such as ` --max-old-space-size ` ) and options that affect the
474
+ process (such as ` --title ` ) are not supported. If set, this will be provided
475
+ as [ ` process.execArgv ` ] [ ] inside the worker. By default, options will be
476
+ inherited from the parent thread .
449
477
* ` stdin ` {boolean} If this is set to ` true ` , then ` worker.stdin ` will
450
478
provide a writable stream whose contents will appear as ` process.stdin `
451
479
inside the Worker. By default, no data is provided.
452
480
* ` stdout ` {boolean} If this is set to ` true ` , then ` worker.stdout ` will
453
481
not automatically be piped through to ` process.stdout ` in the parent.
454
482
* ` stderr ` {boolean} If this is set to ` true ` , then ` worker.stderr ` will
455
483
not automatically be piped through to ` process.stderr ` in the parent.
456
- * ` execArgv ` {string [ ] } List of node CLI options passed to the worker.
457
- V8 options (such as ` --max-old-space-size ` ) and options that affect the
458
- process (such as ` --title ` ) are not supported. If set, this will be provided
459
- as [ ` process.execArgv ` ] [ ] inside the worker. By default, options will be
460
- inherited from the parent thread .
484
+ * ` workerData ` {any} Any JavaScript value that will be cloned and made
485
+ available as [ ` require('worker_threads').workerData ` ] [ ] . The cloning will
486
+ occur as described in the [ HTML structured clone algorithm ] [ ] , and an error
487
+ will be thrown if the object cannot be cloned (e.g. because it contains
488
+ ` function ` s) .
461
489
462
490
### Event: 'error'
463
491
<!-- YAML
@@ -628,6 +656,7 @@ active handle in the event system. If the worker is already `unref()`ed calling
628
656
[ `vm` ] : vm.html
629
657
[ `worker.on('message')` ] : #worker_threads_event_message_1
630
658
[ `worker.postMessage()` ] : #worker_threads_worker_postmessage_value_transferlist
659
+ [ `worker.SHARE_ENV` ] : #worker_threads_worker_share_env
631
660
[ `worker.terminate()` ] : #worker_threads_worker_terminate_callback
632
661
[ `worker.threadId` ] : #worker_threads_worker_threadid_1
633
662
[ Addons worker support ] : addons.html#addons_worker_support
0 commit comments