import type { TransportStats } from '@dsh-remote/protocol' export interface RemoteTransport { connect(): Promise send(data: Uint8Array): Promise onMessage(cb: (data: Uint8Array) => void): () => void onClose?(cb: (error?: Error) => void): () => void close(): Promise getStats(): TransportStats } export interface SecureHandshakeTransport extends RemoteTransport { markSecureReady?(): void connectionInfo(): { connectionId: string; localDeviceId: string; remoteDeviceId: string } sendHandshake(step: number, data: Uint8Array): Promise onHandshake(cb: (step: number, data: Uint8Array) => void): () => void } type MessageHandler = (data: Uint8Array) => void export abstract class BaseTransport implements RemoteTransport { protected handlers = new Set() protected closeHandlers = new Set<(error?: Error) => void>() onMessage(cb: MessageHandler): () => void { this.handlers.add(cb) return () => this.handlers.delete(cb) } onClose(cb: (error?: Error) => void): () => void { this.closeHandlers.add(cb) return () => this.closeHandlers.delete(cb) } protected emit(data: Uint8Array): void { for (const handler of this.handlers) handler(data) } protected emitClose(error?: Error): void { for (const handler of this.closeHandlers) handler(error) } abstract connect(): Promise abstract send(data: Uint8Array): Promise abstract close(): Promise abstract getStats(): TransportStats }