/** * Peer - Clean DX wrapper for peer-to-peer connections * * Provides a simple interface for connecting to a peer by tags/publicKey, * with automatic reconnection and message buffering. */ import { EventEmitter } from 'eventemitter3'; import { RondevuAPI } from '../api/client.js'; import { AnswererConnection } from '../connections/answerer.js'; import { ConnectionConfig } from '../connections/config.js'; import { WebRTCAdapter } from '../webrtc/adapter.js'; /** * Simplified peer state (maps from ConnectionState) */ export type PeerState = 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'failed' | 'closed'; /** * Event map for Peer */ export interface PeerEventMap { /** Emitted when connection state changes */ state: [state: PeerState, previousState: PeerState]; /** Emitted when connection is established */ open: []; /** Emitted when connection is closed */ close: [reason?: string]; /** Emitted when a message is received */ message: [data: string | ArrayBuffer | Blob]; /** Emitted when an error occurs */ error: [error: Error]; /** Emitted when reconnection is attempted */ reconnecting: [attempt: number, maxAttempts: number]; } export type PeerEventName = keyof PeerEventMap; /** * Options for creating a Peer connection */ export interface PeerOptions { /** Tags to match for peer discovery */ tags: string[]; /** Optional: connect to specific public key */ publicKey?: string; /** Optional: custom RTC configuration */ rtcConfig?: RTCConfiguration; /** Optional: connection behavior configuration */ config?: Partial; /** * Optional callback invoked when RTCPeerConnection is created. * Use this to create negotiated data channels that must exist * before the connection opens (e.g., control channels). */ onPeerConnectionCreated?: (pc: RTCPeerConnection) => void; } /** * Internal options passed from Rondevu */ export interface PeerInternalOptions extends PeerOptions { api: RondevuAPI; iceServers: RTCIceServer[]; iceTransportPolicy?: RTCIceTransportPolicy; webrtcAdapter?: WebRTCAdapter; debug?: boolean; } /** * Peer - A clean interface for peer-to-peer connections * * @example * ```typescript * const peer = await rondevu.peer({ * publicKey: 'abc123...', * tags: ['chat'] * }) * * peer.on('open', () => { * console.log('Connected to', peer.peerPublicKey) * peer.send('Hello!') * }) * * peer.on('message', (data) => { * console.log('Received:', data) * }) * * peer.on('state', (state) => { * console.log('Connection state:', state) * }) * * // Access underlying WebRTC objects * const pc = peer.peerConnection // RTCPeerConnection * const dc = peer.dataChannel // RTCDataChannel * ``` */ export declare class Peer extends EventEmitter { private connection; private api; private tags; private targetPublicKey?; private iceServers; private iceTransportPolicy?; private webrtcAdapter?; private connectionConfig?; private debugEnabled; private onPeerConnectionCreated?; private _state; private _peerPublicKey; private _offerId; constructor(options: PeerInternalOptions); /** * Initialize the peer connection (called internally by Rondevu.peer()) */ initialize(): Promise; /** * Setup event handlers to forward from AnswererConnection */ private setupEventHandlers; /** * Map internal ConnectionState to simplified PeerState */ private mapState; /** * Current connection state */ get state(): PeerState; /** * Public key of the connected peer */ get peerPublicKey(): string; /** * The offer ID being used for this connection */ get offerId(): string; /** * Tags used for discovery */ get peerTags(): string[]; /** * The underlying RTCPeerConnection (null if not connected) */ get peerConnection(): RTCPeerConnection | null; /** * The underlying RTCDataChannel (null if not connected) */ get dataChannel(): RTCDataChannel | null; /** * Whether the peer is currently connected */ get isConnected(): boolean; /** * Send a message to the peer * Messages are buffered if not connected (when buffering is enabled) * * @param data - String, ArrayBuffer, or Blob to send */ send(data: string | ArrayBuffer | Blob): void; /** * Close the peer connection */ close(): void; /** * Get the underlying AnswererConnection for advanced use cases */ getConnection(): AnswererConnection | null; /** * Debug logging */ private debug; }