import type { Route } from "./+types/repo-code-file"; import { isCommitHash, refsErrorCode, splatSegments } from "@refs/backend/contract"; import { refs, fullName } from "../lib/session"; /** One file at one commit, as text for the code map. The commit pins the content, so the response is cached for good. */ export async function loader({ params, context }: Route.LoaderArgs) { const segments = splatSegments(params["*"]); if (!segments?.length || !isCommitHash(params.commit)) return new Response("Not found\n", { status: 404 }); try { const repo = await refs(context).repo(fullName(params)); const file = await repo.raw([params.commit, ...segments]); return new Response(file.body, { headers: { "content-type": "text/plain; charset=utf-8", "content-security-policy": "sandbox; default-src 'none'", "x-content-type-options": "nosniff", "cache-control": `${file.cacheable ? "public" : "private"}, max-age=31536000, immutable`, }, }); } catch (error) { if (refsErrorCode(error)) return new Response("Not found\n", { status: 404 }); throw error; } }