@vielzeug/familiar 26.11.5 → 26.11.6
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/dist/_pool-core.js +1 -2
- package/dist/_pool.cjs +1 -1
- package/dist/_pool.cjs.map +1 -1
- package/dist/_pool.js +47 -48
- package/dist/_stream-pool.cjs +1 -1
- package/dist/_stream-pool.cjs.map +1 -1
- package/dist/_stream-pool.js +27 -28
- package/dist/_timers.js +1 -2
- package/dist/errors.js +1 -2
- package/dist/familiar.js +5 -10
- package/dist/familiar.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +3 -3
- package/dist/protocol.js +1 -2
- package/dist/testing/testing.cjs +1 -1
- package/dist/testing/testing.cjs.map +1 -1
- package/dist/testing/testing.js +35 -36
- package/dist/worker.cjs +1 -1
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.js +46 -47
- package/package.json +8 -8
package/dist/worker.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { createPool as e } from "./_pool.js";
|
|
2
|
+
import { validateTimeout as t } from "./_pool-core.js";
|
|
3
|
+
import { createStreamPool as n } from "./_stream-pool.js";
|
|
4
|
+
import { unrefTimer as r } from "./_timers.js";
|
|
5
|
+
import { FamiliarInvalidOptionsError as i, FamiliarRuntimeError as a, FamiliarTaskError as o, FamiliarTerminatedError as s, FamiliarTimeoutError as c } from "./errors.js";
|
|
6
6
|
import "./protocol.js";
|
|
7
|
-
|
|
7
|
+
// src/worker.ts
|
|
8
8
|
function resolveOptions(e = {}) {
|
|
9
|
-
let { concurrency: n = 1, maxQueue: r, onFull:
|
|
10
|
-
if (!Number.isInteger(s) || s < 1 || s > 512) throw new
|
|
11
|
-
if (r !== void 0 && (!Number.isInteger(r) || r < 1)) throw new
|
|
12
|
-
if (
|
|
13
|
-
return o
|
|
9
|
+
let { concurrency: n = 1, maxQueue: r, onFull: a = "reject", timeout: o } = e, s = n === "auto" ? Math.min(512, Math.max(1, globalThis.navigator?.hardwareConcurrency ?? 1)) : n;
|
|
10
|
+
if (!Number.isInteger(s) || s < 1 || s > 512) throw new i("`concurrency` must be a positive integer ≤ 512 or \"auto\"");
|
|
11
|
+
if (r !== void 0 && (!Number.isInteger(r) || r < 1)) throw new i("`maxQueue` must be a positive integer");
|
|
12
|
+
if (a !== "reject" && a !== "wait") throw new i("`onFull` must be \"reject\" or \"wait\"");
|
|
13
|
+
return t(o), {
|
|
14
14
|
concurrency: s,
|
|
15
15
|
maxQueue: r,
|
|
16
|
-
onFull:
|
|
17
|
-
timeout:
|
|
16
|
+
onFull: a,
|
|
17
|
+
timeout: o
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
function isWorkerResponse(e) {
|
|
@@ -24,7 +24,7 @@ function isWorkerResponse(e) {
|
|
|
24
24
|
}
|
|
25
25
|
function responseError(e) {
|
|
26
26
|
let t = Error(e.message);
|
|
27
|
-
return t.name = e.name, t.stack = e.stack, e.category === "protocol" ? new
|
|
27
|
+
return t.name = e.name, t.stack = e.stack, e.category === "protocol" ? new a(e.message, { cause: t }) : new o(e.message, { cause: t });
|
|
28
28
|
}
|
|
29
29
|
var Slot = class {
|
|
30
30
|
#e;
|
|
@@ -43,14 +43,14 @@ var Slot = class {
|
|
|
43
43
|
return this.#a(e, t, n, "run");
|
|
44
44
|
}
|
|
45
45
|
stream(e, t) {
|
|
46
|
-
let stop = () => this.cancel(new
|
|
46
|
+
let stop = () => this.cancel(new s("Stream consumer stopped")), n = [], r = [], i = !1, a, emit = (e) => {
|
|
47
47
|
let t = r.shift();
|
|
48
48
|
t ? t({
|
|
49
49
|
done: !1,
|
|
50
50
|
value: e
|
|
51
51
|
}) : n.push(e);
|
|
52
52
|
}, finish = (e) => {
|
|
53
|
-
for (
|
|
53
|
+
for (i = !0, a = e; r.length > 0;) r.shift()(e === void 0 ? {
|
|
54
54
|
done: !0,
|
|
55
55
|
value: void 0
|
|
56
56
|
} : Promise.reject(e));
|
|
@@ -63,8 +63,8 @@ var Slot = class {
|
|
|
63
63
|
done: !1,
|
|
64
64
|
value: n.shift()
|
|
65
65
|
};
|
|
66
|
-
if (
|
|
67
|
-
return
|
|
66
|
+
if (a !== void 0) throw a;
|
|
67
|
+
return i ? {
|
|
68
68
|
done: !0,
|
|
69
69
|
value: void 0
|
|
70
70
|
} : new Promise((e, t) => {
|
|
@@ -83,31 +83,31 @@ var Slot = class {
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
terminate() {
|
|
86
|
-
this.#t = !0, this.#s(), this.#u("reject", new
|
|
86
|
+
this.#t = !0, this.#s(), this.#u("reject", new s());
|
|
87
87
|
}
|
|
88
|
-
#a(
|
|
89
|
-
if (this.#t) return Promise.reject(new
|
|
90
|
-
if (this.#n) return Promise.reject(new
|
|
88
|
+
#a(e, t, n, i, o) {
|
|
89
|
+
if (this.#t) return Promise.reject(new s());
|
|
90
|
+
if (this.#n) return Promise.reject(new a("Worker slot is already busy"));
|
|
91
91
|
try {
|
|
92
|
-
let
|
|
92
|
+
let s = this.#o(), l = this.#r;
|
|
93
93
|
return this.#r = l === 2 ** 53 - 1 ? 0 : l + 1, new Promise((u, d) => {
|
|
94
94
|
let f = {
|
|
95
|
-
emit:
|
|
95
|
+
emit: o,
|
|
96
96
|
id: l,
|
|
97
|
-
kind:
|
|
97
|
+
kind: i,
|
|
98
98
|
reject: d,
|
|
99
99
|
resolve: u
|
|
100
100
|
};
|
|
101
|
-
this.#n = f,
|
|
101
|
+
this.#n = f, n !== void 0 && (f.timer = setTimeout(() => this.cancel(new c(n)), n), r(f.timer));
|
|
102
102
|
try {
|
|
103
|
-
|
|
103
|
+
s.postMessage({
|
|
104
104
|
id: l,
|
|
105
|
-
input:
|
|
106
|
-
kind:
|
|
105
|
+
input: e,
|
|
106
|
+
kind: i,
|
|
107
107
|
version: 1
|
|
108
|
-
},
|
|
108
|
+
}, t);
|
|
109
109
|
} catch (e) {
|
|
110
|
-
this.cancel(new
|
|
110
|
+
this.cancel(new a("Failed to post message to worker", { cause: e }));
|
|
111
111
|
}
|
|
112
112
|
});
|
|
113
113
|
} catch (e) {
|
|
@@ -116,14 +116,14 @@ var Slot = class {
|
|
|
116
116
|
}
|
|
117
117
|
#o() {
|
|
118
118
|
if (this.#i) return this.#i;
|
|
119
|
-
if (typeof Worker > "u") throw new
|
|
119
|
+
if (typeof Worker > "u") throw new a("Worker API is unavailable in this runtime");
|
|
120
120
|
try {
|
|
121
121
|
let e = new Worker(this.#e, { type: "module" });
|
|
122
122
|
return e.onmessage = (t) => {
|
|
123
123
|
this.#i === e && this.#l(t.data);
|
|
124
|
-
}, e.onerror = (t) => this.#c(e, new
|
|
124
|
+
}, e.onerror = (t) => this.#c(e, new a(t.message || "Worker failed")), e.onmessageerror = () => this.#c(e, new a("Worker response could not be deserialized")), this.#i = e, e;
|
|
125
125
|
} catch (e) {
|
|
126
|
-
throw new
|
|
126
|
+
throw new a("Failed to create Worker", { cause: e });
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
#s() {
|
|
@@ -134,7 +134,7 @@ var Slot = class {
|
|
|
134
134
|
this.#i === e && (this.#s(), this.#u("reject", t), this.notify?.(t));
|
|
135
135
|
}
|
|
136
136
|
#l(e) {
|
|
137
|
-
this.#n && (!isWorkerResponse(e) || e.id !== this.#n.id || this.#n.kind === "run" && e.kind === "chunk" ? this.cancel(new
|
|
137
|
+
this.#n && (!isWorkerResponse(e) || e.id !== this.#n.id || this.#n.kind === "run" && e.kind === "chunk" ? this.cancel(new a("Worker returned an incompatible protocol response")) : e.kind === "chunk" ? this.#n.emit?.(e.value) : e.kind === "error" ? this.#u("reject", responseError(e.error)) : this.#u("resolve", e.value));
|
|
138
138
|
}
|
|
139
139
|
#u(e, t) {
|
|
140
140
|
let n = this.#n;
|
|
@@ -145,23 +145,22 @@ function slots(e, t) {
|
|
|
145
145
|
let n = typeof e == "string" ? e : e.href;
|
|
146
146
|
return Array.from({ length: t.concurrency }, () => new Slot(n));
|
|
147
147
|
}
|
|
148
|
-
function createWorker(
|
|
149
|
-
let
|
|
150
|
-
return
|
|
151
|
-
defaultTimeout:
|
|
152
|
-
maxQueue:
|
|
153
|
-
onFull:
|
|
148
|
+
function createWorker(t, n = {}) {
|
|
149
|
+
let r = resolveOptions(n);
|
|
150
|
+
return e(slots(t, r), {
|
|
151
|
+
defaultTimeout: r.timeout,
|
|
152
|
+
maxQueue: r.maxQueue,
|
|
153
|
+
onFull: r.onFull
|
|
154
154
|
});
|
|
155
155
|
}
|
|
156
156
|
function createStreamWorker(e, t = {}) {
|
|
157
|
-
let
|
|
158
|
-
return
|
|
159
|
-
defaultTimeout:
|
|
160
|
-
maxQueue:
|
|
161
|
-
onFull:
|
|
157
|
+
let r = resolveOptions(t);
|
|
158
|
+
return n(slots(e, r), {
|
|
159
|
+
defaultTimeout: r.timeout,
|
|
160
|
+
maxQueue: r.maxQueue,
|
|
161
|
+
onFull: r.onFull
|
|
162
162
|
});
|
|
163
163
|
}
|
|
164
|
-
//#endregion
|
|
165
164
|
export { createStreamWorker, createWorker };
|
|
166
165
|
|
|
167
166
|
//# sourceMappingURL=worker.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vielzeug/familiar",
|
|
3
|
-
"version": "26.11.
|
|
3
|
+
"version": "26.11.6",
|
|
4
4
|
"description": "Web Worker pool with tasks, timeouts, cancellation",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -51,24 +51,24 @@
|
|
|
51
51
|
"registry": "https://registry.npmjs.org/"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
|
-
"build": "vite build &&
|
|
54
|
+
"build": "vite build && bun run --silent build:bundle && bun run --silent build:types && bun run --silent test:types && bun run --silent verify:exports",
|
|
55
55
|
"build:bundle": "vite build --config vite.bundle.config.ts",
|
|
56
56
|
"build:types": "tsc -p tsconfig.declarations.json",
|
|
57
57
|
"fix": "biome check --write src",
|
|
58
58
|
"lint": "biome ci src",
|
|
59
|
-
"prepublishOnly": "
|
|
59
|
+
"prepublishOnly": "bun run --silent build",
|
|
60
60
|
"preview": "vite preview",
|
|
61
61
|
"test": "vitest",
|
|
62
62
|
"test:types": "tsc -p tsconfig.type-tests.json",
|
|
63
|
-
"verify:exports": "
|
|
63
|
+
"verify:exports": "bun verify-exports.mjs"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@vielzeug/arsenal": "26.11.
|
|
66
|
+
"@vielzeug/arsenal": "26.11.6"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@types/node": "^26.6.
|
|
69
|
+
"@types/node": "^26.6.6",
|
|
70
70
|
"typescript": "^6.0.3",
|
|
71
|
-
"vite": "^8.3.
|
|
72
|
-
"vitest": "^5.0
|
|
71
|
+
"vite": "^8.3.6",
|
|
72
|
+
"vitest": "^5.1.0"
|
|
73
73
|
}
|
|
74
74
|
}
|