( ref: RepoRef, callbacks: RepoFetchCallbacks )
| 40 | }; |
| 41 | |
| 42 | export async function fetchRepositoryFiles( |
| 43 | ref: RepoRef, |
| 44 | callbacks: RepoFetchCallbacks |
| 45 | ): Promise<FetchedRepoFiles> { |
| 46 | const { onProgressText, onProgressValue, isPathIgnored, fetchWithProgress, fetchWithFallbackProxies } = |
| 47 | callbacks; |
| 48 | |
| 49 | let files: { path: string; content: string }[] = []; |
| 50 | let fileContents: Record<string, string> = {}; |
| 51 | |
| 52 | const { detectedBranch, latestCommitSha: initialCommitSha } = await resolveRepoMetadata(ref); |
| 53 | let latestCommitSha = initialCommitSha; |
| 54 | const branchesToTry = getBranchesToTry(detectedBranch); |
| 55 | const { estimatedZipSize, isEstimateReliable } = await estimateZipSize(ref, branchesToTry); |
| 56 | |
| 57 | const updateDownloadProgress = (loaded: number, total: number) => { |
| 58 | const mbLoaded = (loaded / 1024 / 1024).toFixed(2); |
| 59 | const finalTotal = total > 0 ? total : estimatedZipSize; |
| 60 | |
| 61 | let pct = 0; |
| 62 | if (loaded < finalTotal) { |
| 63 | pct = Math.round((loaded / finalTotal) * 90); |
| 64 | } else { |
| 65 | const overflow = loaded - finalTotal; |
| 66 | const extraPct = 9 * (1 - Math.exp(-overflow / (1024 * 1024 * 5))); |
| 67 | pct = Math.round(90 + extraPct); |
| 68 | } |
| 69 | |
| 70 | if (total > 0) { |
| 71 | onProgressText( |
| 72 | `Downloading repository archive: ${pct}% (${mbLoaded} MB of ${(total / 1024 / 1024).toFixed(2)} MB)` |
| 73 | ); |
| 74 | } else if (isEstimateReliable) { |
| 75 | onProgressText( |
| 76 | `Downloading repository archive: ${pct}% (${mbLoaded} MB of ~${(estimatedZipSize / 1024 / 1024).toFixed(2)} MB)` |
| 77 | ); |
| 78 | } else { |
| 79 | onProgressText(`Downloading repository archive: ${pct}% (${mbLoaded} MB)`); |
| 80 | } |
| 81 | onProgressValue(10 + Math.floor(pct * 0.15)); |
| 82 | }; |
| 83 | |
| 84 | try { |
| 85 | onProgressText("Downloading repository archive..."); |
| 86 | onProgressValue(10); |
| 87 | |
| 88 | let response: Response | null = null; |
| 89 | let zipSuccessBranch = detectedBranch; |
| 90 | const authHeaders = getAuthHeaders(ref); |
| 91 | |
| 92 | if (localStorage.getItem(getAuthTokenKey(ref))) { |
| 93 | for (const branch of branchesToTry) { |
| 94 | try { |
| 95 | const zipUrl = getAuthenticatedZipUrl(ref, branch); |
| 96 | if (!zipUrl) continue; |
| 97 | const tempRes = await fetch(zipUrl, { headers: authHeaders }); |
| 98 | if (tempRes.ok) { |
| 99 | response = tempRes; |
no test coverage detected