|
| 1 | +import {type ContentsReponseObject, type TreeResponseObject} from 'list-github-dir-content'; |
| 2 | +import pRetry, {type FailedAttemptError} from 'p-retry'; |
| 3 | +import authenticatedFetch from './authenticated-fetch.js'; |
| 4 | + |
| 5 | +function escapeFilepath(path: string) { |
| 6 | + return path.replaceAll('#', '%23'); |
| 7 | +} |
| 8 | + |
| 9 | +async function maybeResponseLfs(response: Response): Promise<boolean> { |
| 10 | + const length = Number(response.headers.get('content-length')); |
| 11 | + if (length > 128 && length < 140) { |
| 12 | + const contents = await response.clone().text(); |
| 13 | + return contents.startsWith('version https://git-lfs.github.com/spec/v1'); |
| 14 | + } |
| 15 | + |
| 16 | + return false; |
| 17 | +} |
| 18 | + |
| 19 | +type FileRequest = { |
| 20 | + user: string; |
| 21 | + repository: string; |
| 22 | + reference: string; |
| 23 | + file: TreeResponseObject | ContentsReponseObject; |
| 24 | + signal: AbortSignal; |
| 25 | +}; |
| 26 | + |
| 27 | +async function fetchPublicFile({ |
| 28 | + user, |
| 29 | + repository, |
| 30 | + reference, |
| 31 | + file, |
| 32 | + signal, |
| 33 | +}: FileRequest) { |
| 34 | + const response = await authenticatedFetch( |
| 35 | + `https://raw.githubusercontent.com/${user}/${repository}/${reference}/${escapeFilepath(file.path)}`, |
| 36 | + {signal}, |
| 37 | + ); |
| 38 | + |
| 39 | + if (!response.ok) { |
| 40 | + throw new Error(`HTTP ${response.statusText} for ${file.path}`); |
| 41 | + } |
| 42 | + |
| 43 | + const lfsCompatibleResponse = (await maybeResponseLfs(response)) |
| 44 | + ? await authenticatedFetch( |
| 45 | + `https://media.githubusercontent.com/media/${user}/${repository}/${reference}/${escapeFilepath(file.path)}`, |
| 46 | + {signal}, |
| 47 | + ) |
| 48 | + : response; |
| 49 | + |
| 50 | + if (!response.ok) { |
| 51 | + throw new Error(`HTTP ${response.statusText} for ${file.path}`); |
| 52 | + } |
| 53 | + |
| 54 | + return lfsCompatibleResponse.blob(); |
| 55 | +} |
| 56 | + |
| 57 | +async function fetchPrivateFile({ |
| 58 | + file, |
| 59 | + signal, |
| 60 | +}: FileRequest) { |
| 61 | + const response = await authenticatedFetch(file.url, {signal}); |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + throw new Error(`HTTP ${response.statusText} for ${file.path}`); |
| 65 | + } |
| 66 | + |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 68 | + const {content} = await response.json(); |
| 69 | + const decoder = await fetch( |
| 70 | + `data:application/octet-stream;base64,${content}`, |
| 71 | + ); |
| 72 | + return decoder.blob(); |
| 73 | +} |
| 74 | + |
| 75 | +export async function downloadFile({ |
| 76 | + user, |
| 77 | + repository, |
| 78 | + reference, |
| 79 | + file, |
| 80 | + isPrivate, |
| 81 | + signal, |
| 82 | +}: { |
| 83 | + user: string; |
| 84 | + repository: string; |
| 85 | + reference: string; |
| 86 | + isPrivate: boolean; |
| 87 | + file: TreeResponseObject | ContentsReponseObject; |
| 88 | + signal: AbortSignal; |
| 89 | +}) { |
| 90 | + const fileRequest = { |
| 91 | + user, repository, reference, file, signal, |
| 92 | + }; |
| 93 | + const localDownload = async () => |
| 94 | + isPrivate |
| 95 | + ? fetchPrivateFile(fileRequest) |
| 96 | + : fetchPublicFile(fileRequest); |
| 97 | + const onFailedAttempt = (error: FailedAttemptError) => { |
| 98 | + console.error( |
| 99 | + `Error downloading ${file.path}. Attempt ${error.attemptNumber}. ${error.retriesLeft} retries left.`, |
| 100 | + ); |
| 101 | + }; |
| 102 | + |
| 103 | + return pRetry(localDownload, {onFailedAttempt}); |
| 104 | +} |
0 commit comments