@mmstack/mesh 22.9.1 → 22.10.1

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 CHANGED
@@ -41,6 +41,33 @@ delta when possible, and re-applies any writes made while offline on top of what
41
41
  moved to in the meantime. A relay restart is detected through a room instance nonce, so a stale
42
42
  sequence number never corrupts state.
43
43
 
44
+ `mesh.origin()` is `null` until the session sends hello, then reflects the origin that hello
45
+ used. It stays stable across socket reconnects and follows the current op engine when a session
46
+ is rebuilt; an outbox restore publishes the new boot's origin, not the origins of replayed writes.
47
+ The last announced origin remains available after close. `mesh.members()` lists other relay
48
+ origins, including peers that have never published presence. It is empty before welcome and
49
+ while disconnected, and updates on welcomes, joins, and departures.
50
+
51
+ Peer links can share the session's connection directly, without wrapping its transport:
52
+
53
+ ```ts
54
+ const origin = mesh.origin();
55
+ if (origin !== null && mesh.status() === 'live') {
56
+ const links = peerLinks({
57
+ room: 'board-42',
58
+ origin,
59
+ signaling: mesh.signaling,
60
+ });
61
+ }
62
+ ```
63
+
64
+ Create links in an injection context (or pass their `injector` option). In a reactive consumer,
65
+ create them once per origin and close the previous links if that origin changes. The signaling
66
+ port follows socket reconnects without sending a second hello. It buffers up to 256 early
67
+ signaling frames for its first listener, clears stale frames on welcome or disconnect, and
68
+ retires peer links only when the session ends: a socket drop leaves them up, and the next welcome
69
+ says who is still there. Closing the links leaves the mesh session open.
70
+
44
71
  ## Conflict resolution
45
72
 
46
73
  By default the latest write to a path wins, decided by a hybrid logical clock so every peer
@@ -124,21 +151,24 @@ meshSync(board, {
124
151
  ```
125
152
 
126
153
  `store` is any `AsyncStore` (`get`, `set`, `del`), the same interface `persist` takes. On boot the
127
- client restores the saved queue, adopts the origin it used before the reload, and resends the
128
- unacknowledged writes when it reconnects. Those offline edits then rebase onto whatever the room
154
+ client restores the saved queue and resends the unacknowledged writes when it reconnects, each under
155
+ the origin it was recorded with. New writes go out under a fresh origin: every boot mints its own. Those offline edits then rebase onto whatever the room
129
156
  moved to while the tab was gone. The queue is saved on a 300ms debounce; set `debounceMs` to change
130
157
  it, or `0` to write on every change.
131
158
 
132
- One origin is driven by one tab at a time. `crossTab` sets what a second tab on the same key does:
159
+ The saved queue is a single-writer slot, and a Web Lock on the key elects its writer. `crossTab` sets
160
+ what a second tab on the same key does:
133
161
 
134
- - `'queue'` (default) takes a Web Lock on the key. The second tab waits, with `status()` reading
135
- `'connecting'`, until the first tab closes, then takes over. Exactly one durable writer holds the
136
- key at a time.
162
+ - `'queue'` (default): the second tab waits, with `status()` reading `'waiting'`, until the first
163
+ tab closes, then takes over.
164
+ - `'ephemeral'`: the second tab is live at once on its own origin. It never reads or writes the saved
165
+ queue, so its unacknowledged writes are lost if it closes before the room acknowledges them. When
166
+ the lock is free, it takes the lock and behaves like `'queue'`.
137
167
  - `'off'` skips the lock. Use it when you coordinate ownership yourself, for example a distinct key
138
168
  per tab, or leader election over `tabSync`.
139
169
 
140
- When the Web Locks API is unavailable, `'queue'` logs a development warning and runs without the
141
- lock.
170
+ When the Web Locks API is unavailable, `'queue'` and `'ephemeral'` log a development warning and run
171
+ without the lock, as the owner of the saved queue.
142
172
 
143
173
  The outbox persists your unacknowledged writes, not a full snapshot. For a meshed store, use it in
144
174
  place of wrapping the store in `persist`. The two race on boot, and the outbox is the one that
@@ -181,7 +211,7 @@ Run `tabSync` and `meshSync` on the same store to share it across a user's tabs
181
211
  carries it to the room. The outbox lock elects the leader, so only one tab holds the relay
182
212
  connection and the others share state over `tabSync`. A write in any tab reaches the room through the
183
213
  leader, and a room write reaches every tab through `tabSync`. When the leader tab closes, another
184
- tab acquires the lock and takes over, adopting the persisted origin.
214
+ tab acquires the lock and takes over on a fresh origin, restoring and resending the persisted tail.
185
215
 
186
216
  ```ts
187
217
  import { store, tabSync } from '@mmstack/primitives';
@@ -551,3 +581,34 @@ For an external presence roster, pass a subscription function as `roster`. It mu
551
581
  emit the current origins and every subsequent membership transition, and return an unsubscribe
552
582
  function. Do not adapt a signal using an effect: a leave and rejoin can be coalesced into one
553
583
  snapshot, losing the evidence that the previous value must be forgotten.
584
+
585
+ ### Reachability
586
+
587
+ The library ships no ICE servers: it contacts no third party unless you tell it to. Without any,
588
+ browsers hide host addresses behind mDNS names, so links only form between peers on the same
589
+ network segment. Give them servers in one of two ways: a static `RTCConfiguration` passed to
590
+ `rtcPeerConnector(config)`, or a list the relay issues in every welcome through its
591
+ `iceServers` option, which replaces the static list for every link built after that welcome.
592
+ TURN credentials are short-lived and belong to the operator, so the option also takes a function
593
+ of the room and the origin, called at each welcome to mint fresh ones. A link rebuilt long after
594
+ its welcome still uses that welcome's credentials, so choose a lifetime longer than a session.
595
+
596
+ ```ts
597
+ const relay = createRelay({
598
+ iceServers: (room, origin) => [
599
+ { urls: 'turn:turn.example.com', ...mintTurn(room, origin) },
600
+ ],
601
+ });
602
+ ```
603
+
604
+ ### Recovery
605
+
606
+ A link that does not open every channel within `openTimeoutMs` (15 s), loses a channel, or is
607
+ reported gone by its connector is dropped and rebuilt, for as long as the relay lists its origin.
608
+ The WebRTC connector first tries one ICE restart when ICE fails, or stays disconnected for
609
+ `disconnectGraceMs` (5 s). Rebuilds wait a jittered backoff that doubles from `retry.minMs` (1 s)
610
+ to `retry.maxMs` (30 s) and never gives up; a welcome or a join rebuilds at once. Either side may
611
+ rebuild: its new instance announces itself, and the other side rebuilds its end to match, as for
612
+ a new seat. Links are built only after the welcome of the connection they signal over, so every
613
+ link carries that welcome's ICE servers. `links.stalled()` lists the members whose link was lost and has not opened again; with
614
+ `peers()` it tells an open link, a first attempt, and one that keeps failing apart.