Skip to content
241 linesCodeBlameRaw
1import { createRequestHandler } from "react-router";
2
3import { identityClient, isNamespaceShaped } from "@g1t/contracts";
4
5import { addressesFor } from "../app/lib/addresses";
6import { visitorAsksFirst } from "../app/lib/analytics-consent";
7import { hardenRegistryHeaders } from "../app/lib/content-safety";
8import { withSiteHeaders } from "../app/lib/page-headers";
9import { finishResponse, withRequestPerf } from "../app/lib/perf.server";
10import { gitLimited, pageLimited, usercontentLimited } from "../app/lib/front-door-limits";
11import { goImport } from "../app/lib/go-get";
12import { repositoryOfPage, stillPublic } from "../app/lib/public-cache";
13import { registryWorkspace, servicePath } from "../app/lib/registry-paths";
14import { usercontentPath } from "../app/lib/usercontent";
15import { TOKEN_CHALLENGE } from "../app/lib/website-token";
16import { serveUsercontent } from "./usercontent";
17
18const loadBuild = () => import("virtual:react-router/server-build");
19
20/**
21 * Given a function for the build, React Router derives everything from it
22 * again on every request: each route wrapped for the timings in
23 * entry.server.tsx, then the route table flattened and ranked, about a
24 * millisecond and a half of CPU a page. The build never changes while an
25 * isolate lives, so outside development it is loaded once and the handler
26 * made once. Development keeps the function, so a changed file is picked up.
27 * Only the made handler is kept, never a promise of one, so no request
28 * waits on another's (a build that fails to load is loaded again next time).
29 */
30let handler: ReturnType<typeof createRequestHandler> | undefined;
31async function requestHandler(request: Request): Promise<Response> {
32 if (import.meta.env.DEV) return createRequestHandler(loadBuild, import.meta.env.MODE)(request);
33 handler ??= createRequestHandler(await loadBuild(), import.meta.env.MODE);
34 return handler(request);
35}
36
37const DOCS = "https://docs.g1t.sh";
38
39/** Where the documentation pages that used to live under /docs are now. */
40const MOVED_DOCS: Record<string, string> = {
41 "/docs": "/quickstart/",
42 "/docs/concepts": "/concepts/overview/",
43 "/docs/authentication": "/guides/authentication/",
44 "/docs/git": "/guides/git/",
45 "/docs/g1t-agents": "/guides/working-with-g1t/",
46 "/docs/agents": "/guides/bring-your-own-agent/",
47 "/docs/api": "/reference/api/",
48 "/docs/api/reference": "/reference/api/",
49};
50
51export default {
52 async fetch(request, env, ctx) {
53 // Repository files and avatars, on their own origin
54 // (g1tusercontent.com): answered before anything of the site's runs,
55 // so nothing there reads or sets the session cookie.
56 const usercontent = usercontentPath(new URL(request.url), addressesFor(env).usercontent);
57 if (usercontent !== null) {
58 return (await usercontentLimited(env, request, usercontent)) ?? serveUsercontent(env, ctx, request, usercontent);
59 }
60 // Every answer: no sniffing, a referrer of the origin alone, and no
61 // framing of pages (app/lib/page-headers.ts).
62 return withSiteHeaders(await site(request, env, ctx));
63 },
64} satisfies ExportedHandler<Env>;
65
66async function site(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
67 const { pathname } = new URL(request.url);
68 // Git over HTTPS shares this hostname but belongs to the repos service.
69 // Its answer goes back to the git client as it is: a repository under a
70 // renamed workspace's old name answers with a 301, which git follows and
71 // must see, so the redirect is never followed here.
72 // The container registry (`docker login g1t.sh`) and the npm registry
73 // (`g1t.sh/-/npm/`) are the packages
74 // service's, handed over the same way.
75 // `go get g1t.sh/<workspace>/<repo>`: where its code is, from the
76 // address alone, so it costs nothing and caches.
77 const go = request.method === "GET" ? goImport(new URL(request.url)) : null;
78 if (go) {
79 return new Response(go, {
80 headers: { "content-type": "text/html; charset=utf-8", "cache-control": "public, max-age=3600" },
81 });
82 }
83 const service = servicePath(pathname);
84 if (service === "git") {
85 // Per address without credentials, per credential with them
86 // (app/lib/front-door-limits.ts): anonymous clones are not free to
87 // the repository's owner.
88 return (await gitLimited(env, request)) ?? proxyGit(env, request);
89 }
90 if (service === "packages") {
91 return proxyPackages(env, request);
92 }
93 // The documentation is its own site.
94 if (pathname === "/docs" || pathname.startsWith("/docs/")) {
95 const page = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
96 const target = MOVED_DOCS[page] ?? "/";
97 return Response.redirect(DOCS + target, 301);
98 }
99 // Pages and data requests, limited per address signed out and per
100 // session signed in (app/lib/front-door-limits.ts).
101 const limited = await pageLimited(env, request, pathname);
102 if (limited) return limited;
103 // Every page and data request says where its time went (Server-Timing)
104 // and keeps the reader's D1 bookmarks (app/lib/perf.server.ts).
105 const render = () => withRequestPerf(request, async () => finishResponse(request, await requestHandler(request)));
106 if (anonymousPage(request, pathname)) return servePublic(env, request, ctx, render);
107 const answer = await render();
108 // A token the site refused (app/lib/website-token.ts) is told so as the
109 // API tells it: React Router drops the headers of what middleware throws.
110 if (answer.status === 401 && request.headers.has("authorization")) answer.headers.set("www-authenticate", TOKEN_CHALLENGE);
111 return answer;
112}
113
114/**
115 * Public pages as someone signed out sees them: the same for every such
116 * visitor, so kept in this data centre's cache. Reserved first segments
117 * (settings, sign-in, invitations and the like) and workspace pages (`-`)
118 * are never kept; docs/PERFORMANCE.md lists the rules. A repository's kept
119 * page is served only while repos says the repository is still public: one
120 * made private or deleted is never served from any data centre's copy.
121 */
122const PUBLIC_TOP = /^\/(?:|_root\.data|pricing|explore|security|support|policies(?:\/[a-z-]+)?)(?:\.data)?$/;
123const PUBLIC_PROJECT =
124 /^\/(?!(?:settings|u|auth|oauth|integrations|new|invite|workspaces|device|verify|confirm-email|login|register|logout|forgot|reset|search|status|avatars|docs)\/)[^/]+\/(?!-\/|-$)[^/]+(?:\/(?:code|commits|issues|pulls|pull\/\d+|issues\/\d+|commit\/[0-9a-f]+|tree\/.+|blob\/.+))?(?:\.data)?$/;
125/** Fresh for this long; then served once more while a new copy is made. */
126const PUBLIC_FRESH_SECONDS = 30;
127const PUBLIC_STALE_SECONDS = 300;
128
129function anonymousPage(request: Request, pathname: string): boolean {
130 if (request.method !== "GET") return false;
131 if (/(?:^|;\s*)g1t_session=/.test(request.headers.get("cookie") ?? "")) return false;
132 // A token signs the request in (app/lib/website-token.ts): never kept, never served a kept page.
133 if (request.headers.has("authorization")) return false;
134 return PUBLIC_TOP.test(pathname) || PUBLIC_PROJECT.test(pathname);
135}
136
137async function servePublic(env: Env, request: Request, ctx: ExecutionContext, render: () => Promise<Response>): Promise<Response> {
138 const cache = (caches as unknown as { default: Cache }).default;
139 // Visitors asked about analytics first get a page that says so, kept apart.
140 const keyUrl = new URL(request.url);
141 if (visitorAsksFirst(request)) keyUrl.searchParams.set("_g1t_consent", "1");
142 const key = new Request(keyUrl, { method: "GET" });
143 const repository = PUBLIC_PROJECT.test(new URL(request.url).pathname) ? repositoryOfPage(new URL(request.url).pathname) : null;
144 // Asked alongside the cache, so a hit waits for one indexed read at most.
145 const [cached, visible] = await Promise.all([cache.match(key), repository ? isStillPublic(env, repository) : Promise.resolve(true)]);
146 if (cached && !visible) {
147 ctx.waitUntil(cache.delete(key).then(() => undefined, () => undefined));
148 return render();
149 }
150 const keptAt = Number(cached?.headers.get("x-g1t-kept-at") ?? 0);
151 const age = Math.round((Date.now() - keptAt) / 1000);
152 const refresh = async () => {
153 const fresh = await render();
154 // Only a plain answer for everyone: nothing that sets a cookie or says
155 // it is private.
156 const cacheable =
157 (fresh.status === 200 || fresh.status === 404) &&
158 !fresh.headers.has("set-cookie") &&
159 !/private|no-store/.test(fresh.headers.get("cache-control") ?? "");
160 if (cacheable) {
161 const copy = new Response(fresh.clone().body, fresh);
162 copy.headers.set("x-g1t-kept-at", String(Date.now()));
163 copy.headers.set("cache-control", `public, max-age=${PUBLIC_STALE_SECONDS}`);
164 ctx.waitUntil(cache.put(key, copy));
165 }
166 return fresh;
167 };
168 if (cached && keptAt > 0 && age < PUBLIC_STALE_SECONDS) {
169 if (age >= PUBLIC_FRESH_SECONDS) ctx.waitUntil(refresh().then(() => undefined, () => undefined));
170 const answer = new Response(cached.body, cached);
171 answer.headers.delete("x-g1t-kept-at");
172 answer.headers.delete("cache-control");
173 answer.headers.set("server-timing", `cache;desc="hit, ${age}s old"`);
174 return answer;
175 }
176 return refresh();
177}
178
179/** Whether the repository is there and public; anything else, including no answer, is no. */
180async function isStillPublic(env: Env, repository: string): Promise<boolean> {
181 try {
182 const answer = await env.REPOS.fetch("https://service/rpc/visibility", {
183 method: "POST",
184 headers: { "content-type": "application/json" },
185 body: JSON.stringify({ paths: [repository] }),
186 });
187 return answer.ok && stillPublic(await answer.json(), repository);
188 } catch {
189 return false;
190 }
191}
192
193/**
194 * A git request, answered by the repos service. Its `Server-Timing` header
195 * gains `repos`: how long the answer took to start from here, so the time
196 * between this Worker and the repos service shows beside the steps the
197 * repos service reports.
198 */
199async function proxyGit(env: Env, request: Request): Promise<Response> {
200 const started = Date.now();
201 const answer = await env.REPOS.fetch(new Request(request, { redirect: "manual" }));
202 const response = new Response(answer.body, answer);
203 response.headers.append("server-timing", `repos;dur=${Date.now() - started}`);
204 return response;
205}
206
207/**
208 * A registry request, answered by the packages service as it is: its
209 * redirects (a large blob sent to storage) go back to the client, which
210 * follows them itself. One that found nothing under a workspace's old name
211 * or an alias staff set (`g1t` for `flagon-io`) is sent to the same path
212 * under the workspace's name: only the not-found answer pays for the lookup.
213 * Every answer runs nothing in a browser (app/lib/content-safety.ts): what
214 * a registry serves is its publisher's, on this origin.
215 */
216async function proxyPackages(env: Env, request: Request): Promise<Response> {
217 const started = Date.now();
218 const answer = await env.PACKAGES.fetch(new Request(request, { redirect: "manual" }));
219 const moved = answer.status === 404 ? await registryMoved(env, request) : null;
220 const response = moved ?? new Response(answer.body, answer);
221 hardenRegistryHeaders(response.headers);
222 response.headers.append("server-timing", `packages;dur=${Date.now() - started}`);
223 return response;
224}
225
226/** Where a registry request under an alias or old name goes now, or null. */
227async function registryMoved(env: Env, request: Request): Promise<Response | null> {
228 const url = new URL(request.url);
229 const named = registryWorkspace(url.pathname);
230 if (!named || !isNamespaceShaped(named.slug)) return null;
231 let current: string | null = null;
232 try {
233 current = await identityClient(env.IDENTITY).resolveSlug(named.slug);
234 } catch {
235 return null;
236 }
237 if (!current || current === named.slug) return null;
238 const get = request.method === "GET" || request.method === "HEAD";
239 // 308 keeps a publish a PUT, for the clients that follow it.
240 return new Response(null, { status: get ? 301 : 308, headers: { location: named.under(current) + url.search } });
241}