deno.land / std@0.166.0 / node / worker_threads.ts

worker_threads.ts
نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import { resolve, toFileUrl } from "../path/mod.ts";import { notImplemented } from "./_utils.ts";import { EventEmitter, once } from "./events.ts";
let environmentData = new Map();let threads = 0;
export interface WorkerOptions { // only for typings argv?: unknown[]; env?: Record<string, unknown>; execArgv?: string[]; stdin?: boolean; stdout?: boolean; stderr?: boolean; trackUnmanagedFds?: boolean; resourceLimits?: { maxYoungGenerationSizeMb?: number; maxOldGenerationSizeMb?: number; codeRangeSizeMb?: number; stackSizeMb?: number; };
eval?: boolean; transferList?: Transferable[]; workerData?: unknown;}
const kHandle = Symbol("kHandle");const PRIVATE_WORKER_THREAD_NAME = "$DENO_STD_NODE_WORKER_THREAD";class _Worker extends EventEmitter { readonly threadId: number; readonly resourceLimits: Required< NonNullable<WorkerOptions["resourceLimits"]> > = { maxYoungGenerationSizeMb: -1, maxOldGenerationSizeMb: -1, codeRangeSizeMb: -1, stackSizeMb: 4, }; private readonly [kHandle]: Worker;
postMessage: Worker["postMessage"];
constructor(specifier: URL | string, options?: WorkerOptions) { super(); if (options?.eval === true) { specifier = `data:text/javascript,${specifier}`; } else if (typeof specifier === "string") { specifier = toFileUrl(resolve(specifier)); } const handle = this[kHandle] = new Worker( specifier, { name: PRIVATE_WORKER_THREAD_NAME, type: "module", } as globalThis.WorkerOptions, // bypass unstable type error ); handle.addEventListener( "error", (event) => this.emit("error", event.error || event.message), ); handle.addEventListener( "messageerror", (event) => this.emit("messageerror", event.data), ); handle.addEventListener( "message", (event) => this.emit("message", event.data), ); handle.postMessage({ environmentData, threadId: (this.threadId = ++threads), workerData: options?.workerData, }, options?.transferList || []); this.postMessage = handle.postMessage.bind(handle); this.emit("online"); }
terminate() { this[kHandle].terminate(); this.emit("exit", 0); }
readonly getHeapSnapshot = () => notImplemented("Worker.prototype.getHeapSnapshot"); // fake performance readonly performance = globalThis.performance;}
export const isMainThread = // deno-lint-ignore no-explicit-any (globalThis as any).name !== PRIVATE_WORKER_THREAD_NAME;
// fake resourceLimitsexport const resourceLimits = isMainThread ? {} : { maxYoungGenerationSizeMb: 48, maxOldGenerationSizeMb: 2048, codeRangeSizeMb: 0, stackSizeMb: 4,};
let threadId = 0;let workerData: unknown = null;
// Like https://github.com/nodejs/node/blob/48655e17e1d84ba5021d7a94b4b88823f7c9c6cf/lib/internal/event_target.js#L611interface NodeEventTarget extends Pick< EventEmitter, "eventNames" | "listenerCount" | "emit" | "removeAllListeners" > { setMaxListeners(n: number): void; getMaxListeners(): number; // deno-lint-ignore no-explicit-any off(eventName: string, listener: (...args: any[]) => void): NodeEventTarget; // deno-lint-ignore no-explicit-any on(eventName: string, listener: (...args: any[]) => void): NodeEventTarget; // deno-lint-ignore no-explicit-any once(eventName: string, listener: (...args: any[]) => void): NodeEventTarget; addListener: NodeEventTarget["on"]; removeListener: NodeEventTarget["off"];}
type ParentPort = typeof self & NodeEventTarget;
// deno-lint-ignore no-explicit-anylet parentPort: ParentPort = null as any;
if (!isMainThread) { // deno-lint-ignore no-explicit-any delete (globalThis as any).name; // deno-lint-ignore no-explicit-any const listeners = new WeakMap<(...args: any[]) => void, (ev: any) => any>();
parentPort = self as ParentPort; parentPort.off = parentPort.removeListener = function ( this: ParentPort, name, listener, ) { this.removeEventListener(name, listeners.get(listener)!); listeners.delete(listener); return this; }; parentPort.on = parentPort.addListener = function ( this: ParentPort, name, listener, ) { // deno-lint-ignore no-explicit-any const _listener = (ev: any) => listener(ev.data); listeners.set(listener, _listener); this.addEventListener(name, _listener); return this; }; parentPort.once = function (this: ParentPort, name, listener) { // deno-lint-ignore no-explicit-any const _listener = (ev: any) => listener(ev.data); listeners.set(listener, _listener); this.addEventListener(name, _listener); return this; };
// mocks parentPort.setMaxListeners = () => {}; parentPort.getMaxListeners = () => Infinity; parentPort.eventNames = () => [""]; parentPort.listenerCount = () => 0;
parentPort.emit = () => notImplemented("parentPort.emit"); parentPort.removeAllListeners = () => notImplemented("parentPort.removeAllListeners");
// Receive startup message [{ threadId, workerData, environmentData }] = await once( parentPort, "message", );
// alias parentPort.addEventListener("offline", () => { parentPort.emit("close"); });}
export function getEnvironmentData(key: unknown) { return environmentData.get(key);}
export function setEnvironmentData(key: unknown, value?: unknown) { if (value === undefined) { environmentData.delete(key); } else { environmentData.set(key, value); }}
// deno-lint-ignore no-explicit-anyconst _MessagePort: typeof MessagePort = (globalThis as any).MessagePort;const _MessageChannel: typeof MessageChannel = // deno-lint-ignore no-explicit-any (globalThis as any).MessageChannel;export const BroadcastChannel = globalThis.BroadcastChannel;export const SHARE_ENV = Symbol.for("nodejs.worker_threads.SHARE_ENV");export function markAsUntransferable() { notImplemented("markAsUntransferable");}export function moveMessagePortToContext() { notImplemented("moveMessagePortToContext");}export function receiveMessageOnPort() { notImplemented("receiveMessageOnPort");}export { _MessageChannel as MessageChannel, _MessagePort as MessagePort, _Worker as Worker, parentPort, threadId, workerData,};
export default { markAsUntransferable, moveMessagePortToContext, receiveMessageOnPort, MessagePort: _MessagePort, MessageChannel: _MessageChannel, BroadcastChannel, Worker: _Worker, getEnvironmentData, setEnvironmentData, SHARE_ENV, threadId, workerData, resourceLimits, parentPort, isMainThread,};
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago