Public: anyone can read this; the crew can change it.
The new repository page's Server-Timing names its phases: session, each read, render #3523
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3507-the-new-repository-page-s-server-timing → main 1 commit fork
2 files
changed +84
−13
src/web/pages/site.ts +27 −13
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * typed still in it. A dead end that loses input is a bug, not a message. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 11 | 12 | import { PASSWORD_TOO_LONG } from "../../core/hash-gate.ts"; |
| 12 | 13 | import { NOT_A_MEMBER } from "../../core/roles.ts"; |
| 13 | 14 | import { storageLimitMessage } from "../../core/storage.ts"; |
| @@ -731,13 +732,16 @@ interface RepoValues { | ||
| 731 | 732 | visibility: string; |
| 732 | 733 | } |
| 733 | 734 | |
| 734 | function newRepoForm(ctx: Ctx, values: RepoValues, error?: string): Html { | |
| 735 | const user = ctx.auth.user!; | |
| 736 | // Owners the user may create under: themselves, plus orgs they can write to. | |
| 737 | const writeOrgs = listOrgsForUser(ctx.app.db, user.id).filter((org) => | |
| 738 | accessAllows(orgAccess(ctx.app.db, org.id, user), "write"), | |
| 735 | /** Owners the user may create under: themselves, plus orgs they can write to. */ | |
| 736 | function ownersFor(ctx: Ctx, user: User): string[] { | |
| 737 | const orgs = timePhase(ctx.request, "orgs", () => listOrgsForUser(ctx.app.db, user.id)); | |
| 738 | const writeOrgs = timePhase(ctx.request, "access", () => | |
| 739 | orgs.filter((org) => accessAllows(orgAccess(ctx.app.db, org.id, user), "write")), | |
| 739 | 740 | ); |
| 740 | const owners = [user.username, ...writeOrgs.map((o) => o.username)]; | |
| 741 | return [user.username, ...writeOrgs.map((o) => o.username)]; | |
| 742 | } | |
| 743 | ||
| 744 | function newRepoForm(ctx: Ctx, values: RepoValues, owners: string[], error?: string): Html { | |
| 741 | 745 | |
| 742 | 746 | return html`<div class="narrow-wide"> |
| 743 | 747 | ${pageHead("New repository", { |
| @@ -798,16 +802,25 @@ function newRepoForm(ctx: Ctx, values: RepoValues, error?: string): Html { | ||
| 798 | 802 | |
| 799 | 803 | export function newRepoPage(ctx: Ctx): Response { |
| 800 | 804 | if (!ctx.auth.user) return signInFirst(ctx); |
| 801 | return page({ | |
| 805 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 806 | const owners = ownersFor(ctx, ctx.auth.user); | |
| 807 | const rendering = performance.now(); | |
| 808 | const response = page({ | |
| 802 | 809 | ctx, |
| 803 | 810 | title: "New repository", |
| 804 | body: newRepoForm(ctx, { | |
| 805 | owner: ctx.url.searchParams.get("owner") ?? ctx.auth.user.username, | |
| 806 | name: "", | |
| 807 | description: "", | |
| 808 | visibility: "private", | |
| 809 | }), | |
| 811 | body: newRepoForm( | |
| 812 | ctx, | |
| 813 | { | |
| 814 | owner: ctx.url.searchParams.get("owner") ?? ctx.auth.user.username, | |
| 815 | name: "", | |
| 816 | description: "", | |
| 817 | visibility: "private", | |
| 818 | }, | |
| 819 | owners, | |
| 820 | ), | |
| 810 | 821 | }); |
| 822 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 823 | return response; | |
| 811 | 824 | } |
| 812 | 825 | |
| 813 | 826 | export async function newRepoSubmit(ctx: Ctx): Promise<Response> { |
| @@ -844,6 +857,7 @@ export async function newRepoSubmit(ctx: Ctx): Promise<Response> { | ||
| 844 | 857 | body: newRepoForm( |
| 845 | 858 | ctx, |
| 846 | 859 | values, |
| 860 | ownersFor(ctx, ctx.auth.user), | |
| 847 | 861 | messages[created.error] ?? "That name won't work.", |
| 848 | 862 | ), |
| 849 | 863 | }, |
test/new-repo-timing.test.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** `/new` names session, orgs, access and render in Server-Timing (#3507). */ | |
| 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 { 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 { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let root = ""; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | ||
| 19 | beforeAll(async () => { | |
| 20 | root = mkdtempSync(join(tmpdir(), "forge-new-repo-timing-")); | |
| 21 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 22 | const u = await createUser(app.db, { | |
| 23 | origin: "seed", | |
| 24 | trusted: true, | |
| 25 | username: "alice", | |
| 26 | email: "[email protected]", | |
| 27 | password: "a-good-passphrase", | |
| 28 | adminIfFirst: true, | |
| 29 | }); | |
| 30 | if (!u.ok) throw new Error(u.error); | |
| 31 | proveAll(app); | |
| 32 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 33 | if (!made.ok) throw new Error(made.error); | |
| 34 | servers = await serve(app); | |
| 35 | }); | |
| 36 | afterAll(() => { | |
| 37 | servers.stop(); | |
| 38 | app.close(); | |
| 39 | rmSync(root, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | test("GET /new names its phases", async () => { | |
| 43 | const signin = await fetch(`${servers.url}/signin`, { | |
| 44 | method: "POST", | |
| 45 | headers: { origin: servers.url }, | |
| 46 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 47 | redirect: "manual", | |
| 48 | }); | |
| 49 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 50 | const res = await fetch(`${servers.url}/new`, { headers: { cookie } }); | |
| 51 | const body = await res.text(); | |
| 52 | expect(res.status).toBe(200); | |
| 53 | expect(body).toContain("New repository"); | |
| 54 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 55 | for (const name of ["app", "session", "orgs", "access", "render"]) | |
| 56 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 57 | }); |
new above
offline