(span: Span, res: Response)
| 48 | * response to a client. |
| 49 | */ |
| 50 | export async function streamResponse(span: Span, res: Response): Promise<Response> { |
| 51 | const classification = classifyResponseStreaming(res); |
| 52 | |
| 53 | // not streaming, just end the span and return the response |
| 54 | if (!classification.isStreaming || !res.body) { |
| 55 | span.end(); |
| 56 | return res; |
| 57 | } |
| 58 | |
| 59 | // Streaming response detected - monitor consumption to keep span alive |
| 60 | try { |
| 61 | return new Response( |
| 62 | monitorStream(res.body, () => span.end()), |
| 63 | { |
| 64 | status: res.status, |
| 65 | statusText: res.statusText, |
| 66 | headers: res.headers, |
| 67 | }, |
| 68 | ); |
| 69 | } catch (_e) { |
| 70 | // tee() failed - handle without streaming |
| 71 | span.end(); |
| 72 | return res; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * zero-copy monitoring of stream progress. |
no test coverage detected