| 31 | let source: Readable | undefined |
| 32 | return new Readable({ |
| 33 | read() { |
| 34 | if (source === undefined) { |
| 35 | try { |
| 36 | source = evaluate() |
| 37 | } catch (cause) { |
| 38 | this.destroy(cause instanceof Error ? cause : new Error(String(cause))) |
| 39 | return |
| 40 | } |
| 41 | source.on("data", (chunk) => { |
| 42 | if (!this.push(chunk)) source!.pause() |
| 43 | }) |
| 44 | source.once("end", () => this.push(null)) |
| 45 | source.once("error", (cause) => this.destroy(cause)) |
| 46 | source.once("close", () => { |
| 47 | if (!source!.readableEnded) this.destroy(new Error("Response stream closed prematurely")) |
| 48 | }) |
| 49 | } |
| 50 | source.resume() |
| 51 | }, |
| 52 | destroy(error, callback) { |
| 53 | source?.destroy(error ?? undefined) |
| 54 | onDestroy?.() |