Public: anyone can read this; the crew can change it.
The access settings page's Server-Timing names its phases: session, each read, render #3533
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3498-the-access-settings-page-s-server-timing → main 1 commit fork
2 files
changed +67
−5
src/web/pages/access.ts +13 −5
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { listAccess } from "../../actions/access_log.ts"; |
| 9 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 9 | 10 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 11 | import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts"; |
| 11 | 12 | import { html } from "../html.ts"; |
| @@ -15,14 +16,19 @@ import { accountSections } from "./origins.ts"; | ||
| 15 | 16 | |
| 16 | 17 | // GET /settings/access |
| 17 | 18 | export function accessPage(ctx: Ctx): Response { |
| 19 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 18 | 20 | if (!ctx.auth.user) return signInFirst(ctx); |
| 21 | const user = ctx.auth.user; | |
| 19 | 22 | const ua = ctx.url.searchParams.get("ua")?.slice(0, 100) ?? ""; |
| 20 | 23 | const path = ctx.url.searchParams.get("path")?.slice(0, 200) ?? ""; |
| 21 | const listed = listAccess(ctx.app, ctx.auth.user, { | |
| 22 | ...(ua === "" ? {} : { ua }), | |
| 23 | ...(path === "" ? {} : { path }), | |
| 24 | }); | |
| 24 | const listed = timePhase(ctx.request, "access", () => | |
| 25 | listAccess(ctx.app, user, { | |
| 26 | ...(ua === "" ? {} : { ua }), | |
| 27 | ...(path === "" ? {} : { path }), | |
| 28 | }), | |
| 29 | ); | |
| 25 | 30 | if (!listed.ok) return notFoundPage(ctx); |
| 31 | const rendering = performance.now(); | |
| 26 | 32 | const rows = listed.value.map((l) => |
| 27 | 33 | row({ |
| 28 | 34 | lead: icon("activity"), |
| @@ -73,7 +79,7 @@ export function accessPage(ctx: Ctx): Response { | ||
| 73 | 79 | pad: false, |
| 74 | 80 | }, |
| 75 | 81 | )}`; |
| 76 | return page({ | |
| 82 | const response = page({ | |
| 77 | 83 | ctx, |
| 78 | 84 | title: "Access", |
| 79 | 85 | body: html`<div class="page-head"><h1>Access</h1></div> |
| @@ -83,4 +89,6 @@ export function accessPage(ctx: Ctx): Response { | ||
| 83 | 89 | "Account settings", |
| 84 | 90 | )}`, |
| 85 | 91 | }); |
| 92 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 93 | return response; | |
| 86 | 94 | } |
test/access-timing.test.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** `/settings/access` names session, access and render in Server-Timing (#3281). */ | |
| 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 { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { proveAll } from "./helpers/verified.ts"; | |
| 10 | import { quietLogs } from "./helpers/globals.ts"; | |
| 11 | ||
| 12 | quietLogs(); | |
| 13 | ||
| 14 | let root = ""; | |
| 15 | let app: App; | |
| 16 | let servers: Servers; | |
| 17 | ||
| 18 | beforeAll(async () => { | |
| 19 | root = mkdtempSync(join(tmpdir(), "forge-access-timing-")); | |
| 20 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 21 | const u = await createUser(app.db, { | |
| 22 | origin: "seed", | |
| 23 | trusted: true, | |
| 24 | username: "alice", | |
| 25 | email: "[email protected]", | |
| 26 | password: "a-good-passphrase", | |
| 27 | adminIfFirst: true, | |
| 28 | }); | |
| 29 | if (!u.ok) throw new Error(u.error); | |
| 30 | proveAll(app); | |
| 31 | servers = await serve(app); | |
| 32 | }); | |
| 33 | afterAll(() => { | |
| 34 | servers.stop(); | |
| 35 | app.close(); | |
| 36 | rmSync(root, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("GET /settings/access names its phases", async () => { | |
| 40 | const signin = await fetch(`${servers.url}/signin`, { | |
| 41 | method: "POST", | |
| 42 | headers: { origin: servers.url }, | |
| 43 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 44 | redirect: "manual", | |
| 45 | }); | |
| 46 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 47 | const res = await fetch(`${servers.url}/settings/access`, { headers: { cookie } }); | |
| 48 | const body = await res.text(); | |
| 49 | expect(res.status).toBe(200); | |
| 50 | expect(body).toContain("<h1>Access</h1>"); | |
| 51 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 52 | for (const name of ["app", "session", "access", "render"]) | |
| 53 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 54 | }); |
new above
offline