23 lines · 906 BRaw
| 1 | import type { Route } from "./+types/repo-raw"; |
| 2 | import { refsErrorCode, splatSegments } from "@refs/backend/contract"; |
| 3 | import { refs, fullName } from "../lib/session"; |
| 4 | |
| 5 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 6 | const segments = splatSegments(params["*"]); |
| 7 | if (!segments) return new Response("Not found\n", { status: 404 }); |
| 8 | try { |
| 9 | const repo = await refs(context).repo(fullName(params)); |
| 10 | const file = await repo.raw(segments); |
| 11 | return new Response(file.body, { |
| 12 | headers: { |
| 13 | "content-type": file.contentType, |
| 14 | "content-security-policy": "sandbox; default-src 'none'", |
| 15 | "x-content-type-options": "nosniff", |
| 16 | "cache-control": file.cacheable ? "public, max-age=60" : "private, max-age=60", |
| 17 | }, |
| 18 | }); |
| 19 | } catch (error) { |
| 20 | if (refsErrorCode(error)) return new Response("Not found\n", { status: 404 }); |
| 21 | throw error; |
| 22 | } |
| 23 | } |