Skip to content

g1t/apps/web/workers/app.ts

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