Skip to content
refs
History
24 lines · 1.1 KBRaw
1import type { Route } from "./+types/repo-code-file";
2import { isCommitHash, refsErrorCode, splatSegments } from "@refs/backend/contract";
3import { refs, fullName } from "../lib/session";
4
5/** One file at one commit, as text for the code map. The commit pins the content, so the response is cached for good. */
6export async function loader({ params, context }: Route.LoaderArgs) {
7 const segments = splatSegments(params["*"]);
8 if (!segments?.length || !isCommitHash(params.commit)) return new Response("Not found\n", { status: 404 });
9 try {
10 const repo = await refs(context).repo(fullName(params));
11 const file = await repo.raw([params.commit, ...segments]);
12 return new Response(file.body, {
13 headers: {
14 "content-type": "text/plain; charset=utf-8",
15 "content-security-policy": "sandbox; default-src 'none'",
16 "x-content-type-options": "nosniff",
17 "cache-control": `${file.cacheable ? "public" : "private"}, max-age=31536000, immutable`,
18 },
19 });
20 } catch (error) {
21 if (refsErrorCode(error)) return new Response("Not found\n", { status: 404 });
22 throw error;
23 }
24}