g1t/apps/web/app/routes/repo/archive.ts

69 lines3,820 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths1/**
2 * Download ZIP: a branch, tag or commit of a repository as one zip, its
3 * files under `<repo>-<ref>/` as `git archive` would name them. For anyone
4 * who can see the repository. Made on request and not kept, so it is capped:
5 * a repository past the caps is cloned instead.
6 */
7import type { Route } from "./+types/archive";
Self-hosting: the clone pack cache on S3 (MinIO, expiring), the API and MCP on their own port with PUBLIC_URL-derived addresses across the site, API and mail, scheduler --once, and smoke.sh covering pull requests from branches and forks and the merge queue8import { cloneUrl } from "../../lib/addresses";
9import { addresses } from "../../lib/addresses.server";
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths10import { repos } from "../../lib/services.server";
11import { getViewer } from "../../lib/session.server";
12import { zip } from "../../lib/zip";
13
14/** The most files, and bytes in all, an archive is made of. */
15const MAX_FILES = 10_000;
16// The bytes, their base64 and the zip are all held at once, in a Worker of 128 MB.
17const MAX_BYTES = 24 * 1024 * 1024;
Download ZIP reads blobs eight batches at a time and deflates files together18/** Blobs read per call to the repository, and calls at once. */
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths19const PER_READ = 100;
Download ZIP reads blobs eight batches at a time and deflates files together20const READS_AT_ONCE = 8;
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths21
22function refused(status: number, message: string): Response {
23 return new Response(`${message}\n`, { status, headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" } });
24}
25
26export async function loader({ params, context }: Route.LoaderArgs) {
27 const viewer = getViewer(context);
28 const asked = params["*"] ?? "";
29 if (!asked.endsWith(".zip") || asked.length <= 4) return refused(404, "Ask for <branch, tag or commit>.zip.");
30 const ref = asked.slice(0, -4);
31 const repo = await repos.get({ namespace: params.owner, name: params.repo }, viewer).catch(() => null);
32 if (!repo?.ok) return refused(404, "There is no such repository, or you cannot see it.");
33 const listed = await repos.listFiles(repo.value.id, ref, MAX_FILES + 1).catch(() => null);
34 if (!listed?.commit) return refused(404, `There is no branch, tag or commit named ${ref}.`);
Self-hosting: the clone pack cache on S3 (MinIO, expiring), the API and MCP on their own port with PUBLIC_URL-derived addresses across the site, API and mail, scheduler --once, and smoke.sh covering pull requests from branches and forks and the merge queue35 const clone = `git clone ${cloneUrl(addresses(), `${repo.value.namespace}/${repo.value.name}`)}`;
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths36 if (listed.truncated || listed.files.length > MAX_FILES) {
37 return refused(413, `It has more than ${MAX_FILES.toLocaleString("en-US")} files, too many for a download. Clone it instead: ${clone}`);
38 }
39 const files = listed.files.filter((file): file is { path: string; hash: string } => file.hash != null);
40 const unique = [...new Set(files.map((file) => file.hash))];
41 const bytes = new Map<string, Uint8Array>();
42 let total = 0;
Download ZIP reads blobs eight batches at a time and deflates files together43 let tooLarge = false;
44 const batches: string[][] = [];
45 for (let at = 0; at < unique.length; at += PER_READ) batches.push(unique.slice(at, at + PER_READ));
46 // A few reads at a time, each taking the next batch, until all are read or it is too large.
47 const reader = async () => {
48 for (let batch = batches.shift(); batch && !tooLarge; batch = batches.shift()) {
49 for (const blob of await repos.rawBlobs(repo.value.id, batch, MAX_BYTES)) {
50 total += blob.size;
51 if (total > MAX_BYTES || (blob.data == null && blob.size > 0)) tooLarge = true;
52 else bytes.set(blob.hash, Uint8Array.from(atob(blob.data ?? ""), (c) => c.charCodeAt(0)));
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths53 }
54 }
Download ZIP reads blobs eight batches at a time and deflates files together55 };
56 await Promise.all(Array.from({ length: READS_AT_ONCE }, reader));
57 if (tooLarge) return refused(413, `It is over ${MAX_BYTES / 1024 / 1024} MB, too large for a download. Clone it instead: ${clone}`);
Download ZIP from the Code button; a slimmer lifecycle panel; agent steps say what was done, not sandbox paths58 // A branch name can hold slashes; the folder and file name cannot.
59 const label = `${repo.value.name}-${ref.replaceAll("/", "-")}`;
60 const archive = await zip(files.map((file) => ({ path: `${label}/${file.path}`, data: bytes.get(file.hash) ?? new Uint8Array() })));
61 return new Response(archive, {
62 headers: {
63 "content-type": "application/zip",
64 "content-disposition": `attachment; filename="${label}.zip"`,
65 // A commit's files never change; a branch's do.
66 "cache-control": /^[0-9a-f]{40}$/.test(ref) ? "private, max-age=31536000, immutable" : "private, no-cache",
67 },
68 });
69}