* Read a fetch response body to a Blob, reporting bytes as they arrive. Builds * the Blob directly from the chunk array (no intermediate full-buffer copy) to * keep peak memory near one copy of the file.
( body: ReadableStream<Uint8Array>, total: number, onProgress: DownloadProgressCallback )
| 154 | * keep peak memory near one copy of the file. |
| 155 | */ |
| 156 | async function readResponseStreamToBlob( |
| 157 | body: ReadableStream<Uint8Array>, |
| 158 | total: number, |
| 159 | onProgress: DownloadProgressCallback |
| 160 | ): Promise<Blob> { |
| 161 | const reader = body.getReader(); |
| 162 | const chunks: Uint8Array<ArrayBuffer>[] = []; |
| 163 | let loaded = 0; |
| 164 | onProgress(0, total); |
| 165 | try { |
| 166 | for (;;) { |
| 167 | const { done, value } = await reader.read(); |
| 168 | if (done) break; |
| 169 | // Copy into an ArrayBuffer-backed view: detaches from the stream buffer and |
| 170 | // satisfies the BlobPart type (the raw chunk is Uint8Array<ArrayBufferLike>). |
| 171 | chunks.push(new Uint8Array(value)); |
| 172 | loaded += value.length; |
| 173 | onProgress(loaded, total); |
| 174 | } |
| 175 | } finally { |
| 176 | // Tear down the stream if the loop exits abnormally (read rejection, or a |
| 177 | // throwing progress callback) so the underlying connection isn't left open. |
| 178 | // A no-op once the stream is fully drained or already errored. |
| 179 | reader.cancel().catch(() => {}); |
| 180 | } |
| 181 | return new Blob(chunks); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Extract filename from a URL path, percent-decoded so it matches the |
no outgoing calls
no test coverage detected