Open History to see the last commit
1import 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 */
8const WEB_HOSTS = new Set(["github.com", "www.github.com"]);
9const RAW_HOST = "raw.githubusercontent.com";
10const LFS_HOST = "media.githubusercontent.com";
11export const GITHUB_CREDENTIALED_MEDIA_HOSTS: ReadonlySet<string> = new Set([
12 ...WEB_HOSTS,
13 RAW_HOST,
14 LFS_HOST,
15]);
16
17const ATTACHMENT_PATH_PATTERN = /^\/user-attachments\/assets\/[\w-]+$/u;
18const 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. */
20const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.*[^/])$/u;
21/** GitHub rejects 40-hex branch and tag names, and serves no SHA-256 repositories. */
22const COMMIT_SHA_PATTERN = /^[0-9a-f]{40}$/u;
23
24function 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
36function 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. */
44function 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
49export function gitHubMediaUrl(url: URL): SourceControlMediaUrl | null {
50 if (url.protocol !== "https:") return null;
51 const host = url.hostname.toLowerCase();
52 if (host === RAW_HOST) return rawFile(`https://${host}${url.pathname}${url.search}`);
53 if (host === LFS_HOST) return media(`https://${host}${url.pathname}${url.search}`);
54 if (!WEB_HOSTS.has(host)) return null;
55 // GitHub links an uploaded video bare, with no extension, and an image as markdown.
56 if (ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
57 return media(`https://github.com${url.pathname}`, { kind: "video", immutable: true });
58 }
59 if (LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
60 return media(`https://github.com${url.pathname}`, { immutable: true });
61 }
62 const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname);
63 return repositoryFile
64 ? rawFile(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`)
65 : null;
66}