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
scout/forge:scout/3142-sitemap-xml-and-llms-txt-are-built-at-mo → main 1 commit fork
4 files
changed +67
−13
src/actions/repos.ts +2
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * write path — committing files to a branch without a git client. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { forgetListings } from "../web/standards.ts"; | |
| 7 | 8 | import { requireVerified } from "./verify.ts"; |
| 8 | 9 | import { requireAbility } from "./roles.ts"; |
| 9 | 10 | import type { App } from "../app.ts"; |
| @@ -369,6 +370,7 @@ export async function updateRepoSettings( | ||
| 369 | 370 | archived === undefined ? false : await moveArchive(app, repo.id, archived); |
| 370 | 371 | const updated = getRepoById(app.db, repo.id); |
| 371 | 372 | if (updated === undefined) return err("not_found"); |
| 373 | if (updated.visibility !== repo.visibility) forgetListings(app); | |
| 372 | 374 | const changed: string[] = ( |
| 373 | 375 | [ |
| 374 | 376 | "visibility", |
src/origins/sync.ts +2
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | * thrown rejection in a timer takes the process down. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | import { forgetListings } from "../web/standards.ts"; | |
| 25 | 26 | import { existsSync } from "node:fs"; |
| 26 | 27 | import type { App } from "../app.ts"; |
| 27 | 28 | import { |
| @@ -310,6 +311,7 @@ async function syncMirrorUnguarded( | ||
| 310 | 311 | app.db |
| 311 | 312 | .query("UPDATE repo SET visibility = ?1 WHERE id = ?2") |
| 312 | 313 | .run(wanted, repo.id); |
| 314 | forgetListings(app); | |
| 313 | 315 | app.bus.append({ |
| 314 | 316 | kind: "repo.settings_updated", |
| 315 | 317 | repoId: repo.id, |
src/web/standards.ts +34 −12
| @@ -147,17 +147,37 @@ function xml(s: string): string { | ||
| 147 | 147 | * varies `Host` (where `FORGE_URL` does not fix the origin) gets their own |
| 148 | 148 | * origin on the same rows and cannot make the forge read them again. |
| 149 | 149 | */ |
| 150 | const SITEMAP_MS = 60_000; | |
| 150 | const LISTING_MS = 60_000; | |
| 151 | 151 | 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 | } | |
| 152 | 177 | |
| 153 | 178 | /** GET /sitemap.xml — the front page and every public repository. */ |
| 154 | 179 | 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)); | |
| 161 | 181 | // Escaping is per character, so the escaped origin and the escaped path |
| 162 | 182 | // join into the escaped URL. |
| 163 | 183 | const origin = xml(ctx.url.origin); |
| @@ -173,6 +193,7 @@ export function sitemapXml(ctx: Ctx): Response { | ||
| 173 | 193 | |
| 174 | 194 | /** Every public repository's path, escaped for XML, newest first. */ |
| 175 | 195 | function sitemapPaths(app: App): readonly string[] { |
| 196 | listingBuilds.sitemap++; | |
| 176 | 197 | return listReposVisibleTo(app.db, undefined, { limit: 5000 }).map((r) => |
| 177 | 198 | xml(`/${r.ownerName}/${r.name}`), |
| 178 | 199 | ); |
| @@ -256,8 +277,11 @@ const LLMS_REPOS = 200; | ||
| 256 | 277 | */ |
| 257 | 278 | export function llmsTxt(ctx: Ctx): Response { |
| 258 | 279 | 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}`); | |
| 261 | 285 | }); |
| 262 | 286 | return text( |
| 263 | 287 | [ |
| @@ -290,9 +314,7 @@ export function llmsTxt(ctx: Ctx): Response { | ||
| 290 | 314 | // Names and addresses only: a description is whatever its owner |
| 291 | 315 | // wrote, and an agent reading this file must not meet anyone's words |
| 292 | 316 | // 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})`), | |
| 296 | 318 | "", |
| 297 | 319 | ].join("\n"), |
| 298 | 320 | "text/markdown; charset=utf-8", |
test/standards.test.ts +29 −1
| @@ -11,10 +11,12 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 11 | 11 | import { tmpdir } from "node:os"; |
| 12 | 12 | import { join } from "node:path"; |
| 13 | 13 | 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"; | |
| 15 | 15 | import { decodePng, pngHeader } from "../src/design/image/png.ts"; |
| 16 | 16 | import { markPng } from "../src/design/mark.ts"; |
| 17 | 17 | 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"; | |
| 18 | 20 | import { serve, type Servers } from "../src/http/server.ts"; |
| 19 | 21 | |
| 20 | 22 | describe("what the open web expects", () => { |
| @@ -117,6 +119,32 @@ describe("what the open web expects", () => { | ||
| 117 | 119 | expect(body).not.toContain("alice/closed"); |
| 118 | 120 | }); |
| 119 | 121 | |
| 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 | ||
| 120 | 148 | test("the manifest is installable and its icons are real PNGs", async () => { |
| 121 | 149 | const res = await fetch(`${base}/manifest.webmanifest`); |
| 122 | 150 | expect(res.headers.get("content-type")).toContain( |
new above
offline