@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/index.js
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
|
-
import { t as toIssues } from "./standard-schema-
|
|
2
|
-
import { SYSTEM_TOPICS, isIsolateToBus, isSystemTopic, matchAny, topicConfigOf } from "@isoloop/protocol";
|
|
1
|
+
import { t as toIssues } from "./standard-schema-BtwRaC9-.js";
|
|
2
|
+
import { JSON_SCHEMA_TARGET, SYSTEM_TOPICS, UNCHECKED_VENDOR, hasJsonSchema, isIsolateToBus, isRequestVia, isSystemTopic, jsonSchemaOf, matchAny, topicConfigOf, topicJsonSchemas } from "@isoloop/protocol";
|
|
3
3
|
import { CodecError, decode, encode } from "@isoloop/codec";
|
|
4
4
|
// src/topic.ts
|
|
5
|
-
/**
|
|
5
|
+
/** The default `maxRecordBytes`: 256 KiB. */
|
|
6
6
|
const DEFAULT_MAX_RECORD_BYTES = 262144;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Defines a topic. Its types come from its schemas.
|
|
9
|
+
*
|
|
10
|
+
* @param name - The topic's name, for example `cart.items.v1`.
|
|
11
|
+
* @param def - The topic's schemas, retention and options.
|
|
12
|
+
* @returns The topic, frozen. List it in `contracts()` to use it.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { topic } from '@isoloop/bus';
|
|
17
|
+
* import { z } from 'zod';
|
|
18
|
+
*
|
|
19
|
+
* export const cartItems = topic('cart.items.v1', {
|
|
20
|
+
* schema: z.object({ sku: z.string(), qty: z.number().int().positive() }),
|
|
21
|
+
* retention: { kind: 'compacted' },
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
8
25
|
function topic(name, def) {
|
|
9
26
|
return Object.freeze({
|
|
10
27
|
...def,
|
|
11
28
|
name
|
|
12
29
|
});
|
|
13
30
|
}
|
|
14
|
-
const UNCHECKED_VENDOR = "isoloop/unchecked";
|
|
15
31
|
/**
|
|
16
|
-
* A Standard Schema that accepts any value unchanged and only carries the type
|
|
17
|
-
*
|
|
32
|
+
* A Standard Schema that accepts any value unchanged and only carries the type.
|
|
33
|
+
* Tools list the topics that use it.
|
|
34
|
+
*
|
|
35
|
+
* @returns A schema whose input and output type is `T`.
|
|
18
36
|
*/
|
|
19
37
|
function unchecked() {
|
|
20
38
|
return { "~standard": {
|
|
@@ -23,15 +41,25 @@ function unchecked() {
|
|
|
23
41
|
validate: (value) => ({ value })
|
|
24
42
|
} };
|
|
25
43
|
}
|
|
26
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* True if the schema came from `unchecked()`.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
27
49
|
function isUnchecked(schema) {
|
|
28
50
|
return schema["~standard"].vendor === UNCHECKED_VENDOR;
|
|
29
51
|
}
|
|
30
52
|
/** A contracts list that can't be built: duplicate or reserved names, or a bad definition. */
|
|
31
53
|
var ContractsError = class extends Error {
|
|
54
|
+
/** `reserved_topic` for an `isoloop.*` name, `contracts_invalid` for anything else. */
|
|
32
55
|
code;
|
|
33
56
|
/** The topics at fault. */
|
|
34
57
|
topics;
|
|
58
|
+
/**
|
|
59
|
+
* @param code - The error code.
|
|
60
|
+
* @param message - What is wrong, naming the topics.
|
|
61
|
+
* @param topics - The topics at fault.
|
|
62
|
+
*/
|
|
35
63
|
constructor(code, message, topics) {
|
|
36
64
|
super(message);
|
|
37
65
|
this.name = "ContractsError";
|
|
@@ -47,6 +75,14 @@ const RETENTION_KINDS = /* @__PURE__ */ new Set([
|
|
|
47
75
|
const isRecord = (x) => typeof x === "object" && x !== null;
|
|
48
76
|
const isSchema = (x) => isRecord(x) && isRecord(x["~standard"]) && typeof x["~standard"]["validate"] === "function";
|
|
49
77
|
const positiveInt = (n) => typeof n === "number" && Number.isInteger(n) && n > 0;
|
|
78
|
+
const optionalBoolean = (x) => x === void 0 || typeof x === "boolean";
|
|
79
|
+
function toolProblemOf(tool) {
|
|
80
|
+
if (!isRecord(tool)) return "has a `tool` that is not an object";
|
|
81
|
+
if (typeof tool["description"] !== "string" || tool["description"] === "") return "has a `tool` without a `description`";
|
|
82
|
+
if (typeof tool["readOnly"] !== "boolean") return "has a `tool` without a boolean `readOnly`";
|
|
83
|
+
if (tool["title"] !== void 0 && typeof tool["title"] !== "string") return "has a `tool` whose `title` is not a string";
|
|
84
|
+
if (!optionalBoolean(tool["consequential"]) || !optionalBoolean(tool["untrustedContent"])) return "has a `tool` whose hints are not booleans";
|
|
85
|
+
}
|
|
50
86
|
function problemOf(t) {
|
|
51
87
|
if (typeof t.name !== "string" || t.name === "") return "has no name";
|
|
52
88
|
if (!isSchema(t.schema)) return "has no Standard Schema";
|
|
@@ -57,6 +93,10 @@ function problemOf(t) {
|
|
|
57
93
|
if (r.kind === "time" && !positiveInt(r.ms)) return "needs a positive integer `ms`";
|
|
58
94
|
if (t.maxRecordBytes !== void 0 && !positiveInt(t.maxRecordBytes)) return "needs a positive integer `maxRecordBytes`";
|
|
59
95
|
if (t.durability !== void 0 && t.durability !== "memory" && t.durability !== "persistent") return "has a durability other than 'memory' or 'persistent'";
|
|
96
|
+
if (t.tool !== void 0) {
|
|
97
|
+
const problem = toolProblemOf(t.tool);
|
|
98
|
+
if (problem) return problem;
|
|
99
|
+
}
|
|
60
100
|
if (t.seed !== void 0) {
|
|
61
101
|
const s = t.seed;
|
|
62
102
|
if (!isRecord(s) || !isRecord(s.from) || typeof s.from.name !== "string") return "has a seed without a `from` topic";
|
|
@@ -66,8 +106,27 @@ function problemOf(t) {
|
|
|
66
106
|
}
|
|
67
107
|
}
|
|
68
108
|
/**
|
|
69
|
-
* Builds the contracts list
|
|
70
|
-
*
|
|
109
|
+
* Builds the contracts list, the default export of a contracts module.
|
|
110
|
+
*
|
|
111
|
+
* @param topics - Every topic the app uses.
|
|
112
|
+
* @returns The list, frozen.
|
|
113
|
+
* @throws {ContractsError} `reserved_topic` for an `isoloop.*` name, which
|
|
114
|
+
* belongs to the kernel. `contracts_invalid` for a duplicate name, a topic
|
|
115
|
+
* without a valid schema or retention, a bad `tool` or `seed`, or seeds that
|
|
116
|
+
* form a cycle.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { contracts, topic } from '@isoloop/bus';
|
|
121
|
+
* import { z } from 'zod';
|
|
122
|
+
*
|
|
123
|
+
* const prices = topic('markets.prices.v1', {
|
|
124
|
+
* schema: z.object({ symbol: z.string(), last: z.number() }),
|
|
125
|
+
* retention: { kind: 'last-n', n: 1_000 },
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* export default contracts(prices);
|
|
129
|
+
* ```
|
|
71
130
|
*/
|
|
72
131
|
function contracts(...topics) {
|
|
73
132
|
const reserved = topics.filter((t) => isSystemTopic(t.name)).map((t) => t.name);
|
|
@@ -107,16 +166,28 @@ function seedCycle(byName) {
|
|
|
107
166
|
}
|
|
108
167
|
}
|
|
109
168
|
}
|
|
110
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* True for a value built by `contracts()`, from any copy of this package.
|
|
171
|
+
*
|
|
172
|
+
* @internal
|
|
173
|
+
*/
|
|
111
174
|
function isContracts(x) {
|
|
112
175
|
return isRecord(x) && x["~isoloop"] === "contracts";
|
|
113
176
|
}
|
|
114
|
-
/**
|
|
177
|
+
/**
|
|
178
|
+
* The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5).
|
|
179
|
+
*
|
|
180
|
+
* @internal
|
|
181
|
+
*/
|
|
115
182
|
function topicConfig(t) {
|
|
116
183
|
return topicConfigOf(t);
|
|
117
184
|
}
|
|
118
185
|
// src/engine/memory-storage.ts
|
|
119
|
-
/**
|
|
186
|
+
/**
|
|
187
|
+
* Storage that keeps nothing. Every topic behaves as `memory`.
|
|
188
|
+
*
|
|
189
|
+
* @internal
|
|
190
|
+
*/
|
|
120
191
|
var MemoryStorage = class {
|
|
121
192
|
kind = "memory";
|
|
122
193
|
open() {
|
|
@@ -151,7 +222,9 @@ var MemoryStorage = class {
|
|
|
151
222
|
* Engines call this from `createTopic`: the durability a topic actually gets.
|
|
152
223
|
* A `persistent` topic on storage that keeps nothing (no OPFS, or another tab
|
|
153
224
|
* holds it) behaves as `memory`; the kernel reports that as
|
|
154
|
-
* `bus:
|
|
225
|
+
* `bus:durability_degraded` (ADR 0007, decision 6).
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
155
228
|
*/
|
|
156
229
|
function effectiveDurability(cfg, storage) {
|
|
157
230
|
if (cfg.durability !== "persistent") return "memory";
|
|
@@ -207,7 +280,22 @@ function frameOffset(frame) {
|
|
|
207
280
|
return new DataView(frame.buffer, frame.byteOffset).getFloat64(8, true);
|
|
208
281
|
}
|
|
209
282
|
const isList = (x) => Array.isArray(x);
|
|
210
|
-
|
|
283
|
+
/** A stored `req`, rebuilt from its known fields; a `via` this version doesn't know is left out. */
|
|
284
|
+
function reqOf(x) {
|
|
285
|
+
if (typeof x !== "object" || x === null) return void 0;
|
|
286
|
+
const from = Reflect.get(x, "from");
|
|
287
|
+
const id = Reflect.get(x, "id");
|
|
288
|
+
if (typeof from !== "string" || typeof id !== "string") return void 0;
|
|
289
|
+
const via = Reflect.get(x, "via");
|
|
290
|
+
return isRequestVia(via) ? {
|
|
291
|
+
from,
|
|
292
|
+
id,
|
|
293
|
+
via
|
|
294
|
+
} : {
|
|
295
|
+
from,
|
|
296
|
+
id
|
|
297
|
+
};
|
|
298
|
+
}
|
|
211
299
|
/** Decodes one frame written by `encodeFrame`. */
|
|
212
300
|
function decodeFrame(frame) {
|
|
213
301
|
const offset = frameOffset(frame);
|
|
@@ -221,7 +309,8 @@ function decodeFrame(frame) {
|
|
|
221
309
|
producer
|
|
222
310
|
};
|
|
223
311
|
if (typeof key === "string") rec.key = key;
|
|
224
|
-
|
|
312
|
+
const r = reqOf(req);
|
|
313
|
+
if (r) rec.req = r;
|
|
225
314
|
if (typeof epoch === "number") rec.epoch = epoch;
|
|
226
315
|
if (typeof seq === "number") rec.seq = seq;
|
|
227
316
|
return rec;
|
|
@@ -236,6 +325,8 @@ const DIRTY_MIN = 1024;
|
|
|
236
325
|
* The TypeScript `LogEngine` (design §6.2). Each topic is an array of
|
|
237
326
|
* fixed-size chunks, so dropping old records frees a whole chunk at a time:
|
|
238
327
|
* O(1) per chunk. Compacted topics keep a key → latest-offset index.
|
|
328
|
+
*
|
|
329
|
+
* @internal
|
|
239
330
|
*/
|
|
240
331
|
var TsEngine = class {
|
|
241
332
|
name = "ts";
|
|
@@ -520,6 +611,9 @@ var TsEngine = class {
|
|
|
520
611
|
tail(topic) {
|
|
521
612
|
return this.#topic(topic).tail;
|
|
522
613
|
}
|
|
614
|
+
retained(topic) {
|
|
615
|
+
return this.#topic(topic).live;
|
|
616
|
+
}
|
|
523
617
|
commit(topic, consumer, offset) {
|
|
524
618
|
const t = this.#topic(topic);
|
|
525
619
|
t.commits.set(consumer, offset);
|
|
@@ -700,6 +794,8 @@ function remember(t, producer, id, offset) {
|
|
|
700
794
|
* §6.1). Strings count two bytes per UTF-16 unit, numbers eight, object keys
|
|
701
795
|
* like strings, plus a little per entry. Cycles count once. Cheap rather than
|
|
702
796
|
* exact: it only has to tell a 1 KiB record from a 1 MiB one.
|
|
797
|
+
*
|
|
798
|
+
* @internal
|
|
703
799
|
*/
|
|
704
800
|
function estimateSize(value, limit = Infinity) {
|
|
705
801
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -903,7 +999,11 @@ function hash(s) {
|
|
|
903
999
|
return h >>> 0;
|
|
904
1000
|
}
|
|
905
1001
|
const reqKey = (topic, offset) => `${topic}\u0000${offset}`;
|
|
906
|
-
/**
|
|
1002
|
+
/**
|
|
1003
|
+
* A message's `(epoch, seq)`, when it carries both as numbers.
|
|
1004
|
+
*
|
|
1005
|
+
* @internal
|
|
1006
|
+
*/
|
|
907
1007
|
function publishId(p) {
|
|
908
1008
|
const { epoch, seq } = p;
|
|
909
1009
|
return typeof epoch === "number" && typeof seq === "number" ? {
|
|
@@ -922,6 +1022,8 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
|
922
1022
|
* and consumer groups, delivers from the log under credit, and routes
|
|
923
1023
|
* request replies. Knows nothing about workers; the bus worker entry wires
|
|
924
1024
|
* ports to it.
|
|
1025
|
+
*
|
|
1026
|
+
* @internal
|
|
925
1027
|
*/
|
|
926
1028
|
var Broker = class {
|
|
927
1029
|
#engine;
|
|
@@ -936,6 +1038,12 @@ var Broker = class {
|
|
|
936
1038
|
#conns = /* @__PURE__ */ new Map();
|
|
937
1039
|
#groups = /* @__PURE__ */ new Map();
|
|
938
1040
|
#requests = /* @__PURE__ */ new Map();
|
|
1041
|
+
/**
|
|
1042
|
+
* Open request offsets per topic that may not be delivered yet, and the
|
|
1043
|
+
* tail last seen there: when retention moves the tail past one nobody was
|
|
1044
|
+
* handed, its caller gets `dropped` (ADR 0018, decision 5).
|
|
1045
|
+
*/
|
|
1046
|
+
#undelivered = /* @__PURE__ */ new Map();
|
|
939
1047
|
/** Consumers whose offsets were dropped by `detach { keepOffsets: false }`, per topic. */
|
|
940
1048
|
#forgotten = /* @__PURE__ */ new Set();
|
|
941
1049
|
#rejected = {};
|
|
@@ -947,6 +1055,8 @@ var Broker = class {
|
|
|
947
1055
|
#sources = /* @__PURE__ */ new Set();
|
|
948
1056
|
#followReports = /* @__PURE__ */ new Map();
|
|
949
1057
|
#dropped = 0;
|
|
1058
|
+
/** Inspection taps, with the topic patterns each one matches. */
|
|
1059
|
+
#taps = /* @__PURE__ */ new Map();
|
|
950
1060
|
constructor(opts) {
|
|
951
1061
|
this.#engine = opts.engine;
|
|
952
1062
|
this.#now = opts.now ?? Date.now;
|
|
@@ -1026,7 +1136,9 @@ var Broker = class {
|
|
|
1026
1136
|
topics: [...this.#topics].map(([topic, t]) => ({
|
|
1027
1137
|
topic,
|
|
1028
1138
|
records: t.records,
|
|
1029
|
-
bytes: t.bytes
|
|
1139
|
+
bytes: t.bytes,
|
|
1140
|
+
head: this.#engine.head(topic),
|
|
1141
|
+
retained: this.#engine.retained(topic)
|
|
1030
1142
|
})),
|
|
1031
1143
|
subscriptions,
|
|
1032
1144
|
rejected: { ...this.#rejected }
|
|
@@ -1250,6 +1362,46 @@ var Broker = class {
|
|
|
1250
1362
|
publish(from, topic, key, value, id) {
|
|
1251
1363
|
return new Promise((resolve) => this.#publish(from, topic, key, value, void 0, resolve, id));
|
|
1252
1364
|
}
|
|
1365
|
+
/**
|
|
1366
|
+
* A request from the host (`kernel.request`, ADR 0018, decision 4): like
|
|
1367
|
+
* an isolate's `bus.request`, with `req: { from: 'host', id, via }` on the
|
|
1368
|
+
* record. `answer` gets one result: a failure to append, or the first
|
|
1369
|
+
* valid reply (or `reply_invalid`). Nothing, if no reply comes before the
|
|
1370
|
+
* caller's timeout; the request is forgotten after `timeoutMs`.
|
|
1371
|
+
*/
|
|
1372
|
+
request(topic, key, value, opts, answer, appendId) {
|
|
1373
|
+
let answered = false;
|
|
1374
|
+
const once = (r) => {
|
|
1375
|
+
if (answered) return;
|
|
1376
|
+
answered = true;
|
|
1377
|
+
answer(r);
|
|
1378
|
+
};
|
|
1379
|
+
const req = {
|
|
1380
|
+
from: {
|
|
1381
|
+
isolateId: HOST_PRODUCER,
|
|
1382
|
+
grants: {
|
|
1383
|
+
publish: [],
|
|
1384
|
+
subscribe: []
|
|
1385
|
+
},
|
|
1386
|
+
port: {
|
|
1387
|
+
post: (m) => {
|
|
1388
|
+
if (m.t === "result") once(m.p);
|
|
1389
|
+
},
|
|
1390
|
+
onMessage: () => {},
|
|
1391
|
+
close: () => {}
|
|
1392
|
+
},
|
|
1393
|
+
subs: /* @__PURE__ */ new Map(),
|
|
1394
|
+
reset: void 0,
|
|
1395
|
+
host: true
|
|
1396
|
+
},
|
|
1397
|
+
id: opts.id,
|
|
1398
|
+
timeoutMs: opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_REQUEST_TIMEOUT_MS,
|
|
1399
|
+
...opts.via === void 0 ? {} : { via: opts.via }
|
|
1400
|
+
};
|
|
1401
|
+
this.#publish({ kind: "host" }, topic, key, value, req, (r) => {
|
|
1402
|
+
if (!r.ok) once(r);
|
|
1403
|
+
}, appendId);
|
|
1404
|
+
}
|
|
1253
1405
|
#receive(conn, msg) {
|
|
1254
1406
|
if (!isIsolateToBus(msg) || msg.t !== "commits" && !hasTopic(msg.p)) {
|
|
1255
1407
|
this.#dropped++;
|
|
@@ -1348,7 +1500,7 @@ var Broker = class {
|
|
|
1348
1500
|
value: null
|
|
1349
1501
|
};
|
|
1350
1502
|
for (const e of offsets) {
|
|
1351
|
-
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");
|
|
1503
|
+
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");
|
|
1352
1504
|
if (!r.ok && first.ok) first = r;
|
|
1353
1505
|
}
|
|
1354
1506
|
reply(first);
|
|
@@ -1453,13 +1605,17 @@ var Broker = class {
|
|
|
1453
1605
|
...rec,
|
|
1454
1606
|
key: p.key
|
|
1455
1607
|
};
|
|
1456
|
-
if (p.req)
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1608
|
+
if (p.req) {
|
|
1609
|
+
const { via } = p.req;
|
|
1610
|
+
rec = {
|
|
1611
|
+
...rec,
|
|
1612
|
+
req: {
|
|
1613
|
+
from: p.req.from.isolateId,
|
|
1614
|
+
id: p.req.id,
|
|
1615
|
+
...via === void 0 ? {} : { via }
|
|
1616
|
+
}
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1463
1619
|
const offset = this.#engine.append(name, rec, p.id);
|
|
1464
1620
|
topic.records++;
|
|
1465
1621
|
topic.bytes += p.bytes;
|
|
@@ -1469,12 +1625,73 @@ var Broker = class {
|
|
|
1469
1625
|
ok: true,
|
|
1470
1626
|
value: { offset }
|
|
1471
1627
|
});
|
|
1628
|
+
this.#dropEvicted(name);
|
|
1472
1629
|
this.#fanOut(name);
|
|
1473
1630
|
this.#wake(name);
|
|
1474
1631
|
this.#follow(name, {
|
|
1475
1632
|
...rec,
|
|
1476
1633
|
offset
|
|
1477
1634
|
});
|
|
1635
|
+
if (this.#taps.size > 0) this.#toTaps(name, {
|
|
1636
|
+
...rec,
|
|
1637
|
+
offset
|
|
1638
|
+
}, p.bytes);
|
|
1639
|
+
}
|
|
1640
|
+
#toTaps(name, rec, bytes) {
|
|
1641
|
+
for (const [sink, patterns] of this.#taps) {
|
|
1642
|
+
if (!matchAny(patterns, name)) continue;
|
|
1643
|
+
try {
|
|
1644
|
+
sink(name, rec, bytes);
|
|
1645
|
+
} catch (e) {
|
|
1646
|
+
console.error("[isoloop] bus: a tap threw", e);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
/**
|
|
1651
|
+
* The inspection feed's tap (ADR 0020, decision 3): `sink` gets every
|
|
1652
|
+
* record appended to a topic matching one of `patterns` from now on,
|
|
1653
|
+
* through the publish pipeline. Seeds copying into a topic before it opens
|
|
1654
|
+
* aren't tapped. Returns the function that ends it.
|
|
1655
|
+
*/
|
|
1656
|
+
tap(patterns, sink) {
|
|
1657
|
+
this.#taps.set(sink, [...patterns]);
|
|
1658
|
+
return () => {
|
|
1659
|
+
this.#taps.delete(sink);
|
|
1660
|
+
};
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* The last `last` retained records of `topic`, oldest first (ADR 0020,
|
|
1664
|
+
* decision 3), paging evicted records in from disk when needed. Seed
|
|
1665
|
+
* sources the contracts don't list are unknown here, as to isolates.
|
|
1666
|
+
*/
|
|
1667
|
+
async read(name, last) {
|
|
1668
|
+
if (!this.#topics.has(name)) return {
|
|
1669
|
+
ok: false,
|
|
1670
|
+
error: {
|
|
1671
|
+
code: "unknown_topic",
|
|
1672
|
+
message: `unknown topic ${name}`
|
|
1673
|
+
}
|
|
1674
|
+
};
|
|
1675
|
+
if (!Number.isInteger(last) || last < 1) return {
|
|
1676
|
+
ok: false,
|
|
1677
|
+
error: {
|
|
1678
|
+
code: "invalid",
|
|
1679
|
+
message: "last must be a positive whole number"
|
|
1680
|
+
}
|
|
1681
|
+
};
|
|
1682
|
+
const tail = this.#engine.tail(name);
|
|
1683
|
+
const head = this.#engine.head(name);
|
|
1684
|
+
let span = last;
|
|
1685
|
+
for (;;) {
|
|
1686
|
+
const from = Math.max(tail, head - span);
|
|
1687
|
+
if (!this.#engine.readable(name, from)) await this.#engine.page(name, from);
|
|
1688
|
+
const got = this.#engine.read(name, from, Number.MAX_SAFE_INTEGER);
|
|
1689
|
+
if (got.length >= last || from <= this.#engine.tail(name)) return {
|
|
1690
|
+
ok: true,
|
|
1691
|
+
records: got.slice(-last)
|
|
1692
|
+
};
|
|
1693
|
+
span *= 2;
|
|
1694
|
+
}
|
|
1478
1695
|
}
|
|
1479
1696
|
/**
|
|
1480
1697
|
* Turns a schema result into the value to store, or the error to send. A
|
|
@@ -1521,6 +1738,41 @@ var Broker = class {
|
|
|
1521
1738
|
recipients: /* @__PURE__ */ new Set(),
|
|
1522
1739
|
done: false
|
|
1523
1740
|
});
|
|
1741
|
+
let u = this.#undelivered.get(topic);
|
|
1742
|
+
if (!u) {
|
|
1743
|
+
u = {
|
|
1744
|
+
offsets: [],
|
|
1745
|
+
tail: this.#engine.tail(topic)
|
|
1746
|
+
};
|
|
1747
|
+
this.#undelivered.set(topic, u);
|
|
1748
|
+
}
|
|
1749
|
+
u.offsets.push(offset);
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* Answers `dropped` to each caller whose request retention removed before
|
|
1753
|
+
* the bus delivered it to any subscriber (ADR 0018, decision 5). A request
|
|
1754
|
+
* already delivered stays its responder's to answer. Runs only when the
|
|
1755
|
+
* topic's tail has moved.
|
|
1756
|
+
*/
|
|
1757
|
+
#dropEvicted(topic) {
|
|
1758
|
+
const u = this.#undelivered.get(topic);
|
|
1759
|
+
if (!u) return;
|
|
1760
|
+
const tail = this.#engine.tail(topic);
|
|
1761
|
+
if (tail === u.tail) return;
|
|
1762
|
+
u.tail = tail;
|
|
1763
|
+
const kept = [];
|
|
1764
|
+
for (const offset of u.offsets) {
|
|
1765
|
+
if (offset >= tail) {
|
|
1766
|
+
kept.push(offset);
|
|
1767
|
+
continue;
|
|
1768
|
+
}
|
|
1769
|
+
const req = this.#requests.get(reqKey(topic, offset));
|
|
1770
|
+
if (!req || req.done || req.recipients.size > 0) continue;
|
|
1771
|
+
req.done = true;
|
|
1772
|
+
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"));
|
|
1773
|
+
}
|
|
1774
|
+
if (kept.length === 0) this.#undelivered.delete(topic);
|
|
1775
|
+
else u.offsets = kept;
|
|
1524
1776
|
}
|
|
1525
1777
|
#reply(conn, name, offset, value, settle) {
|
|
1526
1778
|
const topic = this.#topics.get(name);
|
|
@@ -1540,7 +1792,7 @@ var Broker = class {
|
|
|
1540
1792
|
if (req.done) return settle(fail("already_replied", `request ${name}@${offset} has an answer`));
|
|
1541
1793
|
req.done = true;
|
|
1542
1794
|
const checked = this.#check(name, r);
|
|
1543
|
-
const callerAlive = this.#conns.get(req.caller.isolateId) === req.caller;
|
|
1795
|
+
const callerAlive = req.caller.host === true || this.#conns.get(req.caller.isolateId) === req.caller;
|
|
1544
1796
|
if (!checked.ok) {
|
|
1545
1797
|
settle(checked);
|
|
1546
1798
|
if (callerAlive) {
|
|
@@ -1679,17 +1931,21 @@ var Broker = class {
|
|
|
1679
1931
|
* A commit covers the records delivered up to `offset`. In a group it also
|
|
1680
1932
|
* needs their delivery's `seq` at or below `seq`: a record redelivered to
|
|
1681
1933
|
* this member after the deliveries the commit settled has a higher one,
|
|
1682
|
-
* whatever its offset (ADR 0016, point 12).
|
|
1683
|
-
*
|
|
1934
|
+
* whatever its offset (ADR 0016, point 12). `partial` also covers the
|
|
1935
|
+
* first records of delivery `seq + 1`, the ones settled before a member
|
|
1936
|
+
* stops partway through it (point 17). An own subscription's deliveries
|
|
1937
|
+
* are in order, so the offset alone decides there.
|
|
1684
1938
|
*/
|
|
1685
|
-
#commit(conn, name, offset, seq) {
|
|
1939
|
+
#commit(conn, name, offset, seq, partial) {
|
|
1686
1940
|
const sub = conn.subs.get(name);
|
|
1687
1941
|
if (!sub) return fail("forbidden", `not subscribed to ${name}`);
|
|
1688
1942
|
if (!isOffset(offset)) return fail("invalid", "offset must be a non-negative integer");
|
|
1689
1943
|
if (!isSeq(seq)) return fail("invalid", "seq must be a non-negative integer");
|
|
1944
|
+
if (partial !== void 0 && !isSeq(partial)) return fail("invalid", "partial must be a non-negative integer");
|
|
1945
|
+
const part = isSeq(partial) ? partial : 0;
|
|
1690
1946
|
if (offset >= this.#engine.head(name)) return fail("invalid", `offset ${offset} is not in ${name} yet`);
|
|
1691
1947
|
const g = sub.group;
|
|
1692
|
-
const covers = (d) => d.offset <= offset &&
|
|
1948
|
+
const covers = (d) => g ? d.offset <= offset && d.seq <= seq || d.seq === seq + 1 && d.i < part : d.offset <= offset;
|
|
1693
1949
|
const before = sub.unacked.length;
|
|
1694
1950
|
if (g) sub.unacked = sub.unacked.filter((d) => !covers(d));
|
|
1695
1951
|
else while (sub.unacked[0] && covers(sub.unacked[0])) sub.unacked.shift();
|
|
@@ -1761,10 +2017,11 @@ var Broker = class {
|
|
|
1761
2017
|
/** Sends one `deliver`, numbered with the subscription's next `seq`, and returns it. */
|
|
1762
2018
|
#deliver(sub, records, gap) {
|
|
1763
2019
|
const seq = ++sub.seq;
|
|
1764
|
-
for (const r of records) {
|
|
2020
|
+
for (const [i, r] of records.entries()) {
|
|
1765
2021
|
sub.unacked.push({
|
|
1766
2022
|
offset: r.offset,
|
|
1767
|
-
seq
|
|
2023
|
+
seq,
|
|
2024
|
+
i
|
|
1768
2025
|
});
|
|
1769
2026
|
if (r.req) this.#requests.get(reqKey(sub.topic, r.offset))?.recipients.add(sub.conn.isolateId);
|
|
1770
2027
|
}
|
|
@@ -1831,7 +2088,10 @@ var Broker = class {
|
|
|
1831
2088
|
batches.delete(m);
|
|
1832
2089
|
if (!b || b.records.length === 0) return;
|
|
1833
2090
|
const seq = this.#deliver(m, b.records);
|
|
1834
|
-
for (const
|
|
2091
|
+
for (const { entry, i } of b.entries) {
|
|
2092
|
+
entry.seq = seq;
|
|
2093
|
+
entry.i = i;
|
|
2094
|
+
}
|
|
1835
2095
|
};
|
|
1836
2096
|
/** Credit left once the records batched for it go out. */
|
|
1837
2097
|
const room = (m) => m ? m.credit - (batches.get(m)?.records.length ?? 0) : 0;
|
|
@@ -1840,8 +2100,11 @@ var Broker = class {
|
|
|
1840
2100
|
records: [],
|
|
1841
2101
|
entries: []
|
|
1842
2102
|
};
|
|
2103
|
+
if (entry) b.entries.push({
|
|
2104
|
+
entry,
|
|
2105
|
+
i: b.records.length
|
|
2106
|
+
});
|
|
1843
2107
|
b.records.push(rec);
|
|
1844
|
-
if (entry) b.entries.push(entry);
|
|
1845
2108
|
batches.set(m, b);
|
|
1846
2109
|
if (rec.key === void 0) g.rr++;
|
|
1847
2110
|
if (b.records.length >= this.#batch) flush(m);
|
|
@@ -1897,7 +2160,8 @@ var Broker = class {
|
|
|
1897
2160
|
const entry = {
|
|
1898
2161
|
offset: rec.offset,
|
|
1899
2162
|
member: m,
|
|
1900
|
-
seq: 0
|
|
2163
|
+
seq: 0,
|
|
2164
|
+
i: 0
|
|
1901
2165
|
};
|
|
1902
2166
|
g.unacked.push(entry);
|
|
1903
2167
|
give(m, rec, entry);
|
|
@@ -1918,4 +2182,4 @@ function validate(schema, value) {
|
|
|
1918
2182
|
if (!(r instanceof Promise)) return r;
|
|
1919
2183
|
return r.then((x) => x, (thrown) => ({ thrown }));
|
|
1920
2184
|
}
|
|
1921
|
-
export { Broker, ContractsError, DEFAULT_MAX_RECORD_BYTES, DEFAULT_REQUEST_TIMEOUT_MS, HOST_PRODUCER, KERNEL_PRODUCER, MemoryStorage, TsEngine, contracts, effectiveDurability, estimateSize, isContracts, isUnchecked, publishId, toIssues, topic, topicConfig, unchecked };
|
|
2185
|
+
export { Broker, ContractsError, DEFAULT_MAX_RECORD_BYTES, DEFAULT_REQUEST_TIMEOUT_MS, HOST_PRODUCER, JSON_SCHEMA_TARGET, KERNEL_PRODUCER, MemoryStorage, TsEngine, contracts, effectiveDurability, estimateSize, hasJsonSchema, isContracts, isUnchecked, jsonSchemaOf, publishId, toIssues, topic, topicConfig, topicJsonSchemas, unchecked };
|
|
@@ -3,6 +3,8 @@ const segmentKey = (s) => typeof s === "object" ? s.key : s;
|
|
|
3
3
|
/**
|
|
4
4
|
* Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
|
|
5
5
|
* (ADR 0003, decision 10). Symbol keys become their description.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
6
8
|
*/
|
|
7
9
|
function toIssues(issues) {
|
|
8
10
|
return issues.map((i) => ({
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { n as Contracts } from "./topic-
|
|
1
|
+
import { n as Contracts } from "./topic-BpgHePIE.js";
|
|
2
2
|
import { Issue } from "@isoloop/protocol";
|
|
3
3
|
/** Sample values for one topic. An array is shorthand for `{ values }`. */
|
|
4
4
|
export type TopicSamples = readonly unknown[] | {
|
|
5
5
|
values: readonly unknown[];
|
|
6
6
|
replies?: readonly unknown[];
|
|
7
7
|
};
|
|
8
|
+
/** One problem `testContracts` found. */
|
|
8
9
|
export type ContractProblem = {
|
|
10
|
+
/** The topic's name. */
|
|
9
11
|
topic: string;
|
|
10
12
|
/** `value` for the topic schema, `reply` for its reply schema. */
|
|
11
13
|
schema: "value" | "reply";
|
|
@@ -16,13 +18,19 @@ export type ContractProblem = {
|
|
|
16
18
|
* plain, structured-cloneable data. `no_samples`: the topic has none.
|
|
17
19
|
*/
|
|
18
20
|
kind: "invalid" | "not_plain" | "no_samples";
|
|
21
|
+
/** What is wrong, in one line. */
|
|
19
22
|
message: string;
|
|
23
|
+
/** The schema's issues, for `invalid`. */
|
|
20
24
|
issues?: Issue[];
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
23
27
|
* Runs sample values through every schema in a contracts list and reports
|
|
24
|
-
* samples the schema rejects and outputs that aren't plain, cloneable data
|
|
25
|
-
*
|
|
26
|
-
*
|
|
28
|
+
* samples the schema rejects and outputs that aren't plain, cloneable data.
|
|
29
|
+
* The bus only checks cloneability in dev builds, so run this in the contracts
|
|
30
|
+
* package's tests.
|
|
31
|
+
*
|
|
32
|
+
* @param list - The contracts list.
|
|
33
|
+
* @param samples - Sample values per topic name. A topic without samples is reported as `no_samples`.
|
|
34
|
+
* @returns Every problem found. An empty array means all good.
|
|
27
35
|
*/
|
|
28
36
|
export declare function testContracts(list: Contracts, samples: Readonly<Record<string, TopicSamples>>): Promise<ContractProblem[]>;
|
package/dist/testing.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as toIssues } from "./standard-schema-
|
|
1
|
+
import { t as toIssues } from "./standard-schema-BtwRaC9-.js";
|
|
2
2
|
// src/testing.ts
|
|
3
3
|
const PLAIN_TAGS = /* @__PURE__ */ new Set([
|
|
4
4
|
"[object Date]",
|
|
@@ -71,9 +71,13 @@ async function check(topic, which, schema, samples) {
|
|
|
71
71
|
const isList = (s) => Array.isArray(s);
|
|
72
72
|
/**
|
|
73
73
|
* Runs sample values through every schema in a contracts list and reports
|
|
74
|
-
* samples the schema rejects and outputs that aren't plain, cloneable data
|
|
75
|
-
*
|
|
76
|
-
*
|
|
74
|
+
* samples the schema rejects and outputs that aren't plain, cloneable data.
|
|
75
|
+
* The bus only checks cloneability in dev builds, so run this in the contracts
|
|
76
|
+
* package's tests.
|
|
77
|
+
*
|
|
78
|
+
* @param list - The contracts list.
|
|
79
|
+
* @param samples - Sample values per topic name. A topic without samples is reported as `no_samples`.
|
|
80
|
+
* @returns Every problem found. An empty array means all good.
|
|
77
81
|
*/
|
|
78
82
|
async function testContracts(list, samples) {
|
|
79
83
|
return (await Promise.all(list.topics.map(async (t) => {
|