g1t/apps/web/workers/app.ts

195 lines8,771 bytesCodeBlame
1import { createRequestHandler } from "react-router";
2
3import { finishResponse, withRequestPerf } from "../app/lib/perf.server";
4import { goImport } from "../app/lib/go-get";
5import { servicePath } from "../app/lib/registry-paths";
6
7const requestHandler = createRequestHandler(
8 () => import("virtual:react-router/server-build"),
9 import.meta.env.MODE,
10);
11
12/** An uploaded avatar, by the SHA-256 of its bytes. */
13const AVATAR_PATH = /^\/avatars\/([0-9a-f]{64})$/;
14/** The only types identity stores, having checked each image's bytes. */
15const AVATAR_TYPES = new Set(["image/png", "image/jpeg", "image/webp", "image/gif"]);
16const DOCS = "https://docs.g1t.sh";
17
18/** Where the documentation pages that used to live under /docs are now. */
19const MOVED_DOCS: Record<string, string> = {
20 "/docs": "/quickstart/",
21 "/docs/concepts": "/concepts/overview/",
22 "/docs/authentication": "/guides/authentication/",
23 "/docs/git": "/guides/git/",
24 "/docs/g1t-agents": "/guides/working-with-g1t/",
25 "/docs/agents": "/guides/bring-your-own-agent/",
26 "/docs/api": "/reference/api/",
27 "/docs/api/reference": "/reference/api/",
28};
29
30export default {
31 async fetch(request, env, ctx) {
32 const { pathname } = new URL(request.url);
33 // Git over HTTPS shares this hostname but belongs to the repos service.
34 // Its answer goes back to the git client as it is: a repository under a
35 // renamed workspace's old name answers with a 301, which git follows and
36 // must see, so the redirect is never followed here.
37 // The container registry (`docker login g1t.sh`) and the npm registry
38 // (`g1t.sh/-/npm/`) are the packages
39 // service's, handed over the same way.
40 // `go get g1t.sh/<workspace>/<repo>`: where its code is, from the
41 // address alone, so it costs nothing and caches.
42 const go = request.method === "GET" ? goImport(new URL(request.url)) : null;
43 if (go) {
44 return new Response(go, {
45 headers: { "content-type": "text/html; charset=utf-8", "cache-control": "public, max-age=3600" },
46 });
47 }
48 const service = servicePath(pathname);
49 if (service === "git") {
50 return proxyGit(env, request);
51 }
52 if (service === "packages") {
53 return proxyPackages(env, request);
54 }
55 const avatar = AVATAR_PATH.exec(pathname);
56 if (avatar) {
57 return serveAvatar(env, ctx, request, avatar[1]);
58 }
59 // The documentation is its own site.
60 if (pathname === "/docs" || pathname.startsWith("/docs/")) {
61 const page = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
62 const target = MOVED_DOCS[page] ?? "/";
63 return Response.redirect(DOCS + target, 301);
64 }
65 // Every page and data request says where its time went (Server-Timing)
66 // and keeps the reader's D1 bookmarks (app/lib/perf.server.ts).
67 const render = () => withRequestPerf(request, async () => finishResponse(request, await requestHandler(request)));
68 if (anonymousPage(request, pathname)) return servePublic(request, ctx, render);
69 return render();
70 },
71} satisfies ExportedHandler<Env>;
72
73/**
74 * Public pages as someone signed out sees them: the same for every such
75 * visitor, so kept in this data centre's cache. Reserved first segments
76 * (settings, sign-in, invitations and the like) and workspace pages (`-`)
77 * are never kept; docs/PERFORMANCE.md lists the rules.
78 */
79const PUBLIC_TOP = /^\/(?:|_root\.data|pricing|explore|security|support|policies(?:\/[a-z-]+)?)(?:\.data)?$/;
80const PUBLIC_PROJECT =
81 /^\/(?!(?:settings|u|auth|oauth|integrations|new|invite|workspaces|device|verify|login|register|logout|forgot|reset|search|status|avatars|docs)\/)[^/]+\/(?!-\/|-$)[^/]+(?:\/(?:code|commits|issues|pulls|pull\/\d+|issues\/\d+|commit\/[0-9a-f]+|tree\/.+|blob\/.+))?(?:\.data)?$/;
82/** Fresh for this long; then served once more while a new copy is made. */
83const PUBLIC_FRESH_SECONDS = 30;
84const PUBLIC_STALE_SECONDS = 300;
85
86function anonymousPage(request: Request, pathname: string): boolean {
87 if (request.method !== "GET") return false;
88 if (/(?:^|;\s*)g1t_session=/.test(request.headers.get("cookie") ?? "")) return false;
89 return PUBLIC_TOP.test(pathname) || PUBLIC_PROJECT.test(pathname);
90}
91
92async function servePublic(request: Request, ctx: ExecutionContext, render: () => Promise<Response>): Promise<Response> {
93 const cache = (caches as unknown as { default: Cache }).default;
94 const key = new Request(request.url, { method: "GET" });
95 const cached = await cache.match(key);
96 const keptAt = Number(cached?.headers.get("x-g1t-kept-at") ?? 0);
97 const age = Math.round((Date.now() - keptAt) / 1000);
98 const refresh = async () => {
99 const fresh = await render();
100 // Only a plain answer for everyone: nothing that sets a cookie or says
101 // it is private.
102 const cacheable =
103 (fresh.status === 200 || fresh.status === 404) &&
104 !fresh.headers.has("set-cookie") &&
105 !/private|no-store/.test(fresh.headers.get("cache-control") ?? "");
106 if (cacheable) {
107 const copy = new Response(fresh.clone().body, fresh);
108 copy.headers.set("x-g1t-kept-at", String(Date.now()));
109 copy.headers.set("cache-control", `public, max-age=${PUBLIC_STALE_SECONDS}`);
110 ctx.waitUntil(cache.put(key, copy));
111 }
112 return fresh;
113 };
114 if (cached && keptAt > 0 && age < PUBLIC_STALE_SECONDS) {
115 if (age >= PUBLIC_FRESH_SECONDS) ctx.waitUntil(refresh().then(() => undefined, () => undefined));
116 const answer = new Response(cached.body, cached);
117 answer.headers.delete("x-g1t-kept-at");
118 answer.headers.delete("cache-control");
119 answer.headers.set("server-timing", `cache;desc="hit, ${age}s old"`);
120 return answer;
121 }
122 return refresh();
123}
124
125/**
126 * A git request, answered by the repos service. Its `Server-Timing` header
127 * gains `repos`: how long the answer took to start from here, so the time
128 * between this Worker and the repos service shows beside the steps the
129 * repos service reports.
130 */
131async function proxyGit(env: Env, request: Request): Promise<Response> {
132 const started = Date.now();
133 const answer = await env.REPOS.fetch(new Request(request, { redirect: "manual" }));
134 const response = new Response(answer.body, answer);
135 response.headers.append("server-timing", `repos;dur=${Date.now() - started}`);
136 return response;
137}
138
139/**
140 * A registry request, answered by the packages service as it is: its
141 * redirects (a large blob sent to storage) go back to the client, which
142 * follows them itself.
143 */
144async function proxyPackages(env: Env, request: Request): Promise<Response> {
145 const started = Date.now();
146 const answer = await env.PACKAGES.fetch(new Request(request, { redirect: "manual" }));
147 const response = new Response(answer.body, answer);
148 response.headers.append("server-timing", `packages;dur=${Date.now() - started}`);
149 return response;
150}
151
152/**
153 * An uploaded avatar. Its address is its hash, so it never changes and is
154 * kept for good. It is served as nothing but an image: the stored type,
155 * no sniffing, and a policy that lets nothing in it run.
156 */
157/**
158 * An uploaded icon. Its address is its content's hash, so it never changes:
159 * each data centre keeps it in its cache after the first view, and storage
160 * is read about once per place, not once per visitor.
161 */
162async function serveAvatar(env: Env, ctx: ExecutionContext, request: Request, hash: string): Promise<Response> {
163 const method = request.method;
164 if (method !== "GET" && method !== "HEAD") {
165 return new Response("Method not allowed", { status: 405, headers: { allow: "GET, HEAD" } });
166 }
167 // The Workers runtime's own cache, which the DOM types do not know.
168 const cache = (caches as unknown as { default: Cache }).default;
169 const key = new Request(new URL(`/avatars/${hash}`, request.url).toString(), { method: "GET" });
170 const cached = await cache.match(key);
171 if (cached) {
172 return method === "HEAD" ? new Response(null, { headers: cached.headers }) : cached;
173 }
174 const { value, metadata } = await env.AVATARS.getWithMetadata<{ contentType?: string }>(hash, {
175 type: "arrayBuffer",
176 cacheTtl: 86400,
177 });
178 const contentType = metadata?.contentType;
179 if (!value || !contentType || !AVATAR_TYPES.has(contentType)) {
180 return new Response("Not found", {
181 status: 404,
182 headers: { "cache-control": "public, max-age=60" },
183 });
184 }
185 const headers = {
186 "content-type": contentType,
187 "content-length": String(value.byteLength),
188 "cache-control": "public, max-age=31536000, immutable",
189 "x-content-type-options": "nosniff",
190 "content-security-policy": "default-src 'none'; sandbox",
191 "cross-origin-resource-policy": "cross-origin",
192 };
193 ctx.waitUntil(cache.put(key, new Response(value, { headers })));
194 return new Response(method === "HEAD" ? null : value, { headers });
195}