Public: anyone can read this; the crew can change it.
The next-up page runs 70 statements, over its 67 budget #15955
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15940-the-next-up-page-runs-70-statements-over → main 0 commits fork
14 files
changed +101
−19
src/actions/runs.ts +3 −1
| @@ -109,9 +109,11 @@ export function mayReadRuns( | ||
| 109 | 109 | app: App, |
| 110 | 110 | actor: User | undefined, |
| 111 | 111 | repo: Repo, |
| 112 | /** The repository's app when the caller has already read it (`undefined` inside: it has none). */ | |
| 113 | known?: { app: ReturnType<typeof getAppByRepo> }, | |
| 112 | 114 | ): boolean { |
| 113 | 115 | if (!requireReader(app, actor, repo).ok) return false; |
| 114 | const record = getAppByRepo(app.db, repo.id); | |
| 116 | const record = known !== undefined ? known.app : getAppByRepo(app.db, repo.id); | |
| 115 | 117 | return ( |
| 116 | 118 | record === undefined || |
| 117 | 119 | canEnterApp(app.db, repo, record, actor, { named: false }) |
src/core/repos.ts +4 −2
| @@ -969,6 +969,8 @@ export interface ListReposOptions { | ||
| 969 | 969 | onlyArchived?: boolean; |
| 970 | 970 | /** Only archived repositories. @default false */ |
| 971 | 971 | archivedOnly?: boolean; |
| 972 | /** Skip the open counts (one read per list) for a caller that never shows them. @default false */ | |
| 973 | withoutCounts?: boolean; | |
| 972 | 974 | /** Only this owner's repositories (a username, any case). */ |
| 973 | 975 | owner?: string; |
| 974 | 976 | /** Only repositories an admin marked as a template for new forges (#12431). */ |
| @@ -1036,7 +1038,7 @@ export function listReposVisibleTo( | ||
| 1036 | 1038 | ) |
| 1037 | 1039 | .all(...params) |
| 1038 | 1040 | .map(toRepo); |
| 1039 | return withCounts(db, rows); | |
| 1041 | return options.withoutCounts === true ? rows : withCounts(db, rows); | |
| 1040 | 1042 | } |
| 1041 | 1043 | |
| 1042 | 1044 | /** |
| @@ -1148,7 +1150,7 @@ export function listReposWritableTo( | ||
| 1148 | 1150 | ) |
| 1149 | 1151 | .all(...params) |
| 1150 | 1152 | .map(toRepo); |
| 1151 | return withCounts(db, rows); | |
| 1153 | return options.withoutCounts === true ? rows : withCounts(db, rows); | |
| 1152 | 1154 | } |
| 1153 | 1155 | |
| 1154 | 1156 | /** Repos owned by one user or org, visible to the viewer. */ |
src/http/archived_noindex.ts +3 −2
| @@ -11,11 +11,12 @@ | ||
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | 12 | import { canRead, getRepo } from "../core/repos.ts"; |
| 13 | 13 | import type { User } from "../core/users.ts"; |
| 14 | import { repoOnRequest } from "./request_repo.ts"; | |
| 14 | 15 | |
| 15 | 16 | /** Whether the route's owner/name is an archived repository the caller may read. */ |
| 16 | export function archivedRepoAt(db: Database, params: Record<string, string>, user: User | undefined): boolean { | |
| 17 | export function archivedRepoAt(db: Database, params: Record<string, string>, user: User | undefined, url?: URL): boolean { | |
| 17 | 18 | const { owner, name } = params; |
| 18 | 19 | if (owner === undefined || name === undefined) return false; |
| 19 | const repo = getRepo(db, owner, name); | |
| 20 | const repo = url === undefined ? getRepo(db, owner, name) : repoOnRequest(db, url, "GET", owner, name); | |
| 20 | 21 | return repo !== undefined && repo.archivedAt !== null && canRead(db, repo, user); |
| 21 | 22 | } |
src/http/request_repo.ts added +22
| @@ -0,0 +1,22 @@ | ||
| 1 | /** | |
| 2 | * The repository a page's address names, read once per request. A repository page asks for it | |
| 3 | * from the archived check, the loader, the palette and the head links; each read is a join with | |
| 4 | * the origin. Only a GET is remembered: a mutation changes the row mid-request. The memo is keyed | |
| 5 | * on the request's URL object, so it ends with the request. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { getRepo, type Repo } from "../core/repos.ts"; | |
| 9 | ||
| 10 | const seen = new WeakMap<URL, Map<string, Repo | undefined>>(); | |
| 11 | ||
| 12 | /** | |
| 13 | * `getRepo`, remembered for the rest of this GET request; `url` is the request's own URL object. | |
| 14 | */ | |
| 15 | export function repoOnRequest(db: Database, url: URL, method: string, owner: string, name: string): Repo | undefined { | |
| 16 | if (method !== "GET" && method !== "HEAD") return getRepo(db, owner, name); | |
| 17 | let byName = seen.get(url); | |
| 18 | if (byName === undefined) seen.set(url, (byName = new Map())); | |
| 19 | const key = `${owner}/${name}`; | |
| 20 | if (!byName.has(key)) byName.set(key, getRepo(db, owner, name)); | |
| 21 | return byName.get(key); | |
| 22 | } |
src/http/server.ts +1 −1
| @@ -3362,7 +3362,7 @@ async function answer( | ||
| 3362 | 3362 | if (clean !== undefined) return clean; |
| 3363 | 3363 | } |
| 3364 | 3364 | // A page under an archived repository is not indexed (`archived_noindex.ts`). |
| 3365 | const noindex = !isApiPath && routeMethod === "GET" && archivedRepoAt(app.db, matched.params, auth.user); | |
| 3365 | const noindex = !isApiPath && routeMethod === "GET" && archivedRepoAt(app.db, matched.params, auth.user, url); | |
| 3366 | 3366 | const invoke = (into: Request): Response | Promise<Response> => |
| 3367 | 3367 | matched.handler({ |
| 3368 | 3368 | app, |
src/perf/page_budgets/next.ts +1 −1
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | import type { PageBudget } from "../page_budget.ts"; |
| 2 | 2 | |
| 3 | export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 72 }; | |
| 3 | export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 66 }; |
src/perf/page_budgets/space.ts +2 −2
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import type { PageBudget } from "../page_budget.ts"; |
| 2 | 2 | |
| 3 | // The space (#12770), which the owner opens to watch growth. Statements: measured warm (15) plus | |
| 3 | // The space (#12770), which the owner opens to watch growth. Statements: measured warm (12) plus | |
| 4 | 4 | // two. |
| 5 | export const budget: PageBudget = { page: "space", pattern: "/:owner/:name/space", medianMs: 50, statements: 17 }; | |
| 5 | export const budget: PageBudget = { page: "space", pattern: "/:owner/:name/space", medianMs: 50, statements: 14 }; |
src/web/chrome.ts +4 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * `ui.ts` may not. That is the line between the two files. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { repoOnRequest } from "../http/request_repo.ts"; | |
| 11 | 12 | import { hereAddress, returnTo, withReturn } from "./return_to.ts"; |
| 12 | 13 | import { MACHINE_PREFIX, typedMiss } from "../http/asset_miss.ts"; |
| 13 | 14 | import { audienceFacts, audienceOf } from "../core/audience.ts"; |
| @@ -16,7 +17,6 @@ import { | ||
| 16 | 17 | canAdmin, |
| 17 | 18 | canRead, |
| 18 | 19 | forkParentFor, |
| 19 | getRepo, | |
| 20 | 20 | } from "../core/repos.ts"; |
| 21 | 21 | import { nearRepos as nearTo } from "../core/near_repos.ts"; |
| 22 | 22 | import { type Repo, visibleForks } from "../core/repos.ts"; |
| @@ -52,8 +52,10 @@ import { BIND, zeroAttr } from "./bind.ts"; | ||
| 52 | 52 | /** The repo, if the viewer may see it. Missing and hidden are the same |
| 53 | 53 | * answer on purpose, so a private repo never leaks its existence. */ |
| 54 | 54 | export function loadRepo(ctx: Ctx): Repo | undefined { |
| 55 | const repo = getRepo( | |
| 55 | const repo = repoOnRequest( | |
| 56 | 56 | ctx.app.db, |
| 57 | ctx.url, | |
| 58 | ctx.request.method, | |
| 57 | 59 | ctx.params.owner ?? "", |
| 58 | 60 | (ctx.params.name ?? "").replace(/\.git$/, ""), |
| 59 | 61 | ); |
src/web/layout.ts +3 −2
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * `/search`), and every dialog (a link to a real page). |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { repoOnRequest } from "../http/request_repo.ts"; | |
| 10 | 11 | import { requestHolds } from "../http/conditional.ts"; |
| 11 | 12 | import { sshOn } from "../settings/ssh.ts"; |
| 12 | 13 | import { hereAddress, returnTo, withReturn } from "./return_to.ts"; |
| @@ -456,7 +457,7 @@ function pageRepo(ctx: Ctx): Repo | undefined { | ||
| 456 | 457 | if (ctx.auth.user === undefined) return undefined; |
| 457 | 458 | const [owner, name] = ctx.url.pathname.split("/").filter((s) => s !== ""); |
| 458 | 459 | return owner !== undefined && name !== undefined && !NOT_AN_OWNER.has(owner) |
| 459 | ? getRepo(ctx.app.db, owner, name) | |
| 460 | ? repoOnRequest(ctx.app.db, ctx.url, ctx.request.method, owner, name) | |
| 460 | 461 | : undefined; |
| 461 | 462 | } |
| 462 | 463 | |
| @@ -490,7 +491,7 @@ function paletteIndex(ctx: Ctx, extra: PaletteItem[]): Html { | ||
| 490 | 491 | // The viewer's own first, apps ahead of the rest in each half, thirty in |
| 491 | 492 | // all; archived ones |
| 492 | 493 | // are put away, found by typing the full name into search. |
| 493 | const wanted = { limit: 30, appsFirst: true, withoutArchived: true }; | |
| 494 | const wanted = { limit: 30, appsFirst: true, withoutArchived: true, withoutCounts: true }; | |
| 494 | 495 | const mine = ctx.auth.user |
| 495 | 496 | ? listReposByOwner(ctx.app.db, ctx.auth.user.username, ctx.auth.user, wanted) |
| 496 | 497 | : []; |
src/web/repo_steps.ts +1 −1
| @@ -104,7 +104,7 @@ function work(ctx: Ctx, base: string, repo: Repo | undefined): Steps { | ||
| 104 | 104 | ...(forks > 0 ? { count: forks } : {}), |
| 105 | 105 | }); |
| 106 | 106 | // The runs, for who may read them (a private app's are for those who may enter it). |
| 107 | if (mayReadRuns(ctx.app, ctx.auth.user, repo)) { | |
| 107 | if (mayReadRuns(ctx.app, ctx.auth.user, repo, { app: record })) { | |
| 108 | 108 | // The number of the list behind the tab, for this reader (`workingNowCount`). |
| 109 | 109 | const running = workingNowCount(ctx.app, ctx.auth.user, repo); |
| 110 | 110 | steps.push({ |
src/web/standards.ts +2 −1
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | * FORGE_SECURITY_CONTACT. |
| 30 | 30 | */ |
| 31 | 31 | |
| 32 | import { repoOnRequest } from "../http/request_repo.ts"; | |
| 32 | 33 | import { noneMatch } from "../http/conditional.ts"; |
| 33 | 34 | import { sshOn } from "../settings/ssh.ts"; |
| 34 | 35 | import { UPSTREAM_REPO } from "../core/upstream.ts"; |
| @@ -721,7 +722,7 @@ export function pageLinks(ctx: Ctx): PageLink[] { | ||
| 721 | 722 | name !== undefined && |
| 722 | 723 | ctx.url.pathname.startsWith(`/${owner}/${name}`) |
| 723 | 724 | ) { |
| 724 | const repo = getRepo(ctx.app.db, owner, name.replace(/\.git$/, "")); | |
| 725 | const repo = repoOnRequest(ctx.app.db, ctx.url, ctx.request.method, owner, name.replace(/\.git$/, "")); | |
| 725 | 726 | if ( |
| 726 | 727 | repo !== undefined && |
| 727 | 728 | canRead(ctx.app.db, repo, undefined) && |
test/did-you-mean.test.ts +3 −3
| @@ -20,12 +20,12 @@ let servers: Awaited<ReturnType<typeof serve>>; | ||
| 20 | 20 | // 41: the repository page reads the repository's wallet cap, one primary-key read, to show the |
| 21 | 21 | // wallet line (#13309). |
| 22 | 22 | // 42: whether reports are closed, one read per request from the row (#15053). |
| 23 | const FOUND_QUERIES = 42; | |
| 23 | const FOUND_QUERIES = 37; | |
| 24 | 24 | const BARE_404_QUERIES = 0; // a 404 with no owner/name to suggest from |
| 25 | // The same 404 for a mistyped repository. One of the 7 is the noindex lookup that | |
| 25 | // The same 404 for a mistyped repository (6 since #15940). One of them is the noindex lookup that | |
| 26 | 26 | // answerGet makes for every /:owner/:name route before its handler runs (1e71740ab, #14305, |
| 27 | 27 | // src/http/archived_noindex.ts); on a missing repository it finds nothing. |
| 28 | const NEAR_404_QUERIES = 7; | |
| 28 | const NEAR_404_QUERIES = 6; | |
| 29 | 29 | const cookies: Record<string, string> = {}; |
| 30 | 30 | |
| 31 | 31 | beforeAll(async () => { |
test/pages-budget.test.ts +1 −1
| @@ -56,7 +56,7 @@ const DEFAULT = { statements: 60, git: 8 }; | ||
| 56 | 56 | const HEAVIER: Record<string, { statements?: number; git?: number }> = { |
| 57 | 57 | "/:owner/:name/issues/:number": { statements: 71 }, // +1: whether the repository is archived, so its pages say noindex (#14305) |
| 58 | 58 | "/next": { statements: 90 }, // a new page: measured 90 warm over the fixture's repositories, one ranking per administered repository, at most GROUPS_MAX = 5 (#15870) |
| 59 | "/:owner/:name/next": { statements: 72 }, // to lower: #15940; +2: the waiting offers and the decision check beside the reports (#15887); +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867) | |
| 59 | "/:owner/:name/next": { statements: 66 }, // -6 (#15940): the page's repository read once per request, the palette lists skip counts, the runs tab reuses the app read; +2: the waiting offers and the decision check beside the reports (#15887); +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867) | |
| 60 | 60 | "/:owner/:name/pulls/:number": { statements: 116 }, // +1: whether the reports door is closed, one primary-key read per request, never from memory (#15053); +1: whether the repository is archived (#14305); +1: the desk hold on the pull, one read of the notes that link it (#12536); +1: whether a repository is the forge's own copy, one read of the source setting for the tell box (#12037); +1: the paused sentence, one read of a queued paused run on the pull (#12010); +1: the header Talk count (#10327); +1: the landing line (#10860); +1: whether a reviewer is seated, read only when Require review is off and the pull would otherwise wait on one; the page loads no crew besides (#11666) |
| 61 | 61 | "/api/v1/asks": { statements: 61 }, // +1: the stopped apps someone asked for, one read of the viewer's archived repositories (#12277) |
| 62 | 62 | "/board": { statements: 72 }, // +1: the forge-copy read for the tell box (#12037); +1: the vitals read the database's growth (#12025); +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps |
test/request-repo.test.ts added +51
| @@ -0,0 +1,51 @@ | ||
| 1 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createRepo } from "../src/actions/repos.ts"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { repoOnRequest } from "../src/http/request_repo.ts"; | |
| 9 | import { once } from "./helpers/statements.ts"; | |
| 10 | ||
| 11 | let root = ""; | |
| 12 | let app: App; | |
| 13 | beforeAll(async () => { | |
| 14 | root = mkdtempSync(join(tmpdir(), "forge-request-repo-")); | |
| 15 | app = createApp({ dataDir: root, port: 0 }); | |
| 16 | const u = await createUser(app.db, { | |
| 17 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 18 | password: "a-good-passphrase", adminIfFirst: true, | |
| 19 | }); | |
| 20 | if (!u.ok) throw new Error(u.error); | |
| 21 | await createRepo(app, u.value, { name: "demo", visibility: "public" }); | |
| 22 | }); | |
| 23 | afterAll(() => { | |
| 24 | app.close(); | |
| 25 | rmSync(root, { recursive: true, force: true }); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a GET reads the repository once however many parts of the page ask", async () => { | |
| 29 | const url = new URL("http://x/alice/demo/next"); | |
| 30 | const reads = await once(app.db, () => { | |
| 31 | for (let i = 0; i < 5; i++) expect(repoOnRequest(app.db, url, "GET", "alice", "demo")?.name).toBe("demo"); | |
| 32 | }); | |
| 33 | expect(reads).toBe(1); | |
| 34 | // a missing repository is remembered as missing too | |
| 35 | const none = await once(app.db, () => { | |
| 36 | for (let i = 0; i < 3; i++) expect(repoOnRequest(app.db, url, "GET", "alice", "nope")).toBeUndefined(); | |
| 37 | }); | |
| 38 | expect(none).toBe(1); | |
| 39 | }); | |
| 40 | ||
| 41 | test("a mutation never reads a remembered row, and another request starts fresh", async () => { | |
| 42 | const url = new URL("http://x/alice/demo/settings"); | |
| 43 | const reads = await once(app.db, () => { | |
| 44 | for (let i = 0; i < 3; i++) repoOnRequest(app.db, url, "POST", "alice", "demo"); | |
| 45 | }); | |
| 46 | expect(reads).toBe(3); | |
| 47 | const fresh = await once(app.db, () => { | |
| 48 | repoOnRequest(app.db, new URL("http://x/alice/demo/next"), "GET", "alice", "demo"); | |
| 49 | }); | |
| 50 | expect(fresh).toBe(1); | |
| 51 | }); |
new above
offline