( response: Response, )
| 8 | } |
| 9 | |
| 10 | export async function* streamResponse( |
| 11 | response: Response, |
| 12 | ): AsyncGenerator<string> { |
| 13 | if (response.status === 499) { |
| 14 | return; // In case of client-side cancellation, just return |
| 15 | } |
| 16 | |
| 17 | if (response.status !== 200) { |
| 18 | throw new Error(await response.text()); |
| 19 | } |
| 20 | |
| 21 | if (!response.body) { |
| 22 | throw new Error("No response body returned."); |
| 23 | } |
| 24 | |
| 25 | // Get the major version of Node.js |
| 26 | const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10); |
| 27 | let chunks = 0; |
| 28 | |
| 29 | try { |
| 30 | if (nodeMajorVersion >= 20) { |
| 31 | // Use the new API for Node 20 and above |
| 32 | const stream = (ReadableStream as any).from(response.body); |
| 33 | for await (const chunk of stream.pipeThrough( |
| 34 | new TextDecoderStream("utf-8"), |
| 35 | )) { |
| 36 | yield chunk; |
| 37 | chunks++; |
| 38 | } |
| 39 | } else { |
| 40 | // Fallback for Node versions below 20 |
| 41 | // Streaming with this method doesn't work as version 20+ does |
| 42 | const decoder = new TextDecoder("utf-8"); |
| 43 | const nodeStream = response.body as unknown as NodeJS.ReadableStream; |
| 44 | for await (const chunk of toAsyncIterable(nodeStream)) { |
| 45 | yield decoder.decode(chunk, { stream: true }); |
| 46 | chunks++; |
| 47 | } |
| 48 | } |
| 49 | } catch (e) { |
| 50 | if (e instanceof Error) { |
| 51 | if (e.name.startsWith("AbortError")) { |
| 52 | return; // In case of client-side cancellation, just return |
| 53 | } |
| 54 | if (e.message.toLowerCase().includes("premature close")) { |
| 55 | // Premature close can happen for various reasons, including: |
| 56 | // - Malformed chunks of data received from the server |
| 57 | // - The server closed the connection before sending the complete response |
| 58 | // - Long delays from the server during streaming |
| 59 | // - 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets |
| 60 | if (chunks === 0) { |
| 61 | throw new Error( |
| 62 | "Stream was closed before any data was received. Try again. (Premature Close)", |
| 63 | ); |
| 64 | } else { |
| 65 | throw new Error( |
| 66 | "The response was cancelled mid-stream. Try again. (Premature Close).", |
| 67 | ); |
no test coverage detected