The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { SourceControlMediaUrl } from "@t3tools/source-control-core/shared/definition"; |
| 2 | |
| 3 | /** |
| 4 | * Only hosts where a credential is what decides the answer belong here. `objects.githubusercontent.com` |
| 5 | * and the `private-user-images` links GitHub's own HTML carries are already signed and load on their |
| 6 | * own, and a token does nothing for them once that signature expires. |
| 7 | */ |
| 8 | const WEB_HOSTS = new Set(["github.com", "www.github.com"]); |
| 9 | const RAW_HOST = "raw.githubusercontent.com"; |
| 10 | const LFS_HOST = "media.githubusercontent.com"; |
| 11 | export const GITHUB_CREDENTIALED_MEDIA_HOSTS: ReadonlySet<string> = new Set([ |
| 12 | ...WEB_HOSTS, |
| 13 | RAW_HOST, |
| 14 | LFS_HOST, |
| 15 | ]); |
| 16 | |
| 17 | const ATTACHMENT_PATH_PATTERN = /^\/user-attachments\/assets\/[\w-]+$/u; |
| 18 | const LEGACY_ATTACHMENT_PATH_PATTERN = /^\/[^/]+\/[^/]+\/assets\/\d+\/[\w-]+$/u; |
| 19 | /** `blob` and `raw` both address file bytes; `raw` is the one the token is honoured on. */ |
| 20 | const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.*[^/])$/u; |
| 21 | /** GitHub rejects 40-hex branch and tag names, and serves no SHA-256 repositories. */ |
| 22 | const COMMIT_SHA_PATTERN = /^[0-9a-f]{40}$/u; |
| 23 | |
| 24 | function fileName(fetchUrl: string): string { |
| 25 | const segment = new URL(fetchUrl).pathname.split("/").pop() ?? ""; |
| 26 | let decoded: string; |
| 27 | try { |
| 28 | decoded = decodeURIComponent(segment); |
| 29 | } catch { |
| 30 | decoded = segment; |
| 31 | } |
| 32 | const name = decoded.replace(/[\p{Cc}\\/]/gu, ""); |
| 33 | return name.length > 0 ? name : "github-media"; |
| 34 | } |
| 35 | |
| 36 | function media( |
| 37 | fetchUrl: string, |
| 38 | extra: Pick<SourceControlMediaUrl, "kind" | "immutable"> = {}, |
| 39 | ): SourceControlMediaUrl { |
| 40 | return { fetchUrl, fileName: fileName(fetchUrl), ...extra }; |
| 41 | } |
| 42 | |
| 43 | /** A branch or tag can move; a raw file is fixed only when read at a full commit SHA. */ |
| 44 | function rawFile(fetchUrl: string): SourceControlMediaUrl { |
| 45 | const ref = new URL(fetchUrl).pathname.split("/")[3] ?? ""; |
| 46 | return media(fetchUrl, COMMIT_SHA_PATTERN.test(ref) ? { immutable: true } : {}); |
| 47 | } |
| 48 | |
| 49 | /** Where GitHub serves a Git LFS object; a raw read of an LFS-tracked file returns its pointer. */ |
| 50 | export function gitHubLfsMediaUrl(rawUrl: string): string | null { |
| 51 | const url = URL.parse(rawUrl); |
| 52 | return url?.protocol === "https:" && url.hostname.toLowerCase() === RAW_HOST |
| 53 | ? `https://${LFS_HOST}/media${url.pathname}${url.search}` |
| 54 | : null; |
| 55 | } |
| 56 | |
| 57 | export function gitHubMediaUrl(url: URL): SourceControlMediaUrl | null { |
| 58 | if (url.protocol !== "https:") return null; |
| 59 | const host = url.hostname.toLowerCase(); |
| 60 | if (host === RAW_HOST) return rawFile(`https://${host}${url.pathname}${url.search}`); |
| 61 | if (host === LFS_HOST) return media(`https://${host}${url.pathname}${url.search}`); |
| 62 | if (!WEB_HOSTS.has(host)) return null; |
| 63 | // GitHub links an uploaded video bare, with no extension, and an image as markdown. |
| 64 | if (ATTACHMENT_PATH_PATTERN.test(url.pathname)) { |
| 65 | return media(`https://github.com${url.pathname}`, { kind: "video", immutable: true }); |
| 66 | } |
| 67 | if (LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)) { |
| 68 | return media(`https://github.com${url.pathname}`, { immutable: true }); |
| 69 | } |
| 70 | const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname); |
| 71 | return repositoryFile |
| 72 | ? rawFile(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`) |
| 73 | : null; |
| 74 | } |
| 75 | |
| 76 | export function gitHubRawFileUrl(input: { |
| 77 | readonly repositoryUrl: string; |
| 78 | readonly sha: string; |
| 79 | readonly path: string; |
| 80 | }): string | null { |
| 81 | if (!COMMIT_SHA_PATTERN.test(input.sha)) return null; |
| 82 | const path = input.path.split("/").map(encodeURIComponent).join("/"); |
| 83 | let url: URL; |
| 84 | try { |
| 85 | url = new URL(`${input.repositoryUrl}/raw/${input.sha}/${path}`); |
| 86 | } catch { |
| 87 | return null; |
| 88 | } |
| 89 | return gitHubMediaUrl(url) === null ? null : url.toString(); |
| 90 | } |