(
stream: ReadableStream<Uint8Array>,
onChunk: (chunk: string) => void,
)
| 187 | } |
| 188 | |
| 189 | async function drainStream( |
| 190 | stream: ReadableStream<Uint8Array>, |
| 191 | onChunk: (chunk: string) => void, |
| 192 | ): Promise<string> { |
| 193 | const reader = stream.getReader() |
| 194 | const decoder = new TextDecoder() |
| 195 | let text = '' |
| 196 | |
| 197 | while (true) { |
| 198 | const { done, value } = await reader.read() |
| 199 | if (done) { |
| 200 | const chunk = decoder.decode() |
| 201 | if (chunk.length > 0) { |
| 202 | text += chunk |
| 203 | onChunk(chunk) |
| 204 | } |
| 205 | return text |
| 206 | } |
| 207 | |
| 208 | const chunk = decoder.decode(value, { stream: true }) |
| 209 | text += chunk |
| 210 | onChunk(chunk) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | function onText(chunk: string): void { |
| 215 | rawText += chunk |
no test coverage detected