@fairfox/polly 0.63.0 → 0.65.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/dist/src/mesh.d.ts +1 -1
- package/dist/src/mesh.js +244 -35
- package/dist/src/mesh.js.map +6 -6
- package/dist/src/peer.js +88 -20
- package/dist/src/peer.js.map +3 -3
- package/dist/src/shared/lib/crdt-state.d.ts +14 -0
- package/dist/src/shared/lib/mesh-state.d.ts +37 -1
- package/dist/src/shared/lib/mesh-webrtc-adapter.d.ts +89 -1
- package/package.json +1 -1
package/dist/src/mesh.js.map
CHANGED
|
@@ -9,24 +9,24 @@
|
|
|
9
9
|
"/**\n * BlobRef — content-addressed reference to a binary blob.\n *\n * CRDT documents held by `$peerState` and `$meshState` primitives should not embed\n * large binary payloads directly. The Automerge history grows monotonically with every\n * op, and embedding a file means every op carries the file, memory explodes, and sync\n * bandwidth is terrible. The sound pattern is to store a reference to the blob inside\n * the document and keep the bytes in a separate, content-addressed blob store.\n *\n * This module defines the reference shape so that applications can hold `BlobRef`\n * values inside their `$peerState`/`$meshState` documents from day one. The actual\n * blob store, the upload/fetch API, and transport-specific blob sync are deliberately\n * out of scope and will land in a follow-up RFC. Provisioning the type now prevents\n * a data-model migration when that RFC ships.\n *\n * @example\n * ```ts\n * const doc = $peerState<Document>(\"doc\", { title: \"\", attachments: [] });\n *\n * const bytes = new Uint8Array(await file.arrayBuffer());\n * const ref = await createBlobRef({\n * bytes,\n * filename: file.name,\n * mimeType: file.type,\n * });\n *\n * doc.value = {\n * ...doc.value,\n * attachments: [...doc.value.attachments, ref],\n * };\n * ```\n */\n\n/**\n * Content-addressed reference to a binary blob.\n *\n * The `hash` field is a hex-encoded SHA-256 digest of the blob's bytes. Two blobs\n * with identical bytes always produce the same hash, so references can be deduplicated\n * and the blob store (when it ships) can use the hash as its primary key.\n */\nexport type BlobRef = {\n /** Hex-encoded SHA-256 digest of the blob's bytes (64 lowercase hex characters). */\n hash: string;\n /** Length of the blob in bytes. */\n size: number;\n /** Filename as supplied by the originating application. Not used for addressing. */\n filename: string;\n /** MIME type as supplied by the originating application. Not used for addressing. */\n mimeType: string;\n};\n\n/**\n * Type guard for {@link BlobRef}. Useful at CRDT document boundaries where values\n * may have been authored by older clients or by clients running different schemas.\n */\nexport function isBlobRef(value: unknown): value is BlobRef {\n if (typeof value !== \"object\" || value === null) return false;\n const v = value as unknown as Record<string, unknown>;\n return (\n typeof v[\"hash\"] === \"string\" &&\n /^[0-9a-f]{64}$/.test(v[\"hash\"]) &&\n typeof v[\"size\"] === \"number\" &&\n Number.isInteger(v[\"size\"]) &&\n v[\"size\"] >= 0 &&\n typeof v[\"filename\"] === \"string\" &&\n typeof v[\"mimeType\"] === \"string\"\n );\n}\n\n/**\n * Compute the SHA-256 hex digest of a byte sequence. Pure and deterministic.\n */\nexport async function computeBlobHash(bytes: Uint8Array): Promise<string> {\n // Copy into a fresh ArrayBuffer-backed view so the buffer type is ArrayBuffer\n // rather than the wider ArrayBufferLike, which crypto.subtle.digest's current\n // type signature rejects for SharedArrayBuffer safety.\n const buffer = new ArrayBuffer(bytes.byteLength);\n const copy = new Uint8Array(buffer);\n copy.set(bytes);\n const digest = await crypto.subtle.digest(\"SHA-256\", buffer);\n const view = new Uint8Array(digest);\n let hex = \"\";\n for (const byte of view) {\n hex += byte.toString(16).padStart(2, \"0\");\n }\n return hex;\n}\n\n/**\n * Arguments for {@link createBlobRef}.\n */\nexport type CreateBlobRefArgs = {\n bytes: Uint8Array;\n filename: string;\n mimeType: string;\n};\n\n/**\n * Build a {@link BlobRef} from raw bytes and metadata. The hash is computed from the\n * bytes; the filename and mimeType are carried from the originating application\n * verbatim and do not affect the content address.\n */\nexport async function createBlobRef({\n bytes,\n filename,\n mimeType,\n}: CreateBlobRefArgs): Promise<BlobRef> {\n const hash = await computeBlobHash(bytes);\n return {\n hash,\n size: bytes.byteLength,\n filename,\n mimeType,\n };\n}\n",
|
|
10
10
|
"/**\n * blob-transfer — chunking, reassembly, and wire format for peer-to-peer\n * blob transfer over WebRTC data channels.\n *\n * Blobs are split into fixed-size chunks (64 KiB) and sent as individual\n * data channel messages. The wire format reuses the same length-prefixed\n * header + binary payload layout that MeshWebRTCAdapter uses for Automerge\n * sync messages. Blob messages are distinguished by a `type` field in the\n * JSON header that starts with \"blob-\".\n *\n * Three message types:\n * - blob-chunk: a single chunk of a blob transfer\n * - blob-request: ask a peer to send chunks for a specific hash\n * - blob-have: announce local availability of a blob\n */\n\n// Constants\n\n/** Default chunk size in bytes: 64 KiB. Stays well within WebRTC SCTP\n * message size limits (~256 KiB in most browsers). */\nexport const BLOB_CHUNK_SIZE = 65_536;\n\n/** High-water mark for RTCDataChannel.bufferedAmount. The sender pauses\n * when the buffer exceeds this threshold. */\nexport const BLOB_BUFFER_HIGH_WATER = 256 * 1024;\n\n// Message header types\n\nexport interface BlobChunkHeader {\n type: \"blob-chunk\";\n hash: string;\n index: number;\n total: number;\n}\n\nexport interface BlobRequestHeader {\n type: \"blob-request\";\n hash: string;\n /** Chunk indices needed. Omit to request all chunks. */\n missing?: number[];\n}\n\nexport interface BlobHaveHeader {\n type: \"blob-have\";\n hash: string;\n}\n\nexport type BlobMessageHeader = BlobChunkHeader | BlobRequestHeader | BlobHaveHeader;\n\n// Chunking\n\n/** Split bytes into fixed-size chunks. The last chunk may be smaller. */\nexport function chunkBlob(bytes: Uint8Array, chunkSize: number = BLOB_CHUNK_SIZE): Uint8Array[] {\n const chunks: Uint8Array[] = [];\n for (let offset = 0; offset < bytes.length; offset += chunkSize) {\n chunks.push(bytes.subarray(offset, Math.min(offset + chunkSize, bytes.length)));\n }\n // An empty blob produces one empty chunk so the protocol always sends\n // at least one blob-chunk message.\n if (chunks.length === 0) {\n chunks.push(new Uint8Array(0));\n }\n return chunks;\n}\n\n/** Reassemble chunks into a single Uint8Array. Throws if any chunk index\n * in [0, total) is missing from the map. */\nexport function reassembleChunks(chunks: Map<number, Uint8Array>, total: number): Uint8Array {\n let totalBytes = 0;\n for (let i = 0; i < total; i++) {\n const chunk = chunks.get(i);\n if (!chunk) {\n throw new Error(`reassembleChunks: missing chunk ${i} of ${total}`);\n }\n totalBytes += chunk.length;\n }\n const out = new Uint8Array(totalBytes);\n let offset = 0;\n for (let i = 0; i < total; i++) {\n const chunk = chunks.get(i) as unknown as Uint8Array;\n out.set(chunk, offset);\n offset += chunk.length;\n }\n return out;\n}\n\n/** Return the indices of chunks missing from a partial chunk map. */\nexport function missingChunkIndices(chunks: Map<number, Uint8Array>, total: number): number[] {\n const missing: number[] = [];\n for (let i = 0; i < total; i++) {\n if (!chunks.has(i)) {\n missing.push(i);\n }\n }\n return missing;\n}\n\n// Wire format\n\n/** Serialise a blob message into the shared wire format:\n * [4-byte BE header length][JSON header bytes][binary payload].\n * Returns an ArrayBuffer-backed Uint8Array usable by RTCDataChannel.send. */\nexport function serialiseBlobMessage(\n header: BlobMessageHeader,\n data: Uint8Array = new Uint8Array(0)\n): Uint8Array<ArrayBuffer> {\n const headerBytes = new TextEncoder().encode(JSON.stringify(header));\n const size = 4 + headerBytes.length + data.length;\n const buffer = new ArrayBuffer(size);\n const out = new Uint8Array(buffer);\n const view = new DataView(buffer);\n view.setUint32(0, headerBytes.length, false);\n out.set(headerBytes, 4);\n out.set(data, 4 + headerBytes.length);\n return out;\n}\n\n/** Peek at the header of a wire-format message without full\n * deserialisation. Returns the parsed header and the data slice,\n * or undefined if the bytes are too short or malformed. */\nexport function parseBlobMessage(\n bytes: Uint8Array\n): { header: BlobMessageHeader; data: Uint8Array } | undefined {\n if (bytes.length < 4) return undefined;\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) return undefined;\n try {\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as BlobMessageHeader;\n const data = bytes.subarray(4 + headerLen);\n return { header, data };\n } catch {\n return undefined;\n }\n}\n\n/** Check whether a wire-format message header type starts with \"blob-\". */\nexport function isBlobMessageType(bytes: Uint8Array): boolean {\n if (bytes.length < 4) return false;\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) return false;\n // Fast check: look for \"blob-\" in the header JSON without full parse.\n // The type field is always near the start of the JSON object.\n const headerSlice = bytes.subarray(4, 4 + headerLen);\n // Search for the byte sequence: \"type\":\"blob-\n const needle = new TextEncoder().encode('\"type\":\"blob-');\n return findSubarray(headerSlice, needle) !== -1;\n}\n\n/** Find the first occurrence of needle in haystack. Returns -1 if not found. */\nfunction findSubarray(haystack: Uint8Array, needle: Uint8Array): number {\n if (needle.length === 0) return 0;\n outer: for (let i = 0; i <= haystack.length - needle.length; i++) {\n for (let j = 0; j < needle.length; j++) {\n if (haystack[i + j] !== needle[j]) continue outer;\n }\n return i;\n }\n return -1;\n}\n",
|
|
11
11
|
"/**\n * blob-store-impl — peer-to-peer blob store backed by WebRTC data channels.\n *\n * The store piggybacks on an existing MeshWebRTCAdapter. Blob messages ride\n * on the same data channel as Automerge sync traffic, distinguished by a\n * \"blob-\" prefix on the message type field.\n *\n * Lifecycle:\n * - put: hash-verify → cache plaintext locally → announce blob-have\n * - get: check cache → if miss, send blob-request(s) → receive chunks\n * → decrypt each chunk as it arrives → reassemble plaintext\n * → hash-verify → cache\n *\n * Encryption model: chunk-then-encrypt. The sender chunks the plaintext\n * into 64 KiB pieces, encrypts each chunk independently under the configured\n * key (with a fresh random nonce per chunk), and sends the sealed envelope.\n * The receiver decrypts each chunk on arrival. Peak sender memory is ~1\n * chunk of ciphertext at a time rather than the entire ciphertext blob.\n *\n * Per-op keys: callers can override the store's default encryption key per\n * put/get via BlobTransferOptions.key. This supports per-document keys\n * without requiring the store to track document ownership.\n *\n * Multi-source fetch: the get() flow requests from a single peer initially,\n * but the re-request timer rotates through peers that have announced\n * availability of the blob, so a transfer that stalls on one peer recovers\n * against another.\n */\n\nimport { MemoryBlobCache } from \"./blob-cache\";\nimport { computeBlobHash } from \"./blob-ref\";\nimport type { BlobCache, BlobProgressCallback, BlobStore, BlobStoreOptions } from \"./blob-store\";\nimport {\n type BlobChunkHeader,\n type BlobHaveHeader,\n type BlobMessageHeader,\n type BlobRequestHeader,\n chunkBlob,\n missingChunkIndices,\n reassembleChunks,\n serialiseBlobMessage,\n} from \"./blob-transfer\";\nimport type { MeshWebRTCAdapter } from \"./mesh-webrtc-adapter\";\n\nconst DEFAULT_MAX_BLOB_SIZE = 100 * 1024 * 1024; // 100 MiB\nconst DOWNLOAD_TIMEOUT_MS = 60_000;\nconst RE_REQUEST_DELAY_MS = 5_000;\n\n/** State for an in-progress blob download. */\ninterface DownloadState {\n total: number;\n /** Decrypted plaintext chunks, keyed by index. */\n chunks: Map<number, Uint8Array>;\n resolve: (bytes: Uint8Array) => void;\n reject: (error: Error) => void;\n onProgress?: BlobProgressCallback;\n timeoutId?: ReturnType<typeof setTimeout>;\n reRequestId?: ReturnType<typeof setTimeout>;\n /** Encryption key for this download, if any. */\n key?: Uint8Array;\n /** Peers we've asked, rotated on re-request for multi-source fetch. */\n peersAttempted: string[];\n /** Index into peersAttempted for the next re-request. */\n peerRotationIndex: number;\n}\n\n/** Create a blob store that transfers blobs peer-to-peer over a\n * MeshWebRTCAdapter's data channels. */\nexport function createBlobStore(adapter: MeshWebRTCAdapter, options?: BlobStoreOptions): BlobStore {\n const maxBlobSize = options?.maxBlobSize ?? DEFAULT_MAX_BLOB_SIZE;\n const defaultKey = options?.encrypt?.key;\n const cache: BlobCache = options?.cache ?? new MemoryBlobCache();\n\n // Per-hash key used at put time. Retained so that blob-request handlers\n // can encrypt chunks with the same key they were put under.\n const keysByHash = new Map<string, Uint8Array>();\n // Track which peers have which blobs (hash → set of peer IDs).\n const peerBlobs = new Map<string, Set<string>>();\n // Track in-progress downloads (hash → download state).\n const downloads = new Map<string, DownloadState>();\n // Hashes we have locally (for announcing to new peers).\n const localHashes = new Set<string>();\n // Object URL cache (hash → object URL string).\n const urlCache = new Map<string, string>();\n let disposed = false;\n\n // Wire up the adapter's blob message callback.\n adapter.onBlobMessage = (peerId, header, data) => {\n if (disposed) return;\n const type = header[\"type\"] as unknown as string;\n switch (type) {\n case \"blob-chunk\":\n void handleChunk(peerId, header as unknown as BlobChunkHeader, data);\n break;\n case \"blob-request\":\n void handleRequest(peerId, header as unknown as BlobRequestHeader);\n break;\n case \"blob-have\":\n handleHave(peerId, header as unknown as BlobHaveHeader);\n break;\n }\n };\n\n // Announce local blobs to new peers when they connect.\n const peerCandidateHandler = (event: { peerId: unknown }) => {\n if (disposed) return;\n const newPeerId = event.peerId as unknown as string;\n for (const hash of localHashes) {\n const msg = serialiseBlobMessage({ type: \"blob-have\", hash } as unknown as BlobMessageHeader);\n adapter.sendBlobMessage(newPeerId, msg);\n }\n };\n adapter.on(\"peer-candidate\", peerCandidateHandler);\n\n // Crypto helpers\n\n async function encryptChunk(plaintext: Uint8Array, key: Uint8Array): Promise<Uint8Array> {\n const { encrypt } = await import(\"./encryption\");\n return encrypt(plaintext, key);\n }\n\n async function decryptChunk(\n sealed: Uint8Array,\n key: Uint8Array\n ): Promise<Uint8Array | undefined> {\n const { decrypt } = await import(\"./encryption\");\n return decrypt(sealed, key);\n }\n\n // Incoming message handlers\n\n async function handleChunk(\n peerId: string,\n header: BlobChunkHeader,\n data: Uint8Array\n ): Promise<void> {\n const download = downloads.get(header.hash);\n if (!download) return;\n download.total = header.total;\n\n // Track this peer as a source of chunks for this blob.\n if (!download.peersAttempted.includes(peerId)) {\n download.peersAttempted.push(peerId);\n }\n\n // Decrypt if a key is configured.\n let chunkBytes: Uint8Array;\n if (download.key) {\n const plaintext = await decryptChunk(data, download.key);\n if (!plaintext) {\n // Skip this chunk — bad key or corrupted. Let re-request retry.\n return;\n }\n chunkBytes = plaintext;\n } else {\n chunkBytes = data.slice();\n }\n\n download.chunks.set(header.index, chunkBytes);\n\n reportChunkProgress(download);\n resetReRequestTimer(download);\n\n if (download.chunks.size >= header.total) {\n finishDownload(header.hash, download);\n } else {\n scheduleReRequest(header.hash, download);\n }\n }\n\n function reportChunkProgress(download: DownloadState): void {\n if (!download.onProgress || download.total <= 0) return;\n let loaded = 0;\n for (const chunk of download.chunks.values()) {\n loaded += chunk.length;\n }\n download.onProgress({ loaded, total: undefined, phase: \"downloading\" });\n }\n\n function resetReRequestTimer(download: DownloadState): void {\n if (download.reRequestId) {\n clearTimeout(download.reRequestId);\n download.reRequestId = undefined;\n }\n }\n\n function finishDownload(hash: string, download: DownloadState): void {\n clearDownloadTimers(download);\n try {\n const assembled = reassembleChunks(download.chunks, download.total);\n downloads.delete(hash);\n download.resolve(assembled);\n } catch (err) {\n downloads.delete(hash);\n download.reject(err instanceof Error ? err : new Error(String(err)));\n }\n }\n\n async function handleRequest(peerId: string, header: BlobRequestHeader): Promise<void> {\n const plaintext = await cache.get(header.hash);\n if (!plaintext) return;\n\n const plaintextChunks = chunkBlob(plaintext);\n const requested = header.missing ?? plaintextChunks.map((_, i) => i);\n const chunkKey = keysByHash.get(header.hash);\n\n for (const index of requested) {\n await sendChunkAtIndex(peerId, header.hash, plaintextChunks, index, chunkKey);\n }\n }\n\n async function sendChunkAtIndex(\n peerId: string,\n hash: string,\n plaintextChunks: Uint8Array[],\n index: number,\n chunkKey: Uint8Array | undefined\n ): Promise<void> {\n if (index < 0 || index >= plaintextChunks.length) return;\n const plainChunk = plaintextChunks[index];\n if (!plainChunk) return;\n\n const payload = chunkKey ? await encryptChunk(plainChunk, chunkKey) : plainChunk;\n const chunkHeader: BlobChunkHeader = {\n type: \"blob-chunk\",\n hash,\n index,\n total: plaintextChunks.length,\n };\n const msg = serialiseBlobMessage(chunkHeader as unknown as BlobMessageHeader, payload);\n\n if (!adapter.sendBlobMessage(peerId, msg)) {\n await waitForBufferDrain();\n adapter.sendBlobMessage(peerId, msg);\n }\n }\n\n function handleHave(peerId: string, header: BlobHaveHeader): void {\n let peers = peerBlobs.get(header.hash);\n if (!peers) {\n peers = new Set();\n peerBlobs.set(header.hash, peers);\n }\n peers.add(peerId);\n }\n\n // Helpers\n\n function announceHave(hash: string): void {\n const msg = serialiseBlobMessage({ type: \"blob-have\", hash } as unknown as BlobMessageHeader);\n for (const peerId of adapter.connectedPeerIds) {\n adapter.sendBlobMessage(peerId, msg);\n }\n }\n\n function clearDownloadTimers(download: DownloadState): void {\n if (download.timeoutId) clearTimeout(download.timeoutId);\n if (download.reRequestId) clearTimeout(download.reRequestId);\n }\n\n function scheduleReRequest(hash: string, download: DownloadState): void {\n download.reRequestId = setTimeout(() => fireReRequest(hash, download), RE_REQUEST_DELAY_MS);\n }\n\n function fireReRequest(hash: string, download: DownloadState): void {\n if (!downloads.has(hash)) return;\n const missing = missingChunkIndices(download.chunks, download.total);\n if (missing.length === 0) return;\n\n // Multi-source fetch: rotate through known peers so a stalled\n // transfer recovers against a different source.\n const peers = peerBlobs.get(hash);\n const pool = peers && peers.size > 0 ? Array.from(peers) : Array.from(adapter.connectedPeerIds);\n if (pool.length === 0) return;\n const target = pool[download.peerRotationIndex % pool.length];\n download.peerRotationIndex++;\n if (!target) return;\n\n const reqHeader: BlobRequestHeader = { type: \"blob-request\", hash, missing };\n const msg = serialiseBlobMessage(reqHeader as unknown as BlobMessageHeader);\n adapter.sendBlobMessage(target, msg);\n\n // Re-arm timer in case this peer also stalls.\n scheduleReRequest(hash, download);\n }\n\n function waitForBufferDrain(): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, 50));\n }\n\n // BlobStore implementation\n\n const store: BlobStore = {\n async put(ref, bytes, options?): Promise<void> {\n if (disposed) throw new Error(\"BlobStore is disposed\");\n options?.signal?.throwIfAborted();\n\n if (bytes.length > maxBlobSize) {\n throw new Error(`Blob exceeds maximum size (${bytes.length} > ${maxBlobSize})`);\n }\n\n // Verify hash.\n const hash = await computeBlobHash(bytes);\n if (hash !== ref.hash) {\n throw new Error(`Hash mismatch: expected ${ref.hash}, got ${hash}`);\n }\n\n options?.signal?.throwIfAborted();\n\n // Resolve the encryption key: per-op override > store default > no\n // encryption. Record the key so incoming blob-requests encrypt with\n // the same one.\n const key = options?.key ?? defaultKey;\n if (key) {\n keysByHash.set(ref.hash, key);\n }\n\n options?.onProgress?.({ loaded: bytes.length, total: bytes.length, phase: \"uploading\" });\n\n // Store plaintext in cache. Chunking + encryption is deferred to\n // handleRequest for streaming behavior.\n await cache.put(ref.hash, bytes);\n localHashes.add(ref.hash);\n\n announceHave(ref.hash);\n },\n\n async get(hash, options?): Promise<Uint8Array | undefined> {\n if (disposed) throw new Error(\"BlobStore is disposed\");\n options?.signal?.throwIfAborted();\n\n // Check local cache first.\n const cached = await cache.get(hash);\n if (cached) return cached;\n\n // Resolve the decryption key: per-op override > store default > no\n // decryption (plaintext chunks).\n const key = options?.key ?? defaultKey;\n\n // Find peers that have this blob.\n const peers = peerBlobs.get(hash);\n const candidates =\n peers && peers.size > 0 ? Array.from(peers) : Array.from(adapter.connectedPeerIds);\n const targetPeer = candidates[0];\n if (!targetPeer) return undefined;\n\n const requestHeader: BlobRequestHeader = { type: \"blob-request\", hash };\n const msg = serialiseBlobMessage(requestHeader as unknown as BlobMessageHeader);\n adapter.sendBlobMessage(targetPeer, msg);\n\n // Wait for chunks to arrive.\n const plaintext = await new Promise<Uint8Array>((resolve, reject) => {\n const download: DownloadState = {\n total: 0,\n chunks: new Map(),\n resolve,\n reject,\n onProgress: options?.onProgress,\n key,\n peersAttempted: [targetPeer],\n peerRotationIndex: 1,\n };\n downloads.set(hash, download);\n\n options?.signal?.addEventListener(\n \"abort\",\n () => {\n if (downloads.has(hash)) {\n clearDownloadTimers(download);\n downloads.delete(hash);\n reject(new Error(\"Blob download aborted\"));\n }\n },\n { once: true }\n );\n\n download.timeoutId = setTimeout(() => {\n if (downloads.has(hash)) {\n clearDownloadTimers(download);\n downloads.delete(hash);\n reject(new Error(\"Blob download timed out\"));\n }\n }, DOWNLOAD_TIMEOUT_MS);\n });\n\n // Verify hash against the plaintext.\n const actualHash = await computeBlobHash(plaintext);\n if (actualHash !== hash) {\n throw new Error(`Blob hash mismatch after download: expected ${hash}, got ${actualHash}`);\n }\n\n // Cache locally. Record the key so future blob-requests from peers\n // use the same one.\n await cache.put(hash, plaintext);\n if (key) keysByHash.set(hash, key);\n localHashes.add(hash);\n\n return plaintext;\n },\n\n async url(hash): Promise<string | undefined> {\n if (disposed) return undefined;\n const cached = urlCache.get(hash);\n if (cached) return cached;\n const bytes = await cache.get(hash);\n if (!bytes) return undefined;\n const buffer = new ArrayBuffer(bytes.byteLength);\n new Uint8Array(buffer).set(bytes);\n const objectUrl = URL.createObjectURL(new Blob([buffer]));\n urlCache.set(hash, objectUrl);\n return objectUrl;\n },\n\n async pin(hash): Promise<void> {\n await cache.pin(hash);\n },\n\n async unpin(hash): Promise<void> {\n await cache.unpin(hash);\n },\n\n async size(): Promise<number> {\n return cache.size();\n },\n\n async evict(maxBytes): Promise<number> {\n return cache.evict(maxBytes);\n },\n\n dispose(): void {\n disposed = true;\n adapter.onBlobMessage = undefined;\n adapter.off(\"peer-candidate\", peerCandidateHandler);\n for (const [hash, download] of downloads) {\n clearDownloadTimers(download);\n download.reject(new Error(\"BlobStore disposed\"));\n downloads.delete(hash);\n }\n for (const objectUrl of urlCache.values()) {\n URL.revokeObjectURL(objectUrl);\n }\n urlCache.clear();\n keysByHash.clear();\n cache.dispose();\n },\n };\n\n return store;\n}\n",
|
|
12
|
-
"/**\n * @fairfox/polly/mesh — mesh-networked CRDT state with end-to-end encryption.\n *\n * Wraps the peer-first primitives with WebRTC mesh transport and\n * sign-then-encrypt envelopes. This subpath export isolates the\n * @automerge/* and tweetnacl dependency trees so consumers who only\n * use $sharedState / $syncedState / $resource never pull them in.\n *\n * @example\n * ```typescript\n * import { $meshState, configureMeshState } from \"@fairfox/polly/mesh\";\n * ```\n */\n\n// Eagerly initialise Automerge's WASM runtime. Must come before anything\n// that touches @automerge/automerge-repo/slim.\nimport \"./shared/lib/wasm-init\";\n\nexport { IndexedDBBlobCache, MemoryBlobCache } from \"./shared/lib/blob-cache\";\nexport type { BlobRef, CreateBlobRefArgs } from \"./shared/lib/blob-ref\";\nexport { computeBlobHash, createBlobRef, isBlobRef } from \"./shared/lib/blob-ref\";\n// Blob store\nexport type {\n BlobCache,\n BlobProgressCallback,\n BlobProgressEvent,\n BlobStore,\n BlobStoreOptions,\n BlobTransferOptions,\n} from \"./shared/lib/blob-store\";\nexport { createBlobStore } from \"./shared/lib/blob-store-impl\";\nexport type {\n EncryptedEnvelope,\n SealedBytes,\n} from \"./shared/lib/encryption\";\nexport {\n decrypt,\n decryptOrThrow,\n EncryptionError,\n encrypt,\n generateDocumentKey,\n KEY_BYTES as ENCRYPTION_KEY_BYTES,\n NONCE_BYTES as ENCRYPTION_NONCE_BYTES,\n TAG_BYTES as ENCRYPTION_TAG_BYTES,\n} from \"./shared/lib/encryption\";\n// Keyring storage (runtime-agnostic)\nexport type { KeyringStorage } from \"./shared/lib/keyring-storage\";\nexport {\n deserialiseKeyring,\n memoryKeyringStorage,\n serialiseKeyring,\n} from \"./shared/lib/keyring-storage\";\n// Mesh client factory\nexport type {\n CreateMeshClientOptions,\n MeshClient,\n MeshClientHandleSnapshot,\n MeshClientPeerStateSnapshot,\n MeshStateLazyWrapperDocIdDuplicate,\n MeshStateModuleDiagnostics,\n} from \"./shared/lib/mesh-client\";\nexport { createMeshClient } from \"./shared/lib/mesh-client\";\n// Mesh network adapter (sign-then-encrypt envelopes over any base adapter)\nexport type {\n MeshKeyring,\n MeshNetworkAdapterOptions,\n} from \"./shared/lib/mesh-network-adapter\";\nexport {\n DEFAULT_MESH_KEY_ID,\n MeshNetworkAdapter,\n} from \"./shared/lib/mesh-network-adapter\";\n// Mesh signaling client\nexport type {\n MeshSignalingClientOptions,\n SignalingMessage as MeshSignalingMessage,\n} from \"./shared/lib/mesh-signaling-client\";\nexport { MeshSignalingClient } from \"./shared/lib/mesh-signaling-client\";\nexport type {\n DocIdResolver,\n LazyWrapperExitReason,\n MeshStateLazyWrapperRecord,\n MeshStateLoadedRejectionBreadcrumb,\n MeshStateOptions,\n MeshStateStorageOpenError,\n} from \"./shared/lib/mesh-state\";\n// Mesh state wrappers\nexport {\n $meshCounter,\n $meshList,\n $meshState,\n $meshText,\n configureMeshState,\n deriveDocumentId,\n getDocIdResolver,\n getLastConfiguredRepoPeerId,\n getLastLoadedRejection,\n getLazyInvocations,\n getLazyReachedRepo,\n getLazyWrappers,\n getMeshStateModuleId,\n getStorageOpenError,\n isMeshStateConfigured,\n MESH_STATE_MODULE_ID,\n registerDocIdResolver,\n resetMeshState,\n resolveDocumentId,\n wasMeshStateResolved,\n} from \"./shared/lib/mesh-state\";\n// Mesh WebRTC adapter\nexport type {\n HandleSyncSnapshot,\n InFlightSyncSnapshot,\n MeshWebRTCAdapterOptions,\n SlotInitiationDecision,\n SlotInitiationRejectionReason,\n SweepSnapshot,\n SyncHandshakeAttemptSnapshot,\n SyncProgressEvent,\n TransportSnapshot,\n} from \"./shared/lib/mesh-webrtc-adapter\";\nexport { DEFAULT_ICE_SERVERS, MeshWebRTCAdapter } from \"./shared/lib/mesh-webrtc-adapter\";\n// Pairing and revocation (depend on signing/encryption)\nexport type {\n CreatePairingTokenOptions,\n PairingToken,\n} from \"./shared/lib/pairing\";\nexport {\n applyPairingToken,\n createPairingToken,\n createPairingTokenWithFreshIdentity,\n DEFAULT_PAIRING_TTL_MS,\n decodePairingToken,\n encodePairingToken,\n isPairingTokenExpired,\n PAIRING_NONCE_BYTES,\n PAIRING_TOKEN_VERSION,\n PairingError,\n parsePairingToken,\n serialisePairingToken,\n} from \"./shared/lib/pairing\";\nexport type {\n CreateRevocationOptions,\n RevocationRecord,\n} from \"./shared/lib/revocation\";\nexport {\n applyRevocation,\n createRevocation,\n decodeRevocation,\n encodeRevocation,\n REVOCATION_MAGIC,\n REVOCATION_RECORD_VERSION,\n RevocationError,\n revokePeerLocally,\n} from \"./shared/lib/revocation\";\n// Cryptographic primitives (signing + encryption)\nexport type { SignedEnvelope, SigningKeyPair } from \"./shared/lib/signing\";\nexport {\n generateSigningKeyPair,\n PUBLIC_KEY_BYTES as SIGNING_PUBLIC_KEY_BYTES,\n SECRET_KEY_BYTES as SIGNING_SECRET_KEY_BYTES,\n SIGNATURE_BYTES as SIGNING_SIGNATURE_BYTES,\n SigningError,\n sign,\n signingKeyPairFromSecret,\n verify,\n} from \"./shared/lib/signing\";\n",
|
|
12
|
+
"/**\n * @fairfox/polly/mesh — mesh-networked CRDT state with end-to-end encryption.\n *\n * Wraps the peer-first primitives with WebRTC mesh transport and\n * sign-then-encrypt envelopes. This subpath export isolates the\n * @automerge/* and tweetnacl dependency trees so consumers who only\n * use $sharedState / $syncedState / $resource never pull them in.\n *\n * @example\n * ```typescript\n * import { $meshState, configureMeshState } from \"@fairfox/polly/mesh\";\n * ```\n */\n\n// Eagerly initialise Automerge's WASM runtime. Must come before anything\n// that touches @automerge/automerge-repo/slim.\nimport \"./shared/lib/wasm-init\";\n\nexport { IndexedDBBlobCache, MemoryBlobCache } from \"./shared/lib/blob-cache\";\nexport type { BlobRef, CreateBlobRefArgs } from \"./shared/lib/blob-ref\";\nexport { computeBlobHash, createBlobRef, isBlobRef } from \"./shared/lib/blob-ref\";\n// Blob store\nexport type {\n BlobCache,\n BlobProgressCallback,\n BlobProgressEvent,\n BlobStore,\n BlobStoreOptions,\n BlobTransferOptions,\n} from \"./shared/lib/blob-store\";\nexport { createBlobStore } from \"./shared/lib/blob-store-impl\";\nexport type {\n EncryptedEnvelope,\n SealedBytes,\n} from \"./shared/lib/encryption\";\nexport {\n decrypt,\n decryptOrThrow,\n EncryptionError,\n encrypt,\n generateDocumentKey,\n KEY_BYTES as ENCRYPTION_KEY_BYTES,\n NONCE_BYTES as ENCRYPTION_NONCE_BYTES,\n TAG_BYTES as ENCRYPTION_TAG_BYTES,\n} from \"./shared/lib/encryption\";\n// Keyring storage (runtime-agnostic)\nexport type { KeyringStorage } from \"./shared/lib/keyring-storage\";\nexport {\n deserialiseKeyring,\n memoryKeyringStorage,\n serialiseKeyring,\n} from \"./shared/lib/keyring-storage\";\n// Mesh client factory\nexport type {\n CreateMeshClientOptions,\n MeshClient,\n MeshClientHandleSnapshot,\n MeshClientPeerStateSnapshot,\n MeshStateLazyWrapperDocIdDuplicate,\n MeshStateModuleDiagnostics,\n} from \"./shared/lib/mesh-client\";\nexport { createMeshClient } from \"./shared/lib/mesh-client\";\n// Mesh network adapter (sign-then-encrypt envelopes over any base adapter)\nexport type {\n MeshKeyring,\n MeshNetworkAdapterOptions,\n} from \"./shared/lib/mesh-network-adapter\";\nexport {\n DEFAULT_MESH_KEY_ID,\n MeshNetworkAdapter,\n} from \"./shared/lib/mesh-network-adapter\";\n// Mesh signaling client\nexport type {\n MeshSignalingClientOptions,\n SignalingMessage as MeshSignalingMessage,\n} from \"./shared/lib/mesh-signaling-client\";\nexport { MeshSignalingClient } from \"./shared/lib/mesh-signaling-client\";\nexport type {\n DocIdResolver,\n LazyWrapperExitReason,\n MeshStateLazyWrapperRecord,\n MeshStateLoadedRejectionBreadcrumb,\n MeshStateOptions,\n MeshStateStorageOpenError,\n} from \"./shared/lib/mesh-state\";\n// Mesh state wrappers\nexport {\n $meshCounter,\n $meshList,\n $meshState,\n $meshText,\n configureMeshState,\n deriveDocumentId,\n getDocIdResolver,\n getLastConfiguredRepoPeerId,\n getLastLoadedRejection,\n getLazyInvocations,\n getLazyReachedRepo,\n getLazyWrappers,\n getMeshStateModuleId,\n getRedirectDetector,\n getStorageOpenError,\n isMeshStateConfigured,\n MESH_STATE_MODULE_ID,\n registerDocIdResolver,\n registerRedirectDetector,\n resetMeshState,\n resolveDocumentId,\n wasMeshStateResolved,\n} from \"./shared/lib/mesh-state\";\n// Mesh WebRTC adapter\nexport type {\n HandleSyncSnapshot,\n InFlightSyncSnapshot,\n MeshWebRTCAdapterOptions,\n SlotInitiationDecision,\n SlotInitiationRejectionReason,\n SweepSnapshot,\n SyncHandshakeAttemptSnapshot,\n SyncProgressEvent,\n TransportSnapshot,\n} from \"./shared/lib/mesh-webrtc-adapter\";\nexport { DEFAULT_ICE_SERVERS, MeshWebRTCAdapter } from \"./shared/lib/mesh-webrtc-adapter\";\n// Pairing and revocation (depend on signing/encryption)\nexport type {\n CreatePairingTokenOptions,\n PairingToken,\n} from \"./shared/lib/pairing\";\nexport {\n applyPairingToken,\n createPairingToken,\n createPairingTokenWithFreshIdentity,\n DEFAULT_PAIRING_TTL_MS,\n decodePairingToken,\n encodePairingToken,\n isPairingTokenExpired,\n PAIRING_NONCE_BYTES,\n PAIRING_TOKEN_VERSION,\n PairingError,\n parsePairingToken,\n serialisePairingToken,\n} from \"./shared/lib/pairing\";\nexport type {\n CreateRevocationOptions,\n RevocationRecord,\n} from \"./shared/lib/revocation\";\nexport {\n applyRevocation,\n createRevocation,\n decodeRevocation,\n encodeRevocation,\n REVOCATION_MAGIC,\n REVOCATION_RECORD_VERSION,\n RevocationError,\n revokePeerLocally,\n} from \"./shared/lib/revocation\";\n// Cryptographic primitives (signing + encryption)\nexport type { SignedEnvelope, SigningKeyPair } from \"./shared/lib/signing\";\nexport {\n generateSigningKeyPair,\n PUBLIC_KEY_BYTES as SIGNING_PUBLIC_KEY_BYTES,\n SECRET_KEY_BYTES as SIGNING_SECRET_KEY_BYTES,\n SIGNATURE_BYTES as SIGNING_SIGNATURE_BYTES,\n SigningError,\n sign,\n signingKeyPairFromSecret,\n verify,\n} from \"./shared/lib/signing\";\n",
|
|
13
13
|
"/**\n * keyring-storage — persistence abstraction for {@link MeshKeyring}.\n *\n * The keyring itself is a plain structural object of `Map`s, `Set`s, and a\n * signing keypair; it is deliberately not coupled to any persistence layer.\n * This module defines a storage interface that applications implement once\n * for their runtime (IndexedDB, the filesystem, a keychain, a secret\n * manager, whatever) and wire into {@link createMeshClient} via its\n * `keyring.storage` option.\n *\n * A canonical JSON-with-base64 serialisation is provided by\n * {@link serialiseKeyring} and {@link deserialiseKeyring}. It is inspectable\n * by humans, survives manual edits, and round-trips every field of the\n * keyring. Storage implementations that write plain strings (files,\n * localStorage, `kv` stores) can lean on these helpers; storage\n * implementations that persist structured data (IndexedDB, a keychain API)\n * can serialise differently if they prefer.\n */\n\nimport type { MeshKeyring } from \"./mesh-network-adapter\";\nimport type { SigningKeyPair } from \"./signing\";\n\n/**\n * A load/save pair for a single {@link MeshKeyring}. Implementations are\n * free to choose where and how the keyring is stored; the factory only\n * cares that `load()` returns the previously-saved keyring or `null`, and\n * that `save(keyring)` durably persists it.\n */\nexport interface KeyringStorage {\n /** Load the previously-saved keyring, or return `null` if none exists.\n * Implementations may throw for truly exceptional conditions (disk\n * errors, permission failures); a missing keyring is not exceptional. */\n load(): Promise<MeshKeyring | null>;\n /** Durably persist the keyring. Implementations should atomically replace\n * any existing stored value; partial writes must not leave the store in\n * an inconsistent state. */\n save(keyring: MeshKeyring): Promise<void>;\n}\n\n/**\n * In-memory storage. Useful for tests, ephemeral tools, and the first-run\n * bootstrap path where the keyring only lives for the duration of the\n * process. Calling `save` holds the keyring in a closed-over variable;\n * `load` returns it on subsequent calls within the same process.\n */\nexport function memoryKeyringStorage(): KeyringStorage {\n let stored: MeshKeyring | null = null;\n return {\n load: async () => stored,\n save: async (keyring) => {\n stored = keyring;\n },\n };\n}\n\n// Canonical JSON+base64 serialisation\n\ninterface SerialisedKeyring {\n version: 1;\n identity: { publicKey: string; secretKey: string };\n knownPeers: Record<string, string>;\n documentKeys: Record<string, string>;\n revokedPeers: string[];\n revocationAuthority?: string[];\n}\n\n/**\n * Encode a {@link MeshKeyring} to a canonical JSON string. Every\n * `Uint8Array` field (identity keys, public keys, document keys) is\n * base64-encoded; `Map`s and `Set`s become plain objects and arrays. The\n * output is pretty-printed so a human operator can eyeball or hand-edit\n * the file on disk.\n */\nexport function serialiseKeyring(keyring: MeshKeyring): string {\n const payload: SerialisedKeyring = {\n version: 1,\n identity: {\n publicKey: bytesToBase64(keyring.identity.publicKey),\n secretKey: bytesToBase64(keyring.identity.secretKey),\n },\n knownPeers: mapToBase64Record(keyring.knownPeers),\n documentKeys: mapToBase64Record(keyring.documentKeys),\n revokedPeers: [...keyring.revokedPeers],\n };\n if (keyring.revocationAuthority && keyring.revocationAuthority.size > 0) {\n payload.revocationAuthority = [...keyring.revocationAuthority];\n }\n return JSON.stringify(payload, null, 2);\n}\n\n/**\n * Decode a keyring from the format produced by {@link serialiseKeyring}.\n * Throws with a descriptive message when the input is malformed, so\n * corrupt storage surfaces as an actionable error rather than a silent\n * downgrade.\n */\nexport function deserialiseKeyring(text: string): MeshKeyring {\n let raw: unknown;\n try {\n raw = JSON.parse(text);\n } catch (err) {\n throw new Error(`KeyringStorage: keyring payload is not valid JSON: ${(err as Error).message}`);\n }\n if (!raw || typeof raw !== \"object\") {\n throw new Error(\"KeyringStorage: keyring payload is not an object\");\n }\n const r = raw as unknown as Partial<SerialisedKeyring>;\n if (r.version !== 1) {\n throw new Error(`KeyringStorage: unsupported keyring version: ${String(r.version)}`);\n }\n if (!r.identity || typeof r.identity !== \"object\") {\n throw new Error(\"KeyringStorage: keyring payload is missing identity\");\n }\n const identity: SigningKeyPair = {\n publicKey: base64ToBytes(r.identity.publicKey),\n secretKey: base64ToBytes(r.identity.secretKey),\n };\n const keyring: MeshKeyring = {\n identity,\n knownPeers: base64RecordToMap(r.knownPeers ?? {}),\n documentKeys: base64RecordToMap(r.documentKeys ?? {}),\n revokedPeers: new Set(r.revokedPeers ?? []),\n };\n if (r.revocationAuthority && r.revocationAuthority.length > 0) {\n keyring.revocationAuthority = new Set(r.revocationAuthority);\n }\n return keyring;\n}\n\nfunction mapToBase64Record(map: Map<string, Uint8Array>): Record<string, string> {\n const out: Record<string, string> = {};\n for (const [key, value] of map) {\n out[key] = bytesToBase64(value);\n }\n return out;\n}\n\nfunction base64RecordToMap(record: Record<string, string>): Map<string, Uint8Array> {\n const out = new Map<string, Uint8Array>();\n for (const [key, value] of Object.entries(record)) {\n out.set(key, base64ToBytes(value));\n }\n return out;\n}\n\nfunction bytesToBase64(bytes: Uint8Array): string {\n // btoa is available in browsers, Node 16+, and Bun.\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nfunction base64ToBytes(b64: string): Uint8Array {\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return bytes;\n}\n",
|
|
14
14
|
"/**\n * mesh-client — first-class factory for constructing a Polly mesh client.\n *\n * The mesh transport stack has five pieces that have to be wired together:\n * a {@link MeshSignalingClient} talking to the relay server, a\n * {@link MeshWebRTCAdapter} that owns the per-peer RTCPeerConnections, a\n * {@link MeshNetworkAdapter} that signs and encrypts every message on the\n * way through, an Automerge `Repo` that drives sync, and a `MeshKeyring`\n * that holds the crypto material. Prior to this module, every consuming\n * application had to assemble the five pieces by hand — and in Node or\n * Bun had to monkey-patch `globalThis.WebSocket` / `globalThis.RTCPeerConnection`\n * because the lower-level primitives reached for those globals.\n *\n * `createMeshClient` takes options, hands back a `MeshClient`, and also\n * calls `configureMeshState(client.repo)` so `$meshState` works without\n * a second setup step. The WebSocket and RTCPeerConnection implementations\n * are injectable; defaults read from `globalThis` for browser ergonomics.\n * The companion `@fairfox/polly/mesh/node` subpath provides a CLI bootstrap\n * helper that wires werift (or `@roamhq/wrtc` if the consumer prefers) and\n * a file-backed keyring store.\n */\n\nimport { type PeerId, Repo, type StorageAdapterInterface } from \"@automerge/automerge-repo/slim\";\nimport type { KeyringStorage } from \"./keyring-storage\";\nimport { DEFAULT_MESH_KEY_ID, type MeshKeyring, MeshNetworkAdapter } from \"./mesh-network-adapter\";\nimport { MeshSignalingClient, type MeshSignalingClientOptions } from \"./mesh-signaling-client\";\nimport {\n configureMeshState,\n getLastConfiguredRepoPeerId,\n getLastLoadedRejection,\n getLazyInvocations,\n getLazyReachedRepo,\n getLazyWrappers,\n getMeshStateModuleId,\n getStorageOpenError,\n isMeshStateConfigured,\n type MeshStateLazyWrapperRecord,\n type MeshStateLoadedRejectionBreadcrumb,\n type MeshStateStorageOpenError,\n wasMeshStateResolved,\n} from \"./mesh-state\";\nimport { MeshWebRTCAdapter, type MeshWebRTCAdapterOptions } from \"./mesh-webrtc-adapter\";\n\n/** Options for {@link createMeshClient}. */\nexport interface CreateMeshClientOptions {\n /** Signalling-server configuration. `peerId` must be the same identity\n * this client's keyring was paired with on other peers. */\n signaling: {\n url: string;\n peerId: string;\n /** Optional WebSocket ctor override (e.g. `ws` on old Node). Defaults\n * to `globalThis.WebSocket`. */\n WebSocket?: MeshSignalingClientOptions[\"WebSocket\"];\n /** Forwarded error callback for diagnostic UI. */\n onError?: MeshSignalingClientOptions[\"onError\"];\n /** Forwarded hook for custom signalling frames. See\n * {@link MeshSignalingClientOptions.onCustomFrame} — consumers who\n * layer application protocols on the signalling socket receive\n * frames of unknown types here. */\n onCustomFrame?: MeshSignalingClientOptions[\"onCustomFrame\"];\n };\n /** WebRTC configuration. On browsers the defaults are fine; in Node or\n * Bun pass the `RTCPeerConnection` ctor from `werift` or `@roamhq/wrtc`. */\n rtc?: {\n RTCPeerConnection?: MeshWebRTCAdapterOptions[\"RTCPeerConnection\"];\n iceServers?: RTCIceServer[];\n /**\n * Async resolver for ICE servers, called once at connect time. Wins\n * over `iceServers` if both are set. The realistic pattern is a\n * consumer fetching short-lived TURN credentials from its own\n * backend before each session — for production WebRTC apps that\n * care about coverage, TURN is the only way two peers behind\n * symmetric NATs (cellular CGNAT, some corporate firewalls) can\n * exchange bytes at all.\n *\n * Common provider shapes:\n *\n * - **Self-hosted coturn**: fetch a HMAC-derived ephemeral\n * `username` / `credential` pair from your backend and return\n * `[{ urls: \"turn:turn.example.com\", username, credential }]`.\n * - **Cloudflare Calls**: hit `/v1/turn/keys/<id>/credentials/generate`\n * from a server route and pass the returned `iceServers` array\n * straight through.\n * - **Twilio NTS**: call the network-traversal-service endpoint\n * server-side and forward `data.iceServers`.\n * - **metered.ca / Xirsys / etc.**: same pattern — credentials live\n * server-side, the resolver is the integration point.\n *\n * Resolution happens once. ICE restart with refreshed credentials\n * is a separate concern that this hook does not yet cover; if your\n * deployment needs it, tear down and rebuild the mesh client when\n * the credential window closes.\n */\n iceCredentialResolver?: () => Promise<RTCIceServer[]>;\n /** Forward of {@link MeshWebRTCAdapterOptions.iceTransportPolicy}.\n * Set to `\"relay\"` to force every candidate pair through TURN; the\n * default leaves the underlying {@link RTCPeerConnection}\n * implementation's default in place. The\n * `examples/mesh-large-initial-sync-turn` harness uses `\"relay\"` to\n * exercise the polly#105 real-transport contract. */\n iceTransportPolicy?: MeshWebRTCAdapterOptions[\"iceTransportPolicy\"];\n /** Forward of {@link MeshWebRTCAdapterOptions.iceRelayEnforcement}.\n * Defaults to `true`. Set to `false` to bypass the polly#105\n * relay-only enforcement layer; the\n * `examples/mesh-large-initial-sync-turn` falsification path\n * (`POLLY_105_DISABLE_TURN_FIX=1`) does this to reproduce the\n * pre-#105 candidate-leak shape. Production callers should leave\n * this at the default. */\n iceRelayEnforcement?: MeshWebRTCAdapterOptions[\"iceRelayEnforcement\"];\n dataChannelLabel?: string;\n /** How often the mesh client re-evaluates whether to dial peers\n * already present in the signalling roster against the live\n * keyring. The sweep is what makes\n * `applyPairingToken(token, keyring)` calls performed *after* the\n * mesh client is up take effect on the WebRTC layer without\n * requiring the consumer to call\n * {@link MeshClient.refreshKnownPeers} by hand — see polly issue\n * #103 for the failure mode this closes. Defaults to 2000ms; set\n * to 0 to disable (the captured-set-only behaviour, kept for\n * migrations and for the pre-fix demonstration shape in\n * `examples/mesh-recovery-pair`). Forwarded straight to\n * {@link MeshWebRTCAdapterOptions.knownPeersRefreshIntervalMs}. */\n knownPeersRefreshIntervalMs?: MeshWebRTCAdapterOptions[\"knownPeersRefreshIntervalMs\"];\n /** Forward of {@link MeshWebRTCAdapterOptions.syncYieldEnabled}.\n * Defaults to `true`. The `examples/mesh-large-initial-sync`\n * example flips this to `false` when `POLLY_104_DISABLE_FIX=1` is\n * set, to demonstrate the pre-#104 tight-loop behaviour against\n * post-fix polly. Production callers should leave this at the\n * default. */\n syncYieldEnabled?: MeshWebRTCAdapterOptions[\"syncYieldEnabled\"];\n /** Forward of\n * {@link MeshWebRTCAdapterOptions.syncFragmentChunkSizeOverride}.\n * Production callers should leave this undefined. The\n * `examples/mesh-large-initial-sync` example passes 64 KiB when\n * `POLLY_104_DISABLE_FIX=1` to recreate the pre-#104\n * fragmentation bug. */\n syncFragmentChunkSizeOverride?: MeshWebRTCAdapterOptions[\"syncFragmentChunkSizeOverride\"];\n };\n /** The local peer's keyring — one of three shapes:\n *\n * - A {@link MeshKeyring} instance. The same object is used for the\n * lifetime of the mesh client; in-place mutations to its `knownPeers`\n * and `documentKeys` maps are visible to the next send and receive\n * (`MeshNetworkAdapter` re-reads the keyring on every message, so\n * `applyPairingToken` and friends take effect without a restart).\n *\n * - `{ storage }`: load the keyring once via the persistence adapter.\n * When `storage.load()` resolves to `null`, the factory throws with\n * a message pointing at the bootstrap helper in\n * `@fairfox/polly/mesh/node`; we deliberately do not generate an\n * identity silently.\n *\n * - `{ source }`: a synchronous function returning the current keyring.\n * Called by `MeshNetworkAdapter` on every send and every receive.\n * This is the shape that lets a long-lived mesh client recover from\n * keyring updates written by a *different* process — wire the\n * `source` to a cached object that a file watcher (or `readFileSync`\n * on each call) refreshes from disk, and a freshly-paired peer\n * becomes visible to the running adapter without any explicit\n * notification path. See polly issue #100. */\n keyring: MeshKeyring | { storage: KeyringStorage } | { source: () => MeshKeyring };\n /** Optional Automerge-Repo storage adapter. Applications that want\n * durable local state pass an IndexedDB adapter in browsers or a\n * filesystem adapter in Node; omitting it keeps the Repo in-memory. */\n repoStorage?: StorageAdapterInterface;\n /** When `false`, signs but does not encrypt. Defaults to `true` — the\n * full $meshState posture where the server is off the data path. */\n encryptionEnabled?: boolean;\n}\n\n/**\n * Resolve the ICE server list a mesh client will use. The resolver wins\n * over a static `iceServers` list because the realistic deployment shape\n * is a consumer fetching short-lived TURN credentials per session — the\n * resolver is the integration point and a static list alongside it would\n * silently mask a broken credential flow. Exported for unit tests; the\n * production call site is inside {@link createMeshClient}.\n */\nexport async function resolveIceServers(\n rtc: CreateMeshClientOptions[\"rtc\"]\n): Promise<RTCIceServer[] | undefined> {\n if (rtc?.iceCredentialResolver) {\n return rtc.iceCredentialResolver();\n }\n return rtc?.iceServers;\n}\n\n/** Build the per-handle entry the polly#107 snapshot enrichment emits.\n * Combines the Repo-side `state` (the canonical lifecycle) with the\n * adapter-side wire timestamps. Extracted from the snapshot mapper to\n * keep the per-peer iteration under the cognitive-complexity ceiling. */\nfunction buildHandleEntry(\n state: string,\n wire:\n | {\n lastSyncMessageOutAt: number | undefined;\n lastSyncMessageInAt: number | undefined;\n lastSyncMessageOutSize: number | undefined;\n lastSyncMessageOutType: string | undefined;\n }\n | undefined\n): MeshClientHandleSnapshot {\n return {\n state,\n announcedToPeer: wire?.lastSyncMessageOutAt !== undefined,\n lastSyncMessageOutAt: wire?.lastSyncMessageOutAt,\n lastSyncMessageInAt: wire?.lastSyncMessageInAt,\n lastSyncMessageOutSize: wire?.lastSyncMessageOutSize,\n lastSyncMessageOutType: wire?.lastSyncMessageOutType,\n };\n}\n\n/** Stringify a Repo handle's `state` value for the enriched snapshot.\n * Handles whose machine snapshot returns an object collapse to a\n * coerced string; in practice polly's $meshState always lands on a\n * leaf string. Returns `\"unknown\"` when the handle entry itself is\n * undefined. */\nfunction stringifyHandleState(handle: { state: unknown } | undefined): string {\n if (handle === undefined) return \"unknown\";\n return typeof handle.state === \"string\" ? handle.state : String(handle.state ?? \"unknown\");\n}\n\n/** Enrich one peer's slot view with the per-handle Repo×adapter merge\n * polly#107 item 7 specifies. Pulled out of the snapshot factory so the\n * outer factory stays under the cognitive-complexity ceiling and so\n * the per-iteration code is testable in isolation. */\nfunction enrichPeerSlot(\n peer: ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"peers\"][number],\n knownHandleIds: string[],\n repoHandles: Record<string, { state: unknown } | undefined>\n): MeshClientPeerStateSnapshot[\"peers\"][number] {\n if (!peer.slot) {\n return { ...peer, slot: undefined } as MeshClientPeerStateSnapshot[\"peers\"][number];\n }\n const enriched: Record<string, MeshClientHandleSnapshot> = {};\n for (const docId of knownHandleIds) {\n enriched[docId] = buildHandleEntry(\n stringifyHandleState(repoHandles[docId]),\n peer.slot.handles[docId]\n );\n }\n for (const docId of Object.keys(peer.slot.handles)) {\n if (enriched[docId]) continue;\n enriched[docId] = buildHandleEntry(\"unknown\", peer.slot.handles[docId]);\n }\n return { ...peer, slot: { ...peer.slot, handles: enriched } };\n}\n\n/** Lift the synchronizer's `reevaluateDocumentShare` method off a Repo\n * through a narrowed structural accessor.\n * `synchronizer.reevaluateDocumentShare` is `@hidden` on the Automerge\n * type, so we reach for it tolerantly — the worst case is the\n * polly#107 fix silently degrades to the pre-fix shape, which the\n * observability layer still surfaces clearly. */\nfunction getReevaluateDocumentShare(repo: Repo): (() => Promise<void>) | undefined {\n const sync = (\n repo as unknown as {\n synchronizer?: { reevaluateDocumentShare?: () => Promise<void> };\n }\n ).synchronizer;\n const fn = sync?.reevaluateDocumentShare;\n if (typeof fn !== \"function\" || sync === undefined) return undefined;\n return () => fn.call(sync);\n}\n\n/** Build the {@link MeshStateModuleDiagnostics} block surfaced on\n * every {@link MeshClient.getPeerStateSnapshot}. Reads the module\n * state in one place so both snapshot construction sites (the\n * pre-adapter early-out and the main path) stay identical. */\nfunction buildMeshStateModuleDiagnostics(): MeshStateModuleDiagnostics {\n const lazyWrappers = getLazyWrappers();\n return {\n moduleId: getMeshStateModuleId(),\n configured: isMeshStateConfigured(),\n lastConfiguredRepoPeerId: getLastConfiguredRepoPeerId(),\n wasResolved: wasMeshStateResolved(),\n lazyInvocations: getLazyInvocations(),\n lazyReachedRepo: getLazyReachedRepo(),\n lastLoadedRejection: getLastLoadedRejection(),\n storageOpenError: getStorageOpenError(),\n lazyWrappers,\n lazyWrapperDuplicateDocIds: findLazyWrapperDocIdDuplicates(lazyWrappers),\n };\n}\n\n/** Polly#107 post-v0.60 instrumentation. Walks the lazy-wrapper log\n * and groups records by `docId`; emits one entry per `docId` that\n * appears in more than one record. Surfaces the \"17 wrappers / 16\n * `repo.handles`\" off-by-one shape that the v0.60.0 single-tab\n * fingerprint flagged — typically two distinct `$mesh*` consumer\n * call sites whose logical keys hash to the same Automerge\n * `DocumentId`, or the same logical key invoked from two consumers\n * during pre-warm before either factory's `repo.import` committed\n * the handle. The snapshot can paste this verbatim; an empty array\n * means the wrapper-to-handle accounting is one-to-one. */\nexport interface MeshStateLazyWrapperDocIdDuplicate {\n /** The `DocumentId` that more than one factory invocation\n * resolved to. */\n docId: string;\n /** Distinct logical keys that derived to this same `DocumentId`.\n * Length 1 means the same key was registered twice; length > 1\n * means two different keys hashed to the same id (a SHA-512-prefix\n * collision in {@link deriveDocumentId}, vanishingly unlikely but\n * detected for completeness). */\n keys: string[];\n /** Total number of records in the lazy-wrapper log that resolved\n * to this `DocumentId`. Typically 2 for a same-key double-call. */\n recordCount: number;\n}\n\nfunction findLazyWrapperDocIdDuplicates(\n records: readonly MeshStateLazyWrapperRecord[]\n): MeshStateLazyWrapperDocIdDuplicate[] {\n const byDocId = new Map<string, { keys: Set<string>; count: number }>();\n for (const record of records) {\n let entry = byDocId.get(record.docId);\n if (!entry) {\n entry = { keys: new Set(), count: 0 };\n byDocId.set(record.docId, entry);\n }\n entry.keys.add(record.key);\n entry.count++;\n }\n const duplicates: MeshStateLazyWrapperDocIdDuplicate[] = [];\n for (const [docId, entry] of byDocId) {\n if (entry.count > 1) {\n duplicates.push({ docId, keys: [...entry.keys], recordCount: entry.count });\n }\n }\n return duplicates;\n}\n\n/** Install the polly#107 peer-candidate hook on a freshly-constructed\n * mesh stack. Hides the falsification-gate branch behind a single call\n * so the outer factory's complexity score stays under the ceiling. */\nfunction installPolly107SyncReevaluation(networkAdapter: MeshNetworkAdapter, repo: Repo): void {\n const disable = typeof process !== \"undefined\" && process.env?.[\"POLLY_107_DISABLE_FIX\"] === \"1\";\n if (disable) return;\n const reevaluate = getReevaluateDocumentShare(repo);\n if (!reevaluate) return;\n networkAdapter.on(\"peer-candidate\", () => {\n void reevaluate().catch(() => {\n // Synchronizer errors are observable through\n // getPeerStateSnapshot; suppressing here keeps a single bad doc\n // from taking down the whole peer-candidate path.\n });\n });\n}\n\n/** Per-peer per-handle enrichment polly#107 adds on top of the\n * {@link MeshWebRTCAdapter} snapshot. `state` and `announcedToPeer`\n * come from the Repo side (which the adapter cannot see); the wire\n * timestamps come from the adapter's per-slot bookkeeping. */\nexport interface MeshClientHandleSnapshot {\n /** The local handle's lifecycle state — `\"ready\"`, `\"loading\"`,\n * `\"unavailable\"`, etc. — as reported by the Repo. `\"unknown\"` is\n * stamped when the wire has seen a sync message for a documentId\n * the local repo hasn't constructed a handle for yet (a remote peer\n * announcing a doc we haven't touched). */\n state: string;\n /** True iff polly's network adapter has dispatched at least one\n * outbound sync message for this document to this peer. Equivalent\n * to `lastSyncMessageOutAt !== undefined` but named for the\n * diagnostic question polly#107 asks the snapshot to answer:\n * \"has Automerge's NetworkSubsystem told this peer about this\n * handle?\". The failing-shape this ticket reports is precisely\n * the case where every entry has `state: \"ready\"` and\n * `announcedToPeer: false`. */\n announcedToPeer: boolean;\n /** `performance.now()` of the most recent outbound sync message for\n * this document to this peer. `undefined` until first send. */\n lastSyncMessageOutAt: number | undefined;\n /** `performance.now()` of the most recent inbound message dispatched\n * upward for this document from this peer. `undefined` until first\n * receive. */\n lastSyncMessageInAt: number | undefined;\n /** Byte length of the most recent outbound message. */\n lastSyncMessageOutSize: number | undefined;\n /** Type field of the most recent outbound message — typically\n * `\"sync\"` after handshake, `\"request\"` while the local side is\n * asking. */\n lastSyncMessageOutType: string | undefined;\n}\n\n/** Polly#107 H5 diagnostics: surfaces the `mesh-state` module\n * instance identity so a single snapshot read tells the operator\n * whether the consumer's `$meshState` wrappers are resolving against\n * the same module instance `createMeshClient` configured. A mismatch\n * here IS the bundle-time module duplication bug. */\nexport interface MeshStateModuleDiagnostics {\n /** The id stamped at import time on the `mesh-state` module\n * instance THIS mesh client was constructed against. Compare to\n * the id seen at the `$meshState` call site (also importable as\n * `MESH_STATE_MODULE_ID` from `@fairfox/polly/mesh`). Two different\n * ids means the consumer is reaching a duplicated copy of\n * `mesh-state.ts` — wrappers register handles against a Repo this\n * mesh client never saw. */\n moduleId: string;\n /** `true` iff THIS module instance has a configured `defaultRepo`.\n * In the H5 scenario, this is `true` for the mesh-client-side\n * snapshot (because `createMeshClient` always calls\n * `configureMeshState` on the module it imports), but the same\n * field read from the consumer's `$meshState` call site would\n * be `false`. */\n configured: boolean;\n /** `peerId` of the most recent Repo wired through\n * `configureMeshState` against THIS module instance. Compared to\n * `client.repo.peerId` and the consumer's wrappers' resolved Repo\n * tells the full story. */\n lastConfiguredRepoPeerId: string | undefined;\n /** `true` if any `$meshState`-family wrapper has been called\n * against THIS module instance at any point. The polly#107 H5\n * fingerprint pair: `configured: true, wasResolved: false` on\n * the mesh-client-side snapshot (the wrappers never reached us);\n * the consumer's wrapper code reading the same field from THEIR\n * `$meshState` import would see `configured: false, wasResolved:\n * true` (they're using a module instance no mesh client ever\n * configured). */\n wasResolved: boolean;\n /** Polly#107 post-H5 instrumentation. Count of `$meshState`-family\n * lazy factory invocations since module load. Once the consumer's\n * pre-warm pass completes, this equals the number of distinct\n * `$mesh*` wrappers whose handles the loader actually tried to\n * resolve. Compared to {@link lazyReachedRepo}: gap = throws\n * before any Repo work. */\n lazyInvocations: number;\n /** Polly#107 post-H5 instrumentation. Count of factory invocations\n * that reached the Repo subsystem (`repo.handles[...]`, `repo.find`\n * or `repo.import`) before returning or throwing. If\n * {@link lazyInvocations} is N and this is N, every wrapper\n * touched the Repo — any missing handles are downstream of Repo\n * registration. If this is < N, the gap is the call site to\n * instrument next. */\n lazyReachedRepo: number;\n /** Polly#107 post-H5 instrumentation. Breadcrumb for the most\n * recent rejection from a `$meshState`-family `loaded` promise\n * (or its factory). Captured even when the consumer's wrapper\n * never awaits `loaded` and the rejection would otherwise vanish\n * silently. `undefined` means no rejection has escaped any\n * wrapper on THIS module instance since module load. */\n lastLoadedRejection: MeshStateLoadedRejectionBreadcrumb | undefined;\n /** Polly#107 post-v0.60 instrumentation. Populated when a storage\n * read inside `buildHandleFactory` exceeds the internal 5s\n * timeout — i.e. `cached.whenReady(...)` or\n * `repo.storageSubsystem.loadDoc(...)` hung. Names the operation,\n * the document id under attempt, the elapsed time, and a\n * pre-formatted message ready to paste into a ticket. The v0.60.0\n * fingerprint diagnosed \"factories hung mid-await, not throwing\"\n * indirectly; this field surfaces the same shape within seconds\n * and in one read. `undefined` means no storage timeout has\n * occurred since module load. */\n storageOpenError: MeshStateStorageOpenError | undefined;\n /** Polly#107 post-v0.59 instrumentation. Per-factory-invocation\n * structured log — one record per `$mesh*` wrapper's lazy handle\n * factory call, ring-buffered at 64 entries. Each row names the\n * exit path (`returned-cached`, `loaded-from-storage`,\n * `seeded-and-imported`, `threw`) and the synchronous peek at\n * `repo.handles[docId]` taken in the factory's `finally`. The\n * v0.59.0 fingerprint had `lazyInvocations === lazyReachedRepo`\n * and no `lastLoadedRejection`; this field disambiguates which\n * exit path each \"successful\" invocation actually took and\n * whether the Repo registered the handle in spite of the lack of\n * a thrown error. The smoking gun for H-Q1 is rows with\n * `exitReason: \"seeded-and-imported\"` and\n * `handleRegistered: false`. */\n lazyWrappers: MeshStateLazyWrapperRecord[];\n /** Polly#107 post-v0.60 instrumentation. One entry per\n * `DocumentId` that appears in more than one\n * {@link lazyWrappers} record — the shape behind the v0.60.0\n * single-tab fingerprint's \"17 wrappers / 16 `repo.handles`\"\n * off-by-one. Empty when wrapper-to-handle accounting is\n * one-to-one, populated when two factory invocations resolved to\n * the same `DocumentId` (most commonly the same logical key\n * registered from two consumer call sites during pre-warm). */\n lazyWrapperDuplicateDocIds: MeshStateLazyWrapperDocIdDuplicate[];\n}\n\n/** The mesh client's enriched per-peer state snapshot. Mirrors the\n * underlying {@link MeshWebRTCAdapter.getPeerStateSnapshot} shape but\n * replaces the slot's `handles` map with the Repo-enriched view\n * polly#107 adds. */\nexport interface MeshClientPeerStateSnapshot {\n localPeerId: string;\n knownPeerIds: string[];\n presentPeerIds: string[];\n sweep: ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"sweep\"];\n peers: Array<\n Omit<ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"peers\"][number], \"slot\"> & {\n slot:\n | undefined\n | (Omit<\n NonNullable<\n ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"peers\"][number][\"slot\"]\n >,\n \"handles\"\n > & {\n handles: Record<string, MeshClientHandleSnapshot>;\n });\n }\n >;\n /** polly#107 H5 diagnostics. See {@link MeshStateModuleDiagnostics}. */\n meshStateModule: MeshStateModuleDiagnostics;\n /** Count of handles known to the Repo this client owns. Compared\n * to the count the consumer's `$meshState` wrappers should have\n * registered: a mismatch (e.g. consumer pre-warmed 14 wrappers,\n * snapshot reports 1) confirms the H5 module-duplication\n * fingerprint without needing a separate read. */\n repoHandleCount: number;\n /** All handle ids the Repo this client owns currently caches.\n * Together with `repoHandleCount` and `meshStateModule.moduleId`,\n * this answers \"did the consumer's wrappers land their handles in\n * THIS Repo?\" in one read. */\n repoHandleIds: string[];\n}\n\n/** Handle returned by {@link createMeshClient}. */\nexport interface MeshClient {\n /** The Automerge Repo. `$meshState` has already been configured against\n * this repo, so primitives just work — but the repo is exposed in case\n * the application needs it directly (server-side cron, bulk exports,\n * migration tools). */\n repo: Repo;\n /** The current keyring. Reads through the same live source the network\n * adapter uses, so the value always reflects the latest snapshot —\n * including changes introduced after construction via in-place mutation\n * or a `{ source }` callback. Applications can inspect or mutate the\n * returned object (add authorised peers, apply revocations) and the\n * next mesh message sees the change. */\n readonly keyring: MeshKeyring;\n /** The signalling client. Exposed for applications that need to hook\n * lifecycle events or send custom signalling payloads. */\n signaling: MeshSignalingClient;\n /** The WebRTC network adapter. Exposed for advanced use (blob store\n * wiring, peer-connection introspection). */\n networkAdapter: MeshNetworkAdapter;\n /** The underlying WebRTC adapter wrapped by {@link networkAdapter}. */\n webrtcAdapter: MeshWebRTCAdapter;\n /** Re-evaluate every peer currently in the signalling roster and\n * dial the ones the keyring now authorises that this client has not\n * already dialled. Equivalent to\n * {@link MeshWebRTCAdapter.refreshKnownPeers}; the periodic sweep\n * inside the WebRTC adapter calls the same code path, so consumers\n * only need this helper when they want to skip the wait between\n * sweeps after applying a pair token. Polly issue #103 — the path\n * the sweep was added to close — is the canonical use case. */\n refreshKnownPeers(): void;\n /** Snapshot of the mesh client's per-peer state — keyring\n * membership, signalling presence, per-slot SDP / ICE /\n * connectionState / data-channel state, queued-send / queued-ICE\n * depth, and per-peer per-handle sync state (state, announcedToPeer,\n * last sync message in/out). Plain data, safe to log or render.\n * Exists so a consumer harness can answer \"is the mesh layer in a\n * known good state, AND has Automerge actually announced every\n * local handle to every connected peer\" without instrumenting polly\n * internals. Polly issue #103 item 7; polly#107 item 7 added the\n * per-handle enrichment that this method overlays on top of the\n * underlying {@link MeshWebRTCAdapter} snapshot. */\n getPeerStateSnapshot(): MeshClientPeerStateSnapshot;\n /** Refresh every active peer slot's transport-level summary —\n * selected ICE candidate pair, SCTP retransmission counters, last\n * data-channel error — and populate it into the next\n * {@link getPeerStateSnapshot}. Walks {@link RTCPeerConnection.getStats}\n * once per peer, so it isn't free; consumers that want continuous\n * visibility should call this on a polling cadence the cost can\n * absorb. Polly issue #105 item 7. */\n refreshTransportStats(): Promise<void>;\n /** Force the Automerge synchronizer to re-evaluate every (handle ×\n * peer) pair and `beginSync` for any pair that should be syncing\n * but isn't. Called automatically on `peer-candidate` post-#107;\n * exposed for explicit invocation by harnesses that need to assert\n * the fix is engaged or that want a deterministic checkpoint after\n * post-construction handle additions.\n *\n * Resolves once the synchronizer's internal `Promise.allSettled`\n * over per-doc reevaluations resolves. Idempotent: if every pair is\n * already syncing this is a no-op. Polly issue #107 item 7. */\n reevaluateAllSync(): Promise<void>;\n /** Close the signalling WebSocket, tear down every RTCPeerConnection,\n * and shut the Repo cleanly. Idempotent. */\n close(): Promise<void>;\n}\n\n/**\n * Construct a fully-wired mesh client. Resolves once the signalling\n * connection is open and the Repo is ready to mutate documents; WebRTC\n * peer connections negotiate asynchronously in the background.\n */\nexport async function createMeshClient(options: CreateMeshClientOptions): Promise<MeshClient> {\n const keyringSource = await resolveKeyringSource(options.keyring);\n const keyring = keyringSource();\n const encryptionEnabled = options.encryptionEnabled ?? true;\n\n // A mesh keyring must carry the per-Repo document key used by\n // MeshNetworkAdapter's encryption layer. We fail loud rather than\n // silently disable encryption when encryptionEnabled is true.\n if (encryptionEnabled && !keyring.documentKeys.has(DEFAULT_MESH_KEY_ID)) {\n throw new Error(\n `createMeshClient: encryption is enabled but the keyring has no document key for \"${DEFAULT_MESH_KEY_ID}\". Bootstrap or apply a pairing token that carries the document key before connecting.`\n );\n }\n\n // Seed the WebRTC adapter with the keyring's current `knownPeers`\n // snapshot AND wire it to the same live keyring source the crypto\n // layer uses. The snapshot is kept for the legacy captured-set path\n // (consumers wiring MeshWebRTCAdapter directly without a source) and\n // as a \"first sweep\" hint; the live source is what closes polly\n // issue #103, where a long-lived daemon's keyring gains entries\n // through `applyPairingToken` after the adapter is already up and\n // the captured Set would otherwise never learn about them. The\n // crypto layer (MeshNetworkAdapter) reads the same source on every\n // send/receive — see mesh-network-adapter.ts for the matching\n // contract.\n const knownPeerIds = [...keyring.knownPeers.keys()].filter(\n (id) => id !== options.signaling.peerId\n );\n\n // The resolver wins when both are set. Failure here surfaces as a\n // create-time rejection rather than a silent fall-through to STUN-only,\n // which would mask broken credential plumbing until a real symmetric\n // NAT user reported \"can't connect\".\n const resolvedIceServers = await resolveIceServers(options.rtc);\n\n const webrtcAdapterOptions: MeshWebRTCAdapterOptions = {\n signaling: undefined as unknown as MeshSignalingClient, // wired after signaling construction\n peerId: options.signaling.peerId,\n knownPeerIds,\n keyringSource,\n ...(resolvedIceServers !== undefined && { iceServers: resolvedIceServers }),\n ...(options.rtc?.iceTransportPolicy !== undefined && {\n iceTransportPolicy: options.rtc.iceTransportPolicy,\n }),\n ...(options.rtc?.iceRelayEnforcement !== undefined && {\n iceRelayEnforcement: options.rtc.iceRelayEnforcement,\n }),\n ...(options.rtc?.dataChannelLabel !== undefined && {\n dataChannelLabel: options.rtc.dataChannelLabel,\n }),\n ...(options.rtc?.RTCPeerConnection !== undefined && {\n RTCPeerConnection: options.rtc.RTCPeerConnection,\n }),\n ...(options.rtc?.knownPeersRefreshIntervalMs !== undefined && {\n knownPeersRefreshIntervalMs: options.rtc.knownPeersRefreshIntervalMs,\n }),\n ...(options.rtc?.syncYieldEnabled !== undefined && {\n syncYieldEnabled: options.rtc.syncYieldEnabled,\n }),\n ...(options.rtc?.syncFragmentChunkSizeOverride !== undefined && {\n syncFragmentChunkSizeOverride: options.rtc.syncFragmentChunkSizeOverride,\n }),\n };\n\n // The signalling client needs a handleSignal callback, but that callback\n // lives on the WebRTC adapter — which itself wants a reference to the\n // signalling client for sending answers. Break the cycle by letting the\n // signalling client's callbacks reach the adapter through a closure\n // over `webrtcAdapter`, which is assigned immediately below. The same\n // closure pattern wires the peer-discovery callbacks\n // (onPeersPresent / onPeerJoined / onPeerLeft) through to the adapter's\n // dispatch methods.\n let webrtcAdapter: MeshWebRTCAdapter | undefined;\n const signaling = new MeshSignalingClient({\n url: options.signaling.url,\n peerId: options.signaling.peerId,\n ...(options.signaling.WebSocket !== undefined && { WebSocket: options.signaling.WebSocket }),\n ...(options.signaling.onError !== undefined && { onError: options.signaling.onError }),\n ...(options.signaling.onCustomFrame !== undefined && {\n onCustomFrame: options.signaling.onCustomFrame,\n }),\n onSignal: (fromPeerId, payload) => {\n webrtcAdapter?.handleSignal(fromPeerId, payload);\n },\n onPeersPresent: (peerIds) => {\n webrtcAdapter?.handlePeersPresent(peerIds);\n },\n onPeerJoined: (peerId) => {\n webrtcAdapter?.handlePeerJoined(peerId);\n },\n onPeerLeft: (peerId) => {\n webrtcAdapter?.handlePeerLeft(peerId);\n },\n });\n\n webrtcAdapterOptions.signaling = signaling;\n webrtcAdapter = new MeshWebRTCAdapter(webrtcAdapterOptions);\n\n const networkAdapter = new MeshNetworkAdapter({\n base: webrtcAdapter,\n keyringSource,\n encryptionEnabled,\n });\n\n // The Repo's peerId MUST match the mesh peer id we signed the keyring\n // against. Automerge would otherwise auto-generate a random \"peer-xxxxx\"\n // identifier, and `MeshNetworkAdapter`'s outgoing envelope would carry\n // that auto-id as its `senderId` — a value the remote keyring has never\n // seen and cannot look up in `knownPeers`. Every message would then fail\n // signature verification silently, and no `$meshState` sync would ever\n // apply.\n const repo = new Repo({\n network: [networkAdapter],\n peerId: options.signaling.peerId as unknown as PeerId,\n ...(options.repoStorage !== undefined && { storage: options.repoStorage }),\n });\n\n configureMeshState(repo);\n\n // polly#107 — on peer-candidate, force the synchronizer to\n // re-evaluate every (handle × peer) pair so any handle whose\n // `addDocument`/`addPeer` ordering race left it un-synced for this\n // peer gets `beginSync` invoked synchronously after the peer is\n // registered. The default Automerge flow IS supposed to handle this\n // through the `addPeer`-iterates-docSynchronizers and\n // `addDocument`-iterates-#peers cross-paths, but the polly#107\n // failing-shape evidence (fourteen pre-warmed handles, peer-candidate\n // fires, firstOutboundSendAt stays `(none)`) shows that path leaves\n // a gap in the realistic case. `reevaluateDocumentShare` walks every\n // docSynchronizer and starts a `beginSync` for every peer that\n // should be syncing but isn't — idempotent for handles that already\n // started, load-bearing for handles that didn't.\n //\n // Falsification gate (POLLY_107_DISABLE_FIX=1): skip the\n // re-evaluation so the pre-fix shape is preserved. The polly#107\n // example asserts that pre-fix-emulated runs produce the named\n // failure \"Automerge never invoked NetworkAdapter.send despite N\n // handles in repo and inbound messages received\", which is exactly\n // the production shape the ticket reports.\n installPolly107SyncReevaluation(networkAdapter, repo);\n\n await signaling.connect();\n\n return {\n repo,\n get keyring(): MeshKeyring {\n return keyringSource();\n },\n signaling,\n networkAdapter,\n webrtcAdapter,\n refreshKnownPeers: () => {\n webrtcAdapter?.refreshKnownPeers();\n },\n getPeerStateSnapshot: () => {\n // The closure-captured `webrtcAdapter` is assigned synchronously\n // before this object is returned, so the optional chain is\n // belt-and-braces against future construction reorderings.\n if (!webrtcAdapter) {\n return {\n localPeerId: options.signaling.peerId,\n knownPeerIds: [],\n presentPeerIds: [],\n sweep: {\n enabled: false,\n intervalMs: 0,\n runCount: 0,\n lastRunAt: undefined,\n },\n peers: [],\n meshStateModule: buildMeshStateModuleDiagnostics(),\n repoHandleCount: Object.keys(repo.handles).length,\n repoHandleIds: Object.keys(repo.handles),\n };\n }\n const base = webrtcAdapter.getPeerStateSnapshot() as ReturnType<\n MeshWebRTCAdapter[\"getPeerStateSnapshot\"]\n >;\n // Enrich the adapter view with Repo-side handle state so the\n // polly#107 item 7 contract is observable in one place. The\n // adapter sees only the wire path; the Repo's `handles` map\n // owns the `state` (ready / loading / unavailable) and is the\n // canonical answer to \"what handles does the local replica\n // hold?\". Combining them produces \"for each handle this replica\n // holds, has Automerge announced it to this peer yet?\". The\n // failure shape polly#107 reports is precisely the case where\n // every entry in `repo.handles` is `ready` and every\n // corresponding `slot.handles[docId].lastSyncMessageOutAt` is\n // `undefined`.\n const repoHandles = repo.handles as unknown as Record<string, { state: unknown } | undefined>;\n const knownHandleIds = Object.keys(repoHandles);\n const enrichedPeers: MeshClientPeerStateSnapshot[\"peers\"] = base.peers.map((peer) =>\n enrichPeerSlot(peer, knownHandleIds, repoHandles)\n );\n const out: MeshClientPeerStateSnapshot = {\n localPeerId: base.localPeerId,\n knownPeerIds: base.knownPeerIds,\n presentPeerIds: base.presentPeerIds,\n sweep: base.sweep,\n peers: enrichedPeers,\n meshStateModule: buildMeshStateModuleDiagnostics(),\n repoHandleCount: knownHandleIds.length,\n repoHandleIds: knownHandleIds,\n };\n return out;\n },\n reevaluateAllSync: async () => {\n const reevaluate = getReevaluateDocumentShare(repo);\n if (!reevaluate) return;\n await reevaluate();\n },\n refreshTransportStats: async () => {\n if (!webrtcAdapter) return;\n await webrtcAdapter.refreshAllTransportStats();\n },\n close: async () => {\n signaling.close();\n webrtcAdapter?.disconnect();\n await repo.shutdown();\n },\n };\n}\n\nasync function resolveKeyringSource(\n source: MeshKeyring | { storage: KeyringStorage } | { source: () => MeshKeyring }\n): Promise<() => MeshKeyring> {\n if (typeof source === \"object\" && source !== null && \"source\" in source) {\n return source.source;\n }\n if (\"storage\" in source) {\n const loaded = await source.storage.load();\n if (loaded === null) {\n throw new Error(\n \"createMeshClient: keyring storage returned null (no saved keyring). In a Node CLI, bootstrap with `bootstrapCliKeyring` from `@fairfox/polly/mesh/node`; in a browser, run your pairing flow first and save the keyring through the storage adapter before constructing the client.\"\n );\n }\n return () => loaded;\n }\n return () => source;\n}\n",
|
|
15
15
|
"/**\n * mesh-network-adapter — Phase 2 wrapping NetworkAdapter that adds Polly's\n * mesh-transport semantics on top of any underlying Automerge NetworkAdapter.\n *\n * The mesh transport's job is to make every message between peers signed\n * and encrypted before it reaches the wire. Rather than reimplementing the\n * Automerge sync protocol, this adapter takes a base adapter (in production\n * a real WebRTC or WebSocket adapter; in tests an in-memory loopback) and\n * applies the crypto envelope to every message that flows through.\n *\n * Outgoing path (Repo → wire):\n * 1. The Repo's NetworkSubsystem calls send(message) on this adapter.\n * 2. We serialise the message to bytes, encrypt them under the local\n * keyring's document key, sign the resulting blob with the local\n * identity's secret key, and pack the pair into a MeshFrame.\n * 3. We hand the MeshFrame off to the base adapter, which puts it on\n * whatever wire it owns.\n *\n * Incoming path (wire → Repo):\n * 1. The base adapter emits a 'message' event with bytes from the wire.\n * 2. We unpack the MeshFrame, look up the sender's public key in the\n * keyring, verify the signature, look up the document key, decrypt\n * the payload, and deserialise it back to the original message.\n * 3. We re-emit the 'message' event upward to the Repo's NetworkSubsystem\n * with the decrypted message.\n *\n * The keyring is an injected dependency. In production it's backed by\n * persistent storage and populated through the pairing flow. For tests it\n * is just a Map of publicly-known fixtures that both sides share.\n *\n * Caveat for the Phase 2 first cut: Automerge sync messages don't have a\n * stable \"what document does this belong to\" field at the wire level (the\n * documentId is part of the message contents). The mesh adapter therefore\n * uses a single per-Repo encryption key for now rather than per-document\n * keys, and stores the key once in the keyring under the well-known id\n * \"polly-mesh-default\". The plan describes per-document keys as the right\n * end state; that requires either parsing the message to extract the\n * documentId before encrypting (peeking inside the binary protocol) or\n * threading the document context through the network subsystem (which\n * needs upstream support). A follow-up will address this.\n */\n\nimport {\n type Message,\n NetworkAdapter,\n type PeerId,\n type PeerMetadata,\n} from \"@automerge/automerge-repo/slim\";\nimport {\n decodeEncryptedEnvelope,\n encodeEncryptedEnvelope,\n openEnvelope as openEncryptedEnvelope,\n sealEnvelope as sealEncryptedEnvelope,\n} from \"./encryption\";\nimport {\n decodeSignedEnvelope,\n encodeSignedEnvelope,\n openEnvelope as openSignedEnvelope,\n type SigningKeyPair,\n signEnvelope,\n} from \"./signing\";\n\n/** The well-known document id used for the Phase 2 first-cut single-key\n * encryption mode. See the file-level comment for the per-document key\n * follow-up. */\nexport const DEFAULT_MESH_KEY_ID = \"polly-mesh-default\";\n\n/**\n * A mesh keyring holds the local peer's signing identity, the public keys\n * of every peer the local node will accept messages from, the symmetric\n * encryption keys for documents the local node has access to, and the set\n * of peers whose keys have been revoked.\n */\nexport interface MeshKeyring {\n /** The local peer's signing keypair. The secret never leaves this\n * keyring; the public key is gossiped through the access set. */\n identity: SigningKeyPair;\n /** Map from peer id (string) to that peer's signing public key. The\n * mesh adapter rejects messages from peers not present in this map. */\n knownPeers: Map<string, Uint8Array>;\n /** Map from document key id (typically the documentId, or the well-known\n * default for the single-key first cut) to the symmetric encryption key. */\n documentKeys: Map<string, Uint8Array>;\n /** Set of peer ids whose keys have been revoked. The mesh adapter drops\n * incoming messages from any peer in this set, even if the peer is still\n * present in {@link knownPeers}. Revocation is applied via the revocation\n * module; the set is kept separate from knownPeers so that an application\n * can audit who was once authorised without losing the revocation record. */\n revokedPeers: Set<string>;\n /** Optional set of peer ids authorised to issue revocations. When present\n * and non-empty, `decodeRevocation` accepts a signed record only if the\n * issuer is in this set. When undefined or empty, any signed revocation\n * from a known peer is accepted (the Phase 2 first-cut default). This\n * field layers a \"who can revoke whom\" check on top of the signature\n * layer without breaking existing callers. */\n revocationAuthority?: Set<string>;\n}\n\n/**\n * Constructor options for {@link MeshNetworkAdapter}.\n */\nexport interface MeshNetworkAdapterOptions {\n /** The underlying NetworkAdapter that puts crypto-wrapped bytes on the\n * wire. In production this is a WebRTC or WebSocket adapter; in tests\n * it's an in-memory loopback. */\n base: NetworkAdapter;\n /** A synchronous function that returns the keyring to use for the next\n * send or receive. The adapter never caches the value across calls —\n * every {@link MeshNetworkAdapter#send | send} and every incoming\n * message asks the source for the current keyring, so callers that\n * mutate the in-memory keyring (e.g. `applyPairingToken`) or re-issue\n * it entirely (e.g. a long-lived CLI watching a disk-backed keyring\n * file that another process can rewrite) see their updates take effect\n * on the very next message without restarting the adapter.\n *\n * The function must be synchronous because Automerge's\n * `NetworkAdapter.send` is synchronous. Implementations that need to\n * read from disk should maintain a cached object — refreshed by a\n * file watcher, a manual reload, or `readFileSync` per call — and\n * return that cached object from the source. */\n keyringSource: () => MeshKeyring;\n /** When false, the adapter signs but does not encrypt. Outgoing messages\n * carry a signature envelope but the payload is plaintext; incoming\n * messages are verified against the sender's public key without a\n * decryption step. This mode is used by $peerState's `sign: true`\n * option, where the server must still be able to parse Automerge sync\n * messages. Defaults to true (encrypt + sign, the full $meshState\n * posture). */\n encryptionEnabled?: boolean;\n}\n\n/**\n * NetworkAdapter that wraps another adapter with Polly's mesh-transport\n * crypto envelope. Every outgoing message is encrypted then signed; every\n * incoming message is verified then decrypted before being forwarded to\n * the Repo's network subsystem.\n *\n * The adapter delegates lifecycle (connect, disconnect, isReady, peer\n * discovery) to the base adapter unchanged. Only the message body is\n * intercepted.\n */\nexport class MeshNetworkAdapter extends NetworkAdapter {\n readonly base: NetworkAdapter;\n readonly keyringSource: () => MeshKeyring;\n readonly encryptionEnabled: boolean;\n\n /** Read-only view of the current keyring. Each access calls\n * {@link MeshNetworkAdapterOptions.keyringSource}, so the value\n * reflects whatever mutations or swaps the caller has applied since\n * the last access — the same contract the send and receive paths\n * follow internally. */\n get keyring(): MeshKeyring {\n return this.keyringSource();\n }\n\n constructor(options: MeshNetworkAdapterOptions) {\n super();\n this.base = options.base;\n this.keyringSource = options.keyringSource;\n this.encryptionEnabled = options.encryptionEnabled ?? true;\n\n // Forward lifecycle and peer events from the base adapter.\n this.base.on(\"close\", () => this.emit(\"close\"));\n this.base.on(\"peer-candidate\", (payload) => this.emit(\"peer-candidate\", payload));\n this.base.on(\"peer-disconnected\", (payload) => this.emit(\"peer-disconnected\", payload));\n\n // Intercept incoming messages: the base adapter will surface them as\n // 'message' events with crypto-wrapped payloads. We unwrap and re-emit.\n this.base.on(\"message\", (rawMessage) => {\n const unwrapped = this.tryUnwrap(rawMessage);\n if (unwrapped) {\n this.emit(\"message\", unwrapped);\n }\n // Silently drop messages that fail verification or decryption. A\n // production adapter would surface this through a diagnostic channel\n // so the application could prompt the user; the Phase 2 first cut\n // logs through the standard \"drop unknown\" semantics of the network\n // subsystem.\n });\n }\n\n isReady(): boolean {\n return this.base.isReady();\n }\n\n whenReady(): Promise<void> {\n return this.base.whenReady();\n }\n\n connect(peerId: PeerId, peerMetadata?: PeerMetadata): void {\n this.peerId = peerId;\n if (peerMetadata !== undefined) {\n this.peerMetadata = peerMetadata;\n }\n this.base.connect(peerId, peerMetadata);\n }\n\n disconnect(): void {\n this.base.disconnect();\n }\n\n send(message: Message): void {\n const wrapped = this.wrap(message);\n this.base.send(wrapped);\n }\n\n /**\n * Wrap an outgoing Automerge message in an encrypt-then-sign envelope.\n * The wrapped payload is returned as a Message with the original sender\n * and target ids and the crypto blob in the `data` field.\n */\n private wrap(message: Message): Message {\n const keyring = this.keyringSource();\n const serialised = serialiseMessage(message);\n\n let payloadToSign: Uint8Array;\n if (this.encryptionEnabled) {\n const docKey = keyring.documentKeys.get(DEFAULT_MESH_KEY_ID);\n if (!docKey) {\n throw new Error(\n `MeshNetworkAdapter: missing document encryption key under id \"${DEFAULT_MESH_KEY_ID}\". Provision the key in the keyring before sending.`\n );\n }\n const encrypted = sealEncryptedEnvelope(serialised, DEFAULT_MESH_KEY_ID, docKey);\n payloadToSign = encodeEncryptedEnvelope(encrypted);\n } else {\n payloadToSign = serialised;\n }\n\n const signed = signEnvelope(payloadToSign, message.senderId, keyring.identity.secretKey);\n const signedBytes = encodeSignedEnvelope(signed);\n\n // Preserve `documentId` on the outer envelope so the base adapter\n // (MeshWebRTCAdapter) can stamp per-handle wire-level bookkeeping\n // without parsing the encrypted payload. The inner serialised\n // message also carries documentId, which the receiver uses after\n // crypto-unwrap; this outer copy is purely for the sender-side\n // observability path that polly#107 item 7 specifies. The signed\n // envelope still binds the inner copy, so the outer copy cannot\n // forge routing — a peer that mismatches the two is observable as\n // a divergent receive shape, not a routing exploit.\n const outer: Record<string, unknown> = {\n type: message.type,\n senderId: message.senderId,\n targetId: message.targetId,\n data: signedBytes,\n };\n if (\"documentId\" in message && (message as { documentId?: unknown }).documentId !== undefined) {\n outer[\"documentId\"] = (message as { documentId: unknown }).documentId;\n }\n return outer as unknown as Message;\n }\n\n /**\n * Try to unwrap an incoming crypto-wrapped message. Returns the original\n * Message on success, undefined on verification or decryption failure.\n */\n private tryUnwrap(message: Message): Message | undefined {\n if (!message.data) return undefined;\n\n let signed: ReturnType<typeof decodeSignedEnvelope>;\n try {\n signed = decodeSignedEnvelope(message.data);\n } catch {\n return undefined;\n }\n\n // Re-read the keyring on every incoming message. This is what makes a\n // post-construction peer addition take effect — if the caller has\n // mutated the keyring's `knownPeers` map (or swapped the keyring\n // object outright via a file watcher), the new entry is visible here\n // without any restart or explicit notification path. See issue #100.\n const keyring = this.keyringSource();\n\n // Drop messages from peers whose keys have been revoked, even if the\n // public key is still present in knownPeers. The revocation set is the\n // authoritative \"this peer is no longer trusted\" marker.\n if (keyring.revokedPeers.has(signed.senderId)) {\n return undefined;\n }\n\n const senderKey = keyring.knownPeers.get(signed.senderId);\n if (!senderKey) {\n return undefined;\n }\n\n let verifiedPayload: Uint8Array;\n try {\n verifiedPayload = openSignedEnvelope(signed, senderKey);\n } catch {\n return undefined;\n }\n\n if (!this.encryptionEnabled) {\n // Sign-only mode: the verified payload IS the serialised message.\n return deserialiseMessage(verifiedPayload);\n }\n\n // Full encrypt+sign mode: unwrap the encryption envelope.\n let encrypted: ReturnType<typeof decodeEncryptedEnvelope>;\n try {\n encrypted = decodeEncryptedEnvelope(verifiedPayload);\n } catch {\n return undefined;\n }\n\n const docKey = keyring.documentKeys.get(encrypted.documentId);\n if (!docKey) {\n return undefined;\n }\n\n let plaintext: Uint8Array;\n try {\n plaintext = openEncryptedEnvelope(encrypted, docKey);\n } catch {\n return undefined;\n }\n\n return deserialiseMessage(plaintext);\n }\n}\n\n// message serialisation\n\n/**\n * Serialise an Automerge sync message to a binary blob suitable for\n * encryption. The format is a length-prefixed JSON header (carrying the\n * non-binary fields) followed by the raw `data` bytes (which Automerge's\n * sync messages carry as Uint8Array). This avoids round-tripping the\n * binary payload through JSON, which would balloon its size.\n */\nfunction serialiseMessage(message: Message): Uint8Array {\n const headerObj: Record<string, unknown> = {\n type: message.type,\n senderId: message.senderId,\n targetId: message.targetId,\n };\n if (\"documentId\" in message && message.documentId !== undefined) {\n headerObj[\"documentId\"] = message.documentId;\n }\n if (\"count\" in message && message.count !== undefined) {\n headerObj[\"count\"] = message.count;\n }\n if (\"sessionId\" in message && message.sessionId !== undefined) {\n headerObj[\"sessionId\"] = message.sessionId;\n }\n const headerBytes = new TextEncoder().encode(JSON.stringify(headerObj));\n const dataBytes: Uint8Array =\n \"data\" in message && message.data instanceof Uint8Array ? message.data : new Uint8Array(0);\n\n const out = new Uint8Array(4 + headerBytes.length + dataBytes.length);\n const view = new DataView(out.buffer);\n view.setUint32(0, headerBytes.length, false);\n out.set(headerBytes, 4);\n out.set(dataBytes, 4 + headerBytes.length);\n return out;\n}\n\n/**\n * Inverse of {@link serialiseMessage}.\n */\nfunction deserialiseMessage(bytes: Uint8Array): Message {\n if (bytes.length < 4) {\n throw new Error(\"MeshNetworkAdapter: message too short to deserialise.\");\n }\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) {\n throw new Error(\"MeshNetworkAdapter: message header truncated.\");\n }\n const header = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + headerLen)));\n const data = bytes.slice(4 + headerLen);\n return { ...header, data } as unknown as Message;\n}\n",
|
|
16
16
|
"/**\n * signing — Ed25519 signing and verification for Polly's $meshState\n * primitive (Phase 2). Wraps tweetnacl with a small Polly-flavoured API\n * so the rest of the codebase never imports tweetnacl directly.\n *\n * Every operation that flows through a $meshState transport is signed by\n * the originating peer's private key before transmission and verified by\n * every receiving peer against a known public-key set before being applied.\n * This is the Byzantine-tolerance mechanism: a peer whose private key is\n * compromised can be revoked through a further signed operation, after\n * which honest peers reject anything signed by the revoked key.\n *\n * tweetnacl uses the Ed25519 curve. Public keys and signatures are 32 and\n * 64 bytes respectively, which keeps the per-op overhead small enough that\n * signing every Automerge sync message is feasible even on mobile.\n *\n * The shape of the wrapper:\n *\n * - {@link generateSigningKeyPair} produces a new Ed25519 keypair. The\n * private key never leaves the device that generated it; the public\n * key is gossiped through the access set.\n *\n * - {@link sign} produces a 64-byte detached signature over a payload.\n *\n * - {@link verify} checks a payload against a signature and a public\n * key. Returns boolean rather than throwing so call sites can handle\n * verification failure as a normal control-flow case.\n *\n * - {@link signEnvelope} and {@link openEnvelope} package payload + sender\n * id + signature into a single binary envelope, which is what the mesh\n * network adapter actually puts on the wire.\n */\n\nimport nacl from \"tweetnacl\";\n\n/** Length in bytes of an Ed25519 public key. */\nexport const PUBLIC_KEY_BYTES = 32;\n/** Length in bytes of an Ed25519 secret (private) key. */\nexport const SECRET_KEY_BYTES = 64;\n/** Length in bytes of an Ed25519 detached signature. */\nexport const SIGNATURE_BYTES = 64;\n\n/**\n * An Ed25519 keypair. The {@link publicKey} is safe to share with peers;\n * the {@link secretKey} must never leave the device.\n */\nexport interface SigningKeyPair {\n publicKey: Uint8Array;\n secretKey: Uint8Array;\n}\n\n/**\n * A signed envelope. The wire format is the concatenation of the sender id\n * length, the sender id bytes, the signature, and the payload. Callers\n * shouldn't rely on the exact layout — use {@link signEnvelope} and\n * {@link openEnvelope} to round-trip.\n */\nexport interface SignedEnvelope {\n /** Stable sender peer identifier (UTF-8 string). The receiving side uses\n * this to look up the sender's public key in the document's access set. */\n senderId: string;\n /** The original payload bytes, untouched. */\n payload: Uint8Array;\n /** 64-byte Ed25519 signature over the payload. */\n signature: Uint8Array;\n}\n\n/** Errors thrown by the signing subsystem. */\nexport class SigningError extends Error {\n readonly code:\n | \"invalid-secret-key\"\n | \"invalid-public-key\"\n | \"invalid-signature-length\"\n | \"envelope-malformed\";\n\n constructor(message: string, code: SigningError[\"code\"]) {\n super(message);\n this.name = \"SigningError\";\n this.code = code;\n }\n}\n\n/**\n * Generate a fresh Ed25519 keypair. Calls into tweetnacl's CSPRNG.\n */\nexport function generateSigningKeyPair(): SigningKeyPair {\n const pair = nacl.sign.keyPair();\n return {\n publicKey: pair.publicKey,\n secretKey: pair.secretKey,\n };\n}\n\n/**\n * Reconstruct a keypair from an existing 64-byte secret key. Useful for\n * loading keys from persistent storage. Throws if the key is the wrong size.\n */\nexport function signingKeyPairFromSecret(secretKey: Uint8Array): SigningKeyPair {\n if (secretKey.length !== SECRET_KEY_BYTES) {\n throw new SigningError(\n `Ed25519 secret key must be ${SECRET_KEY_BYTES} bytes, got ${secretKey.length}.`,\n \"invalid-secret-key\"\n );\n }\n const pair = nacl.sign.keyPair.fromSecretKey(secretKey);\n return {\n publicKey: pair.publicKey,\n secretKey: pair.secretKey,\n };\n}\n\n/**\n * Produce a 64-byte detached signature over the given payload using the\n * supplied secret key.\n */\nexport function sign(payload: Uint8Array, secretKey: Uint8Array): Uint8Array {\n if (secretKey.length !== SECRET_KEY_BYTES) {\n throw new SigningError(\n `Ed25519 secret key must be ${SECRET_KEY_BYTES} bytes, got ${secretKey.length}.`,\n \"invalid-secret-key\"\n );\n }\n return nacl.sign.detached(payload, secretKey);\n}\n\n/**\n * Verify a detached signature against a payload and a public key. Returns\n * true if the signature is valid, false otherwise. Wrong-length keys or\n * signatures throw {@link SigningError} so callers can distinguish a bad\n * signature from a misshapen input.\n */\nexport function verify(payload: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean {\n if (publicKey.length !== PUBLIC_KEY_BYTES) {\n throw new SigningError(\n `Ed25519 public key must be ${PUBLIC_KEY_BYTES} bytes, got ${publicKey.length}.`,\n \"invalid-public-key\"\n );\n }\n if (signature.length !== SIGNATURE_BYTES) {\n throw new SigningError(\n `Ed25519 signature must be ${SIGNATURE_BYTES} bytes, got ${signature.length}.`,\n \"invalid-signature-length\"\n );\n }\n return nacl.sign.detached.verify(payload, signature, publicKey);\n}\n\n/**\n * Sign a payload and pack it into a {@link SignedEnvelope} along with the\n * sender id. The mesh network adapter calls this on every outgoing message\n * before handing it to the transport.\n */\nexport function signEnvelope(\n payload: Uint8Array,\n senderId: string,\n secretKey: Uint8Array\n): SignedEnvelope {\n const signature = sign(payload, secretKey);\n return { senderId, payload, signature };\n}\n\n/**\n * Verify a {@link SignedEnvelope} against the sender's known public key.\n * Returns the inner payload on success, throws on failure. The mesh\n * network adapter calls this on every incoming message before forwarding\n * the payload to the underlying Automerge sync subsystem.\n */\nexport function openEnvelope(envelope: SignedEnvelope, publicKey: Uint8Array): Uint8Array {\n const ok = verify(envelope.payload, envelope.signature, publicKey);\n if (!ok) {\n throw new SigningError(\n `Signature verification failed for envelope from ${envelope.senderId}.`,\n \"envelope-malformed\"\n );\n }\n return envelope.payload;\n}\n\n/**\n * Serialise a {@link SignedEnvelope} to a single binary blob suitable for\n * transmission over a network adapter. Wire format:\n *\n * [4 bytes BE: senderId byte length]\n * [N bytes: senderId UTF-8]\n * [64 bytes: signature]\n * [remaining: payload]\n *\n * Callers should not depend on the exact bytes — they should round-trip\n * through {@link encodeSignedEnvelope} / {@link decodeSignedEnvelope}.\n */\nexport function encodeSignedEnvelope(envelope: SignedEnvelope): Uint8Array {\n const senderBytes = new TextEncoder().encode(envelope.senderId);\n const total = 4 + senderBytes.length + SIGNATURE_BYTES + envelope.payload.length;\n const out = new Uint8Array(total);\n const view = new DataView(out.buffer);\n view.setUint32(0, senderBytes.length, false);\n out.set(senderBytes, 4);\n out.set(envelope.signature, 4 + senderBytes.length);\n out.set(envelope.payload, 4 + senderBytes.length + SIGNATURE_BYTES);\n return out;\n}\n\n/**\n * Deserialise a binary envelope produced by {@link encodeSignedEnvelope}.\n * Throws on malformed input.\n */\nexport function decodeSignedEnvelope(bytes: Uint8Array): SignedEnvelope {\n if (bytes.length < 4 + SIGNATURE_BYTES) {\n throw new SigningError(\n `Envelope too short: ${bytes.length} bytes, need at least ${4 + SIGNATURE_BYTES}.`,\n \"envelope-malformed\"\n );\n }\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const senderLen = view.getUint32(0, false);\n if (bytes.length < 4 + senderLen + SIGNATURE_BYTES) {\n throw new SigningError(\n `Envelope truncated: declared sender length ${senderLen}, total ${bytes.length}.`,\n \"envelope-malformed\"\n );\n }\n const senderId = new TextDecoder().decode(bytes.subarray(4, 4 + senderLen));\n const signature = bytes.slice(4 + senderLen, 4 + senderLen + SIGNATURE_BYTES);\n const payload = bytes.slice(4 + senderLen + SIGNATURE_BYTES);\n return { senderId, payload, signature };\n}\n",
|
|
17
17
|
"/**\n * mesh-signaling-client — browser-side client for Polly's signalingServer\n * Elysia plugin. Connects to the signalling WebSocket, registers a peer\n * id, and relays SDP/ICE messages between local WebRTC connections and\n * remote peers.\n *\n * This module is the companion to {@link signalingServer} from the Elysia\n * plugin family. The server accepts \"join\" and \"signal\" messages; this\n * client produces them. The protocol matches: opening the connection,\n * sending a \"join\" with the local peer id, and then using sendSignal()\n * to forward SDP and ICE messages to specific target peers.\n *\n * Because this client is browser-only in its first incarnation — it\n * assumes the global `WebSocket` is available — it cannot be exercised\n * under bun:test the way the server-side plugin is. The first validation\n * of this code path is either a Playwright harness or a human running\n * the browser-side example that consumes it.\n */\n\n/** A signal message either sent to or received from the signalling server.\n * Matches the wire format produced by the Elysia signalingServer plugin. */\nexport interface SignalingMessage {\n type: \"join\" | \"signal\" | \"error\" | \"peers-present\" | \"peer-joined\" | \"peer-left\";\n peerId?: string;\n peerIds?: string[];\n targetPeerId?: string;\n payload?: unknown;\n reason?: \"unknown-target\" | \"not-joined\" | \"malformed\";\n}\n\n/** A frame whose `type` is outside the built-in {@link SignalingMessage}\n * vocabulary. Consumers who want to layer application protocols on the\n * signalling socket — pairing return tokens, presence pings, anything\n * else that benefits from sharing the existing connection and its\n * reconnect state — receive these through {@link MeshSignalingClientOptions.onCustomFrame}\n * and produce them through {@link MeshSignalingClient.sendCustom}. Polly\n * does not interpret the body; the signalling server routes it per its\n * own conventions. The field `type` is always present; everything else\n * is application-defined. */\nexport interface CustomSignalingFrame {\n type: string;\n [key: string]: unknown;\n}\n\n/** Options for constructing a {@link MeshSignalingClient}. */\nexport interface MeshSignalingClientOptions {\n /** The signalling server URL — unencrypted scheme for local dev, TLS-terminated for production. */\n url: string;\n /** The local peer id that this client will register with on join. */\n peerId: string;\n /** Callback invoked whenever a signal message from another peer arrives.\n * The receiver dispatches to the right PeerConnection based on the\n * `fromPeerId`. */\n onSignal: (fromPeerId: string, payload: unknown) => void;\n /** Optional callback invoked when the server returns an error (for\n * diagnostic UI or reconnection logic). */\n onError?: (reason: string, targetPeerId?: string) => void;\n /** Optional callback for the open and close lifecycle events. */\n onOpen?: () => void;\n onClose?: () => void;\n /** Optional callback invoked once, immediately after the server's\n * response to our `join`, listing every peer already joined at that\n * moment. Empty list when the lobby is otherwise empty. */\n onPeersPresent?: (peerIds: string[]) => void;\n /** Optional callback invoked each time a new peer joins the signalling\n * server after we have already joined. */\n onPeerJoined?: (peerId: string) => void;\n /** Optional callback invoked each time a joined peer's socket closes\n * (including graceful disconnect and abrupt drops detected by the\n * server). Fires at most once per departure. */\n onPeerLeft?: (peerId: string) => void;\n /** Optional callback invoked for any frame whose `type` is outside the\n * built-in {@link SignalingMessage} vocabulary. Consumers use this to\n * layer their own protocol on top of the signalling socket — pairing\n * return tokens, presence pings, anything else that benefits from\n * sharing the existing connection and its reconnect state. A frame\n * that arrives before the join handshake completes or that fails to\n * parse as JSON is dropped silently, as with the built-in types. */\n onCustomFrame?: (frame: CustomSignalingFrame) => void;\n /** WebSocket constructor. Defaults to `globalThis.WebSocket`. Inject a\n * different implementation (e.g. `ws` package's `WebSocket`) when running\n * in an environment without a native WebSocket global, or to use a custom\n * subclass for tests or instrumentation. */\n WebSocket?: typeof WebSocket;\n}\n\n/**\n * Thin wrapper around a WebSocket connection to a Polly signalling server.\n * Handles the join handshake, routes incoming signals to the supplied\n * callback, and exposes a {@link sendSignal} method for outgoing signals.\n *\n * This class is deliberately small. It has no opinion on the signal\n * payload shape (the wire carries it as `unknown`), so it can carry SDP\n * offers, SDP answers, ICE candidates, or any other message the\n * WebRTC adapter wants to exchange with peers.\n */\n/** Base delay (ms) between reconnect attempts. Doubles per attempt up\n * to {@link RECONNECT_MAX_DELAY_MS}. */\nconst RECONNECT_BASE_DELAY_MS = 250;\n/** Ceiling for the exponential backoff so a long outage does not leave\n * the client silent for minutes between probes. */\nconst RECONNECT_MAX_DELAY_MS = 30_000;\n\n/** Parse a raw frame from the socket into a record with a string `type`,\n * or `undefined` if the frame is unparseable or malformed. Extracted so\n * {@link MeshSignalingClient.dispatchFrame} stays below the linter's\n * cognitive-complexity ceiling. */\nfunction parseFrame(raw: unknown): Record<string, unknown> | undefined {\n let parsed: unknown;\n try {\n parsed = typeof raw === \"string\" ? JSON.parse(raw) : raw;\n } catch {\n return undefined;\n }\n if (typeof parsed !== \"object\" || parsed === null) {\n return undefined;\n }\n const record = parsed as unknown as Record<string, unknown>;\n if (typeof record[\"type\"] !== \"string\") {\n return undefined;\n }\n return record;\n}\n\nexport class MeshSignalingClient {\n readonly url: string;\n readonly peerId: string;\n private readonly onSignal: (fromPeerId: string, payload: unknown) => void;\n private readonly onError?: (reason: string, targetPeerId?: string) => void;\n private readonly onOpen?: () => void;\n private readonly onClose?: () => void;\n private readonly onPeersPresent?: (peerIds: string[]) => void;\n private readonly onPeerJoined?: (peerId: string) => void;\n private readonly onPeerLeft?: (peerId: string) => void;\n private readonly onCustomFrame?: (frame: CustomSignalingFrame) => void;\n private socket: WebSocket | undefined;\n private joined = false;\n private stopping = false;\n private reconnectTimer: ReturnType<typeof setTimeout> | undefined;\n private readonly WebSocketCtor: typeof WebSocket;\n\n constructor(options: MeshSignalingClientOptions) {\n this.url = options.url;\n this.peerId = options.peerId;\n this.onSignal = options.onSignal;\n if (options.onError !== undefined) this.onError = options.onError;\n if (options.onOpen !== undefined) this.onOpen = options.onOpen;\n if (options.onClose !== undefined) this.onClose = options.onClose;\n if (options.onPeersPresent !== undefined) this.onPeersPresent = options.onPeersPresent;\n if (options.onPeerJoined !== undefined) this.onPeerJoined = options.onPeerJoined;\n if (options.onPeerLeft !== undefined) this.onPeerLeft = options.onPeerLeft;\n if (options.onCustomFrame !== undefined) this.onCustomFrame = options.onCustomFrame;\n const WS = options.WebSocket ?? globalThis.WebSocket;\n if (typeof WS !== \"function\") {\n throw new Error(\n \"MeshSignalingClient: no WebSocket implementation found. Pass one via options.WebSocket, or run in an environment where `globalThis.WebSocket` exists (Node 21+, Bun, browsers).\"\n );\n }\n this.WebSocketCtor = WS;\n }\n\n /**\n * Open the WebSocket and send the join message. Resolves once the\n * connection is open; callers should not send signals before this\n * promise resolves.\n */\n async connect(): Promise<void> {\n this.stopping = false;\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = undefined;\n }\n return new Promise((resolve, reject) => {\n const ws = new this.WebSocketCtor(this.url);\n this.socket = ws;\n let settled = false;\n\n ws.addEventListener(\"open\", () => {\n // Send the join message as the first frame. The server registers\n // the peer id and uses it as the authenticated sender for all\n // subsequent signals on this connection.\n ws.send(JSON.stringify({ type: \"join\", peerId: this.peerId } satisfies SignalingMessage));\n this.joined = true;\n this.onOpen?.();\n if (!settled) {\n settled = true;\n resolve();\n }\n });\n\n ws.addEventListener(\"message\", (event) => {\n this.dispatchFrame(event.data);\n });\n\n ws.addEventListener(\"error\", (err) => {\n // Only the initial connect rejects here. Post-open errors route\n // through the close handler's reconnect path.\n if (!settled) {\n settled = true;\n reject(err);\n }\n });\n\n ws.addEventListener(\"close\", () => {\n const wasOpen = this.joined;\n this.joined = false;\n this.onClose?.();\n // If the caller asked to stop, respect it. Otherwise a close on\n // an established connection — or a close that preempted `open`\n // — kicks off the reconnect loop.\n if (!this.stopping && wasOpen) {\n this.scheduleReconnect(0);\n }\n });\n });\n }\n\n /** Schedule the next reconnect attempt with exponential backoff. */\n private scheduleReconnect(attempt: number): void {\n if (this.stopping) return;\n const delay = Math.min(RECONNECT_MAX_DELAY_MS, RECONNECT_BASE_DELAY_MS * 2 ** attempt);\n this.reconnectTimer = setTimeout(() => {\n this.reconnectTimer = undefined;\n if (this.stopping) return;\n void this.connect().catch(() => {\n this.scheduleReconnect(attempt + 1);\n });\n }, delay);\n }\n\n /**\n * Parse and route an incoming frame. Extracted from the open/message\n * closure in {@link connect} so the discriminated-union switch stays\n * below the linter's cognitive-complexity ceiling.\n */\n private dispatchFrame(raw: unknown): void {\n const record = parseFrame(raw);\n if (record === undefined) {\n return;\n }\n const handler = this.builtInHandler(record[\"type\"]);\n if (handler !== undefined) {\n handler(record);\n return;\n }\n // Unknown types route to the custom-frame handler, which consumers\n // use to layer application protocols on the shared socket. Without\n // a handler the frame is silently dropped, preserving the old\n // behaviour byte-for-byte.\n this.onCustomFrame?.(record as unknown as CustomSignalingFrame);\n }\n\n private builtInHandler(type: unknown): ((record: Record<string, unknown>) => void) | undefined {\n if (type === \"signal\") {\n return (record) => {\n if (typeof record[\"peerId\"] === \"string\") {\n this.onSignal(record[\"peerId\"], record[\"payload\"]);\n }\n };\n }\n if (type === \"peers-present\") {\n return (record) => {\n if (Array.isArray(record[\"peerIds\"])) {\n this.onPeersPresent?.(record[\"peerIds\"] as unknown as string[]);\n }\n };\n }\n if (type === \"peer-joined\") {\n return (record) => {\n if (typeof record[\"peerId\"] === \"string\") {\n this.onPeerJoined?.(record[\"peerId\"]);\n }\n };\n }\n if (type === \"peer-left\") {\n return (record) => {\n if (typeof record[\"peerId\"] === \"string\") {\n this.onPeerLeft?.(record[\"peerId\"]);\n }\n };\n }\n if (type === \"error\") {\n return (record) => {\n if (typeof record[\"reason\"] !== \"string\") {\n return;\n }\n const targetPeerId =\n typeof record[\"targetPeerId\"] === \"string\" ? record[\"targetPeerId\"] : undefined;\n this.onError?.(record[\"reason\"], targetPeerId);\n };\n }\n return undefined;\n }\n\n /**\n * Send a signal to another peer via the signalling server. The server\n * validates the sender (replacing the claimed peerId with the\n * authenticated join id) and routes to the target. Returns true if\n * the message was sent, false if the connection is not open.\n */\n sendSignal(targetPeerId: string, payload: unknown): boolean {\n if (!this.socket || this.socket.readyState !== this.WebSocketCtor.OPEN || !this.joined) {\n return false;\n }\n const msg: SignalingMessage = {\n type: \"signal\",\n peerId: this.peerId,\n targetPeerId,\n payload,\n };\n this.socket.send(JSON.stringify(msg));\n return true;\n }\n\n /**\n * Send a custom frame over the signalling socket. The frame is serialised\n * as `{ type, ...payload }`. The server must be configured to route this\n * frame type (polly does not interpret it). Returns true if the message\n * was sent, false if the connection is not open.\n *\n * Intended for application-level protocols that want to share the\n * existing signalling connection — for example, a pairing flow that\n * delivers a reciprocal token from the scanner back to the issuer.\n */\n sendCustom(type: string, payload: Record<string, unknown> = {}): boolean {\n if (!this.socket || this.socket.readyState !== this.WebSocketCtor.OPEN || !this.joined) {\n return false;\n }\n this.socket.send(JSON.stringify({ ...payload, type }));\n return true;\n }\n\n /**\n * Close the underlying WebSocket connection. The server's close handler\n * will evict this peer from its routing table. Also cancels any\n * pending reconnect attempt so the client stays closed until the\n * caller reopens it with another {@link connect} call.\n */\n close(): void {\n this.stopping = true;\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = undefined;\n }\n this.socket?.close();\n this.socket = undefined;\n this.joined = false;\n }\n\n /** True if the signalling connection is open and joined. */\n get isConnected(): boolean {\n return this.joined && this.socket?.readyState === this.WebSocketCtor.OPEN;\n }\n}\n",
|
|
18
|
-
"/**\n * mesh-state — Phase 2 wrappers exposing $meshState, $meshText, $meshCounter,\n * and $meshList. These are the application-facing constructors for the\n * strongest resilience tier in RFC-041: every device is a full Automerge\n * replica, the server is *not on the data path at all*, and the application\n * functions with zero server uptime once direct peer connections are\n * established.\n *\n * Each primitive wraps the corresponding Phase 0 base ($crdtState, $crdtText,\n * $crdtCounter, $crdtList) with three additions:\n *\n * 1. The `primitive` label is hard-coded to \"meshState\" so the\n * primitive-registry collision detection knows which family the key\n * belongs to.\n *\n * 2. A handle factory that resolves the application's logical key to an\n * Automerge DocumentId by hashing the key into a deterministic,\n * content-addressable id. Every Repo backed by the same storage lands\n * on the same document without needing any extra state, and two devices\n * that have never met converge on the same id for the same key — which\n * also helps first-sync after pairing. (Prior to this change the factory\n * held an in-memory per-Repo `Map<string, DocumentId>`, which meant that\n * a lone-device reload — a very common onboarding state — produced a\n * fresh DocumentId for the same logical key, orphaned the document the\n * storage adapter still held on disk, and silently lost the user's data.)\n *\n * 3. Signing and encryption are mandatory, not optional. Where $peerState\n * accepts encrypt/sign as opt-in flags (currently throwing in Phase 1),\n * $meshState requires every operation to be signed by the originating\n * peer's key and encrypted under the document's symmetric key. The\n * mechanism lives in the wrapping MeshNetworkAdapter that the Repo\n * uses for transport.\n *\n * The Repo itself is supplied by the application via {@link configureMeshState}\n * or per-call via the `repo` option. In Phase 2 the production transport will\n * be a WebRTC mesh adapter wrapping signing+encryption around an in-process\n * RTCDataChannel; for tests and for the early Phase 2 cut, an in-memory\n * loopback adapter pair satisfies the same contract.\n */\n\nimport {\n Automerge,\n type BinaryDocumentId,\n type DocHandle,\n type DocumentId,\n interpretAsDocumentId,\n type Repo,\n} from \"@automerge/automerge-repo/slim\";\nimport nacl from \"tweetnacl\";\nimport type { Access } from \"./access\";\nimport {\n $crdtCounter,\n $crdtList,\n $crdtText,\n type CounterDoc,\n type ListDoc,\n type SpecialisedPrimitive,\n type TextDoc,\n} from \"./crdt-specialised\";\nimport { $crdtState, type CrdtPrimitive } from \"./crdt-state\";\nimport type { Migrations, VersionedDoc } from \"./schema-version\";\n\n/** Common option shape across all four $mesh* primitives. */\nexport interface MeshStateOptions {\n /** Override the default Repo for this primitive. The Repo must be\n * configured with the mesh transport (signing and encryption at the\n * network layer). */\n repo?: Repo;\n /** Schema version target for the application. Migrations run on load. */\n schemaVersion?: number;\n /** Migration table keyed by target version. Required if schemaVersion is set. */\n migrations?: Migrations;\n /** Declarative read/write access. The mesh transport compiles this into\n * a public-key set used by the signing layer to verify incoming ops. */\n access?: Access;\n}\n\nlet defaultRepo: Repo | undefined;\n\n/** Per-module-instance identifier stamped at import time. Two distinct\n * module instances (e.g. duplicated under a bundler that hoists\n * differently for the consumer's app code and `@fairfox/polly`'s own\n * imports) produce two different ids — and therefore two different\n * `defaultRepo` globals.\n *\n * The polly#107 fingerprint reading is exactly that: `configureMeshState`\n * runs on one module instance (the one `createMeshClient` imports);\n * the consumer's `$meshState(key, initial)` calls run on a different\n * instance, whose `defaultRepo` was never set. `resolveRepo` either\n * throws (caught silently by consumer wrappers) or returns the\n * unconfigured fallback — either way, the handle that should land\n * in the mesh `Repo` lands somewhere else (or nowhere), Automerge's\n * NetworkSubsystem never sees it, and inbound sync from the daemon\n * has nowhere to go.\n *\n * Exported so consumers can compare the id seen at the `$meshState`\n * call site against the id seen at the `configureMeshState` call\n * site. A mismatch IS the bundle-duplication bug; a match rules it\n * out. The id is also surfaced via {@link getMeshStateModuleId} on\n * every `MeshClient.getPeerStateSnapshot()` so a single one-line\n * read from the failing tab tells the operator which module\n * instance the snapshot is rendered from.\n *\n * Polly issue #107 H5 (mesh-state module duplication). */\nexport const MESH_STATE_MODULE_ID: string = `mesh-state-${\n typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\"\n ? crypto.randomUUID()\n : Math.random().toString(36).slice(2) + Date.now().toString(36)\n}`;\n\n/** Returns the per-module-instance id stamped at import time. See\n * {@link MESH_STATE_MODULE_ID}. */\nexport function getMeshStateModuleId(): string {\n return MESH_STATE_MODULE_ID;\n}\n\n/** Last id passed to {@link configureMeshState}; the Repo's\n * `peerId` is captured at configuration time so a snapshot read can\n * answer \"was this module instance ever wired to a mesh client at\n * all?\". `undefined` means `$meshState` is reachable but no\n * `createMeshClient` call against THIS module instance ever\n * succeeded — the polly#107 H5 fingerprint. */\nlet lastConfiguredRepoPeerId: string | undefined;\n\n/** Returns the `peerId` of the most recent Repo wired through\n * {@link configureMeshState} against this module instance. Surfaced\n * via the mesh client snapshot so a consumer can answer \"did\n * configureMeshState run on the same module instance the wrappers\n * are using\" in one read. */\nexport function getLastConfiguredRepoPeerId(): string | undefined {\n return lastConfiguredRepoPeerId;\n}\n\n/**\n * Set the default Repo that the $mesh* primitives use when no `repo` option\n * is supplied. Production code typically calls this once at application\n * startup with a Repo configured for the mesh transport. Tests call it\n * before each scenario with an in-memory or loopback Repo.\n */\nexport function configureMeshState(repo: Repo): void {\n defaultRepo = repo;\n lastConfiguredRepoPeerId = repo.peerId as unknown as string | undefined;\n}\n\n/**\n * Reset the mesh-state subsystem to its initial unconfigured state.\n * Intended for tests; production code should not call this.\n */\nexport function resetMeshState(): void {\n defaultRepo = undefined;\n lastConfiguredRepoPeerId = undefined;\n meshStateEverResolved = false;\n lazyInvocations = 0;\n lazyReachedRepo = 0;\n lastLoadedRejection = undefined;\n lastStorageOpenError = undefined;\n lazyWrappers = [];\n docIdResolver = undefined;\n}\n\n/** Returns whether this module instance's `defaultRepo` was ever\n * set via {@link configureMeshState}. Distinct from \"was set and\n * then reset\" — `resetMeshState` clears it. The polly#107 H5\n * fingerprint is `false` here on the module instance the consumer's\n * `$meshState` calls resolve from. */\nexport function isMeshStateConfigured(): boolean {\n return defaultRepo !== undefined;\n}\n\n/** Set to `true` once any `$meshState`-family wrapper has been\n * called against this module instance. Surfaced via\n * {@link wasMeshStateResolved} so a snapshot can answer \"did any\n * wrapper code ever reach THIS module\" — the polly#107 H5\n * fingerprint pair (`configured: false` AND `wasResolved: true`)\n * confirms the consumer's wrappers reach a different module instance\n * than `createMeshClient` configured. */\nlet meshStateEverResolved = false;\n\n/** Returns `true` once any `$meshState`-family wrapper has been\n * called against this module instance. See\n * {@link meshStateEverResolved}. */\nexport function wasMeshStateResolved(): boolean {\n return meshStateEverResolved;\n}\n\n/** Polly#107 post-H5 instrumentation. Counts every invocation of\n * the lazy handle factory built by {@link buildHandleFactory} — i.e.\n * every time a `$meshState`-family primitive's underlying loader is\n * actually called. Compared against {@link lazyReachedRepo}, the gap\n * pinpoints whether throws are happening between factory entry and\n * the first `repo.*` touch.\n *\n * The polly#107 v0.58.0 fingerprint from `fairfox.fly.dev` ruled out\n * H5: the wrappers ARE reaching the same module instance the mesh\n * client configured. But `repo.handles` still reported one handle of\n * the expected fourteen, so something is throwing between the\n * wrapper's factory entry and the registration. These two counters\n * answer \"where\" without another diagnostic build. */\nlet lazyInvocations = 0;\n\n/** Returns the count of factory invocations since module load. See\n * {@link lazyInvocations}. */\nexport function getLazyInvocations(): number {\n return lazyInvocations;\n}\n\n/** Polly#107 post-H5 instrumentation. Counts every time a factory\n * invocation made it as far as a `repo.find` / `repo.import` /\n * `repo.handles[...]` / `repo.storageSubsystem` call — i.e. the\n * factory reached the Repo subsystem before throwing or returning.\n * If {@link lazyInvocations} ticks but this counter does not, the\n * throw is happening before any Repo work; if both tick, the throw\n * (or the failing async path) is downstream of the Repo touch and\n * the next diagnostic should target Automerge's load/sync path. */\nlet lazyReachedRepo = 0;\n\n/** Returns the count of factory invocations that reached\n * `repo.find` / `repo.import` since module load. See\n * {@link lazyReachedRepo}. */\nexport function getLazyReachedRepo(): number {\n return lazyReachedRepo;\n}\n\n/** Polly#107 post-H5 instrumentation. Records the most recent\n * rejection (or synchronous throw) escaping the factory body — the\n * `loaded` promise's rejection path. Today an unawaited rejection\n * inside a consumer wrapper vanishes without trace; capturing the\n * message + stack here on the module and exposing it via the\n * snapshot leaves a breadcrumb the operator can read in one paste.\n *\n * The format is the JSON-safe `{ name, message, stack, at }` shape\n * so the snapshot read does not have to traffic in `Error`\n * instances. `at` is `Date.now()` at the time the rejection was\n * captured. */\nexport interface MeshStateLoadedRejectionBreadcrumb {\n name: string;\n message: string;\n stack: string | undefined;\n at: number;\n}\nlet lastLoadedRejection: MeshStateLoadedRejectionBreadcrumb | undefined;\n\n/** Returns the most recent rejection escaping a `$meshState` factory\n * invocation since module load. See {@link lastLoadedRejection}. */\nexport function getLastLoadedRejection(): MeshStateLoadedRejectionBreadcrumb | undefined {\n return lastLoadedRejection;\n}\n\nfunction recordLoadedRejection(thrown: unknown): void {\n const err =\n thrown instanceof Error\n ? thrown\n : new Error(typeof thrown === \"string\" ? thrown : String(thrown));\n lastLoadedRejection = {\n name: err.name,\n message: err.message,\n stack: err.stack,\n at: Date.now(),\n };\n}\n\n/** Polly#107 post-v0.60 instrumentation. Default timeout for the two\n * storage-touching `await`s inside {@link buildHandleFactory} — the\n * `cached.whenReady([\"ready\", \"unavailable\"])` wait on a placeholder\n * handle, and the `repo.storageSubsystem.loadDoc()` read against\n * whatever IndexedDB adapter the consumer wired. Healthy completions\n * for both are normally sub-millisecond; the v0.60.0 fingerprint\n * showed 17 factories hung indefinitely against a wedged\n * `fairfox-mesh` IndexedDB that only released when Chrome itself\n * restarted. Five seconds is two orders of magnitude beyond the\n * normal upper bound and well below the operator's \"have I been\n * staring at a stuck app\" threshold — long enough that healthy\n * boots never trip the timeout and short enough that the next\n * polly#107-shaped session sees a populated {@link storageOpenError}\n * within the first few seconds of inspection. */\nconst STORAGE_OP_TIMEOUT_MS = 5000;\n\n/** Polly#107 post-v0.60 instrumentation. Captured when a storage\n * read from inside a `$meshState`-family factory exceeds\n * {@link STORAGE_OP_TIMEOUT_MS}. Names which op timed out\n * (`whenReady`, `loadDoc`), the document id under attempt, the\n * elapsed time, and a single human-readable message that the\n * snapshot can paste verbatim into a ticket. The breadcrumb also\n * flows into {@link lastLoadedRejection} so the existing\n * \"did anything reject\" channel surfaces it alongside.\n *\n * The v0.60.0 fingerprint diagnosed \"factories hung mid-await,\n * not throwing\" indirectly — empty {@link lazyWrappers} beside\n * non-zero {@link lazyInvocations} / {@link lazyReachedRepo}. With\n * this field a future polly#107-shaped session reads\n * `storageOpenError.message: \"Polly $meshState: storage operation\n * 'loadDoc' on document '<id>' timed out after 5000ms\"` and jumps\n * directly to \"the storage layer is hung; clear site data,\"\n * skipping every other rung in the ladder. */\nexport interface MeshStateStorageOpenError {\n /** Which await in `buildHandleFactory` exceeded the timeout. */\n operation: \"whenReady\" | \"loadDoc\";\n /** Stringified Automerge `DocumentId` that was under attempt. */\n documentId: string;\n /** Time the operation was given before the timeout fired (ms). */\n timeoutMs: number;\n /** Wall-clock elapsed when the timeout fired (ms). Always\n * `>= timeoutMs`; a small overshoot is normal. */\n elapsedMs: number;\n /** Pre-formatted human-readable message. Identical to what\n * flows into {@link lastLoadedRejection}.message. */\n message: string;\n /** `Date.now()` at the moment the timeout fired. */\n at: number;\n}\n\nlet lastStorageOpenError: MeshStateStorageOpenError | undefined;\n\n/** Returns the most recent storage-operation timeout escaping a\n * `$meshState` factory invocation since module load. See\n * {@link lastStorageOpenError}. */\nexport function getStorageOpenError(): MeshStateStorageOpenError | undefined {\n return lastStorageOpenError;\n}\n\nfunction recordStorageOpenError(error: MeshStateStorageOpenError): void {\n lastStorageOpenError = error;\n}\n\n/** Polly#107 post-v0.60 instrumentation. Wraps the storage-touching\n * awaits inside {@link buildHandleFactory} with a hard timeout. On\n * timeout the rejection feeds both {@link lastLoadedRejection} (so\n * the existing channel surfaces it) and {@link lastStorageOpenError}\n * (so a snapshot read distinguishes \"storage layer wedged\" from\n * any other factory rejection in one field). */\nasync function withStorageTimeout<T>(\n operation: \"whenReady\" | \"loadDoc\",\n documentId: string,\n promise: Promise<T>,\n timeoutMs: number = STORAGE_OP_TIMEOUT_MS\n): Promise<T> {\n const start = Date.now();\n let timer: ReturnType<typeof setTimeout> | undefined;\n let timedOut = false;\n try {\n return await new Promise<T>((resolve, reject) => {\n timer = setTimeout(() => {\n timedOut = true;\n const elapsedMs = Date.now() - start;\n const message = `Polly $meshState: storage operation '${operation}' on document '${documentId}' timed out after ${timeoutMs}ms`;\n recordStorageOpenError({\n operation,\n documentId,\n timeoutMs,\n elapsedMs,\n message,\n at: Date.now(),\n });\n reject(new Error(message));\n }, timeoutMs);\n promise.then(\n (value) => {\n if (!timedOut) resolve(value);\n },\n (err) => {\n if (!timedOut) reject(err);\n }\n );\n });\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n}\n\n/** Polly#107 post-v0.59 instrumentation. Categorises the exit path\n * a `$mesh*` lazy factory invocation took. The v0.59.0 fingerprint\n * showed `lazyInvocations === lazyReachedRepo === 17` and no\n * `lastLoadedRejection` — every factory reached the first\n * `repo.handles[docId]` access, none rejected loudly, yet 16 of 17\n * handles never landed in `repo.handles`. Without per-exit detail\n * the snapshot cannot disambiguate \"factory returned the cached\n * handle\", \"factory hydrated from storage\", \"factory seeded and\n * imported a fresh doc\", or \"factory took the cached branch but\n * `whenReady` resolved to `unavailable` and the code fell\n * through\". Each exit reason corresponds to a single line in\n * {@link buildHandleFactory}; the snapshot record names it\n * verbatim so the operator can grep the source from one read. */\nexport type LazyWrapperExitReason =\n | \"returned-cached\"\n | \"loaded-from-storage\"\n | \"seeded-and-imported\"\n | \"threw\";\n\n/** Polly#107 post-v0.59 instrumentation. One record per factory\n * invocation, ring-buffered on the module and surfaced through\n * `MeshClient.getPeerStateSnapshot()` as\n * `meshStateModule.lazyWrappers`. The five fields together answer\n * the post-v0.59 question \"if every factory reached the Repo and\n * nothing rejected, why are 16 of 17 handles absent from\n * `repo.handles`?\" — `exitReason` names which of the three success\n * paths each took, `handleRegistered` is the synchronous peek at\n * `repo.handles[docId]` taken in the factory's `finally` (i.e. at\n * the moment of would-be return), and `handleState` is the\n * lifecycle state observed in that peek. A row where\n * `exitReason: \"seeded-and-imported\"` and `handleRegistered: false`\n * is the smoking gun for H-Q1 (`repo.import` returned without\n * registering); rows where `exitReason: \"returned-cached\"` repeats\n * for sixteen of seventeen distinct keys indicates collisions or\n * that the factory is being called multiple times against a\n * Repo-state-machine that has already filled the slot. */\nexport interface MeshStateLazyWrapperRecord {\n /** The logical application key passed to `$meshState(key, ...)`. */\n key: string;\n /** Stringified Automerge `DocumentId` derived from the key. */\n docId: string;\n /** `Date.now()` captured at the moment the factory was about to\n * return (or rethrow). */\n at: number;\n /** Which of the four exit paths the factory took. See\n * {@link LazyWrapperExitReason}. */\n exitReason: LazyWrapperExitReason;\n /** Snapshot peek `repo.handles[docId] !== undefined` taken in the\n * factory's `finally` clause. `true` means the local Repo\n * registered the handle; `false` means the factory thinks it\n * succeeded but the Repo does not hold the handle for this\n * documentId. The \"polly#107 post-v0.59\" smoking gun is\n * `exitReason: \"seeded-and-imported\", handleRegistered: false`. */\n handleRegistered: boolean;\n /** Lifecycle state of `repo.handles[docId]` at the synchronous\n * peek time. `undefined` when the handle was not registered.\n * Useful for distinguishing the \"registered as loading/unavailable\"\n * case from the \"registered as ready\" case. */\n handleState: string | undefined;\n /** Error message if `exitReason === \"threw\"`; `undefined`\n * otherwise. The full rejection still flows into\n * {@link lastLoadedRejection}; this field is the row-local\n * summary so the snapshot can show the failure cause inline. */\n errorMessage: string | undefined;\n}\n\n/** Bounded ring of {@link MeshStateLazyWrapperRecord} captured per\n * factory invocation. Cap kept low so the snapshot stays small\n * enough to paste into a ticket; fairfox's realistic pre-warm count\n * is in the high teens, so 64 leaves plenty of headroom and still\n * truncates pathological logs.\n *\n * Reading guide for a future polly#107-shaped session:\n *\n * - `lazyWrappers: 0 records, lazyInvocations > 0` means the\n * factories are **hung mid-await, not throwing** — the buffer\n * itself is fine; the records simply never reached the `finally`\n * that writes them. The v0.60.0 fingerprint took this exact shape\n * and turned out to be a wedged `fairfox-mesh` IndexedDB the\n * process could no longer evict; restarting Chrome cleared it.\n * `storageOpenError` (added v0.61.0) now populates within\n * {@link STORAGE_OP_TIMEOUT_MS}ms of the hang and names the\n * operation, so this state is rarely empty in practice — but if\n * `lazyInvocations > 0`, `storageOpenError` is unset, and the\n * buffer is empty, the factories are hung somewhere other than\n * the two timeout-wrapped awaits and the next diagnostic should\n * trace each entry in {@link buildHandleFactory} individually.\n * - `lazyWrappers: N records, repoHandleCount < N` is the original\n * v0.59.0 question; the per-record `exitReason` and\n * `handleRegistered` fields answer it directly. */\nconst LAZY_WRAPPER_LOG_CAP = 64;\nlet lazyWrappers: MeshStateLazyWrapperRecord[] = [];\n\n/** Returns a copy of the lazy-wrapper invocation log. See\n * {@link MeshStateLazyWrapperRecord}. */\nexport function getLazyWrappers(): MeshStateLazyWrapperRecord[] {\n return lazyWrappers.slice();\n}\n\nfunction recordLazyWrapper(record: MeshStateLazyWrapperRecord): void {\n lazyWrappers.push(record);\n if (lazyWrappers.length > LAZY_WRAPPER_LOG_CAP) {\n lazyWrappers = lazyWrappers.slice(-LAZY_WRAPPER_LOG_CAP);\n }\n}\n\nfunction peekHandleState(repo: Repo, documentId: DocumentId): string | undefined {\n const handle = (repo.handles as unknown as Record<string, { state: unknown } | undefined>)[\n documentId as unknown as string\n ];\n if (!handle) return undefined;\n return typeof handle.state === \"string\" ? handle.state : String(handle.state ?? \"unknown\");\n}\n\nfunction resolveRepo(option: Repo | undefined): Repo {\n meshStateEverResolved = true;\n const repo = option ?? defaultRepo;\n if (!repo) {\n // Leave a breadcrumb on the console for the polly#107 H5\n // fingerprint even if the consumer's wrappers catch the throw.\n // The id pinpoints WHICH module instance landed here so the\n // operator can compare against the one `createMeshClient`\n // configured.\n if (typeof console !== \"undefined\" && typeof console.warn === \"function\") {\n console.warn(\n `[polly#107 H5] $meshState resolved against unconfigured module instance ${MESH_STATE_MODULE_ID}. If createMeshClient was called elsewhere, the consumer's wrappers and the mesh client are reaching different module instances — see polly#107.`\n );\n }\n throw new Error(\n `Polly $meshState: no Repo configured (module instance ${MESH_STATE_MODULE_ID}). Call configureMeshState(repo) at startup or pass { repo } in the primitive options. If you have called configureMeshState elsewhere, the most likely cause is that the call resolved to a different module instance than this one — see polly#107.`\n );\n }\n return repo;\n}\n\n/**\n * Domain-separated hash of an application key into a 16-byte\n * {@link BinaryDocumentId}. SHA-512 via tweetnacl (already a dep for signing);\n * the first 16 bytes give a DocumentId with uniform distribution across the\n * Automerge id space. The domain prefix pins the derivation to $meshState so\n * that the same logical key used in a different Polly subsystem would\n * produce a different id.\n */\nconst DOC_ID_DOMAIN = \"polly/meshState/v1\";\nconst keyEncoder = new TextEncoder();\n\n/**\n * Domain-separated hash of an application key into a 16-byte\n * {@link DocumentId}. Exported so consumers can compute the\n * derived id for any logical key — useful for ADR 0008-style\n * document compaction where the consumer wants to seed a new doc\n * at `deriveDocumentId(key + ':v' + timestamp)` and stash that id\n * in a runtime index. */\nexport function deriveDocumentId(key: string): DocumentId {\n const digest = nacl.hash(keyEncoder.encode(`${DOC_ID_DOMAIN}:${key}`));\n const bytes = digest.slice(0, 16);\n return interpretAsDocumentId(bytes as unknown as BinaryDocumentId);\n}\n\n/**\n * Caller-installed resolver consulted at every `$mesh*` wrapper\n * lazy construction. Returns the {@link DocumentId} the consumer\n * wants the logical key to resolve to, or `undefined` to fall back\n * to the deterministic {@link deriveDocumentId} path.\n *\n * Designed for fairfox-style document compaction where a logical\n * key (e.g. `mesh:devices`) needs to point at a freshly-seeded\n * cleaned doc instead of the bloated historical one. The consumer\n * registers a resolver that consults a runtime index document\n * (e.g. `mesh:document-index`) for the current docId per key,\n * falling back to derived for keys that haven't been compacted.\n *\n * Synchronous: the resolver runs inside `$meshState` construction,\n * which is called from consumer module-init paths that can't\n * tolerate an async hop. Consumers that need an async lookup\n * (e.g. waiting for an index doc to hydrate) typically register\n * the resolver only after the index has loaded, and the\n * pre-registration period uses the legacy derived path.\n *\n * Note on recursion: a consumer's index doc is itself a `$meshState`\n * wrapper that goes through this resolver. The resolver must\n * short-circuit on its own index-doc key (return `undefined`) or\n * the resolution will recurse infinitely. Per ADR 0008.\n */\nexport type DocIdResolver = (key: string) => DocumentId | undefined;\n\nlet docIdResolver: DocIdResolver | undefined;\n\nexport function registerDocIdResolver(resolver: DocIdResolver | undefined): void {\n docIdResolver = resolver;\n}\n\nexport function getDocIdResolver(): DocIdResolver | undefined {\n return docIdResolver;\n}\n\n/**\n * Resolve a logical `$meshState` key to a {@link DocumentId}. The\n * caller-installed {@link DocIdResolver} (if any) wins; falls back\n * to {@link deriveDocumentId}. ADR 0008.\n */\nexport function resolveDocumentId(key: string): DocumentId {\n return docIdResolver?.(key) ?? deriveDocumentId(key);\n}\n\n/**\n * Build a getHandle factory that resolves a logical key to a DocHandle on\n * the supplied Repo via a deterministic DocumentId. On the first call during\n * a process lifetime — whether the device has never written this key or has\n * written it before a prior reload — the factory short-circuits around\n * {@link Repo.find}'s network-request timeout: it peeks directly at the\n * Repo's storage subsystem, hydrates from storage if the bytes are there,\n * and otherwise seeds a fresh document at the deterministic id. Subsequent\n * calls in the same process return the cached handle.\n */\nfunction buildHandleFactory<D>(\n repo: Repo,\n key: string,\n initialDoc: D\n): () => Promise<DocHandle<D>> {\n // ADR 0008: consult the caller-installed resolver first so a\n // compacted key can point at the freshly-seeded cleaned doc.\n // Falls back to the deterministic derived id when no resolver is\n // registered or the resolver returns undefined.\n const documentId = resolveDocumentId(key);\n return async () => {\n // Polly#107 post-H5: tick on every factory entry. See\n // {@link lazyInvocations} for the diagnostic this enables.\n lazyInvocations++;\n let exitReason: LazyWrapperExitReason = \"threw\";\n let errorMessage: string | undefined;\n try {\n const cached = repo.handles[documentId];\n // Touching `repo.handles` is the first Repo subsystem read; if\n // the throw happens after this line we know the factory got\n // past the Repo-identity gate.\n lazyReachedRepo++;\n const docIdString = documentId as unknown as string;\n if (cached) {\n // Polly#107 post-v0.60: bound the placeholder-handle wait so\n // a wedged storage layer (e.g. zombie IDB connection holding\n // a `loading` handle hostage) surfaces as a timed-out\n // `storageOpenError` within seconds instead of hanging the\n // factory indefinitely.\n await withStorageTimeout(\n \"whenReady\",\n docIdString,\n cached.whenReady([\"ready\", \"unavailable\"])\n );\n if (cached.state === \"ready\") {\n exitReason = \"returned-cached\";\n return cached as unknown as DocHandle<D>;\n }\n }\n // Polly#107 post-v0.60: the `loadDoc()` call drops into\n // whatever IndexedDB adapter the consumer wired (Automerge's\n // `IndexedDBStorageAdapter` in fairfox's case). A wedged DB\n // open or a blocked transaction at this layer is invisible to\n // polly otherwise; the timeout surfaces it as a populated\n // `storageOpenError` field on the snapshot.\n const loadPromise = repo.storageSubsystem?.loadDoc<D>(documentId);\n const stored = loadPromise\n ? await withStorageTimeout(\"loadDoc\", docIdString, loadPromise)\n : undefined;\n if (stored) {\n exitReason = \"loaded-from-storage\";\n return repo.find<D>(documentId, { allowableStates: [\"ready\"] });\n }\n const seeded = Automerge.save(\n Automerge.from(initialDoc as unknown as Record<string, unknown>)\n );\n const handle = repo.import<D>(seeded, { docId: documentId });\n handle.doneLoading();\n exitReason = \"seeded-and-imported\";\n return handle;\n } catch (err) {\n // Capture the breadcrumb on the module so an otherwise-silent\n // unawaited rejection in a consumer wrapper leaves a trace the\n // snapshot can surface. See {@link lastLoadedRejection}.\n errorMessage = err instanceof Error ? err.message : String(err);\n recordLoadedRejection(err);\n throw err;\n } finally {\n // Polly#107 post-v0.59: peek `repo.handles[documentId]` at the\n // moment of would-be return so the snapshot can name, per\n // factory invocation, which exit path was taken AND whether\n // the Repo's local-side bookkeeping actually committed the\n // handle. The catch arm has already set `exitReason = \"threw\"`\n // by default; the success arms overwrite it before returning,\n // so the `finally`'s reading is correct in all four cases.\n const handleState = peekHandleState(repo, documentId);\n recordLazyWrapper({\n key,\n docId: documentId as unknown as string,\n at: Date.now(),\n exitReason,\n handleRegistered: handleState !== undefined,\n handleState,\n errorMessage,\n });\n }\n };\n}\n\n// $meshState\n\n/** Attach a rejection sink to a mesh primitive's `loaded` promise.\n * Polly#107 post-H5: an unawaited `loaded` rejection in a consumer\n * wrapper would vanish without trace; this captures the breadcrumb\n * on the module so a snapshot read can surface it. The original\n * primitive is returned unchanged — the `.catch` is a side-effect\n * sink that does not swallow the rejection from any consumer that\n * does await `loaded`. */\nfunction attachLoadedRejectionSink<P extends { loaded: Promise<unknown> }>(primitive: P): P {\n primitive.loaded.catch((err) => {\n recordLoadedRejection(err);\n });\n return primitive;\n}\n\n/**\n * Create a peer-replicated state primitive backed by Automerge with a mesh\n * transport. Every device holds a full replica; no central server holds a\n * replica. Operations flow peer-to-peer through signed and encrypted\n * messages on the underlying transport.\n *\n * @example\n * ```ts\n * const journal = $meshState<Journal>(\"journal\", { entries: [] });\n * await journal.loaded;\n * journal.value = { entries: [\"my private thoughts\"] };\n * ```\n */\nexport function $meshState<T extends VersionedDoc>(\n key: string,\n initialValue: T,\n options: MeshStateOptions = {}\n): CrdtPrimitive<T> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtState<T>({\n key,\n primitive: \"meshState\",\n initialValue,\n getHandle: buildHandleFactory<T>(repo, key, initialValue),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n// $meshText\n\n/**\n * Create a peer-replicated text primitive backed by a mesh transport.\n * Concurrent character-level edits from peers merge cleanly via Automerge's\n * updateText splicing, and every operation is signed and encrypted before\n * leaving the originating peer.\n */\nexport function $meshText(\n key: string,\n initialValue: string,\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<string> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtText(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<TextDoc>(repo, key, { text: initialValue }),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n// $meshCounter\n\n/**\n * Create a peer-replicated counter primitive backed by a mesh transport.\n * Concurrent increments commute, and every operation is signed and\n * encrypted before leaving the originating peer.\n */\nexport function $meshCounter(\n key: string,\n initialValue: number,\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<number> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtCounter(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<CounterDoc>(repo, key, {}),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n// $meshList\n\n/**\n * Create a peer-replicated list primitive backed by a mesh transport.\n * Phase 0 naive replacement applies to writes for now; Phase 1.1 will\n * refine with structural diff-to-splice for concurrent insert/remove\n * preservation.\n */\nexport function $meshList<T>(\n key: string,\n initialValue: T[],\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<T[]> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtList<T>(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<ListDoc<T>>(repo, key, { items: initialValue }),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n",
|
|
18
|
+
"/**\n * mesh-state — Phase 2 wrappers exposing $meshState, $meshText, $meshCounter,\n * and $meshList. These are the application-facing constructors for the\n * strongest resilience tier in RFC-041: every device is a full Automerge\n * replica, the server is *not on the data path at all*, and the application\n * functions with zero server uptime once direct peer connections are\n * established.\n *\n * Each primitive wraps the corresponding Phase 0 base ($crdtState, $crdtText,\n * $crdtCounter, $crdtList) with three additions:\n *\n * 1. The `primitive` label is hard-coded to \"meshState\" so the\n * primitive-registry collision detection knows which family the key\n * belongs to.\n *\n * 2. A handle factory that resolves the application's logical key to an\n * Automerge DocumentId by hashing the key into a deterministic,\n * content-addressable id. Every Repo backed by the same storage lands\n * on the same document without needing any extra state, and two devices\n * that have never met converge on the same id for the same key — which\n * also helps first-sync after pairing. (Prior to this change the factory\n * held an in-memory per-Repo `Map<string, DocumentId>`, which meant that\n * a lone-device reload — a very common onboarding state — produced a\n * fresh DocumentId for the same logical key, orphaned the document the\n * storage adapter still held on disk, and silently lost the user's data.)\n *\n * 3. Signing and encryption are mandatory, not optional. Where $peerState\n * accepts encrypt/sign as opt-in flags (currently throwing in Phase 1),\n * $meshState requires every operation to be signed by the originating\n * peer's key and encrypted under the document's symmetric key. The\n * mechanism lives in the wrapping MeshNetworkAdapter that the Repo\n * uses for transport.\n *\n * The Repo itself is supplied by the application via {@link configureMeshState}\n * or per-call via the `repo` option. In Phase 2 the production transport will\n * be a WebRTC mesh adapter wrapping signing+encryption around an in-process\n * RTCDataChannel; for tests and for the early Phase 2 cut, an in-memory\n * loopback adapter pair satisfies the same contract.\n */\n\nimport {\n Automerge,\n type BinaryDocumentId,\n type DocHandle,\n type DocumentId,\n interpretAsDocumentId,\n type Repo,\n} from \"@automerge/automerge-repo/slim\";\nimport nacl from \"tweetnacl\";\nimport type { Access } from \"./access\";\nimport {\n $crdtCounter,\n $crdtList,\n $crdtText,\n type CounterDoc,\n type ListDoc,\n type SpecialisedPrimitive,\n type TextDoc,\n} from \"./crdt-specialised\";\nimport { $crdtState, type CrdtPrimitive } from \"./crdt-state\";\nimport type { Migrations, VersionedDoc } from \"./schema-version\";\n\n/** Common option shape across all four $mesh* primitives. */\nexport interface MeshStateOptions {\n /** Override the default Repo for this primitive. The Repo must be\n * configured with the mesh transport (signing and encryption at the\n * network layer). */\n repo?: Repo;\n /** Schema version target for the application. Migrations run on load. */\n schemaVersion?: number;\n /** Migration table keyed by target version. Required if schemaVersion is set. */\n migrations?: Migrations;\n /** Declarative read/write access. The mesh transport compiles this into\n * a public-key set used by the signing layer to verify incoming ops. */\n access?: Access;\n}\n\nlet defaultRepo: Repo | undefined;\n\n/** Per-module-instance identifier stamped at import time. Two distinct\n * module instances (e.g. duplicated under a bundler that hoists\n * differently for the consumer's app code and `@fairfox/polly`'s own\n * imports) produce two different ids — and therefore two different\n * `defaultRepo` globals.\n *\n * The polly#107 fingerprint reading is exactly that: `configureMeshState`\n * runs on one module instance (the one `createMeshClient` imports);\n * the consumer's `$meshState(key, initial)` calls run on a different\n * instance, whose `defaultRepo` was never set. `resolveRepo` either\n * throws (caught silently by consumer wrappers) or returns the\n * unconfigured fallback — either way, the handle that should land\n * in the mesh `Repo` lands somewhere else (or nowhere), Automerge's\n * NetworkSubsystem never sees it, and inbound sync from the daemon\n * has nowhere to go.\n *\n * Exported so consumers can compare the id seen at the `$meshState`\n * call site against the id seen at the `configureMeshState` call\n * site. A mismatch IS the bundle-duplication bug; a match rules it\n * out. The id is also surfaced via {@link getMeshStateModuleId} on\n * every `MeshClient.getPeerStateSnapshot()` so a single one-line\n * read from the failing tab tells the operator which module\n * instance the snapshot is rendered from.\n *\n * Polly issue #107 H5 (mesh-state module duplication). */\nexport const MESH_STATE_MODULE_ID: string = `mesh-state-${\n typeof crypto !== \"undefined\" && typeof crypto.randomUUID === \"function\"\n ? crypto.randomUUID()\n : Math.random().toString(36).slice(2) + Date.now().toString(36)\n}`;\n\n/** Returns the per-module-instance id stamped at import time. See\n * {@link MESH_STATE_MODULE_ID}. */\nexport function getMeshStateModuleId(): string {\n return MESH_STATE_MODULE_ID;\n}\n\n/** Last id passed to {@link configureMeshState}; the Repo's\n * `peerId` is captured at configuration time so a snapshot read can\n * answer \"was this module instance ever wired to a mesh client at\n * all?\". `undefined` means `$meshState` is reachable but no\n * `createMeshClient` call against THIS module instance ever\n * succeeded — the polly#107 H5 fingerprint. */\nlet lastConfiguredRepoPeerId: string | undefined;\n\n/** Returns the `peerId` of the most recent Repo wired through\n * {@link configureMeshState} against this module instance. Surfaced\n * via the mesh client snapshot so a consumer can answer \"did\n * configureMeshState run on the same module instance the wrappers\n * are using\" in one read. */\nexport function getLastConfiguredRepoPeerId(): string | undefined {\n return lastConfiguredRepoPeerId;\n}\n\n/**\n * Set the default Repo that the $mesh* primitives use when no `repo` option\n * is supplied. Production code typically calls this once at application\n * startup with a Repo configured for the mesh transport. Tests call it\n * before each scenario with an in-memory or loopback Repo.\n */\nexport function configureMeshState(repo: Repo): void {\n defaultRepo = repo;\n lastConfiguredRepoPeerId = repo.peerId as unknown as string | undefined;\n}\n\n/**\n * Reset the mesh-state subsystem to its initial unconfigured state.\n * Intended for tests; production code should not call this.\n */\nexport function resetMeshState(): void {\n defaultRepo = undefined;\n lastConfiguredRepoPeerId = undefined;\n meshStateEverResolved = false;\n lazyInvocations = 0;\n lazyReachedRepo = 0;\n lastLoadedRejection = undefined;\n lastStorageOpenError = undefined;\n lazyWrappers = [];\n docIdResolver = undefined;\n}\n\n/** Returns whether this module instance's `defaultRepo` was ever\n * set via {@link configureMeshState}. Distinct from \"was set and\n * then reset\" — `resetMeshState` clears it. The polly#107 H5\n * fingerprint is `false` here on the module instance the consumer's\n * `$meshState` calls resolve from. */\nexport function isMeshStateConfigured(): boolean {\n return defaultRepo !== undefined;\n}\n\n/** Set to `true` once any `$meshState`-family wrapper has been\n * called against this module instance. Surfaced via\n * {@link wasMeshStateResolved} so a snapshot can answer \"did any\n * wrapper code ever reach THIS module\" — the polly#107 H5\n * fingerprint pair (`configured: false` AND `wasResolved: true`)\n * confirms the consumer's wrappers reach a different module instance\n * than `createMeshClient` configured. */\nlet meshStateEverResolved = false;\n\n/** Returns `true` once any `$meshState`-family wrapper has been\n * called against this module instance. See\n * {@link meshStateEverResolved}. */\nexport function wasMeshStateResolved(): boolean {\n return meshStateEverResolved;\n}\n\n/** Polly#107 post-H5 instrumentation. Counts every invocation of\n * the lazy handle factory built by {@link buildHandleFactory} — i.e.\n * every time a `$meshState`-family primitive's underlying loader is\n * actually called. Compared against {@link lazyReachedRepo}, the gap\n * pinpoints whether throws are happening between factory entry and\n * the first `repo.*` touch.\n *\n * The polly#107 v0.58.0 fingerprint from `fairfox.fly.dev` ruled out\n * H5: the wrappers ARE reaching the same module instance the mesh\n * client configured. But `repo.handles` still reported one handle of\n * the expected fourteen, so something is throwing between the\n * wrapper's factory entry and the registration. These two counters\n * answer \"where\" without another diagnostic build. */\nlet lazyInvocations = 0;\n\n/** Returns the count of factory invocations since module load. See\n * {@link lazyInvocations}. */\nexport function getLazyInvocations(): number {\n return lazyInvocations;\n}\n\n/** Polly#107 post-H5 instrumentation. Counts every time a factory\n * invocation made it as far as a `repo.find` / `repo.import` /\n * `repo.handles[...]` / `repo.storageSubsystem` call — i.e. the\n * factory reached the Repo subsystem before throwing or returning.\n * If {@link lazyInvocations} ticks but this counter does not, the\n * throw is happening before any Repo work; if both tick, the throw\n * (or the failing async path) is downstream of the Repo touch and\n * the next diagnostic should target Automerge's load/sync path. */\nlet lazyReachedRepo = 0;\n\n/** Returns the count of factory invocations that reached\n * `repo.find` / `repo.import` since module load. See\n * {@link lazyReachedRepo}. */\nexport function getLazyReachedRepo(): number {\n return lazyReachedRepo;\n}\n\n/** Polly#107 post-H5 instrumentation. Records the most recent\n * rejection (or synchronous throw) escaping the factory body — the\n * `loaded` promise's rejection path. Today an unawaited rejection\n * inside a consumer wrapper vanishes without trace; capturing the\n * message + stack here on the module and exposing it via the\n * snapshot leaves a breadcrumb the operator can read in one paste.\n *\n * The format is the JSON-safe `{ name, message, stack, at }` shape\n * so the snapshot read does not have to traffic in `Error`\n * instances. `at` is `Date.now()` at the time the rejection was\n * captured. */\nexport interface MeshStateLoadedRejectionBreadcrumb {\n name: string;\n message: string;\n stack: string | undefined;\n at: number;\n}\nlet lastLoadedRejection: MeshStateLoadedRejectionBreadcrumb | undefined;\n\n/** Returns the most recent rejection escaping a `$meshState` factory\n * invocation since module load. See {@link lastLoadedRejection}. */\nexport function getLastLoadedRejection(): MeshStateLoadedRejectionBreadcrumb | undefined {\n return lastLoadedRejection;\n}\n\nfunction recordLoadedRejection(thrown: unknown): void {\n const err =\n thrown instanceof Error\n ? thrown\n : new Error(typeof thrown === \"string\" ? thrown : String(thrown));\n lastLoadedRejection = {\n name: err.name,\n message: err.message,\n stack: err.stack,\n at: Date.now(),\n };\n}\n\n/** Polly#107 post-v0.60 instrumentation. Default timeout for the two\n * storage-touching `await`s inside {@link buildHandleFactory} — the\n * `cached.whenReady([\"ready\", \"unavailable\"])` wait on a placeholder\n * handle, and the `repo.storageSubsystem.loadDoc()` read against\n * whatever IndexedDB adapter the consumer wired. Healthy completions\n * for both are normally sub-millisecond; the v0.60.0 fingerprint\n * showed 17 factories hung indefinitely against a wedged\n * `fairfox-mesh` IndexedDB that only released when Chrome itself\n * restarted. Five seconds is two orders of magnitude beyond the\n * normal upper bound and well below the operator's \"have I been\n * staring at a stuck app\" threshold — long enough that healthy\n * boots never trip the timeout and short enough that the next\n * polly#107-shaped session sees a populated {@link storageOpenError}\n * within the first few seconds of inspection. */\nconst STORAGE_OP_TIMEOUT_MS = 5000;\n\n/** Polly#107 post-v0.60 instrumentation. Captured when a storage\n * read from inside a `$meshState`-family factory exceeds\n * {@link STORAGE_OP_TIMEOUT_MS}. Names which op timed out\n * (`whenReady`, `loadDoc`), the document id under attempt, the\n * elapsed time, and a single human-readable message that the\n * snapshot can paste verbatim into a ticket. The breadcrumb also\n * flows into {@link lastLoadedRejection} so the existing\n * \"did anything reject\" channel surfaces it alongside.\n *\n * The v0.60.0 fingerprint diagnosed \"factories hung mid-await,\n * not throwing\" indirectly — empty {@link lazyWrappers} beside\n * non-zero {@link lazyInvocations} / {@link lazyReachedRepo}. With\n * this field a future polly#107-shaped session reads\n * `storageOpenError.message: \"Polly $meshState: storage operation\n * 'loadDoc' on document '<id>' timed out after 5000ms\"` and jumps\n * directly to \"the storage layer is hung; clear site data,\"\n * skipping every other rung in the ladder. */\nexport interface MeshStateStorageOpenError {\n /** Which await in `buildHandleFactory` exceeded the timeout. */\n operation: \"whenReady\" | \"loadDoc\";\n /** Stringified Automerge `DocumentId` that was under attempt. */\n documentId: string;\n /** Time the operation was given before the timeout fired (ms). */\n timeoutMs: number;\n /** Wall-clock elapsed when the timeout fired (ms). Always\n * `>= timeoutMs`; a small overshoot is normal. */\n elapsedMs: number;\n /** Pre-formatted human-readable message. Identical to what\n * flows into {@link lastLoadedRejection}.message. */\n message: string;\n /** `Date.now()` at the moment the timeout fired. */\n at: number;\n}\n\nlet lastStorageOpenError: MeshStateStorageOpenError | undefined;\n\n/** Returns the most recent storage-operation timeout escaping a\n * `$meshState` factory invocation since module load. See\n * {@link lastStorageOpenError}. */\nexport function getStorageOpenError(): MeshStateStorageOpenError | undefined {\n return lastStorageOpenError;\n}\n\nfunction recordStorageOpenError(error: MeshStateStorageOpenError): void {\n lastStorageOpenError = error;\n}\n\n/** Polly#107 post-v0.60 instrumentation. Wraps the storage-touching\n * awaits inside {@link buildHandleFactory} with a hard timeout. On\n * timeout the rejection feeds both {@link lastLoadedRejection} (so\n * the existing channel surfaces it) and {@link lastStorageOpenError}\n * (so a snapshot read distinguishes \"storage layer wedged\" from\n * any other factory rejection in one field). */\nasync function withStorageTimeout<T>(\n operation: \"whenReady\" | \"loadDoc\",\n documentId: string,\n promise: Promise<T>,\n timeoutMs: number = STORAGE_OP_TIMEOUT_MS\n): Promise<T> {\n const start = Date.now();\n let timer: ReturnType<typeof setTimeout> | undefined;\n let timedOut = false;\n try {\n return await new Promise<T>((resolve, reject) => {\n timer = setTimeout(() => {\n timedOut = true;\n const elapsedMs = Date.now() - start;\n const message = `Polly $meshState: storage operation '${operation}' on document '${documentId}' timed out after ${timeoutMs}ms`;\n recordStorageOpenError({\n operation,\n documentId,\n timeoutMs,\n elapsedMs,\n message,\n at: Date.now(),\n });\n reject(new Error(message));\n }, timeoutMs);\n promise.then(\n (value) => {\n if (!timedOut) resolve(value);\n },\n (err) => {\n if (!timedOut) reject(err);\n }\n );\n });\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n}\n\n/** Polly#107 post-v0.59 instrumentation. Categorises the exit path\n * a `$mesh*` lazy factory invocation took. The v0.59.0 fingerprint\n * showed `lazyInvocations === lazyReachedRepo === 17` and no\n * `lastLoadedRejection` — every factory reached the first\n * `repo.handles[docId]` access, none rejected loudly, yet 16 of 17\n * handles never landed in `repo.handles`. Without per-exit detail\n * the snapshot cannot disambiguate \"factory returned the cached\n * handle\", \"factory hydrated from storage\", \"factory seeded and\n * imported a fresh doc\", or \"factory took the cached branch but\n * `whenReady` resolved to `unavailable` and the code fell\n * through\". Each exit reason corresponds to a single line in\n * {@link buildHandleFactory}; the snapshot record names it\n * verbatim so the operator can grep the source from one read. */\nexport type LazyWrapperExitReason =\n | \"returned-cached\"\n | \"loaded-from-storage\"\n | \"seeded-and-imported\"\n | \"threw\";\n\n/** Polly#107 post-v0.59 instrumentation. One record per factory\n * invocation, ring-buffered on the module and surfaced through\n * `MeshClient.getPeerStateSnapshot()` as\n * `meshStateModule.lazyWrappers`. The five fields together answer\n * the post-v0.59 question \"if every factory reached the Repo and\n * nothing rejected, why are 16 of 17 handles absent from\n * `repo.handles`?\" — `exitReason` names which of the three success\n * paths each took, `handleRegistered` is the synchronous peek at\n * `repo.handles[docId]` taken in the factory's `finally` (i.e. at\n * the moment of would-be return), and `handleState` is the\n * lifecycle state observed in that peek. A row where\n * `exitReason: \"seeded-and-imported\"` and `handleRegistered: false`\n * is the smoking gun for H-Q1 (`repo.import` returned without\n * registering); rows where `exitReason: \"returned-cached\"` repeats\n * for sixteen of seventeen distinct keys indicates collisions or\n * that the factory is being called multiple times against a\n * Repo-state-machine that has already filled the slot. */\nexport interface MeshStateLazyWrapperRecord {\n /** The logical application key passed to `$meshState(key, ...)`. */\n key: string;\n /** Stringified Automerge `DocumentId` derived from the key. */\n docId: string;\n /** `Date.now()` captured at the moment the factory was about to\n * return (or rethrow). */\n at: number;\n /** Which of the four exit paths the factory took. See\n * {@link LazyWrapperExitReason}. */\n exitReason: LazyWrapperExitReason;\n /** Snapshot peek `repo.handles[docId] !== undefined` taken in the\n * factory's `finally` clause. `true` means the local Repo\n * registered the handle; `false` means the factory thinks it\n * succeeded but the Repo does not hold the handle for this\n * documentId. The \"polly#107 post-v0.59\" smoking gun is\n * `exitReason: \"seeded-and-imported\", handleRegistered: false`. */\n handleRegistered: boolean;\n /** Lifecycle state of `repo.handles[docId]` at the synchronous\n * peek time. `undefined` when the handle was not registered.\n * Useful for distinguishing the \"registered as loading/unavailable\"\n * case from the \"registered as ready\" case. */\n handleState: string | undefined;\n /** Error message if `exitReason === \"threw\"`; `undefined`\n * otherwise. The full rejection still flows into\n * {@link lastLoadedRejection}; this field is the row-local\n * summary so the snapshot can show the failure cause inline. */\n errorMessage: string | undefined;\n}\n\n/** Bounded ring of {@link MeshStateLazyWrapperRecord} captured per\n * factory invocation. Cap kept low so the snapshot stays small\n * enough to paste into a ticket; fairfox's realistic pre-warm count\n * is in the high teens, so 64 leaves plenty of headroom and still\n * truncates pathological logs.\n *\n * Reading guide for a future polly#107-shaped session:\n *\n * - `lazyWrappers: 0 records, lazyInvocations > 0` means the\n * factories are **hung mid-await, not throwing** — the buffer\n * itself is fine; the records simply never reached the `finally`\n * that writes them. The v0.60.0 fingerprint took this exact shape\n * and turned out to be a wedged `fairfox-mesh` IndexedDB the\n * process could no longer evict; restarting Chrome cleared it.\n * `storageOpenError` (added v0.61.0) now populates within\n * {@link STORAGE_OP_TIMEOUT_MS}ms of the hang and names the\n * operation, so this state is rarely empty in practice — but if\n * `lazyInvocations > 0`, `storageOpenError` is unset, and the\n * buffer is empty, the factories are hung somewhere other than\n * the two timeout-wrapped awaits and the next diagnostic should\n * trace each entry in {@link buildHandleFactory} individually.\n * - `lazyWrappers: N records, repoHandleCount < N` is the original\n * v0.59.0 question; the per-record `exitReason` and\n * `handleRegistered` fields answer it directly. */\nconst LAZY_WRAPPER_LOG_CAP = 64;\nlet lazyWrappers: MeshStateLazyWrapperRecord[] = [];\n\n/** Returns a copy of the lazy-wrapper invocation log. See\n * {@link MeshStateLazyWrapperRecord}. */\nexport function getLazyWrappers(): MeshStateLazyWrapperRecord[] {\n return lazyWrappers.slice();\n}\n\nfunction recordLazyWrapper(record: MeshStateLazyWrapperRecord): void {\n lazyWrappers.push(record);\n if (lazyWrappers.length > LAZY_WRAPPER_LOG_CAP) {\n lazyWrappers = lazyWrappers.slice(-LAZY_WRAPPER_LOG_CAP);\n }\n}\n\nfunction peekHandleState(repo: Repo, documentId: DocumentId): string | undefined {\n const handle = (repo.handles as unknown as Record<string, { state: unknown } | undefined>)[\n documentId as unknown as string\n ];\n if (!handle) return undefined;\n return typeof handle.state === \"string\" ? handle.state : String(handle.state ?? \"unknown\");\n}\n\nfunction resolveRepo(option: Repo | undefined): Repo {\n meshStateEverResolved = true;\n const repo = option ?? defaultRepo;\n if (!repo) {\n // Leave a breadcrumb on the console for the polly#107 H5\n // fingerprint even if the consumer's wrappers catch the throw.\n // The id pinpoints WHICH module instance landed here so the\n // operator can compare against the one `createMeshClient`\n // configured.\n if (typeof console !== \"undefined\" && typeof console.warn === \"function\") {\n console.warn(\n `[polly#107 H5] $meshState resolved against unconfigured module instance ${MESH_STATE_MODULE_ID}. If createMeshClient was called elsewhere, the consumer's wrappers and the mesh client are reaching different module instances — see polly#107.`\n );\n }\n throw new Error(\n `Polly $meshState: no Repo configured (module instance ${MESH_STATE_MODULE_ID}). Call configureMeshState(repo) at startup or pass { repo } in the primitive options. If you have called configureMeshState elsewhere, the most likely cause is that the call resolved to a different module instance than this one — see polly#107.`\n );\n }\n return repo;\n}\n\n/**\n * Domain-separated hash of an application key into a 16-byte\n * {@link BinaryDocumentId}. SHA-512 via tweetnacl (already a dep for signing);\n * the first 16 bytes give a DocumentId with uniform distribution across the\n * Automerge id space. The domain prefix pins the derivation to $meshState so\n * that the same logical key used in a different Polly subsystem would\n * produce a different id.\n */\nconst DOC_ID_DOMAIN = \"polly/meshState/v1\";\nconst keyEncoder = new TextEncoder();\n\n/**\n * Domain-separated hash of an application key into a 16-byte\n * {@link DocumentId}. Exported so consumers can compute the\n * derived id for any logical key — useful for ADR 0008-style\n * document compaction where the consumer wants to seed a new doc\n * at `deriveDocumentId(key + ':v' + timestamp)` and stash that id\n * in a runtime index. */\nexport function deriveDocumentId(key: string): DocumentId {\n const digest = nacl.hash(keyEncoder.encode(`${DOC_ID_DOMAIN}:${key}`));\n const bytes = digest.slice(0, 16);\n return interpretAsDocumentId(bytes as unknown as BinaryDocumentId);\n}\n\n/**\n * Caller-installed resolver consulted at every `$mesh*` wrapper\n * lazy construction. Returns the {@link DocumentId} the consumer\n * wants the logical key to resolve to, or `undefined` to fall back\n * to the deterministic {@link deriveDocumentId} path.\n *\n * Designed for fairfox-style document compaction where a logical\n * key (e.g. `mesh:devices`) needs to point at a freshly-seeded\n * cleaned doc instead of the bloated historical one. The consumer\n * registers a resolver that consults a runtime index document\n * (e.g. `mesh:document-index`) for the current docId per key,\n * falling back to derived for keys that haven't been compacted.\n *\n * Synchronous: the resolver runs inside `$meshState` construction,\n * which is called from consumer module-init paths that can't\n * tolerate an async hop. Consumers that need an async lookup\n * (e.g. waiting for an index doc to hydrate) typically register\n * the resolver only after the index has loaded, and the\n * pre-registration period uses the legacy derived path.\n *\n * Note on recursion: a consumer's index doc is itself a `$meshState`\n * wrapper that goes through this resolver. The resolver must\n * short-circuit on its own index-doc key (return `undefined`) or\n * the resolution will recurse infinitely. Per ADR 0008.\n */\nexport type DocIdResolver = (key: string) => DocumentId | undefined;\n\nlet docIdResolver: DocIdResolver | undefined;\n\nexport function registerDocIdResolver(resolver: DocIdResolver | undefined): void {\n docIdResolver = resolver;\n}\n\nexport function getDocIdResolver(): DocIdResolver | undefined {\n return docIdResolver;\n}\n\n/**\n * Resolve a logical `$meshState` key to a {@link DocumentId}. The\n * caller-installed {@link DocIdResolver} (if any) wins; falls back\n * to {@link deriveDocumentId}. ADR 0008.\n */\nexport function resolveDocumentId(key: string): DocumentId {\n return docIdResolver?.(key) ?? deriveDocumentId(key);\n}\n\n/**\n * Caller-installed detector consulted after a `$mesh*` wrapper's\n * handle reaches `ready`. Inspects the loaded doc; returns a\n * {@link DocumentId} the wrapper should transparently rebind to,\n * or `undefined` to keep the current handle.\n *\n * Designed for fairfox-style document-compaction redirects: when a\n * doc carries an in-band sentinel field (e.g. `__compaction__`),\n * the wrapper's first load gets the sealed doc; the detector reads\n * the sentinel and returns the migrated-to docId; polly swaps to\n * that handle and the consumer sees the cleaned doc transparently.\n *\n * Symmetric with {@link DocIdResolver}: the resolver runs at lazy\n * construction (synchronous, no doc available); the detector runs\n * after first load (synchronous over the materialised doc). Either\n * is sufficient to redirect a wrapper, but the detector is the only\n * mechanism that works on a device whose redirect-index hasn't yet\n * synced — the sealed doc carries enough information on its own to\n * resolve the redirect from local storage.\n *\n * The detector is invoked repeatedly along a chain of redirects\n * with a hard cycle/depth guard (see {@link MAX_REDIRECT_FOLLOWS}),\n * so a compacted-then-compacted-again doc resolves to the latest\n * `currentDocId` in one factory call.\n */\nexport type RedirectDetector = (doc: unknown) => DocumentId | undefined;\n\nlet redirectDetector: RedirectDetector | undefined;\n\nexport function registerRedirectDetector(detector: RedirectDetector | undefined): void {\n redirectDetector = detector;\n}\n\nexport function getRedirectDetector(): RedirectDetector | undefined {\n return redirectDetector;\n}\n\n/** Hard cap on the redirect-follow loop. A correctly-behaved\n * detector terminates in one or two hops (sealed → current; rarely\n * sealed1 → sealed2 → current after a re-compaction). Anything\n * past this is either a cycle (bug in the detector) or an attacker\n * trying to wedge the wrapper. */\nconst MAX_REDIRECT_FOLLOWS = 8;\n\n/**\n * Build a getHandle factory that resolves a logical key to a DocHandle on\n * the supplied Repo via a deterministic DocumentId. On the first call during\n * a process lifetime — whether the device has never written this key or has\n * written it before a prior reload — the factory short-circuits around\n * {@link Repo.find}'s network-request timeout: it peeks directly at the\n * Repo's storage subsystem, hydrates from storage if the bytes are there,\n * and otherwise seeds a fresh document at the deterministic id. Subsequent\n * calls in the same process return the cached handle.\n */\nfunction buildHandleFactory<D>(\n repo: Repo,\n key: string,\n initialDoc: D\n): () => Promise<DocHandle<D>> {\n // ADR 0008: consult the caller-installed resolver first so a\n // compacted key can point at the freshly-seeded cleaned doc.\n // Falls back to the deterministic derived id when no resolver is\n // registered or the resolver returns undefined.\n const documentId = resolveDocumentId(key);\n return async () => {\n // Polly#107 post-H5: tick on every factory entry. See\n // {@link lazyInvocations} for the diagnostic this enables.\n lazyInvocations++;\n let exitReason: LazyWrapperExitReason = \"threw\";\n let errorMessage: string | undefined;\n try {\n const cached = repo.handles[documentId];\n // Touching `repo.handles` is the first Repo subsystem read; if\n // the throw happens after this line we know the factory got\n // past the Repo-identity gate.\n lazyReachedRepo++;\n const docIdString = documentId as unknown as string;\n let handle: DocHandle<D>;\n if (cached) {\n // Polly#107 post-v0.60: bound the placeholder-handle wait so\n // a wedged storage layer (e.g. zombie IDB connection holding\n // a `loading` handle hostage) surfaces as a timed-out\n // `storageOpenError` within seconds instead of hanging the\n // factory indefinitely.\n await withStorageTimeout(\n \"whenReady\",\n docIdString,\n cached.whenReady([\"ready\", \"unavailable\"])\n );\n if (cached.state === \"ready\") {\n exitReason = \"returned-cached\";\n handle = cached as unknown as DocHandle<D>;\n } else {\n // Fall through to the load/seed path below.\n handle = await loadOrSeed<D>(repo, documentId, initialDoc, docIdString, (r) => {\n exitReason = r;\n });\n }\n } else {\n handle = await loadOrSeed<D>(repo, documentId, initialDoc, docIdString, (r) => {\n exitReason = r;\n });\n }\n // ADR 0008 v3b: the redirect detector runs INSIDE\n // {@link $crdtState} on every doc change, not here. The\n // factory's job is to produce the initial handle for the\n // resolver-derived docId; $crdtState handles continuous\n // sentinel-follow against the live handle so a peer-synced\n // sentinel rebinds the wrapper reactively without a reload.\n return handle;\n } catch (err) {\n // Capture the breadcrumb on the module so an otherwise-silent\n // unawaited rejection in a consumer wrapper leaves a trace the\n // snapshot can surface. See {@link lastLoadedRejection}.\n errorMessage = err instanceof Error ? err.message : String(err);\n recordLoadedRejection(err);\n throw err;\n } finally {\n // Polly#107 post-v0.59: peek `repo.handles[documentId]` at the\n // moment of would-be return so the snapshot can name, per\n // factory invocation, which exit path was taken AND whether\n // the Repo's local-side bookkeeping actually committed the\n // handle. The catch arm has already set `exitReason = \"threw\"`\n // by default; the success arms overwrite it before returning,\n // so the `finally`'s reading is correct in all four cases.\n const handleState = peekHandleState(repo, documentId);\n recordLazyWrapper({\n key,\n docId: documentId as unknown as string,\n at: Date.now(),\n exitReason,\n handleRegistered: handleState !== undefined,\n handleState,\n errorMessage,\n });\n }\n };\n}\n\n/** Helper for the load-or-seed arm of {@link buildHandleFactory}.\n * Extracted so the cached/non-cached arms share one body. */\nasync function loadOrSeed<D>(\n repo: Repo,\n documentId: DocumentId,\n initialDoc: D,\n docIdString: string,\n setExitReason: (r: LazyWrapperExitReason) => void\n): Promise<DocHandle<D>> {\n const loadPromise = repo.storageSubsystem?.loadDoc<D>(documentId);\n const stored = loadPromise\n ? await withStorageTimeout(\"loadDoc\", docIdString, loadPromise)\n : undefined;\n if (stored) {\n setExitReason(\"loaded-from-storage\");\n return repo.find<D>(documentId, { allowableStates: [\"ready\"] });\n }\n const seeded = Automerge.save(Automerge.from(initialDoc as unknown as Record<string, unknown>));\n const handle = repo.import<D>(seeded, { docId: documentId });\n handle.doneLoading();\n setExitReason(\"seeded-and-imported\");\n return handle;\n}\n\n/** Resolve a redirect off a ready handle, walking the detector\n * chain (up to {@link MAX_REDIRECT_FOLLOWS}) and returning the\n * final handle. Used by {@link $crdtState}'s continuous-rebind\n * loop. Symmetric structure to the docId resolver, but works\n * against the materialised doc once the handle is ready — so a\n * peer-synced sentinel on the bound doc transparently rebinds the\n * wrapper to the new docId without a reload. */\nexport async function followRedirects<D>(repo: Repo, initial: DocHandle<D>): Promise<DocHandle<D>> {\n let current = initial;\n const seen = new Set<string>([current.documentId as unknown as string]);\n for (let i = 0; i < MAX_REDIRECT_FOLLOWS; i++) {\n const detector = redirectDetector;\n if (!detector) {\n return current;\n }\n let nextId: DocumentId | undefined;\n try {\n const doc = current.doc();\n if (!doc) {\n return current;\n }\n nextId = detector(doc);\n } catch {\n // Detector threw — keep current handle.\n return current;\n }\n if (!nextId) {\n return current;\n }\n const nextIdString = nextId as unknown as string;\n if (nextIdString === (current.documentId as unknown as string)) {\n return current;\n }\n if (seen.has(nextIdString)) {\n // Cycle. Stop following; consumer sees the current handle.\n return current;\n }\n seen.add(nextIdString);\n try {\n // `allowableStates: ['ready', 'unavailable']` lets find resolve\n // when the new doc syncs from a peer OR when polly gives up\n // and marks it unavailable. The wrapper subscribes to the\n // handle either way, so a slow-syncing redirect target shows\n // up in the consumer's UI the moment the bytes arrive.\n current = await repo.find<D>(nextId, {\n allowableStates: [\"ready\", \"unavailable\"],\n });\n } catch {\n // Find threw — fall back to the previous handle.\n return current;\n }\n }\n return current;\n}\n\n// $meshState\n\n/** Attach a rejection sink to a mesh primitive's `loaded` promise.\n * Polly#107 post-H5: an unawaited `loaded` rejection in a consumer\n * wrapper would vanish without trace; this captures the breadcrumb\n * on the module so a snapshot read can surface it. The original\n * primitive is returned unchanged — the `.catch` is a side-effect\n * sink that does not swallow the rejection from any consumer that\n * does await `loaded`. */\nfunction attachLoadedRejectionSink<P extends { loaded: Promise<unknown> }>(primitive: P): P {\n primitive.loaded.catch((err) => {\n recordLoadedRejection(err);\n });\n return primitive;\n}\n\n/**\n * Create a peer-replicated state primitive backed by Automerge with a mesh\n * transport. Every device holds a full replica; no central server holds a\n * replica. Operations flow peer-to-peer through signed and encrypted\n * messages on the underlying transport.\n *\n * @example\n * ```ts\n * const journal = $meshState<Journal>(\"journal\", { entries: [] });\n * await journal.loaded;\n * journal.value = { entries: [\"my private thoughts\"] };\n * ```\n */\nexport function $meshState<T extends VersionedDoc>(\n key: string,\n initialValue: T,\n options: MeshStateOptions = {}\n): CrdtPrimitive<T> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtState<T>({\n key,\n primitive: \"meshState\",\n initialValue,\n getHandle: buildHandleFactory<T>(repo, key, initialValue),\n resolveRedirect: buildRedirectResolver<T>(repo),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n/** Compose the caller-registered {@link RedirectDetector} (if any)\n * with the repo handle the wrapper is operating against, into the\n * `resolveRedirect` callback {@link $crdtState} consumes. Returns\n * `undefined` when no detector is registered, so $crdtState skips\n * the rebind path entirely and behaves exactly as it did pre-v3b.\n *\n * The closure captures the repo at construction time, so a later\n * {@link configureMeshState} call won't redirect an existing\n * wrapper to a different repo — matching how the initial handle is\n * resolved. */\nfunction buildRedirectResolver<T>(\n repo: Repo\n): ((handle: DocHandle<T>, doc: T) => Promise<DocHandle<T> | undefined>) | undefined {\n if (!redirectDetector) {\n // Detector might be registered later; return a callback that\n // checks at call time rather than locking in undefined now.\n }\n return async (_handle, doc) => {\n const detector = redirectDetector;\n if (!detector) return undefined;\n let nextId: DocumentId | undefined;\n try {\n nextId = detector(doc);\n } catch {\n return undefined;\n }\n if (!nextId) return undefined;\n try {\n return await repo.find<T>(nextId, {\n allowableStates: [\"ready\", \"unavailable\"],\n });\n } catch {\n return undefined;\n }\n };\n}\n\n// $meshText\n\n/**\n * Create a peer-replicated text primitive backed by a mesh transport.\n * Concurrent character-level edits from peers merge cleanly via Automerge's\n * updateText splicing, and every operation is signed and encrypted before\n * leaving the originating peer.\n */\nexport function $meshText(\n key: string,\n initialValue: string,\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<string> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtText(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<TextDoc>(repo, key, { text: initialValue }),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n// $meshCounter\n\n/**\n * Create a peer-replicated counter primitive backed by a mesh transport.\n * Concurrent increments commute, and every operation is signed and\n * encrypted before leaving the originating peer.\n */\nexport function $meshCounter(\n key: string,\n initialValue: number,\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<number> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtCounter(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<CounterDoc>(repo, key, {}),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n\n// $meshList\n\n/**\n * Create a peer-replicated list primitive backed by a mesh transport.\n * Phase 0 naive replacement applies to writes for now; Phase 1.1 will\n * refine with structural diff-to-splice for concurrent insert/remove\n * preservation.\n */\nexport function $meshList<T>(\n key: string,\n initialValue: T[],\n options: MeshStateOptions = {}\n): SpecialisedPrimitive<T[]> {\n const repo = resolveRepo(options.repo);\n return attachLoadedRejectionSink(\n $crdtList<T>(key, initialValue, {\n primitive: \"meshState\",\n getHandle: buildHandleFactory<ListDoc<T>>(repo, key, { items: initialValue }),\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n })\n );\n}\n",
|
|
19
19
|
"/**\n * crdt-specialised — text, counter, and list variants for Polly's peer-first\n * state primitives.\n *\n * Where the base $crdtState binds a signal to a whole Automerge document with\n * naive top-level structural assignment, the specialised variants each own a\n * single field inside their document and use type-specific Automerge\n * operations to mutate it. This is the difference between \"your write\n * survives a concurrent edit\" and \"your write clobbers the other peer's\n * edit,\" and it matters most for text editing where every keystroke from a\n * concurrent peer would otherwise be lost.\n *\n * - **$crdtText** stores its value in `doc.text` and writes via\n * `Automerge.updateText`, which computes the minimal sequence of character\n * splices between the previous and new strings and records each one as a\n * CRDT operation. Concurrent text edits merge.\n *\n * - **$crdtCounter** stores its value in `doc.count` as an `Automerge.Counter`\n * instance and writes via `counter.increment(delta)`. Increments commute\n * across peers, so two clients each adding 1 to a counter at 5 produce a\n * counter at 7 after merging — last-write-wins semantics on a plain number\n * would lose one of the increments.\n *\n * - **$crdtList** stores its value in `doc.items` and, for the Phase 0 cut,\n * uses naive whole-array replacement on every write. This is correct for\n * single-writer scenarios and good enough to exercise the rest of the\n * pipeline. Phase 1 will replace the write path with proper diff-to-splice\n * logic so that concurrent inserts and removes preserve list ordering.\n *\n * All three variants share a single internal factory that handles the\n * primitive-registry guard, migration-registry guard, schema-version\n * migration, hydration promise, two-way binding with an `updating` flag,\n * and the {@link MigratableState} interface. Variants differ only in the\n * `extractValue` and `applyWrite` hooks they pass.\n */\n\nimport { Counter, type DocHandle, updateText } from \"@automerge/automerge-repo/slim\";\nimport { effect, signal } from \"@preact/signals\";\nimport type { Access } from \"./access\";\nimport { type MigratableState, MigrationError, migrationRegistry } from \"./migrate-primitive\";\nimport { type PrimitiveKind, primitiveRegistry } from \"./primitive-registry\";\nimport { type Migrations, runMigrations, setDocVersion, type VersionedDoc } from \"./schema-version\";\n\n/**\n * Public interface for a specialised primitive. The signal value type V is\n * not the same as the underlying Automerge document — for example, $crdtText\n * exposes a string signal whose value lives at `doc.text` inside the\n * document. Implements {@link MigratableState} so the cross-primitive\n * migration helper can consume it directly.\n */\nexport interface SpecialisedPrimitive<V> extends MigratableState<V> {\n readonly key: string;\n readonly primitive: PrimitiveKind;\n value: V;\n readonly loaded: Promise<void>;\n readonly handle: DocHandle<unknown> | undefined;\n}\n\n/** Internal config shape consumed by {@link createSpecialisedPrimitive}. */\ninterface SpecialisedConfig<V, D extends VersionedDoc> {\n key: string;\n primitive: PrimitiveKind;\n initialValue: V;\n getHandle: () => Promise<DocHandle<D>>;\n /** Read the typed signal value out of the Automerge document. Called once\n * on hydration and again on every remote change. */\n extractValue: (doc: D) => V;\n /** Apply a typed signal value to the Automerge document inside the\n * `handle.change` block. Called every time the local signal is reassigned. */\n applyWrite: (doc: D, value: V) => void;\n schemaVersion?: number;\n migrations?: Migrations;\n access?: Access;\n callSite?: string;\n}\n\n/**\n * Shared factory. Identical guard, hydration, and binding wiring across all\n * three specialised variants; the variants differ only in the extract and\n * apply hooks they supply.\n */\nfunction createSpecialisedPrimitive<V, D extends VersionedDoc>(\n config: SpecialisedConfig<V, D>\n): SpecialisedPrimitive<V> {\n if (migrationRegistry.isMarked(config.key, config.primitive)) {\n throw new MigrationError(\n `Cannot construct $${config.primitive}(\"${config.key}\"): this key has been marked as migrated. Migrations are one-way; use the destination primitive instead.`,\n \"already-migrated\",\n config.key,\n config.primitive\n );\n }\n primitiveRegistry.register(config.key, config.primitive, config.callSite);\n\n const inner = signal<V>(config.initialValue);\n let updating = false;\n let currentHandle: DocHandle<D> | undefined;\n\n const loaded = (async () => {\n const handle = await config.getHandle();\n await handle.whenReady();\n currentHandle = handle;\n\n if (config.schemaVersion !== undefined) {\n const targetVersion = config.schemaVersion;\n const migrations = config.migrations ?? {};\n handle.change((doc) => {\n runMigrations(doc as unknown as Record<string, unknown>, targetVersion, migrations);\n setDocVersion(doc as unknown as Record<string, unknown>, targetVersion);\n });\n }\n\n updating = true;\n try {\n inner.value = config.extractValue(handle.doc());\n } finally {\n updating = false;\n }\n\n handle.on(\"change\", (payload) => {\n if (updating) return;\n updating = true;\n try {\n inner.value = config.extractValue(payload.doc);\n } finally {\n updating = false;\n }\n });\n\n effect(() => {\n const value = inner.value;\n if (updating) return;\n if (!currentHandle) return;\n updating = true;\n try {\n currentHandle.change((doc) => {\n config.applyWrite(doc, value);\n });\n } finally {\n updating = false;\n }\n });\n })();\n\n return {\n key: config.key,\n primitive: config.primitive,\n get value() {\n return inner.value;\n },\n set value(next: V) {\n inner.value = next;\n },\n loaded,\n get handle() {\n return currentHandle as unknown as DocHandle<unknown> | undefined;\n },\n };\n}\n\n// $crdtText\n\n/** The Automerge document shape backing a $crdtText primitive. */\nexport type TextDoc = VersionedDoc & { text?: string };\n\n/** Options for {@link $crdtText}. */\nexport interface CrdtTextOptions {\n /** Primitive kind label. Defaults to \"peerState\"; Phase 2's $meshState\n * wrapper passes \"meshState\" instead. */\n primitive?: PrimitiveKind;\n /** Async factory that returns a ready DocHandle for the text document. */\n getHandle: () => Promise<DocHandle<TextDoc>>;\n schemaVersion?: number;\n migrations?: Migrations;\n access?: Access;\n callSite?: string;\n}\n\n/**\n * Create a CRDT-backed text primitive. The signal exposes a plain string;\n * writes are diffed into character-level splices via `Automerge.updateText`\n * so that concurrent edits from peers merge cleanly rather than clobbering\n * each other.\n */\nexport function $crdtText(\n key: string,\n initialValue: string,\n options: CrdtTextOptions\n): SpecialisedPrimitive<string> {\n return createSpecialisedPrimitive<string, TextDoc>({\n key,\n primitive: options.primitive ?? \"peerState\",\n initialValue,\n getHandle: options.getHandle,\n extractValue: (doc) => doc.text ?? \"\",\n applyWrite: (doc, value) => {\n if (doc.text === undefined) {\n // First write — seed the field. Subsequent writes will use updateText.\n (doc as unknown as TextDoc).text = value;\n } else {\n updateText(doc, [\"text\"], value);\n }\n },\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n callSite: options.callSite,\n });\n}\n\n// $crdtCounter\n\n/** The Automerge document shape backing a $crdtCounter primitive. */\nexport type CounterDoc = VersionedDoc & { count?: Counter };\n\n/** Options for {@link $crdtCounter}. */\nexport interface CrdtCounterOptions {\n primitive?: PrimitiveKind;\n getHandle: () => Promise<DocHandle<CounterDoc>>;\n schemaVersion?: number;\n migrations?: Migrations;\n access?: Access;\n callSite?: string;\n}\n\n/**\n * Create a CRDT-backed counter primitive. The signal exposes a plain number;\n * writes compute the delta from the document's current value and call\n * `counter.increment(delta)` on the underlying `Automerge.Counter`. Concurrent\n * increments from peers commute, so two clients each adding 1 to a counter at\n * 5 produce a counter at 7 after merging.\n *\n * Application code that wants to express increments idiomatically can write\n * `counter.value = counter.value + 1`; the signal's reactivity captures the\n * read-then-write pattern and the factory translates it into a proper CRDT\n * increment operation underneath.\n */\nexport function $crdtCounter(\n key: string,\n initialValue: number,\n options: CrdtCounterOptions\n): SpecialisedPrimitive<number> {\n return createSpecialisedPrimitive<number, CounterDoc>({\n key,\n primitive: options.primitive ?? \"peerState\",\n initialValue,\n getHandle: options.getHandle,\n extractValue: (doc) => {\n const c = doc.count;\n if (c === undefined) return 0;\n return c.value;\n },\n applyWrite: (doc, value) => {\n const existing = doc.count;\n if (existing === undefined) {\n (doc as unknown as CounterDoc).count = new Counter(value);\n } else {\n const delta = value - existing.value;\n if (delta !== 0) {\n existing.increment(delta);\n }\n }\n },\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n callSite: options.callSite,\n });\n}\n\n// $crdtList\n\n/** The Automerge document shape backing a $crdtList primitive. */\nexport type ListDoc<T> = VersionedDoc & { items?: T[] };\n\n/** Options for {@link $crdtList}. */\nexport interface CrdtListOptions<T> {\n primitive?: PrimitiveKind;\n getHandle: () => Promise<DocHandle<ListDoc<T>>>;\n schemaVersion?: number;\n migrations?: Migrations;\n access?: Access;\n callSite?: string;\n}\n\n/**\n * Create a CRDT-backed list primitive. The signal exposes a plain array;\n * for the Phase 0 cut, writes replace the underlying array wholesale inside\n * an `Automerge.change` block. This is correct for single-writer scenarios\n * and is the simplest way to ship a working list primitive on the same\n * pipeline as text and counter.\n *\n * Phase 1 will replace the write path with proper structural diffing into\n * insert and remove operations so that concurrent edits from peers preserve\n * list ordering. Until then, applications using $crdtList in a multi-writer\n * setting should expect last-writer-wins semantics on the array as a whole.\n */\nexport function $crdtList<T>(\n key: string,\n initialValue: T[],\n options: CrdtListOptions<T>\n): SpecialisedPrimitive<T[]> {\n return createSpecialisedPrimitive<T[], ListDoc<T>>({\n key,\n primitive: options.primitive ?? \"peerState\",\n initialValue,\n getHandle: options.getHandle,\n extractValue: (doc) => (doc.items ? [...doc.items] : []),\n applyWrite: (doc, value) => {\n // Phase 0 naive replacement; see the function-level docstring for the\n // refinement plan.\n (doc as unknown as ListDoc<T>).items = [...value];\n },\n schemaVersion: options.schemaVersion,\n migrations: options.migrations,\n access: options.access,\n callSite: options.callSite,\n });\n}\n",
|
|
20
20
|
"/**\n * migrate-primitive — one-way cross-primitive data migration helper.\n *\n * Moving data from one Polly primitive to another ($sharedState to $peerState,\n * $peerState to $meshState, etc.) is a deliberate, one-time, application-authored\n * operation. The three primitive families serialise differently — LWW value plus\n * Lamport clock, plaintext Automerge ops, signed and encrypted Automerge ops —\n * and Polly never silently coerces between them. An application that wants to\n * move data explicitly reads the current value from the source, applies a\n * user-supplied transform, writes the result into the destination, and marks\n * the source as migrated so subsequent reads from it fail loudly rather than\n * returning stale data.\n *\n * This module provides the helper ({@link migratePrimitive}) and the migration\n * registry ({@link migrationRegistry}) that records which (key, primitive kind)\n * pairs have been migrated. Primitive read paths consult the registry at load\n * time and refuse to hydrate sources that have been marked. Migration is one-way\n * by design: there is no rollback, and running a migration twice throws.\n *\n * The registry is in-memory only. Persistence of the migrated flag across page\n * loads is each primitive's own responsibility at its storage boundary; Phase 1\n * and Phase 2 primitives will restore the registry state on startup from their\n * local stores.\n *\n * @example\n * ```ts\n * await migratePrimitive(\n * $sharedState<OldNotes>(\"notes\"),\n * $meshState<NewNotes>(\"notes\", { entries: [] }),\n * (old) => ({ entries: old.entries ?? [] }),\n * );\n * ```\n */\n\nimport type { PrimitiveKind } from \"./primitive-registry\";\n\n/**\n * Minimal interface that every migratable state primitive must satisfy. Real\n * primitive instances will implement this plus everything else their type\n * expects; tests construct plain objects.\n */\nexport interface MigratableState<T> {\n /** Stable logical key the primitive was registered under. */\n readonly key: string;\n /** The primitive kind owning this state. Used to identify entries in the\n * migration registry alongside the key. */\n readonly primitive: PrimitiveKind;\n /** Current value. Must be readable and writable. */\n value: T;\n /** Hydration promise. The migration helper awaits this on both source and\n * destination before reading the source value. */\n readonly loaded: Promise<void>;\n}\n\n/**\n * Error thrown by the migration subsystem. The {@link code} field distinguishes\n * the failure modes so callers can branch on them.\n */\nexport class MigrationError extends Error {\n readonly code: \"already-migrated\" | \"same-primitive-instance\";\n readonly key: string;\n readonly primitive: PrimitiveKind;\n\n constructor(\n message: string,\n code: MigrationError[\"code\"],\n key: string,\n primitive: PrimitiveKind\n ) {\n super(message);\n this.name = \"MigrationError\";\n this.code = code;\n this.key = key;\n this.primitive = primitive;\n }\n}\n\n/**\n * In-memory registry of migrated (key, primitive kind) pairs. Exported as a\n * class so tests can construct fresh instances; application code uses the\n * module-level {@link migrationRegistry} singleton.\n */\nexport class MigrationRegistry {\n private readonly marks = new Set<string>();\n\n private entryKey(key: string, primitive: PrimitiveKind): string {\n return `${primitive}:${key}`;\n }\n\n /** Mark a source primitive as migrated. Idempotent. */\n mark(key: string, primitive: PrimitiveKind): void {\n this.marks.add(this.entryKey(key, primitive));\n }\n\n /** Check whether a source primitive has been marked as migrated. */\n isMarked(key: string, primitive: PrimitiveKind): boolean {\n return this.marks.has(this.entryKey(key, primitive));\n }\n\n /** Drop every mark. Intended for tests; application code should not call this. */\n clear(): void {\n this.marks.clear();\n }\n\n /** Number of recorded marks. Intended for tests. */\n get size(): number {\n return this.marks.size;\n }\n}\n\n/**\n * The process-wide migration registry. Primitives and application migrations\n * both consult it: tests can reset it with {@link MigrationRegistry.clear}.\n */\nexport const migrationRegistry = new MigrationRegistry();\n\n/**\n * Migrate data from one Polly primitive to another. Reads the source's current\n * value, applies the caller's transform, writes the result to the destination,\n * and marks the source as migrated so subsequent reads fail loudly.\n *\n * The helper is one-way and one-time. Running it twice on the same source\n * throws a {@link MigrationError} with code `already-migrated`. Running it\n * with the same object as both source and destination throws with code\n * `same-primitive-instance`.\n *\n * Applications invoke this explicitly on upgrade, once per device, typically\n * inside a startup hook before the primitives that depend on the migrated data\n * are read. It is not a replacement for the schema-version migration protocol\n * inside a single primitive — that handles shape evolution within one primitive\n * family, whereas this helper handles moves between primitive families.\n */\nexport async function migratePrimitive<Source, Destination>(\n source: MigratableState<Source>,\n destination: MigratableState<Destination>,\n transform: (value: Source) => Destination\n): Promise<void> {\n if ((source as unknown as unknown) === (destination as unknown as unknown)) {\n throw new MigrationError(\n `Cannot migrate a primitive to itself: \"${source.key}\" under ${source.primitive}.`,\n \"same-primitive-instance\",\n source.key,\n source.primitive\n );\n }\n if (migrationRegistry.isMarked(source.key, source.primitive)) {\n throw new MigrationError(\n `Cannot migrate: source \"${source.key}\" under $${source.primitive} has already been migrated. Migrations are one-way and one-time.`,\n \"already-migrated\",\n source.key,\n source.primitive\n );\n }\n await source.loaded;\n await destination.loaded;\n const transformed = transform(source.value);\n destination.value = transformed;\n migrationRegistry.mark(source.key, source.primitive);\n}\n",
|
|
21
21
|
"/**\n * PrimitiveRegistry — runtime namespace collision detection across Polly's\n * synced state primitives.\n *\n * The three primitive families ($sharedState, $peerState, $meshState) each store\n * data under a developer-chosen logical key. If two different primitives both\n * claim the same key, the developer almost certainly has a bug: the on-disk\n * formats are incompatible, no sync happens between them, and whichever primitive\n * resolves first silently \"wins\" from the developer's perspective. By the time\n * the mistake is noticed, data has diverged.\n *\n * The registry catches the mistake at the first mismatched registration and\n * throws a structured error naming the key, both primitives, and (when available)\n * the call site of each registration. This is run-to-failure by design: a\n * collision is always a bug, and the failure should be loud.\n *\n * Same primitive re-registering the same key is allowed and is a no-op — it\n * supports hot module reloading and component re-mounts without spurious errors.\n * Changing the primitive kind of an existing key is still an error; developers\n * doing that during local HMR should hard-reload to reset the registry.\n *\n * @example\n * ```ts\n * primitiveRegistry.register(\"notes\", \"sharedState\", \"src/app.ts:10\");\n * primitiveRegistry.register(\"notes\", \"peerState\", \"src/other.ts:22\");\n * // throws PrimitiveCollisionError — names both primitives and both call sites\n * ```\n */\n\n/**\n * Canonical identifiers for Polly's synced state primitives. The registry\n * uses these as opaque labels; nothing else in Polly needs to match them.\n */\nexport type PrimitiveKind =\n | \"sharedState\"\n | \"syncedState\"\n | \"persistedState\"\n | \"state\"\n | \"peerState\"\n | \"meshState\";\n\n/**\n * Thrown when a logical key is registered under more than one primitive.\n * The message names the key, both primitives, and (when available) the\n * call site of each registration, so the developer can navigate to both\n * sites from the error output.\n */\nexport class PrimitiveCollisionError extends Error {\n readonly key: string;\n readonly firstPrimitive: PrimitiveKind;\n readonly firstCallSite: string | undefined;\n readonly secondPrimitive: PrimitiveKind;\n readonly secondCallSite: string | undefined;\n\n constructor(\n key: string,\n firstPrimitive: PrimitiveKind,\n firstCallSite: string | undefined,\n secondPrimitive: PrimitiveKind,\n secondCallSite: string | undefined\n ) {\n const firstLocation = firstCallSite ? ` (at ${firstCallSite})` : \"\";\n const secondLocation = secondCallSite ? ` (at ${secondCallSite})` : \"\";\n super(\n `Polly primitive key collision: \"${key}\" is already registered as ` +\n `$${firstPrimitive}${firstLocation} and cannot also be registered ` +\n `as $${secondPrimitive}${secondLocation}. Pick a different key or ` +\n `use the same primitive in both places.`\n );\n this.name = \"PrimitiveCollisionError\";\n this.key = key;\n this.firstPrimitive = firstPrimitive;\n this.firstCallSite = firstCallSite;\n this.secondPrimitive = secondPrimitive;\n this.secondCallSite = secondCallSite;\n }\n}\n\ntype RegistryEntry = {\n primitive: PrimitiveKind;\n callSite: string | undefined;\n};\n\n/**\n * A small Map-backed registry of \"logical key → primitive kind\". Exported as\n * a class so tests can construct fresh instances without sharing state; the\n * module-level {@link primitiveRegistry} singleton is what application code\n * actually uses.\n */\nexport class PrimitiveRegistry {\n private readonly entries = new Map<string, RegistryEntry>();\n\n /**\n * Register a key under a primitive kind. Re-registering the same key under\n * the same primitive is a no-op, which is what hot module reloading and\n * component re-mounts produce.\n *\n * @throws {PrimitiveCollisionError} if the key is already registered under\n * a different primitive kind.\n */\n register(key: string, primitive: PrimitiveKind, callSite?: string): void {\n const existing = this.entries.get(key);\n if (existing && existing.primitive !== primitive) {\n throw new PrimitiveCollisionError(\n key,\n existing.primitive,\n existing.callSite,\n primitive,\n callSite\n );\n }\n if (!existing) {\n this.entries.set(key, { primitive, callSite });\n }\n }\n\n /**\n * True if the key has been registered (under any primitive kind).\n */\n has(key: string): boolean {\n return this.entries.has(key);\n }\n\n /**\n * Look up the primitive kind a key is registered under, if any.\n * Returns undefined for unregistered keys.\n */\n kindOf(key: string): PrimitiveKind | undefined {\n return this.entries.get(key)?.primitive;\n }\n\n /**\n * Drop every registration. Intended for test setup and teardown; application\n * code should not call this.\n */\n clear(): void {\n this.entries.clear();\n }\n\n /**\n * Number of registered keys. Intended for tests.\n */\n get size(): number {\n return this.entries.size;\n }\n}\n\n/**\n * The process-wide primitive registry. Application code registers here\n * implicitly via primitive constructors; tests can reset it with `clear()`.\n */\nexport const primitiveRegistry = new PrimitiveRegistry();\n",
|
|
22
22
|
"/**\n * schema-version — plumbing for the shared schema-version migration protocol\n * used by $peerState and $meshState documents.\n *\n * Every peer-first document carries a reserved schema-version field. When a\n * client loads a document whose stored version is lower than the application's\n * declared target version, Polly walks the registered migrations in sequence,\n * mutating the document from one version to the next, and stamps the new\n * version on the way through. When the stored version is higher, Polly refuses\n * to load and surfaces a structured error: the application is older than the\n * document and should be upgraded.\n *\n * For concurrent writes across mixed client versions, every op also carries\n * the schema version under which it was produced. Ops whose version is lower\n * than the document's current version are rejected at sync time; the peer\n * producing them needs to upgrade its local replica through the migration\n * runner before retrying. Ops whose version is higher mean the current peer\n * is behind the application that produced them; rejecting them prevents\n * corruption while still surfacing the mismatch to the user.\n *\n * This module provides only the plumbing — the reserved field name, the\n * reader and writer for the version field, the migration runner, and the\n * op-version compatibility check. Phase 1 and Phase 2 primitives will consume\n * these helpers; nothing in this file depends on Automerge or any transport.\n */\n\n/**\n * The reserved field name used to store the schema version on every peer-first\n * document. Applications must not use this field for their own data; the\n * primitive constructors reserve it at the document boundary.\n */\nexport const SCHEMA_VERSION_FIELD = \"__schemaVersion\" as const;\n\n/**\n * The minimal shape every peer-first document satisfies. Applications layer\n * their own fields on top of this; the reserved {@link SCHEMA_VERSION_FIELD}\n * is the only required key.\n */\nexport type VersionedDoc = {\n [SCHEMA_VERSION_FIELD]?: number;\n [otherField: string]: unknown;\n};\n\n/**\n * A single migration step. Mutates the document in place, transforming it\n * from version (target - 1) to version target. The implementation must be\n * total (accept any valid v(target-1) document) and deterministic (running\n * it twice produces the same result). Applications that want return-a-new-doc\n * semantics should copy inside the migration body rather than returning.\n */\nexport type Migration = (doc: Record<string, unknown>) => void;\n\n/**\n * A map from target schema version to the migration that produces it. The\n * keys must be contiguous from 1 upward through the application's target\n * version, with no gaps. Gaps throw at migration time.\n *\n * @example\n * ```ts\n * const migrations: Migrations = {\n * 1: (doc) => { doc[\"title\"] = \"\"; }, // v0 → v1\n * 2: (doc) => { doc[\"tags\"] = []; }, // v1 → v2\n * 3: (doc) => { doc[\"archived\"] = false; delete doc[\"deleted\"]; }, // v2 → v3\n * };\n * ```\n */\nexport type Migrations = Record<number, Migration>;\n\n/**\n * Error thrown by the schema-version subsystem. The `code` field distinguishes\n * the failure modes so that callers (primitive constructors, application error\n * handlers) can decide whether to surface the error to the user, prompt an\n * upgrade, or drop the offending op.\n */\nexport class SchemaVersionError extends Error {\n readonly code:\n | \"doc-ahead-of-app\"\n | \"missing-migration\"\n | \"op-older-than-doc\"\n | \"op-newer-than-doc\";\n readonly docVersion?: number;\n readonly targetVersion?: number;\n readonly opVersion?: number;\n readonly missingVersion?: number;\n\n constructor(\n message: string,\n code: SchemaVersionError[\"code\"],\n details: {\n docVersion?: number;\n targetVersion?: number;\n opVersion?: number;\n missingVersion?: number;\n } = {}\n ) {\n super(message);\n this.name = \"SchemaVersionError\";\n this.code = code;\n if (details.docVersion !== undefined) this.docVersion = details.docVersion;\n if (details.targetVersion !== undefined) this.targetVersion = details.targetVersion;\n if (details.opVersion !== undefined) this.opVersion = details.opVersion;\n if (details.missingVersion !== undefined) this.missingVersion = details.missingVersion;\n }\n}\n\n/**\n * Read the schema version stored on a document. Returns 0 for documents that\n * have never been stamped (undefined field), which is the canonical \"pre-v1\"\n * sentinel — the first migration in a registry is always keyed at 1 and\n * handles the undefined-to-1 transition.\n */\nexport function getDocVersion(doc: unknown): number {\n if (typeof doc !== \"object\" || doc === null) return 0;\n const record = doc as unknown as Record<string, unknown>;\n const value = record[SCHEMA_VERSION_FIELD];\n return typeof value === \"number\" && Number.isInteger(value) && value >= 0 ? value : 0;\n}\n\n/**\n * Stamp a schema version onto a document in place. Primitive constructors call\n * this after each migration step and once on document creation.\n */\nexport function setDocVersion(doc: Record<string, unknown>, version: number): void {\n doc[SCHEMA_VERSION_FIELD] = version;\n}\n\n/**\n * Run any pending migrations on a document. Mutates the document in place.\n *\n * @throws {SchemaVersionError} with code `doc-ahead-of-app` if the document is\n * already at a version higher than the application's target.\n * @throws {SchemaVersionError} with code `missing-migration` if an intermediate\n * migration is missing from the registry.\n */\nexport function runMigrations(\n doc: Record<string, unknown>,\n targetVersion: number,\n migrations: Migrations\n): void {\n const current = getDocVersion(doc);\n if (current > targetVersion) {\n throw new SchemaVersionError(\n `Document is at schema version ${current} but the application targets ${targetVersion}. Upgrade the application to continue.`,\n \"doc-ahead-of-app\",\n { docVersion: current, targetVersion }\n );\n }\n for (let v = current + 1; v <= targetVersion; v++) {\n const migration = migrations[v];\n if (!migration) {\n throw new SchemaVersionError(\n `Missing migration for schema version ${v}. Migrations must be contiguous from ${current + 1} through ${targetVersion}.`,\n \"missing-migration\",\n { docVersion: current, targetVersion, missingVersion: v }\n );\n }\n migration(doc);\n setDocVersion(doc, v);\n }\n}\n\n/**\n * Result of an op-version compatibility check. Discriminated by the {@link compatible}\n * field so callers can switch cleanly.\n */\nexport type OpVersionCheck =\n | { compatible: true }\n | {\n compatible: false;\n reason: \"op-older-than-doc\" | \"op-newer-than-doc\";\n opVersion: number;\n docVersion: number;\n };\n\n/**\n * Check whether an incoming op's declared schema version is compatible with\n * the document's current version. Ops that match are applied; ops that are\n * older are rejected (the producing peer is behind and should migrate); ops\n * that are newer are also rejected (the current peer is behind and should\n * upgrade).\n */\nexport function checkOpVersion(opVersion: number, docVersion: number): OpVersionCheck {\n if (opVersion < docVersion) {\n return { compatible: false, reason: \"op-older-than-doc\", opVersion, docVersion };\n }\n if (opVersion > docVersion) {\n return { compatible: false, reason: \"op-newer-than-doc\", opVersion, docVersion };\n }\n return { compatible: true };\n}\n\n/**\n * Convenience wrapper around {@link checkOpVersion} that throws a structured\n * {@link SchemaVersionError} on incompatibility. Useful inside sync handlers\n * that want to surface the mismatch through their normal error pipeline.\n */\nexport function assertOpVersion(opVersion: number, docVersion: number): void {\n const result = checkOpVersion(opVersion, docVersion);\n if (result.compatible) return;\n const message =\n result.reason === \"op-older-than-doc\"\n ? `Incoming op was produced at schema version ${opVersion} but the document is at version ${docVersion}. The producing peer is behind.`\n : `Incoming op was produced at schema version ${opVersion} but the document is at version ${docVersion}. The current peer is behind and should upgrade.`;\n throw new SchemaVersionError(message, result.reason, { opVersion, docVersion });\n}\n",
|
|
23
|
-
"/**\n * crdt-state — base machinery for Polly's peer-first state primitives.\n *\n * This module is transport-agnostic: it takes a caller-supplied async factory\n * that produces a ready {@link DocHandle}, binds it bidirectionally to a\n * Preact signal, runs any pending schema migrations on load, and integrates\n * with the primitive-registry and migration-registry guards. Phase 1's\n * $peerState and Phase 2's $meshState both construct these base primitives\n * with their own handle factories — one over Automerge-Repo's WebSocket\n * client adapter, the other over WebRTC — and the base never knows which.\n *\n * The signal-to-handle binding uses an `updating` guard flag to prevent write\n * loops: when a local signal assignment runs the effect that pushes the value\n * into `handle.change`, the flag is raised so that the 'change' event the\n * handle fires back is ignored. The same flag protects in the other direction\n * when a remote change seeds the signal.\n *\n * For the Phase 0 cut, writes are applied with a naive top-level structural\n * replacement inside the `Automerge.change` block. This is correct for\n * JSON-shaped documents with scalar and flat-object fields and is good enough\n * to exercise the rest of the pipeline. The specialised variants for text,\n * counters, and lists (which require type-specific operation capture to\n * preserve concurrent-edit semantics) land in Phase 1's crdt-specialised.ts.\n */\n\nimport type { DocHandle } from \"@automerge/automerge-repo/slim\";\nimport { effect, signal } from \"@preact/signals\";\nimport type { Access } from \"./access\";\nimport { type MigratableState, MigrationError, migrationRegistry } from \"./migrate-primitive\";\nimport { type PrimitiveKind, primitiveRegistry } from \"./primitive-registry\";\nimport {\n type Migrations,\n runMigrations,\n SCHEMA_VERSION_FIELD,\n setDocVersion,\n type VersionedDoc,\n} from \"./schema-version\";\n\n/**\n * The interface a Polly peer-first primitive exposes at the call site. It\n * satisfies {@link MigratableState} so that the cross-primitive migration\n * helper can consume it directly.\n */\nexport interface CrdtPrimitive<T extends VersionedDoc> extends MigratableState<T> {\n /** Stable logical key the primitive was registered under. */\n readonly key: string;\n /** Primitive kind — one of the {@link PrimitiveKind} labels. */\n readonly primitive: PrimitiveKind;\n /** Current value. Writes push into the backing Automerge document. */\n value: T;\n /** Resolves when the handle is ready and migrations have run. */\n readonly loaded: Promise<void>;\n /** The underlying {@link DocHandle}, populated after {@link loaded} resolves.\n * Intended for advanced escape hatches; most callers should stay at the\n * signal surface. */\n readonly handle: DocHandle<T> | undefined;\n}\n\n/**\n * Options for constructing a base CRDT-backed primitive. Phase 1 and Phase 2\n * primitive constructors pass a transport-specific {@link getHandle} factory\n * and their own {@link primitive} label; everything else is shared.\n */\nexport interface CrdtStateOptions<T extends VersionedDoc> {\n /** Stable logical key identifying this piece of state. */\n key: string;\n /** Primitive kind label for registry and error-message purposes. */\n primitive: PrimitiveKind;\n /** Initial value if no stored document exists yet. Applied by the caller's\n * handle factory; the base module does not create documents itself. */\n initialValue: T;\n /** Async factory that resolves to a ready {@link DocHandle}. The factory is\n * responsible for repo lookup, document creation, and any transport-specific\n * setup. The base module calls this once, during hydration. */\n getHandle: () => Promise<DocHandle<T>>;\n /** Target schema version for the application. If set, migrations run on\n * load to bring the document up to this version before the signal hydrates. */\n schemaVersion?: number;\n /** Migration table. Ignored if {@link schemaVersion} is not set. */\n migrations?: Migrations;\n /** Declarative access predicates. Not consumed by the base module; the\n * transport-specific constructors compile it to their enforcement layer. */\n access?: Access;\n /** Optional free-text call-site label for primitive-registry error messages. */\n callSite?: string;\n}\n\n/**\n * Construct a base CRDT-backed Polly primitive. Integrates with\n * primitive-registry (for collision detection), migration-registry (for\n * cross-family migration guards), and schema-version (for on-load migrations).\n *\n * @throws {MigrationError} if the source key has been marked as migrated.\n * @throws {PrimitiveCollisionError} if the key is already registered under a\n * different primitive kind.\n */\nexport function $crdtState<T extends VersionedDoc>(options: CrdtStateOptions<T>): CrdtPrimitive<T> {\n if (migrationRegistry.isMarked(options.key, options.primitive)) {\n throw new MigrationError(\n `Cannot construct $${options.primitive}(\"${options.key}\"): this key has been marked as migrated. Migrations are one-way; use the destination primitive instead.`,\n \"already-migrated\",\n options.key,\n options.primitive\n );\n }\n primitiveRegistry.register(options.key, options.primitive, options.callSite);\n\n const inner = signal<T>(options.initialValue);\n let updating = false;\n let currentHandle: DocHandle<T> | undefined;\n\n const loaded = (async () => {\n const handle = await options.getHandle();\n await handle.whenReady();\n currentHandle = handle;\n\n // Run any pending schema migrations inside a change block so they land\n // as a single Automerge operation set.\n if (options.schemaVersion !== undefined) {\n const targetVersion = options.schemaVersion;\n const migrations = options.migrations ?? {};\n handle.change((doc) => {\n runMigrations(doc as unknown as Record<string, unknown>, targetVersion, migrations);\n // runMigrations stamps the version on every intermediate step; make\n // sure the final value is recorded even when no migrations ran.\n setDocVersion(doc as unknown as Record<string, unknown>, targetVersion);\n });\n }\n\n // Seed the signal with the hydrated doc state. Raise the guard first so\n // the 'change' listener we install below does not echo this write back.\n updating = true;\n try {\n inner.value = cloneDoc(handle.doc());\n } finally {\n updating = false;\n }\n\n // Remote-changes-to-signal binding.\n handle.on(\"change\", (payload) => {\n if (updating) return;\n updating = true;\n try {\n inner.value = cloneDoc(payload.doc);\n } finally {\n updating = false;\n }\n });\n\n // Signal-to-remote binding. The effect runs once on registration with\n // the already-hydrated value; the guard makes that first run a no-op at\n // the handle level because updating is false but the doc already equals\n // the signal, so Automerge records no new operations.\n effect(() => {\n const value = inner.value;\n if (updating) return;\n if (!currentHandle) return;\n updating = true;\n try {\n currentHandle.change((doc) => {\n applyTopLevel(doc as unknown as Record<string, unknown>, value);\n });\n } finally {\n updating = false;\n }\n });\n })();\n\n return {\n key: options.key,\n primitive: options.primitive,\n get value() {\n return inner.value;\n },\n set value(next: T) {\n inner.value = next;\n },\n loaded,\n get handle() {\n return currentHandle;\n },\n };\n}\n\n/**\n * Shallow clone of an Automerge doc into a plain JS object. Automerge docs\n * are Proxies; the signal holds a detached plain-object snapshot so that\n * application code does not accidentally mutate the CRDT through the signal.\n */\nfunction cloneDoc<T>(doc: T): T {\n return JSON.parse(JSON.stringify(doc)) as unknown as T;\n}\n\n/**\n * Copy every top-level field from the incoming value onto the Automerge doc\n * — but only for fields whose serialised form actually differs from what's\n * already there. The equality skip is load-bearing, not an optimisation:\n *\n * - Automerge records a new operation for every assignment (`doc[k] = v`),\n * even when v is structurally identical to the current value.\n * - Every op triggers a state-machine transition, which via\n * `#checkForChanges` fires a `change` event, which the signal binding\n * above handles by setting `inner.value = cloneDoc(...)`.\n * - Under certain event-loop timings — most consistently on bun — preact\n * signals' synchronous effect propagation re-runs this write path with\n * the just-cloned value, which compares ref-different to the previous\n * signal value, so the effect doesn't short-circuit, so applyTopLevel\n * runs again, so more ops are recorded, and the whole chain loops until\n * preact's own cycle counter trips with \"Cycle detected\".\n *\n * Skipping value-equal assignments makes the write a true no-op at the\n * Automerge level, which makes `docChanged` in `#checkForChanges` false,\n * which skips the change emission, which breaks the loop at its origin.\n * Browsers mask this under typical interactive timing; a tight CLI boot\n * reproduces it every time.\n *\n * The reserved schema-version field is not copied — it is managed by the\n * migration subsystem and must not be overwritten by application writes.\n */\nfunction applyTopLevel<T extends VersionedDoc>(doc: Record<string, unknown>, value: T): void {\n const source = value as unknown as Record<string, unknown>;\n for (const key of Object.keys(value)) {\n if (key === SCHEMA_VERSION_FIELD) continue;\n const incoming = source[key];\n if (fieldEquals(doc[key], incoming)) continue;\n doc[key] = incoming;\n }\n}\n\n/** Structural equality check for the top-level-field comparison. JSON.stringify\n * round-trip because Automerge docs are Proxies and `===` would miss a match\n * when the proxy wraps an equal value. Arrays, objects, and primitives all\n * round-trip cleanly for the CRDT-state use case. Cycles in the input aren't\n * supported (neither is Automerge — it refuses cyclic structures). */\nfunction fieldEquals(a: unknown, b: unknown): boolean {\n if (a === b) return true;\n try {\n return JSON.stringify(a) === JSON.stringify(b);\n } catch {\n return false;\n }\n}\n",
|
|
24
|
-
"/**\n * mesh-webrtc-adapter — Phase 2 browser-side WebRTC transport for Polly's\n * $meshState primitive. Extends Automerge's NetworkAdapter base class and\n * uses real native RTCPeerConnection / RTCDataChannel instances to carry\n * sync messages between peers.\n *\n * This is the \"base\" transport that MeshNetworkAdapter wraps with its\n * sign-then-encrypt envelope. The stack is:\n *\n * $meshState\n * └─ Repo\n * └─ MeshNetworkAdapter (sign + encrypt)\n * └─ MeshWebRTCAdapter (real data channels)\n * └─ MeshSignalingClient (SDP/ICE relay)\n * └─ signalingServer (Elysia plugin on the host app)\n *\n * Because WebRTC lives in browsers, this module is browser-only. It\n * assumes global RTCPeerConnection, RTCDataChannel, and WebSocket types\n * are available. Under bun:test the classes cannot be exercised\n * end-to-end — the first validation path is either Playwright running a\n * real browser, a Puppeteer harness, or a human testing a browser-side\n * example app that consumes the adapter.\n *\n * What this module does at runtime:\n *\n * - Constructs a MeshWebRTCAdapter with a signalling client and a local\n * peer id. No data channels exist at construction time.\n *\n * - When Automerge's NetworkSubsystem calls connect(peerId) on the\n * adapter, it starts listening for signals from remote peers and is\n * ready to build peer connections as they are discovered.\n *\n * - When a remote peer sends an SDP offer via the signalling channel,\n * the adapter builds a fresh RTCPeerConnection, accepts the offer,\n * produces an answer, sends it back through signalling, and wires the\n * received data channel to emit Automerge Message events upward.\n *\n * - When the local repo calls send(message), the adapter looks up the\n * peer connection for message.targetId and writes the serialised\n * bytes to its data channel. If no connection exists yet, the adapter\n * creates one by sending an SDP offer through signalling. Outgoing\n * messages are queued until the channel is open.\n *\n * - Disconnect tears down every peer connection and closes the\n * signalling client.\n */\n\nimport {\n type Message,\n NetworkAdapter,\n type PeerId,\n type PeerMetadata,\n} from \"@automerge/automerge-repo/slim\";\nimport { isBlobMessageType } from \"./blob-transfer\";\nimport type { MeshKeyring } from \"./mesh-network-adapter\";\nimport type { MeshSignalingClient } from \"./mesh-signaling-client\";\nimport {\n chunkSyncMessage,\n isSyncFragmentType,\n parseSyncFragment,\n reassembleSyncFragments,\n SYNC_FRAGMENT_CHUNK_SIZE,\n SYNC_FRAGMENT_THRESHOLD,\n} from \"./sync-fragment\";\n\n/** Distil an {@link RTCStatsReport} into a {@link TransportSnapshot}.\n * Resolves the selected candidate pair via the transport stat's\n * `selectedCandidatePairId` (the W3C-spec field that names the pair\n * ICE has nominated for the data path), then pulls the candidate-type\n * labels from the linked candidate stats. Retransmission counters\n * come off the transport stat where present; some implementations\n * surface them on the data-channel stat instead — we accept either.\n *\n * Out-of-band so the test bed can call it against a bare\n * {@link RTCPeerConnection} as well, which the polly#105 harness's\n * `probe-stats.ts` does to validate the stats shape before threading\n * the result through the polly layer. */\nasync function collectTransportSnapshot(\n connection: RTCPeerConnection,\n lastDataChannelError: string | undefined\n): Promise<TransportSnapshot> {\n const at = performance.now();\n let report: RTCStatsReport | undefined;\n try {\n report = await connection.getStats();\n } catch {\n return emptyTransportSnapshot(lastDataChannelError, at);\n }\n const parsed = partitionStats(report);\n const selectedPair = selectActivePair(parsed);\n const selectedCandidatePair = selectedPair\n ? buildCandidatePairView(selectedPair, parsed.localCands, parsed.remoteCands)\n : undefined;\n return {\n selectedCandidatePair,\n retransmittedPacketsSent: parsed.retransmittedPacketsSent,\n retransmittedBytesSent: parsed.retransmittedBytesSent,\n lastDataChannelError,\n at,\n };\n}\n\ninterface ParsedStats {\n localCands: Map<string, Record<string, unknown>>;\n remoteCands: Map<string, Record<string, unknown>>;\n pairs: Map<string, Record<string, unknown>>;\n selectedPairId: string | undefined;\n retransmittedPacketsSent: number | undefined;\n retransmittedBytesSent: number | undefined;\n}\n\nfunction emptyTransportSnapshot(\n lastDataChannelError: string | undefined,\n at: number\n): TransportSnapshot {\n return {\n selectedCandidatePair: undefined,\n retransmittedPacketsSent: undefined,\n retransmittedBytesSent: undefined,\n lastDataChannelError,\n at,\n };\n}\n\nfunction partitionStats(report: RTCStatsReport): ParsedStats {\n const out: ParsedStats = {\n localCands: new Map(),\n remoteCands: new Map(),\n pairs: new Map(),\n selectedPairId: undefined,\n retransmittedPacketsSent: undefined,\n retransmittedBytesSent: undefined,\n };\n const iter = (report as { values?: () => Iterable<unknown> }).values?.() ?? [];\n for (const raw of iter) {\n if (!raw || typeof raw !== \"object\") continue;\n const stat = raw as Record<string, unknown>;\n ingestStat(stat, out);\n }\n return out;\n}\n\nfunction ingestStat(stat: Record<string, unknown>, out: ParsedStats): void {\n const id = String(stat[\"id\"]);\n switch (stat[\"type\"]) {\n case \"local-candidate\":\n out.localCands.set(id, stat);\n return;\n case \"remote-candidate\":\n out.remoteCands.set(id, stat);\n return;\n case \"candidate-pair\":\n out.pairs.set(id, stat);\n return;\n case \"transport\":\n ingestTransport(stat, out);\n return;\n case \"data-channel\":\n ingestDataChannel(stat, out);\n return;\n }\n}\n\nfunction ingestTransport(stat: Record<string, unknown>, out: ParsedStats): void {\n const selectedId = stat[\"selectedCandidatePairId\"];\n if (typeof selectedId === \"string\") out.selectedPairId = selectedId;\n const rp = stat[\"retransmittedPacketsSent\"];\n const rb = stat[\"retransmittedBytesSent\"];\n if (typeof rp === \"number\") out.retransmittedPacketsSent = rp;\n if (typeof rb === \"number\") out.retransmittedBytesSent = rb;\n}\n\nfunction ingestDataChannel(stat: Record<string, unknown>, out: ParsedStats): void {\n // Chrome puts SCTP retransmit counters on the data-channel stat for\n // SCTP-backed channels. werift puts them on the transport. Accept\n // whichever the underlying impl produces first.\n const rp = stat[\"retransmittedPacketsSent\"];\n const rb = stat[\"retransmittedBytesSent\"];\n if (out.retransmittedPacketsSent === undefined && typeof rp === \"number\") {\n out.retransmittedPacketsSent = rp;\n }\n if (out.retransmittedBytesSent === undefined && typeof rb === \"number\") {\n out.retransmittedBytesSent = rb;\n }\n}\n\nfunction selectActivePair(parsed: ParsedStats): Record<string, unknown> | undefined {\n if (parsed.selectedPairId) {\n const named = parsed.pairs.get(parsed.selectedPairId);\n if (named) return named;\n }\n for (const pair of parsed.pairs.values()) {\n if (pair[\"nominated\"]) return pair;\n }\n return undefined;\n}\n\n/** Render a {@link PeerSlot} as the plain-data view\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot} returns. Pulled out\n * so the snapshot method itself stays under biome's cognitive-\n * complexity ceiling — every additional optional field would push it\n * one closer; doing the shape here keeps the per-call diff small. */\nfunction serialiseSlotView(slot: PeerSlot): {\n signalingState: string;\n iceConnectionState: string;\n connectionState: string;\n dataChannelState: string;\n pendingSendCount: number;\n pendingRemoteIceCount: number;\n inFlightSync: InFlightSyncSnapshot | undefined;\n transport: TransportSnapshot | undefined;\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n handles: Record<string, HandleSyncSnapshot>;\n} {\n const handles: Record<string, HandleSyncSnapshot> = {};\n for (const [docId, snapshot] of slot.handles) {\n handles[docId] = { ...snapshot };\n }\n return {\n signalingState: slot.connection.signalingState,\n iceConnectionState: slot.connection.iceConnectionState,\n connectionState: slot.connection.connectionState,\n dataChannelState: slot.channel?.readyState ?? \"no-channel\",\n pendingSendCount: slot.pendingSends.length,\n pendingRemoteIceCount: slot.pendingRemoteIce.length,\n inFlightSync: slot.inFlightSync ? { ...slot.inFlightSync } : undefined,\n transport: slot.transport\n ? {\n ...slot.transport,\n selectedCandidatePair: slot.transport.selectedCandidatePair\n ? { ...slot.transport.selectedCandidatePair }\n : undefined,\n }\n : undefined,\n lastSyncHandshakeAttempt: { ...slot.lastSyncHandshakeAttempt },\n handles,\n };\n}\n\n/** Initial state for {@link PeerSlot.lastSyncHandshakeAttempt}. All\n * timestamps start `undefined` and become populated as the slot\n * progresses through the lifecycle. Pulled out so every `PeerSlot`\n * construction site uses the same shape. */\nfunction emptySyncHandshakeAttempt(): SyncHandshakeAttemptSnapshot {\n return {\n dataChannelOpenedAt: undefined,\n peerCandidateEmittedAt: undefined,\n firstOutboundSendAt: undefined,\n firstInboundMessageAt: undefined,\n };\n}\n\nfunction buildCandidatePairView(\n pair: Record<string, unknown>,\n localCands: Map<string, Record<string, unknown>>,\n remoteCands: Map<string, Record<string, unknown>>\n): NonNullable<TransportSnapshot[\"selectedCandidatePair\"]> {\n const local = localCands.get(String(pair[\"localCandidateId\"]));\n const remote = remoteCands.get(String(pair[\"remoteCandidateId\"]));\n return {\n localCandidateType: String(local?.[\"candidateType\"] ?? \"?\"),\n remoteCandidateType: String(remote?.[\"candidateType\"] ?? \"?\"),\n state: String(pair[\"state\"] ?? \"\"),\n nominated: Boolean(pair[\"nominated\"]),\n bytesSent: Number(pair[\"bytesSent\"] ?? 0),\n bytesReceived: Number(pair[\"bytesReceived\"] ?? 0),\n };\n}\n\n/** Standard STUN servers for NAT traversal. In production, callers who\n * need TURN fallback for peers behind symmetric NATs should replace this\n * with their own ICE server list. */\nexport const DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: \"stun:stun.l.google.com:19302\" },\n { urls: \"stun:stun1.l.google.com:19302\" },\n];\n\n/** Options for constructing a {@link MeshWebRTCAdapter}. */\nexport interface MeshWebRTCAdapterOptions {\n /** The signalling client the adapter uses to exchange SDP and ICE\n * candidates with other peers. Typically constructed once per\n * application and shared across any adapters that need it. */\n signaling: MeshSignalingClient;\n /** The local peer id. Must match the peer id the signalling client\n * registered with. */\n peerId: string;\n /** Peer ids to connect to on startup. When `connect()` is called, the\n * adapter iterates this list and initiates a WebRTC connection to each\n * one by sending an SDP offer through the signalling channel. Peers\n * not in this list can still connect by sending an offer *to* this\n * adapter. The natural source for this list is the keyring's\n * knownPeers map keys.\n *\n * Used only when {@link keyringSource} is not supplied. With\n * `keyringSource` set the adapter reads `knownPeers` live from the\n * keyring on every initiation decision, which is the shape\n * {@link createMeshClient} wires up so post-construction\n * {@link applyPairingToken} calls take effect without the consumer\n * having to call {@link MeshWebRTCAdapter.addKnownPeer} by hand. */\n knownPeerIds?: string[];\n /** Live keyring source. When supplied, the adapter reads\n * `knownPeers` from `keyringSource()` on every initiation decision\n * instead of a Set captured at construction. Combined with the\n * periodic re-sweep started in {@link MeshWebRTCAdapter.connect},\n * this makes mutations to `keyring.knownPeers` — including the ones\n * produced by {@link applyPairingToken} after the mesh client is up\n * — visible to the dial path within at most one sweep interval. The\n * crypto layer already re-reads the keyring on every send and\n * receive; this option closes the same loop for the WebRTC adapter.\n *\n * Polly issue #103: without this, a long-lived daemon that pairs a\n * new device after its mesh client is constructed never dials the\n * new peer — the adapter's captured Set stays stale even though the\n * keyring contains the new entry, and the lex-tie-break rule in\n * {@link MeshWebRTCAdapter.shouldInitiateTo} can leave both peers\n * waiting for the other to offer indefinitely. */\n keyringSource?: () => MeshKeyring;\n /** How often the adapter re-evaluates whether to dial peers in the\n * signalling roster. The sweep is what catches peers that became\n * authorised in the keyring after their `peer-joined` notification\n * already fired — the adapter has no other event to hang the retry\n * on, so it polls. Cheap: one Map lookup per present peer, fired at\n * most once per interval. Defaults to 2000ms; tests override to\n * shorten the failure budget. Set to 0 to disable the sweep\n * (the captured-set behaviour, kept only for migration). */\n knownPeersRefreshIntervalMs?: number;\n /** Optional ICE server list override. Defaults to {@link DEFAULT_ICE_SERVERS}. */\n iceServers?: RTCIceServer[];\n /** Optional ICE transport policy. Defaults to the\n * {@link RTCPeerConnection} implementation's own default (`\"all\"` in\n * Chrome, Firefox, and werift). Set to `\"relay\"` to force every\n * candidate pair through a TURN relay — the shape the polly#105\n * falsification harness needs to exercise the real-transport contract\n * the polly#104 in-process throttle could not reach. */\n iceTransportPolicy?: RTCIceTransportPolicy;\n /** When `true` (the default), polly enforces\n * {@link iceTransportPolicy} `\"relay\"` on its side of the signalling\n * channel by filtering non-relay ICE candidates out of both the SDP\n * description it forwards and the trickle ICE stream it emits. This\n * closes the gap exposed by polly#105: Chrome's implementation\n * already filters at the source, but werift only filters its own\n * outgoing connectivity checks and still advertises host and srflx\n * candidates upstream — so a relay-only peer on werift will, against\n * a peer with the default policy, still pair through a non-relay\n * candidate (or against a host-derived peer-reflexive remote).\n * Setting this option to `false` reverts the enforcement and is the\n * shape used by the `POLLY_105_DISABLE_TURN_FIX=1` falsification\n * path. Production callers should leave this at the default. */\n iceRelayEnforcement?: boolean;\n /** Optional data channel label. Defaults to \"polly-mesh\". Applications\n * that share a signalling server between multiple meshes may want\n * distinct labels per mesh. */\n dataChannelLabel?: string;\n /** RTCPeerConnection constructor. Defaults to\n * `globalThis.RTCPeerConnection`. Inject a different implementation\n * (e.g. `werift` or `@roamhq/wrtc`) when running outside a browser, or\n * to use a custom subclass for tests or instrumentation. */\n RTCPeerConnection?: typeof RTCPeerConnection;\n /** When `true` (the default), the adapter yields to the event loop\n * between the points where a large initial sync would otherwise hold\n * the main thread for tens of seconds: between each batch of\n * fragmented `RTCDataChannel.send` calls on the sender side, and at\n * the boundary between reassembling a sync message and dispatching\n * it (deserialise → MeshNetworkAdapter unwrap → Automerge\n * `applyChanges`) on the receiver side. Set to `false` to recover\n * the pre-#104 tight-loop behaviour; this is the configuration the\n * `POLLY_104_DISABLE_FIX=1` falsification path in\n * `examples/mesh-large-initial-sync` uses to demonstrate the bug\n * against post-fix polly. Production callers should leave this at\n * the default. */\n syncYieldEnabled?: boolean;\n /** Override the sync fragment chunk size. Defaults to\n * {@link SYNC_FRAGMENT_CHUNK_SIZE} (60 KiB), which leaves header\n * overhead inside werift's hard 64 KiB max-message-size cap.\n * Setting this to 64 KiB recreates the pre-#104 fragmentation bug,\n * where peer A's outbound fragments overshoot the cap and werift\n * rejects them silently — sync stalls forever. Used by the\n * `POLLY_104_DISABLE_FIX=1` falsification path. Production callers\n * should leave this at the default. */\n syncFragmentChunkSizeOverride?: number;\n}\n\n/** Types of signalling payload this adapter exchanges through the\n * signalling channel. The signalling server treats these as opaque. */\ntype SignalingPayload =\n | { kind: \"offer\"; sdp: RTCSessionDescriptionInit }\n | { kind: \"answer\"; sdp: RTCSessionDescriptionInit }\n | { kind: \"ice\"; candidate: RTCIceCandidateInit };\n\n/** Payload of the polly-specific `\"sync-progress\"` event emitted by\n * {@link MeshWebRTCAdapter}. Consumers can subscribe via the adapter's\n * standard `.on()` surface (the same one that carries `peer-candidate`\n * and `peer-disconnected`) to observe fragment receive and dispatch\n * activity in real time, without polling\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot}. Polly issue #104\n * item 7. */\nexport interface SyncProgressEvent {\n /** Remote peer the fragment or dispatch is for. */\n peerId: string;\n /** Lifecycle stage. `fragment-received` fires for each chunk that\n * arrives during reassembly; `dispatch-applied` fires once the\n * reassembled message has been emitted upward to Automerge. */\n kind: \"fragment-received\" | \"dispatch-applied\";\n /** Bytes carried by the chunk that triggered the event. Zero for\n * `dispatch-applied`. */\n bytesDelta: number;\n /** Running total of fragments received for the current reassembly. */\n chunksReceived: number;\n /** Running total of bytes received for the current reassembly. */\n bytesReceived: number;\n /** Number of reassembled messages whose dispatch has been scheduled\n * but not yet emitted upward to Automerge. */\n applyBacklog: number;\n /** `performance.now()` at event emission. */\n at: number;\n}\n\n/** Last-seen transport-level summary for a peer slot. Populated\n * lazily by {@link MeshWebRTCAdapter.refreshTransportStats}, which\n * the consumer calls (typically from a polling loop in a debugging\n * harness) so the cost of {@link RTCPeerConnection.getStats} is\n * incurred only on demand. Polly issue #105 item 7 — exposes the\n * dimension of the transport that {@link InFlightSyncSnapshot}\n * doesn't reach: the negotiated ICE candidate pair, the SCTP\n * retransmission counters, and the last data-channel-level error if\n * one has been observed.\n *\n * Field names mirror the W3C stats spec (`selectedCandidatePairId`,\n * `localCandidateType`, etc.) so consumers can correlate with the\n * raw `RTCStatsReport`. werift exposes a stats shape close to the\n * spec; Chrome exposes the spec itself; this view is implementation-\n * agnostic. */\nexport interface TransportSnapshot {\n /** ICE-level summary of the pair currently carrying data. */\n selectedCandidatePair:\n | {\n localCandidateType: string;\n remoteCandidateType: string;\n state: string;\n nominated: boolean;\n bytesSent: number;\n bytesReceived: number;\n }\n | undefined;\n /** SCTP / data-channel retransmission counters. Some implementations\n * surface these on the transport stat, others on the data-channel\n * stat — we expose the values we found, leaving the field undefined\n * when the underlying impl doesn't surface them. */\n retransmittedPacketsSent: number | undefined;\n retransmittedBytesSent: number | undefined;\n /** Most-recent data-channel error message, if `error` ever fired on\n * the channel for this peer. Cleared only when the slot is replaced. */\n lastDataChannelError: string | undefined;\n /** `performance.now()` at the time the stats were last refreshed. */\n at: number;\n}\n\n/** Reasons the adapter declined to construct an RTC slot for a peer\n * that appeared in the signalling roster or the keyring. Recorded\n * per-peer in {@link MeshWebRTCAdapter.getPeerStateSnapshot} so a\n * consumer harness observing \"(no slot)\" can tell which gate inside\n * the adapter stopped construction without having to log-correlate\n * through three layers of timing. Polly issue #106 item 7.\n *\n * - `self`: the peerId equals the local peerId.\n * - `not-in-keyring`: the live keyring (or captured Set) does not\n * currently authorise this peer.\n * - `not-present`: the peer is not in the signalling roster. The\n * adapter only dials peers it has heard about through\n * `peers-present` or `peer-joined`; keyring entries that have not\n * appeared on signalling are quietly held without a slot.\n * - `tie-break-other-side`: the lex-tie-break designates the remote\n * peer as the initiator; we wait for their offer.\n * - `slot-already-exists`: a slot exists already, possibly in any\n * negotiation state.\n * - `fatal-error`: an exception was thrown while attempting to build\n * the slot. The accompanying {@link SlotInitiationDecision.error}\n * string carries the message.\n */\nexport type SlotInitiationRejectionReason =\n | \"self\"\n | \"not-in-keyring\"\n | \"not-present\"\n | \"tie-break-other-side\"\n | \"slot-already-exists\"\n | \"fatal-error\";\n\n/** Most-recent slot-initiation decision for a peer. Computed at\n * snapshot time so the view always reflects the current state of the\n * relevant gates; a `decision === \"accepted\"` value paired with a\n * `slot === undefined` view on the same snapshot is the load-bearing\n * signal for \"the adapter wants to dial but isn't\" — the failure\n * shape polly#106 documents. */\nexport interface SlotInitiationDecision {\n /** \"accepted\" means every gate in `shouldInitiateTo` would pass right\n * now and a sweep tick would construct a slot. \"rejected\" means at\n * least one gate failed; the `reason` names it. */\n decision: \"accepted\" | \"rejected\";\n /** Populated only on `rejected` decisions. */\n reason: SlotInitiationRejectionReason | undefined;\n /** If the previous sweep tick caught a synchronous throw while\n * building this peer's slot, the message is preserved here for the\n * next snapshot. The reason will be `fatal-error`. */\n error: string | undefined;\n /** `performance.now()` at the time the decision was computed. */\n at: number;\n}\n\n/** Most-recent sync-handshake timeline for a peer slot. Each timestamp\n * is `performance.now()` of the corresponding event the first time it\n * fired on the current slot — slots that get evicted and rebuilt start\n * over. The four fields together describe whether the adapter and the\n * receiver downstream of it have done their part in initiating sync:\n *\n * - `dataChannelOpenedAt`: when the wire is ready to carry bytes.\n * - `peerCandidateEmittedAt`: when polly emitted `peer-candidate`\n * upward; Automerge's network subsystem hooks this event to add the\n * peer to its known set and kick off the per-document sync exchange.\n * If this is `undefined` long after the data channel has opened,\n * polly never signalled \"ready\" to Automerge — that's a failure in\n * the adapter.\n * - `firstOutboundSendAt`: when polly first dispatched bytes through\n * {@link MeshWebRTCAdapter.send} for this peer. If\n * `peerCandidateEmittedAt` is set but this is still `undefined`,\n * Automerge's NetworkSubsystem has not asked the adapter to send\n * anything. The polly#106 ladder named \"no handle locally\" as the\n * typical cause for this rung; the polly#107 failing-shape evidence\n * (fourteen `$meshState` handles pre-warmed in `ready` state,\n * `firstOutboundSendAt` still undefined long after peer-candidate\n * fires) shows that's a misleading ladder entry. Revised:\n *\n * - If `repo.handles[docId]` is undefined or in a non-`ready`\n * state for every doc this peer should sync, the consumer\n * fix is real — pre-warm the handles via the documented\n * `$meshState(key, initial)` factory before the slot opens.\n *\n * - If `repo.handles[docId]` is `ready` for every doc AND\n * `getPeerStateSnapshot().peers[…].slot.handles[docId]\n * .announcedToPeer` is `false`, the gate is BETWEEN Automerge's\n * NetworkSubsystem and the adapter — not on the consumer's\n * handle-construction path. This is the polly#107 surface;\n * post-#107 the mesh client hooks `peer-candidate` to invoke\n * the synchronizer's `reevaluateDocumentShare` so the\n * `addPeer`/`addDocument` ordering race that leaves a handle\n * un-announced gets closed by an idempotent re-evaluation.\n *\n * - If `announcedToPeer` becomes `true` for every relevant doc\n * and `firstOutboundSendAt` is still undefined, the gap is in\n * polly's own send path — that's a polly bug, not an\n * Automerge or consumer one.\n *\n * - `firstInboundMessageAt`: when polly first emitted a `message` event\n * for this peer. If `peerCandidateEmittedAt` is set on the remote and\n * `firstOutboundSendAt` is set on the remote but this is `undefined`\n * locally, bytes were sent across the wire but never decoded — that\n * points at the crypto envelope or the wire fragmenter.\n *\n * Polly issue #106 item 7; polly#107 revised the\n * `firstOutboundSendAt` rung to point at the polly⇄Automerge gate\n * rather than the consumer. */\nexport interface SyncHandshakeAttemptSnapshot {\n dataChannelOpenedAt: number | undefined;\n peerCandidateEmittedAt: number | undefined;\n firstOutboundSendAt: number | undefined;\n firstInboundMessageAt: number | undefined;\n}\n\n/** Sweep-loop observability for the periodic dial re-evaluation. The\n * sweep is what catches peers that were not in the keyring at the time\n * of their `peer-joined` notification (polly#103) AND peers whose\n * previous slot failed and got evicted (polly#106). Exposing its tick\n * counter lets a consumer distinguish \"sweep is running but\n * `shouldInitiateTo` rejected the peer\" from \"sweep is broken and\n * never fires\" without instrumenting polly internals. */\nexport interface SweepSnapshot {\n /** True iff the sweep timer is currently scheduled — false on the\n * captured-set fallback path (no keyringSource) or when the interval\n * is configured to 0. */\n enabled: boolean;\n /** Configured interval in milliseconds. 0 means disabled. */\n intervalMs: number;\n /** How many times the sweep callback has fired since `connect()`. */\n runCount: number;\n /** `performance.now()` at the last tick. `undefined` until the\n * first tick fires. */\n lastRunAt: number | undefined;\n}\n\n/** Per-peer view of an in-flight initial sync. Populated by\n * {@link MeshWebRTCAdapter.handleSyncFragment} as fragments of a\n * single reassembly arrive, and reset to `undefined` once the\n * reassembled message has been dispatched. Exposed verbatim through\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot} so a consumer\n * harness can observe progress mid-stream. Polly issue #104 item 7. */\nexport interface InFlightSyncSnapshot {\n /** Fragments received for the current reassembly. Cleared once\n * reassembly completes. */\n chunksReceived: number;\n /** Bytes received across the fragments of the current reassembly.\n * The reassembled message will be slightly smaller than this sum\n * because each fragment carries a small header. */\n bytesReceived: number;\n /** `performance.now()` value at the last fragment arrival. */\n lastChunkAt: number;\n /** Count of reassembled messages whose dispatch has been\n * scheduled but not yet run. With the receiver-side `setTimeout(0)`\n * yield enabled, this is normally 0 or 1; with the yield\n * disabled (the falsification path) dispatch runs synchronously\n * and this stays 0. */\n applyBacklog: number;\n}\n\n/** Per-handle per-peer sync bookkeeping. Closes the diagnostic gap\n * between \"handle exists in repo\" (observable today via `repo.handles`)\n * and \"Automerge's NetworkSubsystem has actually told this peer about\n * this handle\" — the latter being the load-bearing question for the\n * polly#107 failing shape, where fourteen pre-warmed handles sit in\n * `ready` state and the peer slot is fully connected, yet Automerge\n * has not asked the adapter to send a single sync message.\n *\n * Each field is stamped from the adapter's own wire path: `out` from\n * {@link MeshWebRTCAdapter.send}, `in` from {@link dispatchMessage}'s\n * deserialised view. Combined with the consumer's view of\n * `repo.handles[documentId].state`, the mesh client's\n * {@link createMeshClient}-returned `getPeerStateSnapshot` produces the\n * full per-handle observability the polly#107 ticket asks for in\n * item 7. */\nexport interface HandleSyncSnapshot {\n /** `performance.now()` of the most recent send through\n * {@link MeshWebRTCAdapter.send} carrying this `documentId` for this\n * peer. `undefined` means Automerge has never asked the adapter to\n * send a sync message for this document — i.e. the handle is NOT\n * \"announced to peer\" in the polly#107 sense, regardless of whether\n * the handle is `ready` in `repo.handles`. */\n lastSyncMessageOutAt: number | undefined;\n /** `performance.now()` of the most recent inbound message dispatched\n * upward for this `documentId` from this peer. `undefined` means we\n * have never received a sync message for this document from this\n * peer — they have not announced their copy of this handle to us. */\n lastSyncMessageInAt: number | undefined;\n /** Byte length of the most recent outbound sync message (the raw\n * `RTCDataChannel.send` payload, which is the crypto-wrapped envelope\n * — not the inner Automerge sync size). `undefined` until the first\n * outbound send. Used by the polly#107 example's wire-level\n * transcript to distinguish \"Automerge generated an empty sync\n * message\" from \"Automerge generated nothing at all\". */\n lastSyncMessageOutSize: number | undefined;\n /** Type field of the most recent outbound message for this document\n * to this peer. Typically `\"sync\"` once handshake has begun, or\n * `\"request\"` while the local side is still asking. `undefined`\n * until the first outbound send. */\n lastSyncMessageOutType: string | undefined;\n}\n\n/** Initial state for a {@link HandleSyncSnapshot}. */\nfunction emptyHandleSyncSnapshot(): HandleSyncSnapshot {\n return {\n lastSyncMessageOutAt: undefined,\n lastSyncMessageInAt: undefined,\n lastSyncMessageOutSize: undefined,\n lastSyncMessageOutType: undefined,\n };\n}\n\n/** State tracked for each remote peer. */\ninterface PeerSlot {\n connection: RTCPeerConnection;\n channel: RTCDataChannel | undefined;\n /** Outgoing messages queued while the channel is still connecting.\n * Typed as ArrayBuffer-backed so they are directly usable by\n * RTCDataChannel.send under TypeScript's strict buffer-source typing. */\n pendingSends: Uint8Array<ArrayBuffer>[];\n /** Partially-assembled inbound sync messages, keyed by the fragment id\n * stamped on each chunk. Entries are deleted as soon as the last\n * fragment for an id arrives and the reassembled bytes are dispatched. */\n pendingFragments: Map<string, { chunks: Map<number, Uint8Array>; total: number }>;\n /** Remote ICE candidates that arrived before `setRemoteDescription`\n * completed. addIceCandidate throws when `remoteDescription` is null,\n * and real Chrome 148+ does not internally queue these the way Chrome\n * for Testing does — so any candidate that wins the race against the\n * answer SDP would be silently dropped, leaving ICE checking with no\n * remote candidates to pair against. Drained from {@link handleAnswer}\n * and the post-`setRemoteDescription` step of {@link handleOffer}. */\n pendingRemoteIce: RTCIceCandidateInit[];\n /** Observable snapshot of any reassembly currently in progress for\n * this peer. `undefined` whenever the receive side is idle. */\n inFlightSync: InFlightSyncSnapshot | undefined;\n /** Last refreshed transport-level summary. `undefined` until the\n * caller invokes {@link MeshWebRTCAdapter.refreshTransportStats}\n * (or its idle equivalent on the mesh client). */\n transport: TransportSnapshot | undefined;\n /** Sticky cache of the most recent error reported by the data\n * channel's `onerror` event. Captured on the channel itself so the\n * value survives transient slot replacements within a single\n * connection's lifetime. */\n lastDataChannelError: string | undefined;\n /** Per-slot sync-handshake observability. Each field is populated\n * the first time its event fires on the current slot. Polly issue\n * #106 item 7. */\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n /** Per-handle sync bookkeeping for this peer. Keyed by\n * `documentId`. Entries are created on demand — the first time\n * {@link MeshWebRTCAdapter.send} dispatches a message carrying a\n * given documentId to this peer, or the first time a message for\n * that documentId is dispatched inbound. Polly issue #107 item 7 —\n * closes the \"handle exists in repo but Automerge never announced\n * it\" diagnostic gap. */\n handles: Map<string, HandleSyncSnapshot>;\n}\n\n/**\n * Automerge-Repo NetworkAdapter backed by real WebRTC data channels.\n * Manages one RTCPeerConnection per remote peer and uses a supplied\n * {@link MeshSignalingClient} for SDP/ICE exchange.\n */\nexport class MeshWebRTCAdapter extends NetworkAdapter {\n readonly signaling: MeshSignalingClient;\n readonly iceServers: RTCIceServer[];\n readonly iceTransportPolicy: RTCIceTransportPolicy | undefined;\n private readonly iceRelayEnforcement: boolean;\n readonly dataChannelLabel: string;\n /** Peers this adapter is willing to dial. Mutable so callers that pair\n * a new device after construction (e.g. a CLI `add-device` process whose\n * mesh client is long-lived across the pair ceremony) can register the\n * new peer with {@link addKnownPeer} without restarting the client.\n *\n * Used only in the captured-set fallback path — when no\n * {@link keyringSource} is supplied. With `keyringSource` set, the\n * authoritative source is the live keyring and this Set is unused. */\n private readonly knownPeers: Set<string>;\n /** Live keyring source, or undefined when the adapter is operating in\n * the captured-set fallback shape. See the JSDoc on\n * {@link MeshWebRTCAdapterOptions.keyringSource}. */\n private readonly keyringSource: (() => MeshKeyring) | undefined;\n /** Interval handle for the periodic re-sweep that catches peers\n * already in the signalling roster when the keyring grew. Cleared in\n * {@link MeshWebRTCAdapter.disconnect}. */\n private knownPeersRefreshTimer: ReturnType<typeof setInterval> | undefined;\n /** Sweep interval. Resolved from\n * {@link MeshWebRTCAdapterOptions.knownPeersRefreshIntervalMs} at\n * construction. Defaults to 2000ms; tests override to 100–250ms. */\n private readonly knownPeersRefreshIntervalMs: number;\n /** When `true`, the sender side awaits between batches of fragment\n * sends and the receiver side schedules dispatch via `setTimeout(0)`\n * so the JS event loop can drain timers (including the consumer's\n * own setInterval-based liveness probes) between large-message\n * apply calls. Defaults to `true`; set to `false` only by the\n * `POLLY_104_DISABLE_FIX` falsification path. */\n private readonly syncYieldEnabled: boolean;\n /** Resolved chunk size for fragmenting oversized messages.\n * Defaults to {@link SYNC_FRAGMENT_CHUNK_SIZE}; can be overridden\n * via {@link MeshWebRTCAdapterOptions.syncFragmentChunkSizeOverride}. */\n private readonly syncFragmentChunkSize: number;\n /** Peers currently visible in the signalling roster — populated by\n * {@link handlePeersPresent} / {@link handlePeerJoined} and pruned by\n * {@link handlePeerLeft}. Read by {@link addKnownPeer} to decide\n * whether the new peer is already online and an offer can fire now. */\n private readonly presentPeers = new Set<string>();\n /** Local peer id captured at construction time. The base\n * `NetworkAdapter.peerId` is only populated when `connect()` fires,\n * which means glare-resolution and peer-discovery dispatch would\n * otherwise have no id to compare against before the first incoming\n * message. Keeping a private mirror keeps those code paths honest\n * without depending on Automerge's lifecycle. */\n private readonly localPeerId: string;\n private readonly RTCPeerConnectionCtor: typeof RTCPeerConnection;\n private readonly slots = new Map<string, PeerSlot>();\n private ready = false;\n private readyResolver: (() => void) | undefined;\n /** Sticky cache of the most recent {@link SlotInitiationDecision}\n * per peer. Updated on every `shouldInitiateTo` call and on every\n * caught throw inside the sweep loop, so a snapshot taken at any\n * moment can answer \"why is there no slot for this peer right\n * now?\". Polly issue #106 item 7. */\n private readonly lastSlotInitiationDecisions = new Map<string, SlotInitiationDecision>();\n /** Tick count of the periodic sweep — incremented inside the\n * `setInterval` callback, exposed via {@link getPeerStateSnapshot}.\n * Lets a consumer rule out \"sweep is dead\" before chasing the\n * shouldInitiateTo gates. */\n private sweepRunCount = 0;\n /** `performance.now()` at the last sweep tick. Paired with\n * {@link sweepRunCount} so a stalled sweep is visible at a glance\n * via the snapshot's `sweep` block. */\n private lastSweepAt: number | undefined;\n\n /** The peers this adapter will dial. Backward-compatible read accessor\n * for callers that previously iterated the `knownPeerIds` array. With\n * a {@link MeshWebRTCAdapterOptions.keyringSource} configured, the\n * value is read live from the keyring; otherwise it falls back to the\n * captured Set populated through the constructor and\n * {@link addKnownPeer}. */\n get knownPeerIds(): string[] {\n if (this.keyringSource !== undefined) {\n return [...this.keyringSource().knownPeers.keys()].filter((id) => id !== this.localPeerId);\n }\n return [...this.knownPeers];\n }\n\n /** True iff `remotePeerId` is currently in the authoritative\n * knownPeers source — the live keyring when one was supplied, or\n * the captured Set otherwise. Centralises the membership check so\n * {@link shouldInitiateTo} and the JSDoc on\n * {@link MeshWebRTCAdapterOptions.keyringSource} agree on the rule. */\n private hasKnownPeer(remotePeerId: string): boolean {\n if (this.keyringSource !== undefined) {\n return this.keyringSource().knownPeers.has(remotePeerId);\n }\n return this.knownPeers.has(remotePeerId);\n }\n\n /** Callback for incoming blob messages. Set by the blob store.\n * Called with the sender's peer ID, the raw header object, and the\n * binary payload (chunk data). */\n onBlobMessage?: (peerId: string, header: Record<string, unknown>, data: Uint8Array) => void;\n\n constructor(options: MeshWebRTCAdapterOptions) {\n super();\n this.signaling = options.signaling;\n this.iceServers = options.iceServers ?? DEFAULT_ICE_SERVERS;\n this.iceTransportPolicy = options.iceTransportPolicy;\n this.iceRelayEnforcement = options.iceRelayEnforcement ?? true;\n this.dataChannelLabel = options.dataChannelLabel ?? \"polly-mesh\";\n this.knownPeers = new Set(options.knownPeerIds ?? []);\n this.keyringSource = options.keyringSource;\n this.knownPeersRefreshIntervalMs = options.knownPeersRefreshIntervalMs ?? 2000;\n this.syncYieldEnabled = options.syncYieldEnabled ?? true;\n this.syncFragmentChunkSize = options.syncFragmentChunkSizeOverride ?? SYNC_FRAGMENT_CHUNK_SIZE;\n this.localPeerId = options.peerId;\n const PC = options.RTCPeerConnection ?? globalThis.RTCPeerConnection;\n if (typeof PC !== \"function\") {\n throw new Error(\n \"MeshWebRTCAdapter: no RTCPeerConnection implementation found. Pass one via options.RTCPeerConnection (e.g. from `werift` or `@roamhq/wrtc`), or run in a browser where `globalThis.RTCPeerConnection` exists.\"\n );\n }\n this.RTCPeerConnectionCtor = PC;\n }\n\n isReady(): boolean {\n return this.ready;\n }\n\n /** The current number of peer slots the adapter is tracking. Each\n * slot is one ordered pair (local peer ↔ remote peer) with its own\n * RTCPeerConnection and data channel. Exposed for tests that assert\n * \"exactly one channel per pair\" after discovery settles. */\n peerSlotCount(): number {\n return this.slots.size;\n }\n\n /** Snapshot of the adapter's per-peer state at the moment of the\n * call. Returned values are plain data — strings and booleans only —\n * so consumers can log, assert on, or render them without retaining\n * references into the adapter's internals.\n *\n * Polly issue #103 asked for an inspection surface so a consumer\n * harness can answer \"is the mesh layer in a known good state\" from\n * outside polly. This method is that surface. The fields mirror the\n * three layers of the WebRTC connection lifecycle so a \"stuck\"\n * connection can be diagnosed without reaching for the browser\n * devtools: the SDP signalling state, the ICE checking state, and\n * the unified RTCPeerConnection connection state. A `dataChannel`\n * field reports whether the data channel — the thing the mesh\n * actually carries bytes over — is open.\n *\n * Peers visible in the signalling roster but not yet dialled appear\n * with `slot: undefined`, so a consumer can tell \"we know about this\n * peer in signalling but the WebRTC adapter has not started an\n * offer yet\" from \"we have a slot in some negotiation state\". */\n getPeerStateSnapshot(): {\n localPeerId: string;\n knownPeerIds: string[];\n presentPeerIds: string[];\n sweep: SweepSnapshot;\n peers: Array<{\n peerId: string;\n knownInKeyring: boolean;\n presentInSignalling: boolean;\n slotInitiationRejectionReason: SlotInitiationRejectionReason | undefined;\n slotInitiationDecision: SlotInitiationDecision;\n slot:\n | undefined\n | {\n signalingState: string;\n iceConnectionState: string;\n connectionState: string;\n dataChannelState: string;\n pendingSendCount: number;\n pendingRemoteIceCount: number;\n inFlightSync: InFlightSyncSnapshot | undefined;\n transport: TransportSnapshot | undefined;\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n handles: Record<string, HandleSyncSnapshot>;\n };\n }>;\n } {\n const knownPeerIds = this.knownPeerIds;\n const presentPeerIds = [...this.presentPeers];\n const knownPeerSet = new Set(knownPeerIds);\n const allPeers = new Set<string>();\n for (const id of knownPeerIds) allPeers.add(id);\n for (const id of presentPeerIds) allPeers.add(id);\n for (const id of this.slots.keys()) allPeers.add(id);\n const peers: ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"peers\"] = [];\n for (const peerId of allPeers) {\n const slot = this.slots.get(peerId);\n const decision = this.snapshotInitiationDecision(peerId);\n peers.push({\n peerId,\n knownInKeyring: knownPeerSet.has(peerId),\n presentInSignalling: this.presentPeers.has(peerId),\n // Top-level convenience: same value as\n // `slotInitiationDecision.reason`, hoisted so log lines that\n // only need the named reason can read it without a nested\n // access. Polly issue #106 item 7.\n slotInitiationRejectionReason: decision.reason,\n slotInitiationDecision: decision,\n slot: slot ? serialiseSlotView(slot) : undefined,\n });\n }\n return {\n localPeerId: this.localPeerId,\n knownPeerIds,\n presentPeerIds,\n sweep: {\n enabled: this.knownPeersRefreshTimer !== undefined,\n intervalMs: this.knownPeersRefreshIntervalMs,\n runCount: this.sweepRunCount,\n lastRunAt: this.lastSweepAt,\n },\n peers,\n };\n }\n\n /** Handle the signalling server's `peer-joined` notification: a new\n * peer has appeared on the relay. If the peer is in `knownPeerIds`\n * and we do not already have a slot for it, and the tie-break rule\n * designates us as the initiator (our peerId compares greater than\n * theirs), open an initiating slot and fire the SDP offer. Otherwise\n * do nothing — either we are not interested in this peer, we are\n * already connected, or the other side is the one expected to\n * initiate. */\n handlePeerJoined(remotePeerId: string): void {\n this.presentPeers.add(remotePeerId);\n if (!this.shouldInitiateTo(remotePeerId)) return;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n\n /** Handle the signalling server's `peers-present` notification sent\n * once to a newcomer. Applies the same filter as handlePeerJoined to\n * every listed peer, so a device joining into an established lobby\n * dials every knownPeer it is meant to initiate to in one pass. */\n handlePeersPresent(peerIds: string[]): void {\n for (const remotePeerId of peerIds) {\n this.presentPeers.add(remotePeerId);\n if (!this.shouldInitiateTo(remotePeerId)) continue;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n }\n\n /** Construct an initiating slot inside a per-peer try/catch and\n * record any throw as a `fatal-error` rejection on the per-peer\n * decision map so the snapshot surface names it. Every dial entry\n * point ({@link handlePeerJoined}, {@link handlePeersPresent},\n * {@link addKnownPeer}, {@link refreshKnownPeers}) routes through\n * here so a single peer's broken construction can never take down a\n * batch of peers — pre-#106 a thrown `new RTCPeerConnection()`\n * inside `handlePeersPresent` would skip every later peer in the\n * same batch with no observable trace because the signalling\n * client's frame dispatch swallowed the rejection. */\n private tryCreateInitiatingSlot(remotePeerId: string): void {\n try {\n this.createInitiatingSlot(remotePeerId);\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.lastSlotInitiationDecisions.set(remotePeerId, {\n decision: \"rejected\",\n reason: \"fatal-error\",\n error: message,\n at: performance.now(),\n });\n }\n }\n\n /** Handle the signalling server's `peer-left` notification: a\n * previously joined peer has closed its socket. Evict any slot we\n * hold for that peer so a subsequent `peer-joined` from the same\n * peerId (a reconnect) creates a fresh slot rather than colliding\n * with a stale RTCPeerConnection that WebRTC's own ICE timer has\n * not yet noticed is dead. */\n handlePeerLeft(remotePeerId: string): void {\n this.presentPeers.delete(remotePeerId);\n const slot = this.slots.get(remotePeerId);\n if (!slot) return;\n slot.channel?.close();\n slot.connection.close();\n this.slots.delete(remotePeerId);\n }\n\n /** Register a peer the adapter should dial. Used by post-construction\n * pair-acceptance flows — when a long-lived mesh client (e.g. a CLI\n * `add-device` process or a daemon holding the mesh open) accepts a\n * pair token after start-up, its keyring's `knownPeers` map gains a\n * new entry. Calling this method propagates that into the adapter's\n * own allowlist; if the peer is already in the signalling roster and\n * the tie-break rule names us as the initiator, an SDP offer fires\n * immediately. No-op if the peer is already known.\n *\n * When a {@link MeshWebRTCAdapterOptions.keyringSource} is configured\n * the adapter already reads `knownPeers` live and the periodic sweep\n * picks up the new entry within\n * {@link MeshWebRTCAdapterOptions.knownPeersRefreshIntervalMs} on its\n * own, so explicit calls to this method are not required for\n * correctness — but remain supported for callers that want the\n * \"dial now if present\" prompt without waiting for the next sweep. */\n addKnownPeer(remotePeerId: string): void {\n if (remotePeerId === this.localPeerId) return;\n if (this.keyringSource === undefined) {\n // Captured-set fallback path: keep the legacy mutation behaviour\n // so existing consumers that wire the adapter directly without a\n // keyringSource still work.\n if (this.knownPeers.has(remotePeerId)) return;\n this.knownPeers.add(remotePeerId);\n }\n if (!this.presentPeers.has(remotePeerId)) return;\n if (!this.shouldInitiateTo(remotePeerId)) return;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n\n /** Re-evaluate every peer currently in the signalling roster and\n * dial the ones the keyring authorises that we do not already have\n * a slot for. The periodic sweep started in {@link connect} calls\n * this; consumers can call it manually to skip the wait after they\n * apply a fresh pairing token. Idempotent.\n *\n * A throw from {@link createInitiatingSlot} for one peer must not\n * prevent the sweep from continuing to the next one — pre-#106 a\n * synchronous throw inside `new RTCPeerConnection()` (a real risk\n * once the page has built dozens of connections and Chrome's\n * per-page cap is in play) skipped every later peer in the same\n * sweep, with no observable trace because `setInterval` swallows\n * the rejection silently. {@link tryCreateInitiatingSlot} caches\n * the error onto the snapshot's slotInitiationRejectionReason as\n * `fatal-error` so the failing peer is named instead of disguised\n * as \"(no slot)\". */\n refreshKnownPeers(): void {\n for (const remotePeerId of this.presentPeers) {\n if (!this.shouldInitiateTo(remotePeerId)) continue;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n }\n\n private shouldInitiateTo(remotePeerId: string): boolean {\n const reason = this.evaluateInitiation(remotePeerId);\n this.lastSlotInitiationDecisions.set(remotePeerId, {\n decision: reason === undefined ? \"accepted\" : \"rejected\",\n reason,\n error: undefined,\n at: performance.now(),\n });\n return reason === undefined;\n }\n\n /** Pure-function form of the gate cascade behind {@link shouldInitiateTo}.\n * Returns `undefined` when every gate passes (the slot would be\n * built); otherwise returns the named reason the dial was declined.\n * Pulling the gates out of the boolean wrapper lets the snapshot\n * surface name *which* gate stopped construction without the caller\n * having to re-implement the check. Polly issue #106 item 7.\n *\n * The \"not-present\" gate is checked here even though the inbound\n * call sites (`handlePeerJoined`, `handlePeersPresent`,\n * `refreshKnownPeers`, `addKnownPeer`) only invoke `shouldInitiateTo`\n * for peers in the signalling roster — so on those paths the gate\n * never fires. It's load-bearing on the snapshot path, where the\n * reason is computed for every peer the caller might ask about\n * (including keyring entries that aren't currently signalling). */\n private evaluateInitiation(remotePeerId: string): SlotInitiationRejectionReason | undefined {\n if (remotePeerId === this.localPeerId) return \"self\";\n if (!this.hasKnownPeer(remotePeerId)) return \"not-in-keyring\";\n if (!this.presentPeers.has(remotePeerId)) return \"not-present\";\n if (this.slots.has(remotePeerId)) return \"slot-already-exists\";\n // Tie-break: the lexicographically higher peer id initiates. This\n // matches the glare-resolution rule in handleOffer, so pre-offer\n // filtering eliminates the glare pathway for the common case where\n // both sides learn of each other at roughly the same moment.\n if (this.localPeerId <= remotePeerId) return \"tie-break-other-side\";\n return undefined;\n }\n\n /** Compute the latest initiation decision for a peer at snapshot\n * time. Prefers the cached decision when a sweep tick fixed the\n * outcome to `fatal-error` (a thrown construction is sticky until\n * the next successful sweep clears it); otherwise re-evaluates the\n * gates against current state. Pure read; never mutates the map. */\n private snapshotInitiationDecision(remotePeerId: string): SlotInitiationDecision {\n const cached = this.lastSlotInitiationDecisions.get(remotePeerId);\n if (cached?.reason === \"fatal-error\") return cached;\n const reason = this.evaluateInitiation(remotePeerId);\n return {\n decision: reason === undefined ? \"accepted\" : \"rejected\",\n reason,\n error: undefined,\n at: performance.now(),\n };\n }\n\n whenReady(): Promise<void> {\n if (this.ready) return Promise.resolve();\n return new Promise((resolve) => {\n this.readyResolver = resolve;\n });\n }\n\n /**\n * Start the adapter. Marks the adapter ready so Automerge's\n * NetworkSubsystem begins routing messages through it. Discovery of\n * peers is driven entirely by the signalling server's\n * `peers-present` and `peer-joined` frames, handed to\n * {@link handlePeersPresent} and {@link handlePeerJoined}. A peer\n * that calls `signaling.connect()` at any point — before or after\n * this method — will either find its targets already in the server's\n * lobby (peers-present) or learn about them as they arrive\n * (peer-joined); either way the adapter only opens one\n * initiating slot per ordered pair.\n */\n connect(peerId: PeerId, peerMetadata?: PeerMetadata): void {\n this.peerId = peerId;\n if (peerMetadata !== undefined) {\n this.peerMetadata = peerMetadata;\n }\n this.ready = true;\n this.readyResolver?.();\n this.startKnownPeersSweep();\n }\n\n disconnect(): void {\n this.stopKnownPeersSweep();\n for (const slot of this.slots.values()) {\n slot.channel?.close();\n slot.connection.close();\n }\n this.slots.clear();\n this.signaling.close();\n this.ready = false;\n this.emit(\"close\");\n }\n\n /** Start the periodic re-sweep that catches peers added to the\n * keyring after their `peer-joined` notification has already fired.\n * No-op when no keyringSource was supplied — the captured-set\n * fallback has no live source to re-read, so the sweep would be\n * useless. No-op when the interval is configured to 0.\n *\n * Each tick increments {@link sweepRunCount} and stamps\n * {@link lastSweepAt} *before* dispatching to\n * {@link refreshKnownPeers} so a snapshot can distinguish \"sweep is\n * running but every peer is rejected\" from \"sweep is dead\". An\n * outer try/catch keeps the timer alive even if the per-peer\n * try/catch inside `refreshKnownPeers` somehow leaks. Polly issue\n * #106 item 7. */\n private startKnownPeersSweep(): void {\n if (this.keyringSource === undefined) return;\n if (this.knownPeersRefreshIntervalMs <= 0) return;\n if (this.knownPeersRefreshTimer !== undefined) return;\n this.knownPeersRefreshTimer = setInterval(() => {\n this.sweepRunCount += 1;\n this.lastSweepAt = performance.now();\n try {\n this.refreshKnownPeers();\n } catch {\n // The per-peer try/catch inside refreshKnownPeers should\n // already have caught construction errors. This outer catch\n // is a last-resort belt that keeps the interval alive even\n // if a future change adds a non-per-peer throw site.\n }\n }, this.knownPeersRefreshIntervalMs);\n }\n\n private stopKnownPeersSweep(): void {\n if (this.knownPeersRefreshTimer === undefined) return;\n clearInterval(this.knownPeersRefreshTimer);\n this.knownPeersRefreshTimer = undefined;\n }\n\n /**\n * Send a sync message to a specific remote peer. If no RTCPeerConnection\n * exists yet, the adapter initiates one by producing an SDP offer and\n * sending it through the signalling channel; the outgoing bytes are\n * queued until the data channel is open.\n */\n send(message: Message): void {\n const targetId = message.targetId as unknown as string;\n const bytes = this.serialiseMessage(message);\n let slot = this.slots.get(targetId);\n if (!slot) {\n slot = this.createInitiatingSlot(targetId);\n }\n if (slot.lastSyncHandshakeAttempt.firstOutboundSendAt === undefined) {\n slot.lastSyncHandshakeAttempt.firstOutboundSendAt = performance.now();\n }\n // Per-handle wire-level transcript (polly#107 item 5). Automerge\n // stamps `documentId` on every sync/request/remote-heads/etc.\n // message; messages without a documentId (the synchronizer never\n // emits one without it, but the type union allows for legacy\n // ephemeral envelopes) are skipped here rather than blanketing\n // every peer's handle map with a noise key. `wrap` in\n // {@link MeshNetworkAdapter} now preserves `documentId` in the\n // outer envelope so this stamp survives the crypto layer.\n const documentId = (message as unknown as { documentId?: string }).documentId;\n if (typeof documentId === \"string\") {\n let handleEntry = slot.handles.get(documentId);\n if (!handleEntry) {\n handleEntry = emptyHandleSyncSnapshot();\n slot.handles.set(documentId, handleEntry);\n }\n handleEntry.lastSyncMessageOutAt = performance.now();\n handleEntry.lastSyncMessageOutSize = bytes.length;\n handleEntry.lastSyncMessageOutType =\n typeof message.type === \"string\" ? message.type : undefined;\n }\n if (slot.channel && slot.channel.readyState === \"open\") {\n void this.sendBytesMaybeFragmented(slot.channel, bytes);\n } else {\n slot.pendingSends.push(bytes);\n }\n }\n\n /** Number of consecutive fragment sends after which the sender\n * yields to the macrotask queue when {@link syncYieldEnabled} is on.\n * 8 × 64 KiB = 512 KiB of bytes between yields — small enough that\n * a 5 MB sync produces many yield points (and the JS event loop\n * drains the consumer's `setInterval` liveness probes between\n * them), large enough that the per-yield overhead does not dominate\n * the wire cost. */\n private static readonly SEND_YIELD_EVERY_N_FRAGMENTS = 8;\n\n /** Send raw wire bytes, fragmenting if they exceed the SCTP maxMessageSize\n * cap. The default RTCDataChannel limit is 256 KiB in current Chrome and\n * werift; oversized sends either throw, drop silently, or stall the\n * channel, none of which surface as an error to the caller. Fragments\n * use the same length-prefixed JSON header wire format as ordinary\n * messages but carry a `sync-fragment` type that the receive path\n * detects and reassembles before deserialising.\n *\n * When {@link MeshWebRTCAdapterOptions.syncYieldEnabled} is true (the\n * default), the loop awaits the macrotask queue every\n * {@link SEND_YIELD_EVERY_N_FRAGMENTS} fragments so the JS event\n * loop drains between batches — without this, a 5–8 MB initial\n * sync produces 78–125 back-to-back `RTCDataChannel.send` calls in\n * a tight loop, starving anything else on the main thread (polly\n * issue #104, sender side). When the option is false the legacy\n * tight-loop shape is preserved for the falsification path. */\n private async sendBytesMaybeFragmented(\n channel: RTCDataChannel,\n bytes: Uint8Array<ArrayBuffer>\n ): Promise<void> {\n if (bytes.length <= SYNC_FRAGMENT_THRESHOLD) {\n channel.send(bytes);\n return;\n }\n const id = crypto.randomUUID();\n const fragments = chunkSyncMessage(bytes, id, this.syncFragmentChunkSize);\n if (!this.syncYieldEnabled) {\n for (const fragment of fragments) {\n channel.send(fragment);\n }\n return;\n }\n let i = 0;\n for (const fragment of fragments) {\n channel.send(fragment);\n i += 1;\n if (i % MeshWebRTCAdapter.SEND_YIELD_EVERY_N_FRAGMENTS === 0 && i < fragments.length) {\n await new Promise<void>((resolve) => {\n setTimeout(resolve, 0);\n });\n }\n }\n }\n\n /**\n * Entry point the signalling client calls when it receives a signal\n * from a remote peer. Dispatches on the payload `kind` to either\n * accept an offer (building an answer), apply an answer, or add an\n * ICE candidate. Exposed publicly so the caller that constructs the\n * signalling client can wire the onSignal callback directly to this\n * method.\n */\n handleSignal(fromPeerId: string, rawPayload: unknown): void {\n const payload = rawPayload as unknown as SignalingPayload;\n if (!payload || typeof payload !== \"object\" || !(\"kind\" in payload)) {\n return;\n }\n switch (payload.kind) {\n case \"offer\":\n void this.handleOffer(fromPeerId, payload.sdp);\n return;\n case \"answer\":\n void this.handleAnswer(fromPeerId, payload.sdp);\n return;\n case \"ice\":\n void this.handleIceCandidate(fromPeerId, payload.candidate);\n return;\n }\n }\n\n /** Assemble the {@link RTCConfiguration} every new\n * {@link RTCPeerConnection} is built with. Centralised so every slot\n * (initiator and answerer) honours the same iceTransportPolicy. */\n private buildRtcConfiguration(): RTCConfiguration {\n const config: RTCConfiguration = { iceServers: this.iceServers };\n if (this.iceTransportPolicy !== undefined) {\n config.iceTransportPolicy = this.iceTransportPolicy;\n }\n return config;\n }\n\n /** Decide whether a local ICE candidate should be relayed through\n * the signalling channel to the remote peer. Chrome's iceTransport\n * Policy = \"relay\" implementation filters non-relay candidates at\n * the source so the remote peer never sees them; werift's only\n * filters its own outgoing connectivity checks and still emits host\n * and srflx candidates upstream, so a remote peer with policy \"all\"\n * can pair against them — making relay-only enforcement leaky in\n * the mixed-implementation case the polly#105 falsification harness\n * exposes. Mirroring Chrome's filter at this layer gives a uniform\n * contract on every RTCPeerConnection implementation polly supports.\n *\n * The candidate `type` field is the SDP-spec form; we additionally\n * parse it out of the legacy `candidate` string for implementations\n * that don't surface the typed field directly (werift exposes the\n * SDP string only). */\n private isRelayCandidateInit(init: RTCIceCandidateInit): boolean {\n const candidateStr = init.candidate ?? \"\";\n const m = candidateStr.match(/\\btyp\\s+(\\S+)/i);\n return m?.[1]?.toLowerCase() === \"relay\";\n }\n\n private shouldSendCandidate(candidate: RTCIceCandidate): boolean {\n if (!this.iceRelayEnforcement) return true;\n if (this.iceTransportPolicy !== \"relay\") return true;\n const typed = (candidate as unknown as { type?: string }).type;\n const match = candidate.candidate.match(/\\btyp\\s+(\\S+)/i);\n const candidateType = (typed ?? match?.[1] ?? \"\").toLowerCase();\n return candidateType === \"relay\";\n }\n\n /** Strip non-relay `a=candidate:` lines from an SDP when\n * iceTransportPolicy is `\"relay\"`. Some RTCPeerConnection\n * implementations (werift, notably) embed every gathered candidate\n * in the SDP regardless of policy, and a remote peer parsing those\n * via `setRemoteDescription` will pair against them — bypassing the\n * relay-only contract. Chrome already filters in-SDP candidates by\n * policy, so this is only load-bearing for werift consumers, but\n * the SDP rewrite is implementation-agnostic and idempotent. */\n private filterSdpCandidatesByPolicy(sdp: string | undefined): string | undefined {\n if (!this.iceRelayEnforcement) return sdp;\n if (this.iceTransportPolicy !== \"relay\") return sdp;\n if (!sdp) return sdp;\n const lines = sdp.split(/\\r?\\n/);\n const filtered: string[] = [];\n for (const line of lines) {\n if (!line.startsWith(\"a=candidate:\")) {\n filtered.push(line);\n continue;\n }\n const m = line.match(/\\btyp\\s+(\\S+)/i);\n if (m?.[1]?.toLowerCase() === \"relay\") filtered.push(line);\n }\n return filtered.join(\"\\r\\n\");\n }\n\n /** Apply {@link filterSdpCandidatesByPolicy} to an SDP\n * description ahead of `setLocalDescription` (filter outgoing) or\n * `setRemoteDescription` (filter incoming). The defensive\n * receive-side filter exists because we cannot trust the remote\n * peer's adapter to have stripped its non-relay candidates first —\n * polly might be talking to a pre-#105 polly, or to a non-polly\n * peer whose policy enforcement is different. */\n private applySdpPolicyFilter(description: RTCSessionDescriptionInit): RTCSessionDescriptionInit {\n if (!this.iceRelayEnforcement) return description;\n if (this.iceTransportPolicy !== \"relay\") return description;\n const filtered = this.filterSdpCandidatesByPolicy(description.sdp);\n if (filtered === description.sdp) return description;\n return { ...description, sdp: filtered };\n }\n\n private createInitiatingSlot(targetId: string): PeerSlot {\n const connection = new this.RTCPeerConnectionCtor(this.buildRtcConfiguration());\n const channel = connection.createDataChannel(this.dataChannelLabel, { ordered: true });\n const slot: PeerSlot = {\n connection,\n channel,\n pendingSends: [],\n pendingFragments: new Map(),\n pendingRemoteIce: [],\n inFlightSync: undefined,\n transport: undefined,\n lastDataChannelError: undefined,\n lastSyncHandshakeAttempt: emptySyncHandshakeAttempt(),\n handles: new Map(),\n };\n this.slots.set(targetId, slot);\n this.wireConnection(targetId, connection);\n this.wireDataChannel(targetId, channel);\n void this.initiateOffer(targetId, connection);\n return slot;\n }\n\n private async initiateOffer(targetId: string, connection: RTCPeerConnection): Promise<void> {\n const offer = await connection.createOffer();\n await connection.setLocalDescription(offer);\n const localOffer = connection.localDescription ?? offer;\n const sdpToSend = this.applySdpPolicyFilter({\n type: localOffer.type,\n sdp: localOffer.sdp ?? offer.sdp,\n });\n this.signaling.sendSignal(targetId, {\n kind: \"offer\",\n sdp: sdpToSend,\n } satisfies SignalingPayload);\n }\n\n private async handleOffer(fromPeerId: string, sdp: RTCSessionDescriptionInit): Promise<void> {\n const existing = this.slots.get(fromPeerId);\n if (existing) {\n // Glare: we already initiated a connection to this peer. Resolve by\n // peer-id ordering: the lexicographically lower id yields its own\n // offer and accepts the incoming one. The higher id ignores the\n // incoming offer and waits for the answer to its own.\n const localId = this.peerId as unknown as string;\n if (localId > fromPeerId) {\n return;\n }\n existing.channel?.close();\n existing.connection.close();\n this.slots.delete(fromPeerId);\n }\n\n const connection = new this.RTCPeerConnectionCtor(this.buildRtcConfiguration());\n const slot: PeerSlot = {\n connection,\n channel: undefined,\n pendingSends: [],\n pendingFragments: new Map(),\n pendingRemoteIce: [],\n inFlightSync: undefined,\n transport: undefined,\n lastDataChannelError: undefined,\n lastSyncHandshakeAttempt: emptySyncHandshakeAttempt(),\n handles: new Map(),\n };\n this.slots.set(fromPeerId, slot);\n this.wireConnection(fromPeerId, connection);\n connection.ondatachannel = (event) => {\n slot.channel = event.channel;\n this.wireDataChannel(fromPeerId, event.channel);\n };\n await connection.setRemoteDescription(this.applySdpPolicyFilter(sdp));\n await this.flushPendingRemoteIce(slot);\n const answer = await connection.createAnswer();\n await connection.setLocalDescription(answer);\n const localAnswer = connection.localDescription ?? answer;\n const sdpToSend = this.applySdpPolicyFilter({\n type: localAnswer.type,\n sdp: localAnswer.sdp ?? answer.sdp,\n });\n this.signaling.sendSignal(fromPeerId, {\n kind: \"answer\",\n sdp: sdpToSend,\n } satisfies SignalingPayload);\n }\n\n private async handleAnswer(fromPeerId: string, sdp: RTCSessionDescriptionInit): Promise<void> {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n // Drop answers that arrive after the connection has already settled\n // — the signalling relay can echo a peer's frames more than once\n // under reconnect or reload, and a duplicate answer in the `stable`\n // state otherwise throws `InvalidStateError` and kills the slot. We\n // only ever expect an answer while we still have a local offer\n // pending; anything else is benign noise.\n if (slot.connection.signalingState !== \"have-local-offer\") return;\n await slot.connection.setRemoteDescription(this.applySdpPolicyFilter(sdp));\n await this.flushPendingRemoteIce(slot);\n }\n\n private async handleIceCandidate(\n fromPeerId: string,\n candidate: RTCIceCandidateInit\n ): Promise<void> {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n // Receive-side mirror of {@link shouldSendCandidate}: even with a\n // perfectly behaved peer we cannot assume the SDP and trickle\n // streams agree on policy enforcement, and a non-polly remote may\n // not filter at all. Drop anything that isn't relay-typed when\n // the local policy is `\"relay\"`.\n if (\n this.iceRelayEnforcement &&\n this.iceTransportPolicy === \"relay\" &&\n !this.isRelayCandidateInit(candidate)\n )\n return;\n // If the answerer gathers ICE faster than its answer SDP travels\n // back through the signalling relay, candidates can land here before\n // `setRemoteDescription` has run. addIceCandidate throws when\n // `remoteDescription` is null on Chrome 148+, and the spec does not\n // require browsers to queue these internally — so buffer until the\n // remote description is set and drain in handleAnswer/handleOffer.\n if (slot.connection.remoteDescription === null) {\n slot.pendingRemoteIce.push(candidate);\n return;\n }\n try {\n await slot.connection.addIceCandidate(candidate);\n } catch {\n // Ignore candidate errors — a stale candidate after the connection\n // has already opened is benign.\n }\n }\n\n /** Drain the per-slot queue of remote ICE candidates that arrived\n * before `setRemoteDescription` completed. Errors per candidate are\n * swallowed for the same reason {@link handleIceCandidate} swallows\n * them — a single bad candidate must not stall the connection. */\n private async flushPendingRemoteIce(slot: PeerSlot): Promise<void> {\n if (slot.pendingRemoteIce.length === 0) return;\n const queued = slot.pendingRemoteIce;\n slot.pendingRemoteIce = [];\n for (const candidate of queued) {\n try {\n await slot.connection.addIceCandidate(candidate);\n } catch {\n // Same rationale as the live-path catch in handleIceCandidate.\n }\n }\n }\n\n private wireConnection(peerId: string, connection: RTCPeerConnection): void {\n connection.onicecandidate = (event) => {\n if (!event.candidate) return;\n if (!this.shouldSendCandidate(event.candidate)) return;\n this.signaling.sendSignal(peerId, {\n kind: \"ice\",\n candidate: event.candidate.toJSON(),\n } satisfies SignalingPayload);\n };\n connection.onconnectionstatechange = () => {\n const state = connection.connectionState;\n if (state === \"connected\") {\n this.emitPeerCandidateOnce(peerId);\n } else if (state === \"disconnected\" || state === \"failed\" || state === \"closed\") {\n this.slots.delete(peerId);\n this.emit(\"peer-disconnected\", { peerId: peerId as unknown as PeerId });\n }\n };\n }\n\n /** Emit `peer-candidate` upward exactly once per slot lifetime,\n * stamping the slot's `lastSyncHandshakeAttempt.peerCandidateEmittedAt`\n * with the moment of emission. The \"once\" semantics protect Automerge's\n * NetworkSubsystem from a double-add when both the\n * `connectionstatechange = connected` event AND the\n * `dataChannel.onopen` event fire on the same slot — under werift\n * the former is sometimes flaky, under Chrome the latter sometimes\n * fires first; pre-#106 only the connection-state path emitted, so a\n * werift slot whose data channel opened cleanly but whose connection\n * state never advanced past `connecting` would never signal \"ready\"\n * upward. Polly issue #106 Failure B item — closing one named\n * mechanism for \"data channel open, sync never started\". */\n private emitPeerCandidateOnce(peerId: string): void {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n if (slot.lastSyncHandshakeAttempt.peerCandidateEmittedAt !== undefined) return;\n slot.lastSyncHandshakeAttempt.peerCandidateEmittedAt = performance.now();\n this.emit(\"peer-candidate\", {\n peerId: peerId as unknown as PeerId,\n peerMetadata: {},\n });\n }\n\n private wireDataChannel(peerId: string, channel: RTCDataChannel): void {\n channel.onopen = () => {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n slot.lastSyncHandshakeAttempt.dataChannelOpenedAt = performance.now();\n // Mirror the connection-state `connected` path: if Automerge has\n // not yet been told the peer exists (because the connection\n // state event hasn't fired or fired with a state we don't\n // forward), the `onopen` callback is a strictly better signal\n // anyway — bytes can flow now. The dedupe inside\n // {@link emitPeerCandidateOnce} keeps Automerge's bookkeeping\n // single-add. Polly issue #106 Failure B.\n this.emitPeerCandidateOnce(peerId);\n // Drain any pending sends that were queued while the channel\n // was still connecting. The fragmenting helper handles oversized\n // payloads the same way it would on a live send.\n for (const bytes of slot.pendingSends) {\n void this.sendBytesMaybeFragmented(channel, bytes);\n }\n slot.pendingSends = [];\n };\n channel.onmessage = (event) => {\n const data = event.data;\n if (data instanceof ArrayBuffer) {\n this.dispatchMessage(peerId, new Uint8Array(data));\n } else if (data instanceof Uint8Array) {\n this.dispatchMessage(peerId, data);\n }\n // Other types (strings, Blobs) are ignored — Polly's mesh transport\n // only sends binary payloads via this adapter.\n };\n channel.onclose = () => {\n const slot = this.slots.get(peerId);\n if (slot?.channel === channel) {\n slot.channel = undefined;\n }\n };\n // Capture the last data-channel error so a consumer harness can\n // surface \"the wire-level error that took this peer offline\" even\n // if the channel has since closed. Polly issue #105 item 7. The\n // event shape differs between Chrome (RTCErrorEvent) and werift\n // (a plain object with `.error`); we narrow to a string for both.\n channel.onerror = (event) => {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n const ev = event as unknown as { error?: { message?: unknown }; message?: unknown };\n const message =\n typeof ev.error?.message === \"string\"\n ? ev.error.message\n : typeof ev.message === \"string\"\n ? ev.message\n : \"unknown data channel error\";\n slot.lastDataChannelError = message;\n };\n }\n\n /** Refresh the per-peer {@link TransportSnapshot} for one peer by\n * pulling {@link RTCPeerConnection.getStats} and distilling the\n * result. Cheap-ish but not free — getStats walks the underlying\n * stats graph — so this is opt-in: callers (typically a debugging\n * harness or a periodic observability loop) invoke it explicitly\n * and the result lives on the slot for the next\n * {@link getPeerStateSnapshot}. Returns the refreshed snapshot, or\n * `undefined` if there is no slot for the peer or stats are\n * unavailable. Polly issue #105 item 7. */\n async refreshTransportStats(peerId: string): Promise<TransportSnapshot | undefined> {\n const slot = this.slots.get(peerId);\n if (!slot) return undefined;\n const snapshot = await collectTransportSnapshot(slot.connection, slot.lastDataChannelError);\n slot.transport = snapshot;\n return snapshot;\n }\n\n /** Refresh transport stats for every active peer slot in one shot.\n * Returns a map keyed by peerId so a caller can render the result\n * without re-walking {@link getPeerStateSnapshot}. The underlying\n * `getStats` calls run concurrently. */\n async refreshAllTransportStats(): Promise<Map<string, TransportSnapshot>> {\n const out = new Map<string, TransportSnapshot>();\n const peerIds = [...this.slots.keys()];\n const results = await Promise.all(peerIds.map((id) => this.refreshTransportStats(id)));\n for (let i = 0; i < peerIds.length; i += 1) {\n const r = results[i];\n const id = peerIds[i];\n if (r && id !== undefined) out.set(id, r);\n }\n return out;\n }\n\n private dispatchMessage(fromPeerId: string, bytes: Uint8Array): void {\n try {\n // Intercept sync-fragments first: an oversized Automerge sync\n // message is split into fragments on the sender side because a\n // single RTCDataChannel.send above the SCTP maxMessageSize cap\n // silently drops or stalls the channel. Reassemble and re-dispatch\n // once every fragment of an id has arrived.\n if (isSyncFragmentType(bytes)) {\n this.handleSyncFragment(fromPeerId, bytes);\n return;\n }\n // Intercept blob messages before they reach the Automerge deserialiser.\n // Blob headers have type fields starting with \"blob-\" and would fail\n // MeshNetworkAdapter's signed-envelope unwrap if passed through.\n if (this.onBlobMessage && isBlobMessageType(bytes)) {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as Record<string, unknown>;\n const data = bytes.subarray(4 + headerLen);\n this.onBlobMessage(fromPeerId, header, data);\n return;\n }\n const message = this.deserialiseMessage(bytes);\n this.scheduleEmitMessage(fromPeerId, message, false);\n } catch {\n // Drop malformed messages silently — the MeshNetworkAdapter wrapper\n // also drops on verification failure, so a corrupt frame at this\n // layer is observationally the same as a forgery at the layer above.\n }\n }\n\n /** Hand a deserialised Automerge message off to whoever is listening\n * on this adapter's `\"message\"` event. When\n * {@link MeshWebRTCAdapterOptions.syncYieldEnabled} is true (the\n * default), the emit runs on a fresh macrotask so the crypto-unwrap\n * and Automerge `applyChanges` chain downstream of this method does\n * not sit on the same JS stack frame as the wire `onmessage` callback\n * — that's the receiver-side starvation site polly issue #104\n * documents. When the option is false the emit is synchronous,\n * recovering the pre-fix shape used by the falsification path.\n *\n * The `viaFragmentPath` argument tags whether this dispatch came out\n * of a reassembled fragment chain; only those carry an\n * `inFlightSync` reassembly state worth bookkeeping. Small\n * single-message dispatches yield but don't touch inFlightSync. */\n private scheduleEmitMessage(\n fromPeerId: string,\n message: Message,\n viaFragmentPath: boolean\n ): void {\n this.stampFirstInboundMessage(fromPeerId);\n this.stampHandleInbound(fromPeerId, message);\n if (!this.syncYieldEnabled) {\n this.emit(\"message\", message);\n if (viaFragmentPath) {\n this.finishInFlightSyncApply(fromPeerId);\n }\n return;\n }\n if (viaFragmentPath) {\n const slot = this.slots.get(fromPeerId);\n if (slot?.inFlightSync) {\n slot.inFlightSync.applyBacklog += 1;\n }\n }\n setTimeout(() => {\n try {\n this.emit(\"message\", message);\n } finally {\n if (viaFragmentPath) {\n this.finishInFlightSyncApply(fromPeerId);\n }\n }\n }, 0);\n }\n\n /** Stamp the slot's `firstInboundMessageAt` the first time a\n * dispatched (non-fragment, non-blob) message lands for a peer. Pure\n * observability for {@link SyncHandshakeAttemptSnapshot}; does not\n * affect dispatch. */\n private stampFirstInboundMessage(fromPeerId: string): void {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n if (slot.lastSyncHandshakeAttempt.firstInboundMessageAt !== undefined) return;\n slot.lastSyncHandshakeAttempt.firstInboundMessageAt = performance.now();\n }\n\n /** Stamp `handles[documentId].lastSyncMessageInAt` for the\n * per-handle observability layer polly#107 adds. Pure observability;\n * does not affect dispatch. */\n private stampHandleInbound(fromPeerId: string, message: Message): void {\n const documentId = (message as unknown as { documentId?: string }).documentId;\n if (typeof documentId !== \"string\") return;\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n let entry = slot.handles.get(documentId);\n if (!entry) {\n entry = emptyHandleSyncSnapshot();\n slot.handles.set(documentId, entry);\n }\n entry.lastSyncMessageInAt = performance.now();\n }\n\n private finishInFlightSyncApply(fromPeerId: string): void {\n const slot = this.slots.get(fromPeerId);\n if (!slot?.inFlightSync) return;\n slot.inFlightSync.applyBacklog = Math.max(0, slot.inFlightSync.applyBacklog - 1);\n this.emitSyncProgress(fromPeerId, \"dispatch-applied\", 0);\n if (slot.inFlightSync.applyBacklog === 0 && slot.pendingFragments.size === 0) {\n slot.inFlightSync = undefined;\n }\n }\n\n private emitSyncProgress(\n fromPeerId: string,\n kind: \"fragment-received\" | \"dispatch-applied\",\n bytesDelta: number\n ): void {\n const slot = this.slots.get(fromPeerId);\n const inFlightSync = slot?.inFlightSync;\n // The `sync-progress` event is polly-specific; Automerge's\n // NetworkAdapter event-type union doesn't include it. Cast around\n // the typed emit so consumers can subscribe via the same `.on(...)`\n // surface that carries `peer-candidate` and `peer-disconnected`.\n (\n this as unknown as {\n emit: (event: string, payload: SyncProgressEvent) => void;\n }\n ).emit(\"sync-progress\", {\n peerId: fromPeerId,\n kind,\n bytesDelta,\n chunksReceived: inFlightSync?.chunksReceived ?? 0,\n bytesReceived: inFlightSync?.bytesReceived ?? 0,\n applyBacklog: inFlightSync?.applyBacklog ?? 0,\n at: performance.now(),\n });\n }\n\n private handleSyncFragment(fromPeerId: string, bytes: Uint8Array): void {\n const parsed = parseSyncFragment(bytes);\n if (!parsed) return;\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n const { header, data } = parsed;\n let entry = slot.pendingFragments.get(header.id);\n if (!entry) {\n entry = { chunks: new Map(), total: header.total };\n slot.pendingFragments.set(header.id, entry);\n }\n // The data view is a window onto the wire frame buffer. Copy out so\n // the reassembled message owns its bytes and the wire frame can be\n // garbage-collected.\n entry.chunks.set(header.index, data.slice());\n if (!slot.inFlightSync) {\n slot.inFlightSync = {\n chunksReceived: 0,\n bytesReceived: 0,\n lastChunkAt: performance.now(),\n applyBacklog: 0,\n };\n }\n slot.inFlightSync.chunksReceived += 1;\n slot.inFlightSync.bytesReceived += data.byteLength;\n slot.inFlightSync.lastChunkAt = performance.now();\n this.emitSyncProgress(fromPeerId, \"fragment-received\", data.byteLength);\n if (entry.chunks.size < entry.total) return;\n slot.pendingFragments.delete(header.id);\n const reassembled = reassembleSyncFragments(entry.chunks, entry.total);\n if (!this.syncYieldEnabled) {\n this.dispatchReassembled(fromPeerId, reassembled);\n return;\n }\n setTimeout(() => {\n this.dispatchReassembled(fromPeerId, reassembled);\n }, 0);\n }\n\n /** Dispatch a reassembled fragment payload back through\n * {@link dispatchMessage}, but tagged so the\n * {@link scheduleEmitMessage} path knows it owes a\n * `finishInFlightSyncApply` afterwards. Synchronous re-entry into\n * `dispatchMessage` would lose that signal, so the post-fragment\n * deserialise+emit is inlined here. */\n private dispatchReassembled(fromPeerId: string, bytes: Uint8Array): void {\n try {\n if (this.onBlobMessage && isBlobMessageType(bytes)) {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as Record<string, unknown>;\n const data = bytes.subarray(4 + headerLen);\n this.onBlobMessage(fromPeerId, header, data);\n this.finishInFlightSyncApply(fromPeerId);\n return;\n }\n const message = this.deserialiseMessage(bytes);\n this.scheduleEmitMessage(fromPeerId, message, true);\n } catch {\n // Same swallowing rationale as dispatchMessage; record the apply\n // completion so the inFlightSync state doesn't get stuck.\n this.finishInFlightSyncApply(fromPeerId);\n }\n }\n\n /** Peer IDs with an open data channel, suitable for blob requests. */\n get connectedPeerIds(): string[] {\n const ids: string[] = [];\n for (const [peerId, slot] of this.slots) {\n if (slot.channel && slot.channel.readyState === \"open\") {\n ids.push(peerId);\n }\n }\n return ids;\n }\n\n /** Send a pre-serialised blob message to a specific peer. Returns false\n * if the peer is not connected or the send buffer is above the high-water\n * mark (caller should retry after a delay). */\n sendBlobMessage(peerId: string, bytes: Uint8Array<ArrayBuffer>): boolean {\n const slot = this.slots.get(peerId);\n if (!slot?.channel || slot.channel.readyState !== \"open\") return false;\n return this.trySendOnChannel(slot.channel, bytes);\n }\n\n /** Send bytes on a data channel if the buffer is below the high-water\n * mark. Returns true if sent, false if backpressure applies. */\n private trySendOnChannel(channel: RTCDataChannel, bytes: Uint8Array<ArrayBuffer>): boolean {\n // 256 KiB high-water mark — matches the blob transfer default.\n if (channel.bufferedAmount > 256 * 1024) return false;\n channel.send(bytes);\n return true;\n }\n\n /** Pack an Automerge Message into binary for transmission over the\n * data channel. The format mirrors MeshNetworkAdapter's internal\n * serialisation: a length-prefixed JSON header followed by the raw\n * data bytes. Returns a Uint8Array<ArrayBuffer> so the result is\n * directly usable by RTCDataChannel.send under TypeScript's strict\n * buffer-source typing. */\n private serialiseMessage(message: Message): Uint8Array<ArrayBuffer> {\n const headerObj: Record<string, unknown> = {\n type: message.type,\n senderId: message.senderId,\n targetId: message.targetId,\n };\n if (\"documentId\" in message && message.documentId !== undefined) {\n headerObj[\"documentId\"] = message.documentId;\n }\n const headerBytes = new TextEncoder().encode(JSON.stringify(headerObj));\n const dataBytes: Uint8Array =\n \"data\" in message && message.data instanceof Uint8Array ? message.data : new Uint8Array(0);\n const size = 4 + headerBytes.length + dataBytes.length;\n const buffer = new ArrayBuffer(size);\n const out = new Uint8Array(buffer);\n const view = new DataView(buffer);\n view.setUint32(0, headerBytes.length, false);\n out.set(headerBytes, 4);\n out.set(dataBytes, 4 + headerBytes.length);\n return out;\n }\n\n /** Inverse of {@link serialiseMessage}. */\n private deserialiseMessage(bytes: Uint8Array): Message {\n if (bytes.length < 4) {\n throw new Error(\"MeshWebRTCAdapter: message too short to deserialise.\");\n }\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) {\n throw new Error(\"MeshWebRTCAdapter: message header truncated.\");\n }\n const header = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + headerLen)));\n const data = bytes.slice(4 + headerLen);\n return { ...header, data } as unknown as Message;\n }\n}\n",
|
|
23
|
+
"/**\n * crdt-state — base machinery for Polly's peer-first state primitives.\n *\n * This module is transport-agnostic: it takes a caller-supplied async factory\n * that produces a ready {@link DocHandle}, binds it bidirectionally to a\n * Preact signal, runs any pending schema migrations on load, and integrates\n * with the primitive-registry and migration-registry guards. Phase 1's\n * $peerState and Phase 2's $meshState both construct these base primitives\n * with their own handle factories — one over Automerge-Repo's WebSocket\n * client adapter, the other over WebRTC — and the base never knows which.\n *\n * The signal-to-handle binding uses an `updating` guard flag to prevent write\n * loops: when a local signal assignment runs the effect that pushes the value\n * into `handle.change`, the flag is raised so that the 'change' event the\n * handle fires back is ignored. The same flag protects in the other direction\n * when a remote change seeds the signal.\n *\n * For the Phase 0 cut, writes are applied with a naive top-level structural\n * replacement inside the `Automerge.change` block. This is correct for\n * JSON-shaped documents with scalar and flat-object fields and is good enough\n * to exercise the rest of the pipeline. The specialised variants for text,\n * counters, and lists (which require type-specific operation capture to\n * preserve concurrent-edit semantics) land in Phase 1's crdt-specialised.ts.\n */\n\nimport type { DocHandle } from \"@automerge/automerge-repo/slim\";\nimport { effect, signal } from \"@preact/signals\";\nimport type { Access } from \"./access\";\nimport { type MigratableState, MigrationError, migrationRegistry } from \"./migrate-primitive\";\nimport { type PrimitiveKind, primitiveRegistry } from \"./primitive-registry\";\nimport {\n type Migrations,\n runMigrations,\n SCHEMA_VERSION_FIELD,\n setDocVersion,\n type VersionedDoc,\n} from \"./schema-version\";\n\n/**\n * The interface a Polly peer-first primitive exposes at the call site. It\n * satisfies {@link MigratableState} so that the cross-primitive migration\n * helper can consume it directly.\n */\nexport interface CrdtPrimitive<T extends VersionedDoc> extends MigratableState<T> {\n /** Stable logical key the primitive was registered under. */\n readonly key: string;\n /** Primitive kind — one of the {@link PrimitiveKind} labels. */\n readonly primitive: PrimitiveKind;\n /** Current value. Writes push into the backing Automerge document. */\n value: T;\n /** Resolves when the handle is ready and migrations have run. */\n readonly loaded: Promise<void>;\n /** The underlying {@link DocHandle}, populated after {@link loaded} resolves.\n * Intended for advanced escape hatches; most callers should stay at the\n * signal surface. */\n readonly handle: DocHandle<T> | undefined;\n}\n\n/**\n * Options for constructing a base CRDT-backed primitive. Phase 1 and Phase 2\n * primitive constructors pass a transport-specific {@link getHandle} factory\n * and their own {@link primitive} label; everything else is shared.\n */\nexport interface CrdtStateOptions<T extends VersionedDoc> {\n /** Stable logical key identifying this piece of state. */\n key: string;\n /** Primitive kind label for registry and error-message purposes. */\n primitive: PrimitiveKind;\n /** Initial value if no stored document exists yet. Applied by the caller's\n * handle factory; the base module does not create documents itself. */\n initialValue: T;\n /** Async factory that resolves to a ready {@link DocHandle}. The factory is\n * responsible for repo lookup, document creation, and any transport-specific\n * setup. The base module calls this once, during hydration. */\n getHandle: () => Promise<DocHandle<T>>;\n /** Optional reactive redirect. When provided, the primitive runs\n * this against every doc state (initial load and every `change`\n * event afterwards). Returning a {@link DocHandle} different from\n * the currently-bound one tells the primitive to detach from the\n * current handle and rebind to the returned one — the signal\n * re-fires with the new doc's state and subsequent writes target\n * the new handle. Returning `undefined` keeps the current binding.\n *\n * Designed for ADR-0008-style document compaction redirects: the\n * caller (e.g. fairfox) provides a callback that inspects the doc\n * for a sealed-sentinel field and, if present, resolves the named\n * `migratedTo` docId via its repo. Continuous, so a peer-synced\n * sentinel rebinds the wrapper without a reload. */\n resolveRedirect?: (handle: DocHandle<T>, doc: T) => Promise<DocHandle<T> | undefined>;\n /** Target schema version for the application. If set, migrations run on\n * load to bring the document up to this version before the signal hydrates. */\n schemaVersion?: number;\n /** Migration table. Ignored if {@link schemaVersion} is not set. */\n migrations?: Migrations;\n /** Declarative access predicates. Not consumed by the base module; the\n * transport-specific constructors compile it to their enforcement layer. */\n access?: Access;\n /** Optional free-text call-site label for primitive-registry error messages. */\n callSite?: string;\n}\n\n/**\n * Construct a base CRDT-backed Polly primitive. Integrates with\n * primitive-registry (for collision detection), migration-registry (for\n * cross-family migration guards), and schema-version (for on-load migrations).\n *\n * @throws {MigrationError} if the source key has been marked as migrated.\n * @throws {PrimitiveCollisionError} if the key is already registered under a\n * different primitive kind.\n */\nexport function $crdtState<T extends VersionedDoc>(options: CrdtStateOptions<T>): CrdtPrimitive<T> {\n if (migrationRegistry.isMarked(options.key, options.primitive)) {\n throw new MigrationError(\n `Cannot construct $${options.primitive}(\"${options.key}\"): this key has been marked as migrated. Migrations are one-way; use the destination primitive instead.`,\n \"already-migrated\",\n options.key,\n options.primitive\n );\n }\n primitiveRegistry.register(options.key, options.primitive, options.callSite);\n\n const inner = signal<T>(options.initialValue);\n let updating = false;\n let currentHandle: DocHandle<T> | undefined;\n // Detach handle is bound to the live `currentHandle`; rebind swaps\n // both the handle reference (used by the signal→remote effect) and\n // this detacher (called before attaching a fresh listener to the\n // post-redirect handle).\n let detachChangeListener: (() => void) | undefined;\n // Re-entry guard for the change → resolveRedirect → swap →\n // re-listen chain. The change listener fires synchronously off\n // Automerge's state machine; without the guard a swap-while-handling\n // would race the listener attach.\n let swapping = false;\n\n function attachChangeListener(handle: DocHandle<T>): void {\n const listener = (payload: { doc: T }): void => {\n if (updating) return;\n updating = true;\n try {\n inner.value = cloneDoc(payload.doc);\n } finally {\n updating = false;\n }\n // After every change, see if the doc now points elsewhere\n // (e.g. a peer-synced compaction sentinel). The async path\n // runs in the background; the change listener itself stays\n // synchronous so Automerge's event dispatch isn't blocked.\n if (options.resolveRedirect && !swapping) {\n void maybeRebind(handle, payload.doc);\n }\n };\n handle.on(\"change\", listener);\n detachChangeListener = () => {\n handle.off(\"change\", listener);\n };\n }\n\n async function maybeRebind(fromHandle: DocHandle<T>, doc: T): Promise<void> {\n if (!options.resolveRedirect) return;\n if (swapping) return;\n swapping = true;\n try {\n const next = await options.resolveRedirect(fromHandle, doc);\n if (!next) return;\n if (next === currentHandle) return;\n if (next.documentId === fromHandle.documentId) return;\n // Detach from the old handle, swap, re-attach, re-fire the signal.\n detachChangeListener?.();\n detachChangeListener = undefined;\n currentHandle = next;\n attachChangeListener(next);\n updating = true;\n try {\n inner.value = cloneDoc(next.doc());\n } finally {\n updating = false;\n }\n } finally {\n swapping = false;\n }\n }\n\n async function followInitialRedirects(start: DocHandle<T>): Promise<DocHandle<T>> {\n if (!options.resolveRedirect) return start;\n let handle = start;\n const seen = new Set<string>([handle.documentId as unknown as string]);\n for (;;) {\n const doc = handle.doc();\n if (!doc) break;\n const next = await options.resolveRedirect(handle, doc);\n if (!next || next === handle) break;\n const nextIdString = next.documentId as unknown as string;\n if (seen.has(nextIdString)) break;\n seen.add(nextIdString);\n handle = next;\n await handle.whenReady();\n }\n return handle;\n }\n\n function applyPendingMigrations(handle: DocHandle<T>): void {\n if (options.schemaVersion === undefined) return;\n const targetVersion = options.schemaVersion;\n const migrations = options.migrations ?? {};\n handle.change((doc) => {\n runMigrations(doc as unknown as Record<string, unknown>, targetVersion, migrations);\n // runMigrations stamps the version on every intermediate step; make\n // sure the final value is recorded even when no migrations ran.\n setDocVersion(doc as unknown as Record<string, unknown>, targetVersion);\n });\n }\n\n function bindSignalToHandle(): void {\n effect(() => {\n const value = inner.value;\n if (updating) return;\n if (!currentHandle) return;\n updating = true;\n try {\n currentHandle.change((doc) => {\n applyTopLevel(doc as unknown as Record<string, unknown>, value);\n });\n } finally {\n updating = false;\n }\n });\n }\n\n const loaded = (async () => {\n const initialHandle = await options.getHandle();\n await initialHandle.whenReady();\n // Initial redirect check before we attach the listener — a doc\n // that's already sealed at load time shouldn't fire a write loop\n // through the change listener; just resolve to the post-redirect\n // handle up front. The same recursive follow lives in\n // {@link maybeRebind} for changes that arrive after load.\n const handle = await followInitialRedirects(initialHandle);\n currentHandle = handle;\n\n // Run any pending schema migrations inside a change block so they land\n // as a single Automerge operation set.\n applyPendingMigrations(handle);\n\n // Seed the signal with the hydrated doc state. Raise the guard first so\n // the 'change' listener we install below does not echo this write back.\n updating = true;\n try {\n inner.value = cloneDoc(handle.doc());\n } finally {\n updating = false;\n }\n\n // Remote-changes-to-signal binding. Routed through a helper so\n // {@link maybeRebind} can detach + re-attach against the\n // post-redirect handle when a sentinel appears.\n attachChangeListener(handle);\n\n // Signal-to-remote binding. The effect runs once on registration with\n // the already-hydrated value; the guard makes that first run a no-op at\n // the handle level because updating is false but the doc already equals\n // the signal, so Automerge records no new operations.\n bindSignalToHandle();\n })();\n\n return {\n key: options.key,\n primitive: options.primitive,\n get value() {\n return inner.value;\n },\n set value(next: T) {\n inner.value = next;\n },\n loaded,\n get handle() {\n return currentHandle;\n },\n };\n}\n\n/**\n * Shallow clone of an Automerge doc into a plain JS object. Automerge docs\n * are Proxies; the signal holds a detached plain-object snapshot so that\n * application code does not accidentally mutate the CRDT through the signal.\n */\nfunction cloneDoc<T>(doc: T): T {\n return JSON.parse(JSON.stringify(doc)) as unknown as T;\n}\n\n/**\n * Copy every top-level field from the incoming value onto the Automerge doc\n * — but only for fields whose serialised form actually differs from what's\n * already there. The equality skip is load-bearing, not an optimisation:\n *\n * - Automerge records a new operation for every assignment (`doc[k] = v`),\n * even when v is structurally identical to the current value.\n * - Every op triggers a state-machine transition, which via\n * `#checkForChanges` fires a `change` event, which the signal binding\n * above handles by setting `inner.value = cloneDoc(...)`.\n * - Under certain event-loop timings — most consistently on bun — preact\n * signals' synchronous effect propagation re-runs this write path with\n * the just-cloned value, which compares ref-different to the previous\n * signal value, so the effect doesn't short-circuit, so applyTopLevel\n * runs again, so more ops are recorded, and the whole chain loops until\n * preact's own cycle counter trips with \"Cycle detected\".\n *\n * Skipping value-equal assignments makes the write a true no-op at the\n * Automerge level, which makes `docChanged` in `#checkForChanges` false,\n * which skips the change emission, which breaks the loop at its origin.\n * Browsers mask this under typical interactive timing; a tight CLI boot\n * reproduces it every time.\n *\n * The reserved schema-version field is not copied — it is managed by the\n * migration subsystem and must not be overwritten by application writes.\n */\nfunction applyTopLevel<T extends VersionedDoc>(doc: Record<string, unknown>, value: T): void {\n const source = value as unknown as Record<string, unknown>;\n for (const key of Object.keys(value)) {\n if (key === SCHEMA_VERSION_FIELD) continue;\n const incoming = source[key];\n if (fieldEquals(doc[key], incoming)) continue;\n doc[key] = incoming;\n }\n}\n\n/** Structural equality check for the top-level-field comparison. JSON.stringify\n * round-trip because Automerge docs are Proxies and `===` would miss a match\n * when the proxy wraps an equal value. Arrays, objects, and primitives all\n * round-trip cleanly for the CRDT-state use case. Cycles in the input aren't\n * supported (neither is Automerge — it refuses cyclic structures). */\nfunction fieldEquals(a: unknown, b: unknown): boolean {\n if (a === b) return true;\n try {\n return JSON.stringify(a) === JSON.stringify(b);\n } catch {\n return false;\n }\n}\n",
|
|
24
|
+
"/**\n * mesh-webrtc-adapter — Phase 2 browser-side WebRTC transport for Polly's\n * $meshState primitive. Extends Automerge's NetworkAdapter base class and\n * uses real native RTCPeerConnection / RTCDataChannel instances to carry\n * sync messages between peers.\n *\n * This is the \"base\" transport that MeshNetworkAdapter wraps with its\n * sign-then-encrypt envelope. The stack is:\n *\n * $meshState\n * └─ Repo\n * └─ MeshNetworkAdapter (sign + encrypt)\n * └─ MeshWebRTCAdapter (real data channels)\n * └─ MeshSignalingClient (SDP/ICE relay)\n * └─ signalingServer (Elysia plugin on the host app)\n *\n * Because WebRTC lives in browsers, this module is browser-only. It\n * assumes global RTCPeerConnection, RTCDataChannel, and WebSocket types\n * are available. Under bun:test the classes cannot be exercised\n * end-to-end — the first validation path is either Playwright running a\n * real browser, a Puppeteer harness, or a human testing a browser-side\n * example app that consumes the adapter.\n *\n * What this module does at runtime:\n *\n * - Constructs a MeshWebRTCAdapter with a signalling client and a local\n * peer id. No data channels exist at construction time.\n *\n * - When Automerge's NetworkSubsystem calls connect(peerId) on the\n * adapter, it starts listening for signals from remote peers and is\n * ready to build peer connections as they are discovered.\n *\n * - When a remote peer sends an SDP offer via the signalling channel,\n * the adapter builds a fresh RTCPeerConnection, accepts the offer,\n * produces an answer, sends it back through signalling, and wires the\n * received data channel to emit Automerge Message events upward.\n *\n * - When the local repo calls send(message), the adapter looks up the\n * peer connection for message.targetId and writes the serialised\n * bytes to its data channel. If no connection exists yet, the adapter\n * creates one by sending an SDP offer through signalling. Outgoing\n * messages are queued until the channel is open.\n *\n * - Disconnect tears down every peer connection and closes the\n * signalling client.\n */\n\nimport {\n type Message,\n NetworkAdapter,\n type PeerId,\n type PeerMetadata,\n} from \"@automerge/automerge-repo/slim\";\nimport { isBlobMessageType } from \"./blob-transfer\";\nimport type { MeshKeyring } from \"./mesh-network-adapter\";\nimport type { MeshSignalingClient } from \"./mesh-signaling-client\";\nimport {\n chunkSyncMessage,\n isSyncFragmentType,\n parseSyncFragment,\n reassembleSyncFragments,\n SYNC_FRAGMENT_CHUNK_SIZE,\n SYNC_FRAGMENT_THRESHOLD,\n} from \"./sync-fragment\";\n\n/** Distil an {@link RTCStatsReport} into a {@link TransportSnapshot}.\n * Resolves the selected candidate pair via the transport stat's\n * `selectedCandidatePairId` (the W3C-spec field that names the pair\n * ICE has nominated for the data path), then pulls the candidate-type\n * labels from the linked candidate stats. Retransmission counters\n * come off the transport stat where present; some implementations\n * surface them on the data-channel stat instead — we accept either.\n *\n * Out-of-band so the test bed can call it against a bare\n * {@link RTCPeerConnection} as well, which the polly#105 harness's\n * `probe-stats.ts` does to validate the stats shape before threading\n * the result through the polly layer. */\nasync function collectTransportSnapshot(\n connection: RTCPeerConnection,\n lastDataChannelError: string | undefined\n): Promise<TransportSnapshot> {\n const at = performance.now();\n let report: RTCStatsReport | undefined;\n try {\n report = await connection.getStats();\n } catch {\n return emptyTransportSnapshot(lastDataChannelError, at);\n }\n const parsed = partitionStats(report);\n const selectedPair = selectActivePair(parsed);\n const selectedCandidatePair = selectedPair\n ? buildCandidatePairView(selectedPair, parsed.localCands, parsed.remoteCands)\n : undefined;\n return {\n selectedCandidatePair,\n retransmittedPacketsSent: parsed.retransmittedPacketsSent,\n retransmittedBytesSent: parsed.retransmittedBytesSent,\n lastDataChannelError,\n at,\n };\n}\n\ninterface ParsedStats {\n localCands: Map<string, Record<string, unknown>>;\n remoteCands: Map<string, Record<string, unknown>>;\n pairs: Map<string, Record<string, unknown>>;\n selectedPairId: string | undefined;\n retransmittedPacketsSent: number | undefined;\n retransmittedBytesSent: number | undefined;\n}\n\nfunction emptyTransportSnapshot(\n lastDataChannelError: string | undefined,\n at: number\n): TransportSnapshot {\n return {\n selectedCandidatePair: undefined,\n retransmittedPacketsSent: undefined,\n retransmittedBytesSent: undefined,\n lastDataChannelError,\n at,\n };\n}\n\nfunction partitionStats(report: RTCStatsReport): ParsedStats {\n const out: ParsedStats = {\n localCands: new Map(),\n remoteCands: new Map(),\n pairs: new Map(),\n selectedPairId: undefined,\n retransmittedPacketsSent: undefined,\n retransmittedBytesSent: undefined,\n };\n const iter = (report as { values?: () => Iterable<unknown> }).values?.() ?? [];\n for (const raw of iter) {\n if (!raw || typeof raw !== \"object\") continue;\n const stat = raw as Record<string, unknown>;\n ingestStat(stat, out);\n }\n return out;\n}\n\nfunction ingestStat(stat: Record<string, unknown>, out: ParsedStats): void {\n const id = String(stat[\"id\"]);\n switch (stat[\"type\"]) {\n case \"local-candidate\":\n out.localCands.set(id, stat);\n return;\n case \"remote-candidate\":\n out.remoteCands.set(id, stat);\n return;\n case \"candidate-pair\":\n out.pairs.set(id, stat);\n return;\n case \"transport\":\n ingestTransport(stat, out);\n return;\n case \"data-channel\":\n ingestDataChannel(stat, out);\n return;\n }\n}\n\nfunction ingestTransport(stat: Record<string, unknown>, out: ParsedStats): void {\n const selectedId = stat[\"selectedCandidatePairId\"];\n if (typeof selectedId === \"string\") out.selectedPairId = selectedId;\n const rp = stat[\"retransmittedPacketsSent\"];\n const rb = stat[\"retransmittedBytesSent\"];\n if (typeof rp === \"number\") out.retransmittedPacketsSent = rp;\n if (typeof rb === \"number\") out.retransmittedBytesSent = rb;\n}\n\nfunction ingestDataChannel(stat: Record<string, unknown>, out: ParsedStats): void {\n // Chrome puts SCTP retransmit counters on the data-channel stat for\n // SCTP-backed channels. werift puts them on the transport. Accept\n // whichever the underlying impl produces first.\n const rp = stat[\"retransmittedPacketsSent\"];\n const rb = stat[\"retransmittedBytesSent\"];\n if (out.retransmittedPacketsSent === undefined && typeof rp === \"number\") {\n out.retransmittedPacketsSent = rp;\n }\n if (out.retransmittedBytesSent === undefined && typeof rb === \"number\") {\n out.retransmittedBytesSent = rb;\n }\n}\n\nfunction selectActivePair(parsed: ParsedStats): Record<string, unknown> | undefined {\n if (parsed.selectedPairId) {\n const named = parsed.pairs.get(parsed.selectedPairId);\n if (named) return named;\n }\n for (const pair of parsed.pairs.values()) {\n if (pair[\"nominated\"]) return pair;\n }\n return undefined;\n}\n\n/** Render a {@link PeerSlot} as the plain-data view\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot} returns. Pulled out\n * so the snapshot method itself stays under biome's cognitive-\n * complexity ceiling — every additional optional field would push it\n * one closer; doing the shape here keeps the per-call diff small. */\nfunction serialiseSlotView(slot: PeerSlot): {\n signalingState: string;\n iceConnectionState: string;\n connectionState: string;\n dataChannelState: string;\n pendingSendCount: number;\n pendingRemoteIceCount: number;\n inFlightSync: InFlightSyncSnapshot | undefined;\n transport: TransportSnapshot | undefined;\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n handles: Record<string, HandleSyncSnapshot>;\n createdAt: number;\n lastInboundAt: number | undefined;\n} {\n const handles: Record<string, HandleSyncSnapshot> = {};\n for (const [docId, snapshot] of slot.handles) {\n handles[docId] = { ...snapshot };\n }\n return {\n signalingState: slot.connection.signalingState,\n iceConnectionState: slot.connection.iceConnectionState,\n connectionState: slot.connection.connectionState,\n dataChannelState: slot.channel?.readyState ?? \"no-channel\",\n pendingSendCount: slot.pendingSends.length,\n pendingRemoteIceCount: slot.pendingRemoteIce.length,\n inFlightSync: slot.inFlightSync ? { ...slot.inFlightSync } : undefined,\n transport: slot.transport\n ? {\n ...slot.transport,\n selectedCandidatePair: slot.transport.selectedCandidatePair\n ? { ...slot.transport.selectedCandidatePair }\n : undefined,\n }\n : undefined,\n lastSyncHandshakeAttempt: { ...slot.lastSyncHandshakeAttempt },\n handles,\n createdAt: slot.createdAt,\n lastInboundAt: slot.lastInboundAt,\n };\n}\n\n/** Initial state for {@link PeerSlot.lastSyncHandshakeAttempt}. All\n * timestamps start `undefined` and become populated as the slot\n * progresses through the lifecycle. Pulled out so every `PeerSlot`\n * construction site uses the same shape. */\nfunction emptySyncHandshakeAttempt(): SyncHandshakeAttemptSnapshot {\n return {\n dataChannelOpenedAt: undefined,\n peerCandidateEmittedAt: undefined,\n firstOutboundSendAt: undefined,\n firstInboundMessageAt: undefined,\n };\n}\n\nfunction buildCandidatePairView(\n pair: Record<string, unknown>,\n localCands: Map<string, Record<string, unknown>>,\n remoteCands: Map<string, Record<string, unknown>>\n): NonNullable<TransportSnapshot[\"selectedCandidatePair\"]> {\n const local = localCands.get(String(pair[\"localCandidateId\"]));\n const remote = remoteCands.get(String(pair[\"remoteCandidateId\"]));\n return {\n localCandidateType: String(local?.[\"candidateType\"] ?? \"?\"),\n remoteCandidateType: String(remote?.[\"candidateType\"] ?? \"?\"),\n state: String(pair[\"state\"] ?? \"\"),\n nominated: Boolean(pair[\"nominated\"]),\n bytesSent: Number(pair[\"bytesSent\"] ?? 0),\n bytesReceived: Number(pair[\"bytesReceived\"] ?? 0),\n };\n}\n\n/** Standard STUN servers for NAT traversal. In production, callers who\n * need TURN fallback for peers behind symmetric NATs should replace this\n * with their own ICE server list. */\nexport const DEFAULT_ICE_SERVERS: RTCIceServer[] = [\n { urls: \"stun:stun.l.google.com:19302\" },\n { urls: \"stun:stun1.l.google.com:19302\" },\n];\n\n/** Polly issue #109: how long a slot can sit at `connectionState` in\n * `new` or `connecting` before the watchdog tears it down so the\n * recovery sweep can re-attempt. Healthy ICE completes in single-digit\n * seconds; 30s is well past anything legitimate and short enough that\n * a silent `createOffer`/`setLocalDescription` rejection no longer\n * leaves an unrecoverable wedged slot. */\nexport const SLOT_NEVER_CONNECTED_TIMEOUT_MS = 30_000;\n\n/** Polly issue #110: how long a slot whose data channel is open and\n * whose `connectionState` is `connected` can have no inbound bytes\n * before the watchdog assumes the remote process is dead and tears\n * the slot down. The ICE keepalive timer can take minutes to notice a\n * remote SIGKILL or network partition; an application-layer\n * liveness check that runs an order of magnitude faster keeps\n * paired devices from sending sync traffic into the void after the\n * remote daemon restarts. 120s is conservative — Automerge's idle\n * cadence is well below this, so a healthy slot never crosses the\n * threshold. */\nexport const SLOT_IDLE_TIMEOUT_MS = 120_000;\n\n/** Polly issue #109/#110: how often the watchdog evaluates teardown\n * decisions across every slot. 5s is well below either threshold so\n * teardown happens promptly after a deadline lapses without\n * dominating runtime cost (one `connectionState` read per slot per\n * tick). */\nexport const SLOT_WATCHDOG_INTERVAL_MS = 5_000;\n\n/** Options for constructing a {@link MeshWebRTCAdapter}. */\nexport interface MeshWebRTCAdapterOptions {\n /** The signalling client the adapter uses to exchange SDP and ICE\n * candidates with other peers. Typically constructed once per\n * application and shared across any adapters that need it. */\n signaling: MeshSignalingClient;\n /** The local peer id. Must match the peer id the signalling client\n * registered with. */\n peerId: string;\n /** Peer ids to connect to on startup. When `connect()` is called, the\n * adapter iterates this list and initiates a WebRTC connection to each\n * one by sending an SDP offer through the signalling channel. Peers\n * not in this list can still connect by sending an offer *to* this\n * adapter. The natural source for this list is the keyring's\n * knownPeers map keys.\n *\n * Used only when {@link keyringSource} is not supplied. With\n * `keyringSource` set the adapter reads `knownPeers` live from the\n * keyring on every initiation decision, which is the shape\n * {@link createMeshClient} wires up so post-construction\n * {@link applyPairingToken} calls take effect without the consumer\n * having to call {@link MeshWebRTCAdapter.addKnownPeer} by hand. */\n knownPeerIds?: string[];\n /** Live keyring source. When supplied, the adapter reads\n * `knownPeers` from `keyringSource()` on every initiation decision\n * instead of a Set captured at construction. Combined with the\n * periodic re-sweep started in {@link MeshWebRTCAdapter.connect},\n * this makes mutations to `keyring.knownPeers` — including the ones\n * produced by {@link applyPairingToken} after the mesh client is up\n * — visible to the dial path within at most one sweep interval. The\n * crypto layer already re-reads the keyring on every send and\n * receive; this option closes the same loop for the WebRTC adapter.\n *\n * Polly issue #103: without this, a long-lived daemon that pairs a\n * new device after its mesh client is constructed never dials the\n * new peer — the adapter's captured Set stays stale even though the\n * keyring contains the new entry, and the lex-tie-break rule in\n * {@link MeshWebRTCAdapter.shouldInitiateTo} can leave both peers\n * waiting for the other to offer indefinitely. */\n keyringSource?: () => MeshKeyring;\n /** How often the adapter re-evaluates whether to dial peers in the\n * signalling roster. The sweep is what catches peers that became\n * authorised in the keyring after their `peer-joined` notification\n * already fired — the adapter has no other event to hang the retry\n * on, so it polls. Cheap: one Map lookup per present peer, fired at\n * most once per interval. Defaults to 2000ms; tests override to\n * shorten the failure budget. Set to 0 to disable the sweep\n * (the captured-set behaviour, kept only for migration). */\n knownPeersRefreshIntervalMs?: number;\n /** Optional ICE server list override. Defaults to {@link DEFAULT_ICE_SERVERS}. */\n iceServers?: RTCIceServer[];\n /** Optional ICE transport policy. Defaults to the\n * {@link RTCPeerConnection} implementation's own default (`\"all\"` in\n * Chrome, Firefox, and werift). Set to `\"relay\"` to force every\n * candidate pair through a TURN relay — the shape the polly#105\n * falsification harness needs to exercise the real-transport contract\n * the polly#104 in-process throttle could not reach. */\n iceTransportPolicy?: RTCIceTransportPolicy;\n /** When `true` (the default), polly enforces\n * {@link iceTransportPolicy} `\"relay\"` on its side of the signalling\n * channel by filtering non-relay ICE candidates out of both the SDP\n * description it forwards and the trickle ICE stream it emits. This\n * closes the gap exposed by polly#105: Chrome's implementation\n * already filters at the source, but werift only filters its own\n * outgoing connectivity checks and still advertises host and srflx\n * candidates upstream — so a relay-only peer on werift will, against\n * a peer with the default policy, still pair through a non-relay\n * candidate (or against a host-derived peer-reflexive remote).\n * Setting this option to `false` reverts the enforcement and is the\n * shape used by the `POLLY_105_DISABLE_TURN_FIX=1` falsification\n * path. Production callers should leave this at the default. */\n iceRelayEnforcement?: boolean;\n /** Optional data channel label. Defaults to \"polly-mesh\". Applications\n * that share a signalling server between multiple meshes may want\n * distinct labels per mesh. */\n dataChannelLabel?: string;\n /** RTCPeerConnection constructor. Defaults to\n * `globalThis.RTCPeerConnection`. Inject a different implementation\n * (e.g. `werift` or `@roamhq/wrtc`) when running outside a browser, or\n * to use a custom subclass for tests or instrumentation. */\n RTCPeerConnection?: typeof RTCPeerConnection;\n /** When `true` (the default), the adapter yields to the event loop\n * between the points where a large initial sync would otherwise hold\n * the main thread for tens of seconds: between each batch of\n * fragmented `RTCDataChannel.send` calls on the sender side, and at\n * the boundary between reassembling a sync message and dispatching\n * it (deserialise → MeshNetworkAdapter unwrap → Automerge\n * `applyChanges`) on the receiver side. Set to `false` to recover\n * the pre-#104 tight-loop behaviour; this is the configuration the\n * `POLLY_104_DISABLE_FIX=1` falsification path in\n * `examples/mesh-large-initial-sync` uses to demonstrate the bug\n * against post-fix polly. Production callers should leave this at\n * the default. */\n syncYieldEnabled?: boolean;\n /** Override the sync fragment chunk size. Defaults to\n * {@link SYNC_FRAGMENT_CHUNK_SIZE} (60 KiB), which leaves header\n * overhead inside werift's hard 64 KiB max-message-size cap.\n * Setting this to 64 KiB recreates the pre-#104 fragmentation bug,\n * where peer A's outbound fragments overshoot the cap and werift\n * rejects them silently — sync stalls forever. Used by the\n * `POLLY_104_DISABLE_FIX=1` falsification path. Production callers\n * should leave this at the default. */\n syncFragmentChunkSizeOverride?: number;\n /** How long a slot can sit at `connectionState` in `new` or\n * `connecting` before the watchdog tears it down. Defaults to\n * {@link SLOT_NEVER_CONNECTED_TIMEOUT_MS}. Set to 0 to disable the\n * gate. Polly issue #109. */\n slotNeverConnectedTimeoutMs?: number;\n /** How long a connected slot can have no inbound bytes before the\n * watchdog tears it down as idle. Defaults to\n * {@link SLOT_IDLE_TIMEOUT_MS}. Set to 0 to disable the gate.\n * Polly issue #110. */\n slotIdleTimeoutMs?: number;\n /** How often the slot watchdog evaluates teardown decisions.\n * Defaults to {@link SLOT_WATCHDOG_INTERVAL_MS}; tests override to\n * tens of milliseconds. Set to 0 to disable the watchdog entirely\n * (the pre-#109/#110 behaviour, kept only for migration and the\n * falsification path). */\n slotWatchdogIntervalMs?: number;\n}\n\n/** Types of signalling payload this adapter exchanges through the\n * signalling channel. The signalling server treats these as opaque. */\ntype SignalingPayload =\n | { kind: \"offer\"; sdp: RTCSessionDescriptionInit }\n | { kind: \"answer\"; sdp: RTCSessionDescriptionInit }\n | { kind: \"ice\"; candidate: RTCIceCandidateInit };\n\n/** Payload of the polly-specific `\"sync-progress\"` event emitted by\n * {@link MeshWebRTCAdapter}. Consumers can subscribe via the adapter's\n * standard `.on()` surface (the same one that carries `peer-candidate`\n * and `peer-disconnected`) to observe fragment receive and dispatch\n * activity in real time, without polling\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot}. Polly issue #104\n * item 7. */\nexport interface SyncProgressEvent {\n /** Remote peer the fragment or dispatch is for. */\n peerId: string;\n /** Lifecycle stage. `fragment-received` fires for each chunk that\n * arrives during reassembly; `dispatch-applied` fires once the\n * reassembled message has been emitted upward to Automerge. */\n kind: \"fragment-received\" | \"dispatch-applied\";\n /** Bytes carried by the chunk that triggered the event. Zero for\n * `dispatch-applied`. */\n bytesDelta: number;\n /** Running total of fragments received for the current reassembly. */\n chunksReceived: number;\n /** Running total of bytes received for the current reassembly. */\n bytesReceived: number;\n /** Number of reassembled messages whose dispatch has been scheduled\n * but not yet emitted upward to Automerge. */\n applyBacklog: number;\n /** `performance.now()` at event emission. */\n at: number;\n}\n\n/** Last-seen transport-level summary for a peer slot. Populated\n * lazily by {@link MeshWebRTCAdapter.refreshTransportStats}, which\n * the consumer calls (typically from a polling loop in a debugging\n * harness) so the cost of {@link RTCPeerConnection.getStats} is\n * incurred only on demand. Polly issue #105 item 7 — exposes the\n * dimension of the transport that {@link InFlightSyncSnapshot}\n * doesn't reach: the negotiated ICE candidate pair, the SCTP\n * retransmission counters, and the last data-channel-level error if\n * one has been observed.\n *\n * Field names mirror the W3C stats spec (`selectedCandidatePairId`,\n * `localCandidateType`, etc.) so consumers can correlate with the\n * raw `RTCStatsReport`. werift exposes a stats shape close to the\n * spec; Chrome exposes the spec itself; this view is implementation-\n * agnostic. */\nexport interface TransportSnapshot {\n /** ICE-level summary of the pair currently carrying data. */\n selectedCandidatePair:\n | {\n localCandidateType: string;\n remoteCandidateType: string;\n state: string;\n nominated: boolean;\n bytesSent: number;\n bytesReceived: number;\n }\n | undefined;\n /** SCTP / data-channel retransmission counters. Some implementations\n * surface these on the transport stat, others on the data-channel\n * stat — we expose the values we found, leaving the field undefined\n * when the underlying impl doesn't surface them. */\n retransmittedPacketsSent: number | undefined;\n retransmittedBytesSent: number | undefined;\n /** Most-recent data-channel error message, if `error` ever fired on\n * the channel for this peer. Cleared only when the slot is replaced. */\n lastDataChannelError: string | undefined;\n /** `performance.now()` at the time the stats were last refreshed. */\n at: number;\n}\n\n/** Reasons the adapter declined to construct an RTC slot for a peer\n * that appeared in the signalling roster or the keyring. Recorded\n * per-peer in {@link MeshWebRTCAdapter.getPeerStateSnapshot} so a\n * consumer harness observing \"(no slot)\" can tell which gate inside\n * the adapter stopped construction without having to log-correlate\n * through three layers of timing. Polly issue #106 item 7.\n *\n * - `self`: the peerId equals the local peerId.\n * - `not-in-keyring`: the live keyring (or captured Set) does not\n * currently authorise this peer.\n * - `not-present`: the peer is not in the signalling roster. The\n * adapter only dials peers it has heard about through\n * `peers-present` or `peer-joined`; keyring entries that have not\n * appeared on signalling are quietly held without a slot.\n * - `tie-break-other-side`: the lex-tie-break designates the remote\n * peer as the initiator; we wait for their offer.\n * - `slot-already-exists`: a slot exists already, possibly in any\n * negotiation state.\n * - `fatal-error`: an exception was thrown while attempting to build\n * the slot. The accompanying {@link SlotInitiationDecision.error}\n * string carries the message. This is also stamped when the slot\n * watchdog tears a wedged slot down (polly#109's silent throw, or\n * polly#110's idle-but-still-`connected` post-mortem), so the next\n * sweep tick finds no slot and retries.\n */\nexport type SlotInitiationRejectionReason =\n | \"self\"\n | \"not-in-keyring\"\n | \"not-present\"\n | \"tie-break-other-side\"\n | \"slot-already-exists\"\n | \"fatal-error\";\n\n/** Most-recent slot-initiation decision for a peer. Computed at\n * snapshot time so the view always reflects the current state of the\n * relevant gates; a `decision === \"accepted\"` value paired with a\n * `slot === undefined` view on the same snapshot is the load-bearing\n * signal for \"the adapter wants to dial but isn't\" — the failure\n * shape polly#106 documents. */\nexport interface SlotInitiationDecision {\n /** \"accepted\" means every gate in `shouldInitiateTo` would pass right\n * now and a sweep tick would construct a slot. \"rejected\" means at\n * least one gate failed; the `reason` names it. */\n decision: \"accepted\" | \"rejected\";\n /** Populated only on `rejected` decisions. */\n reason: SlotInitiationRejectionReason | undefined;\n /** If the previous sweep tick caught a synchronous throw while\n * building this peer's slot, the message is preserved here for the\n * next snapshot. The reason will be `fatal-error`. */\n error: string | undefined;\n /** `performance.now()` at the time the decision was computed. */\n at: number;\n}\n\n/** Most-recent sync-handshake timeline for a peer slot. Each timestamp\n * is `performance.now()` of the corresponding event the first time it\n * fired on the current slot — slots that get evicted and rebuilt start\n * over. The four fields together describe whether the adapter and the\n * receiver downstream of it have done their part in initiating sync:\n *\n * - `dataChannelOpenedAt`: when the wire is ready to carry bytes.\n * - `peerCandidateEmittedAt`: when polly emitted `peer-candidate`\n * upward; Automerge's network subsystem hooks this event to add the\n * peer to its known set and kick off the per-document sync exchange.\n * If this is `undefined` long after the data channel has opened,\n * polly never signalled \"ready\" to Automerge — that's a failure in\n * the adapter.\n * - `firstOutboundSendAt`: when polly first dispatched bytes through\n * {@link MeshWebRTCAdapter.send} for this peer. If\n * `peerCandidateEmittedAt` is set but this is still `undefined`,\n * Automerge's NetworkSubsystem has not asked the adapter to send\n * anything. The polly#106 ladder named \"no handle locally\" as the\n * typical cause for this rung; the polly#107 failing-shape evidence\n * (fourteen `$meshState` handles pre-warmed in `ready` state,\n * `firstOutboundSendAt` still undefined long after peer-candidate\n * fires) shows that's a misleading ladder entry. Revised:\n *\n * - If `repo.handles[docId]` is undefined or in a non-`ready`\n * state for every doc this peer should sync, the consumer\n * fix is real — pre-warm the handles via the documented\n * `$meshState(key, initial)` factory before the slot opens.\n *\n * - If `repo.handles[docId]` is `ready` for every doc AND\n * `getPeerStateSnapshot().peers[…].slot.handles[docId]\n * .announcedToPeer` is `false`, the gate is BETWEEN Automerge's\n * NetworkSubsystem and the adapter — not on the consumer's\n * handle-construction path. This is the polly#107 surface;\n * post-#107 the mesh client hooks `peer-candidate` to invoke\n * the synchronizer's `reevaluateDocumentShare` so the\n * `addPeer`/`addDocument` ordering race that leaves a handle\n * un-announced gets closed by an idempotent re-evaluation.\n *\n * - If `announcedToPeer` becomes `true` for every relevant doc\n * and `firstOutboundSendAt` is still undefined, the gap is in\n * polly's own send path — that's a polly bug, not an\n * Automerge or consumer one.\n *\n * - `firstInboundMessageAt`: when polly first emitted a `message` event\n * for this peer. If `peerCandidateEmittedAt` is set on the remote and\n * `firstOutboundSendAt` is set on the remote but this is `undefined`\n * locally, bytes were sent across the wire but never decoded — that\n * points at the crypto envelope or the wire fragmenter.\n *\n * Polly issue #106 item 7; polly#107 revised the\n * `firstOutboundSendAt` rung to point at the polly⇄Automerge gate\n * rather than the consumer. */\nexport interface SyncHandshakeAttemptSnapshot {\n dataChannelOpenedAt: number | undefined;\n peerCandidateEmittedAt: number | undefined;\n firstOutboundSendAt: number | undefined;\n firstInboundMessageAt: number | undefined;\n}\n\n/** Sweep-loop observability for the periodic dial re-evaluation. The\n * sweep is what catches peers that were not in the keyring at the time\n * of their `peer-joined` notification (polly#103) AND peers whose\n * previous slot failed and got evicted (polly#106). Exposing its tick\n * counter lets a consumer distinguish \"sweep is running but\n * `shouldInitiateTo` rejected the peer\" from \"sweep is broken and\n * never fires\" without instrumenting polly internals. */\nexport interface SweepSnapshot {\n /** True iff the sweep timer is currently scheduled — false on the\n * captured-set fallback path (no keyringSource) or when the interval\n * is configured to 0. */\n enabled: boolean;\n /** Configured interval in milliseconds. 0 means disabled. */\n intervalMs: number;\n /** How many times the sweep callback has fired since `connect()`. */\n runCount: number;\n /** `performance.now()` at the last tick. `undefined` until the\n * first tick fires. */\n lastRunAt: number | undefined;\n}\n\n/** Per-peer view of an in-flight initial sync. Populated by\n * {@link MeshWebRTCAdapter.handleSyncFragment} as fragments of a\n * single reassembly arrive, and reset to `undefined` once the\n * reassembled message has been dispatched. Exposed verbatim through\n * {@link MeshWebRTCAdapter.getPeerStateSnapshot} so a consumer\n * harness can observe progress mid-stream. Polly issue #104 item 7. */\nexport interface InFlightSyncSnapshot {\n /** Fragments received for the current reassembly. Cleared once\n * reassembly completes. */\n chunksReceived: number;\n /** Bytes received across the fragments of the current reassembly.\n * The reassembled message will be slightly smaller than this sum\n * because each fragment carries a small header. */\n bytesReceived: number;\n /** `performance.now()` value at the last fragment arrival. */\n lastChunkAt: number;\n /** Count of reassembled messages whose dispatch has been\n * scheduled but not yet run. With the receiver-side `setTimeout(0)`\n * yield enabled, this is normally 0 or 1; with the yield\n * disabled (the falsification path) dispatch runs synchronously\n * and this stays 0. */\n applyBacklog: number;\n}\n\n/** Per-handle per-peer sync bookkeeping. Closes the diagnostic gap\n * between \"handle exists in repo\" (observable today via `repo.handles`)\n * and \"Automerge's NetworkSubsystem has actually told this peer about\n * this handle\" — the latter being the load-bearing question for the\n * polly#107 failing shape, where fourteen pre-warmed handles sit in\n * `ready` state and the peer slot is fully connected, yet Automerge\n * has not asked the adapter to send a single sync message.\n *\n * Each field is stamped from the adapter's own wire path: `out` from\n * {@link MeshWebRTCAdapter.send}, `in` from {@link dispatchMessage}'s\n * deserialised view. Combined with the consumer's view of\n * `repo.handles[documentId].state`, the mesh client's\n * {@link createMeshClient}-returned `getPeerStateSnapshot` produces the\n * full per-handle observability the polly#107 ticket asks for in\n * item 7. */\nexport interface HandleSyncSnapshot {\n /** `performance.now()` of the most recent send through\n * {@link MeshWebRTCAdapter.send} carrying this `documentId` for this\n * peer. `undefined` means Automerge has never asked the adapter to\n * send a sync message for this document — i.e. the handle is NOT\n * \"announced to peer\" in the polly#107 sense, regardless of whether\n * the handle is `ready` in `repo.handles`. */\n lastSyncMessageOutAt: number | undefined;\n /** `performance.now()` of the most recent inbound message dispatched\n * upward for this `documentId` from this peer. `undefined` means we\n * have never received a sync message for this document from this\n * peer — they have not announced their copy of this handle to us. */\n lastSyncMessageInAt: number | undefined;\n /** Byte length of the most recent outbound sync message (the raw\n * `RTCDataChannel.send` payload, which is the crypto-wrapped envelope\n * — not the inner Automerge sync size). `undefined` until the first\n * outbound send. Used by the polly#107 example's wire-level\n * transcript to distinguish \"Automerge generated an empty sync\n * message\" from \"Automerge generated nothing at all\". */\n lastSyncMessageOutSize: number | undefined;\n /** Type field of the most recent outbound message for this document\n * to this peer. Typically `\"sync\"` once handshake has begun, or\n * `\"request\"` while the local side is still asking. `undefined`\n * until the first outbound send. */\n lastSyncMessageOutType: string | undefined;\n}\n\n/** Initial state for a {@link HandleSyncSnapshot}. */\nfunction emptyHandleSyncSnapshot(): HandleSyncSnapshot {\n return {\n lastSyncMessageOutAt: undefined,\n lastSyncMessageInAt: undefined,\n lastSyncMessageOutSize: undefined,\n lastSyncMessageOutType: undefined,\n };\n}\n\n/** State tracked for each remote peer. */\ninterface PeerSlot {\n connection: RTCPeerConnection;\n channel: RTCDataChannel | undefined;\n /** Outgoing messages queued while the channel is still connecting.\n * Typed as ArrayBuffer-backed so they are directly usable by\n * RTCDataChannel.send under TypeScript's strict buffer-source typing. */\n pendingSends: Uint8Array<ArrayBuffer>[];\n /** Partially-assembled inbound sync messages, keyed by the fragment id\n * stamped on each chunk. Entries are deleted as soon as the last\n * fragment for an id arrives and the reassembled bytes are dispatched. */\n pendingFragments: Map<string, { chunks: Map<number, Uint8Array>; total: number }>;\n /** Remote ICE candidates that arrived before `setRemoteDescription`\n * completed. addIceCandidate throws when `remoteDescription` is null,\n * and real Chrome 148+ does not internally queue these the way Chrome\n * for Testing does — so any candidate that wins the race against the\n * answer SDP would be silently dropped, leaving ICE checking with no\n * remote candidates to pair against. Drained from {@link handleAnswer}\n * and the post-`setRemoteDescription` step of {@link handleOffer}. */\n pendingRemoteIce: RTCIceCandidateInit[];\n /** Observable snapshot of any reassembly currently in progress for\n * this peer. `undefined` whenever the receive side is idle. */\n inFlightSync: InFlightSyncSnapshot | undefined;\n /** Last refreshed transport-level summary. `undefined` until the\n * caller invokes {@link MeshWebRTCAdapter.refreshTransportStats}\n * (or its idle equivalent on the mesh client). */\n transport: TransportSnapshot | undefined;\n /** Sticky cache of the most recent error reported by the data\n * channel's `onerror` event. Captured on the channel itself so the\n * value survives transient slot replacements within a single\n * connection's lifetime. */\n lastDataChannelError: string | undefined;\n /** Per-slot sync-handshake observability. Each field is populated\n * the first time its event fires on the current slot. Polly issue\n * #106 item 7. */\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n /** Per-handle sync bookkeeping for this peer. Keyed by\n * `documentId`. Entries are created on demand — the first time\n * {@link MeshWebRTCAdapter.send} dispatches a message carrying a\n * given documentId to this peer, or the first time a message for\n * that documentId is dispatched inbound. Polly issue #107 item 7 —\n * closes the \"handle exists in repo but Automerge never announced\n * it\" diagnostic gap. */\n handles: Map<string, HandleSyncSnapshot>;\n /** `performance.now()` at slot construction. The watchdog uses this\n * to detect slots whose `connectionState` never advances past\n * `new`/`connecting` — the polly#109 silent-throw shape — by\n * comparing against {@link SLOT_NEVER_CONNECTED_TIMEOUT_MS}. */\n createdAt: number;\n /** `performance.now()` at the most recent inbound `message` event\n * on this slot's data channel. `undefined` until the first inbound\n * frame arrives. The watchdog uses this to detect connected slots\n * whose remote process died without an OS-layer FIN — the polly#110\n * shape — by comparing against {@link SLOT_IDLE_TIMEOUT_MS} once\n * the channel has opened. */\n lastInboundAt: number | undefined;\n}\n\n/**\n * Automerge-Repo NetworkAdapter backed by real WebRTC data channels.\n * Manages one RTCPeerConnection per remote peer and uses a supplied\n * {@link MeshSignalingClient} for SDP/ICE exchange.\n */\nexport class MeshWebRTCAdapter extends NetworkAdapter {\n readonly signaling: MeshSignalingClient;\n readonly iceServers: RTCIceServer[];\n readonly iceTransportPolicy: RTCIceTransportPolicy | undefined;\n private readonly iceRelayEnforcement: boolean;\n readonly dataChannelLabel: string;\n /** Peers this adapter is willing to dial. Mutable so callers that pair\n * a new device after construction (e.g. a CLI `add-device` process whose\n * mesh client is long-lived across the pair ceremony) can register the\n * new peer with {@link addKnownPeer} without restarting the client.\n *\n * Used only in the captured-set fallback path — when no\n * {@link keyringSource} is supplied. With `keyringSource` set, the\n * authoritative source is the live keyring and this Set is unused. */\n private readonly knownPeers: Set<string>;\n /** Live keyring source, or undefined when the adapter is operating in\n * the captured-set fallback shape. See the JSDoc on\n * {@link MeshWebRTCAdapterOptions.keyringSource}. */\n private readonly keyringSource: (() => MeshKeyring) | undefined;\n /** Interval handle for the periodic re-sweep that catches peers\n * already in the signalling roster when the keyring grew. Cleared in\n * {@link MeshWebRTCAdapter.disconnect}. */\n private knownPeersRefreshTimer: ReturnType<typeof setInterval> | undefined;\n /** Sweep interval. Resolved from\n * {@link MeshWebRTCAdapterOptions.knownPeersRefreshIntervalMs} at\n * construction. Defaults to 2000ms; tests override to 100–250ms. */\n private readonly knownPeersRefreshIntervalMs: number;\n /** When `true`, the sender side awaits between batches of fragment\n * sends and the receiver side schedules dispatch via `setTimeout(0)`\n * so the JS event loop can drain timers (including the consumer's\n * own setInterval-based liveness probes) between large-message\n * apply calls. Defaults to `true`; set to `false` only by the\n * `POLLY_104_DISABLE_FIX` falsification path. */\n private readonly syncYieldEnabled: boolean;\n /** Resolved chunk size for fragmenting oversized messages.\n * Defaults to {@link SYNC_FRAGMENT_CHUNK_SIZE}; can be overridden\n * via {@link MeshWebRTCAdapterOptions.syncFragmentChunkSizeOverride}. */\n private readonly syncFragmentChunkSize: number;\n /** Peers currently visible in the signalling roster — populated by\n * {@link handlePeersPresent} / {@link handlePeerJoined} and pruned by\n * {@link handlePeerLeft}. Read by {@link addKnownPeer} to decide\n * whether the new peer is already online and an offer can fire now. */\n private readonly presentPeers = new Set<string>();\n /** Local peer id captured at construction time. The base\n * `NetworkAdapter.peerId` is only populated when `connect()` fires,\n * which means glare-resolution and peer-discovery dispatch would\n * otherwise have no id to compare against before the first incoming\n * message. Keeping a private mirror keeps those code paths honest\n * without depending on Automerge's lifecycle. */\n private readonly localPeerId: string;\n private readonly RTCPeerConnectionCtor: typeof RTCPeerConnection;\n private readonly slots = new Map<string, PeerSlot>();\n private ready = false;\n private readyResolver: (() => void) | undefined;\n /** Sticky cache of the most recent {@link SlotInitiationDecision}\n * per peer. Updated on every `shouldInitiateTo` call and on every\n * caught throw inside the sweep loop, so a snapshot taken at any\n * moment can answer \"why is there no slot for this peer right\n * now?\". Polly issue #106 item 7. */\n private readonly lastSlotInitiationDecisions = new Map<string, SlotInitiationDecision>();\n /** Tick count of the periodic sweep — incremented inside the\n * `setInterval` callback, exposed via {@link getPeerStateSnapshot}.\n * Lets a consumer rule out \"sweep is dead\" before chasing the\n * shouldInitiateTo gates. */\n private sweepRunCount = 0;\n /** `performance.now()` at the last sweep tick. Paired with\n * {@link sweepRunCount} so a stalled sweep is visible at a glance\n * via the snapshot's `sweep` block. */\n private lastSweepAt: number | undefined;\n /** Watchdog interval (polly#109 + polly#110). Tears down slots that\n * never reach `connected`, or that look connected but have not\n * received bytes for {@link slotIdleTimeoutMs}, so the recovery\n * sweep can re-attempt. Cleared in {@link disconnect}. */\n private slotWatchdogTimer: ReturnType<typeof setInterval> | undefined;\n private readonly slotNeverConnectedTimeoutMs: number;\n private readonly slotIdleTimeoutMs: number;\n private readonly slotWatchdogIntervalMs: number;\n\n /** The peers this adapter will dial. Backward-compatible read accessor\n * for callers that previously iterated the `knownPeerIds` array. With\n * a {@link MeshWebRTCAdapterOptions.keyringSource} configured, the\n * value is read live from the keyring; otherwise it falls back to the\n * captured Set populated through the constructor and\n * {@link addKnownPeer}. */\n get knownPeerIds(): string[] {\n if (this.keyringSource !== undefined) {\n return [...this.keyringSource().knownPeers.keys()].filter((id) => id !== this.localPeerId);\n }\n return [...this.knownPeers];\n }\n\n /** True iff `remotePeerId` is currently in the authoritative\n * knownPeers source — the live keyring when one was supplied, or\n * the captured Set otherwise. Centralises the membership check so\n * {@link shouldInitiateTo} and the JSDoc on\n * {@link MeshWebRTCAdapterOptions.keyringSource} agree on the rule. */\n private hasKnownPeer(remotePeerId: string): boolean {\n if (this.keyringSource !== undefined) {\n return this.keyringSource().knownPeers.has(remotePeerId);\n }\n return this.knownPeers.has(remotePeerId);\n }\n\n /** Callback for incoming blob messages. Set by the blob store.\n * Called with the sender's peer ID, the raw header object, and the\n * binary payload (chunk data). */\n onBlobMessage?: (peerId: string, header: Record<string, unknown>, data: Uint8Array) => void;\n\n constructor(options: MeshWebRTCAdapterOptions) {\n super();\n this.signaling = options.signaling;\n this.iceServers = options.iceServers ?? DEFAULT_ICE_SERVERS;\n this.iceTransportPolicy = options.iceTransportPolicy;\n this.iceRelayEnforcement = options.iceRelayEnforcement ?? true;\n this.dataChannelLabel = options.dataChannelLabel ?? \"polly-mesh\";\n this.knownPeers = new Set(options.knownPeerIds ?? []);\n this.keyringSource = options.keyringSource;\n this.knownPeersRefreshIntervalMs = options.knownPeersRefreshIntervalMs ?? 2000;\n this.syncYieldEnabled = options.syncYieldEnabled ?? true;\n this.syncFragmentChunkSize = options.syncFragmentChunkSizeOverride ?? SYNC_FRAGMENT_CHUNK_SIZE;\n this.slotNeverConnectedTimeoutMs =\n options.slotNeverConnectedTimeoutMs ?? SLOT_NEVER_CONNECTED_TIMEOUT_MS;\n this.slotIdleTimeoutMs = options.slotIdleTimeoutMs ?? SLOT_IDLE_TIMEOUT_MS;\n this.slotWatchdogIntervalMs = options.slotWatchdogIntervalMs ?? SLOT_WATCHDOG_INTERVAL_MS;\n this.localPeerId = options.peerId;\n const PC = options.RTCPeerConnection ?? globalThis.RTCPeerConnection;\n if (typeof PC !== \"function\") {\n throw new Error(\n \"MeshWebRTCAdapter: no RTCPeerConnection implementation found. Pass one via options.RTCPeerConnection (e.g. from `werift` or `@roamhq/wrtc`), or run in a browser where `globalThis.RTCPeerConnection` exists.\"\n );\n }\n this.RTCPeerConnectionCtor = PC;\n }\n\n isReady(): boolean {\n return this.ready;\n }\n\n /** The current number of peer slots the adapter is tracking. Each\n * slot is one ordered pair (local peer ↔ remote peer) with its own\n * RTCPeerConnection and data channel. Exposed for tests that assert\n * \"exactly one channel per pair\" after discovery settles. */\n peerSlotCount(): number {\n return this.slots.size;\n }\n\n /** Snapshot of the adapter's per-peer state at the moment of the\n * call. Returned values are plain data — strings and booleans only —\n * so consumers can log, assert on, or render them without retaining\n * references into the adapter's internals.\n *\n * Polly issue #103 asked for an inspection surface so a consumer\n * harness can answer \"is the mesh layer in a known good state\" from\n * outside polly. This method is that surface. The fields mirror the\n * three layers of the WebRTC connection lifecycle so a \"stuck\"\n * connection can be diagnosed without reaching for the browser\n * devtools: the SDP signalling state, the ICE checking state, and\n * the unified RTCPeerConnection connection state. A `dataChannel`\n * field reports whether the data channel — the thing the mesh\n * actually carries bytes over — is open.\n *\n * Peers visible in the signalling roster but not yet dialled appear\n * with `slot: undefined`, so a consumer can tell \"we know about this\n * peer in signalling but the WebRTC adapter has not started an\n * offer yet\" from \"we have a slot in some negotiation state\". */\n getPeerStateSnapshot(): {\n localPeerId: string;\n knownPeerIds: string[];\n presentPeerIds: string[];\n sweep: SweepSnapshot;\n peers: Array<{\n peerId: string;\n knownInKeyring: boolean;\n presentInSignalling: boolean;\n slotInitiationRejectionReason: SlotInitiationRejectionReason | undefined;\n slotInitiationDecision: SlotInitiationDecision;\n slot:\n | undefined\n | {\n signalingState: string;\n iceConnectionState: string;\n connectionState: string;\n dataChannelState: string;\n pendingSendCount: number;\n pendingRemoteIceCount: number;\n inFlightSync: InFlightSyncSnapshot | undefined;\n transport: TransportSnapshot | undefined;\n lastSyncHandshakeAttempt: SyncHandshakeAttemptSnapshot;\n handles: Record<string, HandleSyncSnapshot>;\n createdAt: number;\n lastInboundAt: number | undefined;\n };\n }>;\n } {\n const knownPeerIds = this.knownPeerIds;\n const presentPeerIds = [...this.presentPeers];\n const knownPeerSet = new Set(knownPeerIds);\n const allPeers = new Set<string>();\n for (const id of knownPeerIds) allPeers.add(id);\n for (const id of presentPeerIds) allPeers.add(id);\n for (const id of this.slots.keys()) allPeers.add(id);\n const peers: ReturnType<MeshWebRTCAdapter[\"getPeerStateSnapshot\"]>[\"peers\"] = [];\n for (const peerId of allPeers) {\n const slot = this.slots.get(peerId);\n const decision = this.snapshotInitiationDecision(peerId);\n peers.push({\n peerId,\n knownInKeyring: knownPeerSet.has(peerId),\n presentInSignalling: this.presentPeers.has(peerId),\n // Top-level convenience: same value as\n // `slotInitiationDecision.reason`, hoisted so log lines that\n // only need the named reason can read it without a nested\n // access. Polly issue #106 item 7.\n slotInitiationRejectionReason: decision.reason,\n slotInitiationDecision: decision,\n slot: slot ? serialiseSlotView(slot) : undefined,\n });\n }\n return {\n localPeerId: this.localPeerId,\n knownPeerIds,\n presentPeerIds,\n sweep: {\n enabled: this.knownPeersRefreshTimer !== undefined,\n intervalMs: this.knownPeersRefreshIntervalMs,\n runCount: this.sweepRunCount,\n lastRunAt: this.lastSweepAt,\n },\n peers,\n };\n }\n\n /** Handle the signalling server's `peer-joined` notification: a new\n * peer has appeared on the relay. If the peer is in `knownPeerIds`\n * and we do not already have a slot for it, and the tie-break rule\n * designates us as the initiator (our peerId compares greater than\n * theirs), open an initiating slot and fire the SDP offer. Otherwise\n * do nothing — either we are not interested in this peer, we are\n * already connected, or the other side is the one expected to\n * initiate. */\n handlePeerJoined(remotePeerId: string): void {\n this.presentPeers.add(remotePeerId);\n if (!this.shouldInitiateTo(remotePeerId)) return;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n\n /** Handle the signalling server's `peers-present` notification sent\n * once to a newcomer. Applies the same filter as handlePeerJoined to\n * every listed peer, so a device joining into an established lobby\n * dials every knownPeer it is meant to initiate to in one pass. */\n handlePeersPresent(peerIds: string[]): void {\n for (const remotePeerId of peerIds) {\n this.presentPeers.add(remotePeerId);\n if (!this.shouldInitiateTo(remotePeerId)) continue;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n }\n\n /** Construct an initiating slot inside a per-peer try/catch and\n * record any throw as a `fatal-error` rejection on the per-peer\n * decision map so the snapshot surface names it. Every dial entry\n * point ({@link handlePeerJoined}, {@link handlePeersPresent},\n * {@link addKnownPeer}, {@link refreshKnownPeers}) routes through\n * here so a single peer's broken construction can never take down a\n * batch of peers — pre-#106 a thrown `new RTCPeerConnection()`\n * inside `handlePeersPresent` would skip every later peer in the\n * same batch with no observable trace because the signalling\n * client's frame dispatch swallowed the rejection. */\n private tryCreateInitiatingSlot(remotePeerId: string): void {\n try {\n this.createInitiatingSlot(remotePeerId);\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.lastSlotInitiationDecisions.set(remotePeerId, {\n decision: \"rejected\",\n reason: \"fatal-error\",\n error: message,\n at: performance.now(),\n });\n }\n }\n\n /** Handle the signalling server's `peer-left` notification: a\n * previously joined peer has closed its socket. Evict any slot we\n * hold for that peer so a subsequent `peer-joined` from the same\n * peerId (a reconnect) creates a fresh slot rather than colliding\n * with a stale RTCPeerConnection that WebRTC's own ICE timer has\n * not yet noticed is dead. */\n handlePeerLeft(remotePeerId: string): void {\n this.presentPeers.delete(remotePeerId);\n const slot = this.slots.get(remotePeerId);\n if (!slot) return;\n slot.channel?.close();\n slot.connection.close();\n this.slots.delete(remotePeerId);\n }\n\n /** Register a peer the adapter should dial. Used by post-construction\n * pair-acceptance flows — when a long-lived mesh client (e.g. a CLI\n * `add-device` process or a daemon holding the mesh open) accepts a\n * pair token after start-up, its keyring's `knownPeers` map gains a\n * new entry. Calling this method propagates that into the adapter's\n * own allowlist; if the peer is already in the signalling roster and\n * the tie-break rule names us as the initiator, an SDP offer fires\n * immediately. No-op if the peer is already known.\n *\n * When a {@link MeshWebRTCAdapterOptions.keyringSource} is configured\n * the adapter already reads `knownPeers` live and the periodic sweep\n * picks up the new entry within\n * {@link MeshWebRTCAdapterOptions.knownPeersRefreshIntervalMs} on its\n * own, so explicit calls to this method are not required for\n * correctness — but remain supported for callers that want the\n * \"dial now if present\" prompt without waiting for the next sweep. */\n addKnownPeer(remotePeerId: string): void {\n if (remotePeerId === this.localPeerId) return;\n if (this.keyringSource === undefined) {\n // Captured-set fallback path: keep the legacy mutation behaviour\n // so existing consumers that wire the adapter directly without a\n // keyringSource still work.\n if (this.knownPeers.has(remotePeerId)) return;\n this.knownPeers.add(remotePeerId);\n }\n if (!this.presentPeers.has(remotePeerId)) return;\n if (!this.shouldInitiateTo(remotePeerId)) return;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n\n /** Re-evaluate every peer currently in the signalling roster and\n * dial the ones the keyring authorises that we do not already have\n * a slot for. The periodic sweep started in {@link connect} calls\n * this; consumers can call it manually to skip the wait after they\n * apply a fresh pairing token. Idempotent.\n *\n * A throw from {@link createInitiatingSlot} for one peer must not\n * prevent the sweep from continuing to the next one — pre-#106 a\n * synchronous throw inside `new RTCPeerConnection()` (a real risk\n * once the page has built dozens of connections and Chrome's\n * per-page cap is in play) skipped every later peer in the same\n * sweep, with no observable trace because `setInterval` swallows\n * the rejection silently. {@link tryCreateInitiatingSlot} caches\n * the error onto the snapshot's slotInitiationRejectionReason as\n * `fatal-error` so the failing peer is named instead of disguised\n * as \"(no slot)\". */\n refreshKnownPeers(): void {\n for (const remotePeerId of this.presentPeers) {\n if (!this.shouldInitiateTo(remotePeerId)) continue;\n this.tryCreateInitiatingSlot(remotePeerId);\n }\n }\n\n private shouldInitiateTo(remotePeerId: string): boolean {\n const reason = this.evaluateInitiation(remotePeerId);\n this.lastSlotInitiationDecisions.set(remotePeerId, {\n decision: reason === undefined ? \"accepted\" : \"rejected\",\n reason,\n error: undefined,\n at: performance.now(),\n });\n return reason === undefined;\n }\n\n /** Pure-function form of the gate cascade behind {@link shouldInitiateTo}.\n * Returns `undefined` when every gate passes (the slot would be\n * built); otherwise returns the named reason the dial was declined.\n * Pulling the gates out of the boolean wrapper lets the snapshot\n * surface name *which* gate stopped construction without the caller\n * having to re-implement the check. Polly issue #106 item 7.\n *\n * The \"not-present\" gate is checked here even though the inbound\n * call sites (`handlePeerJoined`, `handlePeersPresent`,\n * `refreshKnownPeers`, `addKnownPeer`) only invoke `shouldInitiateTo`\n * for peers in the signalling roster — so on those paths the gate\n * never fires. It's load-bearing on the snapshot path, where the\n * reason is computed for every peer the caller might ask about\n * (including keyring entries that aren't currently signalling). */\n private evaluateInitiation(remotePeerId: string): SlotInitiationRejectionReason | undefined {\n if (remotePeerId === this.localPeerId) return \"self\";\n if (!this.hasKnownPeer(remotePeerId)) return \"not-in-keyring\";\n if (!this.presentPeers.has(remotePeerId)) return \"not-present\";\n if (this.slots.has(remotePeerId)) return \"slot-already-exists\";\n // Tie-break: the lexicographically higher peer id initiates. This\n // matches the glare-resolution rule in handleOffer, so pre-offer\n // filtering eliminates the glare pathway for the common case where\n // both sides learn of each other at roughly the same moment.\n if (this.localPeerId <= remotePeerId) return \"tie-break-other-side\";\n return undefined;\n }\n\n /** Compute the latest initiation decision for a peer at snapshot\n * time. Prefers the cached decision when a sweep tick fixed the\n * outcome to `fatal-error` (a thrown construction is sticky until\n * the next successful sweep clears it); otherwise re-evaluates the\n * gates against current state. Pure read; never mutates the map. */\n private snapshotInitiationDecision(remotePeerId: string): SlotInitiationDecision {\n const cached = this.lastSlotInitiationDecisions.get(remotePeerId);\n if (cached?.reason === \"fatal-error\") return cached;\n const reason = this.evaluateInitiation(remotePeerId);\n return {\n decision: reason === undefined ? \"accepted\" : \"rejected\",\n reason,\n error: undefined,\n at: performance.now(),\n };\n }\n\n whenReady(): Promise<void> {\n if (this.ready) return Promise.resolve();\n return new Promise((resolve) => {\n this.readyResolver = resolve;\n });\n }\n\n /**\n * Start the adapter. Marks the adapter ready so Automerge's\n * NetworkSubsystem begins routing messages through it. Discovery of\n * peers is driven entirely by the signalling server's\n * `peers-present` and `peer-joined` frames, handed to\n * {@link handlePeersPresent} and {@link handlePeerJoined}. A peer\n * that calls `signaling.connect()` at any point — before or after\n * this method — will either find its targets already in the server's\n * lobby (peers-present) or learn about them as they arrive\n * (peer-joined); either way the adapter only opens one\n * initiating slot per ordered pair.\n */\n connect(peerId: PeerId, peerMetadata?: PeerMetadata): void {\n this.peerId = peerId;\n if (peerMetadata !== undefined) {\n this.peerMetadata = peerMetadata;\n }\n this.ready = true;\n this.readyResolver?.();\n this.startKnownPeersSweep();\n this.startSlotWatchdog();\n }\n\n disconnect(): void {\n this.stopKnownPeersSweep();\n this.stopSlotWatchdog();\n for (const slot of this.slots.values()) {\n slot.channel?.close();\n slot.connection.close();\n }\n this.slots.clear();\n this.signaling.close();\n this.ready = false;\n this.emit(\"close\");\n }\n\n /** Start the periodic re-sweep that catches peers added to the\n * keyring after their `peer-joined` notification has already fired.\n * No-op when no keyringSource was supplied — the captured-set\n * fallback has no live source to re-read, so the sweep would be\n * useless. No-op when the interval is configured to 0.\n *\n * Each tick increments {@link sweepRunCount} and stamps\n * {@link lastSweepAt} *before* dispatching to\n * {@link refreshKnownPeers} so a snapshot can distinguish \"sweep is\n * running but every peer is rejected\" from \"sweep is dead\". An\n * outer try/catch keeps the timer alive even if the per-peer\n * try/catch inside `refreshKnownPeers` somehow leaks. Polly issue\n * #106 item 7. */\n private startKnownPeersSweep(): void {\n if (this.keyringSource === undefined) return;\n if (this.knownPeersRefreshIntervalMs <= 0) return;\n if (this.knownPeersRefreshTimer !== undefined) return;\n this.knownPeersRefreshTimer = setInterval(() => {\n this.sweepRunCount += 1;\n this.lastSweepAt = performance.now();\n try {\n this.refreshKnownPeers();\n } catch {\n // The per-peer try/catch inside refreshKnownPeers should\n // already have caught construction errors. This outer catch\n // is a last-resort belt that keeps the interval alive even\n // if a future change adds a non-per-peer throw site.\n }\n }, this.knownPeersRefreshIntervalMs);\n }\n\n private stopKnownPeersSweep(): void {\n if (this.knownPeersRefreshTimer === undefined) return;\n clearInterval(this.knownPeersRefreshTimer);\n this.knownPeersRefreshTimer = undefined;\n }\n\n /** Close a slot's data channel and connection, remove it from the\n * map, and emit `peer-disconnected` upward so Automerge stops\n * routing through the dead pair. Used by both the polly#109\n * `.catch` in {@link createInitiatingSlot} and the polly#109/#110\n * watchdog in {@link sweepWedgedSlots}. */\n private tearDownWedgedSlot(peerId: string): void {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n // Delete from the map BEFORE closing the connection. The\n // `connection.close()` call below transitions the connection to\n // `closed` synchronously on some implementations, which re-enters\n // `onconnectionstatechange` — and that handler emits its own\n // `peer-disconnected` if it sees the slot still mapped. Removing\n // it first makes the handler's `has(peerId)` check false so we\n // own the single emit.\n this.slots.delete(peerId);\n try {\n slot.channel?.close();\n } catch {\n // werift's data channel `close()` can throw if the underlying\n // connection has already gone away; swallow so a teardown\n // never leaks an exception out of the watchdog loop.\n }\n try {\n slot.connection.close();\n } catch {\n // Same rationale as above.\n }\n this.emit(\"peer-disconnected\", { peerId: peerId as unknown as PeerId });\n }\n\n /** Start the slot watchdog (polly#109 + polly#110). Walks every\n * active slot every {@link slotWatchdogIntervalMs} and tears down\n * the two named wedge shapes:\n *\n * - `connectionState` in `new`/`connecting` for longer than\n * {@link slotNeverConnectedTimeoutMs} (polly#109: silent\n * `createOffer`/`setLocalDescription` rejection, or a network\n * condition under which ICE never gathers).\n *\n * - `connectionState === \"connected\"` AND data channel `open` AND\n * no inbound bytes for {@link slotIdleTimeoutMs} (polly#110:\n * remote process killed without OS-layer FIN — ICE keepalives\n * take many minutes to fail, the slot sends sync traffic into\n * the void until then).\n *\n * Each teardown stamps `fatal-error` on the per-peer decision so\n * the named gate is visible on the next snapshot, then emits\n * `peer-disconnected` so Automerge stops routing through the dead\n * slot. The next sweep tick re-evaluates and the recovery path\n * creates a fresh slot. No-op when configured to 0. */\n private startSlotWatchdog(): void {\n if (this.slotWatchdogIntervalMs <= 0) return;\n if (this.slotWatchdogTimer !== undefined) return;\n this.slotWatchdogTimer = setInterval(() => {\n try {\n this.sweepWedgedSlots();\n } catch {\n // Belt-and-braces: the per-slot loop already swallows\n // teardown errors. A non-per-slot throw should still leave\n // the timer alive.\n }\n }, this.slotWatchdogIntervalMs);\n }\n\n private stopSlotWatchdog(): void {\n if (this.slotWatchdogTimer === undefined) return;\n clearInterval(this.slotWatchdogTimer);\n this.slotWatchdogTimer = undefined;\n }\n\n private sweepWedgedSlots(): void {\n const now = performance.now();\n const peerIds = [...this.slots.keys()];\n for (const peerId of peerIds) {\n const slot = this.slots.get(peerId);\n if (!slot) continue;\n const reason = this.classifyWedgedSlot(slot, now);\n if (!reason) continue;\n this.lastSlotInitiationDecisions.set(peerId, {\n decision: \"rejected\",\n reason: \"fatal-error\",\n error: reason,\n at: now,\n });\n this.tearDownWedgedSlot(peerId);\n }\n }\n\n /** Decide whether a slot is wedged at the moment of inspection,\n * returning the human-readable diagnosis when it is. Pulled out of\n * {@link sweepWedgedSlots} so the per-slot decision stays under\n * biome's cognitive-complexity ceiling and so a future test can\n * exercise the gates directly. */\n private classifyWedgedSlot(slot: PeerSlot, now: number): string | undefined {\n const state = slot.connection.connectionState;\n const ageMs = now - slot.createdAt;\n if (\n this.slotNeverConnectedTimeoutMs > 0 &&\n (state === \"new\" || state === \"connecting\") &&\n ageMs > this.slotNeverConnectedTimeoutMs\n ) {\n return `slot never reached connected (state=${state}) after ${Math.round(ageMs)}ms`;\n }\n if (\n this.slotIdleTimeoutMs > 0 &&\n state === \"connected\" &&\n slot.channel?.readyState === \"open\"\n ) {\n const lastInbound = slot.lastInboundAt ?? slot.createdAt;\n const idleMs = now - lastInbound;\n if (idleMs > this.slotIdleTimeoutMs) {\n return `slot idle: no inbound bytes for ${Math.round(idleMs)}ms (state=connected, dc=open)`;\n }\n }\n return undefined;\n }\n\n /**\n * Send a sync message to a specific remote peer. If no RTCPeerConnection\n * exists yet, the adapter initiates one by producing an SDP offer and\n * sending it through the signalling channel; the outgoing bytes are\n * queued until the data channel is open.\n */\n send(message: Message): void {\n const targetId = message.targetId as unknown as string;\n const bytes = this.serialiseMessage(message);\n let slot = this.slots.get(targetId);\n if (!slot) {\n slot = this.createInitiatingSlot(targetId);\n }\n if (slot.lastSyncHandshakeAttempt.firstOutboundSendAt === undefined) {\n slot.lastSyncHandshakeAttempt.firstOutboundSendAt = performance.now();\n }\n // Per-handle wire-level transcript (polly#107 item 5). Automerge\n // stamps `documentId` on every sync/request/remote-heads/etc.\n // message; messages without a documentId (the synchronizer never\n // emits one without it, but the type union allows for legacy\n // ephemeral envelopes) are skipped here rather than blanketing\n // every peer's handle map with a noise key. `wrap` in\n // {@link MeshNetworkAdapter} now preserves `documentId` in the\n // outer envelope so this stamp survives the crypto layer.\n const documentId = (message as unknown as { documentId?: string }).documentId;\n if (typeof documentId === \"string\") {\n let handleEntry = slot.handles.get(documentId);\n if (!handleEntry) {\n handleEntry = emptyHandleSyncSnapshot();\n slot.handles.set(documentId, handleEntry);\n }\n handleEntry.lastSyncMessageOutAt = performance.now();\n handleEntry.lastSyncMessageOutSize = bytes.length;\n handleEntry.lastSyncMessageOutType =\n typeof message.type === \"string\" ? message.type : undefined;\n }\n if (slot.channel && slot.channel.readyState === \"open\") {\n void this.sendBytesMaybeFragmented(slot.channel, bytes);\n } else {\n slot.pendingSends.push(bytes);\n }\n }\n\n /** Number of consecutive fragment sends after which the sender\n * yields to the macrotask queue when {@link syncYieldEnabled} is on.\n * 8 × 64 KiB = 512 KiB of bytes between yields — small enough that\n * a 5 MB sync produces many yield points (and the JS event loop\n * drains the consumer's `setInterval` liveness probes between\n * them), large enough that the per-yield overhead does not dominate\n * the wire cost. */\n private static readonly SEND_YIELD_EVERY_N_FRAGMENTS = 8;\n\n /** Send raw wire bytes, fragmenting if they exceed the SCTP maxMessageSize\n * cap. The default RTCDataChannel limit is 256 KiB in current Chrome and\n * werift; oversized sends either throw, drop silently, or stall the\n * channel, none of which surface as an error to the caller. Fragments\n * use the same length-prefixed JSON header wire format as ordinary\n * messages but carry a `sync-fragment` type that the receive path\n * detects and reassembles before deserialising.\n *\n * When {@link MeshWebRTCAdapterOptions.syncYieldEnabled} is true (the\n * default), the loop awaits the macrotask queue every\n * {@link SEND_YIELD_EVERY_N_FRAGMENTS} fragments so the JS event\n * loop drains between batches — without this, a 5–8 MB initial\n * sync produces 78–125 back-to-back `RTCDataChannel.send` calls in\n * a tight loop, starving anything else on the main thread (polly\n * issue #104, sender side). When the option is false the legacy\n * tight-loop shape is preserved for the falsification path. */\n private async sendBytesMaybeFragmented(\n channel: RTCDataChannel,\n bytes: Uint8Array<ArrayBuffer>\n ): Promise<void> {\n if (bytes.length <= SYNC_FRAGMENT_THRESHOLD) {\n channel.send(bytes);\n return;\n }\n const id = crypto.randomUUID();\n const fragments = chunkSyncMessage(bytes, id, this.syncFragmentChunkSize);\n if (!this.syncYieldEnabled) {\n for (const fragment of fragments) {\n channel.send(fragment);\n }\n return;\n }\n let i = 0;\n for (const fragment of fragments) {\n channel.send(fragment);\n i += 1;\n if (i % MeshWebRTCAdapter.SEND_YIELD_EVERY_N_FRAGMENTS === 0 && i < fragments.length) {\n await new Promise<void>((resolve) => {\n setTimeout(resolve, 0);\n });\n }\n }\n }\n\n /**\n * Entry point the signalling client calls when it receives a signal\n * from a remote peer. Dispatches on the payload `kind` to either\n * accept an offer (building an answer), apply an answer, or add an\n * ICE candidate. Exposed publicly so the caller that constructs the\n * signalling client can wire the onSignal callback directly to this\n * method.\n */\n handleSignal(fromPeerId: string, rawPayload: unknown): void {\n const payload = rawPayload as unknown as SignalingPayload;\n if (!payload || typeof payload !== \"object\" || !(\"kind\" in payload)) {\n return;\n }\n switch (payload.kind) {\n case \"offer\":\n void this.handleOffer(fromPeerId, payload.sdp);\n return;\n case \"answer\":\n void this.handleAnswer(fromPeerId, payload.sdp);\n return;\n case \"ice\":\n void this.handleIceCandidate(fromPeerId, payload.candidate);\n return;\n }\n }\n\n /** Assemble the {@link RTCConfiguration} every new\n * {@link RTCPeerConnection} is built with. Centralised so every slot\n * (initiator and answerer) honours the same iceTransportPolicy. */\n private buildRtcConfiguration(): RTCConfiguration {\n const config: RTCConfiguration = { iceServers: this.iceServers };\n if (this.iceTransportPolicy !== undefined) {\n config.iceTransportPolicy = this.iceTransportPolicy;\n }\n return config;\n }\n\n /** Decide whether a local ICE candidate should be relayed through\n * the signalling channel to the remote peer. Chrome's iceTransport\n * Policy = \"relay\" implementation filters non-relay candidates at\n * the source so the remote peer never sees them; werift's only\n * filters its own outgoing connectivity checks and still emits host\n * and srflx candidates upstream, so a remote peer with policy \"all\"\n * can pair against them — making relay-only enforcement leaky in\n * the mixed-implementation case the polly#105 falsification harness\n * exposes. Mirroring Chrome's filter at this layer gives a uniform\n * contract on every RTCPeerConnection implementation polly supports.\n *\n * The candidate `type` field is the SDP-spec form; we additionally\n * parse it out of the legacy `candidate` string for implementations\n * that don't surface the typed field directly (werift exposes the\n * SDP string only). */\n private isRelayCandidateInit(init: RTCIceCandidateInit): boolean {\n const candidateStr = init.candidate ?? \"\";\n const m = candidateStr.match(/\\btyp\\s+(\\S+)/i);\n return m?.[1]?.toLowerCase() === \"relay\";\n }\n\n private shouldSendCandidate(candidate: RTCIceCandidate): boolean {\n if (!this.iceRelayEnforcement) return true;\n if (this.iceTransportPolicy !== \"relay\") return true;\n const typed = (candidate as unknown as { type?: string }).type;\n const match = candidate.candidate.match(/\\btyp\\s+(\\S+)/i);\n const candidateType = (typed ?? match?.[1] ?? \"\").toLowerCase();\n return candidateType === \"relay\";\n }\n\n /** Strip non-relay `a=candidate:` lines from an SDP when\n * iceTransportPolicy is `\"relay\"`. Some RTCPeerConnection\n * implementations (werift, notably) embed every gathered candidate\n * in the SDP regardless of policy, and a remote peer parsing those\n * via `setRemoteDescription` will pair against them — bypassing the\n * relay-only contract. Chrome already filters in-SDP candidates by\n * policy, so this is only load-bearing for werift consumers, but\n * the SDP rewrite is implementation-agnostic and idempotent. */\n private filterSdpCandidatesByPolicy(sdp: string | undefined): string | undefined {\n if (!this.iceRelayEnforcement) return sdp;\n if (this.iceTransportPolicy !== \"relay\") return sdp;\n if (!sdp) return sdp;\n const lines = sdp.split(/\\r?\\n/);\n const filtered: string[] = [];\n for (const line of lines) {\n if (!line.startsWith(\"a=candidate:\")) {\n filtered.push(line);\n continue;\n }\n const m = line.match(/\\btyp\\s+(\\S+)/i);\n if (m?.[1]?.toLowerCase() === \"relay\") filtered.push(line);\n }\n return filtered.join(\"\\r\\n\");\n }\n\n /** Apply {@link filterSdpCandidatesByPolicy} to an SDP\n * description ahead of `setLocalDescription` (filter outgoing) or\n * `setRemoteDescription` (filter incoming). The defensive\n * receive-side filter exists because we cannot trust the remote\n * peer's adapter to have stripped its non-relay candidates first —\n * polly might be talking to a pre-#105 polly, or to a non-polly\n * peer whose policy enforcement is different. */\n private applySdpPolicyFilter(description: RTCSessionDescriptionInit): RTCSessionDescriptionInit {\n if (!this.iceRelayEnforcement) return description;\n if (this.iceTransportPolicy !== \"relay\") return description;\n const filtered = this.filterSdpCandidatesByPolicy(description.sdp);\n if (filtered === description.sdp) return description;\n return { ...description, sdp: filtered };\n }\n\n private createInitiatingSlot(targetId: string): PeerSlot {\n const connection = new this.RTCPeerConnectionCtor(this.buildRtcConfiguration());\n const channel = connection.createDataChannel(this.dataChannelLabel, { ordered: true });\n const slot: PeerSlot = {\n connection,\n channel,\n pendingSends: [],\n pendingFragments: new Map(),\n pendingRemoteIce: [],\n inFlightSync: undefined,\n transport: undefined,\n lastDataChannelError: undefined,\n lastSyncHandshakeAttempt: emptySyncHandshakeAttempt(),\n handles: new Map(),\n createdAt: performance.now(),\n lastInboundAt: undefined,\n };\n this.slots.set(targetId, slot);\n this.wireConnection(targetId, connection);\n this.wireDataChannel(targetId, channel);\n // Pre-#109: `initiateOffer` was fire-and-forget — a rejection in\n // `createOffer` or `setLocalDescription` was dropped on the floor,\n // leaving the slot at `signalingState=stable`, ICE never started\n // gathering, and the recovery sweep hit `slot-already-exists`\n // forever. The `.catch` here surfaces the failure on the snapshot\n // surface and removes the wedged slot so the next sweep retries.\n this.initiateOffer(targetId, connection).catch((err) => {\n const message = err instanceof Error ? err.message : String(err);\n this.lastSlotInitiationDecisions.set(targetId, {\n decision: \"rejected\",\n reason: \"fatal-error\",\n error: message,\n at: performance.now(),\n });\n this.tearDownWedgedSlot(targetId);\n });\n return slot;\n }\n\n private async initiateOffer(targetId: string, connection: RTCPeerConnection): Promise<void> {\n const offer = await connection.createOffer();\n await connection.setLocalDescription(offer);\n const localOffer = connection.localDescription ?? offer;\n const sdpToSend = this.applySdpPolicyFilter({\n type: localOffer.type,\n sdp: localOffer.sdp ?? offer.sdp,\n });\n this.signaling.sendSignal(targetId, {\n kind: \"offer\",\n sdp: sdpToSend,\n } satisfies SignalingPayload);\n }\n\n private async handleOffer(fromPeerId: string, sdp: RTCSessionDescriptionInit): Promise<void> {\n const existing = this.slots.get(fromPeerId);\n if (existing) {\n // Glare: we already initiated a connection to this peer. Resolve by\n // peer-id ordering: the lexicographically lower id yields its own\n // offer and accepts the incoming one. The higher id ignores the\n // incoming offer and waits for the answer to its own.\n const localId = this.peerId as unknown as string;\n if (localId > fromPeerId) {\n return;\n }\n existing.channel?.close();\n existing.connection.close();\n this.slots.delete(fromPeerId);\n }\n\n const connection = new this.RTCPeerConnectionCtor(this.buildRtcConfiguration());\n const slot: PeerSlot = {\n connection,\n channel: undefined,\n pendingSends: [],\n pendingFragments: new Map(),\n pendingRemoteIce: [],\n inFlightSync: undefined,\n transport: undefined,\n lastDataChannelError: undefined,\n lastSyncHandshakeAttempt: emptySyncHandshakeAttempt(),\n handles: new Map(),\n createdAt: performance.now(),\n lastInboundAt: undefined,\n };\n this.slots.set(fromPeerId, slot);\n this.wireConnection(fromPeerId, connection);\n connection.ondatachannel = (event) => {\n slot.channel = event.channel;\n this.wireDataChannel(fromPeerId, event.channel);\n };\n await connection.setRemoteDescription(this.applySdpPolicyFilter(sdp));\n await this.flushPendingRemoteIce(slot);\n const answer = await connection.createAnswer();\n await connection.setLocalDescription(answer);\n const localAnswer = connection.localDescription ?? answer;\n const sdpToSend = this.applySdpPolicyFilter({\n type: localAnswer.type,\n sdp: localAnswer.sdp ?? answer.sdp,\n });\n this.signaling.sendSignal(fromPeerId, {\n kind: \"answer\",\n sdp: sdpToSend,\n } satisfies SignalingPayload);\n }\n\n private async handleAnswer(fromPeerId: string, sdp: RTCSessionDescriptionInit): Promise<void> {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n // Drop answers that arrive after the connection has already settled\n // — the signalling relay can echo a peer's frames more than once\n // under reconnect or reload, and a duplicate answer in the `stable`\n // state otherwise throws `InvalidStateError` and kills the slot. We\n // only ever expect an answer while we still have a local offer\n // pending; anything else is benign noise.\n if (slot.connection.signalingState !== \"have-local-offer\") return;\n await slot.connection.setRemoteDescription(this.applySdpPolicyFilter(sdp));\n await this.flushPendingRemoteIce(slot);\n }\n\n private async handleIceCandidate(\n fromPeerId: string,\n candidate: RTCIceCandidateInit\n ): Promise<void> {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n // Receive-side mirror of {@link shouldSendCandidate}: even with a\n // perfectly behaved peer we cannot assume the SDP and trickle\n // streams agree on policy enforcement, and a non-polly remote may\n // not filter at all. Drop anything that isn't relay-typed when\n // the local policy is `\"relay\"`.\n if (\n this.iceRelayEnforcement &&\n this.iceTransportPolicy === \"relay\" &&\n !this.isRelayCandidateInit(candidate)\n )\n return;\n // If the answerer gathers ICE faster than its answer SDP travels\n // back through the signalling relay, candidates can land here before\n // `setRemoteDescription` has run. addIceCandidate throws when\n // `remoteDescription` is null on Chrome 148+, and the spec does not\n // require browsers to queue these internally — so buffer until the\n // remote description is set and drain in handleAnswer/handleOffer.\n if (slot.connection.remoteDescription === null) {\n slot.pendingRemoteIce.push(candidate);\n return;\n }\n try {\n await slot.connection.addIceCandidate(candidate);\n } catch {\n // Ignore candidate errors — a stale candidate after the connection\n // has already opened is benign.\n }\n }\n\n /** Drain the per-slot queue of remote ICE candidates that arrived\n * before `setRemoteDescription` completed. Errors per candidate are\n * swallowed for the same reason {@link handleIceCandidate} swallows\n * them — a single bad candidate must not stall the connection. */\n private async flushPendingRemoteIce(slot: PeerSlot): Promise<void> {\n if (slot.pendingRemoteIce.length === 0) return;\n const queued = slot.pendingRemoteIce;\n slot.pendingRemoteIce = [];\n for (const candidate of queued) {\n try {\n await slot.connection.addIceCandidate(candidate);\n } catch {\n // Same rationale as the live-path catch in handleIceCandidate.\n }\n }\n }\n\n private wireConnection(peerId: string, connection: RTCPeerConnection): void {\n connection.onicecandidate = (event) => {\n if (!event.candidate) return;\n if (!this.shouldSendCandidate(event.candidate)) return;\n this.signaling.sendSignal(peerId, {\n kind: \"ice\",\n candidate: event.candidate.toJSON(),\n } satisfies SignalingPayload);\n };\n connection.onconnectionstatechange = () => {\n const state = connection.connectionState;\n if (state === \"connected\") {\n this.emitPeerCandidateOnce(peerId);\n } else if (state === \"disconnected\" || state === \"failed\" || state === \"closed\") {\n // Polly#109/#110: `tearDownWedgedSlot` also closes the\n // connection, which re-enters this handler with\n // state=closed. Only emit if WE are the path that removed\n // the slot from the map — otherwise the wedged-slot teardown\n // would emit `peer-disconnected` twice.\n if (!this.slots.has(peerId)) return;\n this.slots.delete(peerId);\n this.emit(\"peer-disconnected\", { peerId: peerId as unknown as PeerId });\n }\n };\n }\n\n /** Emit `peer-candidate` upward exactly once per slot lifetime,\n * stamping the slot's `lastSyncHandshakeAttempt.peerCandidateEmittedAt`\n * with the moment of emission. The \"once\" semantics protect Automerge's\n * NetworkSubsystem from a double-add when both the\n * `connectionstatechange = connected` event AND the\n * `dataChannel.onopen` event fire on the same slot — under werift\n * the former is sometimes flaky, under Chrome the latter sometimes\n * fires first; pre-#106 only the connection-state path emitted, so a\n * werift slot whose data channel opened cleanly but whose connection\n * state never advanced past `connecting` would never signal \"ready\"\n * upward. Polly issue #106 Failure B item — closing one named\n * mechanism for \"data channel open, sync never started\". */\n private emitPeerCandidateOnce(peerId: string): void {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n if (slot.lastSyncHandshakeAttempt.peerCandidateEmittedAt !== undefined) return;\n slot.lastSyncHandshakeAttempt.peerCandidateEmittedAt = performance.now();\n this.emit(\"peer-candidate\", {\n peerId: peerId as unknown as PeerId,\n peerMetadata: {},\n });\n }\n\n private wireDataChannel(peerId: string, channel: RTCDataChannel): void {\n channel.onopen = () => {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n slot.lastSyncHandshakeAttempt.dataChannelOpenedAt = performance.now();\n // Mirror the connection-state `connected` path: if Automerge has\n // not yet been told the peer exists (because the connection\n // state event hasn't fired or fired with a state we don't\n // forward), the `onopen` callback is a strictly better signal\n // anyway — bytes can flow now. The dedupe inside\n // {@link emitPeerCandidateOnce} keeps Automerge's bookkeeping\n // single-add. Polly issue #106 Failure B.\n this.emitPeerCandidateOnce(peerId);\n // Drain any pending sends that were queued while the channel\n // was still connecting. The fragmenting helper handles oversized\n // payloads the same way it would on a live send.\n for (const bytes of slot.pendingSends) {\n void this.sendBytesMaybeFragmented(channel, bytes);\n }\n slot.pendingSends = [];\n };\n channel.onmessage = (event) => {\n // Polly issue #110: stamp inbound liveness at the wire boundary\n // — before deserialisation/dispatch — so the watchdog's idle\n // check is driven by \"we are still hearing from the peer\", not\n // \"we successfully decoded an Automerge message from the peer\".\n // A peer whose wire is healthy but whose envelopes fail\n // verification is still alive from the transport's point of\n // view, and the wedge this is meant to catch is bytewise\n // silence — not protocol-level corruption.\n const liveSlot = this.slots.get(peerId);\n if (liveSlot) liveSlot.lastInboundAt = performance.now();\n const data = event.data;\n if (data instanceof ArrayBuffer) {\n this.dispatchMessage(peerId, new Uint8Array(data));\n } else if (data instanceof Uint8Array) {\n this.dispatchMessage(peerId, data);\n }\n // Other types (strings, Blobs) are ignored — Polly's mesh transport\n // only sends binary payloads via this adapter.\n };\n channel.onclose = () => {\n const slot = this.slots.get(peerId);\n if (slot?.channel === channel) {\n slot.channel = undefined;\n }\n };\n // Capture the last data-channel error so a consumer harness can\n // surface \"the wire-level error that took this peer offline\" even\n // if the channel has since closed. Polly issue #105 item 7. The\n // event shape differs between Chrome (RTCErrorEvent) and werift\n // (a plain object with `.error`); we narrow to a string for both.\n channel.onerror = (event) => {\n const slot = this.slots.get(peerId);\n if (!slot) return;\n const ev = event as unknown as { error?: { message?: unknown }; message?: unknown };\n const message =\n typeof ev.error?.message === \"string\"\n ? ev.error.message\n : typeof ev.message === \"string\"\n ? ev.message\n : \"unknown data channel error\";\n slot.lastDataChannelError = message;\n };\n }\n\n /** Refresh the per-peer {@link TransportSnapshot} for one peer by\n * pulling {@link RTCPeerConnection.getStats} and distilling the\n * result. Cheap-ish but not free — getStats walks the underlying\n * stats graph — so this is opt-in: callers (typically a debugging\n * harness or a periodic observability loop) invoke it explicitly\n * and the result lives on the slot for the next\n * {@link getPeerStateSnapshot}. Returns the refreshed snapshot, or\n * `undefined` if there is no slot for the peer or stats are\n * unavailable. Polly issue #105 item 7. */\n async refreshTransportStats(peerId: string): Promise<TransportSnapshot | undefined> {\n const slot = this.slots.get(peerId);\n if (!slot) return undefined;\n const snapshot = await collectTransportSnapshot(slot.connection, slot.lastDataChannelError);\n slot.transport = snapshot;\n return snapshot;\n }\n\n /** Refresh transport stats for every active peer slot in one shot.\n * Returns a map keyed by peerId so a caller can render the result\n * without re-walking {@link getPeerStateSnapshot}. The underlying\n * `getStats` calls run concurrently. */\n async refreshAllTransportStats(): Promise<Map<string, TransportSnapshot>> {\n const out = new Map<string, TransportSnapshot>();\n const peerIds = [...this.slots.keys()];\n const results = await Promise.all(peerIds.map((id) => this.refreshTransportStats(id)));\n for (let i = 0; i < peerIds.length; i += 1) {\n const r = results[i];\n const id = peerIds[i];\n if (r && id !== undefined) out.set(id, r);\n }\n return out;\n }\n\n private dispatchMessage(fromPeerId: string, bytes: Uint8Array): void {\n try {\n // Intercept sync-fragments first: an oversized Automerge sync\n // message is split into fragments on the sender side because a\n // single RTCDataChannel.send above the SCTP maxMessageSize cap\n // silently drops or stalls the channel. Reassemble and re-dispatch\n // once every fragment of an id has arrived.\n if (isSyncFragmentType(bytes)) {\n this.handleSyncFragment(fromPeerId, bytes);\n return;\n }\n // Intercept blob messages before they reach the Automerge deserialiser.\n // Blob headers have type fields starting with \"blob-\" and would fail\n // MeshNetworkAdapter's signed-envelope unwrap if passed through.\n if (this.onBlobMessage && isBlobMessageType(bytes)) {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as Record<string, unknown>;\n const data = bytes.subarray(4 + headerLen);\n this.onBlobMessage(fromPeerId, header, data);\n return;\n }\n const message = this.deserialiseMessage(bytes);\n this.scheduleEmitMessage(fromPeerId, message, false);\n } catch {\n // Drop malformed messages silently — the MeshNetworkAdapter wrapper\n // also drops on verification failure, so a corrupt frame at this\n // layer is observationally the same as a forgery at the layer above.\n }\n }\n\n /** Hand a deserialised Automerge message off to whoever is listening\n * on this adapter's `\"message\"` event. When\n * {@link MeshWebRTCAdapterOptions.syncYieldEnabled} is true (the\n * default), the emit runs on a fresh macrotask so the crypto-unwrap\n * and Automerge `applyChanges` chain downstream of this method does\n * not sit on the same JS stack frame as the wire `onmessage` callback\n * — that's the receiver-side starvation site polly issue #104\n * documents. When the option is false the emit is synchronous,\n * recovering the pre-fix shape used by the falsification path.\n *\n * The `viaFragmentPath` argument tags whether this dispatch came out\n * of a reassembled fragment chain; only those carry an\n * `inFlightSync` reassembly state worth bookkeeping. Small\n * single-message dispatches yield but don't touch inFlightSync. */\n private scheduleEmitMessage(\n fromPeerId: string,\n message: Message,\n viaFragmentPath: boolean\n ): void {\n this.stampFirstInboundMessage(fromPeerId);\n this.stampHandleInbound(fromPeerId, message);\n if (!this.syncYieldEnabled) {\n this.emit(\"message\", message);\n if (viaFragmentPath) {\n this.finishInFlightSyncApply(fromPeerId);\n }\n return;\n }\n if (viaFragmentPath) {\n const slot = this.slots.get(fromPeerId);\n if (slot?.inFlightSync) {\n slot.inFlightSync.applyBacklog += 1;\n }\n }\n setTimeout(() => {\n try {\n this.emit(\"message\", message);\n } finally {\n if (viaFragmentPath) {\n this.finishInFlightSyncApply(fromPeerId);\n }\n }\n }, 0);\n }\n\n /** Stamp the slot's `firstInboundMessageAt` the first time a\n * dispatched (non-fragment, non-blob) message lands for a peer. Pure\n * observability for {@link SyncHandshakeAttemptSnapshot}; does not\n * affect dispatch. */\n private stampFirstInboundMessage(fromPeerId: string): void {\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n if (slot.lastSyncHandshakeAttempt.firstInboundMessageAt !== undefined) return;\n slot.lastSyncHandshakeAttempt.firstInboundMessageAt = performance.now();\n }\n\n /** Stamp `handles[documentId].lastSyncMessageInAt` for the\n * per-handle observability layer polly#107 adds. Pure observability;\n * does not affect dispatch. */\n private stampHandleInbound(fromPeerId: string, message: Message): void {\n const documentId = (message as unknown as { documentId?: string }).documentId;\n if (typeof documentId !== \"string\") return;\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n let entry = slot.handles.get(documentId);\n if (!entry) {\n entry = emptyHandleSyncSnapshot();\n slot.handles.set(documentId, entry);\n }\n entry.lastSyncMessageInAt = performance.now();\n }\n\n private finishInFlightSyncApply(fromPeerId: string): void {\n const slot = this.slots.get(fromPeerId);\n if (!slot?.inFlightSync) return;\n slot.inFlightSync.applyBacklog = Math.max(0, slot.inFlightSync.applyBacklog - 1);\n this.emitSyncProgress(fromPeerId, \"dispatch-applied\", 0);\n if (slot.inFlightSync.applyBacklog === 0 && slot.pendingFragments.size === 0) {\n slot.inFlightSync = undefined;\n }\n }\n\n private emitSyncProgress(\n fromPeerId: string,\n kind: \"fragment-received\" | \"dispatch-applied\",\n bytesDelta: number\n ): void {\n const slot = this.slots.get(fromPeerId);\n const inFlightSync = slot?.inFlightSync;\n // The `sync-progress` event is polly-specific; Automerge's\n // NetworkAdapter event-type union doesn't include it. Cast around\n // the typed emit so consumers can subscribe via the same `.on(...)`\n // surface that carries `peer-candidate` and `peer-disconnected`.\n (\n this as unknown as {\n emit: (event: string, payload: SyncProgressEvent) => void;\n }\n ).emit(\"sync-progress\", {\n peerId: fromPeerId,\n kind,\n bytesDelta,\n chunksReceived: inFlightSync?.chunksReceived ?? 0,\n bytesReceived: inFlightSync?.bytesReceived ?? 0,\n applyBacklog: inFlightSync?.applyBacklog ?? 0,\n at: performance.now(),\n });\n }\n\n private handleSyncFragment(fromPeerId: string, bytes: Uint8Array): void {\n const parsed = parseSyncFragment(bytes);\n if (!parsed) return;\n const slot = this.slots.get(fromPeerId);\n if (!slot) return;\n const { header, data } = parsed;\n let entry = slot.pendingFragments.get(header.id);\n if (!entry) {\n entry = { chunks: new Map(), total: header.total };\n slot.pendingFragments.set(header.id, entry);\n }\n // The data view is a window onto the wire frame buffer. Copy out so\n // the reassembled message owns its bytes and the wire frame can be\n // garbage-collected.\n entry.chunks.set(header.index, data.slice());\n if (!slot.inFlightSync) {\n slot.inFlightSync = {\n chunksReceived: 0,\n bytesReceived: 0,\n lastChunkAt: performance.now(),\n applyBacklog: 0,\n };\n }\n slot.inFlightSync.chunksReceived += 1;\n slot.inFlightSync.bytesReceived += data.byteLength;\n slot.inFlightSync.lastChunkAt = performance.now();\n this.emitSyncProgress(fromPeerId, \"fragment-received\", data.byteLength);\n if (entry.chunks.size < entry.total) return;\n slot.pendingFragments.delete(header.id);\n const reassembled = reassembleSyncFragments(entry.chunks, entry.total);\n if (!this.syncYieldEnabled) {\n this.dispatchReassembled(fromPeerId, reassembled);\n return;\n }\n setTimeout(() => {\n this.dispatchReassembled(fromPeerId, reassembled);\n }, 0);\n }\n\n /** Dispatch a reassembled fragment payload back through\n * {@link dispatchMessage}, but tagged so the\n * {@link scheduleEmitMessage} path knows it owes a\n * `finishInFlightSyncApply` afterwards. Synchronous re-entry into\n * `dispatchMessage` would lose that signal, so the post-fragment\n * deserialise+emit is inlined here. */\n private dispatchReassembled(fromPeerId: string, bytes: Uint8Array): void {\n try {\n if (this.onBlobMessage && isBlobMessageType(bytes)) {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as Record<string, unknown>;\n const data = bytes.subarray(4 + headerLen);\n this.onBlobMessage(fromPeerId, header, data);\n this.finishInFlightSyncApply(fromPeerId);\n return;\n }\n const message = this.deserialiseMessage(bytes);\n this.scheduleEmitMessage(fromPeerId, message, true);\n } catch {\n // Same swallowing rationale as dispatchMessage; record the apply\n // completion so the inFlightSync state doesn't get stuck.\n this.finishInFlightSyncApply(fromPeerId);\n }\n }\n\n /** Peer IDs with an open data channel, suitable for blob requests. */\n get connectedPeerIds(): string[] {\n const ids: string[] = [];\n for (const [peerId, slot] of this.slots) {\n if (slot.channel && slot.channel.readyState === \"open\") {\n ids.push(peerId);\n }\n }\n return ids;\n }\n\n /** Send a pre-serialised blob message to a specific peer. Returns false\n * if the peer is not connected or the send buffer is above the high-water\n * mark (caller should retry after a delay). */\n sendBlobMessage(peerId: string, bytes: Uint8Array<ArrayBuffer>): boolean {\n const slot = this.slots.get(peerId);\n if (!slot?.channel || slot.channel.readyState !== \"open\") return false;\n return this.trySendOnChannel(slot.channel, bytes);\n }\n\n /** Send bytes on a data channel if the buffer is below the high-water\n * mark. Returns true if sent, false if backpressure applies. */\n private trySendOnChannel(channel: RTCDataChannel, bytes: Uint8Array<ArrayBuffer>): boolean {\n // 256 KiB high-water mark — matches the blob transfer default.\n if (channel.bufferedAmount > 256 * 1024) return false;\n channel.send(bytes);\n return true;\n }\n\n /** Pack an Automerge Message into binary for transmission over the\n * data channel. The format mirrors MeshNetworkAdapter's internal\n * serialisation: a length-prefixed JSON header followed by the raw\n * data bytes. Returns a Uint8Array<ArrayBuffer> so the result is\n * directly usable by RTCDataChannel.send under TypeScript's strict\n * buffer-source typing. */\n private serialiseMessage(message: Message): Uint8Array<ArrayBuffer> {\n const headerObj: Record<string, unknown> = {\n type: message.type,\n senderId: message.senderId,\n targetId: message.targetId,\n };\n if (\"documentId\" in message && message.documentId !== undefined) {\n headerObj[\"documentId\"] = message.documentId;\n }\n const headerBytes = new TextEncoder().encode(JSON.stringify(headerObj));\n const dataBytes: Uint8Array =\n \"data\" in message && message.data instanceof Uint8Array ? message.data : new Uint8Array(0);\n const size = 4 + headerBytes.length + dataBytes.length;\n const buffer = new ArrayBuffer(size);\n const out = new Uint8Array(buffer);\n const view = new DataView(buffer);\n view.setUint32(0, headerBytes.length, false);\n out.set(headerBytes, 4);\n out.set(dataBytes, 4 + headerBytes.length);\n return out;\n }\n\n /** Inverse of {@link serialiseMessage}. */\n private deserialiseMessage(bytes: Uint8Array): Message {\n if (bytes.length < 4) {\n throw new Error(\"MeshWebRTCAdapter: message too short to deserialise.\");\n }\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) {\n throw new Error(\"MeshWebRTCAdapter: message header truncated.\");\n }\n const header = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + headerLen)));\n const data = bytes.slice(4 + headerLen);\n return { ...header, data } as unknown as Message;\n }\n}\n",
|
|
25
25
|
"/**\n * sync-fragment — chunking, reassembly, and wire format for oversized\n * Automerge sync messages sent through MeshWebRTCAdapter.\n *\n * An `RTCDataChannel.send(bytes)` call with a payload above the SCTP\n * `maxMessageSize` limit (256 KiB by default in Chrome and werift) either\n * throws synchronously, drops silently, or stalls the channel — none of\n * which are observable to the caller. A first-connection Automerge sync\n * for a doc whose state exceeds ~200 KiB produces exactly such a message,\n * so without fragmentation the receiver's `dc.onmessage` never fires for\n * the large payload and sync stalls forever.\n *\n * The wire format here mirrors the blob-transfer layout — a 4-byte\n * big-endian header length followed by a JSON header and the chunk\n * payload — but uses a distinct `type` value (`sync-fragment`, no\n * `blob-` prefix) so the existing blob fast-path in MeshWebRTCAdapter\n * does not mistake a sync fragment for a blob chunk.\n */\n\n/** Maximum bytes a single channel.send may carry without fragmentation.\n * Werift (the node-side WebRTC implementation polly recommends for\n * CLI/daemon use) enforces a hard 64 KiB (65536 bytes) maxMessageSize\n * on its RTCDataChannel — anything larger is rejected with a\n * `max-message-size exceeded` error and silently drops the channel\n * for that send. Chrome's SCTP cap is 256 KiB and would tolerate\n * larger frames, but the threshold is chosen to fit inside werift's\n * cap WITH per-fragment header overhead included so a single mesh\n * deployment works on both transports. Matches the blob-transfer\n * chunk size so the two transports have a consistent per-message\n * footprint on the data channel. */\nexport const SYNC_FRAGMENT_THRESHOLD = 60 * 1024;\n\n/** Chunk size used when a message exceeds the threshold. Left at the\n * same value as {@link SYNC_FRAGMENT_THRESHOLD} so the framing\n * header (a JSON-encoded `SyncFragmentHeader` of ~90 bytes plus a\n * 4-byte length prefix) does not push any fragment over werift's\n * 64 KiB wire limit — see polly issue #104 for the failure mode\n * this guards against. */\nexport const SYNC_FRAGMENT_CHUNK_SIZE = 60 * 1024;\n\nexport interface SyncFragmentHeader {\n type: \"sync-fragment\";\n /** Identifier shared by every fragment of one original message. */\n id: string;\n /** Zero-based fragment index within the message. */\n index: number;\n /** Total number of fragments composing the message. */\n total: number;\n}\n\n/** Wire frame for a sync fragment: 4-byte BE header length, JSON header,\n * raw chunk bytes. Returned as an ArrayBuffer-backed Uint8Array so it is\n * directly usable by RTCDataChannel.send under strict buffer-source typing. */\nexport function serialiseSyncFragment(\n header: SyncFragmentHeader,\n data: Uint8Array\n): Uint8Array<ArrayBuffer> {\n const headerBytes = new TextEncoder().encode(JSON.stringify(header));\n const size = 4 + headerBytes.length + data.length;\n const buffer = new ArrayBuffer(size);\n const out = new Uint8Array(buffer);\n const view = new DataView(buffer);\n view.setUint32(0, headerBytes.length, false);\n out.set(headerBytes, 4);\n out.set(data, 4 + headerBytes.length);\n return out;\n}\n\n/** Parse a wire frame produced by {@link serialiseSyncFragment}. Returns\n * undefined if the bytes are too short, the header is unparseable, or the\n * header `type` is not `sync-fragment`. */\nexport function parseSyncFragment(\n bytes: Uint8Array\n): { header: SyncFragmentHeader; data: Uint8Array } | undefined {\n if (bytes.length < 4) return undefined;\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) return undefined;\n try {\n const header = JSON.parse(\n new TextDecoder().decode(bytes.subarray(4, 4 + headerLen))\n ) as SyncFragmentHeader;\n if (header.type !== \"sync-fragment\") return undefined;\n const data = bytes.subarray(4 + headerLen);\n return { header, data };\n } catch {\n return undefined;\n }\n}\n\n/** Cheap probe used on the receive path before parsing — looks for the\n * literal `\"type\":\"sync-fragment\"` byte sequence inside the JSON header\n * without a full parse. Mirrors `isBlobMessageType`. */\nexport function isSyncFragmentType(bytes: Uint8Array): boolean {\n if (bytes.length < 4) return false;\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const headerLen = view.getUint32(0, false);\n if (bytes.length < 4 + headerLen) return false;\n const headerSlice = bytes.subarray(4, 4 + headerLen);\n const needle = new TextEncoder().encode('\"type\":\"sync-fragment\"');\n return findSubarray(headerSlice, needle) !== -1;\n}\n\n/** Split a serialised sync message into ordered fragments suitable for\n * transmission through one data channel. The caller is responsible for\n * generating the shared `id`. Always returns at least one fragment, so\n * callers can use it unconditionally without a separate small-message\n * branch — though MeshWebRTCAdapter shortcuts the small-message case to\n * avoid the per-chunk header overhead. */\nexport function chunkSyncMessage(\n bytes: Uint8Array,\n id: string,\n chunkSize: number = SYNC_FRAGMENT_CHUNK_SIZE\n): Uint8Array<ArrayBuffer>[] {\n const total = Math.max(1, Math.ceil(bytes.length / chunkSize));\n const fragments: Uint8Array<ArrayBuffer>[] = [];\n for (let index = 0; index < total; index++) {\n const start = index * chunkSize;\n const end = Math.min(start + chunkSize, bytes.length);\n fragments.push(\n serialiseSyncFragment({ type: \"sync-fragment\", id, index, total }, bytes.subarray(start, end))\n );\n }\n return fragments;\n}\n\n/** Reassemble a complete fragment map into the original byte sequence.\n * Throws if any index in [0, total) is missing. */\nexport function reassembleSyncFragments(\n chunks: Map<number, Uint8Array>,\n total: number\n): Uint8Array {\n let totalBytes = 0;\n for (let i = 0; i < total; i++) {\n const chunk = chunks.get(i);\n if (!chunk) {\n throw new Error(`reassembleSyncFragments: missing fragment ${i} of ${total}`);\n }\n totalBytes += chunk.length;\n }\n const out = new Uint8Array(totalBytes);\n let offset = 0;\n for (let i = 0; i < total; i++) {\n const chunk = chunks.get(i) as unknown as Uint8Array;\n out.set(chunk, offset);\n offset += chunk.length;\n }\n return out;\n}\n\nfunction findSubarray(haystack: Uint8Array, needle: Uint8Array): number {\n if (needle.length === 0) return 0;\n outer: for (let i = 0; i <= haystack.length - needle.length; i++) {\n for (let j = 0; j < needle.length; j++) {\n if (haystack[i + j] !== needle[j]) continue outer;\n }\n return i;\n }\n return -1;\n}\n",
|
|
26
26
|
"/**\n * pairing — Phase 2 first-cut pairing flow for $meshState.\n *\n * Two devices that want to share a $meshState document must exchange three\n * things before sync can begin: the issuer's Ed25519 signing public key\n * (so the receiver can verify ops authored by the issuer), the symmetric\n * document encryption key (so both sides can encrypt and decrypt the\n * shared document), and the issuer's stable peer id (so the receiver\n * knows which entry in its keyring the public key belongs to). This\n * module packs all three into a {@link PairingToken}, serialises it to a\n * compact binary format suitable for QR codes or copy-paste, and provides\n * the matching parse-and-apply flow on the receiving side.\n *\n * Threat model: pairing tokens are transmitted over an out-of-band channel\n * that the user can authenticate visually — typically a QR code on the\n * issuer's device, scanned by the receiver. Because anyone with the token\n * can decrypt and impersonate, the OOB channel is the only authentication.\n * The token includes a TTL (default 10 minutes) so that a token displayed\n * briefly and then dismissed cannot be replayed by an attacker who later\n * gains access to a screenshot. A production deployment would layer a\n * Short Authentication String (SAS) on top — both devices display a code\n * derived from the shared state, and the user verifies they match — but\n * that is a follow-up.\n *\n * The pairing flow is one-way in the Phase 2 first cut. The issuer\n * generates a token and displays it; the receiver applies it and picks\n * up the issuer's keys. The receiver's own keys reach the issuer through\n * the access set: when the receiver sends its first signed op, the issuer\n * records the receiver's public key alongside its peer id and adds it to\n * the keyring. A bidirectional pairing flow that exchanges both sides'\n * keys in a single QR exchange is straightforward to add later but adds\n * UX surface area that is not needed for the mesh transport to work.\n */\n\nimport { KEY_BYTES as ENCRYPTION_KEY_BYTES, generateDocumentKey } from \"./encryption\";\nimport type { MeshKeyring } from \"./mesh-network-adapter\";\nimport {\n generateSigningKeyPair,\n PUBLIC_KEY_BYTES as SIGNING_PUBLIC_KEY_BYTES,\n type SigningKeyPair,\n} from \"./signing\";\n\n/** Current pairing-token format version. Bumped if the wire format changes. */\nexport const PAIRING_TOKEN_VERSION = 1;\n\n/** Magic header bytes for sanity-checking parsed tokens. ASCII \"PPT1\". */\nexport const PAIRING_TOKEN_MAGIC = new Uint8Array([0x50, 0x50, 0x54, 0x31]);\n\n/** Length of the random nonce embedded in every token. */\nexport const PAIRING_NONCE_BYTES = 16;\n\n/** Default TTL applied when {@link createPairingToken} is called without an\n * explicit `ttlMs` option. */\nexport const DEFAULT_PAIRING_TTL_MS = 10 * 60 * 1000; // 10 minutes\n\n/**\n * The contents of a pairing token. Both sides operate on this shape; the\n * binary serialisation is purely for transport.\n */\nexport interface PairingToken {\n /** Format version. {@link PAIRING_TOKEN_VERSION} at the time of writing. */\n version: number;\n /** Stable peer id of the issuing device. The receiver records this as\n * the lookup key for the issuer's public key in its keyring. */\n issuerPeerId: string;\n /** Issuer's Ed25519 signing public key (32 bytes). */\n issuerPublicKey: Uint8Array;\n /** Shared document encryption key (32 bytes). The receiver stores this\n * under {@link documentKeyId} in its keyring. */\n documentKey: Uint8Array;\n /** Identifier under which the receiver stores the document key. For the\n * Phase 2 first cut this is typically the well-known DEFAULT_MESH_KEY_ID\n * from mesh-network-adapter; per-document keys (one entry per Automerge\n * document) are a follow-up. */\n documentKeyId: string;\n /** Unix timestamp (milliseconds) after which the token is considered\n * expired and {@link applyPairingToken} refuses to use it. */\n expiresAt: number;\n /** 16-byte random nonce. Carried through serialisation so two tokens\n * with otherwise-identical contents are still distinguishable. */\n nonce: Uint8Array;\n}\n\n/** Errors thrown by the pairing subsystem. */\nexport class PairingError extends Error {\n readonly code:\n | \"expired\"\n | \"wrong-magic\"\n | \"unknown-version\"\n | \"truncated\"\n | \"invalid-public-key\"\n | \"invalid-document-key\"\n | \"invalid-nonce\";\n\n constructor(message: string, code: PairingError[\"code\"]) {\n super(message);\n this.name = \"PairingError\";\n this.code = code;\n }\n}\n\n/**\n * Options for {@link createPairingToken}. The signing identity and the\n * document key are required; everything else is optional with sensible\n * defaults.\n */\nexport interface CreatePairingTokenOptions {\n /** The issuing device's signing keypair. Only the public key ends up in\n * the token; the secret never leaves the issuer. */\n identity: SigningKeyPair;\n /** Stable peer id for the issuing device. */\n issuerPeerId: string;\n /** The symmetric document key the receiver should adopt. If omitted, a\n * fresh key is generated and the caller is responsible for using the\n * same key on the issuing side too. */\n documentKey?: Uint8Array;\n /** Identifier under which the receiver stores the document key. */\n documentKeyId: string;\n /** Time-to-live in milliseconds. Defaults to {@link DEFAULT_PAIRING_TTL_MS}. */\n ttlMs?: number;\n /** Override the current time. Intended for tests; production code should\n * not pass this. */\n now?: () => number;\n}\n\n/**\n * Generate a fresh {@link PairingToken}. The token is ready to be\n * serialised and displayed to the receiver via an OOB channel.\n */\nexport function createPairingToken(options: CreatePairingTokenOptions): PairingToken {\n const now = options.now ? options.now() : Date.now();\n const ttlMs = options.ttlMs ?? DEFAULT_PAIRING_TTL_MS;\n const documentKey = options.documentKey ?? generateDocumentKey();\n const nonce = randomBytes(PAIRING_NONCE_BYTES);\n\n return {\n version: PAIRING_TOKEN_VERSION,\n issuerPeerId: options.issuerPeerId,\n issuerPublicKey: options.identity.publicKey,\n documentKey,\n documentKeyId: options.documentKeyId,\n expiresAt: now + ttlMs,\n nonce,\n };\n}\n\n/**\n * Generate a fresh pairing token *and* a fresh signing keypair in one call.\n * Convenience for first-time setup where the device has no existing\n * identity yet. Returns both so the caller can persist the keypair and\n * then display the token.\n */\nexport function createPairingTokenWithFreshIdentity(args: {\n issuerPeerId: string;\n documentKeyId: string;\n ttlMs?: number;\n now?: () => number;\n}): { identity: SigningKeyPair; token: PairingToken } {\n const identity = generateSigningKeyPair();\n const token = createPairingToken({\n identity,\n issuerPeerId: args.issuerPeerId,\n documentKeyId: args.documentKeyId,\n ttlMs: args.ttlMs,\n now: args.now,\n });\n return { identity, token };\n}\n\n/**\n * Check whether a token has expired against the current wall-clock time\n * (or an injected `now`).\n */\nexport function isPairingTokenExpired(token: PairingToken, now?: () => number): boolean {\n const t = now ? now() : Date.now();\n return t >= token.expiresAt;\n}\n\n/**\n * Apply a parsed and validated token to a {@link MeshKeyring}. Mutates the\n * keyring in place: adds the issuer's public key to {@link MeshKeyring.knownPeers}\n * and the document key to {@link MeshKeyring.documentKeys}.\n *\n * Throws {@link PairingError} with code \"expired\" if the token's TTL has\n * elapsed. The receiver is expected to apply the token promptly after\n * scanning; rejecting expired tokens prevents replay of long-lived\n * captures.\n */\nexport function applyPairingToken(\n token: PairingToken,\n keyring: MeshKeyring,\n options: { now?: () => number } = {}\n): void {\n if (isPairingTokenExpired(token, options.now)) {\n throw new PairingError(\n `Pairing token from ${token.issuerPeerId} expired at ${new Date(token.expiresAt).toISOString()}.`,\n \"expired\"\n );\n }\n keyring.knownPeers.set(token.issuerPeerId, token.issuerPublicKey);\n keyring.documentKeys.set(token.documentKeyId, token.documentKey);\n}\n\n// binary serialisation\n\n/**\n * Serialise a token to a binary blob. The wire format is:\n *\n * [4 bytes: magic \"PPT1\"]\n * [1 byte: version]\n * [4 bytes BE: issuer id byte length]\n * [N bytes: issuer id UTF-8]\n * [32 bytes: issuer public key]\n * [32 bytes: document key]\n * [4 bytes BE: document key id byte length]\n * [M bytes: document key id UTF-8]\n * [8 bytes BE: expiresAt (uint64 milliseconds)]\n * [16 bytes: nonce]\n *\n * Use {@link encodePairingToken} to round-trip through a base64 string.\n */\nexport function serialisePairingToken(token: PairingToken): Uint8Array {\n validateForSerialisation(token);\n const issuerBytes = new TextEncoder().encode(token.issuerPeerId);\n const keyIdBytes = new TextEncoder().encode(token.documentKeyId);\n\n const total =\n PAIRING_TOKEN_MAGIC.length +\n 1 + // version\n 4 + // issuer id length\n issuerBytes.length +\n SIGNING_PUBLIC_KEY_BYTES +\n ENCRYPTION_KEY_BYTES +\n 4 + // doc key id length\n keyIdBytes.length +\n 8 + // expiresAt\n PAIRING_NONCE_BYTES;\n\n const out = new Uint8Array(total);\n let offset = 0;\n\n out.set(PAIRING_TOKEN_MAGIC, offset);\n offset += PAIRING_TOKEN_MAGIC.length;\n\n out[offset] = token.version;\n offset += 1;\n\n const view = new DataView(out.buffer);\n view.setUint32(offset, issuerBytes.length, false);\n offset += 4;\n out.set(issuerBytes, offset);\n offset += issuerBytes.length;\n\n out.set(token.issuerPublicKey, offset);\n offset += SIGNING_PUBLIC_KEY_BYTES;\n\n out.set(token.documentKey, offset);\n offset += ENCRYPTION_KEY_BYTES;\n\n view.setUint32(offset, keyIdBytes.length, false);\n offset += 4;\n out.set(keyIdBytes, offset);\n offset += keyIdBytes.length;\n\n // Write expiresAt as uint64 BE. JavaScript numbers are float64 but the\n // value is an integer count of milliseconds, well within 53-bit safe\n // range for any practical timestamp.\n view.setBigUint64(offset, BigInt(token.expiresAt), false);\n offset += 8;\n\n out.set(token.nonce, offset);\n offset += PAIRING_NONCE_BYTES;\n\n return out;\n}\n\n/**\n * Inverse of {@link serialisePairingToken}. Throws {@link PairingError} on\n * malformed input.\n */\nexport function parsePairingToken(bytes: Uint8Array): PairingToken {\n let offset = 0;\n\n // Magic\n if (bytes.length < PAIRING_TOKEN_MAGIC.length) {\n throw new PairingError(`Pairing token too short: ${bytes.length} bytes.`, \"truncated\");\n }\n for (let i = 0; i < PAIRING_TOKEN_MAGIC.length; i++) {\n if (bytes[offset + i] !== PAIRING_TOKEN_MAGIC[i]) {\n throw new PairingError(\n `Pairing token magic mismatch: not a Polly pairing token.`,\n \"wrong-magic\"\n );\n }\n }\n offset += PAIRING_TOKEN_MAGIC.length;\n\n // Version\n if (bytes.length < offset + 1) {\n throw new PairingError(\"Pairing token truncated at version.\", \"truncated\");\n }\n const version = bytes[offset] as unknown as number;\n offset += 1;\n if (version !== PAIRING_TOKEN_VERSION) {\n throw new PairingError(\n `Unknown pairing token version: ${version}. This Polly build supports version ${PAIRING_TOKEN_VERSION}.`,\n \"unknown-version\"\n );\n }\n\n // Issuer id\n if (bytes.length < offset + 4) {\n throw new PairingError(\"Pairing token truncated at issuer id length.\", \"truncated\");\n }\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const issuerLen = view.getUint32(offset, false);\n offset += 4;\n if (bytes.length < offset + issuerLen) {\n throw new PairingError(\"Pairing token truncated at issuer id.\", \"truncated\");\n }\n const issuerPeerId = new TextDecoder().decode(bytes.subarray(offset, offset + issuerLen));\n offset += issuerLen;\n\n // Issuer public key\n if (bytes.length < offset + SIGNING_PUBLIC_KEY_BYTES) {\n throw new PairingError(\"Pairing token truncated at public key.\", \"truncated\");\n }\n const issuerPublicKey = bytes.slice(offset, offset + SIGNING_PUBLIC_KEY_BYTES);\n offset += SIGNING_PUBLIC_KEY_BYTES;\n\n // Document key\n if (bytes.length < offset + ENCRYPTION_KEY_BYTES) {\n throw new PairingError(\"Pairing token truncated at document key.\", \"truncated\");\n }\n const documentKey = bytes.slice(offset, offset + ENCRYPTION_KEY_BYTES);\n offset += ENCRYPTION_KEY_BYTES;\n\n // Document key id\n if (bytes.length < offset + 4) {\n throw new PairingError(\"Pairing token truncated at document key id length.\", \"truncated\");\n }\n const keyIdLen = view.getUint32(offset, false);\n offset += 4;\n if (bytes.length < offset + keyIdLen) {\n throw new PairingError(\"Pairing token truncated at document key id.\", \"truncated\");\n }\n const documentKeyId = new TextDecoder().decode(bytes.subarray(offset, offset + keyIdLen));\n offset += keyIdLen;\n\n // Expires at\n if (bytes.length < offset + 8) {\n throw new PairingError(\"Pairing token truncated at expiry.\", \"truncated\");\n }\n const expiresAtBig = view.getBigUint64(offset, false);\n offset += 8;\n const expiresAt = Number(expiresAtBig);\n\n // Nonce\n if (bytes.length < offset + PAIRING_NONCE_BYTES) {\n throw new PairingError(\"Pairing token truncated at nonce.\", \"truncated\");\n }\n const nonce = bytes.slice(offset, offset + PAIRING_NONCE_BYTES);\n offset += PAIRING_NONCE_BYTES;\n\n return {\n version,\n issuerPeerId,\n issuerPublicKey,\n documentKey,\n documentKeyId,\n expiresAt,\n nonce,\n };\n}\n\n/**\n * Serialise a token and base64-encode it for QR-code or copy-paste display.\n * The encoding uses the standard base64 alphabet (not URL-safe) because\n * QR codes encode bytes directly and do not care about URL safety.\n */\nexport function encodePairingToken(token: PairingToken): string {\n const bytes = serialisePairingToken(token);\n // btoa expects a binary string. Convert via String.fromCharCode per byte;\n // safe for the ~150-byte token size and avoids the spread-into-fromCharCode\n // pattern that runs into argument-count limits on large arrays.\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\n/**\n * Decode a base64-encoded pairing token produced by {@link encodePairingToken}.\n * Throws {@link PairingError} on malformed input.\n */\nexport function decodePairingToken(encoded: string): PairingToken {\n let binary: string;\n try {\n binary = atob(encoded);\n } catch {\n throw new PairingError(\"Pairing token is not valid base64.\", \"wrong-magic\");\n }\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i);\n }\n return parsePairingToken(bytes);\n}\n\n// helpers\n\nfunction validateForSerialisation(token: PairingToken): void {\n if (token.issuerPublicKey.length !== SIGNING_PUBLIC_KEY_BYTES) {\n throw new PairingError(\n `Issuer public key must be ${SIGNING_PUBLIC_KEY_BYTES} bytes, got ${token.issuerPublicKey.length}.`,\n \"invalid-public-key\"\n );\n }\n if (token.documentKey.length !== ENCRYPTION_KEY_BYTES) {\n throw new PairingError(\n `Document key must be ${ENCRYPTION_KEY_BYTES} bytes, got ${token.documentKey.length}.`,\n \"invalid-document-key\"\n );\n }\n if (token.nonce.length !== PAIRING_NONCE_BYTES) {\n throw new PairingError(\n `Nonce must be ${PAIRING_NONCE_BYTES} bytes, got ${token.nonce.length}.`,\n \"invalid-nonce\"\n );\n }\n}\n\nfunction randomBytes(n: number): Uint8Array {\n const out = new Uint8Array(n);\n crypto.getRandomValues(out);\n return out;\n}\n",
|
|
27
27
|
"/**\n * revocation — Phase 2 key-revocation flow for $meshState.\n *\n * A $meshState deployment is only as secure as the keys it trusts. When a\n * device is lost, stolen, or compromised, its public key needs to be\n * removed from every other peer's trust set so that any operations signed\n * by that key are refused. This module provides the primitive pieces for\n * that flow: a signed revocation record that names the revoked peer id,\n * carries the issuer's identity, and travels between peers through the\n * same channels as any other signed message.\n *\n * A RevocationRecord is produced by a peer that has decided to revoke\n * another peer (typically because the user told their device to do so).\n * It is then serialised to a binary envelope, broadcast to other peers\n * through whatever transport is available, and applied to each receiving\n * keyring via {@link applyRevocation}. Applying a revocation adds the\n * target peer id to the keyring's {@link MeshKeyring.revokedPeers} set;\n * the MeshNetworkAdapter then drops all further messages from that peer,\n * even though the public key is still present in the knownPeers map.\n *\n * Signature layer: every revocation is signed by the *issuer's* key. The\n * receiver verifies the signature against the issuer's public key before\n * applying. This prevents a peer with no authority from forging a\n * revocation, at least in the signature-level threat model — a compromised\n * peer could still forge revocations naming any other peer, which is why\n * production deployments layer an access-set check on top (\"who is\n * authorised to revoke whom\"). That layer is a follow-up; the first cut\n * is signature-only, and any signed revocation from a known peer is\n * accepted.\n *\n * Why revocations are signed but not encrypted: revocations are public\n * in the sense that they name \"peer X is no longer trusted.\" There is no\n * confidentiality requirement — anyone seeing a revocation learns only\n * that someone has been kicked out, which is already observable from\n * the absence of their operations. Encrypting them would add cost without\n * adding security, so the wire format is signed-only.\n */\n\nimport type { MeshKeyring } from \"./mesh-network-adapter\";\nimport {\n decodeSignedEnvelope,\n encodeSignedEnvelope,\n openEnvelope as openSignedEnvelope,\n type SigningKeyPair,\n signEnvelope,\n} from \"./signing\";\n\n/** Current revocation-record format version. */\nexport const REVOCATION_RECORD_VERSION = 1;\n\n/** Magic header bytes for sanity-checking parsed revocations. ASCII \"PRV1\". */\nexport const REVOCATION_MAGIC = new Uint8Array([0x50, 0x52, 0x56, 0x31]);\n\n/**\n * The payload carried inside a signed revocation envelope.\n */\nexport interface RevocationRecord {\n /** Format version. */\n version: number;\n /** The peer id doing the revoking. Becomes the `senderId` of the signed\n * envelope when the record is serialised. */\n issuerPeerId: string;\n /** The peer id being revoked. After a receiver applies this record, its\n * MeshNetworkAdapter will drop all further messages from this peer. */\n revokedPeerId: string;\n /** Unix timestamp (milliseconds) when the revocation was issued. Carried\n * for audit and for tie-breaking in case an issuer issues multiple\n * revocations targeting the same peer. */\n issuedAt: number;\n /** Optional human-readable reason. Not used by the enforcement layer;\n * applications can surface it in audit logs. */\n reason?: string;\n}\n\n/** Errors thrown by the revocation subsystem. */\nexport class RevocationError extends Error {\n readonly code:\n | \"unknown-issuer\"\n | \"unauthorised-issuer\"\n | \"signature-invalid\"\n | \"truncated\"\n | \"wrong-magic\"\n | \"unknown-version\"\n | \"not-signed-by-issuer\";\n\n constructor(message: string, code: RevocationError[\"code\"]) {\n super(message);\n this.name = \"RevocationError\";\n this.code = code;\n }\n}\n\n/** Options for {@link createRevocation}. */\nexport interface CreateRevocationOptions {\n /** Signing keypair of the issuer. The private key signs the record; the\n * public key is looked up by the receiver via `issuerPeerId`. */\n issuer: SigningKeyPair;\n /** Stable peer id of the issuer. */\n issuerPeerId: string;\n /** Peer id to revoke. */\n revokedPeerId: string;\n /** Optional human-readable reason. */\n reason?: string;\n /** Override the current time. Intended for tests. */\n now?: () => number;\n}\n\n/**\n * Build a fresh {@link RevocationRecord}. The returned record is plain\n * data; use {@link encodeRevocation} to wrap it in a signed envelope\n * suitable for transport.\n */\nexport function createRevocation(options: CreateRevocationOptions): RevocationRecord {\n const now = options.now ? options.now() : Date.now();\n return {\n version: REVOCATION_RECORD_VERSION,\n issuerPeerId: options.issuerPeerId,\n revokedPeerId: options.revokedPeerId,\n issuedAt: now,\n ...(options.reason === undefined ? {} : { reason: options.reason }),\n };\n}\n\n/**\n * Apply a verified revocation to a keyring. Mutates the keyring in place:\n * adds the target peer id to {@link MeshKeyring.revokedPeers}. The caller\n * must verify the revocation (via {@link decodeRevocation}) before\n * applying; this function does not revalidate the signature.\n */\nexport function applyRevocation(record: RevocationRecord, keyring: MeshKeyring): void {\n keyring.revokedPeers.add(record.revokedPeerId);\n}\n\n/**\n * Revoke a peer locally without producing a transportable record. Useful\n * for the common case where an application decides to drop a peer on this\n * device without propagating the decision elsewhere (tests, single-device\n * setups). Peers that want the revocation to spread to other devices\n * should use {@link createRevocation} + {@link encodeRevocation}.\n */\nexport function revokePeerLocally(peerId: string, keyring: MeshKeyring): void {\n keyring.revokedPeers.add(peerId);\n}\n\n// binary serialisation\n\n/**\n * Serialise a {@link RevocationRecord} to a binary payload ready for\n * signing. The layout is:\n *\n * [4 bytes: magic \"PRV1\"]\n * [1 byte: version]\n * [4 bytes BE: issuer id byte length]\n * [N bytes: issuer id UTF-8]\n * [4 bytes BE: revoked peer id byte length]\n * [M bytes: revoked peer id UTF-8]\n * [8 bytes BE: issuedAt (uint64 milliseconds)]\n * [4 bytes BE: reason byte length]\n * [R bytes: reason UTF-8]\n */\nexport function serialiseRevocationPayload(record: RevocationRecord): Uint8Array {\n const issuerBytes = new TextEncoder().encode(record.issuerPeerId);\n const revokedBytes = new TextEncoder().encode(record.revokedPeerId);\n const reasonBytes = new TextEncoder().encode(record.reason ?? \"\");\n\n const total =\n REVOCATION_MAGIC.length +\n 1 + // version\n 4 +\n issuerBytes.length +\n 4 +\n revokedBytes.length +\n 8 + // issuedAt\n 4 +\n reasonBytes.length;\n\n const out = new Uint8Array(total);\n let offset = 0;\n\n out.set(REVOCATION_MAGIC, offset);\n offset += REVOCATION_MAGIC.length;\n\n out[offset] = record.version;\n offset += 1;\n\n const view = new DataView(out.buffer);\n view.setUint32(offset, issuerBytes.length, false);\n offset += 4;\n out.set(issuerBytes, offset);\n offset += issuerBytes.length;\n\n view.setUint32(offset, revokedBytes.length, false);\n offset += 4;\n out.set(revokedBytes, offset);\n offset += revokedBytes.length;\n\n view.setBigUint64(offset, BigInt(record.issuedAt), false);\n offset += 8;\n\n view.setUint32(offset, reasonBytes.length, false);\n offset += 4;\n out.set(reasonBytes, offset);\n\n return out;\n}\n\n/** Inverse of {@link serialiseRevocationPayload}. */\nfunction parseRevocationPayload(bytes: Uint8Array): RevocationRecord {\n let offset = 0;\n\n if (bytes.length < REVOCATION_MAGIC.length) {\n throw new RevocationError(\"Revocation record too short for magic.\", \"truncated\");\n }\n for (let i = 0; i < REVOCATION_MAGIC.length; i++) {\n if (bytes[offset + i] !== REVOCATION_MAGIC[i]) {\n throw new RevocationError(\"Revocation record magic mismatch.\", \"wrong-magic\");\n }\n }\n offset += REVOCATION_MAGIC.length;\n\n if (bytes.length < offset + 1) {\n throw new RevocationError(\"Revocation record truncated at version.\", \"truncated\");\n }\n const version = bytes[offset] as unknown as number;\n offset += 1;\n if (version !== REVOCATION_RECORD_VERSION) {\n throw new RevocationError(`Unknown revocation record version: ${version}.`, \"unknown-version\");\n }\n\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n\n if (bytes.length < offset + 4) {\n throw new RevocationError(\"Revocation record truncated at issuer length.\", \"truncated\");\n }\n const issuerLen = view.getUint32(offset, false);\n offset += 4;\n if (bytes.length < offset + issuerLen) {\n throw new RevocationError(\"Revocation record truncated at issuer id.\", \"truncated\");\n }\n const issuerPeerId = new TextDecoder().decode(bytes.subarray(offset, offset + issuerLen));\n offset += issuerLen;\n\n if (bytes.length < offset + 4) {\n throw new RevocationError(\"Revocation record truncated at revoked id length.\", \"truncated\");\n }\n const revokedLen = view.getUint32(offset, false);\n offset += 4;\n if (bytes.length < offset + revokedLen) {\n throw new RevocationError(\"Revocation record truncated at revoked id.\", \"truncated\");\n }\n const revokedPeerId = new TextDecoder().decode(bytes.subarray(offset, offset + revokedLen));\n offset += revokedLen;\n\n if (bytes.length < offset + 8) {\n throw new RevocationError(\"Revocation record truncated at issuedAt.\", \"truncated\");\n }\n const issuedAt = Number(view.getBigUint64(offset, false));\n offset += 8;\n\n if (bytes.length < offset + 4) {\n throw new RevocationError(\"Revocation record truncated at reason length.\", \"truncated\");\n }\n const reasonLen = view.getUint32(offset, false);\n offset += 4;\n if (bytes.length < offset + reasonLen) {\n throw new RevocationError(\"Revocation record truncated at reason.\", \"truncated\");\n }\n const reason = new TextDecoder().decode(bytes.subarray(offset, offset + reasonLen));\n offset += reasonLen;\n\n return {\n version,\n issuerPeerId,\n revokedPeerId,\n issuedAt,\n ...(reason ? { reason } : {}),\n };\n}\n\n/**\n * Produce a signed, transportable binary blob from a revocation record.\n * Wraps the serialised payload in a {@link SignedEnvelope} signed by the\n * issuer's keypair, then encodes the envelope to bytes.\n */\nexport function encodeRevocation(record: RevocationRecord, issuer: SigningKeyPair): Uint8Array {\n const payload = serialiseRevocationPayload(record);\n const envelope = signEnvelope(payload, record.issuerPeerId, issuer.secretKey);\n return encodeSignedEnvelope(envelope);\n}\n\n/**\n * Parse and verify a signed revocation blob. Returns the decoded record\n * on success; throws {@link RevocationError} on any failure.\n *\n * Verification requires the keyring to already know the issuer's public\n * key (because Polly does not have a global PKI). If the issuer is\n * unknown, the caller receives `unknown-issuer` and can decide whether\n * to cache the revocation for later verification or drop it outright.\n *\n * Authorisation: when the keyring has a non-empty\n * {@link MeshKeyring.revocationAuthority} set, only issuers inside that\n * set can have their revocations accepted. An issuer outside the set\n * produces `unauthorised-issuer`. When the authority set is undefined\n * or empty, any signed revocation from a known peer is accepted — that\n * is the Phase 2 first-cut default, preserved for backward compatibility.\n */\nexport function decodeRevocation(bytes: Uint8Array, keyring: MeshKeyring): RevocationRecord {\n const envelope = decodeSignedEnvelope(bytes);\n const issuerKey = keyring.knownPeers.get(envelope.senderId);\n if (!issuerKey) {\n throw new RevocationError(\n `Revocation issuer ${envelope.senderId} is not in the local keyring.`,\n \"unknown-issuer\"\n );\n }\n if (\n keyring.revocationAuthority !== undefined &&\n keyring.revocationAuthority.size > 0 &&\n !keyring.revocationAuthority.has(envelope.senderId)\n ) {\n throw new RevocationError(\n `Revocation issuer ${envelope.senderId} is not in the keyring's revocation authority set.`,\n \"unauthorised-issuer\"\n );\n }\n let payloadBytes: Uint8Array;\n try {\n payloadBytes = openSignedEnvelope(envelope, issuerKey);\n } catch {\n throw new RevocationError(\n `Revocation signature failed verification for issuer ${envelope.senderId}.`,\n \"signature-invalid\"\n );\n }\n\n const record = parseRevocationPayload(payloadBytes);\n // Paranoid cross-check: the payload's claimed issuer must match the\n // envelope's authenticated sender. Mismatch means a forged payload.\n if (record.issuerPeerId !== envelope.senderId) {\n throw new RevocationError(\n `Revocation payload claims issuer ${record.issuerPeerId} but the envelope was signed by ${envelope.senderId}.`,\n \"not-signed-by-issuer\"\n );\n }\n return record;\n}\n"
|
|
28
28
|
],
|
|
29
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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