| 1 | import { createRequestHandler } from "react-router"; |
| 2 | |
| 3 | const requestHandler = createRequestHandler( |
| 4 | () => import("virtual:react-router/server-build"), |
| 5 | import.meta.env.MODE, |
| 6 | ); |
| 7 | |
| 8 | const GIT_PATH = /\/(info\/refs|git-upload-pack|git-receive-pack)$/; |
| 9 | /** An uploaded avatar, by the SHA-256 of its bytes. */ |
| 10 | const AVATAR_PATH = /^\/avatars\/([0-9a-f]{64})$/; |
| 11 | /** The only types identity stores, having checked each image's bytes. */ |
| 12 | const AVATAR_TYPES = new Set(["image/png", "image/jpeg", "image/webp", "image/gif"]); |
| 13 | const DOCS = "https://docs.g1t.sh"; |
| 14 | |
| 15 | /** Where the documentation pages that used to live under /docs are now. */ |
| 16 | const MOVED_DOCS: Record<string, string> = { |
| 17 | "/docs": "/quickstart/", |
| 18 | "/docs/concepts": "/concepts/overview/", |
| 19 | "/docs/authentication": "/guides/authentication/", |
| 20 | "/docs/git": "/guides/git/", |
| 21 | "/docs/g1t-agents": "/guides/g1t-agents/", |
| 22 | "/docs/agents": "/guides/bring-your-own-agent/", |
| 23 | "/docs/api": "/reference/api/", |
| 24 | "/docs/api/reference": "/reference/api/", |
| 25 | }; |
| 26 | |
| 27 | export default { |
| 28 | async fetch(request, env) { |
| 29 | const { pathname } = new URL(request.url); |
| 30 | // Git over HTTPS shares this hostname but belongs to the repos service. |
| 31 | if (GIT_PATH.test(pathname)) { |
| 32 | return env.REPOS.fetch(request); |
| 33 | } |
| 34 | const avatar = AVATAR_PATH.exec(pathname); |
| 35 | if (avatar) { |
| 36 | return serveAvatar(env, avatar[1], request.method); |
| 37 | } |
| 38 | // The documentation is its own site. |
| 39 | if (pathname === "/docs" || pathname.startsWith("/docs/")) { |
| 40 | const page = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname; |
| 41 | const target = MOVED_DOCS[page] ?? "/"; |
| 42 | return Response.redirect(DOCS + target, 301); |
| 43 | } |
| 44 | return requestHandler(request); |
| 45 | }, |
| 46 | } satisfies ExportedHandler<Env>; |
| 47 | |
| 48 | /** |
| 49 | * An uploaded avatar. Its address is its hash, so it never changes and is |
| 50 | * kept for good. It is served as nothing but an image: the stored type, |
| 51 | * no sniffing, and a policy that lets nothing in it run. |
| 52 | */ |
| 53 | async function serveAvatar(env: Env, hash: string, method: string): Promise<Response> { |
| 54 | if (method !== "GET" && method !== "HEAD") { |
| 55 | return new Response("Method not allowed", { status: 405, headers: { allow: "GET, HEAD" } }); |
| 56 | } |
| 57 | const { value, metadata } = await env.AVATARS.getWithMetadata<{ contentType?: string }>(hash, { |
| 58 | type: "arrayBuffer", |
| 59 | cacheTtl: 86400, |
| 60 | }); |
| 61 | const contentType = metadata?.contentType; |
| 62 | if (!value || !contentType || !AVATAR_TYPES.has(contentType)) { |
| 63 | return new Response("Not found", { |
| 64 | status: 404, |
| 65 | headers: { "cache-control": "public, max-age=60" }, |
| 66 | }); |
| 67 | } |
| 68 | return new Response(method === "HEAD" ? null : value, { |
| 69 | headers: { |
| 70 | "content-type": contentType, |
| 71 | "content-length": String(value.byteLength), |
| 72 | "cache-control": "public, max-age=31536000, immutable", |
| 73 | "x-content-type-options": "nosniff", |
| 74 | "content-security-policy": "default-src 'none'; sandbox", |
| 75 | "cross-origin-resource-policy": "cross-origin", |
| 76 | }, |
| 77 | }); |
| 78 | } |