@fairfox/polly 0.20.1 → 0.22.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.
Files changed (62) hide show
  1. package/README.md +83 -3
  2. package/dist/cli/polly.js +21 -1
  3. package/dist/cli/polly.js.map +3 -3
  4. package/dist/src/background/index.js.map +7 -7
  5. package/dist/src/background/message-router.js.map +7 -7
  6. package/dist/src/elysia/index.d.ts +2 -0
  7. package/dist/src/elysia/index.js +177 -17
  8. package/dist/src/elysia/index.js.map +8 -5
  9. package/dist/src/elysia/peer-repo-plugin.d.ts +79 -0
  10. package/dist/src/elysia/signaling-server-plugin.d.ts +121 -0
  11. package/dist/src/index.d.ts +4 -0
  12. package/dist/src/index.js +90 -1
  13. package/dist/src/index.js.map +15 -13
  14. package/dist/src/mesh.d.ts +29 -0
  15. package/dist/src/mesh.js +1502 -0
  16. package/dist/src/mesh.js.map +22 -0
  17. package/dist/src/peer.d.ts +29 -0
  18. package/dist/src/peer.js +928 -0
  19. package/dist/src/peer.js.map +20 -0
  20. package/dist/src/shared/adapters/index.js.map +6 -6
  21. package/dist/src/shared/lib/_client-only.d.ts +38 -0
  22. package/dist/src/shared/lib/access.d.ts +124 -0
  23. package/dist/src/shared/lib/blob-ref.d.ts +72 -0
  24. package/dist/src/shared/lib/context-helpers.js.map +7 -7
  25. package/dist/src/shared/lib/crdt-specialised.d.ts +129 -0
  26. package/dist/src/shared/lib/crdt-state.d.ts +86 -0
  27. package/dist/src/shared/lib/encryption.d.ts +117 -0
  28. package/dist/src/shared/lib/mesh-network-adapter.d.ts +130 -0
  29. package/dist/src/shared/lib/mesh-signaling-client.d.ts +85 -0
  30. package/dist/src/shared/lib/mesh-state.d.ts +102 -0
  31. package/dist/src/shared/lib/mesh-webrtc-adapter.d.ts +132 -0
  32. package/dist/src/shared/lib/message-bus.js.map +7 -7
  33. package/dist/src/shared/lib/migrate-primitive.d.ts +100 -0
  34. package/dist/src/shared/lib/pairing.d.ts +170 -0
  35. package/dist/src/shared/lib/peer-relay-adapter.d.ts +80 -0
  36. package/dist/src/shared/lib/peer-repo-server.d.ts +83 -0
  37. package/dist/src/shared/lib/peer-state.d.ts +117 -0
  38. package/dist/src/shared/lib/primitive-registry.d.ts +88 -0
  39. package/dist/src/shared/lib/resource.js.map +4 -4
  40. package/dist/src/shared/lib/revocation.d.ts +126 -0
  41. package/dist/src/shared/lib/schema-version.d.ts +129 -0
  42. package/dist/src/shared/lib/signing.d.ts +118 -0
  43. package/dist/src/shared/lib/state.js.map +4 -4
  44. package/dist/src/shared/state/app-state.js.map +5 -5
  45. package/dist/tools/init/src/cli.js.map +1 -1
  46. package/dist/tools/quality/src/cli.js +162 -0
  47. package/dist/tools/quality/src/cli.js.map +11 -0
  48. package/dist/tools/test/src/adapters/index.js.map +2 -2
  49. package/dist/tools/test/src/browser/harness.d.ts +80 -0
  50. package/dist/tools/test/src/browser/index.d.ts +32 -0
  51. package/dist/tools/test/src/browser/index.js +243 -0
  52. package/dist/tools/test/src/browser/index.js.map +10 -0
  53. package/dist/tools/test/src/browser/run.d.ts +26 -0
  54. package/dist/tools/test/src/index.js.map +2 -2
  55. package/dist/tools/verify/specs/tla/MeshState.cfg +21 -0
  56. package/dist/tools/verify/specs/tla/MeshState.tla +247 -0
  57. package/dist/tools/verify/specs/tla/PeerState.cfg +27 -0
  58. package/dist/tools/verify/specs/tla/PeerState.tla +238 -0
  59. package/dist/tools/verify/specs/tla/README.md +27 -3
  60. package/dist/tools/verify/src/cli.js.map +8 -8
  61. package/dist/tools/visualize/src/cli.js.map +7 -7
  62. package/package.json +51 -5
@@ -0,0 +1,132 @@
1
+ /**
2
+ * mesh-webrtc-adapter — Phase 2 browser-side WebRTC transport for Polly's
3
+ * $meshState primitive. Extends Automerge's NetworkAdapter base class and
4
+ * uses real native RTCPeerConnection / RTCDataChannel instances to carry
5
+ * sync messages between peers.
6
+ *
7
+ * This is the "base" transport that MeshNetworkAdapter wraps with its
8
+ * sign-then-encrypt envelope. The stack is:
9
+ *
10
+ * $meshState
11
+ * └─ Repo
12
+ * └─ MeshNetworkAdapter (sign + encrypt)
13
+ * └─ MeshWebRTCAdapter (real data channels)
14
+ * └─ MeshSignalingClient (SDP/ICE relay)
15
+ * └─ signalingServer (Elysia plugin on the host app)
16
+ *
17
+ * Because WebRTC lives in browsers, this module is browser-only. It
18
+ * assumes global RTCPeerConnection, RTCDataChannel, and WebSocket types
19
+ * are available. Under bun:test the classes cannot be exercised
20
+ * end-to-end — the first validation path is either Playwright running a
21
+ * real browser, a Puppeteer harness, or a human testing a browser-side
22
+ * example app that consumes the adapter.
23
+ *
24
+ * What this module does at runtime:
25
+ *
26
+ * - Constructs a MeshWebRTCAdapter with a signalling client and a local
27
+ * peer id. No data channels exist at construction time.
28
+ *
29
+ * - When Automerge's NetworkSubsystem calls connect(peerId) on the
30
+ * adapter, it starts listening for signals from remote peers and is
31
+ * ready to build peer connections as they are discovered.
32
+ *
33
+ * - When a remote peer sends an SDP offer via the signalling channel,
34
+ * the adapter builds a fresh RTCPeerConnection, accepts the offer,
35
+ * produces an answer, sends it back through signalling, and wires the
36
+ * received data channel to emit Automerge Message events upward.
37
+ *
38
+ * - When the local repo calls send(message), the adapter looks up the
39
+ * peer connection for message.targetId and writes the serialised
40
+ * bytes to its data channel. If no connection exists yet, the adapter
41
+ * creates one by sending an SDP offer through signalling. Outgoing
42
+ * messages are queued until the channel is open.
43
+ *
44
+ * - Disconnect tears down every peer connection and closes the
45
+ * signalling client.
46
+ */
47
+ import { type Message, NetworkAdapter, type PeerId, type PeerMetadata } from "@automerge/automerge-repo";
48
+ import type { MeshSignalingClient } from "./mesh-signaling-client";
49
+ /** Standard STUN servers for NAT traversal. In production, callers who
50
+ * need TURN fallback for peers behind symmetric NATs should replace this
51
+ * with their own ICE server list. */
52
+ export declare const DEFAULT_ICE_SERVERS: RTCIceServer[];
53
+ /** Options for constructing a {@link MeshWebRTCAdapter}. */
54
+ export interface MeshWebRTCAdapterOptions {
55
+ /** The signalling client the adapter uses to exchange SDP and ICE
56
+ * candidates with other peers. Typically constructed once per
57
+ * application and shared across any adapters that need it. */
58
+ signaling: MeshSignalingClient;
59
+ /** The local peer id. Must match the peer id the signalling client
60
+ * registered with. */
61
+ peerId: string;
62
+ /** Peer ids to connect to on startup. When `connect()` is called, the
63
+ * adapter iterates this list and initiates a WebRTC connection to each
64
+ * one by sending an SDP offer through the signalling channel. Peers
65
+ * not in this list can still connect by sending an offer *to* this
66
+ * adapter. The natural source for this list is the keyring's
67
+ * knownPeers map keys. */
68
+ knownPeerIds?: string[];
69
+ /** Optional ICE server list override. Defaults to {@link DEFAULT_ICE_SERVERS}. */
70
+ iceServers?: RTCIceServer[];
71
+ /** Optional data channel label. Defaults to "polly-mesh". Applications
72
+ * that share a signalling server between multiple meshes may want
73
+ * distinct labels per mesh. */
74
+ dataChannelLabel?: string;
75
+ }
76
+ /**
77
+ * Automerge-Repo NetworkAdapter backed by real WebRTC data channels.
78
+ * Manages one RTCPeerConnection per remote peer and uses a supplied
79
+ * {@link MeshSignalingClient} for SDP/ICE exchange.
80
+ */
81
+ export declare class MeshWebRTCAdapter extends NetworkAdapter {
82
+ readonly signaling: MeshSignalingClient;
83
+ readonly iceServers: RTCIceServer[];
84
+ readonly dataChannelLabel: string;
85
+ readonly knownPeerIds: string[];
86
+ private readonly slots;
87
+ private ready;
88
+ private readyResolver;
89
+ constructor(options: MeshWebRTCAdapterOptions);
90
+ isReady(): boolean;
91
+ whenReady(): Promise<void>;
92
+ /**
93
+ * Start the adapter. Wires the signalling client's onSignal callback
94
+ * to the adapter's dispatch, opens the signalling connection if it
95
+ * is not already open, and marks the adapter ready.
96
+ */
97
+ connect(peerId: PeerId, peerMetadata?: PeerMetadata): void;
98
+ disconnect(): void;
99
+ /**
100
+ * Send a sync message to a specific remote peer. If no RTCPeerConnection
101
+ * exists yet, the adapter initiates one by producing an SDP offer and
102
+ * sending it through the signalling channel; the outgoing bytes are
103
+ * queued until the data channel is open.
104
+ */
105
+ send(message: Message): void;
106
+ /**
107
+ * Entry point the signalling client calls when it receives a signal
108
+ * from a remote peer. Dispatches on the payload `kind` to either
109
+ * accept an offer (building an answer), apply an answer, or add an
110
+ * ICE candidate. Exposed publicly so the caller that constructs the
111
+ * signalling client can wire the onSignal callback directly to this
112
+ * method.
113
+ */
114
+ handleSignal(fromPeerId: string, rawPayload: unknown): void;
115
+ private createInitiatingSlot;
116
+ private initiateOffer;
117
+ private handleOffer;
118
+ private handleAnswer;
119
+ private handleIceCandidate;
120
+ private wireConnection;
121
+ private wireDataChannel;
122
+ private dispatchMessage;
123
+ /** Pack an Automerge Message into binary for transmission over the
124
+ * data channel. The format mirrors MeshNetworkAdapter's internal
125
+ * serialisation: a length-prefixed JSON header followed by the raw
126
+ * data bytes. Returns a Uint8Array<ArrayBuffer> so the result is
127
+ * directly usable by RTCDataChannel.send under TypeScript's strict
128
+ * buffer-source typing. */
129
+ private serialiseMessage;
130
+ /** Inverse of {@link serialiseMessage}. */
131
+ private deserialiseMessage;
132
+ }