| 72 | } |
| 73 | |
| 74 | export class WebSocketTransport implements Transport { |
| 75 | private ws: WebSocketLike | null = null |
| 76 | private lastSentId: string | null = null |
| 77 | protected url: URL |
| 78 | protected state: WebSocketTransportState = 'idle' |
| 79 | protected onData?: (data: string) => void |
| 80 | private onCloseCallback?: (closeCode?: number) => void |
| 81 | private onConnectCallback?: () => void |
| 82 | private headers: Record<string, string> |
| 83 | private sessionId?: string |
| 84 | private autoReconnect: boolean |
| 85 | private isBridge: boolean |
| 86 | |
| 87 | // Reconnection state |
| 88 | private reconnectAttempts = 0 |
| 89 | private reconnectStartTime: number | null = null |
| 90 | private reconnectTimer: NodeJS.Timeout | null = null |
| 91 | private lastReconnectAttemptTime: number | null = null |
| 92 | // Wall-clock of last WS data-frame activity (inbound message or outbound |
| 93 | // ws.send). Used to compute idle time at close — the signal for diagnosing |
| 94 | // proxy idle-timeout RSTs (e.g. Cloudflare 5-min). Excludes ping/pong |
| 95 | // control frames (proxies don't count those). |
| 96 | private lastActivityTime = 0 |
| 97 | |
| 98 | // Ping interval for connection health checks |
| 99 | private pingInterval: NodeJS.Timeout | null = null |
| 100 | private pongReceived = true |
| 101 | |
| 102 | // Periodic keep_alive data frames to reset proxy idle timers |
| 103 | private keepAliveInterval: NodeJS.Timeout | null = null |
| 104 | |
| 105 | // Message buffering for replay on reconnection |
| 106 | private messageBuffer: CircularBuffer<StdoutMessage> |
| 107 | // Track which runtime's WS we're using so we can detach listeners |
| 108 | // with the matching API (removeEventListener vs. off). |
| 109 | private isBunWs = false |
| 110 | |
| 111 | // Captured at connect() time for handleOpenEvent timing. Stored as an |
| 112 | // instance field so the onOpen handler can be a stable class-property |
| 113 | // arrow function (removable in doDisconnect) instead of a closure over |
| 114 | // a local variable. |
| 115 | private connectStartTime = 0 |
| 116 | |
| 117 | private refreshHeaders?: () => Record<string, string> |
| 118 | |
| 119 | constructor( |
| 120 | url: URL, |
| 121 | headers: Record<string, string> = {}, |
| 122 | sessionId?: string, |
| 123 | refreshHeaders?: () => Record<string, string>, |
| 124 | options?: WebSocketTransportOptions, |
| 125 | ) { |
| 126 | this.url = url |
| 127 | this.headers = headers |
| 128 | this.sessionId = sessionId |
| 129 | this.refreshHeaders = refreshHeaders |
| 130 | this.autoReconnect = options?.autoReconnect ?? true |
| 131 | this.isBridge = options?.isBridge ?? false |
nothing calls this directly
no test coverage detected