Public: anyone can read this; the crew can change it.
The admin API's settings, limits and capacity reads name their Server-Timing phases #4989
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4979-the-admin-api-s-settings-limits-and-capa → main 1 commit fork
2 files
changed +61
−7
src/http/api.ts +13 −7
| @@ -1628,10 +1628,11 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1628 | 1628 | // The forge's capacity (`core/capacity.ts`): read and changed live by an |
| 1629 | 1629 | // admin, never by an agent; anyone else is told the door is not there. |
| 1630 | 1630 | router.add("GET", "/api/v1/admin/capacity", (ctx) => { |
| 1631 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1631 | 1632 | const denied = accountAdmin(ctx, "ro"); |
| 1632 | 1633 | if (denied) return denied; |
| 1633 | const listed = listCapacity(ctx.app, ctx.auth.user); | |
| 1634 | return listed.ok ? json(listed.value) : notFound(); | |
| 1634 | const listed = timePhase(ctx.request, "capacity", () => listCapacity(ctx.app, ctx.auth.user)); | |
| 1635 | return timePhase(ctx.request, "render", () => (listed.ok ? json(listed.value) : notFound())); | |
| 1635 | 1636 | }); |
| 1636 | 1637 | |
| 1637 | 1638 | router.add("PUT", "/api/v1/admin/capacity/:key", async (ctx) => { |
| @@ -1653,10 +1654,11 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1653 | 1654 | |
| 1654 | 1655 | // The forge's limits (`settings/limits.ts`): the same door as capacity. |
| 1655 | 1656 | router.add("GET", "/api/v1/admin/limits", (ctx) => { |
| 1657 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1656 | 1658 | const denied = accountAdmin(ctx, "ro"); |
| 1657 | 1659 | if (denied) return denied; |
| 1658 | const listed = listLimits(ctx.app, ctx.auth.user); | |
| 1659 | return listed.ok ? json(listed.value) : notFound(); | |
| 1660 | const listed = timePhase(ctx.request, "limits", () => listLimits(ctx.app, ctx.auth.user)); | |
| 1661 | return timePhase(ctx.request, "render", () => (listed.ok ? json(listed.value) : notFound())); | |
| 1660 | 1662 | }); |
| 1661 | 1663 | |
| 1662 | 1664 | router.add("PUT", "/api/v1/admin/limits/:key", async (ctx) => { |
| @@ -1723,11 +1725,15 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1723 | 1725 | // Per-account quotas (`core/quotas.ts`): null is no limit. Nothing |
| 1724 | 1726 | // enforces them yet. |
| 1725 | 1727 | router.add("GET", "/api/v1/admin/settings", (ctx) => { |
| 1728 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1726 | 1729 | const denied = accountAdmin(ctx, "ro"); |
| 1727 | 1730 | if (denied) return denied; |
| 1728 | const read = readQuotas(ctx.app, ctx.auth.user); | |
| 1729 | const signup = readSignup(ctx.app, ctx.auth.user); | |
| 1730 | return read.ok && signup.ok ? json({ ...read.value, ...signup.value, signupChanges: signup.value.changes, changes: read.value.changes }) : notFound(); | |
| 1731 | const { read, signup } = timePhase(ctx.request, "settings", () => ({ | |
| 1732 | read: readQuotas(ctx.app, ctx.auth.user), | |
| 1733 | signup: readSignup(ctx.app, ctx.auth.user), | |
| 1734 | })); | |
| 1735 | return timePhase(ctx.request, "render", () => | |
| 1736 | read.ok && signup.ok ? json({ ...read.value, ...signup.value, signupChanges: signup.value.changes, changes: read.value.changes }) : notFound()); | |
| 1731 | 1737 | }); |
| 1732 | 1738 | |
| 1733 | 1739 | router.add("PATCH", "/api/v1/admin/settings", async (ctx) => { |
test/admin-read-timing.test.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | /** The admin settings, limits and capacity reads name their 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 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 9 | ||
| 10 | let dataDir = ""; | |
| 11 | let app: App, servers: Servers, bearer = ""; | |
| 12 | ||
| 13 | beforeAll(async () => { | |
| 14 | dataDir = await mkdtemp(join(tmpdir(), "forge-atime-")); | |
| 15 | app = createApp({ dataDir, port: 0 }); | |
| 16 | servers = await serve(app); | |
| 17 | await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 18 | const minted = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 19 | method: "POST", | |
| 20 | headers: { "content-type": "application/json" }, | |
| 21 | body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }), | |
| 22 | }); | |
| 23 | bearer = ((await minted.json()) as { token: string }).token; | |
| 24 | }); | |
| 25 | ||
| 26 | afterAll(async () => { | |
| 27 | servers.stop(); | |
| 28 | app.close(); | |
| 29 | await rm(dataDir, { recursive: true, force: true }); | |
| 30 | }); | |
| 31 | ||
| 32 | const phases = (res: Response) => (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 33 | ||
| 34 | for (const [route, read] of [["settings", "settings"], ["limits", "limits"], ["capacity", "capacity"]] as const) { | |
| 35 | test(`GET /admin/${route} names session, ${read}, render in order`, async () => { | |
| 36 | const res = await fetch(`${servers.url}/api/v1/admin/${route}`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 37 | await res.arrayBuffer(); | |
| 38 | expect(res.status).toBe(200); | |
| 39 | expect(phases(res).filter((n) => ["session", read, "render"].includes(n))).toEqual(["session", read, "render"]); | |
| 40 | }); | |
| 41 | ||
| 42 | test(`a refused GET /admin/${route} still names session`, async () => { | |
| 43 | const res = await fetch(`${servers.url}/api/v1/admin/${route}`); | |
| 44 | await res.arrayBuffer(); | |
| 45 | expect(res.status).not.toBe(200); | |
| 46 | expect(phases(res)).toContain("session"); | |
| 47 | }); | |
| 48 | } |
new above
offline