g1t/apps/web/workers/app.ts

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