@dimchansky/meshpost 0.2.0 → 0.4.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/room.d.ts CHANGED
@@ -1,14 +1,17 @@
1
+ import { type PeerIdentity } from './identity.js';
1
2
  import { type Signaling, type SignalingErrorCode, type SignalingState } from './signaling.js';
2
3
  /** Built-in Cloudflare and Google public STUN servers used by default. */
3
4
  export declare const defaultIceServers: readonly RTCIceServer[];
4
5
  /** Delivery guarantee for a packet: fast unordered (`'unreliable'`) or ordered reliable (`'reliable'`). */
5
6
  export type Delivery = 'unreliable' | 'reliable';
7
+ /** Constructor signature for custom or headless WebRTC `RTCPeerConnection` implementations. */
8
+ export type RTCPeerConnectionConstructor = new (configuration?: RTCConfiguration) => RTCPeerConnection;
6
9
  export interface RoomOptions {
7
10
  /** Signaling adapter used to discover peers and exchange WebRTC setup messages. */
8
11
  signaling: Signaling;
9
12
  /** Application namespace isolating rooms across different apps. @default 'meshpost' */
10
13
  appId?: string;
11
- /** Optional non-empty shared password isolating (and on MQTT, encrypting) room signaling. */
14
+ /** Optional non-empty shared password isolating and end-to-end encrypting room signaling. */
12
15
  password?: string;
13
16
  /** Custom WebRTC configuration. Keeps `defaultIceServers` unless `iceServers` is specified. */
14
17
  rtcConfig?: RTCConfiguration;
@@ -20,6 +23,10 @@ export interface RoomOptions {
20
23
  iceGatheringTimeoutMs?: number;
21
24
  /** Maximum simultaneous peer connections in this room. @default 32 */
22
25
  maxPeers?: number;
26
+ /** Optional persistent WebCrypto ECDSA P-256 identity proven to peers over each WebRTC link. */
27
+ identity?: PeerIdentity;
28
+ /** Optional custom `RTCPeerConnection` constructor for headless runtimes (Node.js, Bun, Deno) or tests. */
29
+ RTCPeerConnection?: RTCPeerConnectionConstructor;
23
30
  }
24
31
  export interface SendOptions {
25
32
  /** Optional target peer ID. Omit to broadcast to all connected peers. */
@@ -47,7 +54,7 @@ export interface PeerStats {
47
54
  readonly bufferedReliable: number;
48
55
  }
49
56
  /** Structured error codes emitted via `room.onError()`. */
50
- export type RoomErrorCode = 'room-full' | 'ice-failed' | 'media-error' | SignalingErrorCode;
57
+ export type RoomErrorCode = 'room-full' | 'ice-failed' | 'media-error' | 'auth-failed' | SignalingErrorCode;
51
58
  /** Structured diagnostic error emitted via `room.onError()`. */
52
59
  export interface RoomError {
53
60
  readonly code: RoomErrorCode;
@@ -61,13 +68,34 @@ export interface ListenOptions {
61
68
  /** Automatically unsubscribe when the given AbortSignal aborts. */
62
69
  signal?: AbortSignal;
63
70
  }
71
+ /** Options passed to `room.addStream(stream, options)`. */
72
+ export interface AddStreamOptions {
73
+ /** Optional target peer ID. Omit to broadcast to all connected and future peers. */
74
+ to?: string;
75
+ /** Optional semantic stream tag (`1..32` ASCII characters `[a-zA-Z0-9._-]`, e.g. `'voice'`, `'screen'`). */
76
+ tag?: string;
77
+ }
78
+ /** Options passed to `room.removeStream(stream, options)`. */
79
+ export interface RemoveStreamOptions {
80
+ /** Optional target peer ID. Omit to detach from broadcast. */
81
+ to?: string;
82
+ }
83
+ /** Metadata describing an incoming remote `MediaStream` in `onPeerStream` and `onPeerStreamEnd`. */
84
+ export interface PeerStreamMeta {
85
+ /** Semantic tag passed to `addStream(stream, { tag })` by the sender, if any. */
86
+ readonly tag?: string;
87
+ }
64
88
  export interface Room {
65
89
  /** Local peer ID (random UUID v4). A page reload creates a new peer ID. */
66
90
  readonly id: string;
91
+ /** Local verified `userId` when `options.identity` was provided, or `null` if anonymous. */
92
+ readonly userId: string | null;
67
93
  /** Snapshot array of currently connected remote peer IDs. */
68
94
  readonly peers: readonly string[];
69
95
  /** Current state of the signaling adapter. */
70
96
  readonly signalingState: SignalingState;
97
+ /** Return the cryptographically verified `userId` of a connected remote peer, or `null` if anonymous or disconnected. */
98
+ peerUserId(peerId: string): string | null;
71
99
  /** Send a binary packet (max 16 KiB) synchronously. Returns a per-recipient result map. */
72
100
  send(bytes: Uint8Array, options?: SendOptions): ReadonlyMap<string, SendResult>;
73
101
  /** Read current ping, route, byte counts, and buffer sizes for a connected peer, or `null` if unavailable. */
@@ -84,14 +112,16 @@ export interface Room {
84
112
  onSignalingChange(fn: (state: SignalingState, reason?: string) => void, options?: ListenOptions): () => void;
85
113
  /** Subscribe to structured room and signaling errors. Returns an unsubscribe function. */
86
114
  onError(fn: (error: RoomError) => void, options?: ListenOptions): () => void;
87
- /** Attach a local MediaStream to all connected/future peers (broadcast), or selectively to one peer (`to`). Broadcast and per-peer registrations are tracked independently. */
88
- addStream(stream: MediaStream, to?: string): void;
115
+ /** Attach a local MediaStream to all connected/future peers (broadcast), or selectively to one peer (`to`), optionally carrying a semantic `tag`. */
116
+ addStream(stream: MediaStream, toOrOptions?: string | AddStreamOptions): void;
89
117
  /** Detach a previously added local MediaStream from broadcast (`to` omitted) or from one peer (`to`). Does not stop local tracks. */
90
- removeStream(stream: MediaStream, to?: string): void;
118
+ removeStream(stream: MediaStream, toOrOptions?: string | RemoveStreamOptions): void;
119
+ /** Hot-swap a track inside an already-added local MediaStream via `RTCRtpSender.replaceTrack()` without SDP renegotiation. Does not stop `oldTrack`. */
120
+ replaceStreamTrack(stream: MediaStream, oldTrack: MediaStreamTrack, newTrack: MediaStreamTrack): Promise<void>;
91
121
  /** Subscribe to incoming remote MediaStreams. Fires strictly after `onPeerJoin`. */
92
- onPeerStream(fn: (peerId: string, stream: MediaStream) => void, options?: ListenOptions): () => void;
122
+ onPeerStream(fn: (peerId: string, stream: MediaStream, meta: PeerStreamMeta) => void, options?: ListenOptions): () => void;
93
123
  /** Subscribe to remote MediaStream removals (explicit removal or peer disconnect). */
94
- onPeerStreamEnd(fn: (peerId: string, stream: MediaStream) => void, options?: ListenOptions): () => void;
124
+ onPeerStreamEnd(fn: (peerId: string, stream: MediaStream, meta: PeerStreamMeta) => void, options?: ListenOptions): () => void;
95
125
  /** Leave the room, notify peers, and close all connections. Safe to call more than once. */
96
126
  leave(): void;
97
127
  }