-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnode-worker-threads.js
More file actions
387 lines (348 loc) · 11.1 KB
/
Copy pathnode-worker-threads.js
File metadata and controls
387 lines (348 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"use strict";
// The `node:worker_threads` compatibility shim. The channel half —
// MessagePort, MessageChannel, BroadcastChannel, receiveMessageOnPort — is the
// real thing, shared with the globals of the same name. The thread half is a
// bridge over the runtime's own Worker: this runtime has no thread pool, no
// stdio plumbing and no per-thread environment, so what cannot be honoured
// throws with the option or function named rather than degrading silently.
// See docs/worker-threads.md for the real-vs-shim table.
const {
isMainThread,
threadId,
markAsUntransferable,
isMarkedAsUntransferable,
markAsUncloneable,
setEnvironmentData,
getEnvironmentData,
} = binding;
const {
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
Error,
FunctionPrototypeCall,
ObjectCreate,
ObjectDefineProperty,
ObjectFreeze,
Promise,
PromisePrototypeThen,
PromiseResolve,
SymbolFor,
SymbolToStringTag,
TypeError,
} = primordials;
const {
MessagePort,
MessageChannel,
receiveMessageOnPort,
} = require("internal/message-channel");
const { BroadcastChannel } = require("internal/broadcast-channel");
const {
EventTarget,
defineEventHandler,
globalEventTarget,
} = require("internal/events");
let MessageEvent;
function getMessageEvent() {
if (MessageEvent === undefined) {
({ MessageEvent } = require("internal/message-event"));
}
return MessageEvent;
}
const g = globalThis;
// The platform constructor this shim wraps, and the worker scope's channel
// back to its parent.
const NativeWorker = g.Worker;
const globalPostMessage = g.postMessage;
const addEventListener = EventTarget.prototype.addEventListener;
const removeEventListener = EventTarget.prototype.removeEventListener;
const dispatchEvent = EventTarget.prototype.dispatchEvent;
// Runs `fn` after the caller returns. Node reports 'online' and 'exit' from
// the thread's own lifecycle; the runtime's Worker has no equivalent signal,
// so both are reported off a microtask instead.
function soon(fn) {
PromisePrototypeThen(PromiseResolve(), fn);
}
function notSupported(name) {
throw new Error(`${name} is not supported in this runtime`);
}
// Worker options that carry meaning this runtime cannot honour. The three
// stdio ones default to false, so only an explicit request is an error.
const rejectedOptions = ["workerData", "env", "eval", "transferList"];
const rejectedStdio = ["stdin", "stdout", "stderr"];
class WorkerEmitter {
#listeners = ObjectCreate(null);
on(type, listener) {
if (typeof listener !== "function") {
throw new TypeError('The "listener" argument must be of type function');
}
const key = `${type}`;
const list = this.#listeners[key] || (this.#listeners[key] = []);
ArrayPrototypePush(list, { listener, once: false });
return this;
}
once(type, listener) {
if (typeof listener !== "function") {
throw new TypeError('The "listener" argument must be of type function');
}
const key = `${type}`;
const list = this.#listeners[key] || (this.#listeners[key] = []);
ArrayPrototypePush(list, { listener, once: true });
return this;
}
// Node removes the most recently added registration of a listener.
removeListener(type, listener) {
const list = this.#listeners[`${type}`];
if (list === undefined) {
return this;
}
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].listener === listener) {
ArrayPrototypeSplice(list, i, 1);
return this;
}
}
return this;
}
off(type, listener) {
return this.removeListener(type, listener);
}
emit(type, arg) {
const list = this.#listeners[type];
if (list === undefined) {
return;
}
const snapshot = ArrayPrototypeSlice(list);
for (let i = 0; i < snapshot.length; i++) {
const entry = snapshot[i];
if (entry.once) {
const index = ArrayPrototypeIndexOf(list, entry);
if (index !== -1) {
ArrayPrototypeSplice(list, index, 1);
}
}
FunctionPrototypeCall(entry.listener, this, arg);
}
}
}
class Worker extends WorkerEmitter {
#worker;
#exited = false;
// Every terminate() promise settles when the thread's end is reported.
#exitWaiters = [];
constructor(filename, options) {
super();
if (options !== undefined && options !== null) {
for (let i = 0; i < rejectedOptions.length; i++) {
if (options[rejectedOptions[i]] !== undefined) {
throw new TypeError(
`Worker option '${rejectedOptions[i]}' is not supported in this runtime`
);
}
}
for (let i = 0; i < rejectedStdio.length; i++) {
if (options[rejectedStdio[i]]) {
throw new TypeError(
`Worker option '${rejectedStdio[i]}' is not supported in this runtime`
);
}
}
}
// The runtime's own options (ios, resourceLimits) ride along; the native
// constructor ignores keys it does not know.
const worker =
options === undefined || options === null
? new NativeWorker(`${filename}`)
: new NativeWorker(`${filename}`, options);
this.#worker = worker;
const self = this;
worker.onmessage = function (event) {
self.emit("message", event.data);
};
worker.onmessageerror = function (event) {
self.emit("messageerror", event.data);
};
worker.onerror = function (error) {
self.emit("error", error);
};
// The runtime's end-of-worker event: the one place 'exit' comes from, for
// a worker's own close() and for terminate() alike, so nothing the worker
// sent before it ended can follow 'exit'.
FunctionPrototypeCall(
addEventListener,
worker,
"nsworkerended",
function () {
self.#reportExit();
}
);
soon(function () {
self.emit("online", undefined);
});
}
// Node emits 'exit' once and settles terminate() after it. The code is
// always 0: this runtime has no thread exit status to report, and the
// cross-runtime suite pins that for every end a worker can take.
#reportExit() {
if (this.#exited) {
return;
}
this.#exited = true;
this.emit("exit", 0);
const waiters = this.#exitWaiters;
this.#exitWaiters = [];
for (let i = 0; i < waiters.length; i++) {
waiters[i](0);
}
}
postMessage(value, transfer) {
this.#worker.postMessage(value, transfer);
}
// Resolves with the exit code once the thread has actually ended. A parent
// that is itself tearing down never delivers that signal, so the promise
// stays pending there, as it does in Node when the parent dies.
terminate() {
if (this.#exited) {
return PromiseResolve(0);
}
this.#worker.terminate();
const self = this;
return new Promise(function (resolve) {
ArrayPrototypePush(self.#exitWaiters, resolve);
});
}
}
ObjectDefineProperty(Worker.prototype, SymbolToStringTag, {
__proto__: null,
value: "Worker",
configurable: true,
});
// The worker scope's end of the parent channel. Not a MessagePort: it is not
// transferable and it has no queue of its own, it forwards to the worker
// globals the runtime already provides. close() is a no-op — a worker ends
// through its own close()/terminate().
class ParentPort extends EventTarget {
// Node's parentPort is an EventEmitter as well: on("message") receives the
// payload, not the event. Each listener is registered through its own
// wrapper so removal by the original function still works.
#wrappers = ObjectCreate(null);
postMessage(value, transfer) {
FunctionPrototypeCall(globalPostMessage, g, value, transfer);
}
start() {}
close() {}
on(type, listener) {
return this.#add(`${type}`, listener, false);
}
addListener(type, listener) {
return this.#add(`${type}`, listener, false);
}
once(type, listener) {
return this.#add(`${type}`, listener, true);
}
off(type, listener) {
this.#remove(`${type}`, listener);
return this;
}
removeListener(type, listener) {
this.#remove(`${type}`, listener);
return this;
}
#add(type, listener, once) {
if (typeof listener !== "function") {
throw new TypeError('The "listener" argument must be of type function');
}
const self = this;
const entry = { listener, wrapper: undefined };
// A once registration detaches its own entry, not whichever entry happens
// to hold the same listener: the same function may be on() and once() at
// the same time.
entry.wrapper = function (event) {
if (once) {
self.#removeEntry(type, entry);
}
const arg = type === "message" || type === "messageerror" ? event.data : event;
FunctionPrototypeCall(listener, self, arg);
};
const list = this.#wrappers[type] || (this.#wrappers[type] = []);
ArrayPrototypePush(list, entry);
FunctionPrototypeCall(addEventListener, this, type, entry.wrapper);
return this;
}
// Node removes the most recently added registration of a listener.
#remove(type, listener) {
const list = this.#wrappers[type];
if (list === undefined) {
return;
}
for (let i = list.length - 1; i >= 0; i--) {
if (list[i].listener === listener) {
this.#removeEntry(type, list[i]);
return;
}
}
}
#removeEntry(type, entry) {
const list = this.#wrappers[type];
if (list === undefined) {
return;
}
const index = ArrayPrototypeIndexOf(list, entry);
if (index === -1) {
return;
}
ArrayPrototypeSplice(list, index, 1);
FunctionPrototypeCall(removeEventListener, this, type, entry.wrapper);
}
}
defineEventHandler(ParentPort.prototype, "message");
defineEventHandler(ParentPort.prototype, "messageerror");
ObjectDefineProperty(ParentPort.prototype, SymbolToStringTag, {
__proto__: null,
value: "MessagePort",
configurable: true,
});
let parentPort = null;
if (!isMainThread) {
parentPort = new ParentPort();
const relay = function (event) {
FunctionPrototypeCall(
dispatchEvent,
parentPort,
new (getMessageEvent())(event.type, { data: event.data, ports: event.ports })
);
};
FunctionPrototypeCall(addEventListener, globalEventTarget, "message", relay);
FunctionPrototypeCall(addEventListener, globalEventTarget, "messageerror", relay);
}
module.exports = ObjectFreeze({
BroadcastChannel,
MessageChannel,
MessagePort,
// Exported so an `options.env === SHARE_ENV` spelling still resolves; this
// runtime has one environment and never copies it.
SHARE_ENV: SymbolFor("nodejs.worker_threads.SHARE_ENV"),
Worker,
getEnvironmentData,
isInternalThread: false,
isMainThread,
isMarkedAsUntransferable,
markAsUncloneable,
markAsUntransferable,
moveMessagePortToContext() {
notSupported("moveMessagePortToContext");
},
parentPort,
postMessageToThread() {
notSupported("postMessageToThread");
},
receiveMessageOnPort,
resourceLimits: {},
setEnvironmentData,
threadId,
threadName: undefined,
// No workerData: the Worker constructor rejects the option that would carry
// it, so there is never anything to hand a worker.
workerData: null,
});