Public: anyone can read this; the crew can change it.
The API's explore read names its Server-Timing phases: session, explore, render #4023
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3921-the-api-s-explore-read-names-its-server → main 2 commits fork
2 files
changed +54
−4
src/http/api.ts +5 −4
| @@ -4884,11 +4884,12 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4884 | 4884 | |
| 4885 | 4885 | // Explore (#607): the rows /explore shows, for the same one line. |
| 4886 | 4886 | router.add("GET", "/api/v1/explore", async (ctx) => { |
| 4887 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 4887 | 4888 | const query = parseExploreQuery(ctx.url.searchParams.get("q") ?? ""); |
| 4888 | return json({ | |
| 4889 | query: exploreLine(query), | |
| 4890 | items: await explore(ctx.app, ctx.auth.user, query), | |
| 4891 | }); | |
| 4889 | const items = await timePhaseAsync(ctx.request, "explore", () => | |
| 4890 | explore(ctx.app, ctx.auth.user, query), | |
| 4891 | ); | |
| 4892 | return timePhase(ctx.request, "render", () => json({ query: exploreLine(query), items })); | |
| 4892 | 4893 | }); |
| 4893 | 4894 | |
| 4894 | 4895 | // Make it yours (#606): a fork with its app, planned unless dryRun is |
test/api-explore-timing.test.ts added +49
| @@ -0,0 +1,49 @@ | ||
| 1 | /** The API's explore read names its Server-Timing phases (#3921). */ | |
| 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 | beforeAll(async () => { | |
| 20 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-explore-timing-")); | |
| 21 | app = createApp({ dataDir, port: 0 }); | |
| 22 | servers = await serve(app); | |
| 23 | const made = await createUser(app.db, { | |
| 24 | origin: "seed", | |
| 25 | trusted: true, | |
| 26 | username: "alice", | |
| 27 | email: "[email protected]", | |
| 28 | password: "a-good-passphrase", | |
| 29 | adminIfFirst: true, | |
| 30 | }); | |
| 31 | if (!made.ok) throw new Error(String(made.error)); | |
| 32 | cookie = `forge_session=${createSession(app.db, made.value.id).token}`; | |
| 33 | }); | |
| 34 | ||
| 35 | afterAll(async () => { | |
| 36 | await servers.stop(); | |
| 37 | app.close(); | |
| 38 | await rm(dataDir, { recursive: true, force: true }); | |
| 39 | }); | |
| 40 | ||
| 41 | test("the API explore read names session, explore and render in order", async () => { | |
| 42 | const res = await fetch(`${servers.url}/api/v1/explore`, { headers: { cookie } }); | |
| 43 | const body = JSON.parse(await res.text()); | |
| 44 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 45 | expect(res.status).toBe(200); | |
| 46 | expect(names.slice(names.indexOf("app") + 1)).toEqual(["session", "explore", "render"]); | |
| 47 | expect(Array.isArray(body.items)).toBe(true); | |
| 48 | expect(typeof body.query).toBe("string"); | |
| 49 | }); |
new above
offline