| 31 | const reader = res.body.getReader() |
| 32 | const body = new ReadableStream<Uint8Array>({ |
| 33 | async pull(ctrl) { |
| 34 | const part = await new Promise<Awaited<ReturnType<typeof reader.read>>>((resolve, reject) => { |
| 35 | const id = setTimeout(() => { |
| 36 | const err = new Error("SSE read timed out") |
| 37 | ctl.abort(err) |
| 38 | void reader.cancel(err) |
| 39 | reject(err) |
| 40 | }, ms) |
| 41 | |
| 42 | reader.read().then( |
| 43 | (part) => { |
| 44 | clearTimeout(id) |
| 45 | resolve(part) |
| 46 | }, |
| 47 | (err) => { |
| 48 | clearTimeout(id) |
| 49 | reject(err) |
| 50 | }, |
| 51 | ) |
| 52 | }) |
| 53 | |
| 54 | if (part.done) { |
| 55 | ctrl.close() |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | ctrl.enqueue(part.value) |
| 60 | }, |
| 61 | async cancel(reason) { |
| 62 | ctl.abort(reason) |
| 63 | await reader.cancel(reason) |