Public: anyone can read this; the crew can change it.

sitemap.xml and llms.txt are built at most once a minute, never once per stranger's request #3146

Parked opened this
Closed without merging

scout/forge:scout/3142-sitemap-xml-and-llms-txt-are-built-at-mo → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +67 −13
src/actions/repos.ts +2
@@ -4,6 +4,7 @@
44 * write path — committing files to a branch without a git client.
55 */
66
7 import { forgetListings } from "../web/standards.ts";
78 import { requireVerified } from "./verify.ts";
89 import { requireAbility } from "./roles.ts";
910 import type { App } from "../app.ts";
@@ -369,6 +370,7 @@ export async function updateRepoSettings(
369370 archived === undefined ? false : await moveArchive(app, repo.id, archived);
370371 const updated = getRepoById(app.db, repo.id);
371372 if (updated === undefined) return err("not_found");
373 if (updated.visibility !== repo.visibility) forgetListings(app);
372374 const changed: string[] = (
373375 [
374376 "visibility",
src/origins/sync.ts +2
@@ -22,6 +22,7 @@
2222 * thrown rejection in a timer takes the process down.
2323 */
2424
25 import { forgetListings } from "../web/standards.ts";
2526 import { existsSync } from "node:fs";
2627 import type { App } from "../app.ts";
2728 import {
@@ -310,6 +311,7 @@ async function syncMirrorUnguarded(
310311 app.db
311312 .query("UPDATE repo SET visibility = ?1 WHERE id = ?2")
312313 .run(wanted, repo.id);
314 forgetListings(app);
313315 app.bus.append({
314316 kind: "repo.settings_updated",
315317 repoId: repo.id,
src/web/standards.ts +34 −12
@@ -147,17 +147,37 @@ function xml(s: string): string {
147147 * varies `Host` (where `FORGE_URL` does not fix the origin) gets their own
148148 * origin on the same rows and cannot make the forge read them again.
149149 */
150 const SITEMAP_MS = 60_000;
150 const LISTING_MS = 60_000;
151151 const sitemaps = new WeakMap<App, Fresh<readonly string[]>>();
152 const llmsRepos = new WeakMap<App, Fresh<readonly string[]>>();
153
154 /** How many times each listing was read from the database, for tests. */
155 export const listingBuilds = { sitemap: 0, llms: 0 };
156
157 /**
158 * Forget the kept listings. Called when a repository stops being public, so
159 * a private repository is never listed from the cache, not even briefly.
160 */
161 export function forgetListings(app: App): void {
162 sitemaps.get(app)?.clear();
163 llmsRepos.get(app)?.clear();
164 }
165
166 function kept(
167 map: WeakMap<App, Fresh<readonly string[]>>,
168 app: App,
169 ): Fresh<readonly string[]> {
170 let f = map.get(app);
171 if (f === undefined) {
172 f = new Fresh(LISTING_MS, 1);
173 map.set(app, f);
174 }
175 return f;
176 }
152177
153178 /** GET /sitemap.xml — the front page and every public repository. */
154179 export function sitemapXml(ctx: Ctx): Response {
155 let kept = sitemaps.get(ctx.app);
156 if (kept === undefined) {
157 kept = new Fresh(SITEMAP_MS, 1);
158 sitemaps.set(ctx.app, kept);
159 }
160 const paths = kept.get("", () => sitemapPaths(ctx.app));
180 const paths = kept(sitemaps, ctx.app).get("", () => sitemapPaths(ctx.app));
161181 // Escaping is per character, so the escaped origin and the escaped path
162182 // join into the escaped URL.
163183 const origin = xml(ctx.url.origin);
@@ -173,6 +193,7 @@ export function sitemapXml(ctx: Ctx): Response {
173193
174194 /** Every public repository's path, escaped for XML, newest first. */
175195 function sitemapPaths(app: App): readonly string[] {
196 listingBuilds.sitemap++;
176197 return listReposVisibleTo(app.db, undefined, { limit: 5000 }).map((r) =>
177198 xml(`/${r.ownerName}/${r.name}`),
178199 );
@@ -256,8 +277,11 @@ const LLMS_REPOS = 200;
256277 */
257278 export function llmsTxt(ctx: Ctx): Response {
258279 const o = ctx.url.origin;
259 const repos = listReposVisibleTo(ctx.app.db, undefined, {
260 limit: LLMS_REPOS,
280 const repos = kept(llmsRepos, ctx.app).get("", () => {
281 listingBuilds.llms++;
282 return listReposVisibleTo(ctx.app.db, undefined, {
283 limit: LLMS_REPOS,
284 }).map((r) => `${r.ownerName}/${r.name}`);
261285 });
262286 return text(
263287 [
@@ -290,9 +314,7 @@ export function llmsTxt(ctx: Ctx): Response {
290314 // Names and addresses only: a description is whatever its owner
291315 // wrote, and an agent reading this file must not meet anyone's words
292316 // as the forge's own.
293 ...repos.map(
294 (r) => `- [${r.ownerName}/${r.name}](${o}/${r.ownerName}/${r.name})`,
295 ),
317 ...repos.map((r) => `- [${r}](${o}/${r})`),
296318 "",
297319 ].join("\n"),
298320 "text/markdown; charset=utf-8",
test/standards.test.ts +29 −1
@@ -11,10 +11,12 @@ import { mkdtemp, rm } from "node:fs/promises";
1111 import { tmpdir } from "node:os";
1212 import { join } from "node:path";
1313 import { createApp, type App } from "../src/app.ts";
14 import { createRepo } from "../src/actions/repos.ts";
14 import { createRepo, updateRepoSettings } from "../src/actions/repos.ts";
1515 import { decodePng, pngHeader } from "../src/design/image/png.ts";
1616 import { markPng } from "../src/design/mark.ts";
1717 import { createUser, type User } from "../src/core/users.ts";
18 import { listingBuilds } from "../src/web/standards.ts";
19 import { getRepo } from "../src/core/repos.ts";
1820 import { serve, type Servers } from "../src/http/server.ts";
1921
2022 describe("what the open web expects", () => {
@@ -117,6 +119,32 @@ describe("what the open web expects", () => {
117119 expect(body).not.toContain("alice/closed");
118120 });
119121
122 test("sitemap and llms.txt are each built once for 50 requests, and a repository made private drops out at once", async () => {
123 const mid = await createRepo(app, alice, {
124 name: "midway",
125 visibility: "public",
126 });
127 if (!mid.ok) throw new Error(String(mid.error));
128 for (const path of ["/sitemap.xml", "/llms.txt"]) {
129 const key = path === "/llms.txt" ? "llms" : "sitemap";
130 await fetch(`${base}${path}`).then((r) => r.text());
131 const before = listingBuilds[key];
132 for (let i = 0; i < 50; i++)
133 await fetch(`${base}${path}`).then((r) => r.text());
134 expect(listingBuilds[key]).toBe(before);
135 }
136 const repo = getRepo(app.db, "alice", "midway")!;
137 const done = await updateRepoSettings(app, alice, repo, {
138 visibility: "private",
139 });
140 expect(done.ok).toBe(true);
141 for (const path of ["/sitemap.xml", "/llms.txt"]) {
142 const body = await fetch(`${base}${path}`).then((r) => r.text());
143 expect(body).not.toContain("alice/midway");
144 expect(body).toContain("alice/open");
145 }
146 });
147
120148 test("the manifest is installable and its icons are real PNGs", async () => {
121149 const res = await fetch(`${base}/manifest.webmanifest`);
122150 expect(res.headers.get("content-type")).toContain(

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs