@isoloop/bus 0.1.0-next.8 → 0.1.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 +39 -0
- package/dist/bus-worker.js +41 -15
- package/dist/index.d.ts +3 -397
- package/dist/index.js +60 -1998
- package/dist/testing.d.ts +12 -4
- package/dist/testing.js +24 -4
- package/dist/topic-BkBLtTpT.d.ts +184 -0
- package/package.json +19 -5
- package/dist/standard-schema-DnJtEmJw.js +0 -16
- package/dist/topic-BgqMD0WA.d.ts +0 -135
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @isoloop/bus
|
|
2
|
+
|
|
3
|
+
The [Isoloop](https://isoloop.dev) bus: a log of typed topics in its own worker, through which the parts of your app share data and catch up after a restart.
|
|
4
|
+
|
|
5
|
+
## What you get
|
|
6
|
+
|
|
7
|
+
- **Contracts:** topics with schemas from any [Standard Schema](https://standardschema.dev) library, typed where you use them and checked on every publish.
|
|
8
|
+
- **Retention:** keep the newest records, a span of time, or the latest record for each key.
|
|
9
|
+
- **Replay:** a subscriber that starts late, or restarts, reads what it needs from the log.
|
|
10
|
+
- **Backpressure:** records wait in the log for a busy subscriber, within the topic's retention.
|
|
11
|
+
- **History that lasts:** a persistent topic's records and committed positions survive a reload and a restart of the bus.
|
|
12
|
+
- **Request and reply** (experimental): ask on a topic and get a typed answer.
|
|
13
|
+
- **Contract tests:** `testContracts` from `@isoloop/bus/testing` checks your schemas against samples.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @isoloop/bus zod
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Example
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { contracts, topic } from '@isoloop/bus';
|
|
25
|
+
import { z } from 'zod';
|
|
26
|
+
|
|
27
|
+
export const cartItems = topic('cart.items.v1', {
|
|
28
|
+
schema: z.object({ sku: z.string(), qty: z.number().int().positive() }),
|
|
29
|
+
retention: { kind: 'compacted' },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export default contracts(cartItems);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Learn more
|
|
36
|
+
|
|
37
|
+
- [The bus](https://isoloop.dev/docs/bus/)
|
|
38
|
+
- [Contracts and grants](https://isoloop.dev/docs/contracts/)
|
|
39
|
+
- [API reference](https://isoloop.dev/docs/api/bus/)
|
package/dist/bus-worker.js
CHANGED
|
@@ -701,12 +701,14 @@ function bigint(v) {
|
|
|
701
701
|
}
|
|
702
702
|
bytes(v < 0n ? NBIGINT : BIGINT, -1, Uint8Array.from(out));
|
|
703
703
|
}
|
|
704
|
-
// src/size.ts
|
|
704
|
+
// src/broker/size.ts
|
|
705
705
|
/**
|
|
706
706
|
* A rough structured size of a value in bytes, for `maxRecordBytes` (design
|
|
707
707
|
* §6.1). Strings count two bytes per UTF-16 unit, numbers eight, object keys
|
|
708
708
|
* like strings, plus a little per entry. Cycles count once. Cheap rather than
|
|
709
709
|
* exact: it only has to tell a 1 KiB record from a 1 MiB one.
|
|
710
|
+
*
|
|
711
|
+
* @internal
|
|
710
712
|
*/
|
|
711
713
|
function estimateSize(value, limit = Infinity) {
|
|
712
714
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -886,11 +888,13 @@ async function copySeed(c) {
|
|
|
886
888
|
rejected
|
|
887
889
|
};
|
|
888
890
|
}
|
|
889
|
-
// src/standard-schema.ts
|
|
891
|
+
// src/contracts/standard-schema.ts
|
|
890
892
|
const segmentKey = (s) => typeof s === "object" ? s.key : s;
|
|
891
893
|
/**
|
|
892
894
|
* Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
|
|
893
895
|
* (ADR 0003, decision 10). Symbol keys become their description.
|
|
896
|
+
*
|
|
897
|
+
* @internal
|
|
894
898
|
*/
|
|
895
899
|
function toIssues(issues) {
|
|
896
900
|
return issues.map((i) => ({
|
|
@@ -902,15 +906,23 @@ function toIssues(issues) {
|
|
|
902
906
|
}));
|
|
903
907
|
}
|
|
904
908
|
const isRecord = (x) => typeof x === "object" && x !== null;
|
|
905
|
-
/**
|
|
909
|
+
/**
|
|
910
|
+
* True for a value built by `contracts()`, from any copy of this package.
|
|
911
|
+
*
|
|
912
|
+
* @internal
|
|
913
|
+
*/
|
|
906
914
|
function isContracts(x) {
|
|
907
915
|
return isRecord(x) && x["~isoloop"] === "contracts";
|
|
908
916
|
}
|
|
909
|
-
/**
|
|
917
|
+
/**
|
|
918
|
+
* The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5).
|
|
919
|
+
*
|
|
920
|
+
* @internal
|
|
921
|
+
*/
|
|
910
922
|
function topicConfig(t) {
|
|
911
923
|
return topicConfigOf(t);
|
|
912
924
|
}
|
|
913
|
-
// src/broker.ts
|
|
925
|
+
// src/broker/broker.ts
|
|
914
926
|
const fail = (code, message, extra) => ({
|
|
915
927
|
ok: false,
|
|
916
928
|
error: {
|
|
@@ -934,7 +946,11 @@ function hash(s) {
|
|
|
934
946
|
return h >>> 0;
|
|
935
947
|
}
|
|
936
948
|
const reqKey = (topic, offset) => `${topic}\u0000${offset}`;
|
|
937
|
-
/**
|
|
949
|
+
/**
|
|
950
|
+
* A message's `(epoch, seq)`, when it carries both as numbers.
|
|
951
|
+
*
|
|
952
|
+
* @internal
|
|
953
|
+
*/
|
|
938
954
|
function publishId(p) {
|
|
939
955
|
const { epoch, seq } = p;
|
|
940
956
|
return typeof epoch === "number" && typeof seq === "number" ? {
|
|
@@ -953,6 +969,8 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
|
953
969
|
* and consumer groups, delivers from the log under credit, and routes
|
|
954
970
|
* request replies. Knows nothing about workers; the bus worker entry wires
|
|
955
971
|
* ports to it.
|
|
972
|
+
*
|
|
973
|
+
* @internal
|
|
956
974
|
*/
|
|
957
975
|
var Broker = class {
|
|
958
976
|
#engine;
|
|
@@ -2112,7 +2130,11 @@ function validate(schema, value) {
|
|
|
2112
2130
|
return r.then((x) => x, (thrown) => ({ thrown }));
|
|
2113
2131
|
}
|
|
2114
2132
|
// src/engine/memory-storage.ts
|
|
2115
|
-
/**
|
|
2133
|
+
/**
|
|
2134
|
+
* Storage that keeps nothing. Every topic behaves as `memory`.
|
|
2135
|
+
*
|
|
2136
|
+
* @internal
|
|
2137
|
+
*/
|
|
2116
2138
|
var MemoryStorage = class {
|
|
2117
2139
|
kind = "memory";
|
|
2118
2140
|
open() {
|
|
@@ -2147,7 +2169,9 @@ var MemoryStorage = class {
|
|
|
2147
2169
|
* Engines call this from `createTopic`: the durability a topic actually gets.
|
|
2148
2170
|
* A `persistent` topic on storage that keeps nothing (no OPFS, or another tab
|
|
2149
2171
|
* holds it) behaves as `memory`; the kernel reports that as
|
|
2150
|
-
* `bus:
|
|
2172
|
+
* `bus:durability_degraded` (ADR 0007, decision 6).
|
|
2173
|
+
*
|
|
2174
|
+
* @internal
|
|
2151
2175
|
*/
|
|
2152
2176
|
function effectiveDurability(cfg, storage) {
|
|
2153
2177
|
if (cfg.durability !== "persistent") return "memory";
|
|
@@ -2266,6 +2290,8 @@ const DIRTY_MIN = 1024;
|
|
|
2266
2290
|
* The TypeScript `LogEngine` (design §6.2). Each topic is an array of
|
|
2267
2291
|
* fixed-size chunks, so dropping old records frees a whole chunk at a time:
|
|
2268
2292
|
* O(1) per chunk. Compacted topics keep a key → latest-offset index.
|
|
2293
|
+
*
|
|
2294
|
+
* @internal
|
|
2269
2295
|
*/
|
|
2270
2296
|
var TsEngine = class {
|
|
2271
2297
|
name = "ts";
|
|
@@ -3471,7 +3497,7 @@ var OpfsStorage = class {
|
|
|
3471
3497
|
this.#topics.clear();
|
|
3472
3498
|
}
|
|
3473
3499
|
};
|
|
3474
|
-
// src/preview.ts
|
|
3500
|
+
// src/server/preview.ts
|
|
3475
3501
|
/** Typed arrays and `ArrayBuffer`s show their length, not their contents. */
|
|
3476
3502
|
const binary = (v) => {
|
|
3477
3503
|
if (v instanceof ArrayBuffer) return `ArrayBuffer(${v.byteLength})`;
|
|
@@ -3588,7 +3614,7 @@ function recordPreview(topic, r, bytes) {
|
|
|
3588
3614
|
truncated
|
|
3589
3615
|
};
|
|
3590
3616
|
}
|
|
3591
|
-
// src/tap.ts
|
|
3617
|
+
// src/server/tap.ts
|
|
3592
3618
|
/** `maxPerSec` as given when it's a positive number, else the default. */
|
|
3593
3619
|
const tapRate = (n) => typeof n === "number" && Number.isFinite(n) && n > 0 ? n : 50;
|
|
3594
3620
|
/** Starts a tap on `broker`. Returns the function that ends it; `close` on the port ends it too. */
|
|
@@ -3642,7 +3668,7 @@ function openTap(broker, o) {
|
|
|
3642
3668
|
o.port.start();
|
|
3643
3669
|
return end;
|
|
3644
3670
|
}
|
|
3645
|
-
// src/server.ts
|
|
3671
|
+
// src/server/server.ts
|
|
3646
3672
|
/** Storage use at or past this share of the quota counts as full (M2b task 14). */
|
|
3647
3673
|
const FULL = 1;
|
|
3648
3674
|
/** How long a restarting bus waits for the worker it replaces to release the files' lock. */
|
|
@@ -4093,11 +4119,11 @@ function serve(scope, opts = {}) {
|
|
|
4093
4119
|
else if (!persistent) storage = new MemoryStorage();
|
|
4094
4120
|
else if (!OpfsStorage.available()) {
|
|
4095
4121
|
storage = new MemoryStorage();
|
|
4096
|
-
degraded = "
|
|
4122
|
+
degraded = "no_sync_access";
|
|
4097
4123
|
} else if (await takeBusLock(p.restart ? RESTART_LOCK_WAIT_MS : 0)) storage = new OpfsStorage();
|
|
4098
4124
|
else {
|
|
4099
4125
|
storage = new MemoryStorage();
|
|
4100
|
-
degraded = "
|
|
4126
|
+
degraded = "another_tab";
|
|
4101
4127
|
}
|
|
4102
4128
|
let quarantined;
|
|
4103
4129
|
let stored;
|
|
@@ -4108,7 +4134,7 @@ function serve(scope, opts = {}) {
|
|
|
4108
4134
|
console.warn("[isoloop] persistent topics are in memory: storage failed to open", e);
|
|
4109
4135
|
storage = new MemoryStorage();
|
|
4110
4136
|
stored = await storage.open();
|
|
4111
|
-
degraded = "
|
|
4137
|
+
degraded = "open_failed";
|
|
4112
4138
|
}
|
|
4113
4139
|
let built;
|
|
4114
4140
|
try {
|
|
@@ -4129,7 +4155,7 @@ function serve(scope, opts = {}) {
|
|
|
4129
4155
|
console.warn("[isoloop] persistent topics are in memory: reading them back failed", e);
|
|
4130
4156
|
storage = new MemoryStorage();
|
|
4131
4157
|
built = await build(list, storage, await storage.open(), dev);
|
|
4132
|
-
degraded = "
|
|
4158
|
+
degraded = "open_failed";
|
|
4133
4159
|
}
|
|
4134
4160
|
const { b, restored } = built;
|
|
4135
4161
|
broker = b;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,403 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/** A record before the engine gives it an offset. */
|
|
4
|
-
type NewRecord = Omit<BusRecord, "offset">;
|
|
5
|
-
/** A publish's instance and counter, for deduplicating re-sends (ADR 0007, step 5). */
|
|
6
|
-
type AppendId = {
|
|
7
|
-
epoch: number;
|
|
8
|
-
seq: number;
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Where the bus keeps its logs (design §6.2). The TS engine implements it now,
|
|
12
|
-
* the Rust/WASM engine in M5; both must pass `runEngineConformance`.
|
|
13
|
-
*
|
|
14
|
-
* Offsets are per topic, start at 0, are monotonic and never reused (I5).
|
|
15
|
-
* Retention: `last-n` is applied on append; `time` and `compacted` on
|
|
16
|
-
* `compact()`, which the broker calls on a timer. An engine may also compact
|
|
17
|
-
* a `compacted` topic on its own once enough records are superseded.
|
|
18
|
-
*/
|
|
19
|
-
interface LogEngine {
|
|
20
|
-
/** Throws if the topic exists. */
|
|
21
|
-
createTopic(cfg: TopicConfig): void;
|
|
22
|
-
/**
|
|
23
|
-
* Appends and returns the record's offset. Throws on an unknown topic. On a
|
|
24
|
-
* durable topic, `id` is stored with the record (never delivered) and
|
|
25
|
-
* remembered for `appended`; other topics ignore it.
|
|
26
|
-
*/
|
|
27
|
-
append(topic: string, rec: NewRecord, id?: AppendId): number;
|
|
28
|
-
/**
|
|
29
|
-
* The offset of the record `producer` appended to a durable topic as `id`,
|
|
30
|
-
* including records restored from disk; undefined when the engine doesn't
|
|
31
|
-
* know it. At least the producer's last 1,000 appends per topic are kept
|
|
32
|
-
* (the `BusClient`'s inbox limit), for its current epoch only.
|
|
33
|
-
*/
|
|
34
|
-
appended(topic: string, producer: string, id: AppendId): number | undefined;
|
|
35
|
-
/** Up to `max` retained records with `offset >= fromOffset`, ascending. Skips dropped offsets. */
|
|
36
|
-
read(topic: string, fromOffset: number, max: number): BusRecord[];
|
|
37
|
-
/** Next offset to be written. */
|
|
38
|
-
head(topic: string): number;
|
|
39
|
-
/** Oldest retained offset; equals `head` when nothing is retained. */
|
|
40
|
-
tail(topic: string): number;
|
|
41
|
-
/**
|
|
42
|
-
* How many records are retained in `[tail, head)`: what `read` from the
|
|
43
|
-
* tail would return, counting records evicted to disk (ADR 0020, decision 2).
|
|
44
|
-
*/
|
|
45
|
-
retained(topic: string): number;
|
|
46
|
-
/** Stores a consumer's committed offset. Kept even when the records it names are dropped. */
|
|
47
|
-
commit(topic: string, consumer: string, offset: number): void;
|
|
48
|
-
committed(topic: string, consumer: string): number | undefined;
|
|
49
|
-
/**
|
|
50
|
-
* Retention pass: drops superseded (`compacted`) and expired (`time`)
|
|
51
|
-
* records. Expiry runs in offset order and stops at the first record young
|
|
52
|
-
* enough, so a record with an older `ts` behind it is kept until it reaches the tail.
|
|
53
|
-
*/
|
|
54
|
-
compact(topic: string): void;
|
|
55
|
-
/**
|
|
56
|
-
* Whether appends to `topic` go to durable storage: a `persistent` topic on
|
|
57
|
-
* storage that keeps it (ADR 0007). The broker acks those after `flush`.
|
|
58
|
-
*/
|
|
59
|
-
durable(topic: string): boolean;
|
|
60
|
-
/**
|
|
61
|
-
* Resolves once every append so far to a durable topic is on disk. `now`
|
|
62
|
-
* writes the pending batch at once instead of at the next group commit (the
|
|
63
|
-
* page is freezing or going away).
|
|
64
|
-
*/
|
|
65
|
-
flush(o?: {
|
|
66
|
-
now?: boolean;
|
|
67
|
-
}): Promise<void>;
|
|
68
|
-
/**
|
|
69
|
-
* Whether `read(topic, fromOffset, …)` can answer now. False when the
|
|
70
|
-
* records from there were evicted to disk and aren't paged in: call `page`
|
|
71
|
-
* first (M2b task 11). Memory topics are always readable.
|
|
72
|
-
*/
|
|
73
|
-
readable(topic: string, fromOffset: number): boolean;
|
|
74
|
-
/** Pages evicted records from `fromOffset` back in from disk, so `read` sees them. */
|
|
75
|
-
page(topic: string, fromOffset: number): Promise<void>;
|
|
76
|
-
}
|
|
77
|
-
/** Options every engine takes. */
|
|
78
|
-
type EngineOptions = {
|
|
79
|
-
/** Clock for `time` retention. Default `Date.now`. */
|
|
80
|
-
now?: () => number;
|
|
81
|
-
/** Default `MemoryStorage`. */
|
|
82
|
-
storage?: LogStorage;
|
|
83
|
-
};
|
|
84
|
-
/** What a storage found on open: per topic, the offsets it holds; per topic and consumer, commits. */
|
|
85
|
-
type StoredState = {
|
|
86
|
-
topics: {
|
|
87
|
-
topic: string;
|
|
88
|
-
tail: number;
|
|
89
|
-
head: number;
|
|
90
|
-
}[];
|
|
91
|
-
offsets: Record<string, Record<string, number>>;
|
|
92
|
-
};
|
|
93
|
-
/**
|
|
94
|
-
* Durable storage under the engine, for `durability: 'persistent'` topics
|
|
95
|
-
* (design §6.2, ADR 0007). M2 ships `MemoryStorage`, which keeps nothing;
|
|
96
|
-
* M2b adds `OpfsStorage`.
|
|
97
|
-
*/
|
|
98
|
-
interface LogStorage {
|
|
99
|
-
/** `memory` keeps nothing; reported in the bus `ready` message. */
|
|
100
|
-
readonly kind: "memory" | "opfs";
|
|
101
|
-
open(): Promise<StoredState>;
|
|
102
|
-
/** Buffered; group commit every 5 ms / 256 records. */
|
|
103
|
-
appendBatch(topic: string, encoded: Uint8Array[]): void;
|
|
104
|
-
/**
|
|
105
|
-
* Resolves when the batch is durable; a persistent publish is acked after
|
|
106
|
-
* it. `now` commits the pending batch without waiting for the group commit.
|
|
107
|
-
*/
|
|
108
|
-
flush(o?: {
|
|
109
|
-
now?: boolean;
|
|
110
|
-
}): Promise<void>;
|
|
111
|
-
saveOffsets(offsets: Record<string, Record<string, number>>): void;
|
|
112
|
-
/**
|
|
113
|
-
* Retention on disk (last-n, time): deletes the segments whose records are
|
|
114
|
-
* all below `tail`. Never the newest segment.
|
|
115
|
-
*/
|
|
116
|
-
dropBelow(topic: string, tail: number): void;
|
|
117
|
-
/**
|
|
118
|
-
* Compaction on disk: rewrites the segments before the newest one, keeping
|
|
119
|
-
* the records `keep` accepts. In the background; appends don't wait for it.
|
|
120
|
-
*/
|
|
121
|
-
rewrite(topic: string, keep: (offset: number, key: string | undefined) => boolean): Promise<void>;
|
|
122
|
-
/** Replay of evicted segments. */
|
|
123
|
-
readSegment(topic: string, fromOffset: number): Promise<Uint8Array[]>;
|
|
124
|
-
/** Bytes a topic takes on disk; 0 for one the storage doesn't hold. */
|
|
125
|
-
bytes(topic: string): Promise<number>;
|
|
126
|
-
/** Deletes a topic's files. False when the storage doesn't hold it. */
|
|
127
|
-
deleteTopic(topic: string): Promise<boolean>;
|
|
128
|
-
/** Safe mode: moves the newest segments aside and returns their names. */
|
|
129
|
-
quarantineNewest(): Promise<string[]>;
|
|
130
|
-
}
|
|
131
|
-
/** Storage that keeps nothing. Every topic behaves as `memory`. */
|
|
132
|
-
export declare class MemoryStorage implements LogStorage {
|
|
133
|
-
readonly kind: "memory";
|
|
134
|
-
open(): Promise<StoredState>;
|
|
135
|
-
appendBatch(_topic: string, _encoded: Uint8Array[]): void;
|
|
136
|
-
flush(): Promise<void>;
|
|
137
|
-
saveOffsets(_offsets: Record<string, Record<string, number>>): void;
|
|
138
|
-
dropBelow(_topic: string, _tail: number): void;
|
|
139
|
-
rewrite(_topic: string, _keep: (offset: number, key: string | undefined) => boolean): Promise<void>;
|
|
140
|
-
readSegment(_topic: string, _fromOffset: number): Promise<Uint8Array[]>;
|
|
141
|
-
bytes(_topic: string): Promise<number>;
|
|
142
|
-
deleteTopic(_topic: string): Promise<boolean>;
|
|
143
|
-
quarantineNewest(): Promise<string[]>;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Engines call this from `createTopic`: the durability a topic actually gets.
|
|
147
|
-
* A `persistent` topic on storage that keeps nothing (no OPFS, or another tab
|
|
148
|
-
* holds it) behaves as `memory`; the kernel reports that as
|
|
149
|
-
* `bus:durability-degraded` (ADR 0007, decision 6).
|
|
150
|
-
*/
|
|
151
|
-
export declare function effectiveDurability(cfg: TopicConfig, storage: LogStorage): "memory" | "persistent";
|
|
152
|
-
type TsEngineOptions = EngineOptions & {
|
|
153
|
-
/** Records per chunk. Default 4,096; tests use small chunks to cross boundaries often. */
|
|
154
|
-
chunkSize?: number;
|
|
155
|
-
/** Superseded records before a compacted topic compacts itself. Default 1,024. */
|
|
156
|
-
dirtyMin?: number;
|
|
157
|
-
/**
|
|
158
|
-
* Durable topics: about how many encoded bytes of records each keeps in
|
|
159
|
-
* memory. Older chunks are evicted; `page` reads them back from disk.
|
|
160
|
-
* Default 8 MiB, two segments. The newest chunk always stays.
|
|
161
|
-
*/
|
|
162
|
-
residentBytes?: number;
|
|
163
|
-
};
|
|
164
|
-
/**
|
|
165
|
-
* The TypeScript `LogEngine` (design §6.2). Each topic is an array of
|
|
166
|
-
* fixed-size chunks, so dropping old records frees a whole chunk at a time:
|
|
167
|
-
* O(1) per chunk. Compacted topics keep a key → latest-offset index.
|
|
168
|
-
*/
|
|
169
|
-
export declare class TsEngine implements LogEngine {
|
|
170
|
-
#private;
|
|
171
|
-
readonly name = "ts";
|
|
172
|
-
constructor(opts?: TsEngineOptions);
|
|
173
|
-
createTopic(cfg: TopicConfig): void;
|
|
174
|
-
append(topic: string, rec: NewRecord, id?: AppendId): number;
|
|
175
|
-
appended(topic: string, producer: string, id: AppendId): number | undefined;
|
|
176
|
-
/**
|
|
177
|
-
* Rebuilds durable topics from what the storage found on open (M2b task 7):
|
|
178
|
-
* every stored record, in order and at its own offset, with retention
|
|
179
|
-
* applied as if it had just been appended, and the committed offsets.
|
|
180
|
-
* Call after `createTopic`, before any append. Returns each restored
|
|
181
|
-
* topic's head.
|
|
182
|
-
*/
|
|
183
|
-
restore(state: StoredState): Promise<{
|
|
184
|
-
topic: string;
|
|
185
|
-
head: number;
|
|
186
|
-
}[]>;
|
|
187
|
-
read(topic: string, fromOffset: number, max: number): BusRecord[];
|
|
188
|
-
head(topic: string): number;
|
|
189
|
-
readable(topic: string, fromOffset: number): boolean;
|
|
190
|
-
page(topic: string, fromOffset: number): Promise<void>;
|
|
191
|
-
/** Lowest offset held in memory; below it, records are on disk only. For tests and metrics. */
|
|
192
|
-
resident(topic: string): number;
|
|
193
|
-
tail(topic: string): number;
|
|
194
|
-
retained(topic: string): number;
|
|
195
|
-
commit(topic: string, consumer: string, offset: number): void;
|
|
196
|
-
committed(topic: string, consumer: string): number | undefined;
|
|
197
|
-
durable(topic: string): boolean;
|
|
198
|
-
flush(o?: {
|
|
199
|
-
now?: boolean;
|
|
200
|
-
}): Promise<void>;
|
|
201
|
-
compact(topic: string): void;
|
|
202
|
-
/** Chunks held for a topic, for tests and metrics. */
|
|
203
|
-
chunkCount(topic: string): number;
|
|
204
|
-
/** Records held in memory for a topic, for tests and metrics. */
|
|
205
|
-
heldCount(topic: string): number;
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* One isolate's bus connection, as the broker sees it. The bus worker wraps a
|
|
209
|
-
* `MessagePort`; tests use in-memory ports.
|
|
210
|
-
*/
|
|
211
|
-
export interface BusPort {
|
|
212
|
-
post(msg: BusToIsolate): void;
|
|
213
|
-
/** Sets the handler for messages from the isolate. */
|
|
214
|
-
onMessage(handler: (msg: unknown) => void): void;
|
|
215
|
-
close(): void;
|
|
216
|
-
}
|
|
217
|
-
/** Timer functions, injectable for tests. */
|
|
218
|
-
export type Timers = {
|
|
219
|
-
set(fn: () => void, ms: number): void;
|
|
220
|
-
};
|
|
221
|
-
export type BrokerOptions = {
|
|
222
|
-
contracts: Contracts;
|
|
223
|
-
engine: LogEngine;
|
|
224
|
-
/** Default `Date.now`. Stamps `ts` on every record. */
|
|
225
|
-
now?: () => number;
|
|
226
|
-
/**
|
|
227
|
-
* Dev builds check that every validated output survives `structuredClone`
|
|
228
|
-
* (ADR 0003, decision 11). Off in production.
|
|
229
|
-
*/
|
|
230
|
-
dev?: boolean;
|
|
231
|
-
/** Records delivered per `deliver` message. Default 64. */
|
|
232
|
-
batchSize?: number;
|
|
233
|
-
/**
|
|
234
|
-
* Credit per subscription: records delivered and not yet committed before
|
|
235
|
-
* delivery pauses (design §6.3). Default 1,000.
|
|
236
|
-
*/
|
|
237
|
-
inboxLimit?: number;
|
|
238
|
-
/** Built-in `isoloop.*` topics. Default `SYSTEM_TOPICS` from `@isoloop/protocol`. */
|
|
239
|
-
systemTopics?: readonly AnyTopicDef[];
|
|
240
|
-
/** Default `setTimeout`. Used to forget requests after their timeout. */
|
|
241
|
-
timers?: Timers;
|
|
242
|
-
/**
|
|
243
|
-
* True for topics whose log survived a bus restart. Default: the engine's
|
|
244
|
-
* `durable`, persistent topics on storage that keeps them. A reattached
|
|
245
|
-
* isolate subscribing to any other topic gets `reset` first (ADR 0007).
|
|
246
|
-
*/
|
|
247
|
-
durable?: (topic: string) => boolean;
|
|
248
|
-
/**
|
|
249
|
-
* A record arrived on a topic a suspended isolate subscribed to (ADR 0001).
|
|
250
|
-
* The bus worker sends `wake` on the control port. Called once per sleep.
|
|
251
|
-
*/
|
|
252
|
-
onWake?: (isolateId: string, topic: string) => void;
|
|
253
|
-
/** Seeding news for the host: rejected source records and ended seeds (ADR 0003, decision 18). */
|
|
254
|
-
onSeed?: (e: SeedEvent) => void;
|
|
255
|
-
};
|
|
256
|
-
/**
|
|
257
|
-
* Gets each record appended to a tapped topic (ADR 0020, decision 3), with
|
|
258
|
-
* its estimated size. A tap is not a consumer: it has no cursor or credit.
|
|
259
|
-
*/
|
|
260
|
-
export type TapSink = (topic: string, rec: BusRecord, bytes: number) => void;
|
|
261
|
-
/** Who is publishing: an isolate (grants apply), the host (`kernel.publish`), or the kernel itself (system topics). */
|
|
262
|
-
export type Publisher = {
|
|
263
|
-
kind: "isolate";
|
|
264
|
-
isolateId: string;
|
|
265
|
-
} | {
|
|
266
|
-
kind: "host";
|
|
267
|
-
} | {
|
|
268
|
-
kind: "kernel";
|
|
269
|
-
};
|
|
270
|
-
/** Cumulative counters for metrics (design §6.3); the bus worker turns them into rates. */
|
|
271
|
-
export type BrokerStats = {
|
|
272
|
-
/** `head` and `retained` are the engine's, at the time of the sample (ADR 0020, decision 2). */
|
|
273
|
-
topics: {
|
|
274
|
-
topic: string;
|
|
275
|
-
records: number;
|
|
276
|
-
bytes: number;
|
|
277
|
-
head: number;
|
|
278
|
-
retained: number;
|
|
279
|
-
}[];
|
|
280
|
-
/** `lag` is `head − cursor`: records not yet delivered. */
|
|
281
|
-
subscriptions: {
|
|
282
|
-
consumer: string;
|
|
283
|
-
topic: string;
|
|
284
|
-
lag: number;
|
|
285
|
-
credit: number;
|
|
286
|
-
}[];
|
|
287
|
-
/** Rejected publishes and requests, by error code. */
|
|
288
|
-
rejected: Partial<Record<ErrorCode, number>>;
|
|
289
|
-
};
|
|
290
|
-
/** A message's `(epoch, seq)`, when it carries both as numbers. */
|
|
291
|
-
export declare function publishId(p: {
|
|
292
|
-
epoch?: unknown;
|
|
293
|
-
seq?: unknown;
|
|
294
|
-
}): AppendId | undefined;
|
|
1
|
+
import { a as InputOf, c as ReplyOutputOf, d as TopicDef, f as contracts, g as StandardSchemaV1, h as unchecked, i as DEFAULT_MAX_RECORD_BYTES, l as Retention, m as topic, n as Contracts, o as OutputOf, p as isContracts, r as ContractsError, s as ReplyInputOf, t as AnyTopicDef, u as ToolSpec } from "./topic-BkBLtTpT.js";
|
|
2
|
+
import { JSON_SCHEMA_TARGET, JsonSchema, JsonSchemaPair, JsonSchemaResult, NotExportableReason, StandardJSONSchemaV1, TopicJsonSchemas, TopicJsonSchemasResult, hasJsonSchema, jsonSchemaOf, topicJsonSchemas } from "@isoloop/protocol";
|
|
295
3
|
/** Producer id for records the host publishes through `kernel.publish`. */
|
|
296
4
|
export declare const HOST_PRODUCER = "host";
|
|
297
5
|
/** Producer id for records the kernel publishes to system topics. */
|
|
298
6
|
export declare const KERNEL_PRODUCER = "kernel";
|
|
299
7
|
/** Requests are forgotten after this long when the caller gives no timeout. */
|
|
300
8
|
export declare const DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
301
|
-
|
|
302
|
-
* The bus core (design §6.3): runs the publish pipeline, keeps subscriptions
|
|
303
|
-
* and consumer groups, delivers from the log under credit, and routes
|
|
304
|
-
* request replies. Knows nothing about workers; the bus worker entry wires
|
|
305
|
-
* ports to it.
|
|
306
|
-
*/
|
|
307
|
-
export declare class Broker {
|
|
308
|
-
#private;
|
|
309
|
-
constructor(opts: BrokerOptions);
|
|
310
|
-
/** Messages dropped as malformed or unknown (design §3: dropped and counted). */
|
|
311
|
-
get dropped(): number;
|
|
312
|
-
/** Counters for the 1 s metrics sample. */
|
|
313
|
-
stats(): BrokerStats;
|
|
314
|
-
/**
|
|
315
|
-
* The origin's storage is full (M2b task 14): publishes to persistent
|
|
316
|
-
* topics reject with `quota_exceeded`, memory topics carry on.
|
|
317
|
-
*/
|
|
318
|
-
setStorageFull(full: boolean): void;
|
|
319
|
-
/**
|
|
320
|
-
* Writes what persistent topics have pending at once: the page is freezing
|
|
321
|
-
* or going away (ADR 0001, what the browser still controls).
|
|
322
|
-
*/
|
|
323
|
-
flush(): Promise<void>;
|
|
324
|
-
/**
|
|
325
|
-
* Starts every declared seed whose topic is empty and whose source has
|
|
326
|
-
* records, or is seeding itself (ADR 0003, decision 18). The topics are
|
|
327
|
-
* marked seeding before this returns, so call it before any connection is
|
|
328
|
-
* attached. Resolves when every seed has ended. The bus worker calls it
|
|
329
|
-
* only when it holds the files: elsewhere persistent topics start empty.
|
|
330
|
-
*/
|
|
331
|
-
seed(): Promise<void>;
|
|
332
|
-
/**
|
|
333
|
-
* Why `kernel.dropTopic(name)` may not delete `name`, if it may not (ADR
|
|
334
|
-
* 0003, decision 19): the contracts list it, a declared seed that reads it
|
|
335
|
-
* hasn't ended, or seed records rejected from it are recorded and `force`
|
|
336
|
-
* isn't set.
|
|
337
|
-
*/
|
|
338
|
-
dropRefusal(name: string, force: boolean): CallResult | undefined;
|
|
339
|
-
/** Persistent topics a declared seed reads that the contracts don't list. */
|
|
340
|
-
get seedSources(): readonly string[];
|
|
341
|
-
/** Retention pass over every topic (`time` and `compacted`); the bus worker runs it every second. */
|
|
342
|
-
compact(): void;
|
|
343
|
-
/**
|
|
344
|
-
* Connects an isolate. A second attach for the same id replaces the first.
|
|
345
|
-
* With `reattach`, the isolate was connected to a bus that restarted: its
|
|
346
|
-
* first subscribe to each memory topic is answered with `reset` (ADR 0007).
|
|
347
|
-
*/
|
|
348
|
-
attach(isolateId: string, grants: Grants, port: BusPort, opts?: {
|
|
349
|
-
reattach?: boolean;
|
|
350
|
-
}): void;
|
|
351
|
-
/**
|
|
352
|
-
* Disconnects an isolate, drops its subscriptions and leaves its groups,
|
|
353
|
-
* which rebalance. With `keepOffsets` its commits stay, so a restart with
|
|
354
|
-
* `from: 'committed'` resumes (I6). Replies to its pending requests are dropped.
|
|
355
|
-
*/
|
|
356
|
-
detach(isolateId: string, keepOffsets: boolean, opts?: {
|
|
357
|
-
wake?: boolean;
|
|
358
|
-
}): void;
|
|
359
|
-
/**
|
|
360
|
-
* Publishes for the host (`kernel.publish`) or the kernel (system topics).
|
|
361
|
-
* The host can't publish system topics; the kernel can only publish them.
|
|
362
|
-
*/
|
|
363
|
-
publish(from: Publisher, topic: string, key: string | undefined, value: unknown, id?: AppendId): Promise<CallResult>;
|
|
364
|
-
/**
|
|
365
|
-
* A request from the host (`kernel.request`, ADR 0018, decision 4): like
|
|
366
|
-
* an isolate's `bus.request`, with `req: { from: 'host', id, via }` on the
|
|
367
|
-
* record. `answer` gets one result: a failure to append, or the first
|
|
368
|
-
* valid reply (or `reply_invalid`). Nothing, if no reply comes before the
|
|
369
|
-
* caller's timeout; the request is forgotten after `timeoutMs`.
|
|
370
|
-
*/
|
|
371
|
-
request(topic: string, key: string | undefined, value: unknown, opts: {
|
|
372
|
-
id: string;
|
|
373
|
-
timeoutMs: number;
|
|
374
|
-
via?: RequestVia;
|
|
375
|
-
}, answer: (r: CallResult) => void, appendId?: AppendId): void;
|
|
376
|
-
/**
|
|
377
|
-
* The inspection feed's tap (ADR 0020, decision 3): `sink` gets every
|
|
378
|
-
* record appended to a topic matching one of `patterns` from now on,
|
|
379
|
-
* through the publish pipeline. Seeds copying into a topic before it opens
|
|
380
|
-
* aren't tapped. Returns the function that ends it.
|
|
381
|
-
*/
|
|
382
|
-
tap(patterns: readonly string[], sink: TapSink): () => void;
|
|
383
|
-
/**
|
|
384
|
-
* The last `last` retained records of `topic`, oldest first (ADR 0020,
|
|
385
|
-
* decision 3), paging evicted records in from disk when needed. Seed
|
|
386
|
-
* sources the contracts don't list are unknown here, as to isolates.
|
|
387
|
-
*/
|
|
388
|
-
read(name: string, last: number): Promise<{
|
|
389
|
-
ok: true;
|
|
390
|
-
records: BusRecord[];
|
|
391
|
-
} | {
|
|
392
|
-
ok: false;
|
|
393
|
-
error: CallError;
|
|
394
|
-
}>;
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* A rough structured size of a value in bytes, for `maxRecordBytes` (design
|
|
398
|
-
* §6.1). Strings count two bytes per UTF-16 unit, numbers eight, object keys
|
|
399
|
-
* like strings, plus a little per entry. Cycles count once. Cheap rather than
|
|
400
|
-
* exact: it only has to tell a 1 KiB record from a 1 MiB one.
|
|
401
|
-
*/
|
|
402
|
-
export declare function estimateSize(value: unknown, limit?: number): number;
|
|
403
|
-
export { AnyTopicDef, Contracts, ContractsError, DEFAULT_MAX_RECORD_BYTES, type EngineOptions, InputOf, JSON_SCHEMA_TARGET, type JsonSchema, type JsonSchemaPair, type JsonSchemaResult, type LogEngine, type LogStorage, type NewRecord, type NotExportableReason, OutputOf, ReplyInputOf, ReplyOutputOf, type Retention, type StandardJSONSchemaV1, StandardSchemaV1, type StoredState, type ToolSpec, TopicDef, type TopicJsonSchemas, type TopicJsonSchemasResult, type TsEngineOptions, contracts, hasJsonSchema, isContracts, isUnchecked, jsonSchemaOf, toIssues, topic, topicConfig, topicJsonSchemas, unchecked };
|
|
9
|
+
export { type AnyTopicDef, type Contracts, ContractsError, DEFAULT_MAX_RECORD_BYTES, type InputOf, JSON_SCHEMA_TARGET, type JsonSchema, type JsonSchemaPair, type JsonSchemaResult, type NotExportableReason, type OutputOf, type ReplyInputOf, type ReplyOutputOf, type Retention, type StandardJSONSchemaV1, type StandardSchemaV1, type ToolSpec, type TopicDef, type TopicJsonSchemas, type TopicJsonSchemasResult, contracts, hasJsonSchema, isContracts, jsonSchemaOf, topic, topicJsonSchemas, unchecked };
|