( resolveUrl: string, headers: Record<string, string>, signal: AbortSignal | undefined, onProgress?: (progress: FetchProgress) => void, )
| 191 | * with progress reporting based on the known content length. |
| 192 | */ |
| 193 | export async function fetchResolvedArchive( |
| 194 | resolveUrl: string, |
| 195 | headers: Record<string, string>, |
| 196 | signal: AbortSignal | undefined, |
| 197 | onProgress?: (progress: FetchProgress) => void, |
| 198 | ): Promise<ArchiveResult> { |
| 199 | // Step 1: resolve → { url, contentLength, sha, branch, commitMessage, ... } |
| 200 | const resolveRes = await fetch(resolveUrl, { headers, signal }); |
| 201 | if (!resolveRes.ok) { |
| 202 | const text = await resolveRes.text(); |
| 203 | throw new Error(`Archive resolve error ${resolveRes.status}: ${text}`); |
| 204 | } |
| 205 | const resolved = (await resolveRes.json()) as ResolveResult; |
| 206 | const { url, contentLength } = resolved; |
| 207 | const gitMeta: ArchiveGitMeta = { |
| 208 | sha: resolved.sha, |
| 209 | branch: resolved.branch, |
| 210 | commitMessage: resolved.commitMessage, |
| 211 | authorName: resolved.authorName, |
| 212 | authorDate: resolved.authorDate, |
| 213 | }; |
| 214 | |
| 215 | // Step 2: fetch the actual zip, streaming for progress |
| 216 | const zipRes = await fetch(url, { headers, signal }); |
| 217 | if (!zipRes.ok) { |
| 218 | const text = await zipRes.text(); |
| 219 | throw new Error(`Archive download error ${zipRes.status}: ${text}`); |
| 220 | } |
| 221 | |
| 222 | const total = contentLength || 0; |
| 223 | |
| 224 | // If no ReadableStream body (unlikely in modern browsers), fall back |
| 225 | if (!zipRes.body) { |
| 226 | const buf = new Uint8Array(await zipRes.arrayBuffer()); |
| 227 | onProgress?.({ phase: 'download', current: buf.length, total: buf.length }); |
| 228 | return { data: buf, gitMeta }; |
| 229 | } |
| 230 | |
| 231 | // Stream with progress |
| 232 | const reader = zipRes.body.getReader(); |
| 233 | const chunks: Uint8Array[] = []; |
| 234 | let received = 0; |
| 235 | |
| 236 | for (;;) { |
| 237 | const { done, value } = await reader.read(); |
| 238 | if (done) break; |
| 239 | chunks.push(value); |
| 240 | received += value.length; |
| 241 | onProgress?.({ phase: 'download', current: received, total }); |
| 242 | } |
| 243 | |
| 244 | // Concatenate chunks |
| 245 | const buf = new Uint8Array(received); |
| 246 | let offset = 0; |
| 247 | for (const chunk of chunks) { |
| 248 | buf.set(chunk, offset); |
| 249 | offset += chunk.length; |
| 250 | } |
no test coverage detected