Public: anyone can read this; the crew can change it.
The home's archived list names its Server-Timing phases: session, each read, render #3546
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/3528-the-home-s-archived-list-names-its-serve → main 1 commit fork
2 files
changed +81
−3
src/web/studio/home_archive.ts +11 −3
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { applyFleetIds, getFleetOp, undoFleetOp } from "../../actions/fleet.ts"; |
| 9 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 9 | 10 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 11 | import { signInFirst } from "../chrome.ts"; |
| 11 | 12 | import { readForm } from "../form.ts"; |
| @@ -54,7 +55,11 @@ export async function homeArchive(ctx: Ctx): Promise<Response> { | ||
| 54 | 55 | // GET /home/archived/:op — what was archived, and the undo. |
| 55 | 56 | export function homeArchived(ctx: Ctx): Response { |
| 56 | 57 | if (!ctx.auth.user) return signInFirst(ctx); |
| 57 | const found = getFleetOp(ctx.app, ctx.auth.user, ctx.params.op ?? ""); | |
| 58 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 59 | const user = ctx.auth.user; | |
| 60 | const found = timePhase(ctx.request, "op", () => | |
| 61 | getFleetOp(ctx.app, user, ctx.params.op ?? ""), | |
| 62 | ); | |
| 58 | 63 | if (!found.ok || found.value.verb !== "archive") |
| 59 | 64 | return new Response("not found", { status: 404 }); |
| 60 | 65 | const op = found.value; |
| @@ -63,7 +68,8 @@ export function homeArchived(ctx: Ctx): Response { | ||
| 63 | 68 | const left = op.rows.filter((r) => r.outcome !== "ok"); |
| 64 | 69 | // Only the person who ran it may undo it (`undoFleetOp`), whoever may read it. |
| 65 | 70 | const canUndo = |
| 66 | op.reversible && op.undoneBy === null && !wasUndo && op.actorId === ctx.auth.user.id; | |
| 71 | op.reversible && op.undoneBy === null && !wasUndo && op.actorId === user.id; | |
| 72 | const rendering = performance.now(); | |
| 67 | 73 | const body = html`<h1>${wasUndo ? "Brought back" : "Archived"}</h1> |
| 68 | 74 | <p id="home-archived" role="status"> |
| 69 | 75 | ${rows.length} app${rows.length === 1 ? "" : "s"} |
| @@ -88,7 +94,9 @@ export function homeArchived(ctx: Ctx): Response { | ||
| 88 | 94 | ${btn("Home", { href: "/" })} |
| 89 | 95 | </div> |
| 90 | 96 | ${canUndo ? html`<p class="small muted">Undo is kept for a day.</p>` : null}`; |
| 91 | return page({ ctx, title: wasUndo ? "Brought back" : "Archived", body }); | |
| 97 | const response = page({ ctx, title: wasUndo ? "Brought back" : "Archived", body }); | |
| 98 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 99 | return response; | |
| 92 | 100 | } |
| 93 | 101 | |
| 94 | 102 | // POST /home/archived/:op/undo — the operation's own undo. |
test/home-archived-timing.test.ts added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | /** `/home/archived/:op` names session, op and render in Server-Timing (#3528). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | import { createSession } from "../src/core/sessions.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { promote } from "./helpers/members.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | ||
| 17 | let root = ""; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let cookie = ""; | |
| 21 | let repoId = ""; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | root = mkdtempSync(join(tmpdir(), "forge-home-archived-timing-")); | |
| 25 | app = createApp({ dataDir: root, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const u = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "keeper", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!u.ok) throw new Error(u.error); | |
| 36 | promote(app.db, u.value); | |
| 37 | cookie = `forge_session=${createSession(app.db, u.value.id).token}; forge_mode=studio`; | |
| 38 | const made = await createRepo(app, u.value, { name: "alpha", visibility: "public" }); | |
| 39 | if (!made.ok) throw new Error(String(made.error)); | |
| 40 | insertApp(app.db, { repoId: made.value.id, slug: "alpha", access: "public" }); | |
| 41 | repoId = made.value.id; | |
| 42 | }, 30_000); | |
| 43 | afterAll(() => { | |
| 44 | servers.stop(); | |
| 45 | app.close(); | |
| 46 | rmSync(root, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | test("GET /home/archived/:op names its phases", async () => { | |
| 50 | const archived = await fetch(`${servers.url}/home/archive`, { | |
| 51 | method: "POST", | |
| 52 | headers: { | |
| 53 | cookie, | |
| 54 | origin: servers.url, | |
| 55 | "content-type": "application/x-www-form-urlencoded", | |
| 56 | }, | |
| 57 | body: `repo=${repoId}`, | |
| 58 | redirect: "manual", | |
| 59 | }); | |
| 60 | expect(archived.status).toBe(303); | |
| 61 | const receipt = archived.headers.get("location")!; | |
| 62 | expect(receipt).toStartWith("/home/archived/"); | |
| 63 | const res = await fetch(`${servers.url}${receipt}`, { headers: { cookie }, redirect: "manual" }); | |
| 64 | const body = await res.text(); | |
| 65 | expect(res.status).toBe(200); | |
| 66 | expect(body).toContain("<h1>Archived</h1>"); | |
| 67 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 68 | for (const name of ["app", "session", "op", "render"]) | |
| 69 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 70 | }); |
new above
offline