(
url: string,
onProgress: ZipProgressCallback,
options: ZipDownloadOptions = {}
)
| 28 | * Download an archive file from URL with progress tracking. |
| 29 | */ |
| 30 | export async function downloadZip( |
| 31 | url: string, |
| 32 | onProgress: ZipProgressCallback, |
| 33 | options: ZipDownloadOptions = {} |
| 34 | ): Promise<Blob> { |
| 35 | const fetchImpl = options.fetchImpl ?? fetchWithTimeout; |
| 36 | const timeoutMs = options.timeoutMs ?? DOWNLOAD_TIMEOUT; |
| 37 | |
| 38 | onProgress({ percent: 2, message: 'Starting download...' }); |
| 39 | |
| 40 | const response = await fetchImpl(url, timeoutMs); |
| 41 | |
| 42 | if (!response.ok) { |
| 43 | throw new Error(`Failed to download archive (${response.status})`); |
| 44 | } |
| 45 | |
| 46 | const contentLength = response.headers.get('content-length'); |
| 47 | const total = contentLength ? parseSafeIntegerString(contentLength) ?? 0 : 0; |
| 48 | |
| 49 | if (!response.body) { |
| 50 | return response.blob(); |
| 51 | } |
| 52 | |
| 53 | const blob = await readStreamingResponseBlob(response.body, total, onProgress); |
| 54 | validateDownloadedArchiveSize(blob, options.sizeLimit); |
| 55 | return blob; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Validate downloaded archive size after streaming or fallback blob creation. |
no test coverage detected