import { WebSocketClient } from './websocket.js'; import { KeyPair, BridgeOptions, BridgeEventType, BridgeEventCallback, FailedToConnectEvent, BridgeDisconnectedEvent } from './types.js'; import 'ws'; /** * BridgeConnection implementation * * A single class that handles both creator and joiner roles and manages its own state */ declare class BridgeConnection { private log; private role; private origin?; private _bridgeOrigin?; private bridgeId; readonly keyPair: KeyPair; private remotePublicKey?; private sharedSecret?; private websocket?; private secureChannelEstablished; private intentionalClose; readonly keepalive: boolean; private reconnect; private reconnectAttempts; private maxReconnectAttempts; private reconnectTimer?; private pingTimer?; private pingInterval; private isReconnecting; private isConnected; private resumedSession; private bridgeUrl?; private validMessagesReceived; private lastMessageTimestamp; private eventListeners; private incompleteMessages; private seenMessageIds; /** * Create a new bridge connection * @param options Connection options */ constructor(options: BridgeOptions); resume(): void; /** * Add an event listener * @param event The event type * @param callback The callback function * @returns Function to remove the event listener */ on(event: T, callback: BridgeEventCallback[T]): () => void; /** * Remove an event listener * @param event The event type * @param callback The callback function */ off(event: T, callback: BridgeEventCallback[T]): void; /** * Emit an event * @param event The event type * @param args The event arguments */ private emit; /** * Set up WebSocket event handlers */ private setupWebSocketHandlers; /** * Handle WebSocket messages based on message type */ private handleWebSocketMessage; /** * Handle handshake message (for creator) */ private handleHandshake; /** * Handle encrypted message */ private handleEncryptedMessage; /** * Handle reconnection logic */ private handleReconnect; /** * Reset reconnection state */ private resetReconnection; /** * Check if the bridge is connected */ isBridgeConnected(): boolean; /** * Check if the secure channel is established */ isSecureChannelEstablished(): boolean; /** * Send a secure message * @param method The message method name * @param params The message parameters */ sendSecureMessage(method: string, params?: any): Promise; /** * Register a callback for when the bridge connects * @returns Function to unsubscribe from the event */ onConnect(callback: (reconnection: boolean) => void): () => void; /** * Register a callback for when the secure channel is established * @returns Function to unsubscribe from the event */ onSecureChannelEstablished(callback: () => void): () => void; /** * Register a callback for when a raw message is received * @returns Function to unsubscribe from the event */ onRawMessage(callback: (message: any) => void): () => void; /** * Register a callback for when a message is received * @returns Function to unsubscribe from the event */ onSecureMessage(callback: (message: any) => void): () => void; /** * Register a callback for when an error occurs * @returns Function to unsubscribe from the event */ onError(callback: (error: string) => void): () => void; /** * Register a callback for when the bridge fails to connect * @returns Function to unsubscribe from the event */ onFailedToConnect(callback: (event: FailedToConnectEvent) => void): () => void; /** * Register a callback for when the bridge disconnects * @returns Function to unsubscribe from the event */ onDisconnect(callback: (event: BridgeDisconnectedEvent) => void): () => void; /** * Set the remote public key */ setRemotePublicKey(publicKey: Uint8Array): void; /** * Compute the shared secret */ computeSharedSecret(): void; /** * Create an encrypted greeting */ createEncryptedGreeting(): Promise; /** * Get the WebSocket connection URL for a joiner * NOTE: If the `moc` (message on connect) param is provided in the WS connection URI, * the bridge server will automatically base64 decode and broadcast it to the bridge on connect * @returns The WebSocket connection URL */ _getWsConnectionUrl(): Promise; /** * Get the public key as a hex string */ getPublicKey(): string; /** * Get the remote public key as a hex string */ getRemotePublicKey(): string; /** * Get the bridge ID */ getBridgeId(): string; /** * Get the WebSocket client */ getWebSocket(): WebSocketClient | undefined; /** * Get the bridge origin (the origin of the creator) */ get bridgeOrigin(): string; /** * Get a connection string URI for joining the bridge */ get connectionString(): string; /** * Connect to the bridge service */ connect(url: string): Promise; private _handleCleanup; /** * Close the bridge and cleanup all event listeners and associated state */ cleanup(): void; } export { BridgeConnection };