Public: anyone can read this; the crew can change it.
The API's front page read names its Server-Timing phases: session, front, render #5025
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/5009-the-api-s-front-page-read-names-its-serv → main 1 commit fork
2 files
changed +37
−3
src/http/api.ts +5 −3
| @@ -5145,9 +5145,11 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 5145 | 5145 | |
| 5146 | 5146 | // The front page (#686): what a stranger is shown, for anyone; a site |
| 5147 | 5147 | // admin also gets the picks, and sets them — pinned in order, hidden. |
| 5148 | router.add("GET", "/api/v1/front", (ctx) => | |
| 5149 | json(frontJson(ctx.app, ctx.auth.user)), | |
| 5150 | ); | |
| 5148 | router.add("GET", "/api/v1/front", (ctx) => { | |
| 5149 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 5150 | const front = timePhase(ctx.request, "front", () => frontJson(ctx.app, ctx.auth.user)); | |
| 5151 | return timePhase(ctx.request, "render", () => json(front)); | |
| 5152 | }); | |
| 5151 | 5153 | |
| 5152 | 5154 | router.add("PUT", "/api/v1/front", async (ctx) => { |
| 5153 | 5155 | const input = await body(ctx.request); |
test/front-api-timing.test.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** GET /api/v1/front names its phases in Server-Timing. */ | |
| 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 { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | ||
| 9 | let dataDir = ""; | |
| 10 | let app: App, servers: Servers; | |
| 11 | ||
| 12 | beforeAll(async () => { | |
| 13 | dataDir = await mkdtemp(join(tmpdir(), "forge-ftime-")); | |
| 14 | app = createApp({ dataDir, port: 0 }); | |
| 15 | servers = await serve(app); | |
| 16 | }); | |
| 17 | ||
| 18 | afterAll(async () => { | |
| 19 | servers.stop(); | |
| 20 | app.close(); | |
| 21 | await rm(dataDir, { recursive: true, force: true }); | |
| 22 | }); | |
| 23 | ||
| 24 | test("the front read names session, front, render in order", async () => { | |
| 25 | const res = await fetch(`${servers.url}/api/v1/front`); | |
| 26 | await res.arrayBuffer(); | |
| 27 | expect(res.status).toBe(200); | |
| 28 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 29 | const names = parts.map((p) => p.split(";")[0]!).filter((n) => n !== "app"); | |
| 30 | expect(names).toEqual(["session", "front", "render"]); | |
| 31 | for (const p of parts) expect(Number(/dur=([\d.]+)/.exec(p)?.[1])).not.toBeNaN(); | |
| 32 | }); |
new above
offline