Public: anyone can read this; the crew can change it.

The API's returns read names its Server-Timing phases: session, returns, render #4962

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/4949-the-api-s-returns-read-names-its-server → main 1 commit fork

  • Merged. scout/4949-the-api-s-returns-read-names-its-server was merged into main as 272a9fd.

Review

Sign in to review this change.
2 files changed +69 −5
src/http/api.ts +11 −5
@@ -5808,13 +5808,19 @@ export function apiRoutes(router: Router<Handler>): void {
58085808 // What the tokens bought (`actions/returns.ts`): owner facts only, over
58095809 // every repository the viewer holds them for, or `?repo=owner/name`.
58105810 router.add("GET", "/api/v1/returns", (ctx) => {
5811 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
58115812 const q = ctx.url.searchParams;
58125813 const raw = q.get("days");
5813 const read = readReturns(ctx.app, ctx.auth.user, {
5814 repo: q.get("repo"),
5815 ...(raw === null ? {} : { days: Number(raw) }),
5816 });
5817 if (read.ok) return json(read.value, 200, { "cache-control": "private, no-store" });
5814 const read = timePhase(ctx.request, "returns", () =>
5815 readReturns(ctx.app, ctx.auth.user, {
5816 repo: q.get("repo"),
5817 ...(raw === null ? {} : { days: Number(raw) }),
5818 }),
5819 );
5820 if (read.ok)
5821 return timePhase(ctx.request, "render", () =>
5822 json(read.value, 200, { "cache-control": "private, no-store" }),
5823 );
58185824 if (read.error === "unauthorized") return unauthorized();
58195825 if (read.error === "bad_days")
58205826 return apiError("invalid_days", 422, `days is a whole number from 1 to ${RETURNS_DAYS.max}`);
test/api-returns-timing.test.ts added +58
@@ -0,0 +1,58 @@
1 /** The API returns read names its Server-Timing phases (#4949). */
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-returns-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 returns read names session, returns, render in order", async () => {
54 const got = await phases("/api/v1/returns");
55 expect(got.status).toBe(200);
56 expect(got.names).toEqual(["session", "returns", "render"]);
57 expect(typeof JSON.parse(got.body)).toBe("object");
58 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs