@hybrid-compute/worker 0.40.0 → 1.1.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 CHANGED
@@ -1,5 +1,27 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.1.0](https://github.com/phun-ky/hybrid-compute/compare/%40hybrid-compute%2Fworker%401.0.0...%40hybrid-compute%2Fworker%401.1.0) (2026-11-20)
4
+
5
+ ### Tasks
6
+
7
+ * 🤖 lint the Markdown itself, on ESLint 10 ([123ba45](https://github.com/phun-ky/hybrid-compute/commit/123ba4501d8c51adb285ea3464715dd1f2427281)), closes [#141](https://github.com/phun-ky/hybrid-compute/issues/141)
8
+ * 🤖 move the worker runtime helpers into utils.ts ([740f8c9](https://github.com/phun-ky/hybrid-compute/commit/740f8c99ef1b8f013a6a21c080b91deb4cf749f7))
9
+
10
+ ### Documentation
11
+
12
+ * ✏️ link the WebAssembly guide from the READMEs ([3547673](https://github.com/phun-ky/hybrid-compute/commit/3547673911a7c79052c122fa2f07fb35beac1824)), closes [#135](https://github.com/phun-ky/hybrid-compute/issues/135)
13
+ * ✏️ point each package README's npm badges at its own package ([45c6641](https://github.com/phun-ky/hybrid-compute/commit/45c66417a9749f8839e8425ac54802a374fa7ac7)), closes [#159](https://github.com/phun-ky/hybrid-compute/issues/159)
14
+
15
+ ### Feature
16
+
17
+ * 🎸 run worker tasks in Node.js worker_threads, from @hybrid-compute/worker/node ([363780e](https://github.com/phun-ky/hybrid-compute/commit/363780e6f7dace936955b5a3f513278971a757f0)), closes [#127](https://github.com/phun-ky/hybrid-compute/issues/127)
18
+
19
+ ## [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)
20
+
21
+ ### Tasks
22
+
23
+ * 🤖 @hybrid-compute/core@1.0.0 ([14e36ca](https://github.com/phun-ky/hybrid-compute/commit/14e36ca7e565d0617b5d1c5cee07a4900396bfb8))
24
+
3
25
  ## [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)
4
26
 
5
27
  ### Documentation
package/README.md CHANGED
@@ -3,11 +3,11 @@
3
3
  [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
4
4
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg)](http://makeapullrequest.com)
5
5
  [![SemVer 2.0](https://img.shields.io/badge/SemVer-2.0-green.svg)](http://semver.org/spec/v2.0.0.html)
6
- ![npm version](https://img.shields.io/npm/v/@hybrid-compute/core)
6
+ ![npm version](https://img.shields.io/npm/v/@hybrid-compute/worker)
7
7
  ![issues](https://img.shields.io/github/issues/phun-ky/hybrid-compute)
8
- ![license](https://img.shields.io/npm/l/@hybrid-compute/core)
9
- ![size](https://img.shields.io/bundlephobia/min/@hybrid-compute/core)
10
- ![npm](https://img.shields.io/npm/dm/%40hybrid-compute/core)
8
+ ![license](https://img.shields.io/npm/l/@hybrid-compute/worker)
9
+ ![size](https://img.shields.io/bundlephobia/min/@hybrid-compute/worker)
10
+ ![npm](https://img.shields.io/npm/dm/%40hybrid-compute/worker)
11
11
  ![GitHub Repo stars](https://img.shields.io/github/stars/phun-ky/hybrid-compute)
12
12
  [![codecov](https://codecov.io/gh/phun-ky/hybrid-compute/graph/badge.svg?token=VA91DL7ZLZ)](https://codecov.io/gh/phun-ky/hybrid-compute)
13
13
  [![build](https://github.com/phun-ky/hybrid-compute/actions/workflows/check.yml/badge.svg)](https://github.com/phun-ky/hybrid-compute/actions/workflows/check.yml)
@@ -69,6 +69,13 @@ The worker starts on the first task. Options, errors, cancelling and the message
69
69
  format are in the
70
70
  [worker guide](https://phun-ky.net/hybrid-compute/guide/usage/worker).
71
71
 
72
+ In Node.js, import `createThreadedCompute` from `@hybrid-compute/worker/node`
73
+ instead: it runs the same worker script in a `worker_threads` thread. See
74
+ [In Node.js](https://phun-ky.net/hybrid-compute/guide/usage/worker#in-node-js).
75
+
76
+ To run a WebAssembly module in a worker, see the
77
+ [WebAssembly guide](https://phun-ky.net/hybrid-compute/guide/usage/wasm).
78
+
72
79
  ---
73
80
 
74
81
  ## Contributing
package/dist/expose.d.ts CHANGED
@@ -10,7 +10,8 @@ import type { WorkerScopeInterface, WorkerTaskType } from './types.js';
10
10
  *
11
11
  * @param tasks - The tasks, by name. List the same names in `canRunTasks`.
12
12
  * @param scope - Where to listen and reply. Defaults to the worker's global
13
- * scope.
13
+ * scope, or in a Node.js worker thread to its `parentPort` (on Node.js 22.3
14
+ * and later; on earlier versions, pass `parentPort` yourself).
14
15
  *
15
16
  * @throws {TypeError} If `tasks` isn't an object of functions, or if it's
16
17
  * called outside a worker without a `scope`.
@@ -1 +1 @@
1
- {"version":3,"file":"expose.d.ts","sourceRoot":"","sources":["../src/expose.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA6BvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,EAC/C,KAAK,CAAC,EAAE,oBAAoB,GAC3B,IAAI,CA6DN"}
1
+ {"version":3,"file":"expose.d.ts","sourceRoot":"","sources":["../src/expose.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAiDvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,EAC/C,KAAK,CAAC,EAAE,oBAAoB,GAC3B,IAAI,CA6DN"}
package/dist/expose.js CHANGED
@@ -7,15 +7,30 @@ const toErrorValue = (error) => {
7
7
  return error;
8
8
  return { message: String(error) };
9
9
  };
10
- /** The worker's global scope, or a TypeError outside a worker. */
10
+ /**
11
+ * In a Node.js worker thread, its parentPort. getBuiltinModule rather than an
12
+ * import, so a bundler building for the browser doesn't see node:worker_threads.
13
+ */
14
+ const nodeParentPort = () => {
15
+ const { process } = globalThis;
16
+ const threads = process?.getBuiltinModule?.('node:worker_threads');
17
+ return threads?.parentPort ?? undefined;
18
+ };
19
+ /**
20
+ * The worker's global scope, or a Node.js thread's parentPort, or a TypeError
21
+ * outside a worker.
22
+ */
11
23
  const workerScope = () => {
12
24
  const scope = globalThis;
13
- if (!('WorkerGlobalScope' in globalThis) ||
14
- typeof scope.addEventListener !== 'function' ||
15
- typeof scope.postMessage !== 'function') {
16
- throw new TypeError('exposeTasks must be called in a worker script, or be given a scope to listen on');
25
+ if ('WorkerGlobalScope' in globalThis &&
26
+ typeof scope.addEventListener === 'function' &&
27
+ typeof scope.postMessage === 'function') {
28
+ return scope;
17
29
  }
18
- return scope;
30
+ const port = nodeParentPort();
31
+ if (port)
32
+ return port;
33
+ throw new TypeError('exposeTasks must be called in a worker script, or be given a scope to listen on');
19
34
  };
20
35
  /**
21
36
  * Answers tasks from {@link ThreadedCompute} in a worker script: it listens
@@ -28,7 +43,8 @@ const workerScope = () => {
28
43
  *
29
44
  * @param tasks - The tasks, by name. List the same names in `canRunTasks`.
30
45
  * @param scope - Where to listen and reply. Defaults to the worker's global
31
- * scope.
46
+ * scope, or in a Node.js worker thread to its `parentPort` (on Node.js 22.3
47
+ * and later; on earlier versions, pass `parentPort` yourself).
32
48
  *
33
49
  * @throws {TypeError} If `tasks` isn't an object of functions, or if it's
34
50
  * called outside a worker without a `scope`.
@@ -1 +1 @@
1
- {"version":3,"file":"expose.js","sourceRoot":"","sources":["../src/expose.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,C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OAAO;QAEhE,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YACzB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;YAE9B,OAAO;QACT,CAAC;QAED,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO;QAE1C,KAAK,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACL,CAAC"}
package/dist/index.d.ts CHANGED
@@ -18,7 +18,8 @@ export * from './types.js';
18
18
  *
19
19
  * Web Workers only exist in browsers (and Deno and Bun): where there is no
20
20
  * global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
21
- * another backend.
21
+ * another backend. For Node.js's `worker_threads`, import
22
+ * `createThreadedCompute` from `@hybrid-compute/worker/node` instead.
22
23
  *
23
24
  * @example With a bundler
24
25
  * ```ts
@@ -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;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;IAIp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,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
@@ -1,11 +1,9 @@
1
1
  import { NoBackendError } from '@hybrid-compute/core';
2
2
  import { WorkerTaskError } from './errors.js';
3
- import { describeValue, isPlainObject, messageOf } from './utils.js';
3
+ import { describeValue, isPlainObject, messageOf, RUNTIME, WEB_RUNTIME } from './utils.js';
4
4
  export * from './errors.js';
5
5
  export * from './expose.js';
6
6
  export * from './types.js';
7
- /** Whether this runtime has Web Workers. */
8
- const hasWorkers = () => typeof Worker === 'function';
9
7
  /**
10
8
  * Check the options and return them normalised.
11
9
  *
@@ -39,10 +37,14 @@ const toSettings = (options) => {
39
37
  if (typeof timeout === 'number' && timeout < 0) {
40
38
  throw new RangeError(`timeout must be 0 (no timeout) or more, got ${timeout}`);
41
39
  }
40
+ // Web Workers, unless an entry point such as ./node.ts passed another
41
+ const runtime = options[RUNTIME] ?? WEB_RUNTIME;
42
42
  return {
43
+ available: runtime.available,
43
44
  // One way to start a worker, whichever option was given
44
- createWorker: createWorker ??
45
- (() => new Worker(url, { type: 'module' })),
45
+ createWorker: createWorker === undefined
46
+ ? () => runtime.fromUrl(url)
47
+ : () => runtime.wrap(createWorker()),
46
48
  // For error messages
47
49
  source: createWorker === undefined ? `the worker ${String(url)}` : 'createWorker',
48
50
  tasks: new Set(canRunTasks),
@@ -64,7 +66,8 @@ const toSettings = (options) => {
64
66
  *
65
67
  * Web Workers only exist in browsers (and Deno and Bun): where there is no
66
68
  * global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
67
- * another backend.
69
+ * another backend. For Node.js's `worker_threads`, import
70
+ * `createThreadedCompute` from `@hybrid-compute/worker/node` instead.
68
71
  *
69
72
  * @example With a bundler
70
73
  * ```ts
@@ -117,7 +120,7 @@ export class ThreadedCompute {
117
120
  * Workers.
118
121
  */
119
122
  canRun(taskName) {
120
- return hasWorkers() && this.settings.tasks.has(taskName);
123
+ return this.settings.available() && this.settings.tasks.has(taskName);
121
124
  }
122
125
  /**
123
126
  * Runs a task in the worker and waits for its reply.
@@ -146,8 +149,8 @@ export class ThreadedCompute {
146
149
  if (!this.settings.tasks.has(taskName)) {
147
150
  throw new NoBackendError(taskName, 'worker', `The worker can't run task '${taskName}'; it isn't in canRunTasks`);
148
151
  }
149
- if (!hasWorkers()) {
150
- throw new TypeError('Web Workers are not available in this runtime; @hybrid-compute/worker needs a global Worker');
152
+ if (!this.settings.available()) {
153
+ throw new TypeError("Web Workers are not available in this runtime; @hybrid-compute/worker needs a global Worker. In Node.js, use '@hybrid-compute/worker/node'");
151
154
  }
152
155
  const signal = options?.signal;
153
156
  if (signal?.aborted)
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,E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C7B,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/node.d.ts ADDED
@@ -0,0 +1,92 @@
1
+ import { Worker as Thread } from 'node:worker_threads';
2
+ import { ThreadedCompute as WebThreadedCompute, type ThreadedComputeOptionsInterface } from './index.js';
3
+ export * from './errors.js';
4
+ export * from './expose.js';
5
+ export * from './types.js';
6
+ /**
7
+ * Options for {@link createThreadedCompute} in Node.js. Give exactly one of
8
+ * `url` and `createWorker`.
9
+ *
10
+ * @property createWorker - A function that returns a new `Worker` from
11
+ * `node:worker_threads`, for options such as `execArgv` or `resourceLimits`.
12
+ * It's called on the first task, and again to start a new worker after a
13
+ * crash or {@link ThreadedCompute.close}.
14
+ * @property url - The worker script: a `file:` URL, an absolute path, or a
15
+ * path starting with `./` relative to the working directory.
16
+ * @property canRunTasks - The tasks the worker script can run. Only these are
17
+ * sent to it.
18
+ * @property timeout - How long to wait for a reply, in milliseconds, before
19
+ * rejecting with a `WorkerTaskError`. Defaults to `0`: no timeout.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const options: NodeThreadedComputeOptionsInterface = {
24
+ * url: new URL('./resize.worker.js', import.meta.url),
25
+ * canRunTasks: ['resizeImage']
26
+ * };
27
+ * ```
28
+ */
29
+ export interface NodeThreadedComputeOptionsInterface extends Omit<ThreadedComputeOptionsInterface, 'createWorker'> {
30
+ createWorker?: () => Thread;
31
+ }
32
+ /**
33
+ * ThreadedCompute for Node.js: the backend from `@hybrid-compute/worker`,
34
+ * running tasks in a `worker_threads` thread instead of a Web Worker. The
35
+ * worker script is the same: {@link exposeTasks} finds `parentPort` itself.
36
+ *
37
+ * The thread keeps the process alive only while a task is waiting for its
38
+ * reply, so a script can end without calling {@link ThreadedCompute.close}.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * import { createThreadedCompute } from '@hybrid-compute/worker/node';
43
+ *
44
+ * const threaded = createThreadedCompute({
45
+ * url: new URL('./add.worker.js', import.meta.url),
46
+ * canRunTasks: ['add']
47
+ * });
48
+ *
49
+ * await threaded.runTask('add', [2, 3]); // 5
50
+ * ```
51
+ *
52
+ * @see https://nodejs.org/api/worker_threads.html
53
+ */
54
+ export declare class ThreadedCompute extends WebThreadedCompute {
55
+ /**
56
+ * Creates the backend. It checks the options, and starts no thread until
57
+ * the first task.
58
+ *
59
+ * @param options - The worker script (`url` or `createWorker`), the tasks
60
+ * it can run and, optionally, a timeout.
61
+ *
62
+ * @throws {TypeError} For a missing or wrongly typed option.
63
+ * @throws {RangeError} For a negative timeout.
64
+ */
65
+ constructor(options: NodeThreadedComputeOptionsInterface);
66
+ }
67
+ /**
68
+ * Creates a ThreadedCompute backend for Node.js, running tasks in a
69
+ * `worker_threads` thread.
70
+ *
71
+ * @param options - The worker script (`url` or `createWorker`), the tasks it
72
+ * can run and, optionally, a timeout.
73
+ * @returns A new `ThreadedCompute` instance.
74
+ *
75
+ * @throws {TypeError} For a missing or wrongly typed option.
76
+ * @throws {RangeError} For a negative timeout.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * import { Worker } from 'node:worker_threads';
81
+ *
82
+ * const backend = createThreadedCompute({
83
+ * createWorker: () =>
84
+ * new Worker(new URL('./resize.worker.js', import.meta.url), {
85
+ * resourceLimits: { maxOldGenerationSizeMb: 256 }
86
+ * }),
87
+ * canRunTasks: ['resizeImage']
88
+ * });
89
+ * ```
90
+ */
91
+ export declare function createThreadedCompute(options: NodeThreadedComputeOptionsInterface): ThreadedCompute;
92
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAQvD,OAAO,EACL,eAAe,IAAI,kBAAkB,EACrC,KAAK,+BAA+B,EACrC,MAAM,YAAY,CAAC;AAEpB,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,YAAY,CAAC;AAE3B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,mCAAoC,SAAQ,IAAI,CAC/D,+BAA+B,EAC/B,cAAc,CACf;IACC,YAAY,CAAC,EAAE,MAAM,MAAM,CAAC;CAC7B;AA+ED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,eAAgB,SAAQ,kBAAkB;IACrD;;;;;;;;;OASG;gBACS,OAAO,EAAE,mCAAmC;CAOzD;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,mCAAmC,mBAG7C"}
package/dist/node.js ADDED
@@ -0,0 +1,139 @@
1
+ // The Node.js entry point, @hybrid-compute/worker/node: the same API, with
2
+ // workers started by node:worker_threads instead of the global Worker.
3
+ /// <reference types="node" />
4
+ import { Worker as Thread } from 'node:worker_threads';
5
+ import { isPlainObject, RUNTIME } from './utils.js';
6
+ import { ThreadedCompute as WebThreadedCompute } from './index.js';
7
+ export * from './errors.js';
8
+ export * from './expose.js';
9
+ export * from './types.js';
10
+ /**
11
+ * A worker_threads Worker as a Web Worker: the events ThreadedCompute listens
12
+ * for, and a thread that only keeps the process alive while a task is
13
+ * waiting for its reply.
14
+ */
15
+ class ThreadWorker extends EventTarget {
16
+ thread;
17
+ /** Ids of the tasks waiting for a reply. */
18
+ waiting = new Set();
19
+ constructor(thread) {
20
+ super();
21
+ this.thread = thread;
22
+ thread.unref();
23
+ thread.on('message', (data) => {
24
+ if (isPlainObject(data) && typeof data.id === 'number') {
25
+ this.settle(data.id);
26
+ }
27
+ this.dispatchEvent(new MessageEvent('message', { data }));
28
+ });
29
+ thread.on('error', (error) => this.dispatchEvent(Object.assign(new Event('error'), { message: error.message, error })));
30
+ thread.on('messageerror', (error) => {
31
+ // ThreadedCompute rejects every waiting task on a messageerror
32
+ this.waiting.clear();
33
+ this.thread.unref();
34
+ this.dispatchEvent(Object.assign(new Event('messageerror'), { error }));
35
+ });
36
+ // A worker script that calls process.exit() would otherwise leave its
37
+ // tasks waiting forever. After an error or terminate(), ThreadedCompute
38
+ // has let go of this worker and ignores the event.
39
+ thread.on('exit', (code) => this.dispatchEvent(Object.assign(new Event('error'), {
40
+ message: `it exited with code ${code}`
41
+ })));
42
+ }
43
+ postMessage(message) {
44
+ this.thread.postMessage(message);
45
+ if (!isPlainObject(message) || typeof message.id !== 'number')
46
+ return;
47
+ if (message.cancel === true) {
48
+ this.settle(message.id);
49
+ }
50
+ else {
51
+ this.waiting.add(message.id);
52
+ this.thread.ref();
53
+ }
54
+ }
55
+ terminate() {
56
+ void this.thread.terminate();
57
+ }
58
+ /** A task got its reply or was cancelled: let the process exit when idle. */
59
+ settle(id) {
60
+ this.waiting.delete(id);
61
+ if (this.waiting.size === 0)
62
+ this.thread.unref();
63
+ }
64
+ }
65
+ const NODE_RUNTIME = {
66
+ available: () => true,
67
+ fromUrl: (url) => new ThreadWorker(new Thread(url)),
68
+ // Anything else is left for ThreadedCompute to reject as not a Worker
69
+ wrap: (worker) => (worker instanceof Thread
70
+ ? new ThreadWorker(worker)
71
+ : worker)
72
+ };
73
+ /**
74
+ * ThreadedCompute for Node.js: the backend from `@hybrid-compute/worker`,
75
+ * running tasks in a `worker_threads` thread instead of a Web Worker. The
76
+ * worker script is the same: {@link exposeTasks} finds `parentPort` itself.
77
+ *
78
+ * The thread keeps the process alive only while a task is waiting for its
79
+ * reply, so a script can end without calling {@link ThreadedCompute.close}.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * import { createThreadedCompute } from '@hybrid-compute/worker/node';
84
+ *
85
+ * const threaded = createThreadedCompute({
86
+ * url: new URL('./add.worker.js', import.meta.url),
87
+ * canRunTasks: ['add']
88
+ * });
89
+ *
90
+ * await threaded.runTask('add', [2, 3]); // 5
91
+ * ```
92
+ *
93
+ * @see https://nodejs.org/api/worker_threads.html
94
+ */
95
+ export class ThreadedCompute extends WebThreadedCompute {
96
+ /**
97
+ * Creates the backend. It checks the options, and starts no thread until
98
+ * the first task.
99
+ *
100
+ * @param options - The worker script (`url` or `createWorker`), the tasks
101
+ * it can run and, optionally, a timeout.
102
+ *
103
+ * @throws {TypeError} For a missing or wrongly typed option.
104
+ * @throws {RangeError} For a negative timeout.
105
+ */
106
+ constructor(options) {
107
+ super((isPlainObject(options)
108
+ ? { ...options, [RUNTIME]: NODE_RUNTIME }
109
+ : options));
110
+ }
111
+ }
112
+ /**
113
+ * Creates a ThreadedCompute backend for Node.js, running tasks in a
114
+ * `worker_threads` thread.
115
+ *
116
+ * @param options - The worker script (`url` or `createWorker`), the tasks it
117
+ * can run and, optionally, a timeout.
118
+ * @returns A new `ThreadedCompute` instance.
119
+ *
120
+ * @throws {TypeError} For a missing or wrongly typed option.
121
+ * @throws {RangeError} For a negative timeout.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * import { Worker } from 'node:worker_threads';
126
+ *
127
+ * const backend = createThreadedCompute({
128
+ * createWorker: () =>
129
+ * new Worker(new URL('./resize.worker.js', import.meta.url), {
130
+ * resourceLimits: { maxOldGenerationSizeMb: 256 }
131
+ * }),
132
+ * canRunTasks: ['resizeImage']
133
+ * });
134
+ * ```
135
+ */
136
+ export function createThreadedCompute(options) {
137
+ return new ThreadedCompute(options);
138
+ }
139
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,uEAAuE;AACvE,8BAA8B;AAC9B,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,EACL,aAAa,EACb,OAAO,EAER,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,eAAe,IAAI,kBAAkB,EAEtC,MAAM,YAAY,CAAC;AAEpB,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAE5B,cAAc,YAAY,CAAC;AAgC3B;;;;GAIG;AACH,MAAM,YAAa,SAAQ,WAAW;IAIP;IAH7B,4CAA4C;IAC3B,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAE7C,YAA6B,MAAc;QACzC,KAAK,EAAE,CAAC;QADmB,WAAM,GAAN,MAAM,CAAQ;QAEzC,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,IAAa,EAAE,EAAE;YACrC,IAAI,aAAa,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;gBACvD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACvB,CAAC;YAED,IAAI,CAAC,aAAa,CAAC,IAAI,YAAY,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAC5D,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAY,EAAE,EAAE,CAClC,IAA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IAED,SAAS;QACP,KAAK,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IAC/B,CAAC;IAED,6EAA6E;IACrE,MAAM,CAAC,EAAU;QACvB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAExB,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC;YAAE,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACnD,CAAC;CACF;AAED,MAAM,YAAY,GAA2B;IAC3C,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI;IACrB,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAsB;IACxE,sEAAsE;IACtE,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE,CACf,CAAC,MAAM,YAAY,MAAM;QACvB,CAAC,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC;QAC1B,CAAC,CAAC,MAAM,CAAsB;CACnC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,eAAgB,SAAQ,kBAAkB;IACrD;;;;;;;;;OASG;IACH,YAAY,OAA4C;QACtD,KAAK,CACH,CAAC,aAAa,CAAC,OAAO,CAAC;YACrB,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE;YACzC,CAAC,CAAC,OAAO,CAAoC,CAChD,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAA4C;IAE5C,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC"}
package/dist/utils.d.ts CHANGED
@@ -3,4 +3,17 @@ export declare const describeValue: (value: unknown) => "string" | "number" | "b
3
3
  export declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
4
4
  /** The message for an `error` value: a string, or an object's message. */
5
5
  export declare const messageOf: (error: unknown) => string;
6
+ /** Starts workers, and says whether this runtime can. */
7
+ export interface WorkerRuntimeInterface {
8
+ /** Whether workers can be started here. */
9
+ available(): boolean;
10
+ /** Start a worker for a script URL. */
11
+ fromUrl(url: string | URL): Worker;
12
+ /** Turn what createWorker returned into a Worker, if this runtime has to. */
13
+ wrap(worker: unknown): Worker;
14
+ }
15
+ /** The key the runtime is passed under, in the options object. */
16
+ export declare const RUNTIME: unique symbol;
17
+ /** Web Workers: browsers, Deno and Bun. */
18
+ export declare const WEB_RUNTIME: WorkerRuntimeInterface;
6
19
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,0IAQ3C,CAAC;AAEF,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,KACb,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CACoC,CAAC;AAEvE,0EAA0E;AAC1E,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,WAYvC,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,0IAQ3C,CAAC;AAEF,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,KACb,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CACoC,CAAC;AAEvE,0EAA0E;AAC1E,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,WAYvC,CAAC;AAKF,yDAAyD;AACzD,MAAM,WAAW,sBAAsB;IACrC,2CAA2C;IAC3C,SAAS,IAAI,OAAO,CAAC;IACrB,uCAAuC;IACvC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC;IACnC,6EAA6E;IAC7E,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC;CAC/B;AAED,kEAAkE;AAClE,eAAO,MAAM,OAAO,eAA0C,CAAC;AAE/D,2CAA2C;AAC3C,eAAO,MAAM,WAAW,EAAE,sBAIzB,CAAC"}
package/dist/utils.js CHANGED
@@ -23,4 +23,12 @@ export const messageOf = (error) => {
23
23
  return String(error);
24
24
  }
25
25
  };
26
+ /** The key the runtime is passed under, in the options object. */
27
+ export const RUNTIME = Symbol('hybrid-compute worker runtime');
28
+ /** Web Workers: browsers, Deno and Bun. */
29
+ export const WEB_RUNTIME = {
30
+ available: () => typeof Worker === 'function',
31
+ fromUrl: (url) => new Worker(url, { type: 'module' }),
32
+ wrap: (worker) => worker
33
+ };
26
34
  //# sourceMappingURL=utils.js.map
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAE,EAAE;IAC9C,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IAElC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,UAAU,CAAC;IAE5C,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,iBAAiB,CAAC;IAE3C,OAAO,OAAO,KAAK,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,KAAc,EACoB,EAAE,CACpC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAEvE,0EAA0E;AAC1E,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAc,EAAE,EAAE;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAE5C,IAAI,aAAa,CAA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AAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;AACH,CAAC,CAAC;AAeF,kEAAkE;AAClE,MAAM,CAAC,MAAM,OAAO,GAAG,MAAM,CAAC,+BAA+B,CAAC,CAAC;AAE/D,2CAA2C;AAC3C,MAAM,CAAC,MAAM,WAAW,GAA2B;IACjD,SAAS,EAAE,GAAG,EAAE,CAAC,OAAO,MAAM,KAAK,UAAU;IAC7C,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IACrD,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAgB;CACnC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hybrid-compute/worker",
3
- "version": "0.40.0",
4
- "description": "Threaded compute backend for executing tasks in Web Workers.",
3
+ "version": "1.1.0",
4
+ "description": "Threaded compute backend for executing tasks in Web Workers, or in Node.js worker_threads.",
5
5
  "keywords": [
6
6
  "web-worker",
7
7
  "worker",
@@ -30,6 +30,10 @@
30
30
  "types": "./dist/index.d.ts",
31
31
  "default": "./dist/index.js"
32
32
  },
33
+ "./node": {
34
+ "types": "./dist/node.d.ts",
35
+ "default": "./dist/node.js"
36
+ },
33
37
  "./package.json": "./package.json"
34
38
  },
35
39
  "types": "./dist/index.d.ts",
@@ -49,12 +53,9 @@
49
53
  "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
54
  },
51
55
  "dependencies": {
52
- "@hybrid-compute/core": "^0.40.0"
56
+ "@hybrid-compute/core": "^1.0.0"
53
57
  },
54
58
  "devDependencies": {
55
- "eslint": "^9.27.0",
56
- "eslint-config-phun-ky": "^1.0.3",
57
- "prettier": "^3.8.3",
58
59
  "typescript": "^6.0.3"
59
60
  },
60
61
  "engines": {
package/src/expose.ts CHANGED
@@ -10,21 +10,41 @@ const toErrorValue = (error: unknown) => {
10
10
 
11
11
  return { message: String(error) };
12
12
  };
13
- /** The worker's global scope, or a TypeError outside a worker. */
13
+ /**
14
+ * In a Node.js worker thread, its parentPort. getBuiltinModule rather than an
15
+ * import, so a bundler building for the browser doesn't see node:worker_threads.
16
+ */
17
+ const nodeParentPort = () => {
18
+ const { process } = globalThis as {
19
+ process?: { getBuiltinModule?: (id: string) => unknown };
20
+ };
21
+ const threads = process?.getBuiltinModule?.('node:worker_threads') as
22
+ { parentPort?: WorkerScopeInterface | null } | undefined;
23
+
24
+ return threads?.parentPort ?? undefined;
25
+ };
26
+ /**
27
+ * The worker's global scope, or a Node.js thread's parentPort, or a TypeError
28
+ * outside a worker.
29
+ */
14
30
  const workerScope = (): WorkerScopeInterface => {
15
31
  const scope = globalThis as unknown as Partial<WorkerScopeInterface>;
16
32
 
17
33
  if (
18
- !('WorkerGlobalScope' in globalThis) ||
19
- typeof scope.addEventListener !== 'function' ||
20
- typeof scope.postMessage !== 'function'
34
+ 'WorkerGlobalScope' in globalThis &&
35
+ typeof scope.addEventListener === 'function' &&
36
+ typeof scope.postMessage === 'function'
21
37
  ) {
22
- throw new TypeError(
23
- 'exposeTasks must be called in a worker script, or be given a scope to listen on'
24
- );
38
+ return scope as WorkerScopeInterface;
25
39
  }
26
40
 
27
- return scope as WorkerScopeInterface;
41
+ const port = nodeParentPort();
42
+
43
+ if (port) return port;
44
+
45
+ throw new TypeError(
46
+ 'exposeTasks must be called in a worker script, or be given a scope to listen on'
47
+ );
28
48
  };
29
49
 
30
50
  /**
@@ -38,7 +58,8 @@ const workerScope = (): WorkerScopeInterface => {
38
58
  *
39
59
  * @param tasks - The tasks, by name. List the same names in `canRunTasks`.
40
60
  * @param scope - Where to listen and reply. Defaults to the worker's global
41
- * scope.
61
+ * scope, or in a Node.js worker thread to its `parentPort` (on Node.js 22.3
62
+ * and later; on earlier versions, pass `parentPort` yourself).
42
63
  *
43
64
  * @throws {TypeError} If `tasks` isn't an object of functions, or if it's
44
65
  * called outside a worker without a `scope`.
package/src/index.ts CHANGED
@@ -10,7 +10,14 @@ import type {
10
10
  WorkerCancelMessageInterface,
11
11
  WorkerTaskMessageInterface
12
12
  } from './types.js';
13
- import { describeValue, isPlainObject, messageOf } from './utils.js';
13
+ import {
14
+ describeValue,
15
+ isPlainObject,
16
+ messageOf,
17
+ RUNTIME,
18
+ WEB_RUNTIME,
19
+ type WorkerRuntimeInterface
20
+ } from './utils.js';
14
21
 
15
22
  export * from './errors.js';
16
23
 
@@ -25,8 +32,6 @@ type PendingTaskType = {
25
32
  reject: (reason: unknown) => void;
26
33
  };
27
34
 
28
- /** Whether this runtime has Web Workers. */
29
- const hasWorkers = () => typeof Worker === 'function';
30
35
  /**
31
36
  * Check the options and return them normalised.
32
37
  *
@@ -88,11 +93,17 @@ const toSettings = (options: unknown) => {
88
93
  );
89
94
  }
90
95
 
96
+ // Web Workers, unless an entry point such as ./node.ts passed another
97
+ const runtime =
98
+ (options as { [RUNTIME]?: WorkerRuntimeInterface })[RUNTIME] ?? WEB_RUNTIME;
99
+
91
100
  return {
101
+ available: runtime.available,
92
102
  // One way to start a worker, whichever option was given
93
103
  createWorker:
94
- (createWorker as (() => Worker) | undefined) ??
95
- (() => new Worker(url as string | URL, { type: 'module' })),
104
+ createWorker === undefined
105
+ ? () => runtime.fromUrl(url as string | URL)
106
+ : () => runtime.wrap((createWorker as () => unknown)()),
96
107
  // For error messages
97
108
  source:
98
109
  createWorker === undefined ? `the worker ${String(url)}` : 'createWorker',
@@ -116,7 +127,8 @@ const toSettings = (options: unknown) => {
116
127
  *
117
128
  * Web Workers only exist in browsers (and Deno and Bun): where there is no
118
129
  * global `Worker`, `canRun` returns `false`, so the `auto` strategy uses
119
- * another backend.
130
+ * another backend. For Node.js's `worker_threads`, import
131
+ * `createThreadedCompute` from `@hybrid-compute/worker/node` instead.
120
132
  *
121
133
  * @example With a bundler
122
134
  * ```ts
@@ -171,7 +183,7 @@ export class ThreadedCompute implements ComputeBackendInterface {
171
183
  * Workers.
172
184
  */
173
185
  canRun(taskName: string): boolean {
174
- return hasWorkers() && this.settings.tasks.has(taskName);
186
+ return this.settings.available() && this.settings.tasks.has(taskName);
175
187
  }
176
188
 
177
189
  /**
@@ -210,9 +222,9 @@ export class ThreadedCompute implements ComputeBackendInterface {
210
222
  );
211
223
  }
212
224
 
213
- if (!hasWorkers()) {
225
+ if (!this.settings.available()) {
214
226
  throw new TypeError(
215
- 'Web Workers are not available in this runtime; @hybrid-compute/worker needs a global Worker'
227
+ "Web Workers are not available in this runtime; @hybrid-compute/worker needs a global Worker. In Node.js, use '@hybrid-compute/worker/node'"
216
228
  );
217
229
  }
218
230
 
package/src/node.ts ADDED
@@ -0,0 +1,200 @@
1
+ // The Node.js entry point, @hybrid-compute/worker/node: the same API, with
2
+ // workers started by node:worker_threads instead of the global Worker.
3
+ /// <reference types="node" />
4
+ import { Worker as Thread } from 'node:worker_threads';
5
+
6
+ import {
7
+ isPlainObject,
8
+ RUNTIME,
9
+ type WorkerRuntimeInterface
10
+ } from './utils.js';
11
+
12
+ import {
13
+ ThreadedCompute as WebThreadedCompute,
14
+ type ThreadedComputeOptionsInterface
15
+ } from './index.js';
16
+
17
+ export * from './errors.js';
18
+
19
+ export * from './expose.js';
20
+
21
+ export * from './types.js';
22
+
23
+ /**
24
+ * Options for {@link createThreadedCompute} in Node.js. Give exactly one of
25
+ * `url` and `createWorker`.
26
+ *
27
+ * @property createWorker - A function that returns a new `Worker` from
28
+ * `node:worker_threads`, for options such as `execArgv` or `resourceLimits`.
29
+ * It's called on the first task, and again to start a new worker after a
30
+ * crash or {@link ThreadedCompute.close}.
31
+ * @property url - The worker script: a `file:` URL, an absolute path, or a
32
+ * path starting with `./` relative to the working directory.
33
+ * @property canRunTasks - The tasks the worker script can run. Only these are
34
+ * sent to it.
35
+ * @property timeout - How long to wait for a reply, in milliseconds, before
36
+ * rejecting with a `WorkerTaskError`. Defaults to `0`: no timeout.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const options: NodeThreadedComputeOptionsInterface = {
41
+ * url: new URL('./resize.worker.js', import.meta.url),
42
+ * canRunTasks: ['resizeImage']
43
+ * };
44
+ * ```
45
+ */
46
+ export interface NodeThreadedComputeOptionsInterface extends Omit<
47
+ ThreadedComputeOptionsInterface,
48
+ 'createWorker'
49
+ > {
50
+ createWorker?: () => Thread;
51
+ }
52
+
53
+ /**
54
+ * A worker_threads Worker as a Web Worker: the events ThreadedCompute listens
55
+ * for, and a thread that only keeps the process alive while a task is
56
+ * waiting for its reply.
57
+ */
58
+ class ThreadWorker extends EventTarget {
59
+ /** Ids of the tasks waiting for a reply. */
60
+ private readonly waiting = new Set<number>();
61
+
62
+ constructor(private readonly thread: Thread) {
63
+ super();
64
+ thread.unref();
65
+ thread.on('message', (data: unknown) => {
66
+ if (isPlainObject(data) && typeof data.id === 'number') {
67
+ this.settle(data.id);
68
+ }
69
+
70
+ this.dispatchEvent(new MessageEvent('message', { data }));
71
+ });
72
+ thread.on('error', (error: Error) =>
73
+ this.dispatchEvent(
74
+ Object.assign(new Event('error'), { message: error.message, error })
75
+ )
76
+ );
77
+ thread.on('messageerror', (error: Error) => {
78
+ // ThreadedCompute rejects every waiting task on a messageerror
79
+ this.waiting.clear();
80
+ this.thread.unref();
81
+ this.dispatchEvent(Object.assign(new Event('messageerror'), { error }));
82
+ });
83
+ // A worker script that calls process.exit() would otherwise leave its
84
+ // tasks waiting forever. After an error or terminate(), ThreadedCompute
85
+ // has let go of this worker and ignores the event.
86
+ thread.on('exit', (code: number) =>
87
+ this.dispatchEvent(
88
+ Object.assign(new Event('error'), {
89
+ message: `it exited with code ${code}`
90
+ })
91
+ )
92
+ );
93
+ }
94
+
95
+ postMessage(message: unknown) {
96
+ this.thread.postMessage(message);
97
+
98
+ if (!isPlainObject(message) || typeof message.id !== 'number') return;
99
+
100
+ if (message.cancel === true) {
101
+ this.settle(message.id);
102
+ } else {
103
+ this.waiting.add(message.id);
104
+ this.thread.ref();
105
+ }
106
+ }
107
+
108
+ terminate() {
109
+ void this.thread.terminate();
110
+ }
111
+
112
+ /** A task got its reply or was cancelled: let the process exit when idle. */
113
+ private settle(id: number) {
114
+ this.waiting.delete(id);
115
+
116
+ if (this.waiting.size === 0) this.thread.unref();
117
+ }
118
+ }
119
+
120
+ const NODE_RUNTIME: WorkerRuntimeInterface = {
121
+ available: () => true,
122
+ fromUrl: (url) => new ThreadWorker(new Thread(url)) as unknown as Worker,
123
+ // Anything else is left for ThreadedCompute to reject as not a Worker
124
+ wrap: (worker) =>
125
+ (worker instanceof Thread
126
+ ? new ThreadWorker(worker)
127
+ : worker) as unknown as Worker
128
+ };
129
+
130
+ /**
131
+ * ThreadedCompute for Node.js: the backend from `@hybrid-compute/worker`,
132
+ * running tasks in a `worker_threads` thread instead of a Web Worker. The
133
+ * worker script is the same: {@link exposeTasks} finds `parentPort` itself.
134
+ *
135
+ * The thread keeps the process alive only while a task is waiting for its
136
+ * reply, so a script can end without calling {@link ThreadedCompute.close}.
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * import { createThreadedCompute } from '@hybrid-compute/worker/node';
141
+ *
142
+ * const threaded = createThreadedCompute({
143
+ * url: new URL('./add.worker.js', import.meta.url),
144
+ * canRunTasks: ['add']
145
+ * });
146
+ *
147
+ * await threaded.runTask('add', [2, 3]); // 5
148
+ * ```
149
+ *
150
+ * @see https://nodejs.org/api/worker_threads.html
151
+ */
152
+ export class ThreadedCompute extends WebThreadedCompute {
153
+ /**
154
+ * Creates the backend. It checks the options, and starts no thread until
155
+ * the first task.
156
+ *
157
+ * @param options - The worker script (`url` or `createWorker`), the tasks
158
+ * it can run and, optionally, a timeout.
159
+ *
160
+ * @throws {TypeError} For a missing or wrongly typed option.
161
+ * @throws {RangeError} For a negative timeout.
162
+ */
163
+ constructor(options: NodeThreadedComputeOptionsInterface) {
164
+ super(
165
+ (isPlainObject(options)
166
+ ? { ...options, [RUNTIME]: NODE_RUNTIME }
167
+ : options) as ThreadedComputeOptionsInterface
168
+ );
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Creates a ThreadedCompute backend for Node.js, running tasks in a
174
+ * `worker_threads` thread.
175
+ *
176
+ * @param options - The worker script (`url` or `createWorker`), the tasks it
177
+ * can run and, optionally, a timeout.
178
+ * @returns A new `ThreadedCompute` instance.
179
+ *
180
+ * @throws {TypeError} For a missing or wrongly typed option.
181
+ * @throws {RangeError} For a negative timeout.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * import { Worker } from 'node:worker_threads';
186
+ *
187
+ * const backend = createThreadedCompute({
188
+ * createWorker: () =>
189
+ * new Worker(new URL('./resize.worker.js', import.meta.url), {
190
+ * resourceLimits: { maxOldGenerationSizeMb: 256 }
191
+ * }),
192
+ * canRunTasks: ['resizeImage']
193
+ * });
194
+ * ```
195
+ */
196
+ export function createThreadedCompute(
197
+ options: NodeThreadedComputeOptionsInterface
198
+ ) {
199
+ return new ThreadedCompute(options);
200
+ }
package/src/utils.ts CHANGED
@@ -28,3 +28,26 @@ export const messageOf = (error: unknown) => {
28
28
  return String(error);
29
29
  }
30
30
  };
31
+
32
+ // How ThreadedCompute starts workers in a runtime. The package's entry points
33
+ // pick one; none of this is part of the public API.
34
+
35
+ /** Starts workers, and says whether this runtime can. */
36
+ export interface WorkerRuntimeInterface {
37
+ /** Whether workers can be started here. */
38
+ available(): boolean;
39
+ /** Start a worker for a script URL. */
40
+ fromUrl(url: string | URL): Worker;
41
+ /** Turn what createWorker returned into a Worker, if this runtime has to. */
42
+ wrap(worker: unknown): Worker;
43
+ }
44
+
45
+ /** The key the runtime is passed under, in the options object. */
46
+ export const RUNTIME = Symbol('hybrid-compute worker runtime');
47
+
48
+ /** Web Workers: browsers, Deno and Bun. */
49
+ export const WEB_RUNTIME: WorkerRuntimeInterface = {
50
+ available: () => typeof Worker === 'function',
51
+ fromUrl: (url) => new Worker(url, { type: 'module' }),
52
+ wrap: (worker) => worker as Worker
53
+ };