@isoloop/bus 0.1.0-next.0 → 0.1.0-next.10
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/bus-worker.js +512 -35
- package/dist/index.d.ts +107 -12
- package/dist/index.js +301 -37
- package/dist/{standard-schema-DnJtEmJw.js → standard-schema-BtwRaC9-.js} +2 -0
- package/dist/testing.d.ts +12 -4
- package/dist/testing.js +8 -4
- package/dist/topic-BpgHePIE.d.ts +203 -0
- package/package.json +3 -3
- package/dist/topic-BQbes-gX.d.ts +0 -127
package/dist/bus-worker.js
CHANGED
|
@@ -48,6 +48,16 @@ function matchTopic(grant, topic) {
|
|
|
48
48
|
function matchAny(patterns, topic) {
|
|
49
49
|
return patterns.some((p) => matchTopic(p, topic));
|
|
50
50
|
}
|
|
51
|
+
/** Every {@link RequestVia}, for guards. @experimental */
|
|
52
|
+
const REQUEST_VIA = Object.freeze([
|
|
53
|
+
"mcp",
|
|
54
|
+
"webmcp",
|
|
55
|
+
"builder"
|
|
56
|
+
]);
|
|
57
|
+
/** True for a known {@link RequestVia}. @experimental */
|
|
58
|
+
function isRequestVia(x) {
|
|
59
|
+
return typeof x === "string" && REQUEST_VIA.includes(x);
|
|
60
|
+
}
|
|
51
61
|
const isIsolateToBus = envelopeGuard({
|
|
52
62
|
publish: true,
|
|
53
63
|
subscribe: true,
|
|
@@ -56,14 +66,24 @@ const isIsolateToBus = envelopeGuard({
|
|
|
56
66
|
request: true,
|
|
57
67
|
reply: true
|
|
58
68
|
});
|
|
59
|
-
|
|
69
|
+
/** Longest preview of a record's value, in characters (ADR 0020, decision 3). */
|
|
70
|
+
const PREVIEW_CHARS = 2048;
|
|
71
|
+
const HOST_TO_BUS = {
|
|
60
72
|
boot: true,
|
|
61
73
|
attach: true,
|
|
62
74
|
detach: true,
|
|
63
75
|
publish: true,
|
|
76
|
+
request: true,
|
|
64
77
|
flush: true,
|
|
65
|
-
drop: true
|
|
66
|
-
|
|
78
|
+
drop: true,
|
|
79
|
+
tap: true,
|
|
80
|
+
read: true
|
|
81
|
+
};
|
|
82
|
+
/** The message types a tap's port carries to the bus. @experimental */
|
|
83
|
+
const TAP_TO_BUS = { close: true };
|
|
84
|
+
const isHostToBus = envelopeGuard(HOST_TO_BUS);
|
|
85
|
+
/** A well-formed message from the host on a tap's port. @experimental */
|
|
86
|
+
const isTapToBus = envelopeGuard(TAP_TO_BUS);
|
|
67
87
|
/** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
|
|
68
88
|
function systemSchema(check) {
|
|
69
89
|
return { "~standard": {
|
|
@@ -687,6 +707,8 @@ function bigint(v) {
|
|
|
687
707
|
* §6.1). Strings count two bytes per UTF-16 unit, numbers eight, object keys
|
|
688
708
|
* like strings, plus a little per entry. Cycles count once. Cheap rather than
|
|
689
709
|
* exact: it only has to tell a 1 KiB record from a 1 MiB one.
|
|
710
|
+
*
|
|
711
|
+
* @internal
|
|
690
712
|
*/
|
|
691
713
|
function estimateSize(value, limit = Infinity) {
|
|
692
714
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -871,6 +893,8 @@ const segmentKey = (s) => typeof s === "object" ? s.key : s;
|
|
|
871
893
|
/**
|
|
872
894
|
* Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
|
|
873
895
|
* (ADR 0003, decision 10). Symbol keys become their description.
|
|
896
|
+
*
|
|
897
|
+
* @internal
|
|
874
898
|
*/
|
|
875
899
|
function toIssues(issues) {
|
|
876
900
|
return issues.map((i) => ({
|
|
@@ -882,11 +906,19 @@ function toIssues(issues) {
|
|
|
882
906
|
}));
|
|
883
907
|
}
|
|
884
908
|
const isRecord = (x) => typeof x === "object" && x !== null;
|
|
885
|
-
/**
|
|
909
|
+
/**
|
|
910
|
+
* True for a value built by `contracts()`, from any copy of this package.
|
|
911
|
+
*
|
|
912
|
+
* @internal
|
|
913
|
+
*/
|
|
886
914
|
function isContracts(x) {
|
|
887
915
|
return isRecord(x) && x["~isoloop"] === "contracts";
|
|
888
916
|
}
|
|
889
|
-
/**
|
|
917
|
+
/**
|
|
918
|
+
* The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5).
|
|
919
|
+
*
|
|
920
|
+
* @internal
|
|
921
|
+
*/
|
|
890
922
|
function topicConfig(t) {
|
|
891
923
|
return topicConfigOf(t);
|
|
892
924
|
}
|
|
@@ -914,7 +946,11 @@ function hash(s) {
|
|
|
914
946
|
return h >>> 0;
|
|
915
947
|
}
|
|
916
948
|
const reqKey = (topic, offset) => `${topic}\u0000${offset}`;
|
|
917
|
-
/**
|
|
949
|
+
/**
|
|
950
|
+
* A message's `(epoch, seq)`, when it carries both as numbers.
|
|
951
|
+
*
|
|
952
|
+
* @internal
|
|
953
|
+
*/
|
|
918
954
|
function publishId(p) {
|
|
919
955
|
const { epoch, seq } = p;
|
|
920
956
|
return typeof epoch === "number" && typeof seq === "number" ? {
|
|
@@ -933,6 +969,8 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
|
933
969
|
* and consumer groups, delivers from the log under credit, and routes
|
|
934
970
|
* request replies. Knows nothing about workers; the bus worker entry wires
|
|
935
971
|
* ports to it.
|
|
972
|
+
*
|
|
973
|
+
* @internal
|
|
936
974
|
*/
|
|
937
975
|
var Broker = class {
|
|
938
976
|
#engine;
|
|
@@ -947,6 +985,12 @@ var Broker = class {
|
|
|
947
985
|
#conns = /* @__PURE__ */ new Map();
|
|
948
986
|
#groups = /* @__PURE__ */ new Map();
|
|
949
987
|
#requests = /* @__PURE__ */ new Map();
|
|
988
|
+
/**
|
|
989
|
+
* Open request offsets per topic that may not be delivered yet, and the
|
|
990
|
+
* tail last seen there: when retention moves the tail past one nobody was
|
|
991
|
+
* handed, its caller gets `dropped` (ADR 0018, decision 5).
|
|
992
|
+
*/
|
|
993
|
+
#undelivered = /* @__PURE__ */ new Map();
|
|
950
994
|
/** Consumers whose offsets were dropped by `detach { keepOffsets: false }`, per topic. */
|
|
951
995
|
#forgotten = /* @__PURE__ */ new Set();
|
|
952
996
|
#rejected = {};
|
|
@@ -958,6 +1002,8 @@ var Broker = class {
|
|
|
958
1002
|
#sources = /* @__PURE__ */ new Set();
|
|
959
1003
|
#followReports = /* @__PURE__ */ new Map();
|
|
960
1004
|
#dropped = 0;
|
|
1005
|
+
/** Inspection taps, with the topic patterns each one matches. */
|
|
1006
|
+
#taps = /* @__PURE__ */ new Map();
|
|
961
1007
|
constructor(opts) {
|
|
962
1008
|
this.#engine = opts.engine;
|
|
963
1009
|
this.#now = opts.now ?? Date.now;
|
|
@@ -1037,7 +1083,9 @@ var Broker = class {
|
|
|
1037
1083
|
topics: [...this.#topics].map(([topic, t]) => ({
|
|
1038
1084
|
topic,
|
|
1039
1085
|
records: t.records,
|
|
1040
|
-
bytes: t.bytes
|
|
1086
|
+
bytes: t.bytes,
|
|
1087
|
+
head: this.#engine.head(topic),
|
|
1088
|
+
retained: this.#engine.retained(topic)
|
|
1041
1089
|
})),
|
|
1042
1090
|
subscriptions,
|
|
1043
1091
|
rejected: { ...this.#rejected }
|
|
@@ -1261,6 +1309,46 @@ var Broker = class {
|
|
|
1261
1309
|
publish(from, topic, key, value, id) {
|
|
1262
1310
|
return new Promise((resolve) => this.#publish(from, topic, key, value, void 0, resolve, id));
|
|
1263
1311
|
}
|
|
1312
|
+
/**
|
|
1313
|
+
* A request from the host (`kernel.request`, ADR 0018, decision 4): like
|
|
1314
|
+
* an isolate's `bus.request`, with `req: { from: 'host', id, via }` on the
|
|
1315
|
+
* record. `answer` gets one result: a failure to append, or the first
|
|
1316
|
+
* valid reply (or `reply_invalid`). Nothing, if no reply comes before the
|
|
1317
|
+
* caller's timeout; the request is forgotten after `timeoutMs`.
|
|
1318
|
+
*/
|
|
1319
|
+
request(topic, key, value, opts, answer, appendId) {
|
|
1320
|
+
let answered = false;
|
|
1321
|
+
const once = (r) => {
|
|
1322
|
+
if (answered) return;
|
|
1323
|
+
answered = true;
|
|
1324
|
+
answer(r);
|
|
1325
|
+
};
|
|
1326
|
+
const req = {
|
|
1327
|
+
from: {
|
|
1328
|
+
isolateId: HOST_PRODUCER,
|
|
1329
|
+
grants: {
|
|
1330
|
+
publish: [],
|
|
1331
|
+
subscribe: []
|
|
1332
|
+
},
|
|
1333
|
+
port: {
|
|
1334
|
+
post: (m) => {
|
|
1335
|
+
if (m.t === "result") once(m.p);
|
|
1336
|
+
},
|
|
1337
|
+
onMessage: () => {},
|
|
1338
|
+
close: () => {}
|
|
1339
|
+
},
|
|
1340
|
+
subs: /* @__PURE__ */ new Map(),
|
|
1341
|
+
reset: void 0,
|
|
1342
|
+
host: true
|
|
1343
|
+
},
|
|
1344
|
+
id: opts.id,
|
|
1345
|
+
timeoutMs: opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_REQUEST_TIMEOUT_MS,
|
|
1346
|
+
...opts.via === void 0 ? {} : { via: opts.via }
|
|
1347
|
+
};
|
|
1348
|
+
this.#publish({ kind: "host" }, topic, key, value, req, (r) => {
|
|
1349
|
+
if (!r.ok) once(r);
|
|
1350
|
+
}, appendId);
|
|
1351
|
+
}
|
|
1264
1352
|
#receive(conn, msg) {
|
|
1265
1353
|
if (!isIsolateToBus(msg) || msg.t !== "commits" && !hasTopic(msg.p)) {
|
|
1266
1354
|
this.#dropped++;
|
|
@@ -1359,7 +1447,7 @@ var Broker = class {
|
|
|
1359
1447
|
value: null
|
|
1360
1448
|
};
|
|
1361
1449
|
for (const e of offsets) {
|
|
1362
|
-
const r = typeof e === "object" && e !== null && "topic" in e && typeof e.topic === "string" ? this.#commit(conn, e.topic, "offset" in e ? e.offset : void 0, "seq" in e ? e.seq : void 0) : fail("invalid", "each commit needs a topic, an offset and a seq");
|
|
1450
|
+
const r = typeof e === "object" && e !== null && "topic" in e && typeof e.topic === "string" ? this.#commit(conn, e.topic, "offset" in e ? e.offset : void 0, "seq" in e ? e.seq : void 0, "partial" in e ? e.partial : void 0) : fail("invalid", "each commit needs a topic, an offset and a seq");
|
|
1363
1451
|
if (!r.ok && first.ok) first = r;
|
|
1364
1452
|
}
|
|
1365
1453
|
reply(first);
|
|
@@ -1464,13 +1552,17 @@ var Broker = class {
|
|
|
1464
1552
|
...rec,
|
|
1465
1553
|
key: p.key
|
|
1466
1554
|
};
|
|
1467
|
-
if (p.req)
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1555
|
+
if (p.req) {
|
|
1556
|
+
const { via } = p.req;
|
|
1557
|
+
rec = {
|
|
1558
|
+
...rec,
|
|
1559
|
+
req: {
|
|
1560
|
+
from: p.req.from.isolateId,
|
|
1561
|
+
id: p.req.id,
|
|
1562
|
+
...via === void 0 ? {} : { via }
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1474
1566
|
const offset = this.#engine.append(name, rec, p.id);
|
|
1475
1567
|
topic.records++;
|
|
1476
1568
|
topic.bytes += p.bytes;
|
|
@@ -1480,12 +1572,73 @@ var Broker = class {
|
|
|
1480
1572
|
ok: true,
|
|
1481
1573
|
value: { offset }
|
|
1482
1574
|
});
|
|
1575
|
+
this.#dropEvicted(name);
|
|
1483
1576
|
this.#fanOut(name);
|
|
1484
1577
|
this.#wake(name);
|
|
1485
1578
|
this.#follow(name, {
|
|
1486
1579
|
...rec,
|
|
1487
1580
|
offset
|
|
1488
1581
|
});
|
|
1582
|
+
if (this.#taps.size > 0) this.#toTaps(name, {
|
|
1583
|
+
...rec,
|
|
1584
|
+
offset
|
|
1585
|
+
}, p.bytes);
|
|
1586
|
+
}
|
|
1587
|
+
#toTaps(name, rec, bytes) {
|
|
1588
|
+
for (const [sink, patterns] of this.#taps) {
|
|
1589
|
+
if (!matchAny(patterns, name)) continue;
|
|
1590
|
+
try {
|
|
1591
|
+
sink(name, rec, bytes);
|
|
1592
|
+
} catch (e) {
|
|
1593
|
+
console.error("[isoloop] bus: a tap threw", e);
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* The inspection feed's tap (ADR 0020, decision 3): `sink` gets every
|
|
1599
|
+
* record appended to a topic matching one of `patterns` from now on,
|
|
1600
|
+
* through the publish pipeline. Seeds copying into a topic before it opens
|
|
1601
|
+
* aren't tapped. Returns the function that ends it.
|
|
1602
|
+
*/
|
|
1603
|
+
tap(patterns, sink) {
|
|
1604
|
+
this.#taps.set(sink, [...patterns]);
|
|
1605
|
+
return () => {
|
|
1606
|
+
this.#taps.delete(sink);
|
|
1607
|
+
};
|
|
1608
|
+
}
|
|
1609
|
+
/**
|
|
1610
|
+
* The last `last` retained records of `topic`, oldest first (ADR 0020,
|
|
1611
|
+
* decision 3), paging evicted records in from disk when needed. Seed
|
|
1612
|
+
* sources the contracts don't list are unknown here, as to isolates.
|
|
1613
|
+
*/
|
|
1614
|
+
async read(name, last) {
|
|
1615
|
+
if (!this.#topics.has(name)) return {
|
|
1616
|
+
ok: false,
|
|
1617
|
+
error: {
|
|
1618
|
+
code: "unknown_topic",
|
|
1619
|
+
message: `unknown topic ${name}`
|
|
1620
|
+
}
|
|
1621
|
+
};
|
|
1622
|
+
if (!Number.isInteger(last) || last < 1) return {
|
|
1623
|
+
ok: false,
|
|
1624
|
+
error: {
|
|
1625
|
+
code: "invalid",
|
|
1626
|
+
message: "last must be a positive whole number"
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
const tail = this.#engine.tail(name);
|
|
1630
|
+
const head = this.#engine.head(name);
|
|
1631
|
+
let span = last;
|
|
1632
|
+
for (;;) {
|
|
1633
|
+
const from = Math.max(tail, head - span);
|
|
1634
|
+
if (!this.#engine.readable(name, from)) await this.#engine.page(name, from);
|
|
1635
|
+
const got = this.#engine.read(name, from, Number.MAX_SAFE_INTEGER);
|
|
1636
|
+
if (got.length >= last || from <= this.#engine.tail(name)) return {
|
|
1637
|
+
ok: true,
|
|
1638
|
+
records: got.slice(-last)
|
|
1639
|
+
};
|
|
1640
|
+
span *= 2;
|
|
1641
|
+
}
|
|
1489
1642
|
}
|
|
1490
1643
|
/**
|
|
1491
1644
|
* Turns a schema result into the value to store, or the error to send. A
|
|
@@ -1532,6 +1685,41 @@ var Broker = class {
|
|
|
1532
1685
|
recipients: /* @__PURE__ */ new Set(),
|
|
1533
1686
|
done: false
|
|
1534
1687
|
});
|
|
1688
|
+
let u = this.#undelivered.get(topic);
|
|
1689
|
+
if (!u) {
|
|
1690
|
+
u = {
|
|
1691
|
+
offsets: [],
|
|
1692
|
+
tail: this.#engine.tail(topic)
|
|
1693
|
+
};
|
|
1694
|
+
this.#undelivered.set(topic, u);
|
|
1695
|
+
}
|
|
1696
|
+
u.offsets.push(offset);
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Answers `dropped` to each caller whose request retention removed before
|
|
1700
|
+
* the bus delivered it to any subscriber (ADR 0018, decision 5). A request
|
|
1701
|
+
* already delivered stays its responder's to answer. Runs only when the
|
|
1702
|
+
* topic's tail has moved.
|
|
1703
|
+
*/
|
|
1704
|
+
#dropEvicted(topic) {
|
|
1705
|
+
const u = this.#undelivered.get(topic);
|
|
1706
|
+
if (!u) return;
|
|
1707
|
+
const tail = this.#engine.tail(topic);
|
|
1708
|
+
if (tail === u.tail) return;
|
|
1709
|
+
u.tail = tail;
|
|
1710
|
+
const kept = [];
|
|
1711
|
+
for (const offset of u.offsets) {
|
|
1712
|
+
if (offset >= tail) {
|
|
1713
|
+
kept.push(offset);
|
|
1714
|
+
continue;
|
|
1715
|
+
}
|
|
1716
|
+
const req = this.#requests.get(reqKey(topic, offset));
|
|
1717
|
+
if (!req || req.done || req.recipients.size > 0) continue;
|
|
1718
|
+
req.done = true;
|
|
1719
|
+
if (req.caller.host === true || this.#conns.get(req.caller.isolateId) === req.caller) this.#post(req.caller, req.id, fail("dropped", "the request was dropped: the topic's retention was full; retry or raise retention"));
|
|
1720
|
+
}
|
|
1721
|
+
if (kept.length === 0) this.#undelivered.delete(topic);
|
|
1722
|
+
else u.offsets = kept;
|
|
1535
1723
|
}
|
|
1536
1724
|
#reply(conn, name, offset, value, settle) {
|
|
1537
1725
|
const topic = this.#topics.get(name);
|
|
@@ -1551,7 +1739,7 @@ var Broker = class {
|
|
|
1551
1739
|
if (req.done) return settle(fail("already_replied", `request ${name}@${offset} has an answer`));
|
|
1552
1740
|
req.done = true;
|
|
1553
1741
|
const checked = this.#check(name, r);
|
|
1554
|
-
const callerAlive = this.#conns.get(req.caller.isolateId) === req.caller;
|
|
1742
|
+
const callerAlive = req.caller.host === true || this.#conns.get(req.caller.isolateId) === req.caller;
|
|
1555
1743
|
if (!checked.ok) {
|
|
1556
1744
|
settle(checked);
|
|
1557
1745
|
if (callerAlive) {
|
|
@@ -1690,17 +1878,21 @@ var Broker = class {
|
|
|
1690
1878
|
* A commit covers the records delivered up to `offset`. In a group it also
|
|
1691
1879
|
* needs their delivery's `seq` at or below `seq`: a record redelivered to
|
|
1692
1880
|
* this member after the deliveries the commit settled has a higher one,
|
|
1693
|
-
* whatever its offset (ADR 0016, point 12).
|
|
1694
|
-
*
|
|
1881
|
+
* whatever its offset (ADR 0016, point 12). `partial` also covers the
|
|
1882
|
+
* first records of delivery `seq + 1`, the ones settled before a member
|
|
1883
|
+
* stops partway through it (point 17). An own subscription's deliveries
|
|
1884
|
+
* are in order, so the offset alone decides there.
|
|
1695
1885
|
*/
|
|
1696
|
-
#commit(conn, name, offset, seq) {
|
|
1886
|
+
#commit(conn, name, offset, seq, partial) {
|
|
1697
1887
|
const sub = conn.subs.get(name);
|
|
1698
1888
|
if (!sub) return fail("forbidden", `not subscribed to ${name}`);
|
|
1699
1889
|
if (!isOffset(offset)) return fail("invalid", "offset must be a non-negative integer");
|
|
1700
1890
|
if (!isSeq(seq)) return fail("invalid", "seq must be a non-negative integer");
|
|
1891
|
+
if (partial !== void 0 && !isSeq(partial)) return fail("invalid", "partial must be a non-negative integer");
|
|
1892
|
+
const part = isSeq(partial) ? partial : 0;
|
|
1701
1893
|
if (offset >= this.#engine.head(name)) return fail("invalid", `offset ${offset} is not in ${name} yet`);
|
|
1702
1894
|
const g = sub.group;
|
|
1703
|
-
const covers = (d) => d.offset <= offset &&
|
|
1895
|
+
const covers = (d) => g ? d.offset <= offset && d.seq <= seq || d.seq === seq + 1 && d.i < part : d.offset <= offset;
|
|
1704
1896
|
const before = sub.unacked.length;
|
|
1705
1897
|
if (g) sub.unacked = sub.unacked.filter((d) => !covers(d));
|
|
1706
1898
|
else while (sub.unacked[0] && covers(sub.unacked[0])) sub.unacked.shift();
|
|
@@ -1772,10 +1964,11 @@ var Broker = class {
|
|
|
1772
1964
|
/** Sends one `deliver`, numbered with the subscription's next `seq`, and returns it. */
|
|
1773
1965
|
#deliver(sub, records, gap) {
|
|
1774
1966
|
const seq = ++sub.seq;
|
|
1775
|
-
for (const r of records) {
|
|
1967
|
+
for (const [i, r] of records.entries()) {
|
|
1776
1968
|
sub.unacked.push({
|
|
1777
1969
|
offset: r.offset,
|
|
1778
|
-
seq
|
|
1970
|
+
seq,
|
|
1971
|
+
i
|
|
1779
1972
|
});
|
|
1780
1973
|
if (r.req) this.#requests.get(reqKey(sub.topic, r.offset))?.recipients.add(sub.conn.isolateId);
|
|
1781
1974
|
}
|
|
@@ -1842,7 +2035,10 @@ var Broker = class {
|
|
|
1842
2035
|
batches.delete(m);
|
|
1843
2036
|
if (!b || b.records.length === 0) return;
|
|
1844
2037
|
const seq = this.#deliver(m, b.records);
|
|
1845
|
-
for (const
|
|
2038
|
+
for (const { entry, i } of b.entries) {
|
|
2039
|
+
entry.seq = seq;
|
|
2040
|
+
entry.i = i;
|
|
2041
|
+
}
|
|
1846
2042
|
};
|
|
1847
2043
|
/** Credit left once the records batched for it go out. */
|
|
1848
2044
|
const room = (m) => m ? m.credit - (batches.get(m)?.records.length ?? 0) : 0;
|
|
@@ -1851,8 +2047,11 @@ var Broker = class {
|
|
|
1851
2047
|
records: [],
|
|
1852
2048
|
entries: []
|
|
1853
2049
|
};
|
|
2050
|
+
if (entry) b.entries.push({
|
|
2051
|
+
entry,
|
|
2052
|
+
i: b.records.length
|
|
2053
|
+
});
|
|
1854
2054
|
b.records.push(rec);
|
|
1855
|
-
if (entry) b.entries.push(entry);
|
|
1856
2055
|
batches.set(m, b);
|
|
1857
2056
|
if (rec.key === void 0) g.rr++;
|
|
1858
2057
|
if (b.records.length >= this.#batch) flush(m);
|
|
@@ -1908,7 +2107,8 @@ var Broker = class {
|
|
|
1908
2107
|
const entry = {
|
|
1909
2108
|
offset: rec.offset,
|
|
1910
2109
|
member: m,
|
|
1911
|
-
seq: 0
|
|
2110
|
+
seq: 0,
|
|
2111
|
+
i: 0
|
|
1912
2112
|
};
|
|
1913
2113
|
g.unacked.push(entry);
|
|
1914
2114
|
give(m, rec, entry);
|
|
@@ -1930,7 +2130,11 @@ function validate(schema, value) {
|
|
|
1930
2130
|
return r.then((x) => x, (thrown) => ({ thrown }));
|
|
1931
2131
|
}
|
|
1932
2132
|
// src/engine/memory-storage.ts
|
|
1933
|
-
/**
|
|
2133
|
+
/**
|
|
2134
|
+
* Storage that keeps nothing. Every topic behaves as `memory`.
|
|
2135
|
+
*
|
|
2136
|
+
* @internal
|
|
2137
|
+
*/
|
|
1934
2138
|
var MemoryStorage = class {
|
|
1935
2139
|
kind = "memory";
|
|
1936
2140
|
open() {
|
|
@@ -1965,7 +2169,9 @@ var MemoryStorage = class {
|
|
|
1965
2169
|
* Engines call this from `createTopic`: the durability a topic actually gets.
|
|
1966
2170
|
* A `persistent` topic on storage that keeps nothing (no OPFS, or another tab
|
|
1967
2171
|
* holds it) behaves as `memory`; the kernel reports that as
|
|
1968
|
-
* `bus:
|
|
2172
|
+
* `bus:durability_degraded` (ADR 0007, decision 6).
|
|
2173
|
+
*
|
|
2174
|
+
* @internal
|
|
1969
2175
|
*/
|
|
1970
2176
|
function effectiveDurability(cfg, storage) {
|
|
1971
2177
|
if (cfg.durability !== "persistent") return "memory";
|
|
@@ -2039,7 +2245,22 @@ function scanFrames(bytes) {
|
|
|
2039
2245
|
};
|
|
2040
2246
|
}
|
|
2041
2247
|
const isList = (x) => Array.isArray(x);
|
|
2042
|
-
|
|
2248
|
+
/** A stored `req`, rebuilt from its known fields; a `via` this version doesn't know is left out. */
|
|
2249
|
+
function reqOf(x) {
|
|
2250
|
+
if (typeof x !== "object" || x === null) return void 0;
|
|
2251
|
+
const from = Reflect.get(x, "from");
|
|
2252
|
+
const id = Reflect.get(x, "id");
|
|
2253
|
+
if (typeof from !== "string" || typeof id !== "string") return void 0;
|
|
2254
|
+
const via = Reflect.get(x, "via");
|
|
2255
|
+
return isRequestVia(via) ? {
|
|
2256
|
+
from,
|
|
2257
|
+
id,
|
|
2258
|
+
via
|
|
2259
|
+
} : {
|
|
2260
|
+
from,
|
|
2261
|
+
id
|
|
2262
|
+
};
|
|
2263
|
+
}
|
|
2043
2264
|
/** Decodes one frame written by `encodeFrame`. */
|
|
2044
2265
|
function decodeFrame(frame) {
|
|
2045
2266
|
const offset = frameOffset(frame);
|
|
@@ -2053,7 +2274,8 @@ function decodeFrame(frame) {
|
|
|
2053
2274
|
producer
|
|
2054
2275
|
};
|
|
2055
2276
|
if (typeof key === "string") rec.key = key;
|
|
2056
|
-
|
|
2277
|
+
const r = reqOf(req);
|
|
2278
|
+
if (r) rec.req = r;
|
|
2057
2279
|
if (typeof epoch === "number") rec.epoch = epoch;
|
|
2058
2280
|
if (typeof seq === "number") rec.seq = seq;
|
|
2059
2281
|
return rec;
|
|
@@ -2068,6 +2290,8 @@ const DIRTY_MIN = 1024;
|
|
|
2068
2290
|
* The TypeScript `LogEngine` (design §6.2). Each topic is an array of
|
|
2069
2291
|
* fixed-size chunks, so dropping old records frees a whole chunk at a time:
|
|
2070
2292
|
* O(1) per chunk. Compacted topics keep a key → latest-offset index.
|
|
2293
|
+
*
|
|
2294
|
+
* @internal
|
|
2071
2295
|
*/
|
|
2072
2296
|
var TsEngine = class {
|
|
2073
2297
|
name = "ts";
|
|
@@ -2352,6 +2576,9 @@ var TsEngine = class {
|
|
|
2352
2576
|
tail(topic) {
|
|
2353
2577
|
return this.#topic(topic).tail;
|
|
2354
2578
|
}
|
|
2579
|
+
retained(topic) {
|
|
2580
|
+
return this.#topic(topic).live;
|
|
2581
|
+
}
|
|
2355
2582
|
commit(topic, consumer, offset) {
|
|
2356
2583
|
const t = this.#topic(topic);
|
|
2357
2584
|
t.commits.set(consumer, offset);
|
|
@@ -3270,6 +3497,177 @@ var OpfsStorage = class {
|
|
|
3270
3497
|
this.#topics.clear();
|
|
3271
3498
|
}
|
|
3272
3499
|
};
|
|
3500
|
+
// src/preview.ts
|
|
3501
|
+
/** Typed arrays and `ArrayBuffer`s show their length, not their contents. */
|
|
3502
|
+
const binary = (v) => {
|
|
3503
|
+
if (v instanceof ArrayBuffer) return `ArrayBuffer(${v.byteLength})`;
|
|
3504
|
+
if (ArrayBuffer.isView(v)) {
|
|
3505
|
+
const len = "length" in v && typeof v.length === "number" ? v.length : v.byteLength;
|
|
3506
|
+
return `${v.constructor.name}(${len})`;
|
|
3507
|
+
}
|
|
3508
|
+
};
|
|
3509
|
+
/** `value` as text, at most `max` characters. `truncated` when it was longer. */
|
|
3510
|
+
function previewText(value, max = PREVIEW_CHARS) {
|
|
3511
|
+
let out = "";
|
|
3512
|
+
let full = false;
|
|
3513
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3514
|
+
const put = (s) => {
|
|
3515
|
+
if (full) return;
|
|
3516
|
+
if (out.length + s.length > max) {
|
|
3517
|
+
out += s.slice(0, max - out.length);
|
|
3518
|
+
full = true;
|
|
3519
|
+
return;
|
|
3520
|
+
}
|
|
3521
|
+
out += s;
|
|
3522
|
+
};
|
|
3523
|
+
const walk = (v) => {
|
|
3524
|
+
if (full) return;
|
|
3525
|
+
switch (typeof v) {
|
|
3526
|
+
case "string":
|
|
3527
|
+
put(JSON.stringify(v.length > max ? v.slice(0, max) : v));
|
|
3528
|
+
if (v.length > max) full = true;
|
|
3529
|
+
return;
|
|
3530
|
+
case "number":
|
|
3531
|
+
put(String(v));
|
|
3532
|
+
return;
|
|
3533
|
+
case "bigint":
|
|
3534
|
+
put(`${v}n`);
|
|
3535
|
+
return;
|
|
3536
|
+
case "boolean":
|
|
3537
|
+
put(String(v));
|
|
3538
|
+
return;
|
|
3539
|
+
case "undefined":
|
|
3540
|
+
put("undefined");
|
|
3541
|
+
return;
|
|
3542
|
+
case "symbol":
|
|
3543
|
+
case "function":
|
|
3544
|
+
put("null");
|
|
3545
|
+
return;
|
|
3546
|
+
}
|
|
3547
|
+
if (v === null) return put("null");
|
|
3548
|
+
if (seen.has(v)) return put("[Circular]");
|
|
3549
|
+
const bin = binary(v);
|
|
3550
|
+
if (bin !== void 0) return put(bin);
|
|
3551
|
+
if (v instanceof Date) return put(Number.isNaN(v.getTime()) ? "Invalid Date" : v.toISOString());
|
|
3552
|
+
if (v instanceof RegExp) return put(String(v));
|
|
3553
|
+
seen.add(v);
|
|
3554
|
+
if (Array.isArray(v)) {
|
|
3555
|
+
put("[");
|
|
3556
|
+
v.forEach((x, i) => {
|
|
3557
|
+
if (i > 0) put(",");
|
|
3558
|
+
walk(x);
|
|
3559
|
+
});
|
|
3560
|
+
put("]");
|
|
3561
|
+
} else if (v instanceof Map) {
|
|
3562
|
+
put("Map{");
|
|
3563
|
+
let i = 0;
|
|
3564
|
+
for (const [k, x] of v) {
|
|
3565
|
+
if (full) break;
|
|
3566
|
+
if (i++ > 0) put(",");
|
|
3567
|
+
walk(k);
|
|
3568
|
+
put("=>");
|
|
3569
|
+
walk(x);
|
|
3570
|
+
}
|
|
3571
|
+
put("}");
|
|
3572
|
+
} else if (v instanceof Set) {
|
|
3573
|
+
put("Set[");
|
|
3574
|
+
let i = 0;
|
|
3575
|
+
for (const x of v) {
|
|
3576
|
+
if (full) break;
|
|
3577
|
+
if (i++ > 0) put(",");
|
|
3578
|
+
walk(x);
|
|
3579
|
+
}
|
|
3580
|
+
put("]");
|
|
3581
|
+
} else if (v instanceof Error) put(JSON.stringify(`${v.name}: ${v.message}`));
|
|
3582
|
+
else {
|
|
3583
|
+
put("{");
|
|
3584
|
+
let i = 0;
|
|
3585
|
+
for (const k of Object.keys(v)) {
|
|
3586
|
+
if (full) break;
|
|
3587
|
+
if (i++ > 0) put(",");
|
|
3588
|
+
put(JSON.stringify(k));
|
|
3589
|
+
put(":");
|
|
3590
|
+
walk(Reflect.get(v, k));
|
|
3591
|
+
}
|
|
3592
|
+
put("}");
|
|
3593
|
+
}
|
|
3594
|
+
seen.delete(v);
|
|
3595
|
+
};
|
|
3596
|
+
walk(value);
|
|
3597
|
+
return {
|
|
3598
|
+
text: out,
|
|
3599
|
+
truncated: full
|
|
3600
|
+
};
|
|
3601
|
+
}
|
|
3602
|
+
/** A retained record as the inspection feed shows it. */
|
|
3603
|
+
function recordPreview(topic, r, bytes) {
|
|
3604
|
+
const { text, truncated } = previewText(r.value);
|
|
3605
|
+
return {
|
|
3606
|
+
topic,
|
|
3607
|
+
offset: r.offset,
|
|
3608
|
+
...r.key === void 0 ? {} : { key: r.key },
|
|
3609
|
+
ts: r.ts,
|
|
3610
|
+
producer: r.producer,
|
|
3611
|
+
...r.req === void 0 ? {} : { req: r.req },
|
|
3612
|
+
bytes: bytes ?? estimateSize(r.value),
|
|
3613
|
+
preview: text,
|
|
3614
|
+
truncated
|
|
3615
|
+
};
|
|
3616
|
+
}
|
|
3617
|
+
// src/tap.ts
|
|
3618
|
+
/** `maxPerSec` as given when it's a positive number, else the default. */
|
|
3619
|
+
const tapRate = (n) => typeof n === "number" && Number.isFinite(n) && n > 0 ? n : 50;
|
|
3620
|
+
/** Starts a tap on `broker`. Returns the function that ends it; `close` on the port ends it too. */
|
|
3621
|
+
function openTap(broker, o) {
|
|
3622
|
+
const max = tapRate(o.maxPerSec);
|
|
3623
|
+
const now = o.now ?? (() => performance.now());
|
|
3624
|
+
const every = o.every ?? ((fn, ms) => {
|
|
3625
|
+
const t = setInterval(fn, ms);
|
|
3626
|
+
return () => clearInterval(t);
|
|
3627
|
+
});
|
|
3628
|
+
let windowAt = now();
|
|
3629
|
+
let sent = 0;
|
|
3630
|
+
let skipped = 0;
|
|
3631
|
+
let open = true;
|
|
3632
|
+
const untap = broker.tap(o.topics, (topic, rec, bytes) => {
|
|
3633
|
+
const t = now();
|
|
3634
|
+
if (t - windowAt >= 1e3) {
|
|
3635
|
+
windowAt = t;
|
|
3636
|
+
sent = 0;
|
|
3637
|
+
}
|
|
3638
|
+
if (sent >= max) {
|
|
3639
|
+
skipped++;
|
|
3640
|
+
return;
|
|
3641
|
+
}
|
|
3642
|
+
sent++;
|
|
3643
|
+
o.port.postMessage({
|
|
3644
|
+
v: 1,
|
|
3645
|
+
t: "record",
|
|
3646
|
+
p: recordPreview(topic, rec, bytes)
|
|
3647
|
+
});
|
|
3648
|
+
});
|
|
3649
|
+
const stopReport = every(() => {
|
|
3650
|
+
if (skipped === 0) return;
|
|
3651
|
+
o.port.postMessage({
|
|
3652
|
+
v: 1,
|
|
3653
|
+
t: "skipped",
|
|
3654
|
+
p: { count: skipped }
|
|
3655
|
+
});
|
|
3656
|
+
skipped = 0;
|
|
3657
|
+
}, 1e3);
|
|
3658
|
+
const end = () => {
|
|
3659
|
+
if (!open) return;
|
|
3660
|
+
open = false;
|
|
3661
|
+
untap();
|
|
3662
|
+
stopReport();
|
|
3663
|
+
o.port.close();
|
|
3664
|
+
};
|
|
3665
|
+
o.port.addEventListener("message", (e) => {
|
|
3666
|
+
if (isTapToBus(e.data) && e.data.t === "close") end();
|
|
3667
|
+
});
|
|
3668
|
+
o.port.start();
|
|
3669
|
+
return end;
|
|
3670
|
+
}
|
|
3273
3671
|
// src/server.ts
|
|
3274
3672
|
/** Storage use at or past this share of the quota counts as full (M2b task 14). */
|
|
3275
3673
|
const FULL = 1;
|
|
@@ -3431,6 +3829,39 @@ function serve(scope, opts = {}) {
|
|
|
3431
3829
|
});
|
|
3432
3830
|
return;
|
|
3433
3831
|
}
|
|
3832
|
+
case "request": {
|
|
3833
|
+
const { id } = msg;
|
|
3834
|
+
if (id === void 0) return;
|
|
3835
|
+
const answer = (p) => post({
|
|
3836
|
+
v: 1,
|
|
3837
|
+
t: "result",
|
|
3838
|
+
re: id,
|
|
3839
|
+
p
|
|
3840
|
+
});
|
|
3841
|
+
const key = msg.p.key;
|
|
3842
|
+
const via = msg.p.via;
|
|
3843
|
+
const timeoutMs = msg.p.timeoutMs;
|
|
3844
|
+
if (key !== void 0 && typeof key !== "string") return answer({
|
|
3845
|
+
ok: false,
|
|
3846
|
+
error: {
|
|
3847
|
+
code: "invalid",
|
|
3848
|
+
message: "key must be a string"
|
|
3849
|
+
}
|
|
3850
|
+
});
|
|
3851
|
+
if (via !== void 0 && !isRequestVia(via)) return answer({
|
|
3852
|
+
ok: false,
|
|
3853
|
+
error: {
|
|
3854
|
+
code: "invalid",
|
|
3855
|
+
message: `via must be one of ${REQUEST_VIA.join(", ")}`
|
|
3856
|
+
}
|
|
3857
|
+
});
|
|
3858
|
+
b.request(msg.p.topic, key, msg.p.value, {
|
|
3859
|
+
id,
|
|
3860
|
+
timeoutMs: typeof timeoutMs === "number" ? timeoutMs : 0,
|
|
3861
|
+
...via === void 0 ? {} : { via }
|
|
3862
|
+
}, answer, publishId(msg.p));
|
|
3863
|
+
return;
|
|
3864
|
+
}
|
|
3434
3865
|
case "flush": {
|
|
3435
3866
|
const { id } = msg;
|
|
3436
3867
|
b.flush().then(() => id !== void 0 && post({
|
|
@@ -3485,6 +3916,47 @@ function serve(scope, opts = {}) {
|
|
|
3485
3916
|
}));
|
|
3486
3917
|
return;
|
|
3487
3918
|
}
|
|
3919
|
+
case "tap":
|
|
3920
|
+
openTap(b, {
|
|
3921
|
+
topics: Array.isArray(msg.p.topics) ? msg.p.topics.filter((x) => typeof x === "string") : [],
|
|
3922
|
+
...msg.p.maxPerSec === void 0 ? {} : { maxPerSec: msg.p.maxPerSec },
|
|
3923
|
+
port: msg.p.port,
|
|
3924
|
+
now
|
|
3925
|
+
});
|
|
3926
|
+
return;
|
|
3927
|
+
case "read": {
|
|
3928
|
+
const { id } = msg;
|
|
3929
|
+
if (id === void 0) return;
|
|
3930
|
+
const answer = (p) => post({
|
|
3931
|
+
v: 1,
|
|
3932
|
+
t: "result",
|
|
3933
|
+
re: id,
|
|
3934
|
+
p
|
|
3935
|
+
});
|
|
3936
|
+
const topic = msg.p.topic;
|
|
3937
|
+
const last = msg.p.last;
|
|
3938
|
+
if (typeof last !== "number" || !Number.isInteger(last) || last < 1) return answer({
|
|
3939
|
+
ok: false,
|
|
3940
|
+
error: {
|
|
3941
|
+
code: "invalid",
|
|
3942
|
+
message: "last must be a positive whole number"
|
|
3943
|
+
}
|
|
3944
|
+
});
|
|
3945
|
+
b.read(topic, Math.min(last, 100)).then((r) => answer(r.ok ? {
|
|
3946
|
+
ok: true,
|
|
3947
|
+
value: r.records.map((rec) => recordPreview(topic, rec))
|
|
3948
|
+
} : {
|
|
3949
|
+
ok: false,
|
|
3950
|
+
error: r.error
|
|
3951
|
+
}), (e) => answer({
|
|
3952
|
+
ok: false,
|
|
3953
|
+
error: {
|
|
3954
|
+
code: "bus_unavailable",
|
|
3955
|
+
message: `read ${topic}: ${String(e)}`
|
|
3956
|
+
}
|
|
3957
|
+
}));
|
|
3958
|
+
return;
|
|
3959
|
+
}
|
|
3488
3960
|
default: return msg;
|
|
3489
3961
|
}
|
|
3490
3962
|
};
|
|
@@ -3508,18 +3980,21 @@ function serve(scope, opts = {}) {
|
|
|
3508
3980
|
ports.delete(msg.p.isolateId);
|
|
3509
3981
|
break;
|
|
3510
3982
|
case "publish":
|
|
3983
|
+
case "request":
|
|
3511
3984
|
if (earlyPublishes >= limit) {
|
|
3512
3985
|
if (msg.id !== void 0) post({
|
|
3513
3986
|
v: 1,
|
|
3514
3987
|
t: "result",
|
|
3515
3988
|
re: msg.id,
|
|
3516
|
-
p: unavailable(
|
|
3989
|
+
p: unavailable(`kernel.${msg.t}`)
|
|
3517
3990
|
});
|
|
3518
3991
|
return;
|
|
3519
3992
|
}
|
|
3520
3993
|
earlyPublishes++;
|
|
3521
3994
|
break;
|
|
3522
3995
|
case "drop":
|
|
3996
|
+
case "tap":
|
|
3997
|
+
case "read":
|
|
3523
3998
|
early.push(msg);
|
|
3524
3999
|
return;
|
|
3525
4000
|
case "flush":
|
|
@@ -3579,7 +4054,9 @@ function serve(scope, opts = {}) {
|
|
|
3579
4054
|
topics: stats.topics.map((x) => ({
|
|
3580
4055
|
topic: x.topic,
|
|
3581
4056
|
recordsPerSec: (x.records - (before.get(x.topic)?.records ?? 0)) / secs,
|
|
3582
|
-
bytesPerSec: (x.bytes - (before.get(x.topic)?.bytes ?? 0)) / secs
|
|
4057
|
+
bytesPerSec: (x.bytes - (before.get(x.topic)?.bytes ?? 0)) / secs,
|
|
4058
|
+
head: x.head,
|
|
4059
|
+
retained: x.retained
|
|
3583
4060
|
})),
|
|
3584
4061
|
subscriptions: stats.subscriptions,
|
|
3585
4062
|
rejected: stats.rejected,
|
|
@@ -3642,11 +4119,11 @@ function serve(scope, opts = {}) {
|
|
|
3642
4119
|
else if (!persistent) storage = new MemoryStorage();
|
|
3643
4120
|
else if (!OpfsStorage.available()) {
|
|
3644
4121
|
storage = new MemoryStorage();
|
|
3645
|
-
degraded = "
|
|
4122
|
+
degraded = "no_sync_access";
|
|
3646
4123
|
} else if (await takeBusLock(p.restart ? RESTART_LOCK_WAIT_MS : 0)) storage = new OpfsStorage();
|
|
3647
4124
|
else {
|
|
3648
4125
|
storage = new MemoryStorage();
|
|
3649
|
-
degraded = "
|
|
4126
|
+
degraded = "another_tab";
|
|
3650
4127
|
}
|
|
3651
4128
|
let quarantined;
|
|
3652
4129
|
let stored;
|
|
@@ -3657,7 +4134,7 @@ function serve(scope, opts = {}) {
|
|
|
3657
4134
|
console.warn("[isoloop] persistent topics are in memory: storage failed to open", e);
|
|
3658
4135
|
storage = new MemoryStorage();
|
|
3659
4136
|
stored = await storage.open();
|
|
3660
|
-
degraded = "
|
|
4137
|
+
degraded = "open_failed";
|
|
3661
4138
|
}
|
|
3662
4139
|
let built;
|
|
3663
4140
|
try {
|
|
@@ -3678,7 +4155,7 @@ function serve(scope, opts = {}) {
|
|
|
3678
4155
|
console.warn("[isoloop] persistent topics are in memory: reading them back failed", e);
|
|
3679
4156
|
storage = new MemoryStorage();
|
|
3680
4157
|
built = await build(list, storage, await storage.open(), dev);
|
|
3681
|
-
degraded = "
|
|
4158
|
+
degraded = "open_failed";
|
|
3682
4159
|
}
|
|
3683
4160
|
const { b, restored } = built;
|
|
3684
4161
|
broker = b;
|