Public: anyone can read this; the crew can change it.
The API's person read names its Server-Timing phases: session, person, render #4937
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4921-the-api-s-person-read-names-its-server-t → main 1 commit fork
2 files
changed +71
−4
src/http/api.ts +7 −4
| @@ -1283,10 +1283,13 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1283 | 1283 | }); |
| 1284 | 1284 | |
| 1285 | 1285 | router.add("GET", "/api/v1/users/:username", (ctx) => { |
| 1286 | const user = getUserByName(ctx.app.db, ctx.params.username!); | |
| 1287 | return user && !hiddenFrom(user, ctx.auth.user) | |
| 1288 | ? json(publicProfile(user)) | |
| 1289 | : notFound(); | |
| 1286 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1287 | const user = timePhase(ctx.request, "person", () => { | |
| 1288 | const found = getUserByName(ctx.app.db, ctx.params.username!); | |
| 1289 | return found && !hiddenFrom(found, ctx.auth.user) ? found : null; | |
| 1290 | }); | |
| 1291 | if (!user) return notFound(); | |
| 1292 | return timePhase(ctx.request, "render", () => json(publicProfile(user))); | |
| 1290 | 1293 | }); |
| 1291 | 1294 | |
| 1292 | 1295 | router.add("GET", "/api/v1/users/:username/repos", (ctx) => { |
test/api-person-timing.test.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** The API person read names its Server-Timing phases (#3889). */ | |
| 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 { createSession } from "../src/core/sessions.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let dataDir: string; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | let cookie = ""; | |
| 18 | ||
| 19 | const cells = process.env["FORGE_CELLS"]; | |
| 20 | ||
| 21 | beforeAll(async () => { | |
| 22 | process.env["FORGE_CELLS"] = "off"; | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-person-timing-")); | |
| 24 | app = createApp({ dataDir, port: 0 }); | |
| 25 | servers = await serve(app); | |
| 26 | const made = await createUser(app.db, { | |
| 27 | origin: "seed", | |
| 28 | trusted: true, | |
| 29 | username: "alice", | |
| 30 | email: "[email protected]", | |
| 31 | password: "a-good-passphrase", | |
| 32 | adminIfFirst: true, | |
| 33 | }); | |
| 34 | if (!made.ok) throw new Error(String(made.error)); | |
| 35 | const alice = made.value; | |
| 36 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 37 | }); | |
| 38 | afterAll(async () => { | |
| 39 | await servers.stop(); | |
| 40 | app.close(); | |
| 41 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 42 | else process.env["FORGE_CELLS"] = cells; | |
| 43 | await rm(dataDir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 47 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 48 | const body = await res.text(); | |
| 49 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 50 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 51 | } | |
| 52 | ||
| 53 | test("the person read names session, person, render in order", async () => { | |
| 54 | const got = await phases("/api/v1/users/alice"); | |
| 55 | expect(got.status).toBe(200); | |
| 56 | expect(got.names).toEqual(["session", "person", "render"]); | |
| 57 | expect(JSON.parse(got.body).username).toBe("alice"); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a missing person is a 404 naming session and person", async () => { | |
| 61 | const got = await phases("/api/v1/users/nobody"); | |
| 62 | expect(got.status).toBe(404); | |
| 63 | expect(got.names).toEqual(["session", "person"]); | |
| 64 | }); |
new above
offline