@dimchansky/meshpost 0.2.0 → 0.3.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/docs/behavior.md CHANGED
@@ -21,6 +21,8 @@ This document specifies limits, lifecycle rules, wire formats, and security boun
21
21
  | MQTT replay window / MID TTL | `±60,000` ms / `120,000` ms | Anti-replay window for password-encrypted MQTT signaling (`mid` required, max `64` chars). |
22
22
  | WS handshake / ping timeout | `10,000` ms / `25,000` ms | `wsSignaling()` open deadline and keepalive ping interval (`awaitingPong` dead-socket check). |
23
23
  | In-band `rtc:` queue / cap | `16` pending / `32` remote streams | Max queued incoming `'meshpost/1/rtc:'` frames and max tracked remote `MediaStream`s per peer. |
24
+ | `createVoice` remote cap | `4` audio streams per peer | Max concurrent incoming audio streams played/metered per peer (`180` ms meter default). |
25
+ | `fetchTurnCredentials` caps | `5,000` ms / `64 KiB` / `16` servers | Default request timeout, max JSON response body, and max sanitized `RTCIceServer` entries. |
24
26
 
25
27
  ## Room lifecycle
26
28
 
@@ -29,7 +31,7 @@ This document specifies limits, lifecycle rules, wire formats, and security boun
29
31
  - `joinRoom(name, options)` generates a fresh random peer ID (UUID v4, exposed as `room.id`). A page reload creates a new peer ID.
30
32
  - `joinRoom()` returns synchronously and starts discovery immediately.
31
33
  - `room.peers` returns a snapshot array of remote peer IDs whose unreliable and reliable channels are both open.
32
- - Subscribe to `onPeerJoin` and `onPeerLeave` for future changes. Each subscription (`onPacket`, `onDrain`, `onPeerJoin`, `onPeerLeave`, `onSignalingChange`, `onError`, `onPeerStream`, `onPeerStreamEnd`, `channel.onPacket`, `blobChannel.onBlob`, and `blobChannel.onProgress`) returns an idempotent unsubscribe function and optionally accepts `ListenOptions` (`{ once?: boolean, signal?: AbortSignal }`).
34
+ - Subscribe to `onPeerJoin` and `onPeerLeave` for future changes. Each subscription (`onPacket`, `onDrain`, `onPeerJoin`, `onPeerLeave`, `onSignalingChange`, `onError`, `onPeerStream`, `onPeerStreamEnd`, `channel.onPacket`, `blobChannel.onBlob`, `blobChannel.onProgress`, and `voice.onChange`) returns an idempotent unsubscribe function and optionally accepts `ListenOptions` (`{ once?: boolean, signal?: AbortSignal }`).
33
35
  - Subscribing with an already-aborted `signal` (or after `room.leave()`) is a no-op. Unsubscribing, firing a `once` listener, or calling `room.leave()` detaches any registered `'abort'` listener from `signal`. On `channel.onPacket`, `once` unsubscribes only after a packet matches both delivery mode and channel name.
34
36
  - A room of `N` peers forms up to `N(N - 1) / 2` direct connections (bounded per peer by `maxPeers`).
35
37
  - There is no host election, packet forwarding, or history replay. Any peer can join first.
@@ -74,8 +76,10 @@ Note: on public MQTT topics, `roomTopic` hashes `(appId, room)` without `passwor
74
76
  - `createChannel()` throws `TypeError` for an empty name or invalid `delivery`, and `RangeError` if the UTF-8 name exceeds 64 bytes.
75
77
  - `createBlobChannel()` throws `RangeError` for invalid `maxReceiveBytes`, `chunkSize`, `receiveTimeoutMs`, or `maxConcurrentPerPeer`. `blobChannel.send()` rejects with `Error` when closed and `TypeError` for non-`Uint8Array` payloads.
76
78
  - `wsSignaling()` throws `TypeError` for non-`ws:`/`wss:` URLs and `RangeError` for invalid `handshakeTimeoutMs` or `pingIntervalMs`.
77
- - `combineSignaling()` throws `RangeError` when given 0 adapters and `TypeError` if any argument is not a function.
79
+ - `combineSignaling()` throws `RangeError` when given 0 adapters and `TypeError` if any argument is not a function; `withLocalTabs()` throws `TypeError` if `primary` is not a function.
78
80
  - `mqttSignaling()` throws `RangeError` for empty `urls` or invalid `handshakeTimeoutMs`, and `TypeError` for an empty `password`.
81
+ - `createVoice()` throws `TypeError` for invalid `room`, `audioConstraints`, or `to`, and `RangeError` for out-of-range `volume`, `speakingThreshold`, or `speakingIntervalMs`. `voice.setVolume()` throws `RangeError` for non-finite numbers.
82
+ - `fetchTurnCredentials()` rejects with `TypeError` for invalid URLs, disallowed protocols, embedded URL credentials, or invalid `accessKey`, and `RangeError` for invalid `timeoutMs`.
79
83
 
80
84
  ## Sending and receiving
81
85
 
@@ -125,7 +129,7 @@ Note: on public MQTT topics, `roomTopic` hashes `(appId, room)` without `passwor
125
129
 
126
130
  ### ICE gathering, opt-in Trickle ICE, and loopback fallback
127
131
 
128
- - Adapters can opt in to **Trickle ICE** by setting `trickle: true` on their returned `SignalingSession` (enabled by default in `tabSignaling()`, `wsSignaling()`, and `mqttSignaling()`, and in `combineSignaling()` when every child session sets `trickle: true`).
132
+ - Adapters can opt in to **Trickle ICE** by setting `trickle: true` on their returned `SignalingSession` (enabled by default in `tabSignaling()`, `wsSignaling()`, `mqttSignaling()`, `withLocalTabs(primary)` when `primary` sets `trickle: true`, and in `combineSignaling()` when every child session sets `trickle: true`).
129
133
  - When `trickle` is `true`, Meshpost publishes `offer` and `answer` immediately after `setLocalDescription()` without waiting for `iceGatheringTimeoutMs`, batches outgoing ICE candidates over a `50 ms` window (up to `16` per batch, capped at `64` per link), and buffers incoming candidates that arrive before `setRemoteDescription()` resolves (including early candidates arriving before the `offer` on the answerer).
130
134
  - When `trickle` is omitted or `false` (such as `manualSignaling()` or simple custom adapters), each side waits to publish one complete SDP (no trickle ICE required). Publication occurs when `iceGatheringState` reaches `'complete'` or when `iceGatheringTimeoutMs` elapses, whichever comes first.
131
135
  - In non-trickle mode, when you configure TURN servers (or `iceTransportPolicy: 'relay'`) and omit `iceGatheringTimeoutMs`, Meshpost waits up to `min(10,000 ms, handshakeTimeoutMs / 2)` for the first `relay` or public (`srflx`) candidate.
@@ -174,7 +178,7 @@ Each `RTCPeerConnection` creates two pre-negotiated channels with `protocol` set
174
178
 
175
179
  ### Custom `Signaling` contract
176
180
 
177
- - A `Signaling` factory receives `Discovery` (`appId`, `room`, `self`, optional `password`, `roster`, `receive`, `status`, optional `error`) and returns `{ send(to, signal), close() }`.
181
+ - A `Signaling` factory receives `Discovery` (`appId`, `room`, `self`, optional `password`, `roster`, `receive`, `status`, optional `error`) and returns a `SignalingSession` (`{ trickle?, canReach?(peerId), send(to, signal), close() }`).
178
182
  - `send(to, signal)` must return `boolean` (`true` when accepted for delivery; returning `false` drops the pending link).
179
183
  - Report `status('online')` before publishing a roster.
180
184
  - Deliver a roster containing a sender before calling `receive(from, signal)`.
@@ -215,9 +219,20 @@ Each `RTCPeerConnection` creates two pre-negotiated channels with `protocol` set
215
219
  - Composes one or more `Signaling` adapters into a single adapter (returning the single adapter directly when given one).
216
220
  - Pre-initializes all child states as `'connecting'` and buffers synchronous startup callbacks until every child factory returns without throwing (closing already-started children if any factory throws).
217
221
  - Aggregate `SignalingState` is `'online'` if any child is `'online'`, `'connecting'` if none is `'online'` and any is `'connecting'`, `'offline'` if none is `'online'`/`'connecting'` and any is `'offline'`, and `'closed'` when all children are `'closed'`.
218
- - Maintains per-child peer sets, publishes their union to `Discovery.roster`, clears a child's entries when that child goes `'offline'`/`'closed'` (preserving already-open WebRTC links), routes `send(to, signal)` to every online child advertising `to`, and forwards child `error` reports.
222
+ - Maintains per-child peer sets, publishes their union to `Discovery.roster`, clears unreachable entries when a child goes `'offline'`/`'closed'` (preserving already-open WebRTC links and any peers still reachable via nested `canReach`), routes `send(to, signal)` to every reachable child advertising `to`, and forwards child `error` reports.
219
223
  - Advertises `trickle: true` only when every child session advertises `trickle === true`, and deduplicates incoming `{ kind: 'ice' }` candidates per `(from, attempt)` across child adapters.
220
224
 
225
+ ### `withLocalTabs(primary)`
226
+
227
+ - Composes `tabSignaling()` with `primary`, publishing the union roster and deduplicating Trickle ICE across both transports while forwarding **only `primary`'s `SignalingState` and `error` events** to the room.
228
+ - Exposes `SignalingSession.canReach(peerId)` so same-browser tabs still negotiate and connect even while `primary` (and `room.signalingState`) is `'connecting'` or `'offline'` (including when nested inside `combineSignaling()`).
229
+ - Delegates directly to `primary` without error when `BroadcastChannel` is undefined, on `file:` or opaque (`origin === 'null'`) origins, or if `new BroadcastChannel(...)` throws `SecurityError`.
230
+
231
+ ### `createVoice(room, options?)` and `fetchTurnCredentials(url, options?)`
232
+
233
+ - `createVoice(room, options?)` attaches hidden `<audio autoplay>` elements for up to `4` remote audio streams per peer (hydrating any streams already active on `room` at construction), coalesces concurrent `startMic()`/`toggleMic()` calls, cancels pending `getUserMedia` via `micEpoch` on `stopMic()`/`close()`, saves/restores `navigator.audioSession.type` (`'play-and-record'`) across all exit paths, routes `AnalyserNode` through a zero-gain `GainNode` to `destination` for WebKit pull-graph compatibility, unlocks blocked audio via lock-free `{ capture: true }` gestures + `visibilitychange`, and closes automatically on `room.leave()` without redundant `removeStream()` renegotiation.
234
+ - `fetchTurnCredentials(url, options?)` validates `https://` (or `http://localhost` / `127.0.0.1` / `[::1]`) without embedded credentials, sends `POST` with `redirect: 'error'`, `credentials: 'omit'`, `cache: 'no-store'`, enforces `timeoutMs` (`5,000` ms default) and a `64 KiB` streaming body limit, and returns `1..16` sanitized `RTCIceServer` entries (`^(stuns?|turns?):[^\s]+$`).
235
+
221
236
  ### `manualSignaling(outgoing)`
222
237
 
223
238
  - Produces JSON envelopes with `{ v: 1, app, room, from, auth?, to?, signal? }`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dimchansky/meshpost",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "author": "Dmitrij Koniajev",
5
5
  "license": "MIT",
6
6
  "type": "module",