Skip to content

Commit 66f91af

Browse files
committed
worker: implement SHARE_ENV. see nodejs/node#26544.
1 parent f317ed0 commit 66f91af

File tree

11 files changed

+46
-2
lines changed

11 files changed

+46
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Some caveats for the `child_process` backend:
7878
do not hold event loop references (`ref()` and `unref()` are noops).
7979
- Prior to node 10, objects like `Proxy`s can be serialized and cloned as they
8080
cannot be detected from regular javascript.
81+
- `SHARE_ENV` does not work and will throw an error if passed.
8182

8283
Caveats for the `web_workers` backend:
8384

@@ -102,6 +103,7 @@ Caveats for the `web_workers` backend:
102103
depending on your `Content-Security-Policy`.
103104
- `FileList` will emerge on the other side as an `Array` rather than a
104105
`FileList` when sent as `workerData`.
106+
- `SHARE_ENV` does not work and will throw an error if passed.
105107

106108
Caveats for the `polyfill` backend:
107109

lib/browser/env.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function parseEnv(name) {
2929
WORKER_DATA: undefined,
3030
WORKER_STDIN: false,
3131
WORKER_EVAL: false,
32+
WORKER_ENV: {},
3233
WORKER_BOOTSTRAP: null
3334
};
3435
}
@@ -38,7 +39,8 @@ function parseEnv(name) {
3839
WORKER_DATA: items[1],
3940
WORKER_STDIN: items[2],
4041
WORKER_EVAL: items[3],
41-
WORKER_BOOTSTRAP: items[4]
42+
WORKER_ENV: items[4],
43+
WORKER_BOOTSTRAP: items[5]
4244
};
4345
}
4446

lib/browser/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.MessagePort = MessagePortBase;
2323
exports.MessageChannel = MessageChannel;
2424
exports.Worker = Worker;
2525
exports.moveMessagePortToContext = null;
26+
exports.SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
2627
exports.importScripts = null;
2728

2829
exports.backend = backend.polyfill ? 'polyfill' : 'web_workers';

lib/browser/parent.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
WORKER_ID,
4141
WORKER_DATA,
4242
WORKER_STDIN,
43+
WORKER_ENV,
4344
WORKER_EVAL
4445
} = env;
4546

@@ -138,6 +139,9 @@ class Parent extends MessagePortBase {
138139
process.stderr = this._stderr;
139140

140141
injectConsole(this._console);
142+
143+
for (const key of Object.keys(WORKER_ENV))
144+
process.env[key] = WORKER_ENV[key];
141145
}
142146

143147
_handleMessage(event) {

lib/browser/thread.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ exports.MessagePort = MessagePortBase;
2525
exports.MessageChannel = MessageChannel;
2626
exports.Worker = Worker;
2727
exports.moveMessagePortToContext = null;
28+
exports.SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
2829
exports.importScripts = importScripts;
2930

3031
exports.backend = backend.polyfill ? 'polyfill' : 'web_workers';

lib/browser/worker.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const DEFAULT_BOOTSTRAP_URL =
4949

5050
const BOOTSTRAP_URL = env.WORKER_BOOTSTRAP || DEFAULT_BOOTSTRAP_URL;
5151

52+
const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
53+
5254
let uid = 1;
5355

5456
/**
@@ -68,6 +70,13 @@ class Worker extends EventEmitter {
6870
if (typeof options !== 'object')
6971
throw new ArgError('options', options, 'object');
7072

73+
if (options.env != null
74+
&& typeof options.env !== 'object'
75+
&& options.env !== SHARE_ENV) {
76+
throw new ArgError('env', options.env,
77+
['object', 'worker_threads.SHARE_ENV']);
78+
}
79+
7180
if (options.type != null && typeof options.type !== 'string')
7281
throw new ArgError('type', options.type, 'string');
7382

@@ -98,6 +107,9 @@ class Worker extends EventEmitter {
98107
let code = null;
99108
let type = options.type;
100109

110+
if (options.env === SHARE_ENV)
111+
throw new WorkerError(errors.NO_SHARE_ENV);
112+
101113
if (options.eval) {
102114
if (type === 'module')
103115
throw new WorkerError(errors.ES_MODULE, 'eval');
@@ -125,6 +137,7 @@ class Worker extends EventEmitter {
125137
options.workerData,
126138
Boolean(options.stdin),
127139
Boolean(options.eval),
140+
options.env || process.env,
128141
options.bootstrap || env.WORKER_BOOTSTRAP
129142
])
130143
});

lib/internal/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ const errors = {
130130
'Invalid port number (%s).'
131131
],
132132

133+
NO_SHARE_ENV: [
134+
'ERR_SHARE_ENV_NOT_SUPPORTED',
135+
'SHARE_ENV is not supported on this backend.'
136+
],
137+
133138
// High Level Worker Errors
134139
BLACKLIST: ['ERR_WORKER_BLACKLIST', 'Cannot bind blacklisted event: "%s".'],
135140
FATAL_ERROR: ['ERR_WORKER_FATAL_ERROR', 'Fatal exception.'],

lib/process/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ exports.MessagePort = MessagePortBase;
1919
exports.MessageChannel = MessageChannel;
2020
exports.Worker = Worker;
2121
exports.moveMessagePortToContext = null;
22+
exports.SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
2223
exports.importScripts = null;
2324

2425
exports.backend = 'child_process';

lib/process/thread.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ exports.MessagePort = MessagePortBase;
2525
exports.MessageChannel = MessageChannel;
2626
exports.Worker = Worker;
2727
exports.moveMessagePortToContext = null;
28+
exports.SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
2829
exports.importScripts = null;
2930

3031
exports.backend = 'child_process';

lib/process/worker.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const {
5555

5656
const children = new Set();
5757

58+
const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV');
59+
5860
let uid = 1;
5961
let exitBound = false;
6062
let bundled = null;
@@ -76,6 +78,13 @@ class Worker extends EventEmitter {
7678
if (typeof options !== 'object')
7779
throw new ArgError('options', options, 'object');
7880

81+
if (options.env != null
82+
&& typeof options.env !== 'object'
83+
&& options.env !== SHARE_ENV) {
84+
throw new ArgError('env', options.env,
85+
['object', 'worker_threads.SHARE_ENV']);
86+
}
87+
7988
if (options.execArgv && !Array.isArray(options.execArgv))
8089
throw new ArgError('execArgv', options.execArgv, 'Array');
8190

@@ -109,6 +118,9 @@ class Worker extends EventEmitter {
109118
const bin = process.execPath || process.argv[0];
110119
const args = [];
111120

121+
if (options.env === SHARE_ENV)
122+
throw new WorkerError(errors.NO_SHARE_ENV);
123+
112124
// Validate filename.
113125
if (!options.eval) {
114126
if (!isAbsolute(file)
@@ -229,9 +241,10 @@ class Worker extends EventEmitter {
229241
}
230242

231243
// Setup options.
244+
const env = options.env || process.env;
232245
const opt = {
233246
stdio: ['pipe', 'pipe', 'pipe'],
234-
env: Object.assign(Object.create(null), process.env, {
247+
env: Object.assign(Object.create(null), env, {
235248
BTHREADS_WORKER_ID: this.threadId.toString(10),
236249
BTHREADS_WORKER_DATA: encoding.stringify(options.workerData),
237250
BTHREADS_WORKER_STDIN: options.stdin ? '1' : '0',

0 commit comments

Comments
 (0)