( body: ReadableStream<Uint8Array>, total: number, onProgress: ZipProgressCallback )
| 69 | } |
| 70 | |
| 71 | async function readStreamingResponseBlob( |
| 72 | body: ReadableStream<Uint8Array>, |
| 73 | total: number, |
| 74 | onProgress: ZipProgressCallback |
| 75 | ): Promise<Blob> { |
| 76 | const reader = body.getReader(); |
| 77 | const chunks: Uint8Array[] = []; |
| 78 | let loaded = 0; |
| 79 | |
| 80 | while (true) { |
| 81 | const { done, value } = await reader.read(); |
| 82 | if (done) break; |
| 83 | |
| 84 | chunks.push(new Uint8Array(value)); |
| 85 | loaded += value.length; |
| 86 | onProgress(getDownloadProgress(loaded, total)); |
| 87 | } |
| 88 | |
| 89 | const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0); |
| 90 | const result = new Uint8Array(totalLength); |
| 91 | let offset = 0; |
| 92 | for (const chunk of chunks) { |
| 93 | result.set(chunk, offset); |
| 94 | offset += chunk.length; |
| 95 | } |
| 96 | |
| 97 | return new Blob([result]); |
| 98 | } |
| 99 | |
| 100 | function getDownloadProgress(loaded: number, total: number): ZipProgress { |
| 101 | if (total > 0) { |
no test coverage detected