@isoloop/protocol 0.0.0-next.69da74b
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/LICENSE +21 -0
- package/dist/index.d.ts +521 -0
- package/dist/index.js +255 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sabin Pandelovitch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/** Protocol version this build speaks (`v`) and the oldest it still accepts (`min`). */
|
|
2
|
+
export declare const PROTOCOL: {
|
|
3
|
+
readonly v: 1;
|
|
4
|
+
readonly min: 1;
|
|
5
|
+
};
|
|
6
|
+
/** Every message on every port is an envelope. */
|
|
7
|
+
export type Envelope<T extends string, P> = {
|
|
8
|
+
/** Protocol version. */
|
|
9
|
+
v: 1;
|
|
10
|
+
/** Message type. */
|
|
11
|
+
t: T;
|
|
12
|
+
/** Set when a reply is expected. */
|
|
13
|
+
id?: string;
|
|
14
|
+
/** Id of the message this replies to. */
|
|
15
|
+
re?: string;
|
|
16
|
+
p: P;
|
|
17
|
+
};
|
|
18
|
+
/** Any envelope, before its type is known. */
|
|
19
|
+
export type AnyEnvelope = Envelope<string, unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* True for a well-formed envelope this build can read: `v` is a supported
|
|
22
|
+
* version, `t` is a string, `id`/`re` are absent or strings, and `p` is present.
|
|
23
|
+
* Says nothing about whether `t` is known; see the per-port guards.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isEnvelope(x: unknown): x is AnyEnvelope;
|
|
26
|
+
/**
|
|
27
|
+
* The message types of one port, as a map so the compiler checks that every
|
|
28
|
+
* type in the union is listed: a missing key is a type error.
|
|
29
|
+
*/
|
|
30
|
+
export type MessageTypes<E extends AnyEnvelope> = Readonly<Record<E["t"], true>>;
|
|
31
|
+
/**
|
|
32
|
+
* Builds a guard for one port's union from its message types. Each side drops
|
|
33
|
+
* (and counts) envelopes with an unknown `v` or `t` (design §3), so a guard
|
|
34
|
+
* only checks the envelope and the type, not the payload.
|
|
35
|
+
*/
|
|
36
|
+
export declare function envelopeGuard<E extends AnyEnvelope>(types: MessageTypes<E>): (x: unknown) => x is E;
|
|
37
|
+
/** Exhaustiveness check for switches over message unions. */
|
|
38
|
+
export declare function assertNever(x: never, message?: string): never;
|
|
39
|
+
/** Every error code on the wire (design §3, §4.7, §6.1, §6.3; ADRs 0002, 0003, 0007, 0008). */
|
|
40
|
+
export declare const ERROR_CODES: readonly ["protocol_mismatch", "timeout", "no_surface", "forbidden", "not_found", "unsupported", "cross_origin", "routing_disabled", "navigate_failed", "unknown_topic", "too_large", "invalid", "not_encodable", "quota_exceeded", "bus_unavailable", "not_requestable", "already_replied", "reply_invalid", "contracts_failed", "contracts_invalid", "contracts_mismatch", "reserved_topic", "seed_async", "seed_pending"];
|
|
41
|
+
export type ErrorCode = (typeof ERROR_CODES)[number];
|
|
42
|
+
export declare function isErrorCode(x: unknown): x is ErrorCode;
|
|
43
|
+
/** A schema issue, as reported with `invalid` (design §6.3). */
|
|
44
|
+
export type Issue = {
|
|
45
|
+
message: string;
|
|
46
|
+
path: (string | number)[];
|
|
47
|
+
};
|
|
48
|
+
export type CallError = {
|
|
49
|
+
code: ErrorCode;
|
|
50
|
+
message: string;
|
|
51
|
+
issues?: Issue[];
|
|
52
|
+
};
|
|
53
|
+
/** Reply to a host call or an acknowledged publish. */
|
|
54
|
+
export type CallResult = {
|
|
55
|
+
ok: true;
|
|
56
|
+
value: unknown;
|
|
57
|
+
} | {
|
|
58
|
+
ok: false;
|
|
59
|
+
error: CallError;
|
|
60
|
+
};
|
|
61
|
+
/** Payload of an `error` envelope from an isolate or the bus worker. */
|
|
62
|
+
export type ErrorPayload = {
|
|
63
|
+
message: string;
|
|
64
|
+
stack?: string;
|
|
65
|
+
fatal: boolean;
|
|
66
|
+
code?: ErrorCode;
|
|
67
|
+
};
|
|
68
|
+
/** What an isolate may do on the bus and the host (design §3). */
|
|
69
|
+
export type Grants = {
|
|
70
|
+
/** Topic patterns the isolate may publish to. */
|
|
71
|
+
publish: string[];
|
|
72
|
+
/** Topic patterns the isolate may subscribe to. */
|
|
73
|
+
subscribe: string[];
|
|
74
|
+
/** Programmatic `navigate()` allowed (ADR 0002). Default false. */
|
|
75
|
+
navigate?: boolean;
|
|
76
|
+
};
|
|
77
|
+
/** Topics under this prefix belong to the kernel (ADR 0002). */
|
|
78
|
+
export declare const SYSTEM_TOPIC_PREFIX = "isoloop.";
|
|
79
|
+
export declare function isSystemTopic(topic: string): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* True if `topic` matches the grant pattern. `*` matches one or more
|
|
82
|
+
* characters, dots included, so `catalog.*` covers `catalog.items.v1` and
|
|
83
|
+
* `cart.*.v1` covers `cart.items.v1`. Without `*` the match is exact.
|
|
84
|
+
*/
|
|
85
|
+
export declare function matchTopic(grant: string, topic: string): boolean;
|
|
86
|
+
/** True if any pattern in the list matches `topic`. */
|
|
87
|
+
export declare function matchAny(patterns: readonly string[], topic: string): boolean;
|
|
88
|
+
/** Modifier keys held during an event. */
|
|
89
|
+
export type Mods = {
|
|
90
|
+
alt: boolean;
|
|
91
|
+
ctrl: boolean;
|
|
92
|
+
meta: boolean;
|
|
93
|
+
shift: boolean;
|
|
94
|
+
};
|
|
95
|
+
/** Event types the host forwards (design §4.6), plus `resize` for transferred canvases (§4.7). */
|
|
96
|
+
export type EventType = "click" | "dblclick" | "input" | "change" | "keydown" | "keyup" | "submit" | "focusin" | "focusout" | "pointerdown" | "pointerup" | "pointermove" | "resize";
|
|
97
|
+
/** A DOM event as it reaches an isolate (design §4.6). Node ids are the isolate's own. */
|
|
98
|
+
export type SerializedEvent = {
|
|
99
|
+
type: EventType;
|
|
100
|
+
target: string;
|
|
101
|
+
/** The target and its ancestors that have a listener, innermost first. */
|
|
102
|
+
currentTargets: string[];
|
|
103
|
+
value?: string;
|
|
104
|
+
checked?: boolean;
|
|
105
|
+
key?: string;
|
|
106
|
+
code?: string;
|
|
107
|
+
mods: Mods;
|
|
108
|
+
x?: number;
|
|
109
|
+
y?: number;
|
|
110
|
+
button?: number;
|
|
111
|
+
/** For `resize`. */
|
|
112
|
+
width?: number;
|
|
113
|
+
height?: number;
|
|
114
|
+
};
|
|
115
|
+
/** Why the host asks an isolate to shut down. `suspend` keeps its offsets (M2, ADR 0001). */
|
|
116
|
+
export type ShutdownReason = "stop" | "restart" | "suspend";
|
|
117
|
+
/** Calls an isolate can make to the host (design §4.7). */
|
|
118
|
+
export type HostMethod = "measure" | "focus" | "navigate" | "scrollIntoView" | "requestCanvas";
|
|
119
|
+
/** What `measure` returns: a node's box relative to its surface, in CSS pixels. */
|
|
120
|
+
export type Rect = {
|
|
121
|
+
x: number;
|
|
122
|
+
y: number;
|
|
123
|
+
width: number;
|
|
124
|
+
height: number;
|
|
125
|
+
};
|
|
126
|
+
/** First message to an isolate. The host's half of the version handshake. */
|
|
127
|
+
export type InitPayload = {
|
|
128
|
+
isolateId: string;
|
|
129
|
+
/** Protocol versions the host accepts. */
|
|
130
|
+
protocol: {
|
|
131
|
+
min: number;
|
|
132
|
+
max: number;
|
|
133
|
+
};
|
|
134
|
+
/** `isoloop-dom@1`, or `none` (headless). */
|
|
135
|
+
renderFmt: string;
|
|
136
|
+
/** `manifest.config`, passed through. */
|
|
137
|
+
config: unknown;
|
|
138
|
+
grants: Grants;
|
|
139
|
+
/** Transferred. */
|
|
140
|
+
busPort: MessagePort;
|
|
141
|
+
heartbeatMs: number;
|
|
142
|
+
/** Isolate module URL, imported by the bootstrap. */
|
|
143
|
+
src: string;
|
|
144
|
+
/**
|
|
145
|
+
* Set when the isolate wakes from suspension (ADR 0001): its `latest`
|
|
146
|
+
* subscriptions start from `committed` instead, so it replays what it missed.
|
|
147
|
+
*/
|
|
148
|
+
wake?: boolean;
|
|
149
|
+
};
|
|
150
|
+
/** What a beat says about whether the isolate could be suspended (ADR 0001). */
|
|
151
|
+
export type IdleReport = {
|
|
152
|
+
/** Time since the bus last delivered a record to this isolate. */
|
|
153
|
+
sinceDeliveryMs: number;
|
|
154
|
+
/** Host calls, publishes and requests waiting for an answer. */
|
|
155
|
+
inFlight: number;
|
|
156
|
+
/** Open `ctx.keepAwake()` holds: age and where they were taken. */
|
|
157
|
+
holds: {
|
|
158
|
+
ageMs: number;
|
|
159
|
+
stack?: string;
|
|
160
|
+
}[];
|
|
161
|
+
};
|
|
162
|
+
/** Isolate port: host → isolate. */
|
|
163
|
+
export type HostToIsolate = Envelope<"init", InitPayload> | Envelope<"event", SerializedEvent> | Envelope<"result", CallResult> | Envelope<"shutdown", {
|
|
164
|
+
reason: ShutdownReason;
|
|
165
|
+
graceMs: number;
|
|
166
|
+
}> | Envelope<"reattach", {
|
|
167
|
+
busPort: MessagePort;
|
|
168
|
+
}>;
|
|
169
|
+
/** Isolate port: isolate → host. */
|
|
170
|
+
export type IsolateToHost = Envelope<"ready", {
|
|
171
|
+
runtime: string;
|
|
172
|
+
adapter: string;
|
|
173
|
+
renderFmt: string;
|
|
174
|
+
/** The Isoloop `major.minor` the isolate was built against, from `defineIsolate` (ADR 0012). */
|
|
175
|
+
requires?: string;
|
|
176
|
+
}> | Envelope<"patch", {
|
|
177
|
+
fmt: string;
|
|
178
|
+
batch: unknown;
|
|
179
|
+
styles?: string[];
|
|
180
|
+
head?: string[];
|
|
181
|
+
}> | Envelope<"call", {
|
|
182
|
+
method: HostMethod;
|
|
183
|
+
args: unknown[];
|
|
184
|
+
}> | Envelope<"beat", {
|
|
185
|
+
lagMs: number;
|
|
186
|
+
heapBytes?: number;
|
|
187
|
+
idle?: IdleReport;
|
|
188
|
+
}> | Envelope<"error", ErrorPayload> | Envelope<"bye", Record<string, never>>;
|
|
189
|
+
export declare const HOST_TO_ISOLATE: MessageTypes<HostToIsolate>;
|
|
190
|
+
export declare const ISOLATE_TO_HOST: MessageTypes<IsolateToHost>;
|
|
191
|
+
export declare const isHostToIsolate: (x: unknown) => x is HostToIsolate;
|
|
192
|
+
export declare const isIsolateToHost: (x: unknown) => x is IsolateToHost;
|
|
193
|
+
/** One record in a topic's log (design §6.1). */
|
|
194
|
+
export type BusRecord = {
|
|
195
|
+
offset: number;
|
|
196
|
+
key?: string;
|
|
197
|
+
value: unknown;
|
|
198
|
+
/** `Date.now()` at append. */
|
|
199
|
+
ts: number;
|
|
200
|
+
/** Isolate id of the publisher. */
|
|
201
|
+
producer: string;
|
|
202
|
+
/**
|
|
203
|
+
* Set on a `bus.request` record: the caller's isolate id and its request id.
|
|
204
|
+
* A recipient answers with `reply { topic, offset }` (design §6.3).
|
|
205
|
+
*/
|
|
206
|
+
req?: {
|
|
207
|
+
from: string;
|
|
208
|
+
id: string;
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Identifies a publish so a re-send after a bus crash isn't appended twice to
|
|
213
|
+
* a persistent topic (ADR 0007, step 5): `epoch` names the publisher's
|
|
214
|
+
* instance (new after an isolate restart or a page load), `seq` counts its
|
|
215
|
+
* publishes and requests. The bus answers a re-send it already holds with the
|
|
216
|
+
* original offset. Memory topics ignore it.
|
|
217
|
+
*/
|
|
218
|
+
export type PublishId = {
|
|
219
|
+
epoch?: number;
|
|
220
|
+
seq?: number;
|
|
221
|
+
};
|
|
222
|
+
/** Where a new subscription starts reading. */
|
|
223
|
+
export type StartPosition = "latest" | "earliest" | "committed";
|
|
224
|
+
/** Bus port: isolate → bus worker. */
|
|
225
|
+
export type IsolateToBus = Envelope<"publish", {
|
|
226
|
+
topic: string;
|
|
227
|
+
key?: string;
|
|
228
|
+
value: unknown;
|
|
229
|
+
} & PublishId> | Envelope<"subscribe", {
|
|
230
|
+
topic: string;
|
|
231
|
+
from: StartPosition;
|
|
232
|
+
group?: string;
|
|
233
|
+
}> | Envelope<"unsubscribe", {
|
|
234
|
+
topic: string;
|
|
235
|
+
}> | Envelope<"commit", {
|
|
236
|
+
topic: string;
|
|
237
|
+
offset: number;
|
|
238
|
+
}> | Envelope<"request", {
|
|
239
|
+
topic: string;
|
|
240
|
+
key?: string;
|
|
241
|
+
value: unknown;
|
|
242
|
+
timeoutMs: number;
|
|
243
|
+
} & PublishId> | Envelope<"reply", {
|
|
244
|
+
topic: string;
|
|
245
|
+
offset: number;
|
|
246
|
+
value: unknown;
|
|
247
|
+
}>;
|
|
248
|
+
/** Bus port: bus worker → isolate. */
|
|
249
|
+
export type BusToIsolate = Envelope<"deliver", {
|
|
250
|
+
topic: string;
|
|
251
|
+
records: BusRecord[];
|
|
252
|
+
gap?: {
|
|
253
|
+
from: number;
|
|
254
|
+
to: number;
|
|
255
|
+
};
|
|
256
|
+
}> | Envelope<"reset", {
|
|
257
|
+
topic: string;
|
|
258
|
+
reason: "bus_restart";
|
|
259
|
+
}> | Envelope<"result", CallResult>;
|
|
260
|
+
export declare const ISOLATE_TO_BUS: MessageTypes<IsolateToBus>;
|
|
261
|
+
export declare const BUS_TO_ISOLATE: MessageTypes<BusToIsolate>;
|
|
262
|
+
export declare const isIsolateToBus: (x: unknown) => x is IsolateToBus;
|
|
263
|
+
export declare const isBusToIsolate: (x: unknown) => x is BusToIsolate;
|
|
264
|
+
/**
|
|
265
|
+
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
266
|
+
* `reply`): the broker's error code, and the schema issues for `invalid`.
|
|
267
|
+
*/
|
|
268
|
+
export declare class BusError extends Error {
|
|
269
|
+
readonly code: ErrorCode;
|
|
270
|
+
readonly issues: readonly Issue[] | undefined;
|
|
271
|
+
constructor(error: CallError);
|
|
272
|
+
}
|
|
273
|
+
export type Retention = {
|
|
274
|
+
kind: "last-n";
|
|
275
|
+
n: number;
|
|
276
|
+
} | {
|
|
277
|
+
kind: "time";
|
|
278
|
+
ms: number;
|
|
279
|
+
} | {
|
|
280
|
+
kind: "compacted";
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* The structured-cloneable part of a topic definition: what the host sends at
|
|
284
|
+
* `boot` so the bus worker can check it against the contracts module (ADR 0003).
|
|
285
|
+
*/
|
|
286
|
+
export type TopicConfig = {
|
|
287
|
+
name: string;
|
|
288
|
+
retention: Retention;
|
|
289
|
+
/** Default 256 KiB. */
|
|
290
|
+
maxRecordBytes?: number;
|
|
291
|
+
/** Default `memory`. `persistent` is OPFS (ADR 0007, M2b). */
|
|
292
|
+
durability?: "memory" | "persistent";
|
|
293
|
+
/** The topic has a `reply` schema, so `bus.request` works on it. */
|
|
294
|
+
requestable: boolean;
|
|
295
|
+
/** Name of the `seed.from` topic and its `follow` flag (M2b). */
|
|
296
|
+
seed?: {
|
|
297
|
+
from: string;
|
|
298
|
+
follow: boolean;
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
/** Why persistent topics run in memory (`bus:durability-degraded`). */
|
|
302
|
+
export type DurabilityDegraded = "another-tab" | "no-sync-access" | "open-failed";
|
|
303
|
+
/**
|
|
304
|
+
* Seeding news (ADR 0003, decision 18). `rejected`: a source record `map`
|
|
305
|
+
* threw on, returned a promise for (`seed_async`), or the topic's schema
|
|
306
|
+
* rejected; it was skipped and stays in `from`. `seeded`: the seed ended and
|
|
307
|
+
* the topic opened.
|
|
308
|
+
*/
|
|
309
|
+
export type SeedEvent = {
|
|
310
|
+
kind: "rejected";
|
|
311
|
+
topic: string;
|
|
312
|
+
from: string;
|
|
313
|
+
offset: number;
|
|
314
|
+
code: ErrorCode;
|
|
315
|
+
issues: Issue[];
|
|
316
|
+
} | {
|
|
317
|
+
kind: "seeded";
|
|
318
|
+
topic: string;
|
|
319
|
+
copied: number;
|
|
320
|
+
rejected: number;
|
|
321
|
+
};
|
|
322
|
+
/** Sampled every second on the control port (design §6.3). */
|
|
323
|
+
export type BusMetrics = {
|
|
324
|
+
topics: {
|
|
325
|
+
topic: string;
|
|
326
|
+
recordsPerSec: number;
|
|
327
|
+
bytesPerSec: number;
|
|
328
|
+
}[];
|
|
329
|
+
/** `lag` is `head − cursor`. */
|
|
330
|
+
subscriptions: {
|
|
331
|
+
consumer: string;
|
|
332
|
+
topic: string;
|
|
333
|
+
lag: number;
|
|
334
|
+
credit: number;
|
|
335
|
+
}[];
|
|
336
|
+
rejected: Partial<Record<ErrorCode, number>>;
|
|
337
|
+
/** The origin's storage use, from `navigator.storage.estimate()`, while the bus keeps files. */
|
|
338
|
+
storage?: {
|
|
339
|
+
usage: number;
|
|
340
|
+
quota: number;
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
/** Control port: host → bus worker. */
|
|
344
|
+
export type HostToBus = Envelope<"boot", {
|
|
345
|
+
contractsUrl: string;
|
|
346
|
+
topics: TopicConfig[];
|
|
347
|
+
heartbeatMs?: number;
|
|
348
|
+
dev?: boolean;
|
|
349
|
+
/**
|
|
350
|
+
* Safe mode (ADR 0007, decision 5): the bus crashed twice while
|
|
351
|
+
* loading, so it sets each persistent topic's newest segment aside.
|
|
352
|
+
*/
|
|
353
|
+
safeMode?: boolean;
|
|
354
|
+
/** A bus restart: the worker it replaces may still hold the files' lock a moment. */
|
|
355
|
+
restart?: boolean;
|
|
356
|
+
}> | Envelope<"attach", {
|
|
357
|
+
isolateId: string;
|
|
358
|
+
grants: Grants;
|
|
359
|
+
port: MessagePort;
|
|
360
|
+
reattach?: boolean;
|
|
361
|
+
}> | Envelope<"detach", {
|
|
362
|
+
isolateId: string;
|
|
363
|
+
keepOffsets: boolean;
|
|
364
|
+
wake?: boolean;
|
|
365
|
+
}> | Envelope<"publish", {
|
|
366
|
+
topic: string;
|
|
367
|
+
key?: string;
|
|
368
|
+
value: unknown;
|
|
369
|
+
} & PublishId> | Envelope<"flush", Record<string, never>> | Envelope<"drop", {
|
|
370
|
+
topic: string;
|
|
371
|
+
force: boolean;
|
|
372
|
+
}>;
|
|
373
|
+
/** Control port: bus worker → host. */
|
|
374
|
+
export type BusToHost = Envelope<"ready", {
|
|
375
|
+
engine: string;
|
|
376
|
+
storage: "memory" | "opfs";
|
|
377
|
+
restored: {
|
|
378
|
+
topic: string;
|
|
379
|
+
head: number;
|
|
380
|
+
}[];
|
|
381
|
+
/** Safe mode only: the segments set aside, as `<topic>/<file>`. */
|
|
382
|
+
quarantined?: string[];
|
|
383
|
+
/**
|
|
384
|
+
* Persistent topics run in memory, and why (ADR 0007, decision 6):
|
|
385
|
+
* another tab holds the files, sync access handles are missing, or
|
|
386
|
+
* the files failed to open.
|
|
387
|
+
*/
|
|
388
|
+
degraded?: DurabilityDegraded;
|
|
389
|
+
/**
|
|
390
|
+
* Persistent topics on disk that the contracts no longer list: kept,
|
|
391
|
+
* not opened (ADR 0003, decision 19). `bytes` is their total size.
|
|
392
|
+
*/
|
|
393
|
+
orphaned?: {
|
|
394
|
+
names: string[];
|
|
395
|
+
bytes: number;
|
|
396
|
+
};
|
|
397
|
+
}> | Envelope<"beat", {
|
|
398
|
+
lagMs: number;
|
|
399
|
+
}> | Envelope<"wake", {
|
|
400
|
+
isolateId: string;
|
|
401
|
+
topic: string;
|
|
402
|
+
}> | Envelope<"metrics", BusMetrics> | Envelope<"result", CallResult> | Envelope<"seed", SeedEvent> | Envelope<"error", ErrorPayload>;
|
|
403
|
+
export declare const HOST_TO_BUS: MessageTypes<HostToBus>;
|
|
404
|
+
export declare const BUS_TO_HOST: MessageTypes<BusToHost>;
|
|
405
|
+
export declare const isHostToBus: (x: unknown) => x is HostToBus;
|
|
406
|
+
export declare const isBusToHost: (x: unknown) => x is BusToHost;
|
|
407
|
+
/**
|
|
408
|
+
* A Standard Schema V1 built by hand (ADR 0003, decision 6). Protocol depends
|
|
409
|
+
* on nothing, so this is the structural subset of the interface that system
|
|
410
|
+
* topics need; it is assignable to the bus's vendored `StandardSchemaV1`.
|
|
411
|
+
*/
|
|
412
|
+
export type SystemSchema<T> = {
|
|
413
|
+
readonly "~standard": {
|
|
414
|
+
readonly version: 1;
|
|
415
|
+
readonly vendor: "isoloop";
|
|
416
|
+
readonly validate: (value: unknown) => {
|
|
417
|
+
readonly value: T;
|
|
418
|
+
readonly issues?: undefined;
|
|
419
|
+
} | {
|
|
420
|
+
readonly issues: readonly Issue[];
|
|
421
|
+
};
|
|
422
|
+
readonly types?: {
|
|
423
|
+
readonly input: T;
|
|
424
|
+
readonly output: T;
|
|
425
|
+
} | undefined;
|
|
426
|
+
};
|
|
427
|
+
};
|
|
428
|
+
/** A kernel-owned `isoloop.*` topic: same shape as a bus `TopicDef`. */
|
|
429
|
+
export type SystemTopicDef<T> = {
|
|
430
|
+
readonly name: string;
|
|
431
|
+
readonly schema: SystemSchema<T>;
|
|
432
|
+
readonly retention: Retention;
|
|
433
|
+
readonly maxRecordBytes?: number;
|
|
434
|
+
};
|
|
435
|
+
/**
|
|
436
|
+
* A check that returns the issues found in `value`, or none. `path` is where
|
|
437
|
+
* `value` sits, so nested checks can report full paths.
|
|
438
|
+
*/
|
|
439
|
+
export type Check = (value: unknown, path: (string | number)[]) => Issue[];
|
|
440
|
+
/** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
|
|
441
|
+
export declare function systemSchema<T>(check: Check): SystemSchema<T>;
|
|
442
|
+
/** Defines a system topic. The name must start with `isoloop.`. */
|
|
443
|
+
export declare function systemTopic<T>(name: string, def: Omit<SystemTopicDef<T>, "name">): SystemTopicDef<T>;
|
|
444
|
+
/** Small building blocks for hand-written checks. */
|
|
445
|
+
export declare const checks: {
|
|
446
|
+
string: Check;
|
|
447
|
+
optionalString: Check;
|
|
448
|
+
/** A plain object whose listed fields pass their checks. Other fields are allowed. */
|
|
449
|
+
object: (fields: Readonly<Record<string, Check>>) => Check;
|
|
450
|
+
/** Any value. */
|
|
451
|
+
any: Check;
|
|
452
|
+
};
|
|
453
|
+
/** The current location, as the kernel publishes it (ADR 0002). */
|
|
454
|
+
export type RouteValue = {
|
|
455
|
+
/** Absolute URL. */
|
|
456
|
+
href: string;
|
|
457
|
+
pathname: string;
|
|
458
|
+
/** `pathname` relative to `basePath`, starting with `/`; `null` outside it. */
|
|
459
|
+
path: string | null;
|
|
460
|
+
search: string;
|
|
461
|
+
hash: string;
|
|
462
|
+
/** `history.state` as is; `null` when it was too large for the bus (`route:state_dropped`). */
|
|
463
|
+
state: unknown;
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* `isoloop.route.v1`: compacted, key `"current"`, memory only (the URL is the
|
|
467
|
+
* source of truth). Only the kernel publishes it; every isolate may
|
|
468
|
+
* subscribe without a grant, with `from: 'earliest'` to get the current
|
|
469
|
+
* route at once.
|
|
470
|
+
*/
|
|
471
|
+
export declare const routeTopic: SystemTopicDef<RouteValue>;
|
|
472
|
+
/** The key of the one record that matters on `isoloop.route.v1`. */
|
|
473
|
+
export declare const ROUTE_KEY = "current";
|
|
474
|
+
/** Every system topic, built into the bus. */
|
|
475
|
+
export declare const SYSTEM_TOPICS: readonly SystemTopicDef<any>[];
|
|
476
|
+
/** A topic as `kernel.publish`, `kernel.subscribe` and `BusClient` take it. */
|
|
477
|
+
export type TopicRef<In = unknown, Out = In, RIn = never, ROut = RIn> = {
|
|
478
|
+
readonly name: string;
|
|
479
|
+
readonly schema: SchemaTypes<In, Out>;
|
|
480
|
+
readonly reply?: SchemaTypes<RIn, ROut>;
|
|
481
|
+
};
|
|
482
|
+
/** The type-only part of a Standard Schema V1. */
|
|
483
|
+
export type SchemaTypes<In, Out> = {
|
|
484
|
+
readonly "~standard": {
|
|
485
|
+
readonly types?: {
|
|
486
|
+
readonly input: In;
|
|
487
|
+
readonly output: Out;
|
|
488
|
+
} | undefined;
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
/** A record whose value has the topic's output type. */
|
|
492
|
+
export type TypedRecord<T> = Omit<BusRecord, "value"> & {
|
|
493
|
+
value: T;
|
|
494
|
+
};
|
|
495
|
+
/** The parts of a topic definition that make up its `TopicConfig`. */
|
|
496
|
+
export type TopicShape = {
|
|
497
|
+
readonly name: string;
|
|
498
|
+
readonly retention: Retention;
|
|
499
|
+
readonly maxRecordBytes?: number;
|
|
500
|
+
readonly durability?: "memory" | "persistent";
|
|
501
|
+
readonly reply?: unknown;
|
|
502
|
+
readonly seed?: {
|
|
503
|
+
readonly from: {
|
|
504
|
+
readonly name: string;
|
|
505
|
+
};
|
|
506
|
+
readonly follow?: boolean;
|
|
507
|
+
};
|
|
508
|
+
};
|
|
509
|
+
/** What `KernelConfig.contracts.list` holds: a `contracts()` list, seen structurally. */
|
|
510
|
+
export type ContractsList = {
|
|
511
|
+
readonly topics: readonly TopicShape[];
|
|
512
|
+
};
|
|
513
|
+
/**
|
|
514
|
+
* The structured-cloneable part of a topic, sent at `boot` and compared with
|
|
515
|
+
* the contracts module's (ADR 0003, decision 5). Host and bus both use this
|
|
516
|
+
* one function, so equal definitions always give equal configs.
|
|
517
|
+
*/
|
|
518
|
+
export declare function topicConfigOf(t: TopicShape): TopicConfig;
|
|
519
|
+
/** `"0.4.2"` → `"0.4"`. Anything that isn't `major.minor[.patch…]` gives `undefined`. */
|
|
520
|
+
export declare function majorMinor(version: string): string | undefined;
|
|
521
|
+
export declare function compareVersions(a: string, b: string): number;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// src/envelope.ts
|
|
2
|
+
/** Protocol version this build speaks (`v`) and the oldest it still accepts (`min`). */
|
|
3
|
+
const PROTOCOL = {
|
|
4
|
+
v: 1,
|
|
5
|
+
min: 1
|
|
6
|
+
};
|
|
7
|
+
const isRecord = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
8
|
+
const isOptionalString = (x) => x === void 0 || typeof x === "string";
|
|
9
|
+
/**
|
|
10
|
+
* True for a well-formed envelope this build can read: `v` is a supported
|
|
11
|
+
* version, `t` is a string, `id`/`re` are absent or strings, and `p` is present.
|
|
12
|
+
* Says nothing about whether `t` is known; see the per-port guards.
|
|
13
|
+
*/
|
|
14
|
+
function isEnvelope(x) {
|
|
15
|
+
return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Builds a guard for one port's union from its message types. Each side drops
|
|
19
|
+
* (and counts) envelopes with an unknown `v` or `t` (design §3), so a guard
|
|
20
|
+
* only checks the envelope and the type, not the payload.
|
|
21
|
+
*/
|
|
22
|
+
function envelopeGuard(types) {
|
|
23
|
+
return (x) => isEnvelope(x) && Object.hasOwn(types, x.t);
|
|
24
|
+
}
|
|
25
|
+
/** Exhaustiveness check for switches over message unions. */
|
|
26
|
+
function assertNever(x, message = "Unexpected value") {
|
|
27
|
+
throw new Error(`${message}: ${JSON.stringify(x)}`);
|
|
28
|
+
}
|
|
29
|
+
// src/errors.ts
|
|
30
|
+
/** Every error code on the wire (design §3, §4.7, §6.1, §6.3; ADRs 0002, 0003, 0007, 0008). */
|
|
31
|
+
const ERROR_CODES = [
|
|
32
|
+
"protocol_mismatch",
|
|
33
|
+
"timeout",
|
|
34
|
+
"no_surface",
|
|
35
|
+
"forbidden",
|
|
36
|
+
"not_found",
|
|
37
|
+
"unsupported",
|
|
38
|
+
"cross_origin",
|
|
39
|
+
"routing_disabled",
|
|
40
|
+
"navigate_failed",
|
|
41
|
+
"unknown_topic",
|
|
42
|
+
"too_large",
|
|
43
|
+
"invalid",
|
|
44
|
+
"not_encodable",
|
|
45
|
+
"quota_exceeded",
|
|
46
|
+
"bus_unavailable",
|
|
47
|
+
"not_requestable",
|
|
48
|
+
"already_replied",
|
|
49
|
+
"reply_invalid",
|
|
50
|
+
"contracts_failed",
|
|
51
|
+
"contracts_invalid",
|
|
52
|
+
"contracts_mismatch",
|
|
53
|
+
"reserved_topic",
|
|
54
|
+
"seed_async",
|
|
55
|
+
"seed_pending"
|
|
56
|
+
];
|
|
57
|
+
const codes = new Set(ERROR_CODES);
|
|
58
|
+
function isErrorCode(x) {
|
|
59
|
+
return typeof x === "string" && codes.has(x);
|
|
60
|
+
}
|
|
61
|
+
// src/grants.ts
|
|
62
|
+
/** Topics under this prefix belong to the kernel (ADR 0002). */
|
|
63
|
+
const SYSTEM_TOPIC_PREFIX = "isoloop.";
|
|
64
|
+
function isSystemTopic(topic) {
|
|
65
|
+
return topic.startsWith(SYSTEM_TOPIC_PREFIX);
|
|
66
|
+
}
|
|
67
|
+
const escape = (s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
68
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
69
|
+
/**
|
|
70
|
+
* True if `topic` matches the grant pattern. `*` matches one or more
|
|
71
|
+
* characters, dots included, so `catalog.*` covers `catalog.items.v1` and
|
|
72
|
+
* `cart.*.v1` covers `cart.items.v1`. Without `*` the match is exact.
|
|
73
|
+
*/
|
|
74
|
+
function matchTopic(grant, topic) {
|
|
75
|
+
if (!grant.includes("*")) return grant === topic;
|
|
76
|
+
let re = compiled.get(grant);
|
|
77
|
+
if (!re) {
|
|
78
|
+
re = new RegExp(`^${grant.split("*").map(escape).join(".+")}$`);
|
|
79
|
+
compiled.set(grant, re);
|
|
80
|
+
}
|
|
81
|
+
return re.test(topic);
|
|
82
|
+
}
|
|
83
|
+
/** True if any pattern in the list matches `topic`. */
|
|
84
|
+
function matchAny(patterns, topic) {
|
|
85
|
+
return patterns.some((p) => matchTopic(p, topic));
|
|
86
|
+
}
|
|
87
|
+
// src/isolate.ts
|
|
88
|
+
const HOST_TO_ISOLATE = {
|
|
89
|
+
init: true,
|
|
90
|
+
event: true,
|
|
91
|
+
result: true,
|
|
92
|
+
shutdown: true,
|
|
93
|
+
reattach: true
|
|
94
|
+
};
|
|
95
|
+
const ISOLATE_TO_HOST = {
|
|
96
|
+
ready: true,
|
|
97
|
+
patch: true,
|
|
98
|
+
call: true,
|
|
99
|
+
beat: true,
|
|
100
|
+
error: true,
|
|
101
|
+
bye: true
|
|
102
|
+
};
|
|
103
|
+
const isHostToIsolate = envelopeGuard(HOST_TO_ISOLATE);
|
|
104
|
+
const isIsolateToHost = envelopeGuard(ISOLATE_TO_HOST);
|
|
105
|
+
// src/bus.ts
|
|
106
|
+
const ISOLATE_TO_BUS = {
|
|
107
|
+
publish: true,
|
|
108
|
+
subscribe: true,
|
|
109
|
+
unsubscribe: true,
|
|
110
|
+
commit: true,
|
|
111
|
+
request: true,
|
|
112
|
+
reply: true
|
|
113
|
+
};
|
|
114
|
+
const BUS_TO_ISOLATE = {
|
|
115
|
+
deliver: true,
|
|
116
|
+
reset: true,
|
|
117
|
+
result: true
|
|
118
|
+
};
|
|
119
|
+
const isIsolateToBus = envelopeGuard(ISOLATE_TO_BUS);
|
|
120
|
+
const isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
|
|
121
|
+
// src/bus-error.ts
|
|
122
|
+
/**
|
|
123
|
+
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
124
|
+
* `reply`): the broker's error code, and the schema issues for `invalid`.
|
|
125
|
+
*/
|
|
126
|
+
var BusError = class extends Error {
|
|
127
|
+
code;
|
|
128
|
+
issues;
|
|
129
|
+
constructor(error) {
|
|
130
|
+
super(error.message);
|
|
131
|
+
this.name = "BusError";
|
|
132
|
+
this.code = error.code;
|
|
133
|
+
this.issues = error.issues;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
// src/control.ts
|
|
137
|
+
const HOST_TO_BUS = {
|
|
138
|
+
boot: true,
|
|
139
|
+
attach: true,
|
|
140
|
+
detach: true,
|
|
141
|
+
publish: true,
|
|
142
|
+
flush: true,
|
|
143
|
+
drop: true
|
|
144
|
+
};
|
|
145
|
+
const BUS_TO_HOST = {
|
|
146
|
+
ready: true,
|
|
147
|
+
beat: true,
|
|
148
|
+
wake: true,
|
|
149
|
+
metrics: true,
|
|
150
|
+
result: true,
|
|
151
|
+
seed: true,
|
|
152
|
+
error: true
|
|
153
|
+
};
|
|
154
|
+
const isHostToBus = envelopeGuard(HOST_TO_BUS);
|
|
155
|
+
const isBusToHost = envelopeGuard(BUS_TO_HOST);
|
|
156
|
+
// src/system-topics.ts
|
|
157
|
+
/** Wraps a hand-written check as a Standard Schema that passes valid values through unchanged. */
|
|
158
|
+
function systemSchema(check) {
|
|
159
|
+
return { "~standard": {
|
|
160
|
+
version: 1,
|
|
161
|
+
vendor: "isoloop",
|
|
162
|
+
validate: (value) => {
|
|
163
|
+
const issues = check(value, []);
|
|
164
|
+
return issues.length === 0 ? { value } : { issues };
|
|
165
|
+
}
|
|
166
|
+
} };
|
|
167
|
+
}
|
|
168
|
+
/** Defines a system topic. The name must start with `isoloop.`. */
|
|
169
|
+
function systemTopic(name, def) {
|
|
170
|
+
if (!isSystemTopic(name)) throw new Error(`system topic names start with isoloop.: ${name}`);
|
|
171
|
+
return Object.freeze({
|
|
172
|
+
...def,
|
|
173
|
+
name
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
177
|
+
const issue = (path, message) => [{
|
|
178
|
+
message,
|
|
179
|
+
path
|
|
180
|
+
}];
|
|
181
|
+
/** Small building blocks for hand-written checks. */
|
|
182
|
+
const checks = {
|
|
183
|
+
string: (v, path) => typeof v === "string" ? [] : issue(path, "expected a string"),
|
|
184
|
+
optionalString: (v, path) => v === void 0 || typeof v === "string" ? [] : issue(path, "expected a string or nothing"),
|
|
185
|
+
object: (fields) => (v, path) => {
|
|
186
|
+
if (!isObject(v)) return issue(path, "expected an object");
|
|
187
|
+
return Object.entries(fields).flatMap(([k, check]) => check(v[k], [...path, k]));
|
|
188
|
+
},
|
|
189
|
+
any: () => []
|
|
190
|
+
};
|
|
191
|
+
// src/route.ts
|
|
192
|
+
const stringOrNull = (v, path) => v === null || typeof v === "string" ? [] : [{
|
|
193
|
+
message: "expected a string or null",
|
|
194
|
+
path
|
|
195
|
+
}];
|
|
196
|
+
/**
|
|
197
|
+
* `isoloop.route.v1`: compacted, key `"current"`, memory only (the URL is the
|
|
198
|
+
* source of truth). Only the kernel publishes it; every isolate may
|
|
199
|
+
* subscribe without a grant, with `from: 'earliest'` to get the current
|
|
200
|
+
* route at once.
|
|
201
|
+
*/
|
|
202
|
+
const routeTopic = systemTopic("isoloop.route.v1", {
|
|
203
|
+
schema: systemSchema(checks.object({
|
|
204
|
+
href: checks.string,
|
|
205
|
+
pathname: checks.string,
|
|
206
|
+
path: stringOrNull,
|
|
207
|
+
search: checks.string,
|
|
208
|
+
hash: checks.string,
|
|
209
|
+
state: checks.any
|
|
210
|
+
})),
|
|
211
|
+
retention: { kind: "compacted" }
|
|
212
|
+
});
|
|
213
|
+
/** The key of the one record that matters on `isoloop.route.v1`. */
|
|
214
|
+
const ROUTE_KEY = "current";
|
|
215
|
+
/** Every system topic, built into the bus. */
|
|
216
|
+
const SYSTEM_TOPICS = [routeTopic];
|
|
217
|
+
// src/topics.ts
|
|
218
|
+
/**
|
|
219
|
+
* The structured-cloneable part of a topic, sent at `boot` and compared with
|
|
220
|
+
* the contracts module's (ADR 0003, decision 5). Host and bus both use this
|
|
221
|
+
* one function, so equal definitions always give equal configs.
|
|
222
|
+
*/
|
|
223
|
+
function topicConfigOf(t) {
|
|
224
|
+
const cfg = {
|
|
225
|
+
name: t.name,
|
|
226
|
+
retention: { ...t.retention },
|
|
227
|
+
requestable: t.reply !== void 0
|
|
228
|
+
};
|
|
229
|
+
if (t.maxRecordBytes !== void 0) cfg.maxRecordBytes = t.maxRecordBytes;
|
|
230
|
+
if (t.durability !== void 0) cfg.durability = t.durability;
|
|
231
|
+
if (t.seed !== void 0) cfg.seed = {
|
|
232
|
+
from: t.seed.from.name,
|
|
233
|
+
follow: t.seed.follow ?? false
|
|
234
|
+
};
|
|
235
|
+
return cfg;
|
|
236
|
+
}
|
|
237
|
+
// src/version.ts
|
|
238
|
+
/** `"0.4.2"` → `"0.4"`. Anything that isn't `major.minor[.patch…]` gives `undefined`. */
|
|
239
|
+
function majorMinor(version) {
|
|
240
|
+
const m = /^(\d+)\.(\d+)(?:[.+-].*)?$/.exec(version);
|
|
241
|
+
return m ? `${Number(m[1])}.${Number(m[2])}` : void 0;
|
|
242
|
+
}
|
|
243
|
+
/** Orders two `major.minor` stamps: negative, zero or positive. Unparseable ones throw. */
|
|
244
|
+
const parse = (v) => {
|
|
245
|
+
const mm = majorMinor(v);
|
|
246
|
+
if (mm === void 0) throw new TypeError(`not a version: ${JSON.stringify(v)}`);
|
|
247
|
+
const [major = 0, minor = 0] = mm.split(".").map(Number);
|
|
248
|
+
return [major, minor];
|
|
249
|
+
};
|
|
250
|
+
function compareVersions(a, b) {
|
|
251
|
+
const [am, an] = parse(a);
|
|
252
|
+
const [bm, bn] = parse(b);
|
|
253
|
+
return am - bm || an - bn;
|
|
254
|
+
}
|
|
255
|
+
export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, compareVersions, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isoloop/protocol",
|
|
3
|
+
"version": "0.0.0-next.69da74b",
|
|
4
|
+
"description": "Wire protocol types and guards shared by every Isoloop package.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/isoloop/isoloop.git",
|
|
11
|
+
"directory": "packages/protocol"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "7.0.2"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsdown --platform neutral --dts",
|
|
27
|
+
"typecheck": "tsc --noEmit"
|
|
28
|
+
}
|
|
29
|
+
}
|