@hybrid-compute/worker 0.38.0 → 0.38.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/expose.d.ts +39 -0
- package/dist/expose.d.ts.map +1 -0
- package/dist/expose.js +111 -0
- package/dist/expose.js.map +1 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +276 -0
- package/dist/index.js.map +1 -0
- package/{src/types.ts → dist/types.d.ts} +17 -26
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +26 -0
- package/dist/utils.js.map +1 -0
- package/package.json +5 -1
- package/.release-it.json +0 -83
- package/eslint.config.mjs +0 -3
- package/src/__tests__/expose.spec.ts +0 -175
- package/src/__tests__/fixtures/exposed.ts +0 -30
- package/src/__tests__/fixtures/raw.ts +0 -21
- package/src/__tests__/thread-worker.ts +0 -69
- package/src/__tests__/worker.spec.ts +0 -449
- package/src/errors.ts +0 -49
- package/src/expose.ts +0 -130
- package/src/index.ts +0 -389
- package/src/utils.ts +0 -30
- package/tsconfig.build.json +0 -13
- package/tsconfig.json +0 -13
package/src/index.ts
DELETED
|
@@ -1,389 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
NoBackendError,
|
|
3
|
-
type ComputeBackendInterface,
|
|
4
|
-
type RunTaskOptionsInterface
|
|
5
|
-
} from '@hybrid-compute/core';
|
|
6
|
-
|
|
7
|
-
import { WorkerTaskError } from './errors.js';
|
|
8
|
-
import type {
|
|
9
|
-
ThreadedComputeOptionsInterface,
|
|
10
|
-
WorkerCancelMessageInterface,
|
|
11
|
-
WorkerTaskMessageInterface
|
|
12
|
-
} from './types.js';
|
|
13
|
-
import { describeValue, isPlainObject, messageOf } from './utils.js';
|
|
14
|
-
|
|
15
|
-
export * from './errors.js';
|
|
16
|
-
|
|
17
|
-
export * from './expose.js';
|
|
18
|
-
|
|
19
|
-
export * from './types.js';
|
|
20
|
-
|
|
21
|
-
/** A task waiting for its reply from the worker. */
|
|
22
|
-
type PendingTaskType = {
|
|
23
|
-
taskName: string;
|
|
24
|
-
resolve: (value: unknown) => void;
|
|
25
|
-
reject: (reason: unknown) => void;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/** Whether this runtime has Web Workers. */
|
|
29
|
-
const hasWorkers = () => typeof Worker === 'function';
|
|
30
|
-
/**
|
|
31
|
-
* Check the options and return them normalised.
|
|
32
|
-
*
|
|
33
|
-
* @throws {TypeError} For a missing or wrongly typed option.
|
|
34
|
-
* @throws {RangeError} For a negative timeout.
|
|
35
|
-
*/
|
|
36
|
-
const toSettings = (options: unknown) => {
|
|
37
|
-
if (!isPlainObject(options)) {
|
|
38
|
-
throw new TypeError(
|
|
39
|
-
`createThreadedCompute expects an options object, got ${describeValue(options)}`
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const { url, canRunTasks, timeout } = options;
|
|
44
|
-
|
|
45
|
-
if (!(url instanceof URL) && (typeof url !== 'string' || url === '')) {
|
|
46
|
-
throw new TypeError(
|
|
47
|
-
`url must be a URL or a non-empty string, got ${describeValue(url)}`
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
!Array.isArray(canRunTasks) ||
|
|
53
|
-
!canRunTasks.every((name) => typeof name === 'string' && name !== '')
|
|
54
|
-
) {
|
|
55
|
-
throw new TypeError(
|
|
56
|
-
`canRunTasks must be an array of task names, got ${describeValue(canRunTasks)}`
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
timeout !== undefined &&
|
|
62
|
-
(typeof timeout !== 'number' || !Number.isFinite(timeout))
|
|
63
|
-
) {
|
|
64
|
-
throw new TypeError(
|
|
65
|
-
`timeout must be a number of milliseconds, got ${describeValue(timeout)}`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (typeof timeout === 'number' && timeout < 0) {
|
|
70
|
-
throw new RangeError(
|
|
71
|
-
`timeout must be 0 (no timeout) or more, got ${timeout}`
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
url: url as string | URL,
|
|
77
|
-
tasks: new Set(canRunTasks as string[]) as ReadonlySet<string>,
|
|
78
|
-
timeout: (timeout as number | undefined) ?? 0
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* ThreadedCompute is a backend that runs tasks in a dedicated module Web
|
|
84
|
-
* Worker, so heavy work doesn't block the main thread.
|
|
85
|
-
*
|
|
86
|
-
* It starts the worker on the first task, not when it's created, sends each
|
|
87
|
-
* task with an id and matches the replies by id. Write the worker script with
|
|
88
|
-
* {@link exposeTasks}, or by hand following {@link WorkerTaskMessageInterface}
|
|
89
|
-
* and {@link WorkerResultMessageInterface}.
|
|
90
|
-
*
|
|
91
|
-
* Every failure (an `error` in the reply, a crash, the timeout) rejects with a
|
|
92
|
-
* {@link WorkerTaskError}. If the worker crashes, a new one starts on the next
|
|
93
|
-
* task. Call {@link ThreadedCompute.close} when you're done with it.
|
|
94
|
-
*
|
|
95
|
-
* Web Workers only exist in browsers (and Deno and Bun): where there is no
|
|
96
|
-
* global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
|
|
97
|
-
* another backend.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```ts
|
|
101
|
-
* const threaded = createThreadedCompute({
|
|
102
|
-
* url: new URL('./worker.js', import.meta.url),
|
|
103
|
-
* canRunTasks: ['add']
|
|
104
|
-
* });
|
|
105
|
-
*
|
|
106
|
-
* const result = await threaded.runTask<number, number>('add', 5);
|
|
107
|
-
* ```
|
|
108
|
-
*
|
|
109
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker
|
|
110
|
-
*/
|
|
111
|
-
export class ThreadedCompute implements ComputeBackendInterface {
|
|
112
|
-
private readonly settings: ReturnType<typeof toSettings>;
|
|
113
|
-
private worker?: Worker;
|
|
114
|
-
private readonly pending = new Map<number, PendingTaskType>();
|
|
115
|
-
private nextId = 1;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Creates the backend. It checks the options, and starts no worker until
|
|
119
|
-
* the first task.
|
|
120
|
-
*
|
|
121
|
-
* @param options - The worker script, the tasks it can run and,
|
|
122
|
-
* optionally, a timeout.
|
|
123
|
-
*
|
|
124
|
-
* @throws {TypeError} For a missing or wrongly typed option.
|
|
125
|
-
* @throws {RangeError} For a negative timeout.
|
|
126
|
-
*/
|
|
127
|
-
constructor(options: ThreadedComputeOptionsInterface) {
|
|
128
|
-
this.settings = toSettings(options);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Determines if this backend can run the given task.
|
|
133
|
-
*
|
|
134
|
-
* @param taskName - Name of the task.
|
|
135
|
-
* @returns `true` if the task is in `canRunTasks` and this runtime has Web
|
|
136
|
-
* Workers.
|
|
137
|
-
*/
|
|
138
|
-
canRun(taskName: string): boolean {
|
|
139
|
-
return hasWorkers() && this.settings.tasks.has(taskName);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Runs a task in the worker and waits for its reply.
|
|
144
|
-
*
|
|
145
|
-
* @typeParam Input - The input type of the task.
|
|
146
|
-
* @typeParam Output - The output type of the task.
|
|
147
|
-
*
|
|
148
|
-
* @param taskName - The name of the task to run.
|
|
149
|
-
* @param input - The input for the task. It's copied with the structured
|
|
150
|
-
* clone algorithm, so it can't hold functions or DOM nodes.
|
|
151
|
-
* @param options - Options for this call. With a `signal`, `runTask`
|
|
152
|
-
* rejects with the signal's reason as soon as it aborts, and the worker is
|
|
153
|
-
* told to cancel the task.
|
|
154
|
-
* @returns A Promise resolving to the reply's `result`. It rejects with a
|
|
155
|
-
* `NoBackendError` (from `@hybrid-compute/core`) if the task isn't in
|
|
156
|
-
* `canRunTasks`, a `TypeError` if this runtime has no Web Workers, a
|
|
157
|
-
* {@link WorkerTaskError} if the task fails, and with the signal's reason
|
|
158
|
-
* if the call is cancelled.
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```ts
|
|
162
|
-
* const result = await threaded.runTask('square', 9); // 81
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
async runTask<Input, Output>(
|
|
166
|
-
taskName: string,
|
|
167
|
-
input: Input,
|
|
168
|
-
options?: RunTaskOptionsInterface
|
|
169
|
-
): Promise<Output> {
|
|
170
|
-
if (!this.settings.tasks.has(taskName)) {
|
|
171
|
-
throw new NoBackendError(
|
|
172
|
-
taskName,
|
|
173
|
-
'worker',
|
|
174
|
-
`The worker can't run task '${taskName}'; it isn't in canRunTasks`
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (!hasWorkers()) {
|
|
179
|
-
throw new TypeError(
|
|
180
|
-
'Web Workers are not available in this runtime; @hybrid-compute/worker needs a global Worker'
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const signal = options?.signal;
|
|
185
|
-
|
|
186
|
-
if (signal?.aborted) throw signal.reason;
|
|
187
|
-
|
|
188
|
-
const worker = this.start(taskName);
|
|
189
|
-
const { timeout } = this.settings;
|
|
190
|
-
const id = this.nextId++;
|
|
191
|
-
|
|
192
|
-
return new Promise<Output>((resolve, reject) => {
|
|
193
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
194
|
-
|
|
195
|
-
const finish = () => {
|
|
196
|
-
clearTimeout(timer);
|
|
197
|
-
signal?.removeEventListener('abort', onAbort);
|
|
198
|
-
this.pending.delete(id);
|
|
199
|
-
};
|
|
200
|
-
// Settled before the worker replied: tell the worker to stop
|
|
201
|
-
const cancel = () => {
|
|
202
|
-
const message: WorkerCancelMessageInterface = { id, cancel: true };
|
|
203
|
-
|
|
204
|
-
worker.postMessage(message);
|
|
205
|
-
};
|
|
206
|
-
const onAbort = () => {
|
|
207
|
-
finish();
|
|
208
|
-
cancel();
|
|
209
|
-
reject(signal?.reason);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
if (timeout > 0) {
|
|
213
|
-
timer = setTimeout(() => {
|
|
214
|
-
finish();
|
|
215
|
-
cancel();
|
|
216
|
-
reject(
|
|
217
|
-
new WorkerTaskError(
|
|
218
|
-
taskName,
|
|
219
|
-
`Worker task '${taskName}' timed out after ${timeout} ms`
|
|
220
|
-
)
|
|
221
|
-
);
|
|
222
|
-
}, timeout);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
226
|
-
this.pending.set(id, {
|
|
227
|
-
taskName,
|
|
228
|
-
resolve: (value) => {
|
|
229
|
-
finish();
|
|
230
|
-
resolve(value as Output);
|
|
231
|
-
},
|
|
232
|
-
reject: (reason) => {
|
|
233
|
-
finish();
|
|
234
|
-
reject(reason);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
const message: WorkerTaskMessageInterface<Input> = {
|
|
239
|
-
id,
|
|
240
|
-
task: taskName,
|
|
241
|
-
input
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
try {
|
|
245
|
-
worker.postMessage(message);
|
|
246
|
-
} catch (error) {
|
|
247
|
-
this.pending
|
|
248
|
-
.get(id)
|
|
249
|
-
?.reject(
|
|
250
|
-
new WorkerTaskError(
|
|
251
|
-
taskName,
|
|
252
|
-
`Worker task '${taskName}' couldn't send its input to the worker`,
|
|
253
|
-
{ cause: error }
|
|
254
|
-
)
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Terminates the worker, if one is running. Tasks still waiting for a
|
|
262
|
-
* reply reject with a {@link WorkerTaskError}. The next task starts a new
|
|
263
|
-
* worker.
|
|
264
|
-
*/
|
|
265
|
-
close(): void {
|
|
266
|
-
const { worker } = this;
|
|
267
|
-
|
|
268
|
-
this.worker = undefined;
|
|
269
|
-
worker?.terminate();
|
|
270
|
-
this.rejectPending('the worker was closed');
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/** The running worker, starting one first if needed. */
|
|
274
|
-
private start(taskName: string): Worker {
|
|
275
|
-
if (this.worker) return this.worker;
|
|
276
|
-
|
|
277
|
-
let worker: Worker;
|
|
278
|
-
|
|
279
|
-
try {
|
|
280
|
-
worker = new Worker(this.settings.url, { type: 'module' });
|
|
281
|
-
} catch (error) {
|
|
282
|
-
throw new WorkerTaskError(
|
|
283
|
-
taskName,
|
|
284
|
-
`Worker task '${taskName}' couldn't start the worker ${String(this.settings.url)}`,
|
|
285
|
-
{ cause: error }
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
worker.addEventListener('message', (event) => this.onMessage(event));
|
|
290
|
-
// An uncaught error, or a script that fails to load: the worker is in an
|
|
291
|
-
// unknown state, so reject what it was doing and start over next time
|
|
292
|
-
worker.addEventListener('error', (event) => {
|
|
293
|
-
if (this.worker !== worker) return;
|
|
294
|
-
|
|
295
|
-
const detail = (event as Partial<ErrorEvent>).message;
|
|
296
|
-
|
|
297
|
-
this.worker = undefined;
|
|
298
|
-
worker.terminate();
|
|
299
|
-
this.rejectPending(
|
|
300
|
-
detail
|
|
301
|
-
? `the worker threw: ${detail}`
|
|
302
|
-
: 'the worker failed: its script did not load, or threw while starting',
|
|
303
|
-
event
|
|
304
|
-
);
|
|
305
|
-
});
|
|
306
|
-
// A reply that couldn't be deserialised: nothing tells which task it
|
|
307
|
-
// was for, so reject them all instead of letting one wait forever
|
|
308
|
-
worker.addEventListener('messageerror', (event) => {
|
|
309
|
-
if (this.worker !== worker) return;
|
|
310
|
-
|
|
311
|
-
this.rejectPending('a reply from the worker could not be read', event);
|
|
312
|
-
});
|
|
313
|
-
this.worker = worker;
|
|
314
|
-
|
|
315
|
-
return worker;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/** Match a reply to its task by id; ignore anything that can't be matched. */
|
|
319
|
-
private onMessage(event: MessageEvent) {
|
|
320
|
-
const reply: unknown = event.data;
|
|
321
|
-
|
|
322
|
-
if (!isPlainObject(reply) || typeof reply.id !== 'number') return;
|
|
323
|
-
|
|
324
|
-
const task = this.pending.get(reply.id);
|
|
325
|
-
|
|
326
|
-
if (!task) return;
|
|
327
|
-
|
|
328
|
-
const { taskName } = task;
|
|
329
|
-
const { error } = reply;
|
|
330
|
-
|
|
331
|
-
if (error !== undefined && error !== null) {
|
|
332
|
-
const text = messageOf(error);
|
|
333
|
-
|
|
334
|
-
task.reject(
|
|
335
|
-
new WorkerTaskError(
|
|
336
|
-
taskName,
|
|
337
|
-
`Worker task '${taskName}' failed${text ? `: ${text}` : ''}`,
|
|
338
|
-
{ detail: error }
|
|
339
|
-
)
|
|
340
|
-
);
|
|
341
|
-
} else if ('result' in reply) {
|
|
342
|
-
task.resolve(reply.result);
|
|
343
|
-
} else {
|
|
344
|
-
task.reject(
|
|
345
|
-
new WorkerTaskError(
|
|
346
|
-
taskName,
|
|
347
|
-
`Worker task '${taskName}' got a reply with neither a result nor an error`
|
|
348
|
-
)
|
|
349
|
-
);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/** Reject every task still waiting for a reply. */
|
|
354
|
-
private rejectPending(why: string, cause?: unknown) {
|
|
355
|
-
for (const task of [...this.pending.values()]) {
|
|
356
|
-
task.reject(
|
|
357
|
-
new WorkerTaskError(
|
|
358
|
-
task.taskName,
|
|
359
|
-
`Worker task '${task.taskName}' got no reply: ${why}`,
|
|
360
|
-
{ cause }
|
|
361
|
-
)
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Creates a ThreadedCompute backend.
|
|
369
|
-
*
|
|
370
|
-
* @param options - The worker script, the tasks it can run and,
|
|
371
|
-
* optionally, a timeout.
|
|
372
|
-
* @returns A new `ThreadedCompute` instance.
|
|
373
|
-
*
|
|
374
|
-
* @throws {TypeError} For a missing or wrongly typed option.
|
|
375
|
-
* @throws {RangeError} For a negative timeout.
|
|
376
|
-
*
|
|
377
|
-
* @example
|
|
378
|
-
* ```ts
|
|
379
|
-
* const backend = createThreadedCompute({
|
|
380
|
-
* url: new URL('./worker.js', import.meta.url),
|
|
381
|
-
* canRunTasks: ['resizeImage']
|
|
382
|
-
* });
|
|
383
|
-
* ```
|
|
384
|
-
*/
|
|
385
|
-
export function createThreadedCompute(
|
|
386
|
-
options: ThreadedComputeOptionsInterface
|
|
387
|
-
) {
|
|
388
|
-
return new ThreadedCompute(options);
|
|
389
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/** What a value is, for error messages: null, an array, or its typeof. */
|
|
2
|
-
export const describeValue = (value: unknown) => {
|
|
3
|
-
if (value === null) return 'null';
|
|
4
|
-
|
|
5
|
-
if (Array.isArray(value)) return 'an array';
|
|
6
|
-
|
|
7
|
-
if (value === '') return 'an empty string';
|
|
8
|
-
|
|
9
|
-
return typeof value;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const isPlainObject = (
|
|
13
|
-
value: unknown
|
|
14
|
-
): value is Record<string, unknown> =>
|
|
15
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
16
|
-
|
|
17
|
-
/** The message for an `error` value: a string, or an object's message. */
|
|
18
|
-
export const messageOf = (error: unknown) => {
|
|
19
|
-
if (typeof error === 'string') return error;
|
|
20
|
-
|
|
21
|
-
if (isPlainObject(error) && typeof error.message === 'string') {
|
|
22
|
-
return error.message;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
return JSON.stringify(error);
|
|
27
|
-
} catch {
|
|
28
|
-
return String(error);
|
|
29
|
-
}
|
|
30
|
-
};
|
package/tsconfig.build.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "dist",
|
|
5
|
-
"rootDir": "src",
|
|
6
|
-
"composite": true,
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["src"],
|
|
11
|
-
"exclude": ["src/**/__tests__/**"],
|
|
12
|
-
"references": [{ "path": "../core/tsconfig.build.json" }]
|
|
13
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.build.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"composite": false,
|
|
5
|
-
"noEmit": true,
|
|
6
|
-
"declaration": false,
|
|
7
|
-
"declarationMap": false,
|
|
8
|
-
"types": ["node"]
|
|
9
|
-
},
|
|
10
|
-
"include": ["src"],
|
|
11
|
-
"exclude": [],
|
|
12
|
-
"references": [{ "path": "../core/tsconfig.build.json" }]
|
|
13
|
-
}
|