@orkestrel/worker 0.0.10 → 0.0.12
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/README.md +18 -19
- package/dist/src/core/index.cjs +38 -34
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +94 -71
- package/dist/src/core/index.d.ts +94 -71
- package/dist/src/core/index.js +38 -34
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/server/index.cjs +243 -221
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +146 -96
- package/dist/src/server/index.d.ts +146 -96
- package/dist/src/server/index.js +242 -220
- package/dist/src/server/index.js.map +1 -1
- package/package.json +17 -19
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
import { ContractShape } from '@orkestrel/contract';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import type { ContractShape } from '@orkestrel/contract';
|
|
2
|
+
import type { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
3
|
+
import type { EmitterHooks } from '@orkestrel/emitter';
|
|
4
|
+
import type { Guard } from '@orkestrel/contract';
|
|
5
|
+
import type { Infer } from '@orkestrel/contract';
|
|
6
|
+
import type { QueueContext } from '@orkestrel/queue';
|
|
7
|
+
import type { QueueStoreInterface } from '@orkestrel/queue';
|
|
8
|
+
import type { Worker } from 'node:worker_threads';
|
|
9
|
+
import type { WorkerEventMap } from '@orkestrel/worker';
|
|
10
|
+
import type { WorkerInterface } from '@orkestrel/worker';
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* Creates a persistent JSON-file {@link QueueStoreInterface} — the core
|
|
11
14
|
* `createDatabaseQueueStore` over a server {@link createJSONDriver}.
|
|
12
15
|
*
|
|
13
16
|
* @remarks
|
|
14
|
-
* A queue's durable state is
|
|
17
|
+
* A queue's durable state is a database table, so JSON persistence reuses the
|
|
15
18
|
* existing JSON-file driver rather than a bespoke store: the entries are written to
|
|
16
19
|
* (and reloaded from) the file at `path`, surviving a process restart. There is no new
|
|
17
20
|
* class — the store engine ({@link createDatabaseQueueStore}) is shared, and only the
|
|
18
21
|
* driver changes where the bytes live. The `input` shape must be JSON-serializable
|
|
19
|
-
* (the JSON driver round-trips it as JSON). Build a second store over the
|
|
22
|
+
* (the JSON driver round-trips it as JSON). Build a second store over the same `path` to
|
|
20
23
|
* resume the outstanding entries a prior store persisted.
|
|
21
24
|
*
|
|
22
25
|
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
@@ -39,28 +42,29 @@ import { WorkerInterface } from '@orkestrel/worker';
|
|
|
39
42
|
export declare function createJSONQueueStore<TInput extends ContractShape>(path: string, input: TInput): QueueStoreInterface<Infer<TInput>>;
|
|
40
43
|
|
|
41
44
|
/**
|
|
42
|
-
*
|
|
43
|
-
* core `createWorker` whose pooled resource is a worker
|
|
45
|
+
* Creates a CPU-parallel worker over `node:worker_threads` — a thin specialization of the
|
|
46
|
+
* core `createWorker` whose pooled resource is a worker thread.
|
|
44
47
|
*
|
|
45
48
|
* @remarks
|
|
46
49
|
* Composition, not reimplementation: all concurrency, retries, per-attempt timeout,
|
|
47
50
|
* lifecycle, and durability are the core `Worker`'s (a `Queue` ⨉ `Pool`). This factory
|
|
48
|
-
* supplies only the thread pairing — the pool `create`s a thread (
|
|
49
|
-
* `destroy`s it with `terminate()`, and `validate`s it by
|
|
50
|
-
* evicted / crashed thread is dropped and replaced) — and an
|
|
51
|
-
* narrows the input through `options.input` (fail-fast before the
|
|
52
|
-
* boundary) then
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
51
|
+
* supplies only the thread pairing — the pool `create`s a thread (the same spawn
|
|
52
|
+
* {@link createThread} publishes), `destroy`s it with `terminate()`, and `validate`s it by
|
|
53
|
+
* `alive && threadId > 0` (so an evicted / crashed thread is dropped and replaced) — and an
|
|
54
|
+
* internal handler that narrows the input through `options.input` (fail-fast before the
|
|
55
|
+
* structured-clone boundary) then runs a {@link Dispatch} against the leased thread,
|
|
56
|
+
* narrowing the reply through
|
|
57
|
+
* `options.result`. `TInput` and `TResult` infer from the `input` and `result` guards, so
|
|
58
|
+
* call sites need no explicit type arguments. The boundary is crossed with no `as`: the
|
|
59
|
+
* guards reconstruct `TInput` / `TResult` by validation. An `abort` / `timeout`
|
|
60
|
+
* terminates the in-flight thread (CPU-bound work can't honour a signal) and evicts it; a
|
|
57
61
|
* subsequent job spawns a fresh thread. The worker script's module must call
|
|
58
62
|
* `serveWorker`. Returns the plain {@link WorkerInterface} — its methods are the Worker's.
|
|
59
63
|
*
|
|
60
64
|
* @typeParam TInput - The work payload each job carries (inferred from `input`)
|
|
61
65
|
* @typeParam TResult - The value a thread resolves for a job (inferred from `result`)
|
|
62
66
|
* @param options - The `script` plus the `input` / `result` guards and optional
|
|
63
|
-
* `workerData` / `concurrency` / `retries` / `timeout` / `store`
|
|
67
|
+
* `on` / `error` / `workerData` / `concurrency` / `retries` / `timeout` / `store`
|
|
64
68
|
* (see {@link NodeWorkerOptions})
|
|
65
69
|
* @returns A working {@link WorkerInterface} backed by a thread pool
|
|
66
70
|
*
|
|
@@ -82,65 +86,126 @@ export declare function createJSONQueueStore<TInput extends ContractShape>(path:
|
|
|
82
86
|
export declare function createNodeWorker<TInput, TResult>(options: NodeWorkerOptions<TInput, TResult>): WorkerInterface<TInput, TResult>;
|
|
83
87
|
|
|
84
88
|
/**
|
|
85
|
-
*
|
|
89
|
+
* Creates one live worker thread and resolves it as a {@link NodeThread} after it comes
|
|
90
|
+
* online.
|
|
86
91
|
*
|
|
87
92
|
* @remarks
|
|
88
|
-
*
|
|
89
|
-
* resolves
|
|
90
|
-
*
|
|
91
|
-
*
|
|
93
|
+
* Constructs the thread with the `script` module and the cloned `workerData`, then
|
|
94
|
+
* resolves on the thread's `online` event (rejecting on an early `error` or on an `exit`
|
|
95
|
+
* that arrives before `online`, so the spawn promise is total — it can never dangle on a
|
|
96
|
+
* thread that died without erroring). The returned entity attaches persistent `error` /
|
|
97
|
+
* `exit` listeners that flip `alive` to `false` and latch the first terminal event on
|
|
98
|
+
* {@link NodeThread.death}: a crash is observable to an in-flight {@link Dispatch} (through
|
|
99
|
+
* its own listeners), to a pool's `validate` (through `alive`), and — crucially — to a
|
|
100
|
+
* dispatch that attaches only after the death (through the latch). A `messageerror` is terminal
|
|
101
|
+
* too, so a thread whose inbound payload could not be deserialized is never reused. The latch
|
|
102
|
+
* closes a real race: a thread can become terminal before the readiness promise continuation
|
|
103
|
+
* hands it to a {@link Dispatch}, leaving no future death event for that dispatch to observe.
|
|
104
|
+
* Without the latch, that job would wait forever. {@link createNodeWorker} spawns its pooled
|
|
105
|
+
* threads the same way; reach for this to drive one thread yourself.
|
|
106
|
+
*
|
|
107
|
+
* @param script - The worker module the thread runs (its module must call `serveWorker`)
|
|
108
|
+
* @param workerData - Opaque, structured-cloneable data handed to the thread at spawn
|
|
109
|
+
* @returns A promise resolving the online {@link NodeThread}
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* import { createThread } from '@orkestrel/worker/server'
|
|
114
|
+
*
|
|
115
|
+
* const thread = await createThread(new URL('./double.js', import.meta.url))
|
|
116
|
+
* await thread.worker.terminate()
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function createThread(script: string | URL, workerData?: unknown): Promise<NodeThread>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Represents one dispatched worker-thread job — the lifecycle entity behind a job posted to a
|
|
123
|
+
* leased {@link NodeThread}, whose {@link promise} settles with the narrowed reply.
|
|
124
|
+
*
|
|
125
|
+
* @remarks
|
|
126
|
+
* Mints a fresh per-dispatch correlation `id`, posts it with `job: context.id`, and settles
|
|
127
|
+
* when the thread replies for that correlation id. The stable Queue job id reaches the worker
|
|
128
|
+
* handler for idempotency across retries and restore; it is not caller identity or
|
|
129
|
+
* authentication / authorization evidence. Per-job consumer context is explicit,
|
|
92
130
|
* structured-cloneable `input`; ambient context is not worker-thread transport. A success
|
|
93
|
-
* `value` is narrowed through `result` (a value that fails the guard
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
131
|
+
* `value` is narrowed through `result` (a value that fails the guard rejects — the zero-`as`
|
|
132
|
+
* type bridge); a failure rejects with the thread's error string. A thread that had already died
|
|
133
|
+
* rejects synchronously at construction from the latched {@link NodeThread.death} — its death
|
|
134
|
+
* events fired before this dispatch existed and will never fire again, so waiting on the
|
|
135
|
+
* listeners would dangle forever; the latch makes death total across every event ordering. If
|
|
136
|
+
* the thread `error`s / `exit`s mid-flight the job rejects. On a `context.signal` abort it
|
|
137
|
+
* contains the cooperative `abort` post, evicts the thread, and observes `terminate()`
|
|
138
|
+
* settlement because CPU-bound work cannot honour the signal.
|
|
139
|
+
*
|
|
140
|
+
* It owns stable `message` / `messageerror` / death listener identities, settlement,
|
|
141
|
+
* result-guard containment, and abort eviction for one dispatch. Deserialization failure, a
|
|
142
|
+
* matching-id malformed reply, and abort each evict and terminate the thread before rejecting,
|
|
143
|
+
* with termination failure preserved. Non-record, id-less, hostile-id, and foreign-id chatter
|
|
144
|
+
* is ignored. Every per-job listener (`message` / `messageerror` / `error` / `exit` / `abort`)
|
|
145
|
+
* is removed on settle.
|
|
146
|
+
*
|
|
147
|
+
* Eviction reaches `alive` for a {@link NodeThread} this package produced. Against a
|
|
148
|
+
* consumer-supplied `NodeThread` an abort or a `messageerror` still terminates the supplied
|
|
149
|
+
* `worker` and rejects the job, and the implementer owns flipping its own `alive`.
|
|
105
150
|
*
|
|
106
151
|
* @typeParam TResult - The reply type the `result` guard narrows to
|
|
107
|
-
*
|
|
108
|
-
* @
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { createThread, Dispatch } from '@orkestrel/worker/server'
|
|
156
|
+
*
|
|
157
|
+
* const isNumber = (value: unknown): value is number => typeof value === 'number'
|
|
158
|
+
*
|
|
159
|
+
* const thread = await createThread(new URL('./double.js', import.meta.url))
|
|
160
|
+
* const controller = new AbortController()
|
|
161
|
+
* const job = new Dispatch(thread, 21, { id: 'job-1', signal: controller.signal }, isNumber)
|
|
162
|
+
* console.log(await job.promise) // 42
|
|
163
|
+
* await thread.worker.terminate()
|
|
164
|
+
* ```
|
|
112
165
|
*/
|
|
113
|
-
export declare
|
|
166
|
+
export declare class Dispatch<TResult> {
|
|
167
|
+
#private;
|
|
168
|
+
constructor(thread: NodeThread, input: unknown, context: QueueContext, result: Guard<TResult>);
|
|
169
|
+
get promise(): Promise<TResult>;
|
|
170
|
+
}
|
|
114
171
|
|
|
115
172
|
/**
|
|
116
|
-
*
|
|
173
|
+
* Narrows an inbound `message` to a {@link Reply} for a given correlation `id` — no assertion.
|
|
117
174
|
*
|
|
118
175
|
* @remarks
|
|
119
176
|
* A total predicate: a record whose `id` matches and whose `ok` discriminant is well-formed.
|
|
120
177
|
* Anything else is rejected so a dispatch listener can ignore foreign or malformed messages.
|
|
178
|
+
* It correlates against the `id` argument rather than narrowing one value alone, so it is a
|
|
179
|
+
* correlated predicate rather than a `Guard<Reply>` and is not accepted where a `Guard` is.
|
|
121
180
|
*
|
|
122
181
|
* @param value - The inbound message to narrow
|
|
123
|
-
* @param id - The
|
|
124
|
-
* @returns
|
|
182
|
+
* @param id - The per-dispatch correlation id a matching reply must carry
|
|
183
|
+
* @returns True if the value is this dispatch's well-formed reply; false otherwise
|
|
125
184
|
*/
|
|
126
185
|
export declare function isReply(value: unknown, id: string): value is Reply;
|
|
127
186
|
|
|
128
187
|
/**
|
|
129
|
-
*
|
|
188
|
+
* Represents a live worker thread plus its latched liveness state — the pooled resource a
|
|
130
189
|
* {@link createNodeWorker} leases per job.
|
|
131
190
|
*
|
|
132
191
|
* @remarks
|
|
133
192
|
* `alive` starts `true` and flips to `false` when the thread `error`s, reports a
|
|
134
193
|
* `messageerror`, exits, or is evicted on abort; the pool's `validate` reads
|
|
135
194
|
* `alive && worker.threadId > 0`, so a
|
|
136
|
-
* dead thread is destroyed and replaced rather than reused. `death`
|
|
195
|
+
* dead thread is destroyed and replaced rather than reused. `death` latches the first
|
|
137
196
|
* terminal event (`error` / `messageerror`, or a synthesized error on `exit`) — the death-signal
|
|
138
|
-
* record a
|
|
197
|
+
* record a {@link Dispatch} checks at construction, so a job dispatched after the thread died (its
|
|
139
198
|
* death events already fired and will never fire again) rejects immediately instead of
|
|
140
199
|
* awaiting events that already happened. A thread can become terminal before the readiness
|
|
141
200
|
* promise continuation attaches dispatch listeners; the latch is what makes that ordering
|
|
142
201
|
* safe. `worker` is the underlying
|
|
143
202
|
* `node:worker_threads` thread (its `postMessage` / `terminate` drive the protocol).
|
|
203
|
+
*
|
|
204
|
+
* A dispatch marks a thread dead for a `NodeThread` this package produced, through
|
|
205
|
+
* {@link createThread} or a {@link createNodeWorker} pool. A foreign implementation of this
|
|
206
|
+
* interface owns flipping its own `alive` when its `worker` is terminated: an abort or a
|
|
207
|
+
* `messageerror` terminates the supplied `worker` and rejects the job, and leaves the
|
|
208
|
+
* implementer's `alive` and `death` exactly as the implementer reports them.
|
|
144
209
|
*/
|
|
145
210
|
export declare interface NodeThread {
|
|
146
211
|
readonly worker: Worker;
|
|
@@ -149,31 +214,40 @@ export declare interface NodeThread {
|
|
|
149
214
|
}
|
|
150
215
|
|
|
151
216
|
/**
|
|
152
|
-
*
|
|
217
|
+
* Configures `createNodeWorker` — a CPU-parallel worker over `node:worker_threads`.
|
|
153
218
|
*
|
|
154
219
|
* @remarks
|
|
155
220
|
* - `script` — the worker module each pooled thread runs; its module must call
|
|
156
221
|
* `serveWorker(...)`. Raw TypeScript is unflagged on Node 22.18+ and Node 23.6+;
|
|
157
222
|
* Node 22.12–22.17 and Node 23.0–23.5 require `--experimental-strip-types`. A built
|
|
158
|
-
* `.js` / `.mjs` script
|
|
159
|
-
* - `input` — narrows the work payload
|
|
223
|
+
* `.js` / `.mjs` script is an alternative across supported Node versions.
|
|
224
|
+
* - `input` — narrows the work payload before it crosses the structured-clone boundary
|
|
160
225
|
* (fail-fast) and supplies the `TInput` inference, so call sites need no type argument.
|
|
161
226
|
* - `result` — narrows every reply value coming back from a thread; an invalid reply
|
|
162
227
|
* rejects the job. This is the zero-`as` type bridge — `TResult` is inferred from it.
|
|
163
|
-
* - `workerData` — opaque data cloned to every thread
|
|
164
|
-
* `
|
|
228
|
+
* - `workerData` — opaque data cloned to every thread at spawn; the key mirrors the
|
|
229
|
+
* `node:worker_threads` `Worker` constructor option of the same name, and the thread reads
|
|
230
|
+
* it back from `node:worker_threads`. It must be structured-cloneable.
|
|
165
231
|
* - `concurrency` — the maximum jobs in flight at once; the thread pool's `max` matches
|
|
166
|
-
* it, so at most this many threads exist.
|
|
167
|
-
*
|
|
168
|
-
* - `retries` — the default extra attempts per job on failure / timeout
|
|
169
|
-
* - `timeout` — the default per-attempt deadline in milliseconds
|
|
232
|
+
* it, so at most this many threads exist. It must be a positive safe integer, as
|
|
233
|
+
* validated by the underlying queue. Default: 1.
|
|
234
|
+
* - `retries` — the default extra attempts per job on failure / timeout. Default: 0.
|
|
235
|
+
* - `timeout` — the default per-attempt deadline in milliseconds. Default: no per-attempt
|
|
236
|
+
* deadline.
|
|
170
237
|
* - `store` — durable backing for outstanding jobs (survives a restart; `restore()`
|
|
171
238
|
* re-runs them).
|
|
239
|
+
* - `on` — the reserved {@link EmitterHooks} key: initial listeners for the worker's
|
|
240
|
+
* {@link WorkerEventMap} (the job lifecycle it surfaces from its underlying queue), wired
|
|
241
|
+
* at construction. A thread worker takes the same hooks as the core worker.
|
|
242
|
+
* - `error` — the emitter's listener-error handler; a listener throw routes
|
|
243
|
+
* here, not to a domain event.
|
|
172
244
|
*
|
|
173
245
|
* @typeParam TInput - The work payload each job carries (inferred from `input`)
|
|
174
246
|
* @typeParam TResult - The value a thread resolves for a job (inferred from `result`)
|
|
175
247
|
*/
|
|
176
248
|
export declare interface NodeWorkerOptions<TInput, TResult> {
|
|
249
|
+
readonly on?: EmitterHooks<WorkerEventMap<TResult>>;
|
|
250
|
+
readonly error?: EmitterErrorHandler;
|
|
177
251
|
readonly script: string | URL;
|
|
178
252
|
readonly input: Guard<TInput>;
|
|
179
253
|
readonly result: Guard<TResult>;
|
|
@@ -185,16 +259,15 @@ export declare interface NodeWorkerOptions<TInput, TResult> {
|
|
|
185
259
|
}
|
|
186
260
|
|
|
187
261
|
/**
|
|
188
|
-
*
|
|
189
|
-
* message —
|
|
262
|
+
* Represents a thread→main reply envelope — a success carrying an opaque `value`, or a failure with a
|
|
263
|
+
* message — the reply half of the wire protocol `createNodeWorker` posts and `serveWorker`
|
|
190
264
|
* answers.
|
|
191
265
|
*
|
|
192
266
|
* @remarks
|
|
193
|
-
*
|
|
194
|
-
* impl file holds only its class / functions). A reply is a discriminated union on `ok`: a
|
|
267
|
+
* A reply is a discriminated union on `ok`: a
|
|
195
268
|
* `true` carries any opaque `value` (narrowed at the boundary by the `result` guard,
|
|
196
269
|
* with no `as`); a `false` carries a string `error`. The worker-side `serve.ts` cannot import
|
|
197
|
-
* this
|
|
270
|
+
* this because it loads as raw source in a spawned thread, and posts the same
|
|
198
271
|
* shape structurally. The `id` ties a reply to its job: id-less / foreign-id chatter is ignored,
|
|
199
272
|
* while a matching-id malformed envelope taints the thread and causes dispatch to terminate it.
|
|
200
273
|
*/
|
|
@@ -209,7 +282,7 @@ export declare type Reply = {
|
|
|
209
282
|
};
|
|
210
283
|
|
|
211
284
|
/**
|
|
212
|
-
*
|
|
285
|
+
* Registers a worker-thread handler — the worker-side half of {@link createNodeWorker}.
|
|
213
286
|
*
|
|
214
287
|
* @remarks
|
|
215
288
|
* Must be the spawned thread's module entry. It listens on the parent port for the
|
|
@@ -220,11 +293,11 @@ export declare type Reply = {
|
|
|
220
293
|
* success value cannot be cloned, the post is retried as a clone-safe failure; if that post also
|
|
221
294
|
* fails, the parent port closes so the main side observes thread exit instead of waiting forever.
|
|
222
295
|
* The run envelope's `id` is fresh per dispatch and keys controllers, aborts, and replies;
|
|
223
|
-
* its `job` is the stable Queue idempotency key exposed as `
|
|
296
|
+
* its `job` is the stable Queue idempotency key exposed as `context.id` across retries
|
|
224
297
|
* and restore. That job id identifies work, not a caller, and is not authentication or
|
|
225
298
|
* authorization evidence. Each attempt has its own `AbortController`, so an `abort`
|
|
226
299
|
* message for the correlation id fires the handler's `signal` (cooperative — the main
|
|
227
|
-
* side
|
|
300
|
+
* side also terminates the thread, so a handler that ignores its signal is still stopped).
|
|
228
301
|
* Every inbound message is narrowed with the inlined guards — no `as`. On the main thread
|
|
229
302
|
* (`parentPort === null`) it is a no-op.
|
|
230
303
|
*
|
|
@@ -246,16 +319,16 @@ export declare type Reply = {
|
|
|
246
319
|
export declare function serveWorker<TInput, TResult>(options: ServeWorkerOptions<TInput, TResult>): void;
|
|
247
320
|
|
|
248
321
|
/**
|
|
249
|
-
*
|
|
322
|
+
* Configures `serveWorker` — the worker-side entry a thread script registers.
|
|
250
323
|
*
|
|
251
324
|
* @remarks
|
|
252
325
|
* - `input` — narrows each inbound payload inside the thread; an invalid payload replies
|
|
253
326
|
* with an error envelope rather than running the handler. Supplies the `TInput`
|
|
254
327
|
* inference for the handler.
|
|
255
|
-
* - `handler` — runs one job; receives the narrowed input and the Queue's
|
|
256
|
-
* `
|
|
328
|
+
* - `handler` — runs one job; receives the narrowed input and the Queue's context.
|
|
329
|
+
* `context.id` is the stable Queue idempotency key across retries and crash restore;
|
|
257
330
|
* it identifies work, not a caller, and is not authentication or authorization evidence.
|
|
258
|
-
* `
|
|
331
|
+
* `context.signal` is per attempt and fires when the main side aborts that attempt
|
|
259
332
|
* (cooperative). The handler may be async; its resolved value (which must be
|
|
260
333
|
* structured-cloneable) is the reply.
|
|
261
334
|
*
|
|
@@ -264,30 +337,7 @@ export declare function serveWorker<TInput, TResult>(options: ServeWorkerOptions
|
|
|
264
337
|
*/
|
|
265
338
|
export declare interface ServeWorkerOptions<TInput, TResult> {
|
|
266
339
|
readonly input: Guard<TInput>;
|
|
267
|
-
readonly handler: (input: TInput,
|
|
340
|
+
readonly handler: (input: TInput, context: QueueContext) => Promise<TResult> | TResult;
|
|
268
341
|
}
|
|
269
342
|
|
|
270
|
-
/**
|
|
271
|
-
* Spawn one worker thread and resolve a live {@link NodeThread} once it comes online.
|
|
272
|
-
*
|
|
273
|
-
* @remarks
|
|
274
|
-
* Constructs the thread with the `script` module and the cloned `workerData`, then
|
|
275
|
-
* resolves on the thread's `online` event (rejecting on an early `error` OR an `exit`
|
|
276
|
-
* that arrives before `online`, so the spawn promise is total — it can never dangle on a
|
|
277
|
-
* thread that died without erroring). The wrapper attaches persistent `error` / `exit`
|
|
278
|
-
* listeners that flip `alive` to `false` AND latch the first terminal event on
|
|
279
|
-
* {@link NodeThread.death}: a crash is observable to an in-flight {@link dispatch} (via
|
|
280
|
-
* its own listeners), to the pool's `validate` (via `alive`), and — crucially — to a
|
|
281
|
-
* dispatch that attaches AFTER the death (via the latch). A `messageerror` is terminal too,
|
|
282
|
-
* so a thread whose inbound payload could not be deserialized is never reused. The latch
|
|
283
|
-
* closes a real race: a thread can become terminal before the readiness promise continuation
|
|
284
|
-
* hands it to `dispatch`, leaving no future death event for that dispatch to observe. Without
|
|
285
|
-
* the latch, that job would wait forever. The pool's `create` hook calls this.
|
|
286
|
-
*
|
|
287
|
-
* @param script - The worker module each thread runs (must call `serveWorker`)
|
|
288
|
-
* @param workerData - Opaque, structured-cloneable data handed to the thread at spawn
|
|
289
|
-
* @returns A promise resolving the online {@link NodeThread}
|
|
290
|
-
*/
|
|
291
|
-
export declare function spawnThread(script: string | URL, workerData: unknown): Promise<NodeThread>;
|
|
292
|
-
|
|
293
343
|
export { }
|