@mmstack/mesh 22.15.2 → 22.16.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 +264 -58
- package/fesm2022/mmstack-mesh.mjs +2695 -793
- package/fesm2022/mmstack-mesh.mjs.map +1 -1
- package/package.json +8 -4
- package/types/mmstack-mesh.d.ts +303 -362
package/types/mmstack-mesh.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Injector, Signal, WritableSignal } from "@angular/core";
|
|
2
|
-
import { ClientMsg, ClientSignalMsg,
|
|
3
|
-
import {
|
|
2
|
+
import { ClientMsg, ClientSignalMsg, GenerationReason, Hlc, IceServer, OpPolicy, PresenceState, PrincipalCtx, Relay, ServerMsg, TailPolicy } from "@mmstack/mesh-protocol";
|
|
3
|
+
import { DiffOpsOptions, ForkStoreOptions, JournalStore, MergePolicyEntry, OpEnvelope, OpSync, StoreOp, SyncedFork, WritableSignalStore, toStoreOptions } from "@mmstack/primitives/core";
|
|
4
|
+
/** The authored intent of an own envelope: its raw ops and one bump flag, what a re-author rebuilds from. */
|
|
5
|
+
type Intent = {
|
|
6
|
+
readonly raws: readonly StoreOp[];
|
|
7
|
+
readonly bump: boolean;
|
|
8
|
+
};
|
|
4
9
|
/**
|
|
5
10
|
* The client half of a room generation: the record fields a snapshot welcome carries, the admission
|
|
6
11
|
* evidence the client keeps for its pre-check, and the pure decisions the session takes on them
|
|
@@ -19,112 +24,6 @@ type InstalledGeneration = {
|
|
|
19
24
|
/** The seq of the snapshot welcome that installed it (or a later one of the same generation). */
|
|
20
25
|
readonly seq: number;
|
|
21
26
|
};
|
|
22
|
-
/**
|
|
23
|
-
* The admission evidence of the installed generation as this client knows it: the welcome's ranges,
|
|
24
|
-
* settled vector and first stamps, then every envelope the client ingests (its own included).
|
|
25
|
-
* A lead or lag against the relay's view only ever yields a refusal the client lands as a conflict.
|
|
26
|
-
*/
|
|
27
|
-
type ClientAdmission = {
|
|
28
|
-
readonly ranges: Map<string, Ranges>;
|
|
29
|
-
readonly settled: Map<string, Hlc>;
|
|
30
|
-
readonly firstStamps: Map<string, Hlc>;
|
|
31
|
-
};
|
|
32
|
-
/** The serializable form of {@link ClientAdmission}. */
|
|
33
|
-
type ClientAdmissionRecord = {
|
|
34
|
-
readonly ranges: Readonly<Record<string, readonly VersionRange[]>>;
|
|
35
|
-
readonly settled: Readonly<Record<string, Hlc>>;
|
|
36
|
-
readonly firstStamps: Readonly<Record<string, Hlc>>;
|
|
37
|
-
};
|
|
38
|
-
declare const emptyAdmission: () => ClientAdmission;
|
|
39
|
-
/** The admission evidence a snapshot welcome carries, as of its seq. */
|
|
40
|
-
declare const admissionOf: (src: ClientAdmissionRecord) => ClientAdmission;
|
|
41
|
-
declare const admissionRecord: (a: ClientAdmission) => ClientAdmissionRecord;
|
|
42
|
-
/**
|
|
43
|
-
* One envelope reaches this client's view (its own emission, a relay delivery, a replayed or
|
|
44
|
-
* re-applied own write). A version it already holds is a duplicate, and one below its origin's
|
|
45
|
-
* maximum was skipped by the relay: both are refused (`false`), nothing is recorded. Otherwise the
|
|
46
|
-
* version joins the evidence exactly as the relay records it.
|
|
47
|
-
*/
|
|
48
|
-
declare function admitLocal(a: ClientAdmission, env: {
|
|
49
|
-
readonly origin: string;
|
|
50
|
-
readonly version: number;
|
|
51
|
-
readonly hlc: Hlc;
|
|
52
|
-
}): boolean;
|
|
53
|
-
/** The context `admits` reads: the installed record's extent and preCutMax, the client's evidence. */
|
|
54
|
-
declare const coverContext: (gen: Pick<InstalledGeneration, "extent" | "preCutMax">, a: ClientAdmission) => CoverContext;
|
|
55
|
-
/**
|
|
56
|
-
* The replay pre-check: every cite of `env` (its own dot excepted) the installed state does not
|
|
57
|
-
* cover, by the same `admits` the relay runs, over the client's register store. An envelope with
|
|
58
|
-
* an uncovered cite is never sent; it is a conflict.
|
|
59
|
-
*/
|
|
60
|
-
declare function uncoveredCites(store: RegisterStore, ctx: CoverContext, env: OpEnvelope): {
|
|
61
|
-
readonly path: readonly Key[];
|
|
62
|
-
readonly dot: Dot;
|
|
63
|
-
}[];
|
|
64
|
-
/** What the client knows of its installed generation when a snapshot welcome arrives. */
|
|
65
|
-
type WelcomeFence = {
|
|
66
|
-
readonly index: number;
|
|
67
|
-
readonly instance: string | undefined;
|
|
68
|
-
/** The seq of the installed snapshot. */
|
|
69
|
-
readonly seq: number;
|
|
70
|
-
/** The highest seq of a relay-delivered envelope of this generation, deduplicated or not. */
|
|
71
|
-
readonly maxSeq: number;
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* Which snapshot welcome installs. A newer generation (higher index) always does. One of the
|
|
75
|
-
* installed generation does when it is not older than the installed snapshot (`stale` otherwise)
|
|
76
|
-
* and not older than relay traffic already delivered (`older-than-traffic`): installing it would
|
|
77
|
-
* replace that traffic with an earlier state. Anything else is stale.
|
|
78
|
-
*/
|
|
79
|
-
declare function welcomeVerdict(installed: WelcomeFence, msg: {
|
|
80
|
-
readonly index: number;
|
|
81
|
-
readonly instance: string;
|
|
82
|
-
readonly seq: number;
|
|
83
|
-
}): 'newer' | 'same' | 'stale' | 'older-than-traffic';
|
|
84
|
-
/** A pending envelope of an earlier generation, as the tail policy reads it. */
|
|
85
|
-
type Pending = {
|
|
86
|
-
readonly env: Pick<OpEnvelope, 'origin' | 'instance' | 'version'>;
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* A new generation's first step with the pending tail: in version order, an envelope whose version
|
|
90
|
-
* the receipts of its origin hold — in ANY generation — was admitted and is never resent; the rest
|
|
91
|
-
* is kept for the tail policy. Membership is instance-agnostic: a replayed envelope carries the
|
|
92
|
-
* current instance, so the relay receipts it there while the tail entry still names the generation
|
|
93
|
-
* it was authored in, and a crash between the wire and the record would otherwise replay it again
|
|
94
|
-
* after the next cut. An origin's versions never repeat (the counter runs through replay; a restart
|
|
95
|
-
* that loses it mints a new origin), so the version alone identifies the write.
|
|
96
|
-
*/
|
|
97
|
-
declare function trimByReceipts<T extends Pending>(pending: readonly T[], receipts: Receipts): {
|
|
98
|
-
readonly trimmed: T[];
|
|
99
|
-
readonly keep: T[];
|
|
100
|
-
};
|
|
101
|
-
/** A refusal was classified: a snapshot at or past its seq is owed. */
|
|
102
|
-
declare const owedAfterRefusal: (owed: number | undefined, seq: number) => number;
|
|
103
|
-
/** A snapshot installed at `seq`: the owed rehydration completes when it reaches the refusal. */
|
|
104
|
-
declare const owedAfterInstall: (owed: number | undefined, seq: number) => number | undefined;
|
|
105
|
-
/**
|
|
106
|
-
* A member's readiness for a consensus cut: it pressed Compact, it is connected, and nothing it
|
|
107
|
-
* wrote is outstanding (its outbox empty, no replay running). A conflict is never outstanding: it
|
|
108
|
-
* is out of the outbox the moment it is classified.
|
|
109
|
-
*/
|
|
110
|
-
declare const readyValue: (s: {
|
|
111
|
-
readonly pressed: boolean;
|
|
112
|
-
readonly connected: boolean;
|
|
113
|
-
readonly replaying: boolean;
|
|
114
|
-
readonly outstanding: number;
|
|
115
|
-
}) => boolean;
|
|
116
|
-
/**
|
|
117
|
-
* The authored intent of an envelope: its raw op list and one bump flag, what a re-author rebuilds
|
|
118
|
-
* from. The raws are the stamped ops without their expansion clears (a diffed write never emits a
|
|
119
|
-
* `clear` of its own, so every `clear` is one) and without cites and epoch. The envelope bumped when
|
|
120
|
-
* an op's epoch rises above the register baseline at its path as the client held it before the
|
|
121
|
-
* envelope (the relay's own test for a bump).
|
|
122
|
-
*/
|
|
123
|
-
type Intent = {
|
|
124
|
-
readonly raws: readonly StoreOp[];
|
|
125
|
-
readonly bump: boolean;
|
|
126
|
-
};
|
|
127
|
-
declare function intentOf(env: OpEnvelope, baseline: (path: readonly Key[]) => number): Intent;
|
|
128
27
|
/**
|
|
129
28
|
* One live connection to a relay. `meshSync` calls the factory per (re)connect; a transport
|
|
130
29
|
* represents a single connection and never reconnects itself.
|
|
@@ -176,17 +75,50 @@ type ConflictCause = 'uncovered' | 'citation' | 'epoch-bump';
|
|
|
176
75
|
* A write that will never be admitted as it stands. Its local effect is retracted (a replay that
|
|
177
76
|
* failed its pre-check is never applied; a refused write is dropped by the snapshot that follows).
|
|
178
77
|
* The two verbs are the whole surface: `discard()` lets it go, `reauthor()` writes its raw ops again
|
|
179
|
-
* as a new write citing the current state. Neither can submit the original envelope.
|
|
78
|
+
* as a new write citing the current state. Neither can submit the original envelope. A conflict is
|
|
79
|
+
* listed once its verdict is stored, and stays listed across reboots until it is answered.
|
|
180
80
|
*/
|
|
181
81
|
type MeshConflict = {
|
|
182
82
|
/** The envelope as it was authored (its values, cites and epoch). */
|
|
183
|
-
readonly env: OpEnvelope
|
|
83
|
+
readonly env: OpEnvelope;
|
|
184
84
|
readonly intent: Intent;
|
|
185
85
|
readonly cause: ConflictCause;
|
|
86
|
+
/** Let the write go. Nothing happens once the session has ended. */
|
|
186
87
|
discard(): void;
|
|
187
|
-
/**
|
|
88
|
+
/**
|
|
89
|
+
* Write the raw ops again as a new write; false when the shell cannot write (no `reauthor`).
|
|
90
|
+
* Also false when the conflict's ops cannot be written again as one write (a path twice, or an
|
|
91
|
+
* op under another op's path that is not an earlier set of a plain record); the conflict then
|
|
92
|
+
* stays, so it can still be discarded. Also false once the session has ended (closed or
|
|
93
|
+
* ejected): nothing is written and the conflict stays held. Also false inside
|
|
94
|
+
* {@link MeshSession.recordTogether}, with the same outcome.
|
|
95
|
+
* `true` means the write was emitted, not that it shows: one whose parent is gone by now, or
|
|
96
|
+
* that loses its fold, leaves the store as it was. The answer and the new write are stored
|
|
97
|
+
* together: after a crash either both are there or neither is. A write made before the call is
|
|
98
|
+
* not part of them: it is recorded on its own, first. False when the re-author emitted nothing.
|
|
99
|
+
*/
|
|
188
100
|
reauthor(): boolean;
|
|
189
101
|
};
|
|
102
|
+
/**
|
|
103
|
+
* Why an own write was not stored: `'generation'` (written in a room generation a cut or a lost
|
|
104
|
+
* room creation closed), `'schema'` (a data shape older than the room's), `'order'` (a version below
|
|
105
|
+
* this origin's admitted maximum that was never admitted: a second writer on one origin),
|
|
106
|
+
* `'compacting'` (a host refused it while this client was declared ready for a cut) or
|
|
107
|
+
* `'incompatible'` (a restored write this client can no longer send).
|
|
108
|
+
*/
|
|
109
|
+
type UnstoredReason = 'generation' | 'schema' | 'order' | 'compacting' | 'incompatible';
|
|
110
|
+
/**
|
|
111
|
+
* An own write that is not in the room and never will be, listed until the application dismisses it.
|
|
112
|
+
* It is listed once its verdict is stored, and again after a reboot until its dismissal is stored.
|
|
113
|
+
*/
|
|
114
|
+
type UnstoredWrite = {
|
|
115
|
+
/** The envelope as it was authored: its values, so the application can offer to write them again. */
|
|
116
|
+
readonly env: OpEnvelope;
|
|
117
|
+
readonly intent: Intent;
|
|
118
|
+
readonly reason: UnstoredReason;
|
|
119
|
+
/** Remove the entry; it leaves the list at once. Nothing happens once the session has ended. */
|
|
120
|
+
dismiss(): void;
|
|
121
|
+
};
|
|
190
122
|
/** The room's compaction state, as the relay last announced it. */
|
|
191
123
|
type CompactionState = {
|
|
192
124
|
/** Droppable tombstones counted against the budget. */
|
|
@@ -198,29 +130,6 @@ type CompactionState = {
|
|
|
198
130
|
};
|
|
199
131
|
/** The room generation the client installed, as its snapshot welcome named it. */
|
|
200
132
|
type GenerationState = Pick<InstalledGeneration, 'instance' | 'index' | 'reason' | 'policy'>;
|
|
201
|
-
/**
|
|
202
|
-
* The session's durable half, persisted as one unit with the outbox: the fields of the generation
|
|
203
|
-
* the tail was written in and its admission evidence, the intents of the pending writes, unresolved
|
|
204
|
-
* conflicts and an owed snapshot. It holds NO registers and no sequence knowledge: a restart never
|
|
205
|
-
* trusts stale registers — it installs nothing from disk, asks for a snapshot (hello without seq)
|
|
206
|
-
* and resends its outbox first.
|
|
207
|
-
*/
|
|
208
|
-
type SessionRecord = {
|
|
209
|
-
readonly generation?: Omit<InstalledGeneration, 'seq'>;
|
|
210
|
-
readonly admission?: ClientAdmissionRecord;
|
|
211
|
-
readonly intents?: readonly {
|
|
212
|
-
readonly origin: string;
|
|
213
|
-
readonly version: number;
|
|
214
|
-
readonly intent: Intent;
|
|
215
|
-
}[];
|
|
216
|
-
readonly conflicts?: readonly {
|
|
217
|
-
readonly env: OpEnvelope$1;
|
|
218
|
-
readonly intent: Intent;
|
|
219
|
-
readonly cause: ConflictCause;
|
|
220
|
-
}[];
|
|
221
|
-
/** A refusal's seq: a snapshot at or past it must install before the client trusts its view. */
|
|
222
|
-
readonly rehydrateOwed?: number;
|
|
223
|
-
};
|
|
224
133
|
type MeshStatus = 'connecting' |
|
|
225
134
|
/** Queued behind another tab that owns this key's durable outbox (`crossTab: 'queue'`). */
|
|
226
135
|
'waiting' | 'live' | 'reconnecting' | 'ejected' | 'closed';
|
|
@@ -231,172 +140,10 @@ type MeshDirect = {
|
|
|
231
140
|
readonly data: unknown;
|
|
232
141
|
};
|
|
233
142
|
/**
|
|
234
|
-
*
|
|
235
|
-
* @
|
|
236
|
-
|
|
237
|
-
declare const deliverDirect: (listeners: ReadonlySet<(msg: MeshDirect) => void>, msg: ServerDirectMsg) => void;
|
|
238
|
-
/** One sequenced remote envelope's ops, attributed to the writer that made them. */
|
|
239
|
-
type RemoteBatch = {
|
|
240
|
-
/** The relay's total-order stamp for this batch. */
|
|
241
|
-
readonly seq: number;
|
|
242
|
-
readonly writer: string;
|
|
243
|
-
readonly ops: readonly StoreOp$1[];
|
|
244
|
-
};
|
|
245
|
-
/**
|
|
246
|
-
* Environment callbacks a session shell provides. All are optional except `onStatus`; the
|
|
247
|
-
* session never touches signals, timers beyond `schedule`, storage, or the DOM itself.
|
|
248
|
-
*/
|
|
249
|
-
type MeshSessionHooks = {
|
|
250
|
-
onStatus(status: MeshStatus, reason?: string): void;
|
|
251
|
-
/** The current op engine's origin, published immediately before each hello. */
|
|
252
|
-
onOrigin?(origin: string): void;
|
|
253
|
-
/** Relay membership, independent of presence; undefined while disconnected or before welcome. */
|
|
254
|
-
onMembers?(members: readonly string[] | undefined): void;
|
|
255
|
-
/**
|
|
256
|
-
* A room message after the session has processed it, for consumers sharing its connection.
|
|
257
|
-
* `direct` frames never pass here; they go to {@link MeshSessionHooks.onDirect} only.
|
|
258
|
-
*/
|
|
259
|
-
onMessage?(msg: ServerMsg): void;
|
|
260
|
-
/**
|
|
261
|
-
* A `direct` frame addressed to this session, in the frame version this side speaks; any other
|
|
262
|
-
* version is refused unread. One that arrives before the current connection's welcome is held
|
|
263
|
-
* (up to 256) and handed over right after it, unless its sender leaves first.
|
|
264
|
-
*/
|
|
265
|
-
onDirect?(msg: ServerDirectMsg): void;
|
|
266
|
-
/** A welcome or envelope arrived — the shell's "last synced" moment. */
|
|
267
|
-
onSynced?(): void;
|
|
268
|
-
/** The presence roster changed (welcome replace, join/leave, teardown clear). */
|
|
269
|
-
onPeers?(peers: ReadonlyMap<string, PresenceState>): void;
|
|
270
|
-
/** A remote (non-own) envelope was ingested, in seq order. */
|
|
271
|
-
onRemote?(batch: RemoteBatch): void;
|
|
272
|
-
/**
|
|
273
|
-
* Seq continuity broke: a snapshot welcome (or a relay instance change) established state
|
|
274
|
-
* without delivering the individual envelopes in between. Any consumer deriving from the
|
|
275
|
-
* `onRemote` stream must rebuild from current state at this seq; appending across a resync
|
|
276
|
-
* would silently skip history.
|
|
277
|
-
*/
|
|
278
|
-
onResync?(seq: number): void;
|
|
279
|
-
/** The unacknowledged local tail changed (emit or ack) — the durable-outbox trigger. */
|
|
280
|
-
onOutboxChange?(): void;
|
|
281
|
-
/**
|
|
282
|
-
* A local write violated the room policy. The session ejects itself immediately after —
|
|
283
|
-
* the outcome the relay's own independent check would have produced one hop later. The
|
|
284
|
-
* emit-side check spares the room the round trip; it is never the enforcement.
|
|
285
|
-
*/
|
|
286
|
-
onLocalReject?(violation: PolicyViolation): void;
|
|
287
|
-
/** The transport closed and a reconnect is scheduled; `code` is the close code (1009 = message too big). */
|
|
288
|
-
onDisconnect?(code: number, reason: string): void;
|
|
289
|
-
/**
|
|
290
|
-
* Teardown is starting; fired before the unacked tail is dropped, so a shell can persist
|
|
291
|
-
* it (and release any single-writer locks) while it is still intact.
|
|
292
|
-
*/
|
|
293
|
-
onTerminal?(state: 'ejected' | 'closed', reason?: string): void;
|
|
294
|
-
/**
|
|
295
|
-
* The relay refused one of this session's writes: it is not in the room and never will be.
|
|
296
|
-
* `'generation'` — written in a room generation that a cut with the `discard` policy closed (a
|
|
297
|
-
* migration or a rollback: the reset is the intent), or in a room recreated without its
|
|
298
|
-
* generation record; `'schema'` — a data shape older than the room's; `'order'` — a version
|
|
299
|
-
* below this origin's admitted maximum that was never admitted (a second tab on one origin);
|
|
300
|
-
* `'compacting'` — a host refused a write from a connection declared ready for the current
|
|
301
|
-
* generation (the relay itself never sends it). The session drops the write locally and hydrates from a fresh snapshot; the envelope carries the
|
|
302
|
-
* values, so the application can offer to write them again. Any `whenAcked` waiter rejects.
|
|
303
|
-
* A write a compaction cut replays is never refused here: it is resent, or it is a conflict.
|
|
304
|
-
*/
|
|
305
|
-
onRefused?(env: OpEnvelope$1, reason: 'generation' | 'schema' | 'order' | 'compacting'): void;
|
|
306
|
-
/** The unresolved conflicts changed (a write classified, discarded or re-authored). */
|
|
307
|
-
onConflicts?(conflicts: readonly MeshConflict[]): void;
|
|
308
|
-
/** The relay announced the room's compaction state (count, budget, ready members). */
|
|
309
|
-
onCompaction?(state: CompactionState): void;
|
|
310
|
-
/**
|
|
311
|
-
* A snapshot welcome of a NEW generation installed (a cut's welcome, or the first snapshot of
|
|
312
|
-
* the session); `undefined` when the room was recreated without its record. A welcome of the
|
|
313
|
-
* installed generation never fires it.
|
|
314
|
-
*/
|
|
315
|
-
onGeneration?(generation: GenerationState | undefined): void;
|
|
316
|
-
};
|
|
317
|
-
type MeshSessionOptions<T extends object> = {
|
|
318
|
-
readonly room: string;
|
|
319
|
-
readonly writer: string;
|
|
320
|
-
readonly transport: MeshTransportFactory;
|
|
321
|
-
/** The op engine this session feeds. The shell creates (and destroys) it. */
|
|
322
|
-
readonly sync: OpSync<T>;
|
|
323
|
-
readonly policies?: readonly MergePolicyEntry[];
|
|
324
|
-
readonly policy?: OpPolicy;
|
|
325
|
-
readonly ctx?: Omit<PrincipalCtx, 'writer'>;
|
|
326
|
-
readonly policyVersion: number;
|
|
327
|
-
readonly schemaVersion?: number;
|
|
328
|
-
/** The room generation a restored outbox was written in; the first welcome decides whether it still holds. */
|
|
329
|
-
readonly instance?: string;
|
|
330
|
-
/**
|
|
331
|
-
* The restored durable record (see {@link SessionRecord}), applied at `connect()` on top of the
|
|
332
|
-
* restored outbox: the registers are hydrated with the pending writes re-applied over them.
|
|
333
|
-
*/
|
|
334
|
-
readonly record?: SessionRecord;
|
|
335
|
-
/**
|
|
336
|
-
* Write a conflict's raw ops again (a new write citing the current state), on the shell's store;
|
|
337
|
-
* with `bump`, as an authority override. Without it a conflict can only be discarded.
|
|
338
|
-
*/
|
|
339
|
-
readonly reauthor?: (intent: Intent) => void;
|
|
340
|
-
readonly reconnect?: {
|
|
341
|
-
readonly maxDelayMs?: number;
|
|
342
|
-
};
|
|
343
|
-
/** Timer seam (tests, deterministic harnesses). Defaults to global timers. */
|
|
344
|
-
readonly schedule?: {
|
|
345
|
-
set(fn: () => void, ms: number): unknown;
|
|
346
|
-
clear(handle: unknown): void;
|
|
347
|
-
};
|
|
348
|
-
readonly hooks: MeshSessionHooks;
|
|
349
|
-
};
|
|
350
|
-
/**
|
|
351
|
-
* The relay-room session protocol, shell-agnostic: hello/welcome handshakes (snapshot
|
|
352
|
-
* hydrate, delta replay, fresh-room seed), own-echo acknowledgement, unacked resend,
|
|
353
|
-
* the relay's per-envelope answers (`drop`), reconnect backoff, presence, eject/reject, the
|
|
354
|
-
* generation fence and settled-vector garbage collection. `meshSync` wraps it for
|
|
355
|
-
* Angular; `agentSeat` wraps it for injector-free environments. One implementation of the
|
|
356
|
-
* wire contract — shells differ only in environment concerns.
|
|
143
|
+
* A data channel as the P2P engine needs it; it opens later. Peer links send only on an open
|
|
144
|
+
* channel, so the contract asks for no buffering, but the channels of {@link rtcPeerConnector}
|
|
145
|
+
* buffer sends made before open and flush them when the channel opens.
|
|
357
146
|
*/
|
|
358
|
-
type MeshSession = {
|
|
359
|
-
/** Open the first connection. Call once, after any outbox restore has replayed. */
|
|
360
|
-
connect(): void;
|
|
361
|
-
status(): MeshStatus;
|
|
362
|
-
/** Highest relay seq observed. With no unacked writes, local state is the pure fold of the room at this seq. */
|
|
363
|
-
lastSeq(): number;
|
|
364
|
-
hasUnacked(): boolean;
|
|
365
|
-
/**
|
|
366
|
-
* Resolves once the unacknowledged local tail is empty — the acknowledgement barrier a
|
|
367
|
-
* caller awaits before treating its writes as the room's. Resolves immediately when
|
|
368
|
-
* nothing is outstanding. Rejects with the terminal reason if the session ends (eject or
|
|
369
|
-
* close) while entries remain: those writes never reached the room, and every later call
|
|
370
|
-
* rejects the same way.
|
|
371
|
-
*
|
|
372
|
-
* With a relay whose adapter confirms durability before echoing, acknowledged means
|
|
373
|
-
* stored; against a relay that echoes eagerly it means only "the relay has it".
|
|
374
|
-
*/
|
|
375
|
-
whenAcked(): Promise<void>;
|
|
376
|
-
/** The unacknowledged local tail (the durable-outbox payload). */
|
|
377
|
-
unackedEnvs(): readonly OpEnvelope$1[];
|
|
378
|
-
/** The room generation this session is in, once a welcome (or a restored outbox) named one. */
|
|
379
|
-
instance(): string | undefined;
|
|
380
|
-
peers(): ReadonlyMap<string, PresenceState>;
|
|
381
|
-
setPresence(data: unknown): void;
|
|
382
|
-
/** Send peer signaling over the current connection without a second hello. */
|
|
383
|
-
sendSignal(msg: ClientSignalMsg): void;
|
|
384
|
-
/** Send a `direct` frame to one member over the current connection; lost when there is none. */
|
|
385
|
-
sendDirect(to: string, data: unknown): void;
|
|
386
|
-
/** Writes that will never be admitted as they stand, each with its two verbs. */
|
|
387
|
-
conflicts(): readonly MeshConflict[];
|
|
388
|
-
/**
|
|
389
|
-
* The member pressed (or released) Compact. While pressed, the session declares itself ready for
|
|
390
|
-
* a consensus cut whenever nothing it wrote is outstanding, and re-declares on every change; a
|
|
391
|
-
* reconnect and a new generation release the press. Safety never depends on it.
|
|
392
|
-
*/
|
|
393
|
-
declareReady(pressed: boolean): void;
|
|
394
|
-
/** The durable record to persist with the outbox (see {@link SessionRecord}). */
|
|
395
|
-
record(): SessionRecord;
|
|
396
|
-
close(): void;
|
|
397
|
-
};
|
|
398
|
-
declare function meshSession<T extends object>(opt: MeshSessionOptions<T>): MeshSession;
|
|
399
|
-
/** A data channel as the P2P engine needs it; opens later, buffers nothing itself. */
|
|
400
147
|
type DataChannelLike = {
|
|
401
148
|
send(frame: string): void;
|
|
402
149
|
onMessage(cb: (frame: string) => void): () => void;
|
|
@@ -573,25 +320,37 @@ type MeshPeer = PresenceState;
|
|
|
573
320
|
/**
|
|
574
321
|
* A composed, user-facing health status for a synced store the surface that
|
|
575
322
|
* turns a versioned reject from a dead socket into a speakable 'outdated' banner.
|
|
323
|
+
* `'storage'`: the outbox can no longer save this handle's writes, and the session has ended and
|
|
324
|
+
* sends nothing more. The reason says why: `'moved'` (another tab wrote the outbox in its place),
|
|
325
|
+
* `'exhausted'` (the outbox ran out of sequence numbers) or `'storage'` (a save failed, or the
|
|
326
|
+
* outcome of a write is not known: it is reported neither as saved nor as lost). A first read of
|
|
327
|
+
* the outbox that fails does not end the handle; see `outboxUnreadable`.
|
|
576
328
|
*/
|
|
577
|
-
type SyncHealthStatus = 'live' | 'waiting' | 'offline' | 'outdated' | 'ejected' | 'degraded';
|
|
329
|
+
type SyncHealthStatus = 'live' | 'waiting' | 'offline' | 'outdated' | 'ejected' | 'degraded' | 'storage';
|
|
578
330
|
type SyncHealth = {
|
|
579
331
|
readonly status: SyncHealthStatus;
|
|
580
|
-
/** Why, when the status is `outdated`/`ejected`/`degraded`. */
|
|
581
|
-
readonly reason?: 'proto' | 'policy-version' | 'schema' | 'quota' | 'worker' | (string & {});
|
|
332
|
+
/** Why, when the status is `outdated`/`ejected`/`degraded`/`storage`. */
|
|
333
|
+
readonly reason?: 'proto' | 'policy-version' | 'schema' | 'quota' | 'worker' | 'storage' | 'moved' | 'exhausted' | (string & {});
|
|
582
334
|
/** `Date.now()` of the last successful sync (welcome or applied env). */
|
|
583
335
|
readonly lastSyncedAt?: number;
|
|
584
336
|
/**
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
|
|
337
|
+
* How many groups of this handle's writes are in storage's hands and not yet saved. Nothing times
|
|
338
|
+
* out: a store that never answers keeps it above zero until the handle closes.
|
|
339
|
+
*/
|
|
340
|
+
readonly storing: number;
|
|
341
|
+
/**
|
|
342
|
+
* Saved offline writes this boot cannot send: written under another writer, protocol version or
|
|
343
|
+
* policy version, malformed, or rejected by the room policy. They are never sent; each is listed
|
|
344
|
+
* in `unstored` with the reason `'incompatible'`.
|
|
588
345
|
*/
|
|
589
346
|
readonly droppedOfflineWrites?: number;
|
|
590
347
|
/**
|
|
591
|
-
*
|
|
592
|
-
*
|
|
348
|
+
* The saved outbox could not be read: it is in a format this build cannot read, or the store
|
|
349
|
+
* failed the read. It is left as it is, and this boot's writes are kept in memory only, under an
|
|
350
|
+
* origin of their own, until the handle closes. The handle runs live over memory, and its
|
|
351
|
+
* health does not turn `'storage'` for this.
|
|
593
352
|
*/
|
|
594
|
-
readonly
|
|
353
|
+
readonly outboxUnreadable?: true;
|
|
595
354
|
/**
|
|
596
355
|
* Received envelopes rejected as malformed by the deterministic well-formedness check (a bad id
|
|
597
356
|
* or path segment, a non-integer version, an unknown op kind, a negative epoch, forged cites, a
|
|
@@ -632,14 +391,36 @@ type MeshSyncOptions = {
|
|
|
632
391
|
readonly reconnect?: {
|
|
633
392
|
readonly maxDelayMs?: number;
|
|
634
393
|
};
|
|
394
|
+
/**
|
|
395
|
+
* How long a hello may go unanswered before the connection is closed and opened again:
|
|
396
|
+
* `timeoutMs` (default 15 000), doubled for every hello in a row that went unanswered, up to
|
|
397
|
+
* `maxTimeoutMs` (default 120 000). An answered hello starts the count again. A hello that goes
|
|
398
|
+
* unanswered also counts as a failed attempt for the reconnect backoff. A relay that never
|
|
399
|
+
* answers a hello within `maxTimeoutMs` leaves the client resetting the connection at that pace
|
|
400
|
+
* for as long as it lasts: nothing is lost, and nothing gets through.
|
|
401
|
+
*/
|
|
402
|
+
readonly handshake?: {
|
|
403
|
+
readonly timeoutMs?: number;
|
|
404
|
+
readonly maxTimeoutMs?: number;
|
|
405
|
+
};
|
|
635
406
|
readonly injector?: Injector;
|
|
636
407
|
/** Register with the nearest transition scope so (re)connection surfaces as `pending`. */
|
|
637
408
|
readonly register?: 'track' | 'suspend';
|
|
409
|
+
/**
|
|
410
|
+
* Called once when the handle ends by itself (`ejected`, with the reason) and when a
|
|
411
|
+
* `clearOutbox()` ends it (`'cleared'`); never for `close()`. A callback that throws is reported
|
|
412
|
+
* and changes nothing.
|
|
413
|
+
*/
|
|
638
414
|
readonly onEject?: (reason: string) => void;
|
|
639
|
-
/**
|
|
415
|
+
/**
|
|
416
|
+
* The connection dropped and a reconnect is scheduled; `code` is the close code (1009 = message
|
|
417
|
+
* too big, 1006 = dropped). The client also closes a connection itself, with code 1000 and the
|
|
418
|
+
* reason `'timeout'` (a hello went unanswered), `'displaced'` (a later connection of this client
|
|
419
|
+
* took its place) or `'counter'` (the connection used up its request ids). A transport factory or
|
|
420
|
+
* a `send` that throws is reported and loses the connection, with code 1006 and the reason
|
|
421
|
+
* `'transport'` or `'send'`.
|
|
422
|
+
*/
|
|
640
423
|
readonly onDisconnect?: (code: number, reason: string) => void;
|
|
641
|
-
/** A write the relay refused (see `MeshSessionHooks.onRefused`); the envelope carries its values. */
|
|
642
|
-
readonly onRefused?: (env: OpEnvelope$1, reason: 'generation' | 'schema' | 'order' | 'compacting') => void;
|
|
643
424
|
/**
|
|
644
425
|
* Hold the connection until the local base is assembled. `meshSync` awaits this before it connects
|
|
645
426
|
* (and before it restores an `outbox`), so a store hydrated from another source first (a worker
|
|
@@ -649,35 +430,50 @@ type MeshSyncOptions = {
|
|
|
649
430
|
*/
|
|
650
431
|
readonly whenReady?: () => PromiseLike<void> | void;
|
|
651
432
|
/**
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
*
|
|
655
|
-
*
|
|
433
|
+
* Keep this client's own writes in `store` under `key`, so offline writes survive a reboot, not
|
|
434
|
+
* just a live reconnect. Every write is saved before it is sent, and every answer about it is
|
|
435
|
+
* saved before it shows, so a crash at any moment loses nothing that was sent. On boot the saved
|
|
436
|
+
* writes that are still pending are restored and sent again, unchanged, after the next welcome.
|
|
437
|
+
* The saved outbox also keeps this client's origin, so every boot that reads it continues as the
|
|
438
|
+
* same client. Without an outbox, writes are kept in memory only and each boot has an origin of
|
|
439
|
+
* its own.
|
|
440
|
+
*
|
|
441
|
+
* The store is a `JournalStore` (build one over idb-keyval's shape with `journalStore`). Two
|
|
442
|
+
* keys are written, `<key>:log` and `<key>:snap`. A value saved under `key` itself by an earlier
|
|
443
|
+
* version of this library is deleted unread at each boot, when the store can delete.
|
|
656
444
|
*
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
*
|
|
445
|
+
* Storage decides between tabs: when another tab has written the outbox in this handle's place,
|
|
446
|
+
* this handle ends `ejected` with the reason `'moved'`. A write it had not saved by then is not
|
|
447
|
+
* saved, and no other tab can send it: `acked` reads `false` and `whenAcked()` rejects, and the
|
|
448
|
+
* application decides what to do with it. A Web Lock on `key` keeps live tabs from taking each
|
|
449
|
+
* other's place; `crossTab` sets what a tab that did not get the lock does. Within one page, a handle
|
|
450
|
+
* made in the turn another handle over the same key was closed takes over from it (unless another
|
|
451
|
+
* tab asked for the lock first). A handle made while another handle of this page over the
|
|
452
|
+
* same key is still open is treated as a second tab would be: `'queue'` waits for it, `'ephemeral'`
|
|
453
|
+
* runs over memory for its life. So close the old handle first. Two copies of this library in one
|
|
454
|
+
* page behave as two tabs. Store writes made while the boot
|
|
455
|
+
* waits (for the lock or the read) are held in memory and sent, as their net change, when the tab
|
|
456
|
+
* boots. Nothing times out: a store call that never answers keeps the handle live and holding the
|
|
457
|
+
* lock, and `health().storing` above zero, until it closes.
|
|
666
458
|
*/
|
|
667
459
|
readonly outbox?: {
|
|
668
460
|
readonly key: string;
|
|
669
|
-
readonly store:
|
|
670
|
-
/** Coalesce outbox writes by this many ms (default 300; `0` = write on every change). */
|
|
671
|
-
readonly debounceMs?: number;
|
|
461
|
+
readonly store: JournalStore;
|
|
672
462
|
/**
|
|
673
|
-
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
463
|
+
* What a tab does when another tab holds the lock for `key`.
|
|
464
|
+
* `'queue'` (default): it WAITS (`status` reads `'waiting'`) and takes over when the owner
|
|
465
|
+
* closes; store writes made while it waits are sent, as their net change, when it takes over.
|
|
466
|
+
* `'ephemeral'`: it runs live at once, as a client of its own, and never touches the saved
|
|
467
|
+
* outbox: its writes live in memory only and are lost if that tab ends before the room
|
|
468
|
+
* acknowledges them. It stays ephemeral for life, also after the owner leaves. When the lock is
|
|
469
|
+
* free both behave the same (take it, restore, save).
|
|
470
|
+
* A `'queue'` handle closed while it waits for another tab keeps its place in the browser's lock
|
|
471
|
+
* queue until its turn; then the lock is taken and given back at once. Until then a new handle
|
|
472
|
+
* of this page is treated as it would be while that other tab holds the lock.
|
|
473
|
+
* `'off'` skips the lock: every tab that opens the key continues the same saved client, and
|
|
474
|
+
* when two tabs write it, one of them ends `ejected` with the reason `'moved'`. Without the
|
|
475
|
+
* lock a tab that has only read the outbox does not keep another tab's `clearOutbox()` from
|
|
476
|
+
* wiping what it is about to send. In development mode it warns once.
|
|
681
477
|
*/
|
|
682
478
|
readonly crossTab?: 'queue' | 'off' | 'ephemeral';
|
|
683
479
|
};
|
|
@@ -694,7 +490,10 @@ type MeshSyncOptions = {
|
|
|
694
490
|
type SeatSync<T = unknown> = Pick<OpSync<T>, 'override' | 'captureFrontier' | 'commitScope' | 'liveUnder' | 'liveAt' | 'appliedFrontier'>;
|
|
695
491
|
type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
696
492
|
readonly status: Signal<MeshStatus>;
|
|
697
|
-
/**
|
|
493
|
+
/**
|
|
494
|
+
* This client's origin: the saved outbox's, the same on every boot that reads it, or one of its
|
|
495
|
+
* own for a boot without a saved outbox. Null until the outbox has loaded.
|
|
496
|
+
*/
|
|
698
497
|
readonly origin: Signal<string | null>;
|
|
699
498
|
/** Other relay members, including peers with no presence; empty before welcome and while disconnected. */
|
|
700
499
|
readonly members: Signal<readonly string[]>;
|
|
@@ -703,16 +502,36 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
703
502
|
/** Composed sync-health for a user-facing surface. */
|
|
704
503
|
readonly health: Signal<SyncHealth>;
|
|
705
504
|
/**
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
505
|
+
* Everything written through this handle is saved as the room's: nothing is pending or still
|
|
506
|
+
* being saved and no conflict is held. The signal follows the sync engine, which takes a store
|
|
507
|
+
* write up a moment later, at its next tick: read in the same turn as a write, it still gives
|
|
508
|
+
* the earlier answer. `whenAcked()` counts a write made in this turn, and from that call this
|
|
509
|
+
* signal counts it too. To ask whether everything is saved, including what was just written,
|
|
510
|
+
* call `whenAcked()`. `false` until the outbox has
|
|
511
|
+
* loaded (what is pending is not known before), while a write waits for the room or for storage,
|
|
512
|
+
* while a conflict is held (until it is discarded or written again), and for good once a write
|
|
513
|
+
* can no longer land: a write after an eject, or a write the emit-side policy
|
|
514
|
+
* rejected. After an eject it is `true` exactly when nothing was outstanding at the ejection
|
|
515
|
+
* and nothing was written since. A close freezes it: it keeps the value it had at the close,
|
|
516
|
+
* and a write after the close does not change it. After a storage failure or `clearOutbox()` it
|
|
517
|
+
* is `false`. Writes inside `whenReady` are the local base, not writes of their own. Writes made
|
|
518
|
+
* while the boot is held (waiting for the outbox lock or its read) are saved and sent, as their
|
|
519
|
+
* net change, when the boot ends; each `sync.override` made in the hold is its own authority
|
|
520
|
+
* write, in order. A write the relay refused is listed in `unstored`, not here.
|
|
709
521
|
*/
|
|
710
522
|
readonly acked: Signal<boolean>;
|
|
711
523
|
/**
|
|
712
|
-
* Resolves
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
524
|
+
* Resolves once nothing written through this handle is outstanding and that is saved
|
|
525
|
+
* (immediately if it already is). Before the outbox has loaded it waits for the load. Rejects
|
|
526
|
+
* with the terminal reason if the session ends while writes are outstanding or a write can no
|
|
527
|
+
* longer land (`'closed'` for writes held in a boot that is closed before it connects), with
|
|
528
|
+
* `'storage'`, `'moved'` or `'exhausted'` once the outbox can no longer save, with `'cleared'`
|
|
529
|
+
* after `clearOutbox()`, and with `'schema'` when this client's data shape is older than the
|
|
530
|
+
* room's. While a conflict is held it rejects at once with `conflict: <cause>`; a terminal reason
|
|
531
|
+
* wins. A write the relay refused rejects the calls pending when its answer is saved with
|
|
532
|
+
* `refused: <reason>`; a later call does not wait for it. The barrier to await before publishing
|
|
533
|
+
* something that must contain every edit; `synced()` is a connection indicator, not a save
|
|
534
|
+
* barrier.
|
|
716
535
|
*/
|
|
717
536
|
whenAcked(): Promise<void>;
|
|
718
537
|
readonly peers: Signal<readonly MeshPeer[]>;
|
|
@@ -726,6 +545,7 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
726
545
|
/**
|
|
727
546
|
* Listen for `direct` frames addressed to this client; returns the unsubscribe. A frame that
|
|
728
547
|
* arrives with no listener is lost. Peer links on {@link MeshSyncRef.signaling} never see them.
|
|
548
|
+
* A listener that throws is reported, and the others still hear the frame.
|
|
729
549
|
*/
|
|
730
550
|
direct(cb: (msg: MeshDirect) => void): () => void;
|
|
731
551
|
/**
|
|
@@ -733,23 +553,40 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
733
553
|
* fork observes the room as it is now; committing emits its diff citing only those observed
|
|
734
554
|
* writes, so an edit that lands on the room while the fork is open stays a concurrent value the
|
|
735
555
|
* merge policy decides rather than being overwritten by the commit. Call `rebase()` to re-observe
|
|
736
|
-
* the room and commit on top of the latest instead. `discard()` drops the staged edits
|
|
556
|
+
* the room and commit on top of the latest instead. `discard()` drops the staged edits and also
|
|
557
|
+
* re-observes the room, so a later commit on the same fork cites the room as it is at the discard.
|
|
558
|
+
* Before the boot nothing has been observed, so a fork commit made then is a plain held write.
|
|
737
559
|
*/
|
|
738
560
|
fork(opt?: ForkStoreOptions<T & Record<string, any>>): SyncedFork<T & Record<string, any>>;
|
|
739
|
-
/**
|
|
561
|
+
/**
|
|
562
|
+
* Authority surface over this handle's op-sync; see {@link SeatSync}. It is one object for the
|
|
563
|
+
* handle's life: taken before the boot, it works over the engine once the outbox has loaded.
|
|
564
|
+
* When the callback of `override` throws, the error reaches the caller and the writes it made are
|
|
565
|
+
* still sent with the override's authority, also while the boot is held. Before the boot nothing
|
|
566
|
+
* has been observed, so a `commitScope` run then is a plain held write.
|
|
567
|
+
*/
|
|
740
568
|
readonly sync: SeatSync<T>;
|
|
741
569
|
/**
|
|
742
570
|
* Writes that will never be admitted as they stand: a write a compaction cut could not replay
|
|
743
571
|
* (it cites something the cut folded away) or one the relay refused. Each offers `discard()` or
|
|
744
|
-
* `reauthor()` (write its values again, citing the current state).
|
|
572
|
+
* `reauthor()` (write its values again, citing the current state). Saved with the outbox: a
|
|
573
|
+
* conflict stays listed across reboots until it is answered.
|
|
745
574
|
*/
|
|
746
575
|
readonly conflicts: Signal<readonly MeshConflict[]>;
|
|
576
|
+
/**
|
|
577
|
+
* Own writes that are not in the room and never will be, in the order they were made: refused by
|
|
578
|
+
* the relay, discarded with a room generation, or saved offline and no longer sendable by this
|
|
579
|
+
* boot (`'incompatible'`). Each entry holds the write, so it can be offered again, its reason, and
|
|
580
|
+
* `dismiss()`, which removes it. An entry is listed once its answer is saved and stays listed
|
|
581
|
+
* across reboots until its dismissal is saved. Empty before the outbox has loaded.
|
|
582
|
+
*/
|
|
583
|
+
readonly unstored: Signal<readonly UnstoredWrite[]>;
|
|
747
584
|
/** The room's compaction state as the relay last announced it; undefined before any notice. */
|
|
748
585
|
readonly compaction: Signal<CompactionState | undefined>;
|
|
749
586
|
/**
|
|
750
587
|
* The room generation this client installed: set by every snapshot welcome of a new generation
|
|
751
588
|
* (a cut's welcome flips it), untouched by one of the installed generation. Undefined until the
|
|
752
|
-
* first snapshot welcome
|
|
589
|
+
* first snapshot welcome of this boot.
|
|
753
590
|
*/
|
|
754
591
|
readonly generation: Signal<GenerationState | undefined>;
|
|
755
592
|
/**
|
|
@@ -758,6 +595,55 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
758
595
|
* reconnect or a new generation releases it.
|
|
759
596
|
*/
|
|
760
597
|
declareReady(pressed: boolean): void;
|
|
598
|
+
/**
|
|
599
|
+
* Discard every write not yet in the room, every conflict and every `unstored` entry, and end the
|
|
600
|
+
* session. From the call nothing more is sent or saved, `acked` reads `false` and `whenAcked()`
|
|
601
|
+
* rejects with `'cleared'`; the handle keeps the outbox lock and its status until storage answers.
|
|
602
|
+
* The session ends `closed` with the reason `'cleared'` once storage holds the wipe, and the
|
|
603
|
+
* promise resolves then. It rejects with `'moved'` when another tab wrote the outbox first, with
|
|
604
|
+
* `'storage'` when the outcome is not known, and with the reason of any ending that comes first
|
|
605
|
+
* (`'closed'` for `close()`). A crash before it resolves leaves the outbox as it was, and the next
|
|
606
|
+
* boot continues it and sends what is pending. Called before the boot has finished, it clears
|
|
607
|
+
* once the boot is done, or rejects with the boot's reason.
|
|
608
|
+
*
|
|
609
|
+
* A handle runs over memory when it was given no `outbox`, when it is an `'ephemeral'` tab beside
|
|
610
|
+
* another tab that holds the outbox, or when the saved outbox could not be read. Called while
|
|
611
|
+
* such a handle is live, the clear discards this handle's own writes, resolves, and ends the
|
|
612
|
+
* handle `closed` with `'cleared'`, calling `onEject('cleared')`. A saved outbox the handle never
|
|
613
|
+
* held is left exactly as it is, and the next boot that can read it sends what it holds. An
|
|
614
|
+
* outbox that could not be read is not removed by `clearOutbox()`: the application removes it
|
|
615
|
+
* from its own store, under the key it gave, when it wants it gone.
|
|
616
|
+
*
|
|
617
|
+
* Called once the session has ended by itself (ejected, outdated, a storage ending), it takes the
|
|
618
|
+
* outbox lock if it is free (else it rejects with `'busy'`), reads the outbox again and wipes it
|
|
619
|
+
* only when no other tab has written it since this handle ended (else it rejects with `'moved'`);
|
|
620
|
+
* it rejects with `'unreadable'` when the outbox cannot be read again, and with `'storage'` when
|
|
621
|
+
* the store does not say whether the wipe was stored. It
|
|
622
|
+
* holds the lock until storage answers, and `close()` meanwhile rejects it with `'closed'`.
|
|
623
|
+
* After `close()`, and for a handle that never held the saved outbox, it rejects with
|
|
624
|
+
* `'closed'`. A second call while one runs joins it.
|
|
625
|
+
*/
|
|
626
|
+
clearOutbox(): Promise<void>;
|
|
627
|
+
/**
|
|
628
|
+
* Leave the room, release the outbox lock and destroy the op engine. A write made in the same
|
|
629
|
+
* tick is handed to storage, before the lock is released, only when nothing of this handle is in
|
|
630
|
+
* storage's hands: no earlier write still being saved, and not the record the handle saves as it
|
|
631
|
+
* goes live, which storage holds for a moment as the status turns `'live'`. A write queued behind
|
|
632
|
+
* anything still out is not saved. Either way nobody is told it is stored: `acked` reads `false`
|
|
633
|
+
* and `whenAcked()` rejects with `'closed'`. To know that every write is saved, wait for
|
|
634
|
+
* `whenAcked()`, or for `acked` to read `true`, before closing.
|
|
635
|
+
* A save in storage's hands may still land after the lock is released, and storage decides
|
|
636
|
+
* between it and the next tab's writes.
|
|
637
|
+
* Closed while the boot is still held, the writes made in the hold are dropped: they were never
|
|
638
|
+
* saved, and with a net change `acked` reads `false` and `whenAcked()` rejects with `'closed'`.
|
|
639
|
+
* Without one, an outbox read in flight is let finish, writing nothing, and both answer from it:
|
|
640
|
+
* nothing pending and no conflict held reads `true` and resolves; anything else, or an outbox
|
|
641
|
+
* that cannot be read, reads `false` and rejects with `'closed'`. Until then `acked` is `false`
|
|
642
|
+
* and `whenAcked()` waits.
|
|
643
|
+
* The handle is frozen from then on: the store keeps working locally, but nothing is sent or
|
|
644
|
+
* saved, and `acked`, `conflicts`, `unstored` and `generation` keep the value they had at close.
|
|
645
|
+
* Destroying the injector closes it the same way.
|
|
646
|
+
*/
|
|
761
647
|
close(): void;
|
|
762
648
|
};
|
|
763
649
|
/**
|
|
@@ -774,7 +660,7 @@ type SeatChange = {
|
|
|
774
660
|
/** The relay's total-order stamp. Strictly increasing across `change` events between resyncs. */
|
|
775
661
|
readonly seq: number;
|
|
776
662
|
readonly writer: string;
|
|
777
|
-
readonly ops: readonly StoreOp
|
|
663
|
+
readonly ops: readonly StoreOp[];
|
|
778
664
|
};
|
|
779
665
|
/**
|
|
780
666
|
* Seq continuity broke: the seat re-established state from a room snapshot (a reconnect past
|
|
@@ -822,14 +708,30 @@ type AgentSeatOptions = {
|
|
|
822
708
|
readonly reconnect?: {
|
|
823
709
|
readonly maxDelayMs?: number;
|
|
824
710
|
};
|
|
711
|
+
/**
|
|
712
|
+
* How long a hello may go unanswered before the connection is closed and opened again:
|
|
713
|
+
* `timeoutMs` (default 15 000), doubled for every hello in a row that went unanswered, up to
|
|
714
|
+
* `maxTimeoutMs` (default 120 000). An answered hello starts the count again. A relay that never
|
|
715
|
+
* answers a hello within `maxTimeoutMs` leaves the seat resetting the connection at that pace for
|
|
716
|
+
* as long as it lasts: nothing is lost, and nothing gets through.
|
|
717
|
+
*/
|
|
718
|
+
readonly handshake?: {
|
|
719
|
+
readonly timeoutMs?: number;
|
|
720
|
+
readonly maxTimeoutMs?: number;
|
|
721
|
+
};
|
|
825
722
|
/**
|
|
826
723
|
* Store context for the replica (from `createStoreContext()`). Defaults to a fresh one;
|
|
827
724
|
* pass your own to share proxy caches across several seats in one process.
|
|
828
725
|
*/
|
|
829
726
|
readonly context?: toStoreOptions;
|
|
727
|
+
/**
|
|
728
|
+
* Called once when the seat ends by itself (`ejected`, with the reason, an outdated data shape
|
|
729
|
+
* included) and when a `clearOutbox()` ends it (`'cleared'`); never for `close()`. It is called
|
|
730
|
+
* last: the seat has ended and every clear it owed has settled, so inside it `status()`,
|
|
731
|
+
* `peers()`, `acked()` and `whenAcked()` answer as they do after it (`'ejected'`, or `'closed'`
|
|
732
|
+
* after a clear). A callback that throws is reported and changes nothing.
|
|
733
|
+
*/
|
|
830
734
|
readonly onEject?: (reason: string) => void;
|
|
831
|
-
/** A write the relay refused; it is not in the room, and the doc was rehydrated. The envelope carries its values. */
|
|
832
|
-
readonly onRefused?: (env: OpEnvelope$1, reason: 'generation' | 'schema' | 'order' | 'compacting') => void;
|
|
833
735
|
};
|
|
834
736
|
type AgentSeat<T extends object> = {
|
|
835
737
|
/** The live converging replica. Reads exactly like a local signal store. */
|
|
@@ -839,8 +741,11 @@ type AgentSeat<T extends object> = {
|
|
|
839
741
|
/** The current document value — plain JSON-serializable data, for context assembly. */
|
|
840
742
|
snapshot(): T;
|
|
841
743
|
/**
|
|
842
|
-
* The current document, stamped with the relay seq it is provably the pure fold of
|
|
843
|
-
*
|
|
744
|
+
* The current document, stamped with the relay seq it is provably the pure fold of, or null
|
|
745
|
+
* when that cannot be guaranteed: before the first welcome, while one of the seat's own writes
|
|
746
|
+
* is still unacknowledged, and from a refused write until the snapshot that replaces it lands.
|
|
747
|
+
* It stays null for good once the seat ended with writes outstanding, after an emit-side policy
|
|
748
|
+
* reject, and after `close()`.
|
|
844
749
|
*/
|
|
845
750
|
stableSnapshot(): StableSnapshot<T> | null;
|
|
846
751
|
/** Run local writes and emit them to the room as one envelope, like any peer's edit. */
|
|
@@ -850,6 +755,12 @@ type AgentSeat<T extends object> = {
|
|
|
850
755
|
* carry, or a relay refusal), each with `discard()` and `reauthor()`.
|
|
851
756
|
*/
|
|
852
757
|
conflicts(): readonly MeshConflict[];
|
|
758
|
+
/**
|
|
759
|
+
* The seat's writes that are not in the room and never will be, in the order they were made:
|
|
760
|
+
* refused by the relay or discarded with a room generation. Each entry holds the write, its
|
|
761
|
+
* reason and `dismiss()`, which removes it.
|
|
762
|
+
*/
|
|
763
|
+
readonly unstored: Signal<readonly UnstoredWrite[]>;
|
|
853
764
|
/** The room's compaction state as the relay last announced it; undefined before any notice. */
|
|
854
765
|
readonly compaction: Signal<CompactionState | undefined>;
|
|
855
766
|
/**
|
|
@@ -873,20 +784,43 @@ type AgentSeat<T extends object> = {
|
|
|
873
784
|
* `commit()` emits its diff citing only those observed writes, so a room edit that lands
|
|
874
785
|
* while the fork is open survives as a concurrent value for the merge policy to resolve
|
|
875
786
|
* instead of being overwritten. `rebase()` re-observes the room; `discard()` drops the
|
|
876
|
-
* staged edits
|
|
787
|
+
* staged edits and also re-observes the room, so a later commit on the same fork cites the
|
|
788
|
+
* room as it is at the discard.
|
|
877
789
|
*/
|
|
878
790
|
fork(opt?: ForkStoreOptions<T & Record<string, any>>): SyncedFork<T & Record<string, any>>;
|
|
879
791
|
/**
|
|
880
|
-
*
|
|
881
|
-
*
|
|
792
|
+
* Everything written through this seat is the room's: nothing is pending and no conflict is
|
|
793
|
+
* held. It counts a write made in the same turn as the call. `false` until the seat has started
|
|
794
|
+
* (a moment after it is created),
|
|
795
|
+
* while a write waits for the room, while a conflict is held (until it is discarded or written
|
|
796
|
+
* again), and for good once a write can no longer land: a write after an eject,
|
|
797
|
+
* or a write the emit-side policy rejected. After an eject it is `true` exactly when nothing was
|
|
798
|
+
* outstanding at the ejection and nothing was written since. A close freezes it: it keeps the
|
|
799
|
+
* answer it had at the close, and a write after the close does not change it. After a storage
|
|
800
|
+
* failure or `clearOutbox()` it is `false`. A write the relay refused is listed in `unstored`,
|
|
801
|
+
* not here.
|
|
882
802
|
*/
|
|
883
803
|
acked(): boolean;
|
|
884
804
|
/**
|
|
885
|
-
* Resolves
|
|
886
|
-
*
|
|
887
|
-
*
|
|
805
|
+
* Resolves once nothing written through this seat is outstanding (immediately if it already is;
|
|
806
|
+
* before the seat has started it waits for the start). Rejects with the terminal reason if the
|
|
807
|
+
* seat ends while writes are outstanding or a write can no longer land, and with `'cleared'`
|
|
808
|
+
* after `clearOutbox()`. While a conflict is held it rejects at once with `conflict: <cause>`; a
|
|
809
|
+
* terminal reason wins. A write the relay refused rejects the calls pending when it is answered
|
|
810
|
+
* with `refused: <reason>`. The barrier to await before handing a result to something that must
|
|
811
|
+
* contain every edit.
|
|
888
812
|
*/
|
|
889
813
|
whenAcked(): Promise<void>;
|
|
814
|
+
/**
|
|
815
|
+
* Discard every write not yet in the room, every conflict and every `unstored` entry, and end the
|
|
816
|
+
* seat: nothing more is sent, the status reads `'closed'`, and `whenAcked()` rejects with
|
|
817
|
+
* `'cleared'`. Called before the seat has started, it clears once it has; when the start fails it
|
|
818
|
+
* rejects with that reason, then and on every later call. After `close()` it rejects with
|
|
819
|
+
* `'closed'`. After an eject or an outdated ending the seat's journal is already closed, as at
|
|
820
|
+
* every ending, and a seat keeps nothing on storage: the call marks the seat cleared (`acked()`
|
|
821
|
+
* reads `false`, `whenAcked()` rejects with `'cleared'`) and resolves at once.
|
|
822
|
+
*/
|
|
823
|
+
clearOutbox(): Promise<void>;
|
|
890
824
|
/** Publish this seat's ephemeral presence payload (e.g. `{ name, kind: 'agent' }`). */
|
|
891
825
|
setPresence(data: unknown): void;
|
|
892
826
|
/**
|
|
@@ -901,6 +835,13 @@ type AgentSeat<T extends object> = {
|
|
|
901
835
|
direct(cb: (msg: MeshDirect) => void): () => void;
|
|
902
836
|
/** Authority surface over this seat's op-sync; see {@link SeatSync}. */
|
|
903
837
|
readonly sync: SeatSync<T>;
|
|
838
|
+
/**
|
|
839
|
+
* Leave the room and destroy the seat's op engine. The handle is frozen from then on: `doc`
|
|
840
|
+
* still reads and writes locally, but nothing is sent, `acked()` and `whenAcked()` keep the
|
|
841
|
+
* answer they had at close, and `stableSnapshot()` is null. Closed before the seat has started,
|
|
842
|
+
* they answer once its journal has been read: with nothing written, `acked()` reads `true` and
|
|
843
|
+
* `whenAcked()` resolves; until then `acked()` is `false` and `whenAcked()` waits.
|
|
844
|
+
*/
|
|
904
845
|
close(): void;
|
|
905
846
|
};
|
|
906
847
|
/**
|
|
@@ -927,7 +868,7 @@ export declare function setAtPath(node: unknown, path: string | readonly PathSeg
|
|
|
927
868
|
* activity feeds and model context. Values render as truncated JSON. Pair with a seat's
|
|
928
869
|
* `changes` stream: narrating each batch's ops yields an append-only account of the room.
|
|
929
870
|
*/
|
|
930
|
-
export declare function describeOp(op: StoreOp
|
|
871
|
+
export declare function describeOp(op: StoreOp, writer: string): string;
|
|
931
872
|
type WebRtcMeshOptions = {
|
|
932
873
|
readonly room: string;
|
|
933
874
|
readonly writer: string;
|
|
@@ -965,8 +906,8 @@ type WebRtcMeshRef = {
|
|
|
965
906
|
* Peer-to-peer mesh sync: the relay only signals
|
|
966
907
|
* and tracks membership; envelopes flow over WebRTC data channels and converge via the
|
|
967
908
|
* per-path register map. Catch-up is pairwise: on channel open both sides exchange
|
|
968
|
-
* watermarks; a side whose state is
|
|
969
|
-
* checkpoint (root + per-path register state + watermark), so supersession and precedence
|
|
909
|
+
* watermarks; a side whose state is covered (the other holds every envelope it holds)
|
|
910
|
+
* hydrates from the other's FULL checkpoint (root + per-path register state + watermark), so supersession and precedence
|
|
970
911
|
* carry over intact: a late joiner is indistinguishable from a peer that saw every
|
|
971
912
|
* envelope. Two peers that each hold envelopes the other lacks keep their convergent
|
|
972
913
|
* go-forward guarantees but do not exchange the missed envelopes retroactively.
|
|
@@ -1039,4 +980,4 @@ type RtcPresenceRef<T> = {
|
|
|
1039
980
|
* Values travel as JSON.
|
|
1040
981
|
*/
|
|
1041
982
|
export declare function rtcPresence<T>(links: PeerLinks, opt: RtcPresenceOptions): RtcPresenceRef<T>;
|
|
1042
|
-
export type { AgentSeat, AgentSeatOptions, DataChannelLike, DirectTransportOptions, MeshDirect, MeshPeer, MeshStatus, MeshSyncOptions, MeshSyncRef, MeshTransport, MeshTransportFactory, PeerChannelSpec, PeerConnector, PeerLinks, PeerLinksOptions, PresenceFrame, RtcPresenceOptions, RtcPresenceRef, SeatChange, SeatEvent, SeatResync, SeatSync, SignalingPort, StableSnapshot, SyncHealth, SyncHealthStatus, WebRtcMeshOptions, WebRtcMeshRef };
|
|
983
|
+
export type { AgentSeat, AgentSeatOptions, DataChannelLike, DirectTransportOptions, MeshDirect, MeshPeer, MeshStatus, MeshSyncOptions, MeshSyncRef, MeshTransport, MeshTransportFactory, PeerChannelSpec, PeerConnector, PeerLinks, PeerLinksOptions, PresenceFrame, RtcPresenceOptions, RtcPresenceRef, SeatChange, SeatEvent, SeatResync, SeatSync, SignalingPort, StableSnapshot, SyncHealth, SyncHealthStatus, UnstoredReason, UnstoredWrite, WebRtcMeshOptions, WebRtcMeshRef };
|