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

Merged opened this

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

Merged

scout/forge:scout/3498-the-access-settings-page-s-server-timing → main 1 commit fork

  • Merged. scout/3498-the-access-settings-page-s-server-timing was merged into main as 062f1cc.

Review

Sign in to review this change.
2 files changed +67 −5
src/web/pages/access.ts +13 −5
@@ -6,6 +6,7 @@
66 */
77
88 import { listAccess } from "../../actions/access_log.ts";
9 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
910 import type { Ctx } from "../../http/context.ts";
1011 import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts";
1112 import { html } from "../html.ts";
@@ -15,14 +16,19 @@ import { accountSections } from "./origins.ts";
1516
1617 // GET /settings/access
1718 export function accessPage(ctx: Ctx): Response {
19 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
1820 if (!ctx.auth.user) return signInFirst(ctx);
21 const user = ctx.auth.user;
1922 const ua = ctx.url.searchParams.get("ua")?.slice(0, 100) ?? "";
2023 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 );
2530 if (!listed.ok) return notFoundPage(ctx);
31 const rendering = performance.now();
2632 const rows = listed.value.map((l) =>
2733 row({
2834 lead: icon("activity"),
@@ -73,7 +79,7 @@ export function accessPage(ctx: Ctx): Response {
7379 pad: false,
7480 },
7581 )}`;
76 return page({
82 const response = page({
7783 ctx,
7884 title: "Access",
7985 body: html`<div class="page-head"><h1>Access</h1></div>
@@ -83,4 +89,6 @@ export function accessPage(ctx: Ctx): Response {
8389 "Account settings",
8490 )}`,
8591 });
92 notePhase(ctx.request, "render", performance.now() - rendering);
93 return response;
8694 }
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs