@mmstack/mesh 21.15.1 → 21.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 +269 -59
- package/fesm2022/mmstack-mesh.mjs +3679 -1046
- package/fesm2022/mmstack-mesh.mjs.map +1 -1
- package/package.json +8 -4
- package/types/mmstack-mesh.d.ts +322 -81
package/types/mmstack-mesh.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Signal, Injector, WritableSignal } from '@angular/core';
|
|
2
|
-
import {
|
|
3
|
-
import { OpEnvelope, OpSync, MergePolicyEntry, DiffOpsOptions,
|
|
2
|
+
import { GenerationReason, TailPolicy, Hlc, ClientMsg, ServerMsg, Relay, PrincipalCtx, IceServer, ClientSignalMsg, PresenceState, OpPolicy } from '@mmstack/mesh-protocol';
|
|
3
|
+
import { StoreOp, OpEnvelope, OpSync, MergePolicyEntry, DiffOpsOptions, JournalStore, ForkStoreOptions, SyncedFork, WritableSignalStore, toStoreOptions } from '@mmstack/primitives/core';
|
|
4
|
+
|
|
5
|
+
/** The authored intent of an own envelope: its raw ops and one bump flag, what a re-author rebuilds from. */
|
|
6
|
+
type Intent = {
|
|
7
|
+
readonly raws: readonly StoreOp[];
|
|
8
|
+
readonly bump: boolean;
|
|
9
|
+
};
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* The client half of a room generation: the record fields a snapshot welcome carries, the admission
|
|
@@ -20,17 +26,6 @@ type InstalledGeneration = {
|
|
|
20
26
|
/** The seq of the snapshot welcome that installed it (or a later one of the same generation). */
|
|
21
27
|
readonly seq: number;
|
|
22
28
|
};
|
|
23
|
-
/**
|
|
24
|
-
* The authored intent of an envelope: its raw op list and one bump flag, what a re-author rebuilds
|
|
25
|
-
* from. The raws are the stamped ops without their expansion clears (a diffed write never emits a
|
|
26
|
-
* `clear` of its own, so every `clear` is one) and without cites and epoch. The envelope bumped when
|
|
27
|
-
* an op's epoch rises above the register baseline at its path as the client held it before the
|
|
28
|
-
* envelope (the relay's own test for a bump).
|
|
29
|
-
*/
|
|
30
|
-
type Intent = {
|
|
31
|
-
readonly raws: readonly StoreOp[];
|
|
32
|
-
readonly bump: boolean;
|
|
33
|
-
};
|
|
34
29
|
|
|
35
30
|
/**
|
|
36
31
|
* One live connection to a relay. `meshSync` calls the factory per (re)connect; a transport
|
|
@@ -49,15 +44,30 @@ type MeshTransportFactory = () => MeshTransport;
|
|
|
49
44
|
* flushed on open; malformed inbound frames are dropped.
|
|
50
45
|
*/
|
|
51
46
|
declare function webSocketTransport(url: string, protocols?: string | string[]): MeshTransportFactory;
|
|
47
|
+
type DirectTransportOptions = {
|
|
48
|
+
/**
|
|
49
|
+
* Hand the relay's messages to this peer BY REFERENCE instead of through a JSON hop. A snapshot
|
|
50
|
+
* welcome then costs the peer no copy of the room's registers: its stores hold the relay's own
|
|
51
|
+
* sibling objects, the same ones every other such peer holds. The peer must therefore treat
|
|
52
|
+
* everything it receives as the relay's memory and never mutate it — a write in place would
|
|
53
|
+
* corrupt the room for everyone. The peer's own messages still cross as JSON, so what it writes
|
|
54
|
+
* reaches the relay exactly as a socket peer's would. Off by default; opt in only for a process
|
|
55
|
+
* whose readers are known not to mutate what a seat materializes.
|
|
56
|
+
*/
|
|
57
|
+
readonly byReference?: boolean;
|
|
58
|
+
};
|
|
52
59
|
/**
|
|
53
60
|
* Connects directly to an in-process `createRelay` — no network. The backbone of full-loop
|
|
54
61
|
* tests, and handy for single-process demos or an SSR-side room.
|
|
55
62
|
*
|
|
56
63
|
* Messages cross as JSON in both directions, as they would over a socket, so a direct peer holds
|
|
57
64
|
* exactly what a socket peer would. Handed over by reference, an `undefined` an emitter wrote
|
|
58
|
-
* would reach relay memory where a socket peer's would have been dropped.
|
|
65
|
+
* would reach relay memory where a socket peer's would have been dropped. The relay's own
|
|
66
|
+
* messages may skip the hop ({@link DirectTransportOptions.byReference}): they never hold an
|
|
67
|
+
* `undefined` (every value in a room arrived as JSON), so the two peers read the same state and
|
|
68
|
+
* differ only in who owns the objects.
|
|
59
69
|
*/
|
|
60
|
-
declare function directTransport(relay: Relay, ctx: PrincipalCtx): MeshTransportFactory;
|
|
70
|
+
declare function directTransport(relay: Relay, ctx: PrincipalCtx, options?: DirectTransportOptions): MeshTransportFactory;
|
|
61
71
|
|
|
62
72
|
/**
|
|
63
73
|
* Why a write is a conflict: its replay into a new generation cites something the generation does
|
|
@@ -69,17 +79,50 @@ type ConflictCause = 'uncovered' | 'citation' | 'epoch-bump';
|
|
|
69
79
|
* A write that will never be admitted as it stands. Its local effect is retracted (a replay that
|
|
70
80
|
* failed its pre-check is never applied; a refused write is dropped by the snapshot that follows).
|
|
71
81
|
* The two verbs are the whole surface: `discard()` lets it go, `reauthor()` writes its raw ops again
|
|
72
|
-
* as a new write citing the current state. Neither can submit the original envelope.
|
|
82
|
+
* as a new write citing the current state. Neither can submit the original envelope. A conflict is
|
|
83
|
+
* listed once its verdict is stored, and stays listed across reboots until it is answered.
|
|
73
84
|
*/
|
|
74
85
|
type MeshConflict = {
|
|
75
86
|
/** The envelope as it was authored (its values, cites and epoch). */
|
|
76
87
|
readonly env: OpEnvelope;
|
|
77
88
|
readonly intent: Intent;
|
|
78
89
|
readonly cause: ConflictCause;
|
|
90
|
+
/** Let the write go. Nothing happens once the session has ended. */
|
|
79
91
|
discard(): void;
|
|
80
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Write the raw ops again as a new write; false when the shell cannot write (no `reauthor`).
|
|
94
|
+
* Also false when the conflict's ops cannot be written again as one write (a path twice, or an
|
|
95
|
+
* op under another op's path that is not an earlier set of a plain record); the conflict then
|
|
96
|
+
* stays, so it can still be discarded. Also false once the session has ended (closed or
|
|
97
|
+
* ejected): nothing is written and the conflict stays held. Also false inside
|
|
98
|
+
* {@link MeshSession.recordTogether}, with the same outcome.
|
|
99
|
+
* `true` means the write was emitted, not that it shows: one whose parent is gone by now, or
|
|
100
|
+
* that loses its fold, leaves the store as it was. The answer and the new write are stored
|
|
101
|
+
* together: after a crash either both are there or neither is. A write made before the call is
|
|
102
|
+
* not part of them: it is recorded on its own, first. False when the re-author emitted nothing.
|
|
103
|
+
*/
|
|
81
104
|
reauthor(): boolean;
|
|
82
105
|
};
|
|
106
|
+
/**
|
|
107
|
+
* Why an own write was not stored: `'generation'` (written in a room generation a cut or a lost
|
|
108
|
+
* room creation closed), `'schema'` (a data shape older than the room's), `'order'` (a version below
|
|
109
|
+
* this origin's admitted maximum that was never admitted: a second writer on one origin),
|
|
110
|
+
* `'compacting'` (a host refused it while this client was declared ready for a cut) or
|
|
111
|
+
* `'incompatible'` (a restored write this client can no longer send).
|
|
112
|
+
*/
|
|
113
|
+
type UnstoredReason = 'generation' | 'schema' | 'order' | 'compacting' | 'incompatible';
|
|
114
|
+
/**
|
|
115
|
+
* An own write that is not in the room and never will be, listed until the application dismisses it.
|
|
116
|
+
* It is listed once its verdict is stored, and again after a reboot until its dismissal is stored.
|
|
117
|
+
*/
|
|
118
|
+
type UnstoredWrite = {
|
|
119
|
+
/** The envelope as it was authored: its values, so the application can offer to write them again. */
|
|
120
|
+
readonly env: OpEnvelope;
|
|
121
|
+
readonly intent: Intent;
|
|
122
|
+
readonly reason: UnstoredReason;
|
|
123
|
+
/** Remove the entry; it leaves the list at once. Nothing happens once the session has ended. */
|
|
124
|
+
dismiss(): void;
|
|
125
|
+
};
|
|
83
126
|
/** The room's compaction state, as the relay last announced it. */
|
|
84
127
|
type CompactionState = {
|
|
85
128
|
/** Droppable tombstones counted against the budget. */
|
|
@@ -101,7 +144,11 @@ type MeshDirect = {
|
|
|
101
144
|
readonly data: unknown;
|
|
102
145
|
};
|
|
103
146
|
|
|
104
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* A data channel as the P2P engine needs it; it opens later. Peer links send only on an open
|
|
149
|
+
* channel, so the contract asks for no buffering, but the channels of {@link rtcPeerConnector}
|
|
150
|
+
* buffer sends made before open and flush them when the channel opens.
|
|
151
|
+
*/
|
|
105
152
|
type DataChannelLike = {
|
|
106
153
|
send(frame: string): void;
|
|
107
154
|
onMessage(cb: (frame: string) => void): () => void;
|
|
@@ -279,25 +326,37 @@ type MeshPeer = PresenceState;
|
|
|
279
326
|
/**
|
|
280
327
|
* A composed, user-facing health status for a synced store the surface that
|
|
281
328
|
* turns a versioned reject from a dead socket into a speakable 'outdated' banner.
|
|
329
|
+
* `'storage'`: the outbox can no longer save this handle's writes, and the session has ended and
|
|
330
|
+
* sends nothing more. The reason says why: `'moved'` (another tab wrote the outbox in its place),
|
|
331
|
+
* `'exhausted'` (the outbox ran out of sequence numbers) or `'storage'` (a save failed, or the
|
|
332
|
+
* outcome of a write is not known: it is reported neither as saved nor as lost). A first read of
|
|
333
|
+
* the outbox that fails does not end the handle; see `outboxUnreadable`.
|
|
282
334
|
*/
|
|
283
|
-
type SyncHealthStatus = 'live' | 'waiting' | 'offline' | 'outdated' | 'ejected' | 'degraded';
|
|
335
|
+
type SyncHealthStatus = 'live' | 'waiting' | 'offline' | 'outdated' | 'ejected' | 'degraded' | 'storage';
|
|
284
336
|
type SyncHealth = {
|
|
285
337
|
readonly status: SyncHealthStatus;
|
|
286
|
-
/** Why, when the status is `outdated`/`ejected`/`degraded`. */
|
|
287
|
-
readonly reason?: 'proto' | 'policy-version' | 'schema' | 'quota' | 'worker' | (string & {});
|
|
338
|
+
/** Why, when the status is `outdated`/`ejected`/`degraded`/`storage`. */
|
|
339
|
+
readonly reason?: 'proto' | 'policy-version' | 'schema' | 'quota' | 'worker' | 'storage' | 'moved' | 'exhausted' | (string & {});
|
|
288
340
|
/** `Date.now()` of the last successful sync (welcome or applied env). */
|
|
289
341
|
readonly lastSyncedAt?: number;
|
|
290
342
|
/**
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
|
|
343
|
+
* How many groups of this handle's writes are in storage's hands and not yet saved. Nothing times
|
|
344
|
+
* out: a store that never answers keeps it above zero until the handle closes.
|
|
345
|
+
*/
|
|
346
|
+
readonly storing: number;
|
|
347
|
+
/**
|
|
348
|
+
* Saved offline writes this boot cannot send: written under another writer, protocol version or
|
|
349
|
+
* policy version, malformed, or rejected by the room policy. They are never sent; each is listed
|
|
350
|
+
* in `unstored` with the reason `'incompatible'`.
|
|
294
351
|
*/
|
|
295
352
|
readonly droppedOfflineWrites?: number;
|
|
296
353
|
/**
|
|
297
|
-
*
|
|
298
|
-
*
|
|
354
|
+
* The saved outbox could not be read: it is in a format this build cannot read, or the store
|
|
355
|
+
* failed the read. It is left as it is, and this boot's writes are kept in memory only, under an
|
|
356
|
+
* origin of their own, until the handle closes. The handle runs live over memory, and its
|
|
357
|
+
* health does not turn `'storage'` for this.
|
|
299
358
|
*/
|
|
300
|
-
readonly
|
|
359
|
+
readonly outboxUnreadable?: true;
|
|
301
360
|
/**
|
|
302
361
|
* Received envelopes rejected as malformed by the deterministic well-formedness check (a bad id
|
|
303
362
|
* or path segment, a non-integer version, an unknown op kind, a negative epoch, forged cites, a
|
|
@@ -338,14 +397,36 @@ type MeshSyncOptions = {
|
|
|
338
397
|
readonly reconnect?: {
|
|
339
398
|
readonly maxDelayMs?: number;
|
|
340
399
|
};
|
|
400
|
+
/**
|
|
401
|
+
* How long a hello may go unanswered before the connection is closed and opened again:
|
|
402
|
+
* `timeoutMs` (default 15 000), doubled for every hello in a row that went unanswered, up to
|
|
403
|
+
* `maxTimeoutMs` (default 120 000). An answered hello starts the count again. A hello that goes
|
|
404
|
+
* unanswered also counts as a failed attempt for the reconnect backoff. A relay that never
|
|
405
|
+
* answers a hello within `maxTimeoutMs` leaves the client resetting the connection at that pace
|
|
406
|
+
* for as long as it lasts: nothing is lost, and nothing gets through.
|
|
407
|
+
*/
|
|
408
|
+
readonly handshake?: {
|
|
409
|
+
readonly timeoutMs?: number;
|
|
410
|
+
readonly maxTimeoutMs?: number;
|
|
411
|
+
};
|
|
341
412
|
readonly injector?: Injector;
|
|
342
413
|
/** Register with the nearest transition scope so (re)connection surfaces as `pending`. */
|
|
343
414
|
readonly register?: 'track' | 'suspend';
|
|
415
|
+
/**
|
|
416
|
+
* Called once when the handle ends by itself (`ejected`, with the reason) and when a
|
|
417
|
+
* `clearOutbox()` ends it (`'cleared'`); never for `close()`. A callback that throws is reported
|
|
418
|
+
* and changes nothing.
|
|
419
|
+
*/
|
|
344
420
|
readonly onEject?: (reason: string) => void;
|
|
345
|
-
/**
|
|
421
|
+
/**
|
|
422
|
+
* The connection dropped and a reconnect is scheduled; `code` is the close code (1009 = message
|
|
423
|
+
* too big, 1006 = dropped). The client also closes a connection itself, with code 1000 and the
|
|
424
|
+
* reason `'timeout'` (a hello went unanswered), `'displaced'` (a later connection of this client
|
|
425
|
+
* took its place) or `'counter'` (the connection used up its request ids). A transport factory or
|
|
426
|
+
* a `send` that throws is reported and loses the connection, with code 1006 and the reason
|
|
427
|
+
* `'transport'` or `'send'`.
|
|
428
|
+
*/
|
|
346
429
|
readonly onDisconnect?: (code: number, reason: string) => void;
|
|
347
|
-
/** A write the relay refused (see `MeshSessionHooks.onRefused`); the envelope carries its values. */
|
|
348
|
-
readonly onRefused?: (env: OpEnvelope, reason: 'generation' | 'schema' | 'order' | 'compacting') => void;
|
|
349
430
|
/**
|
|
350
431
|
* Hold the connection until the local base is assembled. `meshSync` awaits this before it connects
|
|
351
432
|
* (and before it restores an `outbox`), so a store hydrated from another source first (a worker
|
|
@@ -355,35 +436,50 @@ type MeshSyncOptions = {
|
|
|
355
436
|
*/
|
|
356
437
|
readonly whenReady?: () => PromiseLike<void> | void;
|
|
357
438
|
/**
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
439
|
+
* Keep this client's own writes in `store` under `key`, so offline writes survive a reboot, not
|
|
440
|
+
* just a live reconnect. Every write is saved before it is sent, and every answer about it is
|
|
441
|
+
* saved before it shows, so a crash at any moment loses nothing that was sent. On boot the saved
|
|
442
|
+
* writes that are still pending are restored and sent again, unchanged, after the next welcome.
|
|
443
|
+
* The saved outbox also keeps this client's origin, so every boot that reads it continues as the
|
|
444
|
+
* same client. Without an outbox, writes are kept in memory only and each boot has an origin of
|
|
445
|
+
* its own.
|
|
446
|
+
*
|
|
447
|
+
* The store is a `JournalStore` (build one over idb-keyval's shape with `journalStore`). Two
|
|
448
|
+
* keys are written, `<key>:log` and `<key>:snap`. A value saved under `key` itself by an earlier
|
|
449
|
+
* version of this library is deleted unread at each boot, when the store can delete.
|
|
362
450
|
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
451
|
+
* Storage decides between tabs: when another tab has written the outbox in this handle's place,
|
|
452
|
+
* this handle ends `ejected` with the reason `'moved'`. A write it had not saved by then is not
|
|
453
|
+
* saved, and no other tab can send it: `acked` reads `false` and `whenAcked()` rejects, and the
|
|
454
|
+
* application decides what to do with it. A Web Lock on `key` keeps live tabs from taking each
|
|
455
|
+
* other's place; `crossTab` sets what a tab that did not get the lock does. Within one page, a handle
|
|
456
|
+
* made in the turn another handle over the same key was closed takes over from it (unless another
|
|
457
|
+
* tab asked for the lock first). A handle made while another handle of this page over the
|
|
458
|
+
* same key is still open is treated as a second tab would be: `'queue'` waits for it, `'ephemeral'`
|
|
459
|
+
* runs over memory for its life. So close the old handle first. Two copies of this library in one
|
|
460
|
+
* page behave as two tabs. Store writes made while the boot
|
|
461
|
+
* waits (for the lock or the read) are held in memory and sent, as their net change, when the tab
|
|
462
|
+
* boots. Nothing times out: a store call that never answers keeps the handle live and holding the
|
|
463
|
+
* lock, and `health().storing` above zero, until it closes.
|
|
372
464
|
*/
|
|
373
465
|
readonly outbox?: {
|
|
374
466
|
readonly key: string;
|
|
375
|
-
readonly store:
|
|
376
|
-
/** Coalesce outbox writes by this many ms (default 300; `0` = write on every change). */
|
|
377
|
-
readonly debounceMs?: number;
|
|
467
|
+
readonly store: JournalStore;
|
|
378
468
|
/**
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
469
|
+
* What a tab does when another tab holds the lock for `key`.
|
|
470
|
+
* `'queue'` (default): it WAITS (`status` reads `'waiting'`) and takes over when the owner
|
|
471
|
+
* closes; store writes made while it waits are sent, as their net change, when it takes over.
|
|
472
|
+
* `'ephemeral'`: it runs live at once, as a client of its own, and never touches the saved
|
|
473
|
+
* outbox: its writes live in memory only and are lost if that tab ends before the room
|
|
474
|
+
* acknowledges them. It stays ephemeral for life, also after the owner leaves. When the lock is
|
|
475
|
+
* free both behave the same (take it, restore, save).
|
|
476
|
+
* A `'queue'` handle closed while it waits for another tab keeps its place in the browser's lock
|
|
477
|
+
* queue until its turn; then the lock is taken and given back at once. Until then a new handle
|
|
478
|
+
* of this page is treated as it would be while that other tab holds the lock.
|
|
479
|
+
* `'off'` skips the lock: every tab that opens the key continues the same saved client, and
|
|
480
|
+
* when two tabs write it, one of them ends `ejected` with the reason `'moved'`. Without the
|
|
481
|
+
* lock a tab that has only read the outbox does not keep another tab's `clearOutbox()` from
|
|
482
|
+
* wiping what it is about to send. In development mode it warns once.
|
|
387
483
|
*/
|
|
388
484
|
readonly crossTab?: 'queue' | 'off' | 'ephemeral';
|
|
389
485
|
};
|
|
@@ -400,7 +496,10 @@ type MeshSyncOptions = {
|
|
|
400
496
|
type SeatSync<T = unknown> = Pick<OpSync<T>, 'override' | 'captureFrontier' | 'commitScope' | 'liveUnder' | 'liveAt' | 'appliedFrontier'>;
|
|
401
497
|
type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
402
498
|
readonly status: Signal<MeshStatus>;
|
|
403
|
-
/**
|
|
499
|
+
/**
|
|
500
|
+
* This client's origin: the saved outbox's, the same on every boot that reads it, or one of its
|
|
501
|
+
* own for a boot without a saved outbox. Null until the outbox has loaded.
|
|
502
|
+
*/
|
|
404
503
|
readonly origin: Signal<string | null>;
|
|
405
504
|
/** Other relay members, including peers with no presence; empty before welcome and while disconnected. */
|
|
406
505
|
readonly members: Signal<readonly string[]>;
|
|
@@ -409,16 +508,36 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
409
508
|
/** Composed sync-health for a user-facing surface. */
|
|
410
509
|
readonly health: Signal<SyncHealth>;
|
|
411
510
|
/**
|
|
412
|
-
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
511
|
+
* Everything written through this handle is saved as the room's: nothing is pending or still
|
|
512
|
+
* being saved and no conflict is held. The signal follows the sync engine, which takes a store
|
|
513
|
+
* write up a moment later, at its next tick: read in the same turn as a write, it still gives
|
|
514
|
+
* the earlier answer. `whenAcked()` counts a write made in this turn, and from that call this
|
|
515
|
+
* signal counts it too. To ask whether everything is saved, including what was just written,
|
|
516
|
+
* call `whenAcked()`. `false` until the outbox has
|
|
517
|
+
* loaded (what is pending is not known before), while a write waits for the room or for storage,
|
|
518
|
+
* while a conflict is held (until it is discarded or written again), and for good once a write
|
|
519
|
+
* can no longer land: a write after an eject, or a write the emit-side policy
|
|
520
|
+
* rejected. After an eject it is `true` exactly when nothing was outstanding at the ejection
|
|
521
|
+
* and nothing was written since. A close freezes it: it keeps the value it had at the close,
|
|
522
|
+
* and a write after the close does not change it. After a storage failure or `clearOutbox()` it
|
|
523
|
+
* is `false`. Writes inside `whenReady` are the local base, not writes of their own. Writes made
|
|
524
|
+
* while the boot is held (waiting for the outbox lock or its read) are saved and sent, as their
|
|
525
|
+
* net change, when the boot ends; each `sync.override` made in the hold is its own authority
|
|
526
|
+
* write, in order. A write the relay refused is listed in `unstored`, not here.
|
|
415
527
|
*/
|
|
416
528
|
readonly acked: Signal<boolean>;
|
|
417
529
|
/**
|
|
418
|
-
* Resolves
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
530
|
+
* Resolves once nothing written through this handle is outstanding and that is saved
|
|
531
|
+
* (immediately if it already is). Before the outbox has loaded it waits for the load. Rejects
|
|
532
|
+
* with the terminal reason if the session ends while writes are outstanding or a write can no
|
|
533
|
+
* longer land (`'closed'` for writes held in a boot that is closed before it connects), with
|
|
534
|
+
* `'storage'`, `'moved'` or `'exhausted'` once the outbox can no longer save, with `'cleared'`
|
|
535
|
+
* after `clearOutbox()`, and with `'schema'` when this client's data shape is older than the
|
|
536
|
+
* room's. While a conflict is held it rejects at once with `conflict: <cause>`; a terminal reason
|
|
537
|
+
* wins. A write the relay refused rejects the calls pending when its answer is saved with
|
|
538
|
+
* `refused: <reason>`; a later call does not wait for it. The barrier to await before publishing
|
|
539
|
+
* something that must contain every edit; `synced()` is a connection indicator, not a save
|
|
540
|
+
* barrier.
|
|
422
541
|
*/
|
|
423
542
|
whenAcked(): Promise<void>;
|
|
424
543
|
readonly peers: Signal<readonly MeshPeer[]>;
|
|
@@ -432,6 +551,7 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
432
551
|
/**
|
|
433
552
|
* Listen for `direct` frames addressed to this client; returns the unsubscribe. A frame that
|
|
434
553
|
* arrives with no listener is lost. Peer links on {@link MeshSyncRef.signaling} never see them.
|
|
554
|
+
* A listener that throws is reported, and the others still hear the frame.
|
|
435
555
|
*/
|
|
436
556
|
direct(cb: (msg: MeshDirect) => void): () => void;
|
|
437
557
|
/**
|
|
@@ -439,23 +559,40 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
439
559
|
* fork observes the room as it is now; committing emits its diff citing only those observed
|
|
440
560
|
* writes, so an edit that lands on the room while the fork is open stays a concurrent value the
|
|
441
561
|
* merge policy decides rather than being overwritten by the commit. Call `rebase()` to re-observe
|
|
442
|
-
* the room and commit on top of the latest instead. `discard()` drops the staged edits
|
|
562
|
+
* the room and commit on top of the latest instead. `discard()` drops the staged edits and also
|
|
563
|
+
* re-observes the room, so a later commit on the same fork cites the room as it is at the discard.
|
|
564
|
+
* Before the boot nothing has been observed, so a fork commit made then is a plain held write.
|
|
443
565
|
*/
|
|
444
566
|
fork(opt?: ForkStoreOptions<T & Record<string, any>>): SyncedFork<T & Record<string, any>>;
|
|
445
|
-
/**
|
|
567
|
+
/**
|
|
568
|
+
* Authority surface over this handle's op-sync; see {@link SeatSync}. It is one object for the
|
|
569
|
+
* handle's life: taken before the boot, it works over the engine once the outbox has loaded.
|
|
570
|
+
* When the callback of `override` throws, the error reaches the caller and the writes it made are
|
|
571
|
+
* still sent with the override's authority, also while the boot is held. Before the boot nothing
|
|
572
|
+
* has been observed, so a `commitScope` run then is a plain held write.
|
|
573
|
+
*/
|
|
446
574
|
readonly sync: SeatSync<T>;
|
|
447
575
|
/**
|
|
448
576
|
* Writes that will never be admitted as they stand: a write a compaction cut could not replay
|
|
449
577
|
* (it cites something the cut folded away) or one the relay refused. Each offers `discard()` or
|
|
450
|
-
* `reauthor()` (write its values again, citing the current state).
|
|
578
|
+
* `reauthor()` (write its values again, citing the current state). Saved with the outbox: a
|
|
579
|
+
* conflict stays listed across reboots until it is answered.
|
|
451
580
|
*/
|
|
452
581
|
readonly conflicts: Signal<readonly MeshConflict[]>;
|
|
582
|
+
/**
|
|
583
|
+
* Own writes that are not in the room and never will be, in the order they were made: refused by
|
|
584
|
+
* the relay, discarded with a room generation, or saved offline and no longer sendable by this
|
|
585
|
+
* boot (`'incompatible'`). Each entry holds the write, so it can be offered again, its reason, and
|
|
586
|
+
* `dismiss()`, which removes it. An entry is listed once its answer is saved and stays listed
|
|
587
|
+
* across reboots until its dismissal is saved. Empty before the outbox has loaded.
|
|
588
|
+
*/
|
|
589
|
+
readonly unstored: Signal<readonly UnstoredWrite[]>;
|
|
453
590
|
/** The room's compaction state as the relay last announced it; undefined before any notice. */
|
|
454
591
|
readonly compaction: Signal<CompactionState | undefined>;
|
|
455
592
|
/**
|
|
456
593
|
* The room generation this client installed: set by every snapshot welcome of a new generation
|
|
457
594
|
* (a cut's welcome flips it), untouched by one of the installed generation. Undefined until the
|
|
458
|
-
* first snapshot welcome
|
|
595
|
+
* first snapshot welcome of this boot.
|
|
459
596
|
*/
|
|
460
597
|
readonly generation: Signal<GenerationState | undefined>;
|
|
461
598
|
/**
|
|
@@ -464,6 +601,55 @@ type MeshSyncRef<T extends object = Record<string, unknown>> = {
|
|
|
464
601
|
* reconnect or a new generation releases it.
|
|
465
602
|
*/
|
|
466
603
|
declareReady(pressed: boolean): void;
|
|
604
|
+
/**
|
|
605
|
+
* Discard every write not yet in the room, every conflict and every `unstored` entry, and end the
|
|
606
|
+
* session. From the call nothing more is sent or saved, `acked` reads `false` and `whenAcked()`
|
|
607
|
+
* rejects with `'cleared'`; the handle keeps the outbox lock and its status until storage answers.
|
|
608
|
+
* The session ends `closed` with the reason `'cleared'` once storage holds the wipe, and the
|
|
609
|
+
* promise resolves then. It rejects with `'moved'` when another tab wrote the outbox first, with
|
|
610
|
+
* `'storage'` when the outcome is not known, and with the reason of any ending that comes first
|
|
611
|
+
* (`'closed'` for `close()`). A crash before it resolves leaves the outbox as it was, and the next
|
|
612
|
+
* boot continues it and sends what is pending. Called before the boot has finished, it clears
|
|
613
|
+
* once the boot is done, or rejects with the boot's reason.
|
|
614
|
+
*
|
|
615
|
+
* A handle runs over memory when it was given no `outbox`, when it is an `'ephemeral'` tab beside
|
|
616
|
+
* another tab that holds the outbox, or when the saved outbox could not be read. Called while
|
|
617
|
+
* such a handle is live, the clear discards this handle's own writes, resolves, and ends the
|
|
618
|
+
* handle `closed` with `'cleared'`, calling `onEject('cleared')`. A saved outbox the handle never
|
|
619
|
+
* held is left exactly as it is, and the next boot that can read it sends what it holds. An
|
|
620
|
+
* outbox that could not be read is not removed by `clearOutbox()`: the application removes it
|
|
621
|
+
* from its own store, under the key it gave, when it wants it gone.
|
|
622
|
+
*
|
|
623
|
+
* Called once the session has ended by itself (ejected, outdated, a storage ending), it takes the
|
|
624
|
+
* outbox lock if it is free (else it rejects with `'busy'`), reads the outbox again and wipes it
|
|
625
|
+
* only when no other tab has written it since this handle ended (else it rejects with `'moved'`);
|
|
626
|
+
* it rejects with `'unreadable'` when the outbox cannot be read again, and with `'storage'` when
|
|
627
|
+
* the store does not say whether the wipe was stored. It
|
|
628
|
+
* holds the lock until storage answers, and `close()` meanwhile rejects it with `'closed'`.
|
|
629
|
+
* After `close()`, and for a handle that never held the saved outbox, it rejects with
|
|
630
|
+
* `'closed'`. A second call while one runs joins it.
|
|
631
|
+
*/
|
|
632
|
+
clearOutbox(): Promise<void>;
|
|
633
|
+
/**
|
|
634
|
+
* Leave the room, release the outbox lock and destroy the op engine. A write made in the same
|
|
635
|
+
* tick is handed to storage, before the lock is released, only when nothing of this handle is in
|
|
636
|
+
* storage's hands: no earlier write still being saved, and not the record the handle saves as it
|
|
637
|
+
* goes live, which storage holds for a moment as the status turns `'live'`. A write queued behind
|
|
638
|
+
* anything still out is not saved. Either way nobody is told it is stored: `acked` reads `false`
|
|
639
|
+
* and `whenAcked()` rejects with `'closed'`. To know that every write is saved, wait for
|
|
640
|
+
* `whenAcked()`, or for `acked` to read `true`, before closing.
|
|
641
|
+
* A save in storage's hands may still land after the lock is released, and storage decides
|
|
642
|
+
* between it and the next tab's writes.
|
|
643
|
+
* Closed while the boot is still held, the writes made in the hold are dropped: they were never
|
|
644
|
+
* saved, and with a net change `acked` reads `false` and `whenAcked()` rejects with `'closed'`.
|
|
645
|
+
* Without one, an outbox read in flight is let finish, writing nothing, and both answer from it:
|
|
646
|
+
* nothing pending and no conflict held reads `true` and resolves; anything else, or an outbox
|
|
647
|
+
* that cannot be read, reads `false` and rejects with `'closed'`. Until then `acked` is `false`
|
|
648
|
+
* and `whenAcked()` waits.
|
|
649
|
+
* The handle is frozen from then on: the store keeps working locally, but nothing is sent or
|
|
650
|
+
* saved, and `acked`, `conflicts`, `unstored` and `generation` keep the value they had at close.
|
|
651
|
+
* Destroying the injector closes it the same way.
|
|
652
|
+
*/
|
|
467
653
|
close(): void;
|
|
468
654
|
};
|
|
469
655
|
/**
|
|
@@ -481,7 +667,7 @@ type SeatChange = {
|
|
|
481
667
|
/** The relay's total-order stamp. Strictly increasing across `change` events between resyncs. */
|
|
482
668
|
readonly seq: number;
|
|
483
669
|
readonly writer: string;
|
|
484
|
-
readonly ops: readonly StoreOp
|
|
670
|
+
readonly ops: readonly StoreOp[];
|
|
485
671
|
};
|
|
486
672
|
/**
|
|
487
673
|
* Seq continuity broke: the seat re-established state from a room snapshot (a reconnect past
|
|
@@ -529,14 +715,30 @@ type AgentSeatOptions = {
|
|
|
529
715
|
readonly reconnect?: {
|
|
530
716
|
readonly maxDelayMs?: number;
|
|
531
717
|
};
|
|
718
|
+
/**
|
|
719
|
+
* How long a hello may go unanswered before the connection is closed and opened again:
|
|
720
|
+
* `timeoutMs` (default 15 000), doubled for every hello in a row that went unanswered, up to
|
|
721
|
+
* `maxTimeoutMs` (default 120 000). An answered hello starts the count again. A relay that never
|
|
722
|
+
* answers a hello within `maxTimeoutMs` leaves the seat resetting the connection at that pace for
|
|
723
|
+
* as long as it lasts: nothing is lost, and nothing gets through.
|
|
724
|
+
*/
|
|
725
|
+
readonly handshake?: {
|
|
726
|
+
readonly timeoutMs?: number;
|
|
727
|
+
readonly maxTimeoutMs?: number;
|
|
728
|
+
};
|
|
532
729
|
/**
|
|
533
730
|
* Store context for the replica (from `createStoreContext()`). Defaults to a fresh one;
|
|
534
731
|
* pass your own to share proxy caches across several seats in one process.
|
|
535
732
|
*/
|
|
536
733
|
readonly context?: toStoreOptions;
|
|
734
|
+
/**
|
|
735
|
+
* Called once when the seat ends by itself (`ejected`, with the reason, an outdated data shape
|
|
736
|
+
* included) and when a `clearOutbox()` ends it (`'cleared'`); never for `close()`. It is called
|
|
737
|
+
* last: the seat has ended and every clear it owed has settled, so inside it `status()`,
|
|
738
|
+
* `peers()`, `acked()` and `whenAcked()` answer as they do after it (`'ejected'`, or `'closed'`
|
|
739
|
+
* after a clear). A callback that throws is reported and changes nothing.
|
|
740
|
+
*/
|
|
537
741
|
readonly onEject?: (reason: string) => void;
|
|
538
|
-
/** A write the relay refused; it is not in the room, and the doc was rehydrated. The envelope carries its values. */
|
|
539
|
-
readonly onRefused?: (env: OpEnvelope, reason: 'generation' | 'schema' | 'order' | 'compacting') => void;
|
|
540
742
|
};
|
|
541
743
|
type AgentSeat<T extends object> = {
|
|
542
744
|
/** The live converging replica. Reads exactly like a local signal store. */
|
|
@@ -546,8 +748,11 @@ type AgentSeat<T extends object> = {
|
|
|
546
748
|
/** The current document value — plain JSON-serializable data, for context assembly. */
|
|
547
749
|
snapshot(): T;
|
|
548
750
|
/**
|
|
549
|
-
* The current document, stamped with the relay seq it is provably the pure fold of
|
|
550
|
-
*
|
|
751
|
+
* The current document, stamped with the relay seq it is provably the pure fold of, or null
|
|
752
|
+
* when that cannot be guaranteed: before the first welcome, while one of the seat's own writes
|
|
753
|
+
* is still unacknowledged, and from a refused write until the snapshot that replaces it lands.
|
|
754
|
+
* It stays null for good once the seat ended with writes outstanding, after an emit-side policy
|
|
755
|
+
* reject, and after `close()`.
|
|
551
756
|
*/
|
|
552
757
|
stableSnapshot(): StableSnapshot<T> | null;
|
|
553
758
|
/** Run local writes and emit them to the room as one envelope, like any peer's edit. */
|
|
@@ -557,6 +762,12 @@ type AgentSeat<T extends object> = {
|
|
|
557
762
|
* carry, or a relay refusal), each with `discard()` and `reauthor()`.
|
|
558
763
|
*/
|
|
559
764
|
conflicts(): readonly MeshConflict[];
|
|
765
|
+
/**
|
|
766
|
+
* The seat's writes that are not in the room and never will be, in the order they were made:
|
|
767
|
+
* refused by the relay or discarded with a room generation. Each entry holds the write, its
|
|
768
|
+
* reason and `dismiss()`, which removes it.
|
|
769
|
+
*/
|
|
770
|
+
readonly unstored: Signal<readonly UnstoredWrite[]>;
|
|
560
771
|
/** The room's compaction state as the relay last announced it; undefined before any notice. */
|
|
561
772
|
readonly compaction: Signal<CompactionState | undefined>;
|
|
562
773
|
/**
|
|
@@ -580,20 +791,43 @@ type AgentSeat<T extends object> = {
|
|
|
580
791
|
* `commit()` emits its diff citing only those observed writes, so a room edit that lands
|
|
581
792
|
* while the fork is open survives as a concurrent value for the merge policy to resolve
|
|
582
793
|
* instead of being overwritten. `rebase()` re-observes the room; `discard()` drops the
|
|
583
|
-
* staged edits
|
|
794
|
+
* staged edits and also re-observes the room, so a later commit on the same fork cites the
|
|
795
|
+
* room as it is at the discard.
|
|
584
796
|
*/
|
|
585
797
|
fork(opt?: ForkStoreOptions<T & Record<string, any>>): SyncedFork<T & Record<string, any>>;
|
|
586
798
|
/**
|
|
587
|
-
*
|
|
588
|
-
*
|
|
799
|
+
* Everything written through this seat is the room's: nothing is pending and no conflict is
|
|
800
|
+
* held. It counts a write made in the same turn as the call. `false` until the seat has started
|
|
801
|
+
* (a moment after it is created),
|
|
802
|
+
* while a write waits for the room, while a conflict is held (until it is discarded or written
|
|
803
|
+
* again), and for good once a write can no longer land: a write after an eject,
|
|
804
|
+
* or a write the emit-side policy rejected. After an eject it is `true` exactly when nothing was
|
|
805
|
+
* outstanding at the ejection and nothing was written since. A close freezes it: it keeps the
|
|
806
|
+
* answer it had at the close, and a write after the close does not change it. After a storage
|
|
807
|
+
* failure or `clearOutbox()` it is `false`. A write the relay refused is listed in `unstored`,
|
|
808
|
+
* not here.
|
|
589
809
|
*/
|
|
590
810
|
acked(): boolean;
|
|
591
811
|
/**
|
|
592
|
-
* Resolves
|
|
593
|
-
*
|
|
594
|
-
*
|
|
812
|
+
* Resolves once nothing written through this seat is outstanding (immediately if it already is;
|
|
813
|
+
* before the seat has started it waits for the start). Rejects with the terminal reason if the
|
|
814
|
+
* seat ends while writes are outstanding or a write can no longer land, and with `'cleared'`
|
|
815
|
+
* after `clearOutbox()`. While a conflict is held it rejects at once with `conflict: <cause>`; a
|
|
816
|
+
* terminal reason wins. A write the relay refused rejects the calls pending when it is answered
|
|
817
|
+
* with `refused: <reason>`. The barrier to await before handing a result to something that must
|
|
818
|
+
* contain every edit.
|
|
595
819
|
*/
|
|
596
820
|
whenAcked(): Promise<void>;
|
|
821
|
+
/**
|
|
822
|
+
* Discard every write not yet in the room, every conflict and every `unstored` entry, and end the
|
|
823
|
+
* seat: nothing more is sent, the status reads `'closed'`, and `whenAcked()` rejects with
|
|
824
|
+
* `'cleared'`. Called before the seat has started, it clears once it has; when the start fails it
|
|
825
|
+
* rejects with that reason, then and on every later call. After `close()` it rejects with
|
|
826
|
+
* `'closed'`. After an eject or an outdated ending the seat's journal is already closed, as at
|
|
827
|
+
* every ending, and a seat keeps nothing on storage: the call marks the seat cleared (`acked()`
|
|
828
|
+
* reads `false`, `whenAcked()` rejects with `'cleared'`) and resolves at once.
|
|
829
|
+
*/
|
|
830
|
+
clearOutbox(): Promise<void>;
|
|
597
831
|
/** Publish this seat's ephemeral presence payload (e.g. `{ name, kind: 'agent' }`). */
|
|
598
832
|
setPresence(data: unknown): void;
|
|
599
833
|
/**
|
|
@@ -608,6 +842,13 @@ type AgentSeat<T extends object> = {
|
|
|
608
842
|
direct(cb: (msg: MeshDirect) => void): () => void;
|
|
609
843
|
/** Authority surface over this seat's op-sync; see {@link SeatSync}. */
|
|
610
844
|
readonly sync: SeatSync<T>;
|
|
845
|
+
/**
|
|
846
|
+
* Leave the room and destroy the seat's op engine. The handle is frozen from then on: `doc`
|
|
847
|
+
* still reads and writes locally, but nothing is sent, `acked()` and `whenAcked()` keep the
|
|
848
|
+
* answer they had at close, and `stableSnapshot()` is null. Closed before the seat has started,
|
|
849
|
+
* they answer once its journal has been read: with nothing written, `acked()` reads `true` and
|
|
850
|
+
* `whenAcked()` resolves; until then `acked()` is `false` and `whenAcked()` waits.
|
|
851
|
+
*/
|
|
611
852
|
close(): void;
|
|
612
853
|
};
|
|
613
854
|
/**
|
|
@@ -634,7 +875,7 @@ declare function setAtPath(node: unknown, path: string | readonly PathSegment[],
|
|
|
634
875
|
* activity feeds and model context. Values render as truncated JSON. Pair with a seat's
|
|
635
876
|
* `changes` stream: narrating each batch's ops yields an append-only account of the room.
|
|
636
877
|
*/
|
|
637
|
-
declare function describeOp(op: StoreOp
|
|
878
|
+
declare function describeOp(op: StoreOp, writer: string): string;
|
|
638
879
|
|
|
639
880
|
type WebRtcMeshOptions = {
|
|
640
881
|
readonly room: string;
|
|
@@ -673,8 +914,8 @@ type WebRtcMeshRef = {
|
|
|
673
914
|
* Peer-to-peer mesh sync: the relay only signals
|
|
674
915
|
* and tracks membership; envelopes flow over WebRTC data channels and converge via the
|
|
675
916
|
* per-path register map. Catch-up is pairwise: on channel open both sides exchange
|
|
676
|
-
* watermarks; a side whose state is
|
|
677
|
-
* checkpoint (root + per-path register state + watermark), so supersession and precedence
|
|
917
|
+
* watermarks; a side whose state is covered (the other holds every envelope it holds)
|
|
918
|
+
* hydrates from the other's FULL checkpoint (root + per-path register state + watermark), so supersession and precedence
|
|
678
919
|
* carry over intact: a late joiner is indistinguishable from a peer that saw every
|
|
679
920
|
* envelope. Two peers that each hold envelopes the other lacks keep their convergent
|
|
680
921
|
* go-forward guarantees but do not exchange the missed envelopes retroactively.
|
|
@@ -750,4 +991,4 @@ type RtcPresenceRef<T> = {
|
|
|
750
991
|
declare function rtcPresence<T>(links: PeerLinks, opt: RtcPresenceOptions): RtcPresenceRef<T>;
|
|
751
992
|
|
|
752
993
|
export { agentSeat, defaultPeerChannels, describeOp, directTransport, meshSync, peerLinks, presenceChannel, rtcPeerConnector, rtcPresence, setAtPath, webRtcMesh, webSocketTransport };
|
|
753
|
-
export type { AgentSeat, AgentSeatOptions, DataChannelLike, MeshDirect, MeshPeer, MeshStatus, MeshSyncOptions, MeshSyncRef, MeshTransport, MeshTransportFactory, PeerChannelSpec, PeerConnector, PeerLinks, PeerLinksOptions, PresenceFrame, RtcPresenceOptions, RtcPresenceRef, SeatChange, SeatEvent, SeatResync, SeatSync, SignalingPort, StableSnapshot, SyncHealth, SyncHealthStatus, WebRtcMeshOptions, WebRtcMeshRef };
|
|
994
|
+
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 };
|