Public: anyone can read this; the crew can change it.
A repository's waves page's Server-Timing names its phases: session, each read, render #3336
scout/forge:scout/3282-a-repository-s-waves-page-s-server-timin → main 3 commits fork
2 files
changed +97
−5
src/web/pages/waves.ts +13 −5
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 10 | 10 | import { repoDir } from "../../git/repos.ts"; |
| 11 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; | |
| 11 | 12 | import type { Ctx } from "../../http/context.ts"; |
| 12 | 13 | import { VERSION } from "../../story/wave.ts"; |
| 13 | 14 | import { readWave, readWaves } from "../../story/waves.ts"; |
| @@ -19,14 +20,15 @@ import { localScope } from "../scope.ts"; | ||
| 19 | 20 | |
| 20 | 21 | // GET /:owner/:name/waves |
| 21 | 22 | export async function wavesPage(ctx: Ctx): Promise<Response> { |
| 22 | const repo = loadRepo(ctx); | |
| 23 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 24 | const repo = timePhase(ctx.request, "repo", () => loadRepo(ctx)); | |
| 23 | 25 | if (!repo) return notFoundPage(ctx); |
| 24 | 26 | const scope = localScope(ctx, repo); |
| 25 | 27 | const branch = repo.defaultBranch; |
| 26 | const entries = await readWaves( | |
| 27 | repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), | |
| 28 | branch, | |
| 28 | const entries = await timePhaseAsync(ctx.request, "waves", () => | |
| 29 | readWaves(repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), branch), | |
| 29 | 30 | ); |
| 31 | const rendering = performance.now(); | |
| 30 | 32 | const fileHref = (file: string): string => |
| 31 | 33 | VERSION.test(file.slice(0, -3)) |
| 32 | 34 | ? `${repoBase(repo)}/waves/${encodeURIComponent(file.slice(0, -3))}` |
| @@ -53,13 +55,15 @@ export async function wavesPage(ctx: Ctx): Promise<Response> { | ||
| 53 | 55 | : listCard(rows, { head: html`<h2>Waves</h2>` })} |
| 54 | 56 | </div>`; |
| 55 | 57 | |
| 56 | return page({ | |
| 58 | const response = page({ | |
| 57 | 59 | ctx, |
| 58 | 60 | title: `Waves · ${scope.facts.name}`, |
| 59 | 61 | crumbs: scope.crumbs, |
| 60 | 62 | palette: scope.palette, |
| 61 | 63 | body, |
| 62 | 64 | }); |
| 65 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 66 | return response; | |
| 63 | 67 | } |
| 64 | 68 | |
| 65 | 69 | /** Only http(s) becomes a link; anything else is shown as plain text. */ |
| @@ -70,7 +74,11 @@ export async function wavePage(ctx: Ctx): Promise<Response> { | ||
| 70 | 74 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 71 | 75 | const repo = timePhase(ctx.request, "repo", () => loadRepo(ctx)); |
| 72 | 76 | if (!repo) return notFoundPage(ctx); |
| 77 | <<<<<<< HEAD | |
| 78 | const entry = await timePhaseAsync(ctx.request, "waves", () => | |
| 79 | ======= | |
| 73 | 80 | const entry = await timePhaseAsync(ctx.request, "wave", () => |
| 81 | >>>>>>> origin/main | |
| 74 | 82 | readWave( |
| 75 | 83 | repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), |
| 76 | 84 | repo.defaultBranch, |
test/waves-timing.test.ts added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | /** `/:owner/:name/waves` names session, repo, waves and render in Server-Timing (#3282). */ | |
| 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 { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { proveAll } from "./helpers/verified.ts"; | |
| 11 | import { writeWave } from "../src/story/wave.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let root = ""; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | root = mkdtempSync(join(tmpdir(), "forge-waves-timing-")); | |
| 22 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 23 | const u = await createUser(app.db, { | |
| 24 | origin: "seed", | |
| 25 | trusted: true, | |
| 26 | username: "alice", | |
| 27 | email: "[email protected]", | |
| 28 | password: "a-good-passphrase", | |
| 29 | adminIfFirst: true, | |
| 30 | }); | |
| 31 | if (!u.ok) throw new Error(u.error); | |
| 32 | proveAll(app); | |
| 33 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 34 | if (!made.ok) throw new Error(made.error); | |
| 35 | const w = writeWave({ | |
| 36 | version: "0.1.0", | |
| 37 | date: "2026-01-01", | |
| 38 | headline: "First", | |
| 39 | highlights: [1, 2, 3].map((n) => ({ title: `Thing ${n}`, why: "because" })), | |
| 40 | pulls: [{ title: "#1 A pull", url: "https://forge.example/o/r/pulls/1" }], | |
| 41 | }); | |
| 42 | if (!w.ok) throw new Error("wave"); | |
| 43 | const put = await commitToBranch(app, { id: u.value.id, username: "alice" } as never, made.value, { | |
| 44 | message: "wave", | |
| 45 | files: [{ path: "waves/0.1.0.md", content: w.value }], | |
| 46 | }); | |
| 47 | if (!put.ok) throw new Error(JSON.stringify(put.error)); | |
| 48 | servers = await serve(app); | |
| 49 | }); | |
| 50 | afterAll(() => { | |
| 51 | servers.stop(); | |
| 52 | app.close(); | |
| 53 | rmSync(root, { recursive: true, force: true }); | |
| 54 | }); | |
| 55 | ||
| 56 | const phases = ["app", "session", "repo", "waves", "render"]; | |
| 57 | const signin = async (): Promise<string> => { | |
| 58 | const res = await fetch(`${servers.url}/signin`, { | |
| 59 | method: "POST", | |
| 60 | headers: { origin: servers.url }, | |
| 61 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 62 | redirect: "manual", | |
| 63 | }); | |
| 64 | return (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 65 | }; | |
| 66 | const expectPhases = (res: Response): void => { | |
| 67 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 68 | for (const name of phases) | |
| 69 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 70 | }; | |
| 71 | ||
| 72 | test("GET /:owner/:name/waves names its phases, listing waves", async () => { | |
| 73 | const res = await fetch(`${servers.url}/alice/forge/waves`, { headers: { cookie: await signin() } }); | |
| 74 | expect(res.status).toBe(200); | |
| 75 | expect(await res.text()).toContain("First"); | |
| 76 | expectPhases(res); | |
| 77 | }); | |
| 78 | ||
| 79 | test("GET /:owner/:name/waves/:version names its phases", async () => { | |
| 80 | const res = await fetch(`${servers.url}/alice/forge/waves/0.1.0`, { headers: { cookie: await signin() } }); | |
| 81 | expect(res.status).toBe(200); | |
| 82 | expect(await res.text()).toContain("Thing 1"); | |
| 83 | expectPhases(res); | |
| 84 | }); |
new above
offline