MCPcopy Index your code
hub / github.com/codeaashu/claude-code / WebSocketTransport

Class WebSocketTransport

src/cli/transports/WebSocketTransport.ts:74–800  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72}
73
74export 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

Callers

nothing calls this directly

Calls 7

handleOpenEventMethod · 0.95
handleConnectionErrorMethod · 0.95
logForDiagnosticsNoPIIFunction · 0.85
logForDebuggingFunction · 0.85
onDataMethod · 0.80
toStringMethod · 0.65

Tested by

no test coverage detected