@vielzeug/familiar 2.2.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -11
- package/dist/_dev.cjs +2 -0
- package/dist/_dev.cjs.map +1 -0
- package/dist/_dev.js +6 -0
- package/dist/_dev.js.map +1 -0
- package/dist/_pool-core.cjs +1 -1
- package/dist/_pool-core.cjs.map +1 -1
- package/dist/_pool-core.d.ts +7 -6
- package/dist/_pool-core.d.ts.map +1 -1
- package/dist/_pool-core.js +80 -67
- package/dist/_pool-core.js.map +1 -1
- package/dist/_pool.cjs +1 -1
- package/dist/_pool.cjs.map +1 -1
- package/dist/_pool.d.ts +8 -5
- package/dist/_pool.d.ts.map +1 -1
- package/dist/_pool.js +116 -109
- package/dist/_pool.js.map +1 -1
- package/dist/_queue.cjs +1 -1
- package/dist/_queue.cjs.map +1 -1
- package/dist/_queue.d.ts +1 -1
- package/dist/_queue.d.ts.map +1 -1
- package/dist/_queue.js +10 -7
- package/dist/_queue.js.map +1 -1
- package/dist/_stream-pool.cjs +1 -1
- package/dist/_stream-pool.cjs.map +1 -1
- package/dist/_stream-pool.d.ts +3 -4
- package/dist/_stream-pool.d.ts.map +1 -1
- package/dist/_stream-pool.js +81 -61
- package/dist/_stream-pool.js.map +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +8 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +16 -6
- package/dist/errors.js.map +1 -1
- package/dist/familiar.cjs +1 -1
- package/dist/familiar.cjs.map +1 -1
- package/dist/familiar.iife.js +1 -1
- package/dist/familiar.iife.js.map +1 -1
- package/dist/familiar.js +1 -1
- package/dist/familiar.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/protocol.cjs +1 -1
- package/dist/protocol.cjs.map +1 -1
- package/dist/protocol.d.ts +1 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +16 -5
- package/dist/protocol.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/testing.cjs +1 -1
- package/dist/testing/testing.cjs.map +1 -1
- package/dist/testing/testing.d.ts +3 -3
- package/dist/testing/testing.d.ts.map +1 -1
- package/dist/testing/testing.js +25 -24
- package/dist/testing/testing.js.map +1 -1
- package/dist/types.d.ts +2 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.cjs +1 -1
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.ts +5 -4
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +49 -38
- package/dist/worker.js.map +1 -1
- package/package.json +10 -8
package/dist/testing/testing.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { FamiliarError as e, FamiliarInvalidOptionsError as t, FamiliarQueueFullError as n, FamiliarRuntimeError as r, FamiliarTaskError as i, FamiliarTerminatedError as a, FamiliarTimeoutError as o } from "../errors.js";
|
|
2
|
-
import {
|
|
2
|
+
import { validateTimeout as s } from "../_pool-core.js";
|
|
3
|
+
import { createPool as c } from "../_pool.js";
|
|
3
4
|
//#region src/testing/testing.ts
|
|
4
|
-
function
|
|
5
|
-
let { concurrency: r = 1, maxQueue:
|
|
6
|
-
if (!Number.isInteger(r) || r < 1) throw new t("`concurrency` must be a positive integer");
|
|
7
|
-
if (
|
|
8
|
-
if (u !==
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
function l(e, n = {}) {
|
|
6
|
+
let { concurrency: r = 1, maxQueue: l, onFull: u = "reject", timeout: d } = n;
|
|
7
|
+
if (!Number.isInteger(r) || r < 1 || r > 512) throw new t("`concurrency` must be a positive integer ≤ 512");
|
|
8
|
+
if (l !== void 0 && (!Number.isInteger(l) || l < 1)) throw new t("`maxQueue` must be a positive integer");
|
|
9
|
+
if (u !== "reject" && u !== "wait") throw new t("`onFull` must be \"reject\" or \"wait\"");
|
|
10
|
+
s(d);
|
|
11
|
+
let f = [];
|
|
12
|
+
function p() {
|
|
11
13
|
let t, n = !1;
|
|
12
14
|
return {
|
|
13
15
|
cancel(e) {
|
|
14
16
|
t?.reject(e), t = void 0;
|
|
15
17
|
},
|
|
16
|
-
prime: () => Promise.resolve(),
|
|
17
18
|
run(r, s, c) {
|
|
18
19
|
if (n) return Promise.reject(new a());
|
|
19
20
|
let l;
|
|
@@ -25,32 +26,32 @@ function c(e, n = {}) {
|
|
|
25
26
|
return new Promise((n, r) => {
|
|
26
27
|
let a = Symbol("task"), s, u = (e, n) => {
|
|
27
28
|
t?.token === a && (t = void 0, s && clearTimeout(s), e(n));
|
|
28
|
-
},
|
|
29
|
-
t?.token === a && (t = void 0, s && clearTimeout(s),
|
|
29
|
+
}, d = (e) => {
|
|
30
|
+
t?.token === a && (t = void 0, s && clearTimeout(s), f.push({
|
|
30
31
|
input: l,
|
|
31
32
|
reason: e,
|
|
32
33
|
status: "rejected"
|
|
33
34
|
}), r(e));
|
|
34
35
|
};
|
|
35
36
|
t = {
|
|
36
|
-
reject:
|
|
37
|
+
reject: d,
|
|
37
38
|
token: a
|
|
38
|
-
}, c !== void 0 && (s = setTimeout(() =>
|
|
39
|
+
}, c !== void 0 && (s = setTimeout(() => d(new o(c)), c)), Promise.resolve().then(() => e(l)).then((e) => {
|
|
39
40
|
let r;
|
|
40
41
|
try {
|
|
41
42
|
r = structuredClone(e);
|
|
42
43
|
} catch (e) {
|
|
43
|
-
|
|
44
|
+
d(new i("Failed to clone task output", { cause: e }));
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
|
-
t?.token === a && (
|
|
47
|
+
t?.token === a && (f.push({
|
|
47
48
|
input: l,
|
|
48
49
|
status: "fulfilled",
|
|
49
50
|
value: r
|
|
50
51
|
}), u(n, r));
|
|
51
52
|
}, (e) => {
|
|
52
53
|
let t = e instanceof Error ? e : Error(String(e));
|
|
53
|
-
|
|
54
|
+
d(new i(t.message, { cause: t }));
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
57
|
},
|
|
@@ -59,18 +60,18 @@ function c(e, n = {}) {
|
|
|
59
60
|
}
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
|
-
let
|
|
63
|
+
let m = c(Array.from({ length: r }, p), {
|
|
63
64
|
concurrency: r,
|
|
64
|
-
defaultTimeout:
|
|
65
|
-
maxQueue:
|
|
66
|
-
onFull:
|
|
65
|
+
defaultTimeout: d,
|
|
66
|
+
maxQueue: l,
|
|
67
|
+
onFull: u
|
|
67
68
|
});
|
|
68
|
-
return Object.defineProperty(
|
|
69
|
+
return Object.defineProperty(m, "calls", {
|
|
69
70
|
enumerable: !0,
|
|
70
|
-
get: () =>
|
|
71
|
-
}),
|
|
71
|
+
get: () => f
|
|
72
|
+
}), m;
|
|
72
73
|
}
|
|
73
74
|
//#endregion
|
|
74
|
-
export { e as FamiliarError, t as FamiliarInvalidOptionsError, n as FamiliarQueueFullError, r as FamiliarRuntimeError, i as FamiliarTaskError, a as FamiliarTerminatedError, o as FamiliarTimeoutError,
|
|
75
|
+
export { e as FamiliarError, t as FamiliarInvalidOptionsError, n as FamiliarQueueFullError, r as FamiliarRuntimeError, i as FamiliarTaskError, a as FamiliarTerminatedError, o as FamiliarTimeoutError, l as createTestWorker };
|
|
75
76
|
|
|
76
77
|
//# sourceMappingURL=testing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testing.js","names":[],"sources":["../../src/testing/testing.ts"],"sourcesContent":["import { createPool } from '../_pool';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from '../errors';\nimport type { SlotStrategy, WorkerOptions, WorkerPool } from '../types';\n\nexport type TestWorkerOptions = Omit<WorkerOptions, 'concurrency' | 'onSlotError'> & { concurrency?: number };\n\nexport type TestWorkerCall<TInput, TOutput> =\n | { input: TInput; status: 'fulfilled'; value: TOutput }\n | { input: TInput; reason: unknown; status: 'rejected' };\n\nexport type TestWorkerHandle<TInput, TOutput> = WorkerPool<TInput, TOutput> & {\n readonly calls: ReadonlyArray<TestWorkerCall<TInput, TOutput>>;\n};\n\nexport function createTestWorker<TInput, TOutput>(\n handler: (input: TInput) => TOutput | Promise<TOutput>,\n options: TestWorkerOptions = {},\n): TestWorkerHandle<TInput, TOutput> {\n const { concurrency = 1, maxQueue, onFull = 'reject', timeout } = options;\n\n if (!Number.isInteger(concurrency) || concurrency < 1) {\n throw new FamiliarInvalidOptionsError(
|
|
1
|
+
{"version":3,"file":"testing.js","names":[],"sources":["../../src/testing/testing.ts"],"sourcesContent":["import { createPool } from '../_pool.js';\nimport { MAX_CONCURRENCY, validateTimeout } from '../_pool-core.js';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from '../errors.js';\nimport type { SlotStrategy, WorkerOptions, WorkerPool } from '../types.js';\n\nexport type TestWorkerOptions = Omit<WorkerOptions, 'concurrency' | 'onSlotError'> & { concurrency?: number };\n\nexport type TestWorkerCall<TInput, TOutput> =\n | { input: TInput; status: 'fulfilled'; value: TOutput }\n | { input: TInput; reason: unknown; status: 'rejected' };\n\nexport type TestWorkerHandle<TInput, TOutput> = WorkerPool<TInput, TOutput> & {\n readonly calls: ReadonlyArray<TestWorkerCall<TInput, TOutput>>;\n};\n\nexport function createTestWorker<TInput, TOutput>(\n handler: (input: TInput) => TOutput | Promise<TOutput>,\n options: TestWorkerOptions = {},\n): TestWorkerHandle<TInput, TOutput> {\n const { concurrency = 1, maxQueue, onFull = 'reject', timeout } = options;\n\n if (!Number.isInteger(concurrency) || concurrency < 1 || concurrency > MAX_CONCURRENCY) {\n throw new FamiliarInvalidOptionsError(`\\`concurrency\\` must be a positive integer ≤ ${MAX_CONCURRENCY}`);\n }\n\n if (maxQueue !== undefined && (!Number.isInteger(maxQueue) || maxQueue < 1)) {\n throw new FamiliarInvalidOptionsError('`maxQueue` must be a positive integer');\n }\n\n if (onFull !== 'reject' && onFull !== 'wait') {\n throw new FamiliarInvalidOptionsError('`onFull` must be \"reject\" or \"wait\"');\n }\n\n validateTimeout(timeout);\n\n const calls: TestWorkerCall<TInput, TOutput>[] = [];\n\n function makeSlot(): SlotStrategy<TInput, TOutput> {\n let current: { reject(reason: unknown): void; token: symbol } | undefined;\n let terminated = false;\n\n return {\n cancel(reason: unknown): void {\n current?.reject(reason);\n current = undefined;\n },\n run(input, transferables, timeoutMs): Promise<TOutput> {\n if (terminated) return Promise.reject(new FamiliarTerminatedError());\n\n let clonedInput: TInput;\n\n try {\n clonedInput = structuredClone(input, { transfer: transferables });\n } catch (error) {\n return Promise.reject(new FamiliarTaskError('Failed to clone task input', { cause: error }));\n }\n\n return new Promise<TOutput>((resolve, reject) => {\n const token = Symbol('task');\n let timer: ReturnType<typeof setTimeout> | undefined;\n const settle = (fn: (value: TOutput) => void, value: TOutput): void => {\n if (current?.token !== token) return;\n\n current = undefined;\n\n if (timer) clearTimeout(timer);\n\n fn(value);\n };\n const rejectTask = (reason: unknown): void => {\n if (current?.token !== token) return;\n\n current = undefined;\n\n if (timer) clearTimeout(timer);\n\n calls.push({ input: clonedInput, reason, status: 'rejected' });\n reject(reason);\n };\n\n current = { reject: rejectTask, token };\n\n if (timeoutMs !== undefined) {\n timer = setTimeout(() => rejectTask(new FamiliarTimeoutError(timeoutMs)), timeoutMs);\n }\n\n void Promise.resolve()\n .then(() => handler(clonedInput))\n .then(\n (output) => {\n let clonedOutput: TOutput;\n\n try {\n clonedOutput = structuredClone(output);\n } catch (error) {\n rejectTask(new FamiliarTaskError('Failed to clone task output', { cause: error }));\n\n return;\n }\n\n if (current?.token !== token) return;\n\n calls.push({ input: clonedInput, status: 'fulfilled', value: clonedOutput });\n settle(resolve, clonedOutput);\n },\n (error: unknown) => {\n const cause = error instanceof Error ? error : new Error(String(error));\n\n rejectTask(new FamiliarTaskError(cause.message, { cause }));\n },\n );\n });\n },\n terminate(): void {\n terminated = true;\n current?.reject(new FamiliarTerminatedError());\n current = undefined;\n },\n };\n }\n\n const pool = createPool(Array.from({ length: concurrency }, makeSlot), {\n concurrency,\n defaultTimeout: timeout,\n maxQueue,\n onFull,\n });\n\n Object.defineProperty(pool, 'calls', {\n enumerable: true,\n get: () => calls as ReadonlyArray<TestWorkerCall<TInput, TOutput>>,\n });\n\n return pool as TestWorkerHandle<TInput, TOutput>;\n}\n\nexport {\n FamiliarError,\n FamiliarInvalidOptionsError,\n FamiliarQueueFullError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from '../errors.js';\nexport type { WorkerPool } from '../types.js';\n"],"mappings":";;;;AAoBA,SAAgB,EACd,GACA,IAA6B,CAAC,GACK;CACnC,IAAM,EAAE,iBAAc,GAAG,aAAU,YAAS,UAAU,eAAY;CAElE,IAAI,CAAC,OAAO,UAAU,CAAW,KAAK,IAAc,KAAK,IAAA,KACvD,MAAM,IAAI,EAA4B,gDAAiE;CAGzG,IAAI,MAAa,KAAA,MAAc,CAAC,OAAO,UAAU,CAAQ,KAAK,IAAW,IACvE,MAAM,IAAI,EAA4B,uCAAuC;CAG/E,IAAI,MAAW,YAAY,MAAW,QACpC,MAAM,IAAI,EAA4B,yCAAqC;CAG7E,EAAgB,CAAO;CAEvB,IAAM,IAA2C,CAAC;CAElD,SAAS,IAA0C;EACjD,IAAI,GACA,IAAa;EAEjB,OAAO;GACL,OAAO,GAAuB;IAE5B,AADA,GAAS,OAAO,CAAM,GACtB,IAAU,KAAA;GACZ;GACA,IAAI,GAAO,GAAe,GAA6B;IACrD,IAAI,GAAY,OAAO,QAAQ,OAAO,IAAI,EAAwB,CAAC;IAEnE,IAAI;IAEJ,IAAI;KACF,IAAc,gBAAgB,GAAO,EAAE,UAAU,EAAc,CAAC;IAClE,SAAS,GAAO;KACd,OAAO,QAAQ,OAAO,IAAI,EAAkB,8BAA8B,EAAE,OAAO,EAAM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OALA,OAAO,eAAe,GAAM,SAAS;EACnC,YAAY;EACZ,WAAW;CACb,CAAC,GAEM;AACT"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FamiliarRuntimeError } from './errors';
|
|
1
|
+
import type { FamiliarRuntimeError } from './errors.js';
|
|
2
2
|
export type WorkerStatus = 'idle' | 'running' | 'terminated';
|
|
3
3
|
export type WorkerStats = {
|
|
4
4
|
readonly active: number;
|
|
@@ -26,7 +26,7 @@ export type RunOptions = {
|
|
|
26
26
|
/** Per-operation timeout in milliseconds. Overrides WorkerOptions.timeout. */
|
|
27
27
|
timeout?: number;
|
|
28
28
|
/** Values transferred to the worker instead of structured-cloned. */
|
|
29
|
-
transferables?: Transferable[];
|
|
29
|
+
transferables?: readonly Transferable[];
|
|
30
30
|
};
|
|
31
31
|
export type DrainOptions = {
|
|
32
32
|
/** Maximum time to wait before terminating remaining work. */
|
|
@@ -37,7 +37,6 @@ export interface PoolBase {
|
|
|
37
37
|
dispose(): void;
|
|
38
38
|
readonly disposed: boolean;
|
|
39
39
|
drain(options?: DrainOptions): Promise<void>;
|
|
40
|
-
prime(): Promise<void>;
|
|
41
40
|
readonly stats: WorkerStats;
|
|
42
41
|
readonly status: WorkerStatus;
|
|
43
42
|
[Symbol.asyncDispose](): Promise<void>;
|
|
@@ -49,20 +48,8 @@ export interface WorkerPool<TInput, TOutput> extends PoolBase {
|
|
|
49
48
|
export interface StreamWorkerPool<TInput, TChunk> extends PoolBase {
|
|
50
49
|
runStream(input: TInput, options?: RunOptions): AsyncIterable<TChunk>;
|
|
51
50
|
}
|
|
52
|
-
export type TaskGroup<TInput, TOutput> = {
|
|
53
|
-
abort(reason?: unknown): void;
|
|
54
|
-
drain(): Promise<PromiseSettledResult<TOutput>[]>;
|
|
55
|
-
readonly name: string | undefined;
|
|
56
|
-
readonly pending: number;
|
|
57
|
-
run(input: TInput, options?: Omit<RunOptions, 'signal'>): Promise<TOutput>;
|
|
58
|
-
readonly size: number;
|
|
59
|
-
};
|
|
60
|
-
export type TaskGroupOptions = {
|
|
61
|
-
signal?: AbortSignal;
|
|
62
|
-
};
|
|
63
51
|
export type SlotStrategy<TInput, TOutput> = {
|
|
64
52
|
cancel(reason: unknown): void;
|
|
65
|
-
prime(): Promise<void>;
|
|
66
53
|
run(input: TInput, transferables: Transferable[], timeout: number | undefined): Promise<TOutput>;
|
|
67
54
|
terminate(): void;
|
|
68
55
|
};
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,YAAY,CAAC;AAE7D,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,4FAA4F;IAC5F,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9B,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,MAAM,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,0FAA0F;IAC1F,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,CAAC;IACpD,+FAA+F;IAC/F,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,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E,OAAO,GAAG,IAAI,CAAC;IAC9B,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACjG,SAAS,IAAI,IAAI,CAAC;CACnB,CAAC"}
|
package/dist/worker.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("./_timers.cjs"),t=require("./errors.cjs"),n=require("./_pool.cjs"),r=require("./_stream-pool.cjs");require("./protocol.cjs");
|
|
1
|
+
const e=require("./_timers.cjs"),t=require("./errors.cjs"),n=require("./_pool-core.cjs"),r=require("./_pool.cjs");require("./_dev.cjs");const i=require("./_stream-pool.cjs");require("./protocol.cjs");function a(e={}){let{concurrency:r=1,maxQueue:i,onFull:a=`reject`,onSlotError:o,timeout:s}=e,c=r===`auto`?Math.min(512,Math.max(1,globalThis.navigator?.hardwareConcurrency??1)):r;if(!Number.isInteger(c)||c<1||c>512)throw new t.FamiliarInvalidOptionsError('`concurrency` must be a positive integer ≤ 512 or "auto"');if(i!==void 0&&(!Number.isInteger(i)||i<1))throw new t.FamiliarInvalidOptionsError("`maxQueue` must be a positive integer");if(a!==`reject`&&a!==`wait`)throw new t.FamiliarInvalidOptionsError('`onFull` must be "reject" or "wait"');return n.validateTimeout(s),{concurrency:c,maxQueue:i,onFull:a,onSlotError:o,timeout:s}}function o(e){if(typeof e!=`object`||!e)return!1;let t=e;return t.version!==1||typeof t.id!=`number`||!Number.isSafeInteger(t.id)||t.id<0?!1:t.kind===`chunk`||t.kind===`result`?`value`in t:t.kind!==`error`||typeof t.error!=`object`||t.error===null?!1:(t.error.category===void 0||t.error.category===`protocol`)&&typeof t.error.message==`string`&&typeof t.error.name==`string`&&(t.error.stack===void 0||typeof t.error.stack==`string`)}function s(e){let n=Error(e.message);return n.name=e.name,n.stack=e.stack,e.category===`protocol`?new t.FamiliarRuntimeError(e.message,{cause:n}):new t.FamiliarTaskError(e.message,{cause:n})}var c=class{#e;#t;#n=!1;#r;#i=0;#a;constructor(e,t){this.#t=e,this.#e=t}cancel(e){this.#r&&(this.#c(),this.#d(`reject`,e))}run(e,t,n){return this.#o(e,t,n,`run`)}stream(e,n){let r=()=>this.cancel(new t.FamiliarTerminatedError(`Stream consumer stopped`)),i=[],a=[],o=!1,s,c=e=>{let t=a.shift();t?t({done:!1,value:e}):i.push(e)},l=e=>{for(o=!0,s=e;a.length>0;)a.shift()(e===void 0?{done:!0,value:void 0}:Promise.reject(e))};return{done:this.#o(e,[...n.transferables??[]],n.timeout,`stream`,c).then(()=>l(),e=>l(e)),iterable:{[Symbol.asyncIterator]:()=>({async next(){if(i.length>0)return{done:!1,value:i.shift()};if(s!==void 0)throw s;return o?{done:!0,value:void 0}:new Promise((e,t)=>{a.push(n=>{n instanceof Promise?n.then(e,t):e(n)})})},async return(){return r(),{done:!0,value:void 0}}})}}}terminate(){this.#n=!0,this.#c(),this.#d(`reject`,new t.FamiliarTerminatedError)}#o(n,r,i,a,o){if(this.#n)return Promise.reject(new t.FamiliarTerminatedError);if(this.#r)return Promise.reject(new t.FamiliarRuntimeError(`Worker slot is already busy`));try{let s=this.#s(),c=this.#i;return this.#i=c===2**53-1?0:c+1,new Promise((l,u)=>{let d={emit:o,id:c,kind:a,reject:u,resolve:l};this.#r=d,i!==void 0&&(d.timer=setTimeout(()=>this.cancel(new t.FamiliarTimeoutError(i)),i),e.unrefTimer(d.timer));try{s.postMessage({id:c,input:n,kind:a,version:1},r)}catch(e){this.cancel(new t.FamiliarRuntimeError(`Failed to post message to worker`,{cause:e}))}})}catch(e){return Promise.reject(e)}}#s(){if(this.#a)return this.#a;if(typeof Worker>`u`)throw new t.FamiliarRuntimeError(`Worker API is unavailable in this runtime`);try{let e=new Worker(this.#t,{type:`module`});return e.onmessage=t=>{this.#a===e&&this.#u(t.data)},e.onerror=n=>this.#l(e,new t.FamiliarRuntimeError(n.message||`Worker failed`)),e.onmessageerror=()=>this.#l(e,new t.FamiliarRuntimeError(`Worker response could not be deserialized`)),this.#a=e,e}catch(e){throw new t.FamiliarRuntimeError(`Failed to create Worker`,{cause:e})}}#c(){let e=this.#a;this.#a=void 0,e&&(e.onmessage=null,e.onerror=null,e.onmessageerror=null,e.terminate())}#l(e,t){if(this.#a===e){this.#c(),this.#d(`reject`,t);try{this.#e?.(t)}catch{}}}#u(e){if(this.#r){if(!o(e)||e.id!==this.#r.id||this.#r.kind===`run`&&e.kind===`chunk`){this.cancel(new t.FamiliarRuntimeError(`Worker returned an incompatible protocol response`));return}if(e.kind===`chunk`){this.#r.emit?.(e.value);return}if(e.kind===`error`){this.#d(`reject`,s(e.error));return}this.#d(`resolve`,e.value)}}#d(e,t){let n=this.#r;n&&(this.#r=void 0,n.timer&&clearTimeout(n.timer),e===`resolve`?n.resolve(t):n.reject(t))}};function l(e,t){let n=typeof e==`string`?e:e.href;return Array.from({length:t.concurrency},()=>new c(n,t.onSlotError))}function u(e,t={}){let n=a(t);return r.createPool(l(e,n),{concurrency:n.concurrency,defaultTimeout:n.timeout,maxQueue:n.maxQueue,onFull:n.onFull})}function d(e,t={}){let n=a(t);return i.createStreamPool(l(e,n),{concurrency:n.concurrency,defaultTimeout:n.timeout,maxQueue:n.maxQueue,onFull:n.onFull})}exports.FamiliarError=t.FamiliarError,exports.FamiliarInvalidOptionsError=t.FamiliarInvalidOptionsError,exports.FamiliarQueueFullError=t.FamiliarQueueFullError,exports.FamiliarRuntimeError=t.FamiliarRuntimeError,exports.FamiliarTaskError=t.FamiliarTaskError,exports.FamiliarTerminatedError=t.FamiliarTerminatedError,exports.FamiliarTimeoutError=t.FamiliarTimeoutError,exports.createStreamWorker=d,exports.createWorker=u,exports.runBatch=r.runBatch;
|
|
2
2
|
//# sourceMappingURL=worker.cjs.map
|
package/dist/worker.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.cjs","names":["#onSlotError","#url","#worker","#settlePending","#disposed","#ensureWorker","#dispatch","#pending","#taskId","#onMessage"],"sources":["../src/worker.ts"],"sourcesContent":["export { batch, createTaskGroup } from './_pool';\nexport {\n FamiliarError,\n FamiliarInvalidOptionsError,\n FamiliarQueueFullError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors';\nexport type {\n DrainOptions,\n PoolBase,\n RunOptions,\n StreamWorkerPool,\n TaskGroup,\n TaskGroupOptions,\n WorkerOptions,\n WorkerPool,\n WorkerStats,\n WorkerStatus,\n} from './types';\n\nimport { createPool } from './_pool';\nimport { createStreamPool, type StreamSlot } from './_stream-pool';\nimport { unrefTimer } from './_timers';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors';\nimport type { SerializedError, WorkerResponse } from './protocol';\nimport { PROTOCOL_VERSION } from './protocol';\nimport type { RunOptions, SlotStrategy, StreamWorkerPool, WorkerOptions, WorkerPool } from './types';\n\nconst MAX_CONCURRENCY = 512;\n\ntype ResolvedOptions = {\n concurrency: number;\n maxQueue: number | undefined;\n onFull: 'reject' | 'wait';\n onSlotError: WorkerOptions['onSlotError'];\n timeout: number | undefined;\n};\n\ntype Pending<TOutput> = {\n emit?: (value: TOutput) => void;\n id: number;\n reject: (reason: unknown) => void;\n resolve: (value: TOutput) => void;\n timer?: ReturnType<typeof setTimeout>;\n};\n\nexport type RunningStream<TChunk> = {\n done: Promise<void>;\n iterable: AsyncIterable<TChunk>;\n};\n\nfunction resolveOptions(options: WorkerOptions = {}): ResolvedOptions {\n const { concurrency = 1, maxQueue, onFull = 'reject', onSlotError, timeout } = options;\n const resolvedConcurrency =\n concurrency === 'auto' ? Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1) : concurrency;\n\n if (!Number.isInteger(resolvedConcurrency) || resolvedConcurrency < 1 || resolvedConcurrency > MAX_CONCURRENCY) {\n throw new FamiliarInvalidOptionsError(`\\`concurrency\\` must be a positive integer ≤ ${MAX_CONCURRENCY} or \"auto\"`);\n }\n\n if (maxQueue !== undefined && (!Number.isInteger(maxQueue) || maxQueue < 1)) {\n throw new FamiliarInvalidOptionsError('`maxQueue` must be a positive integer');\n }\n\n if (timeout !== undefined && (!Number.isFinite(timeout) || timeout <= 0)) {\n throw new FamiliarInvalidOptionsError('`timeout` must be a finite number greater than 0');\n }\n\n return { concurrency: resolvedConcurrency, maxQueue, onFull, onSlotError, timeout };\n}\n\nfunction isWorkerResponse<TOutput>(value: unknown): value is WorkerResponse<TOutput> {\n if (typeof value !== 'object' || value === null) return false;\n\n const response = value as Partial<WorkerResponse<TOutput>>;\n\n return (\n response.version === PROTOCOL_VERSION &&\n typeof response.id === 'number' &&\n (response.kind === 'chunk' || response.kind === 'error' || response.kind === 'result')\n );\n}\n\nfunction taskError(error: SerializedError): FamiliarTaskError {\n const cause = new Error(error.message);\n\n cause.name = error.name;\n cause.stack = error.stack;\n\n return new FamiliarTaskError(error.message, { cause });\n}\n\nclass Slot<TInput, TOutput> implements SlotStrategy<TInput, TOutput>, StreamSlot<TInput, TOutput> {\n readonly #onSlotError: WorkerOptions['onSlotError'];\n readonly #url: URL | string;\n #disposed = false;\n #pending: Pending<TOutput> | undefined;\n #taskId = 0;\n #worker: Worker | undefined;\n\n constructor(url: URL | string, onSlotError: WorkerOptions['onSlotError']) {\n this.#url = url;\n this.#onSlotError = onSlotError;\n }\n\n cancel(reason: unknown): void {\n this.#worker?.terminate();\n this.#worker = undefined;\n this.#settlePending('reject', reason);\n }\n\n prime(): Promise<void> {\n if (!this.#disposed) this.#ensureWorker();\n\n return Promise.resolve();\n }\n\n run(input: TInput, transferables: Transferable[], timeout: number | undefined): Promise<TOutput> {\n return this.#dispatch(input, transferables, timeout, 'run') as Promise<TOutput>;\n }\n\n stream(input: TInput, options: RunOptions): RunningStream<TOutput> {\n const stop = () => this.cancel(new FamiliarTerminatedError('Stream consumer stopped'));\n const chunks: TOutput[] = [];\n const waiters: Array<(result: IteratorResult<TOutput>) => void> = [];\n let done = false;\n let error: unknown;\n const emit = (value: TOutput) => {\n const waiter = waiters.shift();\n\n if (waiter) waiter({ done: false, value });\n else chunks.push(value);\n };\n const finish = (reason?: unknown) => {\n done = true;\n error = reason;\n\n while (waiters.length > 0) {\n const waiter = waiters.shift()!;\n\n if (reason !== undefined) waiter(Promise.reject(reason) as never);\n else waiter({ done: true, value: undefined as never });\n }\n };\n const donePromise = this.#dispatch(input, options.transferables ?? [], options.timeout, 'stream', emit).then(\n () => finish(),\n (reason: unknown) => finish(reason),\n );\n\n return {\n done: donePromise,\n iterable: {\n [Symbol.asyncIterator]: () => ({\n async next(): Promise<IteratorResult<TOutput>> {\n if (chunks.length > 0) return { done: false, value: chunks.shift()! };\n\n if (error !== undefined) throw error;\n\n if (done) return { done: true, value: undefined as never };\n\n return new Promise<IteratorResult<TOutput>>((resolve, reject) => {\n waiters.push((result) => {\n if (result instanceof Promise) void result.then(resolve, reject);\n else resolve(result);\n });\n });\n },\n async return(): Promise<IteratorResult<TOutput>> {\n stop();\n\n return { done: true, value: undefined as never };\n },\n }),\n },\n };\n }\n\n terminate(): void {\n this.#disposed = true;\n this.cancel(new FamiliarTerminatedError());\n }\n\n #dispatch(\n input: TInput,\n transferables: Transferable[],\n timeout: number | undefined,\n kind: 'run' | 'stream',\n emit?: (value: TOutput) => void,\n ): Promise<TOutput> {\n if (this.#disposed) return Promise.reject(new FamiliarTerminatedError());\n\n if (this.#pending) return Promise.reject(new FamiliarRuntimeError('Worker slot is already busy'));\n\n try {\n const worker = this.#ensureWorker();\n const id = this.#taskId++;\n\n return new Promise<TOutput>((resolve, reject) => {\n const pending: Pending<TOutput> = { emit, id, reject, resolve };\n\n this.#pending = pending;\n\n if (timeout !== undefined) {\n pending.timer = setTimeout(() => this.cancel(new FamiliarTimeoutError(timeout)), timeout);\n unrefTimer(pending.timer);\n }\n\n try {\n worker.postMessage({ id, input, kind, version: PROTOCOL_VERSION }, transferables);\n } catch (error) {\n this.cancel(new FamiliarRuntimeError('Failed to post message to worker', { cause: error }));\n }\n });\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #ensureWorker(): Worker {\n if (this.#worker) return this.#worker;\n\n if (typeof Worker === 'undefined') throw new FamiliarRuntimeError('Worker API is unavailable in this runtime');\n\n try {\n const worker = new Worker(this.#url, { type: 'module' });\n\n worker.onmessage = (event: MessageEvent<unknown>) => this.#onMessage(event.data);\n worker.onerror = (event) => {\n const error = new FamiliarRuntimeError(event.message || 'Worker failed');\n\n this.#worker = undefined;\n this.#onSlotError?.(error);\n this.#settlePending('reject', error);\n };\n this.#worker = worker;\n\n return worker;\n } catch (error) {\n throw new FamiliarRuntimeError('Failed to create Worker', { cause: error });\n }\n }\n\n #onMessage(message: unknown): void {\n if (!this.#pending) return;\n\n if (!isWorkerResponse<TOutput>(message) || message.id !== this.#pending.id) {\n this.cancel(new FamiliarRuntimeError('Worker returned an incompatible protocol response'));\n\n return;\n }\n\n if (message.kind === 'chunk') {\n this.#pending.emit?.(message.value);\n\n return;\n }\n\n if (message.kind === 'error') {\n this.#settlePending('reject', taskError(message.error));\n\n return;\n }\n\n this.#settlePending('resolve', message.value);\n }\n\n #settlePending(kind: 'resolve' | 'reject', value: unknown): void {\n const pending = this.#pending;\n\n if (!pending) return;\n\n this.#pending = undefined;\n\n if (pending.timer) clearTimeout(pending.timer);\n\n if (kind === 'resolve') pending.resolve(value as TOutput);\n else pending.reject(value);\n }\n}\n\nfunction slots<TInput, TOutput>(url: URL | string, options: ResolvedOptions): Slot<TInput, TOutput>[] {\n return Array.from({ length: options.concurrency }, () => new Slot<TInput, TOutput>(url, options.onSlotError));\n}\n\n/** Create a pool backed by an ES module worker registered with exposeTask(). */\nexport function createWorker<TInput, TOutput>(\n url: URL | string,\n options: WorkerOptions = {},\n): WorkerPool<TInput, TOutput> {\n const resolved = resolveOptions(options);\n\n return createPool(slots<TInput, TOutput>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n\n/** Create a stream-only pool backed by an ES module worker registered with exposeStream(). */\nexport function createStreamWorker<TInput, TChunk>(\n url: URL | string,\n options: WorkerOptions = {},\n): StreamWorkerPool<TInput, TChunk> {\n const resolved = resolveOptions(options);\n\n return createStreamPool(slots<TInput, TChunk>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n"],"mappings":"8IAqCA,IAAM,EAAkB,IAuBxB,SAAS,EAAe,EAAyB,CAAC,EAAoB,CACpE,GAAM,CAAE,cAAc,EAAG,WAAU,SAAS,SAAU,cAAa,WAAY,EACzE,EACJ,IAAgB,OAAS,KAAK,IAAI,EAAG,WAAW,WAAW,qBAAuB,CAAC,EAAI,EAEzF,GAAI,CAAC,OAAO,UAAU,CAAmB,GAAK,EAAsB,GAAK,EAAsB,EAC7F,MAAM,IAAI,EAAA,4BAA4B,gDAAgD,EAAgB,WAAW,EAGnH,GAAI,IAAa,IAAA,KAAc,CAAC,OAAO,UAAU,CAAQ,GAAK,EAAW,GACvE,MAAM,IAAI,EAAA,4BAA4B,uCAAuC,EAG/E,GAAI,IAAY,IAAA,KAAc,CAAC,OAAO,SAAS,CAAO,GAAK,GAAW,GACpE,MAAM,IAAI,EAAA,4BAA4B,kDAAkD,EAG1F,MAAO,CAAE,YAAa,EAAqB,WAAU,SAAQ,cAAa,SAAQ,CACpF,CAEA,SAAS,EAA0B,EAAkD,CACnF,GAAI,OAAO,GAAU,WAAY,EAAgB,MAAO,GAExD,IAAM,EAAW,EAEjB,OACE,EAAS,UAAA,GACT,OAAO,EAAS,IAAO,WACtB,EAAS,OAAS,SAAW,EAAS,OAAS,SAAW,EAAS,OAAS,SAEjF,CAEA,SAAS,EAAU,EAA2C,CAC5D,IAAM,EAAY,MAAM,EAAM,OAAO,EAKrC,MAHA,GAAM,KAAO,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,CAAC,CAC9G,CAGA,SAAgB,EACd,EACA,EAAyB,CAAC,EACG,CAC7B,IAAM,EAAW,EAAe,CAAO,EAEvC,OAAO,EAAA,WAAW,EAAuB,EAAK,CAAQ,EAAG,CACvD,YAAa,EAAS,YACtB,eAAgB,EAAS,QACzB,SAAU,EAAS,SACnB,OAAQ,EAAS,MACnB,CAAC,CACH,CAGA,SAAgB,EACd,EACA,EAAyB,CAAC,EACQ,CAClC,IAAM,EAAW,EAAe,CAAO,EAEvC,OAAO,EAAA,iBAAiB,EAAsB,EAAK,CAAQ,EAAG,CAC5D,YAAa,EAAS,YACtB,eAAgB,EAAS,QACzB,SAAU,EAAS,SACnB,OAAQ,EAAS,MACnB,CAAC,CACH"}
|
|
1
|
+
{"version":3,"file":"worker.cjs","names":[],"sources":["../src/worker.ts"],"sourcesContent":["export type { BatchOptions } from './_pool.js';\nexport { runBatch } from './_pool.js';\nexport {\n FamiliarError,\n FamiliarInvalidOptionsError,\n FamiliarQueueFullError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors.js';\nexport type {\n DrainOptions,\n PoolBase,\n RunOptions,\n StreamWorkerPool,\n WorkerOptions,\n WorkerPool,\n WorkerStats,\n WorkerStatus,\n} from './types.js';\n\nimport { warn } from './_dev.js';\nimport { createPool } from './_pool.js';\nimport { MAX_CONCURRENCY, validateTimeout } from './_pool-core.js';\nimport { createStreamPool, type StreamSlot } from './_stream-pool.js';\nimport { unrefTimer } from './_timers.js';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors.js';\nimport type { SerializedError, WorkerResponse } from './protocol.js';\nimport { PROTOCOL_VERSION } from './protocol.js';\nimport type { RunOptions, SlotStrategy, StreamWorkerPool, WorkerOptions, WorkerPool } from './types.js';\n\ntype ResolvedOptions = {\n concurrency: number;\n maxQueue: number | undefined;\n onFull: 'reject' | 'wait';\n onSlotError: WorkerOptions['onSlotError'];\n timeout: number | undefined;\n};\n\ntype Pending<TOutput> = {\n emit?: (value: TOutput) => void;\n id: number;\n kind: 'run' | 'stream';\n reject: (reason: unknown) => void;\n resolve: (value: TOutput) => void;\n timer?: ReturnType<typeof setTimeout>;\n};\n\nexport type RunningStream<TChunk> = {\n done: Promise<void>;\n iterable: AsyncIterable<TChunk>;\n};\n\nfunction resolveOptions(options: WorkerOptions = {}): ResolvedOptions {\n const { concurrency = 1, maxQueue, onFull = 'reject', onSlotError, timeout } = options;\n const resolvedConcurrency =\n concurrency === 'auto'\n ? Math.min(MAX_CONCURRENCY, Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1))\n : concurrency;\n\n if (!Number.isInteger(resolvedConcurrency) || resolvedConcurrency < 1 || resolvedConcurrency > MAX_CONCURRENCY) {\n throw new FamiliarInvalidOptionsError(`\\`concurrency\\` must be a positive integer ≤ ${MAX_CONCURRENCY} or \"auto\"`);\n }\n\n if (maxQueue !== undefined && (!Number.isInteger(maxQueue) || maxQueue < 1)) {\n throw new FamiliarInvalidOptionsError('`maxQueue` must be a positive integer');\n }\n\n if (onFull !== 'reject' && onFull !== 'wait') {\n throw new FamiliarInvalidOptionsError('`onFull` must be \"reject\" or \"wait\"');\n }\n\n validateTimeout(timeout);\n\n return { concurrency: resolvedConcurrency, maxQueue, onFull, onSlotError, timeout };\n}\n\nfunction isWorkerResponse<TOutput>(value: unknown): value is WorkerResponse<TOutput> {\n if (typeof value !== 'object' || value === null) return false;\n\n const response = value as Partial<WorkerResponse<TOutput>>;\n if (\n response.version !== PROTOCOL_VERSION ||\n typeof response.id !== 'number' ||\n !Number.isSafeInteger(response.id) ||\n response.id < 0\n )\n return false;\n\n if (response.kind === 'chunk' || response.kind === 'result') return 'value' in response;\n if (response.kind !== 'error' || typeof response.error !== 'object' || response.error === null) return false;\n\n return (\n (response.error.category === undefined || response.error.category === 'protocol') &&\n typeof response.error.message === 'string' &&\n typeof response.error.name === 'string' &&\n (response.error.stack === undefined || typeof response.error.stack === 'string')\n );\n}\n\nfunction responseError(error: SerializedError): FamiliarRuntimeError | FamiliarTaskError {\n const cause = new Error(error.message);\n\n cause.name = error.name;\n cause.stack = error.stack;\n\n return error.category === 'protocol'\n ? new FamiliarRuntimeError(error.message, { cause })\n : new FamiliarTaskError(error.message, { cause });\n}\n\nclass Slot<TInput, TOutput> implements SlotStrategy<TInput, TOutput>, StreamSlot<TInput, TOutput> {\n readonly #onSlotError: WorkerOptions['onSlotError'];\n readonly #url: URL | string;\n #disposed = false;\n #pending: Pending<TOutput> | undefined;\n #taskId = 0;\n #worker: Worker | undefined;\n\n constructor(url: URL | string, onSlotError: WorkerOptions['onSlotError']) {\n this.#url = url;\n this.#onSlotError = onSlotError;\n }\n\n cancel(reason: unknown): void {\n if (!this.#pending) return;\n this.#stopWorker();\n this.#settlePending('reject', reason);\n }\n\n run(input: TInput, transferables: Transferable[], timeout: number | undefined): Promise<TOutput> {\n return this.#dispatch(input, transferables, timeout, 'run') as Promise<TOutput>;\n }\n\n stream(input: TInput, options: RunOptions): RunningStream<TOutput> {\n const stop = () => this.cancel(new FamiliarTerminatedError('Stream consumer stopped'));\n const chunks: TOutput[] = [];\n const waiters: Array<(result: IteratorResult<TOutput>) => void> = [];\n let done = false;\n let error: unknown;\n const emit = (value: TOutput) => {\n const waiter = waiters.shift();\n\n if (waiter) waiter({ done: false, value });\n else chunks.push(value);\n };\n const finish = (reason?: unknown) => {\n done = true;\n error = reason;\n\n while (waiters.length > 0) {\n const waiter = waiters.shift()!;\n\n if (reason !== undefined) waiter(Promise.reject(reason) as never);\n else waiter({ done: true, value: undefined as never });\n }\n };\n const donePromise = this.#dispatch(input, [...(options.transferables ?? [])], options.timeout, 'stream', emit).then(\n () => finish(),\n (reason: unknown) => finish(reason),\n );\n\n return {\n done: donePromise,\n iterable: {\n [Symbol.asyncIterator]: () => ({\n async next(): Promise<IteratorResult<TOutput>> {\n if (chunks.length > 0) return { done: false, value: chunks.shift()! };\n\n if (error !== undefined) throw error;\n\n if (done) return { done: true, value: undefined as never };\n\n return new Promise<IteratorResult<TOutput>>((resolve, reject) => {\n waiters.push((result) => {\n if (result instanceof Promise) void result.then(resolve, reject);\n else resolve(result);\n });\n });\n },\n async return(): Promise<IteratorResult<TOutput>> {\n stop();\n\n return { done: true, value: undefined as never };\n },\n }),\n },\n };\n }\n\n terminate(): void {\n this.#disposed = true;\n this.#stopWorker();\n this.#settlePending('reject', new FamiliarTerminatedError());\n }\n\n #dispatch(\n input: TInput,\n transferables: Transferable[],\n timeout: number | undefined,\n kind: 'run' | 'stream',\n emit?: (value: TOutput) => void,\n ): Promise<TOutput> {\n if (this.#disposed) return Promise.reject(new FamiliarTerminatedError());\n\n if (this.#pending) return Promise.reject(new FamiliarRuntimeError('Worker slot is already busy'));\n\n try {\n const worker = this.#ensureWorker();\n const id = this.#taskId;\n this.#taskId = id === Number.MAX_SAFE_INTEGER ? 0 : id + 1;\n\n return new Promise<TOutput>((resolve, reject) => {\n const pending: Pending<TOutput> = { emit, id, kind, reject, resolve };\n\n this.#pending = pending;\n\n if (timeout !== undefined) {\n pending.timer = setTimeout(() => this.cancel(new FamiliarTimeoutError(timeout)), timeout);\n unrefTimer(pending.timer);\n }\n\n try {\n worker.postMessage({ id, input, kind, version: PROTOCOL_VERSION }, transferables);\n } catch (error) {\n this.cancel(new FamiliarRuntimeError('Failed to post message to worker', { cause: error }));\n }\n });\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #ensureWorker(): Worker {\n if (this.#worker) return this.#worker;\n\n if (typeof Worker === 'undefined') throw new FamiliarRuntimeError('Worker API is unavailable in this runtime');\n\n try {\n const worker = new Worker(this.#url, { type: 'module' });\n\n worker.onmessage = (event: MessageEvent<unknown>) => {\n if (this.#worker === worker) this.#onMessage(event.data);\n };\n worker.onerror = (event) => this.#failWorker(worker, new FamiliarRuntimeError(event.message || 'Worker failed'));\n worker.onmessageerror = () =>\n this.#failWorker(worker, new FamiliarRuntimeError('Worker response could not be deserialized'));\n this.#worker = worker;\n\n return worker;\n } catch (error) {\n throw new FamiliarRuntimeError('Failed to create Worker', { cause: error });\n }\n }\n\n #stopWorker(): void {\n const worker = this.#worker;\n this.#worker = undefined;\n if (!worker) return;\n\n worker.onmessage = null;\n worker.onerror = null;\n worker.onmessageerror = null;\n worker.terminate();\n }\n\n #failWorker(worker: Worker, error: FamiliarRuntimeError): void {\n if (this.#worker !== worker) return;\n\n this.#stopWorker();\n this.#settlePending('reject', error);\n\n try {\n this.#onSlotError?.(error);\n } catch {\n warn('onSlotError callback failed; Familiar continued replacing the failed worker slot.');\n }\n }\n\n #onMessage(message: unknown): void {\n if (!this.#pending) return;\n\n if (\n !isWorkerResponse<TOutput>(message) ||\n message.id !== this.#pending.id ||\n (this.#pending.kind === 'run' && message.kind === 'chunk')\n ) {\n this.cancel(new FamiliarRuntimeError('Worker returned an incompatible protocol response'));\n\n return;\n }\n\n if (message.kind === 'chunk') {\n this.#pending.emit?.(message.value);\n\n return;\n }\n\n if (message.kind === 'error') {\n this.#settlePending('reject', responseError(message.error));\n\n return;\n }\n\n this.#settlePending('resolve', message.value);\n }\n\n #settlePending(kind: 'resolve' | 'reject', value: unknown): void {\n const pending = this.#pending;\n\n if (!pending) return;\n\n this.#pending = undefined;\n\n if (pending.timer) clearTimeout(pending.timer);\n\n if (kind === 'resolve') pending.resolve(value as TOutput);\n else pending.reject(value);\n }\n}\n\nfunction slots<TInput, TOutput>(url: URL | string, options: ResolvedOptions): Slot<TInput, TOutput>[] {\n const resolvedUrl = typeof url === 'string' ? url : url.href;\n return Array.from({ length: options.concurrency }, () => new Slot<TInput, TOutput>(resolvedUrl, options.onSlotError));\n}\n\n/** Create a pool backed by an ES module worker registered with exposeTask(). */\nexport function createWorker<TInput, TOutput>(\n url: URL | string,\n options: WorkerOptions = {},\n): WorkerPool<TInput, TOutput> {\n const resolved = resolveOptions(options);\n\n return createPool(slots<TInput, TOutput>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n\n/** Create a stream-only pool backed by an ES module worker registered with exposeStream(). */\nexport function createStreamWorker<TInput, TChunk>(\n url: URL | string,\n options: WorkerOptions = {},\n): StreamWorkerPool<TInput, TChunk> {\n const resolved = resolveOptions(options);\n\n return createStreamPool(slots<TInput, TChunk>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n"],"mappings":"wMA4DA,SAAS,EAAe,EAAyB,CAAC,EAAoB,CACpE,GAAM,CAAE,cAAc,EAAG,WAAU,SAAS,SAAU,cAAa,WAAY,EACzE,EACJ,IAAgB,OACZ,KAAK,IAAA,IAAqB,KAAK,IAAI,EAAG,WAAW,WAAW,qBAAuB,CAAC,CAAC,EACrF,EAEN,GAAI,CAAC,OAAO,UAAU,CAAmB,GAAK,EAAsB,GAAK,EAAA,IACvE,MAAM,IAAI,EAAA,4BAA4B,0DAA2E,EAGnH,GAAI,IAAa,IAAA,KAAc,CAAC,OAAO,UAAU,CAAQ,GAAK,EAAW,GACvE,MAAM,IAAI,EAAA,4BAA4B,uCAAuC,EAG/E,GAAI,IAAW,UAAY,IAAW,OACpC,MAAM,IAAI,EAAA,4BAA4B,qCAAqC,EAK7E,OAFA,EAAA,gBAAgB,CAAO,EAEhB,CAAE,YAAa,EAAqB,WAAU,SAAQ,cAAa,SAAQ,CACpF,CAEA,SAAS,EAA0B,EAAkD,CACnF,GAAI,OAAO,GAAU,WAAY,EAAgB,MAAO,GAExD,IAAM,EAAW,EAYjB,OAVE,EAAS,UAAA,GACT,OAAO,EAAS,IAAO,UACvB,CAAC,OAAO,cAAc,EAAS,EAAE,GACjC,EAAS,GAAK,EAEP,GAEL,EAAS,OAAS,SAAW,EAAS,OAAS,SAAiB,UAAW,EAC3E,EAAS,OAAS,SAAW,OAAO,EAAS,OAAU,UAA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,8 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
export type { BatchOptions } from './_pool.js';
|
|
2
|
+
export { runBatch } from './_pool.js';
|
|
3
|
+
export { FamiliarError, FamiliarInvalidOptionsError, FamiliarQueueFullError, FamiliarRuntimeError, FamiliarTaskError, FamiliarTerminatedError, FamiliarTimeoutError, } from './errors.js';
|
|
4
|
+
export type { DrainOptions, PoolBase, RunOptions, StreamWorkerPool, WorkerOptions, WorkerPool, WorkerStats, WorkerStatus, } from './types.js';
|
|
5
|
+
import type { StreamWorkerPool, WorkerOptions, WorkerPool } from './types.js';
|
|
5
6
|
export type RunningStream<TChunk> = {
|
|
6
7
|
done: Promise<void>;
|
|
7
8
|
iterable: AsyncIterable<TChunk>;
|
package/dist/worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EACL,aAAa,EACb,2BAA2B,EAC3B,sBAAsB,EACtB,oBAAoB,EACpB,iBAAiB,EACjB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,gBAAgB,EAChB,aAAa,EACb,UAAU,EACV,WAAW,EACX,YAAY,GACb,MAAM,YAAY,CAAC;AAgBpB,OAAO,KAAK,EAA4B,gBAAgB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAmBxG,MAAM,MAAM,aAAa,CAAC,MAAM,IAAI;IAClC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACpB,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CACjC,CAAC;AAmRF,gFAAgF;AAChF,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,EAC1C,GAAG,EAAE,GAAG,GAAG,MAAM,EACjB,OAAO,GAAE,aAAkB,GAC1B,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAS7B;AAED,8FAA8F;AAC9F,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAC/C,GAAG,EAAE,GAAG,GAAG,MAAM,EACjB,OAAO,GAAE,aAAkB,GAC1B,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,CASlC"}
|
package/dist/worker.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { unrefTimer as e } from "./_timers.js";
|
|
2
2
|
import { FamiliarError as t, FamiliarInvalidOptionsError as n, FamiliarQueueFullError as r, FamiliarRuntimeError as i, FamiliarTaskError as a, FamiliarTerminatedError as o, FamiliarTimeoutError as s } from "./errors.js";
|
|
3
|
-
import {
|
|
3
|
+
import { validateTimeout as c } from "./_pool-core.js";
|
|
4
|
+
import { createPool as l, runBatch as u } from "./_pool.js";
|
|
5
|
+
import "./_dev.js";
|
|
4
6
|
import { createStreamPool as d } from "./_stream-pool.js";
|
|
5
7
|
import "./protocol.js";
|
|
6
8
|
//#region src/worker.ts
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (!Number.isInteger(s) || s < 1 || s > f) throw new n(`\`concurrency\` must be a positive integer ≤ ${f} or "auto"`);
|
|
9
|
+
function f(e = {}) {
|
|
10
|
+
let { concurrency: t = 1, maxQueue: r, onFull: i = "reject", onSlotError: a, timeout: o } = e, s = t === "auto" ? Math.min(512, Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1)) : t;
|
|
11
|
+
if (!Number.isInteger(s) || s < 1 || s > 512) throw new n("`concurrency` must be a positive integer ≤ 512 or \"auto\"");
|
|
11
12
|
if (r !== void 0 && (!Number.isInteger(r) || r < 1)) throw new n("`maxQueue` must be a positive integer");
|
|
12
|
-
if (
|
|
13
|
-
return {
|
|
13
|
+
if (i !== "reject" && i !== "wait") throw new n("`onFull` must be \"reject\" or \"wait\"");
|
|
14
|
+
return c(o), {
|
|
14
15
|
concurrency: s,
|
|
15
16
|
maxQueue: r,
|
|
16
17
|
onFull: i,
|
|
@@ -18,16 +19,16 @@ function p(e = {}) {
|
|
|
18
19
|
timeout: o
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
|
-
function
|
|
22
|
+
function p(e) {
|
|
22
23
|
if (typeof e != "object" || !e) return !1;
|
|
23
24
|
let t = e;
|
|
24
|
-
return t.version
|
|
25
|
+
return t.version !== 1 || typeof t.id != "number" || !Number.isSafeInteger(t.id) || t.id < 0 ? !1 : t.kind === "chunk" || t.kind === "result" ? "value" in t : t.kind !== "error" || typeof t.error != "object" || t.error === null ? !1 : (t.error.category === void 0 || t.error.category === "protocol") && typeof t.error.message == "string" && typeof t.error.name == "string" && (t.error.stack === void 0 || typeof t.error.stack == "string");
|
|
25
26
|
}
|
|
26
|
-
function
|
|
27
|
+
function m(e) {
|
|
27
28
|
let t = Error(e.message);
|
|
28
|
-
return t.name = e.name, t.stack = e.stack, new a(e.message, { cause: t });
|
|
29
|
+
return t.name = e.name, t.stack = e.stack, e.category === "protocol" ? new i(e.message, { cause: t }) : new a(e.message, { cause: t });
|
|
29
30
|
}
|
|
30
|
-
var
|
|
31
|
+
var h = class {
|
|
31
32
|
#e;
|
|
32
33
|
#t;
|
|
33
34
|
#n = !1;
|
|
@@ -38,10 +39,7 @@ var g = class {
|
|
|
38
39
|
this.#t = e, this.#e = t;
|
|
39
40
|
}
|
|
40
41
|
cancel(e) {
|
|
41
|
-
this.#
|
|
42
|
-
}
|
|
43
|
-
prime() {
|
|
44
|
-
return this.#n || this.#s(), Promise.resolve();
|
|
42
|
+
this.#r && (this.#c(), this.#d("reject", e));
|
|
45
43
|
}
|
|
46
44
|
run(e, t, n) {
|
|
47
45
|
return this.#o(e, t, n, "run");
|
|
@@ -60,7 +58,7 @@ var g = class {
|
|
|
60
58
|
} : Promise.reject(e));
|
|
61
59
|
};
|
|
62
60
|
return {
|
|
63
|
-
done: this.#o(e, t.transferables ?? [], t.timeout, "stream", c).then(() => l(), (e) => l(e)),
|
|
61
|
+
done: this.#o(e, [...t.transferables ?? []], t.timeout, "stream", c).then(() => l(), (e) => l(e)),
|
|
64
62
|
iterable: { [Symbol.asyncIterator]: () => ({
|
|
65
63
|
async next() {
|
|
66
64
|
if (r.length > 0) return {
|
|
@@ -87,17 +85,18 @@ var g = class {
|
|
|
87
85
|
};
|
|
88
86
|
}
|
|
89
87
|
terminate() {
|
|
90
|
-
this.#n = !0, this
|
|
88
|
+
this.#n = !0, this.#c(), this.#d("reject", new o());
|
|
91
89
|
}
|
|
92
90
|
#o(t, n, r, a, c) {
|
|
93
91
|
if (this.#n) return Promise.reject(new o());
|
|
94
92
|
if (this.#r) return Promise.reject(new i("Worker slot is already busy"));
|
|
95
93
|
try {
|
|
96
|
-
let o = this.#s(), l = this.#i
|
|
97
|
-
return new Promise((u, d) => {
|
|
94
|
+
let o = this.#s(), l = this.#i;
|
|
95
|
+
return this.#i = l === 2 ** 53 - 1 ? 0 : l + 1, new Promise((u, d) => {
|
|
98
96
|
let f = {
|
|
99
97
|
emit: c,
|
|
100
98
|
id: l,
|
|
99
|
+
kind: a,
|
|
101
100
|
reject: d,
|
|
102
101
|
resolve: u
|
|
103
102
|
};
|
|
@@ -122,17 +121,28 @@ var g = class {
|
|
|
122
121
|
if (typeof Worker > "u") throw new i("Worker API is unavailable in this runtime");
|
|
123
122
|
try {
|
|
124
123
|
let e = new Worker(this.#t, { type: "module" });
|
|
125
|
-
return e.onmessage = (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}, this.#a = e, e;
|
|
124
|
+
return e.onmessage = (t) => {
|
|
125
|
+
this.#a === e && this.#u(t.data);
|
|
126
|
+
}, e.onerror = (t) => this.#l(e, new i(t.message || "Worker failed")), e.onmessageerror = () => this.#l(e, new i("Worker response could not be deserialized")), this.#a = e, e;
|
|
129
127
|
} catch (e) {
|
|
130
128
|
throw new i("Failed to create Worker", { cause: e });
|
|
131
129
|
}
|
|
132
130
|
}
|
|
133
|
-
#c(
|
|
131
|
+
#c() {
|
|
132
|
+
let e = this.#a;
|
|
133
|
+
this.#a = void 0, e && (e.onmessage = null, e.onerror = null, e.onmessageerror = null, e.terminate());
|
|
134
|
+
}
|
|
135
|
+
#l(e, t) {
|
|
136
|
+
if (this.#a === e) {
|
|
137
|
+
this.#c(), this.#d("reject", t);
|
|
138
|
+
try {
|
|
139
|
+
this.#e?.(t);
|
|
140
|
+
} catch {}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
#u(e) {
|
|
134
144
|
if (this.#r) {
|
|
135
|
-
if (!
|
|
145
|
+
if (!p(e) || e.id !== this.#r.id || this.#r.kind === "run" && e.kind === "chunk") {
|
|
136
146
|
this.cancel(new i("Worker returned an incompatible protocol response"));
|
|
137
147
|
return;
|
|
138
148
|
}
|
|
@@ -141,32 +151,33 @@ var g = class {
|
|
|
141
151
|
return;
|
|
142
152
|
}
|
|
143
153
|
if (e.kind === "error") {
|
|
144
|
-
this.#
|
|
154
|
+
this.#d("reject", m(e.error));
|
|
145
155
|
return;
|
|
146
156
|
}
|
|
147
|
-
this.#
|
|
157
|
+
this.#d("resolve", e.value);
|
|
148
158
|
}
|
|
149
159
|
}
|
|
150
|
-
#
|
|
160
|
+
#d(e, t) {
|
|
151
161
|
let n = this.#r;
|
|
152
162
|
n && (this.#r = void 0, n.timer && clearTimeout(n.timer), e === "resolve" ? n.resolve(t) : n.reject(t));
|
|
153
163
|
}
|
|
154
164
|
};
|
|
155
|
-
function
|
|
156
|
-
|
|
165
|
+
function g(e, t) {
|
|
166
|
+
let n = typeof e == "string" ? e : e.href;
|
|
167
|
+
return Array.from({ length: t.concurrency }, () => new h(n, t.onSlotError));
|
|
157
168
|
}
|
|
158
|
-
function
|
|
159
|
-
let n =
|
|
160
|
-
return l(
|
|
169
|
+
function _(e, t = {}) {
|
|
170
|
+
let n = f(t);
|
|
171
|
+
return l(g(e, n), {
|
|
161
172
|
concurrency: n.concurrency,
|
|
162
173
|
defaultTimeout: n.timeout,
|
|
163
174
|
maxQueue: n.maxQueue,
|
|
164
175
|
onFull: n.onFull
|
|
165
176
|
});
|
|
166
177
|
}
|
|
167
|
-
function
|
|
168
|
-
let n =
|
|
169
|
-
return d(
|
|
178
|
+
function v(e, t = {}) {
|
|
179
|
+
let n = f(t);
|
|
180
|
+
return d(g(e, n), {
|
|
170
181
|
concurrency: n.concurrency,
|
|
171
182
|
defaultTimeout: n.timeout,
|
|
172
183
|
maxQueue: n.maxQueue,
|
|
@@ -174,6 +185,6 @@ function y(e, t = {}) {
|
|
|
174
185
|
});
|
|
175
186
|
}
|
|
176
187
|
//#endregion
|
|
177
|
-
export { t as FamiliarError, n as FamiliarInvalidOptionsError, r as FamiliarQueueFullError, i as FamiliarRuntimeError, a as FamiliarTaskError, o as FamiliarTerminatedError, s as FamiliarTimeoutError,
|
|
188
|
+
export { t as FamiliarError, n as FamiliarInvalidOptionsError, r as FamiliarQueueFullError, i as FamiliarRuntimeError, a as FamiliarTaskError, o as FamiliarTerminatedError, s as FamiliarTimeoutError, v as createStreamWorker, _ as createWorker, u as runBatch };
|
|
178
189
|
|
|
179
190
|
//# sourceMappingURL=worker.js.map
|
package/dist/worker.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","names":["#onSlotError","#url","#worker","#settlePending","#disposed","#ensureWorker","#dispatch","#pending","#taskId","#onMessage"],"sources":["../src/worker.ts"],"sourcesContent":["export { batch, createTaskGroup } from './_pool';\nexport {\n FamiliarError,\n FamiliarInvalidOptionsError,\n FamiliarQueueFullError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors';\nexport type {\n DrainOptions,\n PoolBase,\n RunOptions,\n StreamWorkerPool,\n TaskGroup,\n TaskGroupOptions,\n WorkerOptions,\n WorkerPool,\n WorkerStats,\n WorkerStatus,\n} from './types';\n\nimport { createPool } from './_pool';\nimport { createStreamPool, type StreamSlot } from './_stream-pool';\nimport { unrefTimer } from './_timers';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors';\nimport type { SerializedError, WorkerResponse } from './protocol';\nimport { PROTOCOL_VERSION } from './protocol';\nimport type { RunOptions, SlotStrategy, StreamWorkerPool, WorkerOptions, WorkerPool } from './types';\n\nconst MAX_CONCURRENCY = 512;\n\ntype ResolvedOptions = {\n concurrency: number;\n maxQueue: number | undefined;\n onFull: 'reject' | 'wait';\n onSlotError: WorkerOptions['onSlotError'];\n timeout: number | undefined;\n};\n\ntype Pending<TOutput> = {\n emit?: (value: TOutput) => void;\n id: number;\n reject: (reason: unknown) => void;\n resolve: (value: TOutput) => void;\n timer?: ReturnType<typeof setTimeout>;\n};\n\nexport type RunningStream<TChunk> = {\n done: Promise<void>;\n iterable: AsyncIterable<TChunk>;\n};\n\nfunction resolveOptions(options: WorkerOptions = {}): ResolvedOptions {\n const { concurrency = 1, maxQueue, onFull = 'reject', onSlotError, timeout } = options;\n const resolvedConcurrency =\n concurrency === 'auto' ? Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1) : concurrency;\n\n if (!Number.isInteger(resolvedConcurrency) || resolvedConcurrency < 1 || resolvedConcurrency > MAX_CONCURRENCY) {\n throw new FamiliarInvalidOptionsError(`\\`concurrency\\` must be a positive integer ≤ ${MAX_CONCURRENCY} or \"auto\"`);\n }\n\n if (maxQueue !== undefined && (!Number.isInteger(maxQueue) || maxQueue < 1)) {\n throw new FamiliarInvalidOptionsError('`maxQueue` must be a positive integer');\n }\n\n if (timeout !== undefined && (!Number.isFinite(timeout) || timeout <= 0)) {\n throw new FamiliarInvalidOptionsError('`timeout` must be a finite number greater than 0');\n }\n\n return { concurrency: resolvedConcurrency, maxQueue, onFull, onSlotError, timeout };\n}\n\nfunction isWorkerResponse<TOutput>(value: unknown): value is WorkerResponse<TOutput> {\n if (typeof value !== 'object' || value === null) return false;\n\n const response = value as Partial<WorkerResponse<TOutput>>;\n\n return (\n response.version === PROTOCOL_VERSION &&\n typeof response.id === 'number' &&\n (response.kind === 'chunk' || response.kind === 'error' || response.kind === 'result')\n );\n}\n\nfunction taskError(error: SerializedError): FamiliarTaskError {\n const cause = new Error(error.message);\n\n cause.name = error.name;\n cause.stack = error.stack;\n\n return new FamiliarTaskError(error.message, { cause });\n}\n\nclass Slot<TInput, TOutput> implements SlotStrategy<TInput, TOutput>, StreamSlot<TInput, TOutput> {\n readonly #onSlotError: WorkerOptions['onSlotError'];\n readonly #url: URL | string;\n #disposed = false;\n #pending: Pending<TOutput> | undefined;\n #taskId = 0;\n #worker: Worker | undefined;\n\n constructor(url: URL | string, onSlotError: WorkerOptions['onSlotError']) {\n this.#url = url;\n this.#onSlotError = onSlotError;\n }\n\n cancel(reason: unknown): void {\n this.#worker?.terminate();\n this.#worker = undefined;\n this.#settlePending('reject', reason);\n }\n\n prime(): Promise<void> {\n if (!this.#disposed) this.#ensureWorker();\n\n return Promise.resolve();\n }\n\n run(input: TInput, transferables: Transferable[], timeout: number | undefined): Promise<TOutput> {\n return this.#dispatch(input, transferables, timeout, 'run') as Promise<TOutput>;\n }\n\n stream(input: TInput, options: RunOptions): RunningStream<TOutput> {\n const stop = () => this.cancel(new FamiliarTerminatedError('Stream consumer stopped'));\n const chunks: TOutput[] = [];\n const waiters: Array<(result: IteratorResult<TOutput>) => void> = [];\n let done = false;\n let error: unknown;\n const emit = (value: TOutput) => {\n const waiter = waiters.shift();\n\n if (waiter) waiter({ done: false, value });\n else chunks.push(value);\n };\n const finish = (reason?: unknown) => {\n done = true;\n error = reason;\n\n while (waiters.length > 0) {\n const waiter = waiters.shift()!;\n\n if (reason !== undefined) waiter(Promise.reject(reason) as never);\n else waiter({ done: true, value: undefined as never });\n }\n };\n const donePromise = this.#dispatch(input, options.transferables ?? [], options.timeout, 'stream', emit).then(\n () => finish(),\n (reason: unknown) => finish(reason),\n );\n\n return {\n done: donePromise,\n iterable: {\n [Symbol.asyncIterator]: () => ({\n async next(): Promise<IteratorResult<TOutput>> {\n if (chunks.length > 0) return { done: false, value: chunks.shift()! };\n\n if (error !== undefined) throw error;\n\n if (done) return { done: true, value: undefined as never };\n\n return new Promise<IteratorResult<TOutput>>((resolve, reject) => {\n waiters.push((result) => {\n if (result instanceof Promise) void result.then(resolve, reject);\n else resolve(result);\n });\n });\n },\n async return(): Promise<IteratorResult<TOutput>> {\n stop();\n\n return { done: true, value: undefined as never };\n },\n }),\n },\n };\n }\n\n terminate(): void {\n this.#disposed = true;\n this.cancel(new FamiliarTerminatedError());\n }\n\n #dispatch(\n input: TInput,\n transferables: Transferable[],\n timeout: number | undefined,\n kind: 'run' | 'stream',\n emit?: (value: TOutput) => void,\n ): Promise<TOutput> {\n if (this.#disposed) return Promise.reject(new FamiliarTerminatedError());\n\n if (this.#pending) return Promise.reject(new FamiliarRuntimeError('Worker slot is already busy'));\n\n try {\n const worker = this.#ensureWorker();\n const id = this.#taskId++;\n\n return new Promise<TOutput>((resolve, reject) => {\n const pending: Pending<TOutput> = { emit, id, reject, resolve };\n\n this.#pending = pending;\n\n if (timeout !== undefined) {\n pending.timer = setTimeout(() => this.cancel(new FamiliarTimeoutError(timeout)), timeout);\n unrefTimer(pending.timer);\n }\n\n try {\n worker.postMessage({ id, input, kind, version: PROTOCOL_VERSION }, transferables);\n } catch (error) {\n this.cancel(new FamiliarRuntimeError('Failed to post message to worker', { cause: error }));\n }\n });\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #ensureWorker(): Worker {\n if (this.#worker) return this.#worker;\n\n if (typeof Worker === 'undefined') throw new FamiliarRuntimeError('Worker API is unavailable in this runtime');\n\n try {\n const worker = new Worker(this.#url, { type: 'module' });\n\n worker.onmessage = (event: MessageEvent<unknown>) => this.#onMessage(event.data);\n worker.onerror = (event) => {\n const error = new FamiliarRuntimeError(event.message || 'Worker failed');\n\n this.#worker = undefined;\n this.#onSlotError?.(error);\n this.#settlePending('reject', error);\n };\n this.#worker = worker;\n\n return worker;\n } catch (error) {\n throw new FamiliarRuntimeError('Failed to create Worker', { cause: error });\n }\n }\n\n #onMessage(message: unknown): void {\n if (!this.#pending) return;\n\n if (!isWorkerResponse<TOutput>(message) || message.id !== this.#pending.id) {\n this.cancel(new FamiliarRuntimeError('Worker returned an incompatible protocol response'));\n\n return;\n }\n\n if (message.kind === 'chunk') {\n this.#pending.emit?.(message.value);\n\n return;\n }\n\n if (message.kind === 'error') {\n this.#settlePending('reject', taskError(message.error));\n\n return;\n }\n\n this.#settlePending('resolve', message.value);\n }\n\n #settlePending(kind: 'resolve' | 'reject', value: unknown): void {\n const pending = this.#pending;\n\n if (!pending) return;\n\n this.#pending = undefined;\n\n if (pending.timer) clearTimeout(pending.timer);\n\n if (kind === 'resolve') pending.resolve(value as TOutput);\n else pending.reject(value);\n }\n}\n\nfunction slots<TInput, TOutput>(url: URL | string, options: ResolvedOptions): Slot<TInput, TOutput>[] {\n return Array.from({ length: options.concurrency }, () => new Slot<TInput, TOutput>(url, options.onSlotError));\n}\n\n/** Create a pool backed by an ES module worker registered with exposeTask(). */\nexport function createWorker<TInput, TOutput>(\n url: URL | string,\n options: WorkerOptions = {},\n): WorkerPool<TInput, TOutput> {\n const resolved = resolveOptions(options);\n\n return createPool(slots<TInput, TOutput>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n\n/** Create a stream-only pool backed by an ES module worker registered with exposeStream(). */\nexport function createStreamWorker<TInput, TChunk>(\n url: URL | string,\n options: WorkerOptions = {},\n): StreamWorkerPool<TInput, TChunk> {\n const resolved = resolveOptions(options);\n\n return createStreamPool(slots<TInput, TChunk>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n"],"mappings":";;;;;;AAqCA,IAAM,IAAkB;AAuBxB,SAAS,EAAe,IAAyB,CAAC,GAAoB;CACpE,IAAM,EAAE,iBAAc,GAAG,aAAU,YAAS,UAAU,gBAAa,eAAY,GACzE,IACJ,MAAgB,SAAS,KAAK,IAAI,GAAG,WAAW,WAAW,uBAAuB,CAAC,IAAI;CAEzF,IAAI,CAAC,OAAO,UAAU,CAAmB,KAAK,IAAsB,KAAK,IAAsB,GAC7F,MAAM,IAAI,EAA4B,gDAAgD,EAAgB,WAAW;CAGnH,IAAI,MAAa,KAAA,MAAc,CAAC,OAAO,UAAU,CAAQ,KAAK,IAAW,IACvE,MAAM,IAAI,EAA4B,uCAAuC;CAG/E,IAAI,MAAY,KAAA,MAAc,CAAC,OAAO,SAAS,CAAO,KAAK,KAAW,IACpE,MAAM,IAAI,EAA4B,kDAAkD;CAG1F,OAAO;EAAE,aAAa;EAAqB;EAAU;EAAQ;EAAa;CAAQ;AACpF;AAEA,SAAS,EAA0B,GAAkD;CACnF,IAAI,OAAO,KAAU,aAAY,GAAgB,OAAO;CAExD,IAAM,IAAW;CAEjB,OACE,EAAS,YAAA,KACT,OAAO,EAAS,MAAO,aACtB,EAAS,SAAS,WAAW,EAAS,SAAS,WAAW,EAAS,SAAS;AAEjF;AAEA,SAAS,EAAU,GAA2C;CAC5D,IAAM,IAAY,MAAM,EAAM,OAAO;CAKrC,OAHA,EAAM,OAAO,EAAM,MACnB,EAAM,QAAQ,EAAM,OAEb,IAAI,EAAkB,EAAM,SAAS,EAAE,SAAM,CAAC;AACvD;AAEA,IAAM,IAAN,MAAkG;CAChG;CACA;CACA,KAAY;CACZ;CACA,KAAU;CACV;CAEA,YAAY,GAAmB,GAA2C;EAExE,AADA,KAAKC,KAAO,GACZ,KAAKD,KAAe;CACtB;CAEA,O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,GACQ;CAClC,IAAM,IAAW,EAAe,CAAO;CAEvC,OAAO,EAAiB,EAAsB,GAAK,CAAQ,GAAG;EAC5D,aAAa,EAAS;EACtB,gBAAgB,EAAS;EACzB,UAAU,EAAS;EACnB,QAAQ,EAAS;CACnB,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"worker.js","names":[],"sources":["../src/worker.ts"],"sourcesContent":["export type { BatchOptions } from './_pool.js';\nexport { runBatch } from './_pool.js';\nexport {\n FamiliarError,\n FamiliarInvalidOptionsError,\n FamiliarQueueFullError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors.js';\nexport type {\n DrainOptions,\n PoolBase,\n RunOptions,\n StreamWorkerPool,\n WorkerOptions,\n WorkerPool,\n WorkerStats,\n WorkerStatus,\n} from './types.js';\n\nimport { warn } from './_dev.js';\nimport { createPool } from './_pool.js';\nimport { MAX_CONCURRENCY, validateTimeout } from './_pool-core.js';\nimport { createStreamPool, type StreamSlot } from './_stream-pool.js';\nimport { unrefTimer } from './_timers.js';\nimport {\n FamiliarInvalidOptionsError,\n FamiliarRuntimeError,\n FamiliarTaskError,\n FamiliarTerminatedError,\n FamiliarTimeoutError,\n} from './errors.js';\nimport type { SerializedError, WorkerResponse } from './protocol.js';\nimport { PROTOCOL_VERSION } from './protocol.js';\nimport type { RunOptions, SlotStrategy, StreamWorkerPool, WorkerOptions, WorkerPool } from './types.js';\n\ntype ResolvedOptions = {\n concurrency: number;\n maxQueue: number | undefined;\n onFull: 'reject' | 'wait';\n onSlotError: WorkerOptions['onSlotError'];\n timeout: number | undefined;\n};\n\ntype Pending<TOutput> = {\n emit?: (value: TOutput) => void;\n id: number;\n kind: 'run' | 'stream';\n reject: (reason: unknown) => void;\n resolve: (value: TOutput) => void;\n timer?: ReturnType<typeof setTimeout>;\n};\n\nexport type RunningStream<TChunk> = {\n done: Promise<void>;\n iterable: AsyncIterable<TChunk>;\n};\n\nfunction resolveOptions(options: WorkerOptions = {}): ResolvedOptions {\n const { concurrency = 1, maxQueue, onFull = 'reject', onSlotError, timeout } = options;\n const resolvedConcurrency =\n concurrency === 'auto'\n ? Math.min(MAX_CONCURRENCY, Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1))\n : concurrency;\n\n if (!Number.isInteger(resolvedConcurrency) || resolvedConcurrency < 1 || resolvedConcurrency > MAX_CONCURRENCY) {\n throw new FamiliarInvalidOptionsError(`\\`concurrency\\` must be a positive integer ≤ ${MAX_CONCURRENCY} or \"auto\"`);\n }\n\n if (maxQueue !== undefined && (!Number.isInteger(maxQueue) || maxQueue < 1)) {\n throw new FamiliarInvalidOptionsError('`maxQueue` must be a positive integer');\n }\n\n if (onFull !== 'reject' && onFull !== 'wait') {\n throw new FamiliarInvalidOptionsError('`onFull` must be \"reject\" or \"wait\"');\n }\n\n validateTimeout(timeout);\n\n return { concurrency: resolvedConcurrency, maxQueue, onFull, onSlotError, timeout };\n}\n\nfunction isWorkerResponse<TOutput>(value: unknown): value is WorkerResponse<TOutput> {\n if (typeof value !== 'object' || value === null) return false;\n\n const response = value as Partial<WorkerResponse<TOutput>>;\n if (\n response.version !== PROTOCOL_VERSION ||\n typeof response.id !== 'number' ||\n !Number.isSafeInteger(response.id) ||\n response.id < 0\n )\n return false;\n\n if (response.kind === 'chunk' || response.kind === 'result') return 'value' in response;\n if (response.kind !== 'error' || typeof response.error !== 'object' || response.error === null) return false;\n\n return (\n (response.error.category === undefined || response.error.category === 'protocol') &&\n typeof response.error.message === 'string' &&\n typeof response.error.name === 'string' &&\n (response.error.stack === undefined || typeof response.error.stack === 'string')\n );\n}\n\nfunction responseError(error: SerializedError): FamiliarRuntimeError | FamiliarTaskError {\n const cause = new Error(error.message);\n\n cause.name = error.name;\n cause.stack = error.stack;\n\n return error.category === 'protocol'\n ? new FamiliarRuntimeError(error.message, { cause })\n : new FamiliarTaskError(error.message, { cause });\n}\n\nclass Slot<TInput, TOutput> implements SlotStrategy<TInput, TOutput>, StreamSlot<TInput, TOutput> {\n readonly #onSlotError: WorkerOptions['onSlotError'];\n readonly #url: URL | string;\n #disposed = false;\n #pending: Pending<TOutput> | undefined;\n #taskId = 0;\n #worker: Worker | undefined;\n\n constructor(url: URL | string, onSlotError: WorkerOptions['onSlotError']) {\n this.#url = url;\n this.#onSlotError = onSlotError;\n }\n\n cancel(reason: unknown): void {\n if (!this.#pending) return;\n this.#stopWorker();\n this.#settlePending('reject', reason);\n }\n\n run(input: TInput, transferables: Transferable[], timeout: number | undefined): Promise<TOutput> {\n return this.#dispatch(input, transferables, timeout, 'run') as Promise<TOutput>;\n }\n\n stream(input: TInput, options: RunOptions): RunningStream<TOutput> {\n const stop = () => this.cancel(new FamiliarTerminatedError('Stream consumer stopped'));\n const chunks: TOutput[] = [];\n const waiters: Array<(result: IteratorResult<TOutput>) => void> = [];\n let done = false;\n let error: unknown;\n const emit = (value: TOutput) => {\n const waiter = waiters.shift();\n\n if (waiter) waiter({ done: false, value });\n else chunks.push(value);\n };\n const finish = (reason?: unknown) => {\n done = true;\n error = reason;\n\n while (waiters.length > 0) {\n const waiter = waiters.shift()!;\n\n if (reason !== undefined) waiter(Promise.reject(reason) as never);\n else waiter({ done: true, value: undefined as never });\n }\n };\n const donePromise = this.#dispatch(input, [...(options.transferables ?? [])], options.timeout, 'stream', emit).then(\n () => finish(),\n (reason: unknown) => finish(reason),\n );\n\n return {\n done: donePromise,\n iterable: {\n [Symbol.asyncIterator]: () => ({\n async next(): Promise<IteratorResult<TOutput>> {\n if (chunks.length > 0) return { done: false, value: chunks.shift()! };\n\n if (error !== undefined) throw error;\n\n if (done) return { done: true, value: undefined as never };\n\n return new Promise<IteratorResult<TOutput>>((resolve, reject) => {\n waiters.push((result) => {\n if (result instanceof Promise) void result.then(resolve, reject);\n else resolve(result);\n });\n });\n },\n async return(): Promise<IteratorResult<TOutput>> {\n stop();\n\n return { done: true, value: undefined as never };\n },\n }),\n },\n };\n }\n\n terminate(): void {\n this.#disposed = true;\n this.#stopWorker();\n this.#settlePending('reject', new FamiliarTerminatedError());\n }\n\n #dispatch(\n input: TInput,\n transferables: Transferable[],\n timeout: number | undefined,\n kind: 'run' | 'stream',\n emit?: (value: TOutput) => void,\n ): Promise<TOutput> {\n if (this.#disposed) return Promise.reject(new FamiliarTerminatedError());\n\n if (this.#pending) return Promise.reject(new FamiliarRuntimeError('Worker slot is already busy'));\n\n try {\n const worker = this.#ensureWorker();\n const id = this.#taskId;\n this.#taskId = id === Number.MAX_SAFE_INTEGER ? 0 : id + 1;\n\n return new Promise<TOutput>((resolve, reject) => {\n const pending: Pending<TOutput> = { emit, id, kind, reject, resolve };\n\n this.#pending = pending;\n\n if (timeout !== undefined) {\n pending.timer = setTimeout(() => this.cancel(new FamiliarTimeoutError(timeout)), timeout);\n unrefTimer(pending.timer);\n }\n\n try {\n worker.postMessage({ id, input, kind, version: PROTOCOL_VERSION }, transferables);\n } catch (error) {\n this.cancel(new FamiliarRuntimeError('Failed to post message to worker', { cause: error }));\n }\n });\n } catch (error) {\n return Promise.reject(error);\n }\n }\n\n #ensureWorker(): Worker {\n if (this.#worker) return this.#worker;\n\n if (typeof Worker === 'undefined') throw new FamiliarRuntimeError('Worker API is unavailable in this runtime');\n\n try {\n const worker = new Worker(this.#url, { type: 'module' });\n\n worker.onmessage = (event: MessageEvent<unknown>) => {\n if (this.#worker === worker) this.#onMessage(event.data);\n };\n worker.onerror = (event) => this.#failWorker(worker, new FamiliarRuntimeError(event.message || 'Worker failed'));\n worker.onmessageerror = () =>\n this.#failWorker(worker, new FamiliarRuntimeError('Worker response could not be deserialized'));\n this.#worker = worker;\n\n return worker;\n } catch (error) {\n throw new FamiliarRuntimeError('Failed to create Worker', { cause: error });\n }\n }\n\n #stopWorker(): void {\n const worker = this.#worker;\n this.#worker = undefined;\n if (!worker) return;\n\n worker.onmessage = null;\n worker.onerror = null;\n worker.onmessageerror = null;\n worker.terminate();\n }\n\n #failWorker(worker: Worker, error: FamiliarRuntimeError): void {\n if (this.#worker !== worker) return;\n\n this.#stopWorker();\n this.#settlePending('reject', error);\n\n try {\n this.#onSlotError?.(error);\n } catch {\n warn('onSlotError callback failed; Familiar continued replacing the failed worker slot.');\n }\n }\n\n #onMessage(message: unknown): void {\n if (!this.#pending) return;\n\n if (\n !isWorkerResponse<TOutput>(message) ||\n message.id !== this.#pending.id ||\n (this.#pending.kind === 'run' && message.kind === 'chunk')\n ) {\n this.cancel(new FamiliarRuntimeError('Worker returned an incompatible protocol response'));\n\n return;\n }\n\n if (message.kind === 'chunk') {\n this.#pending.emit?.(message.value);\n\n return;\n }\n\n if (message.kind === 'error') {\n this.#settlePending('reject', responseError(message.error));\n\n return;\n }\n\n this.#settlePending('resolve', message.value);\n }\n\n #settlePending(kind: 'resolve' | 'reject', value: unknown): void {\n const pending = this.#pending;\n\n if (!pending) return;\n\n this.#pending = undefined;\n\n if (pending.timer) clearTimeout(pending.timer);\n\n if (kind === 'resolve') pending.resolve(value as TOutput);\n else pending.reject(value);\n }\n}\n\nfunction slots<TInput, TOutput>(url: URL | string, options: ResolvedOptions): Slot<TInput, TOutput>[] {\n const resolvedUrl = typeof url === 'string' ? url : url.href;\n return Array.from({ length: options.concurrency }, () => new Slot<TInput, TOutput>(resolvedUrl, options.onSlotError));\n}\n\n/** Create a pool backed by an ES module worker registered with exposeTask(). */\nexport function createWorker<TInput, TOutput>(\n url: URL | string,\n options: WorkerOptions = {},\n): WorkerPool<TInput, TOutput> {\n const resolved = resolveOptions(options);\n\n return createPool(slots<TInput, TOutput>(url, resolved), {\n concurrency: resolved.concurrency,\n defaultTimeout: resolved.timeout,\n maxQueue: resolved.maxQueue,\n onFull: resolved.onFull,\n });\n}\n\n/** Create a stream-only pool backed by an ES module worker registered with exposeStream(). */\nexport function createStreamWorker<TInput, TChunk>(\n url: URL | string,\n options: WorkerOptions = {},\n): StreamWorkerPool<TInput, TChunk> {\n const resolved = resolveOptions(options);\n\n return createStreamPool(slots<TInput, TChunk>(url, resolved), {\n concurrency: resolved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GAAe,WAAW,EAAQ,KAAK;EAd5C;CAeF;CAEA,GAAe,GAA4B,GAAsB;EAC/D,IAAM,IAAU,KAAK;EAEhB,MAEL,KAAK,KAAW,KAAA,GAEZ,EAAQ,SAAO,aAAa,EAAQ,KAAK,GAEzC,MAAS,YAAW,EAAQ,QAAQ,CAAgB,IACnD,EAAQ,OAAO,CAAK;CAC3B;AACF;AAEA,SAAS,EAAuB,GAAmB,GAAmD;CACpG,IAAM,IAAc,OAAO,KAAQ,WAAW,IAAM,EAAI;CACxD,OAAO,MAAM,KAAK,EAAE,QAAQ,EAAQ,YAAY,SAAS,IAAI,EAAsB,GAAa,EAAQ,WAAW,CAAC;AACtH;AAGA,SAAgB,EACd,GACA,IAAyB,CAAC,GACG;CAC7B,IAAM,IAAW,EAAe,CAAO;CAEvC,OAAO,EAAW,EAAuB,GAAK,CAAQ,GAAG;EACvD,aAAa,EAAS;EACtB,gBAAgB,EAAS;EACzB,UAAU,EAAS;EACnB,QAAQ,EAAS;CACnB,CAAC;AACH;AAGA,SAAgB,EACd,GACA,IAAyB,CAAC,GACQ;CAClC,IAAM,IAAW,EAAe,CAAO;CAEvC,OAAO,EAAiB,EAAsB,GAAK,CAAQ,GAAG;EAC5D,aAAa,EAAS;EACtB,gBAAgB,EAAS;EACzB,UAAU,EAAS;EACnB,QAAQ,EAAS;CACnB,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vielzeug/familiar",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Typed ES module Worker pools with queueing, streaming, and AbortSignal cancellation",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -50,22 +50,24 @@
|
|
|
50
50
|
"registry": "https://registry.npmjs.org/"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
|
-
"build": "vite build && pnpm run build:bundle && pnpm run build:types",
|
|
53
|
+
"build": "vite build && pnpm --silent run build:bundle && pnpm --silent run build:types && pnpm --silent run test:types && pnpm --silent run verify:exports",
|
|
54
54
|
"build:bundle": "vite build --config vite.bundle.config.ts",
|
|
55
55
|
"build:types": "tsc -p tsconfig.declarations.json",
|
|
56
56
|
"fix": "biome check --write src",
|
|
57
57
|
"lint": "biome ci src",
|
|
58
|
-
"prepublishOnly": "pnpm run build",
|
|
58
|
+
"prepublishOnly": "pnpm --silent run build",
|
|
59
59
|
"preview": "vite preview",
|
|
60
|
-
"test": "vitest"
|
|
60
|
+
"test": "vitest",
|
|
61
|
+
"test:types": "tsc -p tsconfig.type-tests.json",
|
|
62
|
+
"verify:exports": "node verify-exports.mjs"
|
|
61
63
|
},
|
|
62
64
|
"dependencies": {
|
|
63
|
-
"@vielzeug/arsenal": "
|
|
65
|
+
"@vielzeug/arsenal": "3.0.0"
|
|
64
66
|
},
|
|
65
67
|
"devDependencies": {
|
|
66
|
-
"@types/node": "^26.
|
|
68
|
+
"@types/node": "^26.4.1",
|
|
67
69
|
"typescript": "^6.0.3",
|
|
68
|
-
"vite": "^8.2.
|
|
69
|
-
"vitest": "^
|
|
70
|
+
"vite": "^8.2.2",
|
|
71
|
+
"vitest": "^5.0.0"
|
|
70
72
|
}
|
|
71
73
|
}
|