Public: anyone can read this; the crew can change it.
The API's asked list names its Server-Timing phases: session, asked, render #4972
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4948-the-api-s-asked-list-names-its-server-ti → main 1 commit fork
2 files
changed +74
−7
src/http/api.ts +13 −7
| @@ -3618,6 +3618,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3618 | 3618 | // What people asked of the crew in the repositories the caller can write — |
| 3619 | 3619 | // by default what has nothing merged yet, oldest first — and what became of each (core/asked.ts, the /asked page). |
| 3620 | 3620 | router.add("GET", "/api/v1/asked", (ctx) => { |
| 3621 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 3621 | 3622 | const denied = requireScope(ctx, "ro"); |
| 3622 | 3623 | if (denied) return denied; |
| 3623 | 3624 | const raw = ctx.url.searchParams.get("show"); |
| @@ -3626,14 +3627,19 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3626 | 3627 | const order = ctx.url.searchParams.get("order"); |
| 3627 | 3628 | if (order !== null && !isAskedOrder(order)) |
| 3628 | 3629 | return apiError("invalid_order", 422, "order is oldest or newest"); |
| 3629 | const repos = askedRepos(ctx.app.db, ctx.auth.user, ctx.url.searchParams.get("repo")); | |
| 3630 | return json({ | |
| 3631 | asked: askedIn(ctx.app.db, repos, { | |
| 3632 | show: raw ?? "unshipped", | |
| 3633 | order: order ?? "oldest", | |
| 3634 | }).map(askedJson), | |
| 3635 | thoughts: ctx.auth.user ? thoughtStream(ctx.app.db, ctx.auth.user).map(streamJson) : [], | |
| 3630 | const read = timePhase(ctx.request, "asked", () => { | |
| 3631 | const repos = askedRepos(ctx.app.db, ctx.auth.user, ctx.url.searchParams.get("repo")); | |
| 3632 | return { | |
| 3633 | asked: askedIn(ctx.app.db, repos, { | |
| 3634 | show: raw ?? "unshipped", | |
| 3635 | order: order ?? "oldest", | |
| 3636 | }), | |
| 3637 | thoughts: ctx.auth.user ? thoughtStream(ctx.app.db, ctx.auth.user) : [], | |
| 3638 | }; | |
| 3636 | 3639 | }); |
| 3640 | return timePhase(ctx.request, "render", () => | |
| 3641 | json({ asked: read.asked.map(askedJson), thoughts: read.thoughts.map(streamJson) }), | |
| 3642 | ); | |
| 3637 | 3643 | }); |
| 3638 | 3644 | // Every repository the caller can see, one entry each: the global map. |
| 3639 | 3645 | router.add("GET", "/api/v1/map", (ctx) => |
test/api-asked-timing.test.ts added +61
| @@ -0,0 +1,61 @@ | ||
| 1 | /** The API asked list names its Server-Timing phases (#4948). */ | |
| 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-asked-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 asked list names session, asked, render in order", async () => { | |
| 57 | const got = await phases("/api/v1/asked"); | |
| 58 | expect(got.status).toBe(200); | |
| 59 | expect(got.names).toEqual(["session", "asked", "render"]); | |
| 60 | expect(Array.isArray(JSON.parse(got.body).asked)).toBe(true); | |
| 61 | }); |
new above
offline