(data: WebSocket.RawData, isBinary: boolean)
| 178 | } |
| 179 | |
| 180 | async function onMessage(data: WebSocket.RawData, isBinary: boolean) { |
| 181 | if (completed) return |
| 182 | if (isBinary) { |
| 183 | invalidate(new ProviderError.ResponseStreamError("Unexpected binary WebSocket frame")) |
| 184 | return |
| 185 | } |
| 186 | |
| 187 | const text = data.toString() |
| 188 | const event = (() => { |
| 189 | try { |
| 190 | const parsed = JSON.parse(text) |
| 191 | return typeof parsed === "object" && parsed !== null ? parsed : undefined |
| 192 | } catch { |
| 193 | return undefined |
| 194 | } |
| 195 | })() |
| 196 | |
| 197 | if (event?.type === "error" && options.onRetryableTerminal) { |
| 198 | cleanupSocket() |
| 199 | if (idleTimer) clearTimeout(idleTimer) |
| 200 | idleTimer = undefined |
| 201 | try { |
| 202 | const next = await options.onRetryableTerminal(event) |
| 203 | if (completed) { |
| 204 | if (next) terminateSocket(next) |
| 205 | return |
| 206 | } |
| 207 | if (next) { |
| 208 | attach(next) |
| 209 | return |
| 210 | } |
| 211 | } catch (error) { |
| 212 | invalidate( |
| 213 | new ProviderError.ResponseStreamError(error instanceof Error ? error.message : String(error), { |
| 214 | cause: error, |
| 215 | }), |
| 216 | ) |
| 217 | return |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | const wrappedError = parseWrappedError(event, text) |
| 222 | if (wrappedError && event) { |
| 223 | if (!emitted) options.onFirstEvent?.(wrappedError) |
| 224 | completed = true |
| 225 | cleanup() |
| 226 | options.onTerminal?.(event) |
| 227 | controller?.error( |
| 228 | new APICallError({ |
| 229 | message: wrappedError.message, |
| 230 | url: socket.url, |
| 231 | requestBodyValues: options.body, |
| 232 | statusCode: wrappedError.status, |
| 233 | responseHeaders: wrappedError.headers, |
| 234 | responseBody: wrappedError.body, |
| 235 | }), |
| 236 | ) |
| 237 | return |
nothing calls this directly
no test coverage detected