Public: anyone can read this; the crew can change it.
The API's pins list names its Server-Timing phases: session, pins, render #4958
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4943-the-api-s-pins-list-names-its-server-tim → main 1 commit fork
2 files
changed +94
−26
src/http/api.ts +33 −26
| @@ -5013,35 +5013,42 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 5013 | 5013 | // clone needs: both URLs and where the repo came from. |
| 5014 | 5014 | router.add("GET", "/api/v1/user/pins", (ctx) => { |
| 5015 | 5015 | if (!ctx.auth.user) return unauthorized(); |
| 5016 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 5016 | 5017 | const host = ctx.app.config.host ?? ctx.url.hostname; |
| 5017 | 5018 | const sshPort = ctx.app.config.sshPort; |
| 5018 | const pins = listPinned(ctx.app, ctx.auth.user); | |
| 5019 | // One query for every pinned repo, not one per pin. | |
| 5020 | const repos = getReposByIds( | |
| 5021 | ctx.app.db, | |
| 5022 | pins.map((pin) => pin.repoId), | |
| 5023 | ); | |
| 5024 | const items = pins.flatMap((pin) => { | |
| 5025 | const repo = repos.get(pin.repoId); | |
| 5026 | if (!repo) return []; | |
| 5027 | const path = `${encodeURIComponent(repo.ownerName)}/${encodeURIComponent(repo.name)}`; | |
| 5028 | return [ | |
| 5029 | { | |
| 5030 | repoId: repo.id, | |
| 5031 | owner: repo.ownerName, | |
| 5032 | name: repo.name, | |
| 5033 | defaultBranch: repo.defaultBranch, | |
| 5034 | cloneUrl: `${ctx.url.origin}/${path}.git`, | |
| 5035 | sshUrl: | |
| 5036 | sshPort !== undefined && sshPort > 0 | |
| 5037 | ? `ssh://git@${host}:${sshPort}/${path}.git` | |
| 5038 | : null, | |
| 5039 | origin: originFacts(repo), | |
| 5040 | pinnedAt: pin.createdAt, | |
| 5041 | }, | |
| 5042 | ]; | |
| 5019 | const user = ctx.auth.user; | |
| 5020 | const { pins, repos } = timePhase(ctx.request, "pins", () => { | |
| 5021 | const pins = listPinned(ctx.app, user); | |
| 5022 | // One query for every pinned repo, not one per pin. | |
| 5023 | const repos = getReposByIds( | |
| 5024 | ctx.app.db, | |
| 5025 | pins.map((pin) => pin.repoId), | |
| 5026 | ); | |
| 5027 | return { pins, repos }; | |
| 5028 | }); | |
| 5029 | return timePhase(ctx.request, "render", () => { | |
| 5030 | const items = pins.flatMap((pin) => { | |
| 5031 | const repo = repos.get(pin.repoId); | |
| 5032 | if (!repo) return []; | |
| 5033 | const path = `${encodeURIComponent(repo.ownerName)}/${encodeURIComponent(repo.name)}`; | |
| 5034 | return [ | |
| 5035 | { | |
| 5036 | repoId: repo.id, | |
| 5037 | owner: repo.ownerName, | |
| 5038 | name: repo.name, | |
| 5039 | defaultBranch: repo.defaultBranch, | |
| 5040 | cloneUrl: `${ctx.url.origin}/${path}.git`, | |
| 5041 | sshUrl: | |
| 5042 | sshPort !== undefined && sshPort > 0 | |
| 5043 | ? `ssh://git@${host}:${sshPort}/${path}.git` | |
| 5044 | : null, | |
| 5045 | origin: originFacts(repo), | |
| 5046 | pinnedAt: pin.createdAt, | |
| 5047 | }, | |
| 5048 | ]; | |
| 5049 | }); | |
| 5050 | return json(items); | |
| 5043 | 5051 | }); |
| 5044 | return json(items); | |
| 5045 | 5052 | }); |
| 5046 | 5053 | |
| 5047 | 5054 | router.add("PUT", "/api/v1/user/pins/:owner/:name", (ctx) => { |
test/api-pins-timing.test.ts added +61
| @@ -0,0 +1,61 @@ | ||
| 1 | /** The API pins list names its Server-Timing phases (#4943). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let cookie = ""; | |
| 19 | ||
| 20 | const cells = process.env["FORGE_CELLS"]; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | process.env["FORGE_CELLS"] = "off"; | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-pins-timing-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const made = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "alice", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | const alice = made.value; | |
| 37 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 38 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 39 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 40 | }); | |
| 41 | afterAll(async () => { | |
| 42 | await servers.stop(); | |
| 43 | app.close(); | |
| 44 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 45 | else process.env["FORGE_CELLS"] = cells; | |
| 46 | await rm(dataDir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 50 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 51 | const body = await res.text(); | |
| 52 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 53 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 54 | } | |
| 55 | ||
| 56 | test("the pins list names session, pins, render in order", async () => { | |
| 57 | const got = await phases("/api/v1/user/pins"); | |
| 58 | expect(got.status).toBe(200); | |
| 59 | expect(got.names).toEqual(["session", "pins", "render"]); | |
| 60 | expect(Array.isArray(JSON.parse(got.body))).toBe(true); | |
| 61 | }); |
new above
offline