@orkestrel/worker 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/dist/src/core/index.cjs +169 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +193 -0
- package/dist/src/core/index.d.ts +193 -0
- package/dist/src/core/index.js +167 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/server/index.cjs +360 -0
- package/dist/src/server/index.cjs.map +1 -0
- package/dist/src/server/index.d.cts +299 -0
- package/dist/src/server/index.d.ts +299 -0
- package/dist/src/server/index.js +354 -0
- package/dist/src/server/index.js.map +1 -0
- package/package.json +99 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { Worker, parentPort } from "node:worker_threads";
|
|
2
|
+
import { isRecord } from "@orkestrel/contract";
|
|
3
|
+
import { createWorker } from "../core/index.js";
|
|
4
|
+
import { createJSONDriver } from "@orkestrel/database/server";
|
|
5
|
+
import { createDatabaseQueueStore } from "@orkestrel/queue";
|
|
6
|
+
//#region src/server/helpers.ts
|
|
7
|
+
/**
|
|
8
|
+
* Spawn one worker thread and resolve a live {@link NodeThread} once it comes online.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Constructs the thread with the `script` module and the cloned `workerData`, then
|
|
12
|
+
* resolves on the thread's `online` event (rejecting on an early `error` OR an `exit`
|
|
13
|
+
* that arrives before `online`, so the spawn promise is total — it can never dangle on a
|
|
14
|
+
* thread that died without erroring). The wrapper attaches persistent `error` / `exit`
|
|
15
|
+
* listeners that flip `alive` to `false` AND latch the first terminal event on
|
|
16
|
+
* {@link NodeThread.death}: a crash is observable to an in-flight {@link dispatch} (via
|
|
17
|
+
* its own listeners), to the pool's `validate` (via `alive`), and — crucially — to a
|
|
18
|
+
* dispatch that attaches AFTER the death (via the latch). The latch closes a real race:
|
|
19
|
+
* under event-loop pressure a dead thread's `online` + `error` + `exit` are delivered in
|
|
20
|
+
* ONE synchronous exit-drain batch, so every death event fires before the microtask chain
|
|
21
|
+
* resolving this spawn can hand the thread to `dispatch` — without the latch that job
|
|
22
|
+
* would await events that already fired, forever. The pool's `create` hook calls this.
|
|
23
|
+
*
|
|
24
|
+
* @param script - The worker module each thread runs (must call `serveWorker`)
|
|
25
|
+
* @param workerData - Opaque, structured-cloneable data handed to the thread at spawn
|
|
26
|
+
* @returns A promise resolving the online {@link NodeThread}
|
|
27
|
+
*/
|
|
28
|
+
function spawnThread(script, workerData) {
|
|
29
|
+
const worker = new Worker(script, { workerData });
|
|
30
|
+
const thread = {
|
|
31
|
+
worker,
|
|
32
|
+
alive: true,
|
|
33
|
+
death: void 0
|
|
34
|
+
};
|
|
35
|
+
worker.on("error", (error) => {
|
|
36
|
+
thread.alive = false;
|
|
37
|
+
if (thread.death === void 0) thread.death = error;
|
|
38
|
+
});
|
|
39
|
+
worker.on("exit", (code) => {
|
|
40
|
+
thread.alive = false;
|
|
41
|
+
if (thread.death === void 0) thread.death = /* @__PURE__ */ new Error(`worker thread exited (code ${code})`);
|
|
42
|
+
});
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const onOnline = () => {
|
|
45
|
+
worker.off("error", onError);
|
|
46
|
+
worker.off("exit", onExit);
|
|
47
|
+
resolve(thread);
|
|
48
|
+
};
|
|
49
|
+
const onError = (error) => {
|
|
50
|
+
worker.off("online", onOnline);
|
|
51
|
+
worker.off("exit", onExit);
|
|
52
|
+
reject(error);
|
|
53
|
+
};
|
|
54
|
+
const onExit = (code) => {
|
|
55
|
+
worker.off("online", onOnline);
|
|
56
|
+
worker.off("error", onError);
|
|
57
|
+
reject(/* @__PURE__ */ new Error(`worker thread exited before coming online (code ${code})`));
|
|
58
|
+
};
|
|
59
|
+
worker.once("online", onOnline);
|
|
60
|
+
worker.once("error", onError);
|
|
61
|
+
worker.once("exit", onExit);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Narrow an inbound `message` to a {@link Reply} for a given job `id` — no assertion.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* A total {@link Guard}-style predicate (never throws): a record whose `id` matches and
|
|
69
|
+
* whose `ok` discriminant is well-formed (a `true` carries any `value`; a `false` carries a
|
|
70
|
+
* string `error`). Anything else — another job's reply, a malformed payload — is `false`, so
|
|
71
|
+
* a {@link dispatch} listener ignores it (a thread that chatters on the channel can't corrupt
|
|
72
|
+
* a job).
|
|
73
|
+
*
|
|
74
|
+
* @param value - The inbound message to narrow
|
|
75
|
+
* @param id - The job id a matching reply must carry
|
|
76
|
+
* @returns `true` (narrowing `value` to {@link Reply}) when it is this job's well-formed reply
|
|
77
|
+
*/
|
|
78
|
+
function isReply(value, id) {
|
|
79
|
+
if (!isRecord(value)) return false;
|
|
80
|
+
if (value.id !== id) return false;
|
|
81
|
+
if (value.ok === true) return true;
|
|
82
|
+
return value.ok === false && typeof value.error === "string";
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Dispatch one job to a leased {@link NodeThread} and await its narrowed reply.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Mints a fresh `id`, posts a `run` envelope, and resolves when the thread replies for
|
|
89
|
+
* that id: a success `value` is narrowed through `result` (a value that fails the guard
|
|
90
|
+
* rejects — the zero-`as` type bridge), a failure rejects with the thread's error string.
|
|
91
|
+
* A thread that ALREADY died rejects synchronously at entry from the latched
|
|
92
|
+
* {@link NodeThread.death} — its death events fired before this dispatch existed (under
|
|
93
|
+
* load they arrive in one batched exit drain) and will never fire again, so waiting on
|
|
94
|
+
* the listeners below would dangle forever; the latch makes the death total across every
|
|
95
|
+
* event ordering. If the thread `error`s / `exit`s mid-flight it is marked dead and the
|
|
96
|
+
* job rejects. On `execution.signal` abort it posts an `abort` envelope (cooperative) AND
|
|
97
|
+
* evicts the thread — `alive = false` + `terminate()` — because CPU-bound work cannot
|
|
98
|
+
* honour the signal; the freed pool slot then gets a fresh thread. Every listener (the
|
|
99
|
+
* thread's `message` / `error` / `exit` and the signal's `abort`) is removed on settle,
|
|
100
|
+
* and a `settled` guard prevents a double-settle.
|
|
101
|
+
*
|
|
102
|
+
* @typeParam TResult - The reply type the `result` guard narrows to
|
|
103
|
+
* @param thread - The leased thread to run the job on
|
|
104
|
+
* @param input - The work payload (structured-cloned to the thread)
|
|
105
|
+
* @param execution - The per-attempt handle; its `signal` aborts → terminate + evict
|
|
106
|
+
* @param result - The {@link Guard} narrowing the reply value with no assertion
|
|
107
|
+
* @returns A promise resolving the narrowed `TResult`, or rejecting on error / abort
|
|
108
|
+
*/
|
|
109
|
+
function dispatch(thread, input, execution, result) {
|
|
110
|
+
const id = crypto.randomUUID();
|
|
111
|
+
const worker = thread.worker;
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
if (thread.death !== void 0 || !thread.alive) {
|
|
114
|
+
reject(thread.death ?? /* @__PURE__ */ new Error("worker thread is dead"));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let settled = false;
|
|
118
|
+
let detach = () => {};
|
|
119
|
+
const settle = (action) => {
|
|
120
|
+
if (settled) return;
|
|
121
|
+
settled = true;
|
|
122
|
+
detach();
|
|
123
|
+
action();
|
|
124
|
+
};
|
|
125
|
+
const onMessage = (value) => {
|
|
126
|
+
if (!isReply(value, id)) return;
|
|
127
|
+
if (value.ok) {
|
|
128
|
+
const reply = value.value;
|
|
129
|
+
if (result(reply)) settle(() => resolve(reply));
|
|
130
|
+
else settle(() => reject(/* @__PURE__ */ new Error("reply did not satisfy result guard")));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const message = value.error;
|
|
134
|
+
settle(() => reject(new Error(message)));
|
|
135
|
+
};
|
|
136
|
+
const onError = (error) => {
|
|
137
|
+
thread.alive = false;
|
|
138
|
+
settle(() => reject(error));
|
|
139
|
+
};
|
|
140
|
+
const onExit = () => {
|
|
141
|
+
thread.alive = false;
|
|
142
|
+
settle(() => reject(/* @__PURE__ */ new Error("worker thread exited")));
|
|
143
|
+
};
|
|
144
|
+
const onAbort = () => {
|
|
145
|
+
worker.postMessage({
|
|
146
|
+
id,
|
|
147
|
+
command: "abort"
|
|
148
|
+
});
|
|
149
|
+
thread.alive = false;
|
|
150
|
+
worker.terminate();
|
|
151
|
+
settle(() => reject(/* @__PURE__ */ new Error("job aborted")));
|
|
152
|
+
};
|
|
153
|
+
detach = () => {
|
|
154
|
+
worker.off("message", onMessage);
|
|
155
|
+
worker.off("error", onError);
|
|
156
|
+
worker.off("exit", onExit);
|
|
157
|
+
execution.signal.removeEventListener("abort", onAbort);
|
|
158
|
+
};
|
|
159
|
+
worker.on("message", onMessage);
|
|
160
|
+
worker.on("error", onError);
|
|
161
|
+
worker.on("exit", onExit);
|
|
162
|
+
if (execution.signal.aborted) {
|
|
163
|
+
onAbort();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
execution.signal.addEventListener("abort", onAbort, { once: true });
|
|
167
|
+
try {
|
|
168
|
+
worker.postMessage({
|
|
169
|
+
id,
|
|
170
|
+
command: "run",
|
|
171
|
+
input
|
|
172
|
+
});
|
|
173
|
+
} catch (error) {
|
|
174
|
+
settle(() => reject(error instanceof Error ? error : new Error(String(error))));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/server/serve.ts
|
|
180
|
+
function isRecord$1(value) {
|
|
181
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
182
|
+
}
|
|
183
|
+
function isRun(value) {
|
|
184
|
+
return isRecord$1(value) && typeof value.id === "string" && value.command === "run" && "input" in value;
|
|
185
|
+
}
|
|
186
|
+
function isAbort(value) {
|
|
187
|
+
return isRecord$1(value) && typeof value.id === "string" && value.command === "abort";
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Register a worker-thread handler — the worker-side half of {@link createNodeWorker}.
|
|
191
|
+
*
|
|
192
|
+
* @remarks
|
|
193
|
+
* Must be the spawned thread's module entry. It listens on the parent port for the
|
|
194
|
+
* run/abort protocol: a `run` message narrows its `input` through `options.input` (an
|
|
195
|
+
* invalid payload replies with an error envelope, never running the handler), then runs
|
|
196
|
+
* `options.handler(input, { signal })` and replies `{ id, ok: true, value }` on success or
|
|
197
|
+
* `{ id, ok: false, error }` on throw. Each in-flight job has its own `AbortController`,
|
|
198
|
+
* so an `abort` message for that id fires the handler's `signal` (cooperative — the main
|
|
199
|
+
* side ALSO terminates the thread, so a handler that ignores its signal is still stopped).
|
|
200
|
+
* Every inbound message is narrowed with the inlined guards — no `as`. On the main thread
|
|
201
|
+
* (`parentPort === null`) it is a no-op.
|
|
202
|
+
*
|
|
203
|
+
* @typeParam TInput - The work payload (inferred from `options.input`)
|
|
204
|
+
* @typeParam TResult - The value the handler resolves (the reply payload)
|
|
205
|
+
* @param options - The `input` guard and the `handler` (see {@link ServeWorkerOptions})
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* // double.ts — a worker script
|
|
210
|
+
* import { serveWorker } from '@src/server'
|
|
211
|
+
*
|
|
212
|
+
* serveWorker<number, number>({
|
|
213
|
+
* input: (value): value is number => typeof value === 'number',
|
|
214
|
+
* handler: (value) => value * 2,
|
|
215
|
+
* })
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
function serveWorker(options) {
|
|
219
|
+
const port = parentPort;
|
|
220
|
+
if (port === null) return;
|
|
221
|
+
const controllers = /* @__PURE__ */ new Map();
|
|
222
|
+
port.on("message", (raw) => {
|
|
223
|
+
if (isAbort(raw)) {
|
|
224
|
+
controllers.get(raw.id)?.abort();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (!isRun(raw)) return;
|
|
228
|
+
const id = raw.id;
|
|
229
|
+
if (!options.input(raw.input)) {
|
|
230
|
+
port.postMessage({
|
|
231
|
+
id,
|
|
232
|
+
ok: false,
|
|
233
|
+
error: "input did not satisfy input guard"
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const input = raw.input;
|
|
238
|
+
const controller = new AbortController();
|
|
239
|
+
controllers.set(id, controller);
|
|
240
|
+
Promise.resolve().then(() => options.handler(input, { signal: controller.signal })).then((value) => {
|
|
241
|
+
controllers.delete(id);
|
|
242
|
+
port.postMessage({
|
|
243
|
+
id,
|
|
244
|
+
ok: true,
|
|
245
|
+
value
|
|
246
|
+
});
|
|
247
|
+
}, (error) => {
|
|
248
|
+
controllers.delete(id);
|
|
249
|
+
port.postMessage({
|
|
250
|
+
id,
|
|
251
|
+
ok: false,
|
|
252
|
+
error: error instanceof Error ? error.message : String(error)
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/server/factories.ts
|
|
259
|
+
/**
|
|
260
|
+
* Create a persistent JSON-file {@link QueueStoreInterface} — the core
|
|
261
|
+
* `createDatabaseQueueStore` over a server {@link createJSONDriver}.
|
|
262
|
+
*
|
|
263
|
+
* @remarks
|
|
264
|
+
* A queue's durable state is just a database table, so JSON persistence reuses the
|
|
265
|
+
* existing JSON-file driver rather than a bespoke store: the entries are written to
|
|
266
|
+
* (and reloaded from) the file at `path`, surviving a process restart. There is no new
|
|
267
|
+
* class — the store engine ({@link createDatabaseQueueStore}) is shared, and only the
|
|
268
|
+
* driver changes where the bytes live. The `input` shape must be JSON-serializable
|
|
269
|
+
* (the JSON driver round-trips it as JSON). Build a second store over the SAME `path` to
|
|
270
|
+
* resume the outstanding entries a prior store persisted.
|
|
271
|
+
*
|
|
272
|
+
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
273
|
+
* @param path - The JSON file the entries are loaded from and flushed to
|
|
274
|
+
* @param input - The {@link ContractShape} for the work payload (the `input` column)
|
|
275
|
+
* @returns A JSON-file-backed {@link QueueStoreInterface}, typed by `input`
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* import { stringShape } from '@src/core'
|
|
280
|
+
* import { createJSONQueueStore } from '@src/server'
|
|
281
|
+
*
|
|
282
|
+
* const store = createJSONQueueStore('data/queue.json', stringShape())
|
|
283
|
+
* await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })
|
|
284
|
+
* // A later process resumes the outstanding work:
|
|
285
|
+
* const resumed = createJSONQueueStore('data/queue.json', stringShape())
|
|
286
|
+
* const outstanding = await resumed.load()
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
function createJSONQueueStore(path, input) {
|
|
290
|
+
return createDatabaseQueueStore(input, createJSONDriver(path));
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Create a CPU-parallel worker over `node:worker_threads` — a thin specialization of the
|
|
294
|
+
* core `createWorker` whose pooled resource is a worker THREAD.
|
|
295
|
+
*
|
|
296
|
+
* @remarks
|
|
297
|
+
* Composition, not reimplementation: all concurrency, retries, per-attempt timeout,
|
|
298
|
+
* lifecycle, and durability are the core `Worker`'s (a `Queue` ⨉ `Pool`). This factory
|
|
299
|
+
* supplies only the thread pairing — the pool `create`s a thread (via `spawnThread`),
|
|
300
|
+
* `destroy`s it with `terminate()`, and `validate`s it by `alive && threadId > 0` (so an
|
|
301
|
+
* evicted / crashed thread is dropped and replaced) — and an internal handler that
|
|
302
|
+
* narrows the input through `options.input` (fail-fast before the structured-clone
|
|
303
|
+
* boundary) then `dispatch`es the job to the leased thread, narrowing the reply through
|
|
304
|
+
* `options.result`. Both generics INFER from the `input` / `result` guards, so call sites
|
|
305
|
+
* need no explicit type arguments. The boundary is crossed with ZERO `as`: the guards
|
|
306
|
+
* reconstruct `TInput` / `TResult` by validation (AGENTS §14). An `abort` / `timeout`
|
|
307
|
+
* TERMINATES the in-flight thread (CPU-bound work can't honour a signal) and evicts it; a
|
|
308
|
+
* subsequent job spawns a fresh thread. The worker script's module must call
|
|
309
|
+
* `serveWorker`. Returns the plain {@link WorkerInterface} — its methods are the Worker's.
|
|
310
|
+
*
|
|
311
|
+
* @typeParam TInput - The work payload each job carries (inferred from `input`)
|
|
312
|
+
* @typeParam TResult - The value a thread resolves for a job (inferred from `result`)
|
|
313
|
+
* @param options - The `script` plus the `input` / `result` guards and optional
|
|
314
|
+
* `workerData` / `concurrency` / `retries` / `timeout` / `store`
|
|
315
|
+
* (see {@link NodeWorkerOptions})
|
|
316
|
+
* @returns A working {@link WorkerInterface} backed by a thread pool
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* import { createNodeWorker } from '@src/server'
|
|
321
|
+
*
|
|
322
|
+
* const worker = createNodeWorker({
|
|
323
|
+
* script: new URL('./double.js', import.meta.url),
|
|
324
|
+
* input: (value): value is number => typeof value === 'number',
|
|
325
|
+
* result: (value): value is number => typeof value === 'number',
|
|
326
|
+
* concurrency: 4,
|
|
327
|
+
* })
|
|
328
|
+
*
|
|
329
|
+
* const doubled = await worker.enqueue(21) // 42, computed on a worker thread
|
|
330
|
+
* worker.destroy() // terminates every thread
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
function createNodeWorker(options) {
|
|
334
|
+
return createWorker({
|
|
335
|
+
pool: {
|
|
336
|
+
create: () => spawnThread(options.script, options.workerData),
|
|
337
|
+
destroy: (thread) => thread.worker.terminate().then(() => {}),
|
|
338
|
+
validate: (thread) => thread.alive && thread.worker.threadId > 0,
|
|
339
|
+
max: options.concurrency
|
|
340
|
+
},
|
|
341
|
+
handler: (input, thread, execution) => {
|
|
342
|
+
if (!options.input(input)) return Promise.reject(/* @__PURE__ */ new Error("input did not satisfy input guard"));
|
|
343
|
+
return dispatch(thread, input, execution, options.result);
|
|
344
|
+
},
|
|
345
|
+
concurrency: options.concurrency,
|
|
346
|
+
retries: options.retries,
|
|
347
|
+
timeout: options.timeout,
|
|
348
|
+
store: options.store
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
//#endregion
|
|
352
|
+
export { createJSONQueueStore, createNodeWorker, dispatch, isReply, serveWorker, spawnThread };
|
|
353
|
+
|
|
354
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/server/helpers.ts","../../../src/server/serve.ts","../../../src/server/factories.ts"],"sourcesContent":["import type { QueueExecution } from '@orkestrel/queue'\nimport type { Guard, NodeThread, Reply } from './types.js'\nimport { Worker as ThreadWorker } from 'node:worker_threads'\nimport { isRecord } from '@orkestrel/contract'\n\n// === The wire protocol (main ↔ thread)\n//\n// The main-side half of the run/abort/reply protocol `serveWorker` answers — spawning a\n// pooled thread, narrowing its replies, and dispatching one job at a time. The envelope\n// types ({@link Reply}, {@link NodeThread}) live in `./types.js` (AGENTS §5); the public\n// bridge across the structured-clone boundary is the `input` / `result` `Guard`s, which\n// narrow the envelopes' opaque `unknown` payloads with no assertion (AGENTS §14).\n\n/**\n * Spawn one worker thread and resolve a live {@link NodeThread} once it comes online.\n *\n * @remarks\n * Constructs the thread with the `script` module and the cloned `workerData`, then\n * resolves on the thread's `online` event (rejecting on an early `error` OR an `exit`\n * that arrives before `online`, so the spawn promise is total — it can never dangle on a\n * thread that died without erroring). The wrapper attaches persistent `error` / `exit`\n * listeners that flip `alive` to `false` AND latch the first terminal event on\n * {@link NodeThread.death}: a crash is observable to an in-flight {@link dispatch} (via\n * its own listeners), to the pool's `validate` (via `alive`), and — crucially — to a\n * dispatch that attaches AFTER the death (via the latch). The latch closes a real race:\n * under event-loop pressure a dead thread's `online` + `error` + `exit` are delivered in\n * ONE synchronous exit-drain batch, so every death event fires before the microtask chain\n * resolving this spawn can hand the thread to `dispatch` — without the latch that job\n * would await events that already fired, forever. The pool's `create` hook calls this.\n *\n * @param script - The worker module each thread runs (must call `serveWorker`)\n * @param workerData - Opaque, structured-cloneable data handed to the thread at spawn\n * @returns A promise resolving the online {@link NodeThread}\n */\nexport function spawnThread(script: string | URL, workerData: unknown): Promise<NodeThread> {\n\tconst worker = new ThreadWorker(script, { workerData })\n\tconst thread: NodeThread = { worker, alive: true, death: undefined }\n\t// The persistent death latch — attached BEFORE any once-listener, so the first terminal\n\t// event records its cause on the record even when it fires inside a batched exit drain.\n\tworker.on('error', (error: Error) => {\n\t\tthread.alive = false\n\t\tif (thread.death === undefined) thread.death = error\n\t})\n\tworker.on('exit', (code: number) => {\n\t\tthread.alive = false\n\t\tif (thread.death === undefined) thread.death = new Error(`worker thread exited (code ${code})`)\n\t})\n\treturn new Promise<NodeThread>((resolve, reject) => {\n\t\tconst onOnline = (): void => {\n\t\t\tworker.off('error', onError)\n\t\t\tworker.off('exit', onExit)\n\t\t\tresolve(thread)\n\t\t}\n\t\tconst onError = (error: Error): void => {\n\t\t\tworker.off('online', onOnline)\n\t\t\tworker.off('exit', onExit)\n\t\t\treject(error)\n\t\t}\n\t\tconst onExit = (code: number): void => {\n\t\t\tworker.off('online', onOnline)\n\t\t\tworker.off('error', onError)\n\t\t\treject(new Error(`worker thread exited before coming online (code ${code})`))\n\t\t}\n\t\tworker.once('online', onOnline)\n\t\tworker.once('error', onError)\n\t\tworker.once('exit', onExit)\n\t})\n}\n\n/**\n * Narrow an inbound `message` to a {@link Reply} for a given job `id` — no assertion.\n *\n * @remarks\n * A total {@link Guard}-style predicate (never throws): a record whose `id` matches and\n * whose `ok` discriminant is well-formed (a `true` carries any `value`; a `false` carries a\n * string `error`). Anything else — another job's reply, a malformed payload — is `false`, so\n * a {@link dispatch} listener ignores it (a thread that chatters on the channel can't corrupt\n * a job).\n *\n * @param value - The inbound message to narrow\n * @param id - The job id a matching reply must carry\n * @returns `true` (narrowing `value` to {@link Reply}) when it is this job's well-formed reply\n */\nexport function isReply(value: unknown, id: string): value is Reply {\n\tif (!isRecord(value)) return false\n\tif (value.id !== id) return false\n\tif (value.ok === true) return true\n\treturn value.ok === false && typeof value.error === 'string'\n}\n\n/**\n * Dispatch one job to a leased {@link NodeThread} and await its narrowed reply.\n *\n * @remarks\n * Mints a fresh `id`, posts a `run` envelope, and resolves when the thread replies for\n * that id: a success `value` is narrowed through `result` (a value that fails the guard\n * rejects — the zero-`as` type bridge), a failure rejects with the thread's error string.\n * A thread that ALREADY died rejects synchronously at entry from the latched\n * {@link NodeThread.death} — its death events fired before this dispatch existed (under\n * load they arrive in one batched exit drain) and will never fire again, so waiting on\n * the listeners below would dangle forever; the latch makes the death total across every\n * event ordering. If the thread `error`s / `exit`s mid-flight it is marked dead and the\n * job rejects. On `execution.signal` abort it posts an `abort` envelope (cooperative) AND\n * evicts the thread — `alive = false` + `terminate()` — because CPU-bound work cannot\n * honour the signal; the freed pool slot then gets a fresh thread. Every listener (the\n * thread's `message` / `error` / `exit` and the signal's `abort`) is removed on settle,\n * and a `settled` guard prevents a double-settle.\n *\n * @typeParam TResult - The reply type the `result` guard narrows to\n * @param thread - The leased thread to run the job on\n * @param input - The work payload (structured-cloned to the thread)\n * @param execution - The per-attempt handle; its `signal` aborts → terminate + evict\n * @param result - The {@link Guard} narrowing the reply value with no assertion\n * @returns A promise resolving the narrowed `TResult`, or rejecting on error / abort\n */\nexport function dispatch<TResult>(\n\tthread: NodeThread,\n\tinput: unknown,\n\texecution: QueueExecution,\n\tresult: Guard<TResult>,\n): Promise<TResult> {\n\tconst id = crypto.randomUUID()\n\tconst worker = thread.worker\n\treturn new Promise<TResult>((resolve, reject) => {\n\t\t// The latched-death entry check: a thread that died BEFORE this dispatch attached has\n\t\t// already emitted its `error` / `exit` (a batched exit drain delivers them before this\n\t\t// microtask runs) — no listener below will ever fire, and a `postMessage` to it is a\n\t\t// silent no-op. Reject NOW from the latch; this check + the attaches are synchronous,\n\t\t// so there is no gap a death can slip through.\n\t\tif (thread.death !== undefined || !thread.alive) {\n\t\t\treject(thread.death ?? new Error('worker thread is dead'))\n\t\t\treturn\n\t\t}\n\t\tlet settled = false\n\t\tlet detach = (): void => {}\n\t\tconst settle = (action: () => void): void => {\n\t\t\tif (settled) return\n\t\t\tsettled = true\n\t\t\tdetach()\n\t\t\taction()\n\t\t}\n\t\tconst onMessage = (value: unknown): void => {\n\t\t\tif (!isReply(value, id)) return\n\t\t\tif (value.ok) {\n\t\t\t\tconst reply = value.value\n\t\t\t\tif (result(reply)) settle(() => resolve(reply))\n\t\t\t\telse settle(() => reject(new Error('reply did not satisfy result guard')))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst message = value.error\n\t\t\tsettle(() => reject(new Error(message)))\n\t\t}\n\t\tconst onError = (error: Error): void => {\n\t\t\tthread.alive = false\n\t\t\tsettle(() => reject(error))\n\t\t}\n\t\tconst onExit = (): void => {\n\t\t\tthread.alive = false\n\t\t\tsettle(() => reject(new Error('worker thread exited')))\n\t\t}\n\t\t// Cooperative abort first, then EVICT: CPU-bound work won't honour the signal, so\n\t\t// terminate the thread and mark it dead — the pool replaces it on the next acquire.\n\t\t// NOTE: this `terminate()` may run TWICE — once here, and again when the pool's\n\t\t// `destroy` hook (`thread.worker.terminate()`) tears down the now-dead thread its\n\t\t// `validate` evicts. A second `terminate()` on an already-terminated Node thread is a\n\t\t// safe, idempotent no-op (it resolves with the prior exit code), so do NOT \"dedupe\" it\n\t\t// by gating on `alive` — that would skip the pool's eviction and reuse a tainted thread.\n\t\tconst onAbort = (): void => {\n\t\t\tworker.postMessage({ id, command: 'abort' })\n\t\t\tthread.alive = false\n\t\t\tvoid worker.terminate()\n\t\t\tsettle(() => reject(new Error('job aborted')))\n\t\t}\n\t\tdetach = (): void => {\n\t\t\tworker.off('message', onMessage)\n\t\t\tworker.off('error', onError)\n\t\t\tworker.off('exit', onExit)\n\t\t\texecution.signal.removeEventListener('abort', onAbort)\n\t\t}\n\t\tworker.on('message', onMessage)\n\t\tworker.on('error', onError)\n\t\tworker.on('exit', onExit)\n\t\tif (execution.signal.aborted) {\n\t\t\tonAbort()\n\t\t\treturn\n\t\t}\n\t\texecution.signal.addEventListener('abort', onAbort, { once: true })\n\t\t// `postMessage` structured-clones `input`; a non-cloneable payload throws here —\n\t\t// settle-reject so the listeners detach (no leak) rather than escaping the executor.\n\t\ttry {\n\t\t\tworker.postMessage({ id, command: 'run', input })\n\t\t} catch (error: unknown) {\n\t\t\tsettle(() => reject(error instanceof Error ? error : new Error(String(error))))\n\t\t}\n\t})\n}\n","import type { ServeWorkerOptions } from './types.js'\nimport { parentPort } from 'node:worker_threads'\n\n// The worker-side entry. SELF-CONTAINED by necessity: this module loads as RAW `.ts`\n// inside a spawned thread (Node ≥ 23.6 type-stripping), so it imports ONLY\n// `node:worker_threads` at runtime — no `@src/*`, no `.js`-relative value imports (the\n// only non-node import is the type-only `ServeWorkerOptions`, fully erased at runtime).\n// Its guards are inlined for the same reason. A worker script that needs the cloned\n// `workerData` reads it directly from `node:worker_threads` (it is in a thread already).\n\n// Inlined record guard (do NOT import `isRecord` from `@src/core` — see above). Total:\n// adversarial input returns `false`, never throws (AGENTS §14).\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\n// Narrow an inbound message to a `run` envelope (a string `id` + a `'run'` command + an\n// `input` payload) — no assertion.\nfunction isRun(value: unknown): value is { readonly id: string; readonly input: unknown } {\n\treturn (\n\t\tisRecord(value) && typeof value.id === 'string' && value.command === 'run' && 'input' in value\n\t)\n}\n\n// Narrow an inbound message to an `abort` envelope (a string `id` + an `'abort'` command).\nfunction isAbort(value: unknown): value is { readonly id: string } {\n\treturn isRecord(value) && typeof value.id === 'string' && value.command === 'abort'\n}\n\n/**\n * Register a worker-thread handler — the worker-side half of {@link createNodeWorker}.\n *\n * @remarks\n * Must be the spawned thread's module entry. It listens on the parent port for the\n * run/abort protocol: a `run` message narrows its `input` through `options.input` (an\n * invalid payload replies with an error envelope, never running the handler), then runs\n * `options.handler(input, { signal })` and replies `{ id, ok: true, value }` on success or\n * `{ id, ok: false, error }` on throw. Each in-flight job has its own `AbortController`,\n * so an `abort` message for that id fires the handler's `signal` (cooperative — the main\n * side ALSO terminates the thread, so a handler that ignores its signal is still stopped).\n * Every inbound message is narrowed with the inlined guards — no `as`. On the main thread\n * (`parentPort === null`) it is a no-op.\n *\n * @typeParam TInput - The work payload (inferred from `options.input`)\n * @typeParam TResult - The value the handler resolves (the reply payload)\n * @param options - The `input` guard and the `handler` (see {@link ServeWorkerOptions})\n *\n * @example\n * ```ts\n * // double.ts — a worker script\n * import { serveWorker } from '@src/server'\n *\n * serveWorker<number, number>({\n * \tinput: (value): value is number => typeof value === 'number',\n * \thandler: (value) => value * 2,\n * })\n * ```\n */\nexport function serveWorker<TInput, TResult>(options: ServeWorkerOptions<TInput, TResult>): void {\n\tconst port = parentPort\n\tif (port === null) return\n\tconst controllers = new Map<string, AbortController>()\n\tport.on('message', (raw: unknown) => {\n\t\tif (isAbort(raw)) {\n\t\t\tcontrollers.get(raw.id)?.abort()\n\t\t\treturn\n\t\t}\n\t\tif (!isRun(raw)) return\n\t\tconst id = raw.id\n\t\tif (!options.input(raw.input)) {\n\t\t\tport.postMessage({ id, ok: false, error: 'input did not satisfy input guard' })\n\t\t\treturn\n\t\t}\n\t\tconst input = raw.input\n\t\tconst controller = new AbortController()\n\t\tcontrollers.set(id, controller)\n\t\t// Defer the handler call into the `then` so a SYNCHRONOUS throw becomes a rejection\n\t\t// (not an uncaught thread exception) and is reported as an error reply.\n\t\tPromise.resolve()\n\t\t\t.then(() => options.handler(input, { signal: controller.signal }))\n\t\t\t.then(\n\t\t\t\t(value) => {\n\t\t\t\t\tcontrollers.delete(id)\n\t\t\t\t\tport.postMessage({ id, ok: true, value })\n\t\t\t\t},\n\t\t\t\t(error: unknown) => {\n\t\t\t\t\tcontrollers.delete(id)\n\t\t\t\t\tport.postMessage({\n\t\t\t\t\t\tid,\n\t\t\t\t\t\tok: false,\n\t\t\t\t\t\terror: error instanceof Error ? error.message : String(error),\n\t\t\t\t\t})\n\t\t\t\t},\n\t\t\t)\n\t})\n}\n","import type { WorkerInterface } from '@src/core'\nimport type { ContractShape, Infer } from '@orkestrel/contract'\nimport type { QueueStoreInterface } from '@orkestrel/queue'\nimport type { NodeThread, NodeWorkerOptions } from './types.js'\nimport { createWorker } from '@src/core'\nimport { createJSONDriver } from '@orkestrel/database/server'\nimport { createDatabaseQueueStore } from '@orkestrel/queue'\nimport { dispatch, spawnThread } from './helpers.js'\n\n/**\n * Create a persistent JSON-file {@link QueueStoreInterface} — the core\n * `createDatabaseQueueStore` over a server {@link createJSONDriver}.\n *\n * @remarks\n * A queue's durable state is just a database table, so JSON persistence reuses the\n * existing JSON-file driver rather than a bespoke store: the entries are written to\n * (and reloaded from) the file at `path`, surviving a process restart. There is no new\n * class — the store engine ({@link createDatabaseQueueStore}) is shared, and only the\n * driver changes where the bytes live. The `input` shape must be JSON-serializable\n * (the JSON driver round-trips it as JSON). Build a second store over the SAME `path` to\n * resume the outstanding entries a prior store persisted.\n *\n * @typeParam TInput - The contract shape of each entry's `input` payload\n * @param path - The JSON file the entries are loaded from and flushed to\n * @param input - The {@link ContractShape} for the work payload (the `input` column)\n * @returns A JSON-file-backed {@link QueueStoreInterface}, typed by `input`\n *\n * @example\n * ```ts\n * import { stringShape } from '@src/core'\n * import { createJSONQueueStore } from '@src/server'\n *\n * const store = createJSONQueueStore('data/queue.json', stringShape())\n * await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })\n * // A later process resumes the outstanding work:\n * const resumed = createJSONQueueStore('data/queue.json', stringShape())\n * const outstanding = await resumed.load()\n * ```\n */\nexport function createJSONQueueStore<TInput extends ContractShape>(\n\tpath: string,\n\tinput: TInput,\n): QueueStoreInterface<Infer<TInput>> {\n\treturn createDatabaseQueueStore(input, createJSONDriver(path))\n}\n\n/**\n * Create a CPU-parallel worker over `node:worker_threads` — a thin specialization of the\n * core `createWorker` whose pooled resource is a worker THREAD.\n *\n * @remarks\n * Composition, not reimplementation: all concurrency, retries, per-attempt timeout,\n * lifecycle, and durability are the core `Worker`'s (a `Queue` ⨉ `Pool`). This factory\n * supplies only the thread pairing — the pool `create`s a thread (via `spawnThread`),\n * `destroy`s it with `terminate()`, and `validate`s it by `alive && threadId > 0` (so an\n * evicted / crashed thread is dropped and replaced) — and an internal handler that\n * narrows the input through `options.input` (fail-fast before the structured-clone\n * boundary) then `dispatch`es the job to the leased thread, narrowing the reply through\n * `options.result`. Both generics INFER from the `input` / `result` guards, so call sites\n * need no explicit type arguments. The boundary is crossed with ZERO `as`: the guards\n * reconstruct `TInput` / `TResult` by validation (AGENTS §14). An `abort` / `timeout`\n * TERMINATES the in-flight thread (CPU-bound work can't honour a signal) and evicts it; a\n * subsequent job spawns a fresh thread. The worker script's module must call\n * `serveWorker`. Returns the plain {@link WorkerInterface} — its methods are the Worker's.\n *\n * @typeParam TInput - The work payload each job carries (inferred from `input`)\n * @typeParam TResult - The value a thread resolves for a job (inferred from `result`)\n * @param options - The `script` plus the `input` / `result` guards and optional\n * `workerData` / `concurrency` / `retries` / `timeout` / `store`\n * (see {@link NodeWorkerOptions})\n * @returns A working {@link WorkerInterface} backed by a thread pool\n *\n * @example\n * ```ts\n * import { createNodeWorker } from '@src/server'\n *\n * const worker = createNodeWorker({\n * \tscript: new URL('./double.js', import.meta.url),\n * \tinput: (value): value is number => typeof value === 'number',\n * \tresult: (value): value is number => typeof value === 'number',\n * \tconcurrency: 4,\n * })\n *\n * const doubled = await worker.enqueue(21) // 42, computed on a worker thread\n * worker.destroy() // terminates every thread\n * ```\n */\nexport function createNodeWorker<TInput, TResult>(\n\toptions: NodeWorkerOptions<TInput, TResult>,\n): WorkerInterface<TInput, TResult> {\n\treturn createWorker<TInput, NodeThread, TResult>({\n\t\tpool: {\n\t\t\tcreate: () => spawnThread(options.script, options.workerData),\n\t\t\tdestroy: (thread) => thread.worker.terminate().then(() => {}),\n\t\t\tvalidate: (thread) => thread.alive && thread.worker.threadId > 0,\n\t\t\tmax: options.concurrency,\n\t\t},\n\t\thandler: (input, thread, execution) => {\n\t\t\tif (!options.input(input)) {\n\t\t\t\treturn Promise.reject(new Error('input did not satisfy input guard'))\n\t\t\t}\n\t\t\treturn dispatch(thread, input, execution, options.result)\n\t\t},\n\t\tconcurrency: options.concurrency,\n\t\tretries: options.retries,\n\t\ttimeout: options.timeout,\n\t\tstore: options.store,\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAgB,YAAY,QAAsB,YAA0C;CAC3F,MAAM,SAAS,IAAI,OAAa,QAAQ,EAAE,WAAW,CAAC;CACtD,MAAM,SAAqB;EAAE;EAAQ,OAAO;EAAM,OAAO,KAAA;CAAU;CAGnE,OAAO,GAAG,UAAU,UAAiB;EACpC,OAAO,QAAQ;EACf,IAAI,OAAO,UAAU,KAAA,GAAW,OAAO,QAAQ;CAChD,CAAC;CACD,OAAO,GAAG,SAAS,SAAiB;EACnC,OAAO,QAAQ;EACf,IAAI,OAAO,UAAU,KAAA,GAAW,OAAO,wBAAQ,IAAI,MAAM,8BAA8B,KAAK,EAAE;CAC/F,CAAC;CACD,OAAO,IAAI,SAAqB,SAAS,WAAW;EACnD,MAAM,iBAAuB;GAC5B,OAAO,IAAI,SAAS,OAAO;GAC3B,OAAO,IAAI,QAAQ,MAAM;GACzB,QAAQ,MAAM;EACf;EACA,MAAM,WAAW,UAAuB;GACvC,OAAO,IAAI,UAAU,QAAQ;GAC7B,OAAO,IAAI,QAAQ,MAAM;GACzB,OAAO,KAAK;EACb;EACA,MAAM,UAAU,SAAuB;GACtC,OAAO,IAAI,UAAU,QAAQ;GAC7B,OAAO,IAAI,SAAS,OAAO;GAC3B,uBAAO,IAAI,MAAM,mDAAmD,KAAK,EAAE,CAAC;EAC7E;EACA,OAAO,KAAK,UAAU,QAAQ;EAC9B,OAAO,KAAK,SAAS,OAAO;EAC5B,OAAO,KAAK,QAAQ,MAAM;CAC3B,CAAC;AACF;;;;;;;;;;;;;;;AAgBA,SAAgB,QAAQ,OAAgB,IAA4B;CACnE,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO;CAC7B,IAAI,MAAM,OAAO,IAAI,OAAO;CAC5B,IAAI,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|
+
"description": "A typed, resource-backed job worker for the @orkestrel line — a Queue paired with a Pool over an execution seam, plus a node:worker_threads server surface for CPU-parallel jobs. Part of the @orkestrel line.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"async",
|
|
7
|
+
"concurrency",
|
|
8
|
+
"job-queue",
|
|
9
|
+
"pool",
|
|
10
|
+
"typescript",
|
|
11
|
+
"worker",
|
|
12
|
+
"worker-threads"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/orkestrel/worker#readme",
|
|
15
|
+
"bugs": "https://github.com/orkestrel/worker/issues",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/orkestrel/worker.git"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"main": "./dist/src/core/index.cjs",
|
|
28
|
+
"module": "./dist/src/core/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/src/core/index.d.ts",
|
|
33
|
+
"default": "./dist/src/core/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/src/core/index.d.cts",
|
|
37
|
+
"default": "./dist/src/core/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./server": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/src/server/index.d.ts",
|
|
43
|
+
"default": "./dist/src/server/index.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/src/server/index.d.cts",
|
|
47
|
+
"default": "./dist/src/server/index.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./package.json": "./package.json"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"clean": "node -e \"try{require('node:fs').rmSync('dist',{recursive:true,force:true})}catch{}\"",
|
|
57
|
+
"copy": "node -e \"const fs=require('node:fs'),p=require('node:path'),a=process.argv[1],b=process.argv[2];fs.mkdirSync(p.dirname(b),{recursive:true});fs.cpSync(a,b,{force:true});console.log('Copied: '+a+' to '+b)\"",
|
|
58
|
+
"tmp:txt": "node -e \"const fs=require('node:fs'),p=require('node:path');function walk(d){for(const e of fs.readdirSync(d,{withFileTypes:true})){const f=p.join(d,e.name);if(e.isDirectory()){walk(f)}else if(!e.name.endsWith('.md')&&!e.name.endsWith('.txt')){const t=f+'.txt';if(!fs.existsSync(t)){fs.renameSync(f,t)}else{console.warn('Skipping '+f+' — target exists: '+t)}}}}try{walk('tmp')}catch(e){if(e.code!=='ENOENT')throw e}\"",
|
|
59
|
+
"lint": "oxlint --config .oxlintrc.json --fix .",
|
|
60
|
+
"check": "tsc --noEmit --project tsconfig.json",
|
|
61
|
+
"check:src": "npm run check:src:core && npm run check:src:server",
|
|
62
|
+
"check:src:core": "tsc --noEmit -p configs/src/tsconfig.core.json",
|
|
63
|
+
"check:src:server": "tsc --noEmit -p configs/src/tsconfig.server.json",
|
|
64
|
+
"format": "oxfmt --config .oxfmtrc.json --write .",
|
|
65
|
+
"format:check": "oxfmt --config .oxfmtrc.json --check .",
|
|
66
|
+
"lint:check": "oxlint --config .oxlintrc.json .",
|
|
67
|
+
"test": "npm run test:src && npm run test:guides",
|
|
68
|
+
"test:src": "vitest run --config vite.config.ts --no-cache --reporter=dot --project src:core --project src:server",
|
|
69
|
+
"test:src:core": "vitest run --config vite.config.ts --no-cache --reporter=dot --project src:core",
|
|
70
|
+
"test:src:server": "vitest run --config vite.config.ts --no-cache --reporter=dot --project src:server",
|
|
71
|
+
"test:guides": "vitest run --config vite.config.ts --reporter=dot --project guides",
|
|
72
|
+
"build": "npm run clean && npm run build:src",
|
|
73
|
+
"build:src": "npm run build:src:core && npm run build:src:server",
|
|
74
|
+
"build:src:core": "vite build --config configs/src/vite.core.config.ts && npm run copy dist/src/core/index.d.ts dist/src/core/index.d.cts",
|
|
75
|
+
"build:src:server": "vite build --config configs/src/vite.server.config.ts && npm run copy dist/src/server/index.d.ts dist/src/server/index.d.cts",
|
|
76
|
+
"prepublishOnly": "npm run format:check && npm run lint:check && npm run check && npm run check:src && npm run build && npm test"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"@orkestrel/contract": "^0.0.1",
|
|
80
|
+
"@orkestrel/database": "^0.0.2",
|
|
81
|
+
"@orkestrel/emitter": "^0.0.1",
|
|
82
|
+
"@orkestrel/pool": "^0.0.1",
|
|
83
|
+
"@orkestrel/queue": "^0.0.1"
|
|
84
|
+
},
|
|
85
|
+
"devDependencies": {
|
|
86
|
+
"@microsoft/api-extractor": "^7.58.9",
|
|
87
|
+
"@orkestrel/guide": "^0.0.1",
|
|
88
|
+
"@types/node": "^26.1.1",
|
|
89
|
+
"oxfmt": "^0.58.0",
|
|
90
|
+
"oxlint": "^1.73.0",
|
|
91
|
+
"typescript": "^6.0.3",
|
|
92
|
+
"vite": "^8.1.4",
|
|
93
|
+
"vite-plugin-dts": "^5.0.3",
|
|
94
|
+
"vitest": "^4.1.10"
|
|
95
|
+
},
|
|
96
|
+
"engines": {
|
|
97
|
+
"node": ">=24"
|
|
98
|
+
}
|
|
99
|
+
}
|