@hybrid-compute/worker 0.39.0 → 1.0.0
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 +21 -0
- package/README.md +2 -1
- package/dist/index.d.ts +23 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -12
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +15 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +61 -12
- package/src/types.ts +15 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.0](https://github.com/phun-ky/hybrid-compute/compare/%40hybrid-compute%2Fworker%400.40.0...%40hybrid-compute%2Fworker%401.0.0) (2026-10-20)
|
|
4
|
+
|
|
5
|
+
### Tasks
|
|
6
|
+
|
|
7
|
+
* 🤖 @hybrid-compute/core@1.0.0 ([14e36ca](https://github.com/phun-ky/hybrid-compute/commit/14e36ca7e565d0617b5d1c5cee07a4900396bfb8))
|
|
8
|
+
|
|
9
|
+
## [0.40.0](https://github.com/phun-ky/hybrid-compute/compare/%40hybrid-compute%2Fworker%400.39.0...%40hybrid-compute%2Fworker%400.40.0) (2026-10-20)
|
|
10
|
+
|
|
11
|
+
### Documentation
|
|
12
|
+
|
|
13
|
+
* ✏️ use createWorker in every worker example, and explain why bundlers need it ([b407e8d](https://github.com/phun-ky/hybrid-compute/commit/b407e8d22a32526c9dfe59842078a45cbe20dfa7)), references [#132](https://github.com/phun-ky/hybrid-compute/issues/132)
|
|
14
|
+
|
|
15
|
+
### Feature
|
|
16
|
+
|
|
17
|
+
* 🎸 add createWorker, so bundlers build the worker script ([7cd67f1](https://github.com/phun-ky/hybrid-compute/commit/7cd67f16c01867953cb8984069103cfa48cedd21)), closes [#132](https://github.com/phun-ky/hybrid-compute/issues/132)
|
|
18
|
+
|
|
19
|
+
### Adding missing tests
|
|
20
|
+
|
|
21
|
+
* 💍 make worker's timeout test robust under load, and never leave a thread running ([f612e52](https://github.com/phun-ky/hybrid-compute/commit/f612e52b0b295424af5e2072a32e6ad48fd64fc0)), references [#88](https://github.com/phun-ky/hybrid-compute/issues/88)
|
|
22
|
+
* 💍 property-based tests with fast-check ([5f74311](https://github.com/phun-ky/hybrid-compute/commit/5f743113eb7cc9969964cd828d8c0f6971ca45e3)), references [#88](https://github.com/phun-ky/hybrid-compute/issues/88)
|
|
23
|
+
|
|
3
24
|
## [0.39.0](https://github.com/phun-ky/hybrid-compute/compare/%40hybrid-compute%2Fworker%400.38.1...%40hybrid-compute%2Fworker%400.39.0) (2026-10-20)
|
|
4
25
|
|
|
5
26
|
### ⚠ BREAKING CHANGES
|
package/README.md
CHANGED
|
@@ -57,7 +57,8 @@ On the main thread:
|
|
|
57
57
|
import { createThreadedCompute } from '@hybrid-compute/worker';
|
|
58
58
|
|
|
59
59
|
const threaded = createThreadedCompute({
|
|
60
|
-
|
|
60
|
+
createWorker: () =>
|
|
61
|
+
new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),
|
|
61
62
|
canRunTasks: ['add']
|
|
62
63
|
});
|
|
63
64
|
|
package/dist/index.d.ts
CHANGED
|
@@ -20,16 +20,29 @@ export * from './types.js';
|
|
|
20
20
|
* global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
|
|
21
21
|
* another backend.
|
|
22
22
|
*
|
|
23
|
-
* @example
|
|
23
|
+
* @example With a bundler
|
|
24
24
|
* ```ts
|
|
25
|
+
* // Vite and webpack build the worker script when they see this literal
|
|
26
|
+
* // new Worker(new URL(...)), so keep it written out in full
|
|
25
27
|
* const threaded = createThreadedCompute({
|
|
26
|
-
*
|
|
28
|
+
* createWorker: () =>
|
|
29
|
+
* new Worker(new URL('./add.worker.ts', import.meta.url), {
|
|
30
|
+
* type: 'module'
|
|
31
|
+
* }),
|
|
27
32
|
* canRunTasks: ['add']
|
|
28
33
|
* });
|
|
29
34
|
*
|
|
30
35
|
* const result = await threaded.runTask<number, number>('add', 5);
|
|
31
36
|
* ```
|
|
32
37
|
*
|
|
38
|
+
* @example Without a bundler
|
|
39
|
+
* ```ts
|
|
40
|
+
* const threaded = createThreadedCompute({
|
|
41
|
+
* url: '/workers/add.worker.js',
|
|
42
|
+
* canRunTasks: ['add']
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
33
46
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker
|
|
34
47
|
*/
|
|
35
48
|
export declare class ThreadedCompute implements ComputeBackendInterface {
|
|
@@ -41,8 +54,8 @@ export declare class ThreadedCompute implements ComputeBackendInterface {
|
|
|
41
54
|
* Creates the backend. It checks the options, and starts no worker until
|
|
42
55
|
* the first task.
|
|
43
56
|
*
|
|
44
|
-
* @param options - The worker script, the tasks
|
|
45
|
-
* optionally, a timeout.
|
|
57
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks
|
|
58
|
+
* it can run and, optionally, a timeout.
|
|
46
59
|
*
|
|
47
60
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
48
61
|
* @throws {RangeError} For a negative timeout.
|
|
@@ -96,8 +109,8 @@ export declare class ThreadedCompute implements ComputeBackendInterface {
|
|
|
96
109
|
/**
|
|
97
110
|
* Creates a ThreadedCompute backend.
|
|
98
111
|
*
|
|
99
|
-
* @param options - The worker script, the tasks it
|
|
100
|
-
* optionally, a timeout.
|
|
112
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks it
|
|
113
|
+
* can run and, optionally, a timeout.
|
|
101
114
|
* @returns A new `ThreadedCompute` instance.
|
|
102
115
|
*
|
|
103
116
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
@@ -106,7 +119,10 @@ export declare class ThreadedCompute implements ComputeBackendInterface {
|
|
|
106
119
|
* @example
|
|
107
120
|
* ```ts
|
|
108
121
|
* const backend = createThreadedCompute({
|
|
109
|
-
*
|
|
122
|
+
* createWorker: () =>
|
|
123
|
+
* new Worker(new URL('./resize.worker.ts', import.meta.url), {
|
|
124
|
+
* type: 'module'
|
|
125
|
+
* }),
|
|
110
126
|
* canRunTasks: ['resizeImage']
|
|
111
127
|
* });
|
|
112
128
|
* ```
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EACV,+BAA+B,EAGhC,MAAM,YAAY,CAAC;AAGpB,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EACV,+BAA+B,EAGhC,MAAM,YAAY,CAAC;AAGpB,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,YAAY,CAAC;AAqF3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,qBAAa,eAAgB,YAAW,uBAAuB;IAC7D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;IACzD,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;IAC9D,OAAO,CAAC,MAAM,CAAK;IAEnB;;;;;;;;;OASG;gBACS,OAAO,EAAE,+BAA+B;IAIpD;;;;;;OAMG;IACH,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIjC;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EACzB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,KAAK,EACZ,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,MAAM,CAAC;IA2FlB;;;;OAIG;IACH,KAAK,IAAI,IAAI;IAQb,wDAAwD;IACxD,OAAO,CAAC,KAAK;IAuDb,8EAA8E;IAC9E,OAAO,CAAC,SAAS;IAkCjB,mDAAmD;IACnD,OAAO,CAAC,aAAa;CAWtB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,+BAA+B,mBAGzC"}
|
package/dist/index.js
CHANGED
|
@@ -16,10 +16,18 @@ const toSettings = (options) => {
|
|
|
16
16
|
if (!isPlainObject(options)) {
|
|
17
17
|
throw new TypeError(`createThreadedCompute expects an options object, got ${describeValue(options)}`);
|
|
18
18
|
}
|
|
19
|
-
const { url, canRunTasks, timeout } = options;
|
|
20
|
-
if (
|
|
19
|
+
const { url, createWorker, canRunTasks, timeout } = options;
|
|
20
|
+
if ((url === undefined) === (createWorker === undefined)) {
|
|
21
|
+
throw new TypeError('Give either url or createWorker: url for a worker script served as it is, createWorker when a bundler builds it');
|
|
22
|
+
}
|
|
23
|
+
if (url !== undefined &&
|
|
24
|
+
!(url instanceof URL) &&
|
|
25
|
+
(typeof url !== 'string' || url === '')) {
|
|
21
26
|
throw new TypeError(`url must be a URL or a non-empty string, got ${describeValue(url)}`);
|
|
22
27
|
}
|
|
28
|
+
if (createWorker !== undefined && typeof createWorker !== 'function') {
|
|
29
|
+
throw new TypeError(`createWorker must be a function that returns a new Worker, got ${describeValue(createWorker)}`);
|
|
30
|
+
}
|
|
23
31
|
if (!Array.isArray(canRunTasks) ||
|
|
24
32
|
!canRunTasks.every((name) => typeof name === 'string' && name !== '')) {
|
|
25
33
|
throw new TypeError(`canRunTasks must be an array of task names, got ${describeValue(canRunTasks)}`);
|
|
@@ -32,7 +40,11 @@ const toSettings = (options) => {
|
|
|
32
40
|
throw new RangeError(`timeout must be 0 (no timeout) or more, got ${timeout}`);
|
|
33
41
|
}
|
|
34
42
|
return {
|
|
35
|
-
|
|
43
|
+
// One way to start a worker, whichever option was given
|
|
44
|
+
createWorker: createWorker ??
|
|
45
|
+
(() => new Worker(url, { type: 'module' })),
|
|
46
|
+
// For error messages
|
|
47
|
+
source: createWorker === undefined ? `the worker ${String(url)}` : 'createWorker',
|
|
36
48
|
tasks: new Set(canRunTasks),
|
|
37
49
|
timeout: timeout ?? 0
|
|
38
50
|
};
|
|
@@ -54,16 +66,29 @@ const toSettings = (options) => {
|
|
|
54
66
|
* global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
|
|
55
67
|
* another backend.
|
|
56
68
|
*
|
|
57
|
-
* @example
|
|
69
|
+
* @example With a bundler
|
|
58
70
|
* ```ts
|
|
71
|
+
* // Vite and webpack build the worker script when they see this literal
|
|
72
|
+
* // new Worker(new URL(...)), so keep it written out in full
|
|
59
73
|
* const threaded = createThreadedCompute({
|
|
60
|
-
*
|
|
74
|
+
* createWorker: () =>
|
|
75
|
+
* new Worker(new URL('./add.worker.ts', import.meta.url), {
|
|
76
|
+
* type: 'module'
|
|
77
|
+
* }),
|
|
61
78
|
* canRunTasks: ['add']
|
|
62
79
|
* });
|
|
63
80
|
*
|
|
64
81
|
* const result = await threaded.runTask<number, number>('add', 5);
|
|
65
82
|
* ```
|
|
66
83
|
*
|
|
84
|
+
* @example Without a bundler
|
|
85
|
+
* ```ts
|
|
86
|
+
* const threaded = createThreadedCompute({
|
|
87
|
+
* url: '/workers/add.worker.js',
|
|
88
|
+
* canRunTasks: ['add']
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
67
92
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker
|
|
68
93
|
*/
|
|
69
94
|
export class ThreadedCompute {
|
|
@@ -75,8 +100,8 @@ export class ThreadedCompute {
|
|
|
75
100
|
* Creates the backend. It checks the options, and starts no worker until
|
|
76
101
|
* the first task.
|
|
77
102
|
*
|
|
78
|
-
* @param options - The worker script, the tasks
|
|
79
|
-
* optionally, a timeout.
|
|
103
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks
|
|
104
|
+
* it can run and, optionally, a timeout.
|
|
80
105
|
*
|
|
81
106
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
82
107
|
* @throws {RangeError} For a negative timeout.
|
|
@@ -198,10 +223,15 @@ export class ThreadedCompute {
|
|
|
198
223
|
return this.worker;
|
|
199
224
|
let worker;
|
|
200
225
|
try {
|
|
201
|
-
worker =
|
|
226
|
+
worker = this.settings.createWorker();
|
|
202
227
|
}
|
|
203
228
|
catch (error) {
|
|
204
|
-
throw new WorkerTaskError(taskName, `Worker task '${taskName}' couldn't start
|
|
229
|
+
throw new WorkerTaskError(taskName, `Worker task '${taskName}' couldn't start ${this.settings.source}`, { cause: error });
|
|
230
|
+
}
|
|
231
|
+
if (typeof worker?.postMessage !== 'function' ||
|
|
232
|
+
typeof worker.addEventListener !== 'function' ||
|
|
233
|
+
typeof worker.terminate !== 'function') {
|
|
234
|
+
throw new WorkerTaskError(taskName, `Worker task '${taskName}' couldn't start: createWorker returned ${describeValue(worker)}, not a Worker`);
|
|
205
235
|
}
|
|
206
236
|
worker.addEventListener('message', (event) => this.onMessage(event));
|
|
207
237
|
// An uncaught error, or a script that fails to load: the worker is in an
|
|
@@ -257,8 +287,8 @@ export class ThreadedCompute {
|
|
|
257
287
|
/**
|
|
258
288
|
* Creates a ThreadedCompute backend.
|
|
259
289
|
*
|
|
260
|
-
* @param options - The worker script, the tasks it
|
|
261
|
-
* optionally, a timeout.
|
|
290
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks it
|
|
291
|
+
* can run and, optionally, a timeout.
|
|
262
292
|
* @returns A new `ThreadedCompute` instance.
|
|
263
293
|
*
|
|
264
294
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
@@ -267,7 +297,10 @@ export class ThreadedCompute {
|
|
|
267
297
|
* @example
|
|
268
298
|
* ```ts
|
|
269
299
|
* const backend = createThreadedCompute({
|
|
270
|
-
*
|
|
300
|
+
* createWorker: () =>
|
|
301
|
+
* new Worker(new URL('./resize.worker.ts', import.meta.url), {
|
|
302
|
+
* type: 'module'
|
|
303
|
+
* }),
|
|
271
304
|
* canRunTasks: ['resizeImage']
|
|
272
305
|
* });
|
|
273
306
|
* ```
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAGf,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAM9C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAErE,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,YAAY,CAAC;AAS3B,4CAA4C;AAC5C,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,OAAO,MAAM,KAAK,UAAU,CAAC;AACtD;;;;;GAKG;AACH,MAAM,UAAU,GAAG,CAAC,OAAgB,EAAE,EAAE;IACtC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,SAAS,CACjB,wDAAwD,aAAa,CAAC,OAAO,CAAC,EAAE,CACjF,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAGf,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAM9C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAErE,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,Y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AC,EAAE,EAAE,EAC5D,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB,CACF,CAAC;QACJ,CAAC;aAAM,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;YAC7B,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CACT,IAAI,eAAe,CACjB,QAAQ,EACR,gBAAgB,QAAQ,kDAAkD,CAC3E,CACF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,mDAAmD;IAC3C,aAAa,CAAC,GAAW,EAAE,KAAe;QAChD,KAAK,MAAM,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;YAC9C,IAAI,CAAC,MAAM,CACT,IAAI,eAAe,CACjB,IAAI,CAAC,QAAQ,EACb,gBAAgB,IAAI,CAAC,QAAQ,mBAAmB,GAAG,EAAE,EACrD,EAAE,KAAK,EAAE,CACV,CACF,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAAwC;IAExC,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { TaskContextInterface } from '@hybrid-compute/core';
|
|
2
2
|
/**
|
|
3
|
-
* Options for {@link createThreadedCompute}.
|
|
3
|
+
* Options for {@link createThreadedCompute}. Give exactly one of `url` and
|
|
4
|
+
* `createWorker`.
|
|
4
5
|
*
|
|
5
|
-
* @property
|
|
6
|
-
*
|
|
6
|
+
* @property createWorker - A function that returns a new module `Worker`.
|
|
7
|
+
* Use it with a bundler: Vite and webpack build the worker script only when
|
|
8
|
+
* they see `new Worker(new URL('./x.worker.ts', import.meta.url), ...)`
|
|
9
|
+
* written out in full. It's called on the first task, and again to start a
|
|
10
|
+
* new worker after a crash or {@link ThreadedCompute.close}.
|
|
11
|
+
* @property url - The worker script, for a script served as it is, without a
|
|
12
|
+
* bundler. It's started as a module worker.
|
|
7
13
|
* @property canRunTasks - The tasks the worker script can run. Only these are
|
|
8
14
|
* sent to it.
|
|
9
15
|
* @property timeout - How long to wait for a reply, in milliseconds, before
|
|
@@ -13,14 +19,18 @@ import type { TaskContextInterface } from '@hybrid-compute/core';
|
|
|
13
19
|
* @example
|
|
14
20
|
* ```ts
|
|
15
21
|
* const options: ThreadedComputeOptionsInterface = {
|
|
16
|
-
*
|
|
22
|
+
* createWorker: () =>
|
|
23
|
+
* new Worker(new URL('./resize.worker.ts', import.meta.url), {
|
|
24
|
+
* type: 'module'
|
|
25
|
+
* }),
|
|
17
26
|
* canRunTasks: ['resizeImage'],
|
|
18
27
|
* timeout: 10_000
|
|
19
28
|
* };
|
|
20
29
|
* ```
|
|
21
30
|
*/
|
|
22
31
|
export interface ThreadedComputeOptionsInterface {
|
|
23
|
-
|
|
32
|
+
createWorker?: () => Worker;
|
|
33
|
+
url?: string | URL;
|
|
24
34
|
canRunTasks: readonly string[];
|
|
25
35
|
timeout?: number;
|
|
26
36
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEjE
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,WAAW,+BAA+B;IAC9C,YAAY,CAAC,EAAE,MAAM,MAAM,CAAC;IAC5B,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IACnB,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,0BAA0B,CAAC,KAAK,GAAG,OAAO;IACzD,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,4BAA4B;IAC3C,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,IAAI,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,4BAA4B,CAAC,MAAM,GAAG,OAAO;IAC5D,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,cAAc,CAAC,KAAK,GAAG,KAAK,EAAE,MAAM,GAAG,OAAO,IAAI,CAC5D,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAC,KACpC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAE9B;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC,gBAAgB,CACd,IAAI,EAAE,SAAS,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE;QAAE,IAAI,EAAE,OAAO,CAAA;KAAE,KAAK,IAAI,GAC3C,IAAI,CAAC;IACR,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CACrC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybrid-compute/worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Threaded compute backend for executing tasks in Web Workers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web-worker",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"release": "release-it",
|
|
47
47
|
"test": "tsx --test **/__tests__/**/*.spec.ts",
|
|
48
48
|
"pretest:ci": "rm -rf coverage && mkdir -p coverage",
|
|
49
|
-
"test:ci": "glob-bin -c \"node --import tsx --test --no-warnings --experimental-test-coverage --test-coverage-include='src/**/*.ts' --test-coverage-exclude='src/**/__tests__/**' --test-coverage-exclude='src/types.ts' --test-reporter=cobertura --test-reporter-destination=coverage/cobertura-coverage.xml --test-reporter=spec --test-reporter-destination=stdout\" \"**/__tests__/**/*.spec.ts\""
|
|
49
|
+
"test:ci": "glob-bin -c \"node --enable-source-maps --import tsx --test --no-warnings --experimental-test-coverage --test-coverage-lines=99 --test-coverage-functions=100 --test-coverage-branches=95 --test-coverage-include='src/**/*.ts' --test-coverage-exclude='src/**/__tests__/**' --test-coverage-exclude='src/types.ts' --test-reporter=cobertura --test-reporter-destination=coverage/cobertura-coverage.xml --test-reporter=spec --test-reporter-destination=stdout\" \"**/__tests__/**/*.spec.ts\""
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@hybrid-compute/core": "^0.
|
|
52
|
+
"@hybrid-compute/core": "^1.0.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"eslint": "^9.27.0",
|
package/src/index.ts
CHANGED
|
@@ -40,14 +40,30 @@ const toSettings = (options: unknown) => {
|
|
|
40
40
|
);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const { url, canRunTasks, timeout } = options;
|
|
43
|
+
const { url, createWorker, canRunTasks, timeout } = options;
|
|
44
44
|
|
|
45
|
-
if (
|
|
45
|
+
if ((url === undefined) === (createWorker === undefined)) {
|
|
46
|
+
throw new TypeError(
|
|
47
|
+
'Give either url or createWorker: url for a worker script served as it is, createWorker when a bundler builds it'
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (
|
|
52
|
+
url !== undefined &&
|
|
53
|
+
!(url instanceof URL) &&
|
|
54
|
+
(typeof url !== 'string' || url === '')
|
|
55
|
+
) {
|
|
46
56
|
throw new TypeError(
|
|
47
57
|
`url must be a URL or a non-empty string, got ${describeValue(url)}`
|
|
48
58
|
);
|
|
49
59
|
}
|
|
50
60
|
|
|
61
|
+
if (createWorker !== undefined && typeof createWorker !== 'function') {
|
|
62
|
+
throw new TypeError(
|
|
63
|
+
`createWorker must be a function that returns a new Worker, got ${describeValue(createWorker)}`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
51
67
|
if (
|
|
52
68
|
!Array.isArray(canRunTasks) ||
|
|
53
69
|
!canRunTasks.every((name) => typeof name === 'string' && name !== '')
|
|
@@ -73,7 +89,13 @@ const toSettings = (options: unknown) => {
|
|
|
73
89
|
}
|
|
74
90
|
|
|
75
91
|
return {
|
|
76
|
-
|
|
92
|
+
// One way to start a worker, whichever option was given
|
|
93
|
+
createWorker:
|
|
94
|
+
(createWorker as (() => Worker) | undefined) ??
|
|
95
|
+
(() => new Worker(url as string | URL, { type: 'module' })),
|
|
96
|
+
// For error messages
|
|
97
|
+
source:
|
|
98
|
+
createWorker === undefined ? `the worker ${String(url)}` : 'createWorker',
|
|
77
99
|
tasks: new Set(canRunTasks as string[]) as ReadonlySet<string>,
|
|
78
100
|
timeout: (timeout as number | undefined) ?? 0
|
|
79
101
|
};
|
|
@@ -96,16 +118,29 @@ const toSettings = (options: unknown) => {
|
|
|
96
118
|
* global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
|
|
97
119
|
* another backend.
|
|
98
120
|
*
|
|
99
|
-
* @example
|
|
121
|
+
* @example With a bundler
|
|
100
122
|
* ```ts
|
|
123
|
+
* // Vite and webpack build the worker script when they see this literal
|
|
124
|
+
* // new Worker(new URL(...)), so keep it written out in full
|
|
101
125
|
* const threaded = createThreadedCompute({
|
|
102
|
-
*
|
|
126
|
+
* createWorker: () =>
|
|
127
|
+
* new Worker(new URL('./add.worker.ts', import.meta.url), {
|
|
128
|
+
* type: 'module'
|
|
129
|
+
* }),
|
|
103
130
|
* canRunTasks: ['add']
|
|
104
131
|
* });
|
|
105
132
|
*
|
|
106
133
|
* const result = await threaded.runTask<number, number>('add', 5);
|
|
107
134
|
* ```
|
|
108
135
|
*
|
|
136
|
+
* @example Without a bundler
|
|
137
|
+
* ```ts
|
|
138
|
+
* const threaded = createThreadedCompute({
|
|
139
|
+
* url: '/workers/add.worker.js',
|
|
140
|
+
* canRunTasks: ['add']
|
|
141
|
+
* });
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
109
144
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Worker
|
|
110
145
|
*/
|
|
111
146
|
export class ThreadedCompute implements ComputeBackendInterface {
|
|
@@ -118,8 +153,8 @@ export class ThreadedCompute implements ComputeBackendInterface {
|
|
|
118
153
|
* Creates the backend. It checks the options, and starts no worker until
|
|
119
154
|
* the first task.
|
|
120
155
|
*
|
|
121
|
-
* @param options - The worker script, the tasks
|
|
122
|
-
* optionally, a timeout.
|
|
156
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks
|
|
157
|
+
* it can run and, optionally, a timeout.
|
|
123
158
|
*
|
|
124
159
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
125
160
|
* @throws {RangeError} For a negative timeout.
|
|
@@ -277,15 +312,26 @@ export class ThreadedCompute implements ComputeBackendInterface {
|
|
|
277
312
|
let worker: Worker;
|
|
278
313
|
|
|
279
314
|
try {
|
|
280
|
-
worker =
|
|
315
|
+
worker = this.settings.createWorker();
|
|
281
316
|
} catch (error) {
|
|
282
317
|
throw new WorkerTaskError(
|
|
283
318
|
taskName,
|
|
284
|
-
`Worker task '${taskName}' couldn't start
|
|
319
|
+
`Worker task '${taskName}' couldn't start ${this.settings.source}`,
|
|
285
320
|
{ cause: error }
|
|
286
321
|
);
|
|
287
322
|
}
|
|
288
323
|
|
|
324
|
+
if (
|
|
325
|
+
typeof worker?.postMessage !== 'function' ||
|
|
326
|
+
typeof worker.addEventListener !== 'function' ||
|
|
327
|
+
typeof worker.terminate !== 'function'
|
|
328
|
+
) {
|
|
329
|
+
throw new WorkerTaskError(
|
|
330
|
+
taskName,
|
|
331
|
+
`Worker task '${taskName}' couldn't start: createWorker returned ${describeValue(worker)}, not a Worker`
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
289
335
|
worker.addEventListener('message', (event) => this.onMessage(event));
|
|
290
336
|
// An uncaught error, or a script that fails to load: the worker is in an
|
|
291
337
|
// unknown state, so reject what it was doing and start over next time
|
|
@@ -367,8 +413,8 @@ export class ThreadedCompute implements ComputeBackendInterface {
|
|
|
367
413
|
/**
|
|
368
414
|
* Creates a ThreadedCompute backend.
|
|
369
415
|
*
|
|
370
|
-
* @param options - The worker script, the tasks it
|
|
371
|
-
* optionally, a timeout.
|
|
416
|
+
* @param options - The worker script (`url` or `createWorker`), the tasks it
|
|
417
|
+
* can run and, optionally, a timeout.
|
|
372
418
|
* @returns A new `ThreadedCompute` instance.
|
|
373
419
|
*
|
|
374
420
|
* @throws {TypeError} For a missing or wrongly typed option.
|
|
@@ -377,7 +423,10 @@ export class ThreadedCompute implements ComputeBackendInterface {
|
|
|
377
423
|
* @example
|
|
378
424
|
* ```ts
|
|
379
425
|
* const backend = createThreadedCompute({
|
|
380
|
-
*
|
|
426
|
+
* createWorker: () =>
|
|
427
|
+
* new Worker(new URL('./resize.worker.ts', import.meta.url), {
|
|
428
|
+
* type: 'module'
|
|
429
|
+
* }),
|
|
381
430
|
* canRunTasks: ['resizeImage']
|
|
382
431
|
* });
|
|
383
432
|
* ```
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { TaskContextInterface } from '@hybrid-compute/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Options for {@link createThreadedCompute}.
|
|
4
|
+
* Options for {@link createThreadedCompute}. Give exactly one of `url` and
|
|
5
|
+
* `createWorker`.
|
|
5
6
|
*
|
|
6
|
-
* @property
|
|
7
|
-
*
|
|
7
|
+
* @property createWorker - A function that returns a new module `Worker`.
|
|
8
|
+
* Use it with a bundler: Vite and webpack build the worker script only when
|
|
9
|
+
* they see `new Worker(new URL('./x.worker.ts', import.meta.url), ...)`
|
|
10
|
+
* written out in full. It's called on the first task, and again to start a
|
|
11
|
+
* new worker after a crash or {@link ThreadedCompute.close}.
|
|
12
|
+
* @property url - The worker script, for a script served as it is, without a
|
|
13
|
+
* bundler. It's started as a module worker.
|
|
8
14
|
* @property canRunTasks - The tasks the worker script can run. Only these are
|
|
9
15
|
* sent to it.
|
|
10
16
|
* @property timeout - How long to wait for a reply, in milliseconds, before
|
|
@@ -14,14 +20,18 @@ import type { TaskContextInterface } from '@hybrid-compute/core';
|
|
|
14
20
|
* @example
|
|
15
21
|
* ```ts
|
|
16
22
|
* const options: ThreadedComputeOptionsInterface = {
|
|
17
|
-
*
|
|
23
|
+
* createWorker: () =>
|
|
24
|
+
* new Worker(new URL('./resize.worker.ts', import.meta.url), {
|
|
25
|
+
* type: 'module'
|
|
26
|
+
* }),
|
|
18
27
|
* canRunTasks: ['resizeImage'],
|
|
19
28
|
* timeout: 10_000
|
|
20
29
|
* };
|
|
21
30
|
* ```
|
|
22
31
|
*/
|
|
23
32
|
export interface ThreadedComputeOptionsInterface {
|
|
24
|
-
|
|
33
|
+
createWorker?: () => Worker;
|
|
34
|
+
url?: string | URL;
|
|
25
35
|
canRunTasks: readonly string[];
|
|
26
36
|
timeout?: number;
|
|
27
37
|
}
|