(response: Response)
| 263 | } |
| 264 | |
| 265 | async function readResponseSnippet(response: Response): Promise<string> { |
| 266 | const reader = response.body?.getReader() |
| 267 | |
| 268 | if (!reader) { |
| 269 | return (await response.text()).slice(0, TITLE_BYTE_BUDGET) |
| 270 | } |
| 271 | |
| 272 | const chunks: Uint8Array[] = [] |
| 273 | let done = false |
| 274 | let bytes = 0 |
| 275 | |
| 276 | try { |
| 277 | while (bytes < TITLE_BYTE_BUDGET) { |
| 278 | const chunk = await reader.read() |
| 279 | |
| 280 | if (chunk.done) { |
| 281 | done = true |
| 282 | |
| 283 | break |
| 284 | } |
| 285 | |
| 286 | const value = chunk.value |
| 287 | |
| 288 | if (!value?.length) { |
| 289 | continue |
| 290 | } |
| 291 | |
| 292 | const remaining = TITLE_BYTE_BUDGET - bytes |
| 293 | const next = value.length > remaining ? value.subarray(0, remaining) : value |
| 294 | |
| 295 | chunks.push(next) |
| 296 | bytes += next.length |
| 297 | |
| 298 | if (next.length < value.length) { |
| 299 | break |
| 300 | } |
| 301 | } |
| 302 | } catch { |
| 303 | return '' |
| 304 | } finally { |
| 305 | if (!done) { |
| 306 | try { |
| 307 | await reader.cancel() |
| 308 | } catch { |
| 309 | // Ignore stream teardown failures. |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (!chunks.length) { |
| 315 | return '' |
| 316 | } |
| 317 | |
| 318 | const joined = new Uint8Array(bytes) |
| 319 | let offset = 0 |
| 320 | |
| 321 | for (const chunk of chunks) { |
| 322 | joined.set(chunk, offset) |
no test coverage detected