Public: anyone can read this; the crew can change it.
The file editor page's Server-Timing names its phases: session, each read, render #3535
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3499-the-file-editor-page-s-server-timing-nam → main 2 commits fork
2 files
changed +74
−7
src/web/pages/edit.ts +13 −7
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import { editFiles, MAX_FILE_BYTES } from "../../actions/contents.ts"; |
| 21 | 21 | import { contribute } from "../../actions/contribute.ts"; |
| 22 | 22 | import { canAdmin, getRepoById } from "../../core/repos.ts"; |
| 23 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; | |
| 23 | 24 | import { isSafeRef, isSafeTreePath } from "../../git/guard.ts"; |
| 24 | 25 | import type { Ctx } from "../../http/context.ts"; |
| 25 | 26 | import { highlightAsset } from "../asset.ts"; |
| @@ -557,13 +558,16 @@ async function mustBranchFrom( | ||
| 557 | 558 | |
| 558 | 559 | // GET /:owner/:name/edit/:ref/*path |
| 559 | 560 | export async function editPage(ctx: Ctx): Promise<Response> { |
| 560 | const found = editContext(ctx); | |
| 561 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 562 | const found = timePhase(ctx.request, "repo", () => editContext(ctx)); | |
| 561 | 563 | if (found instanceof Response) return found; |
| 562 | 564 | const { scope, ref } = found; |
| 563 | 565 | const path = ctx.params.path ?? ""; |
| 564 | 566 | if (!isSafeTreePath(path)) return notFoundPage(ctx); |
| 565 | 567 | |
| 566 | const blob = await scope.source.blob(ref, path); | |
| 568 | const blob = await timePhaseAsync(ctx.request, "blob", () => | |
| 569 | scope.source.blob(ref, path), | |
| 570 | ); | |
| 567 | 571 | if (!blob.ok) return notFoundPage(ctx); |
| 568 | 572 | if (blob.value.kind !== "text") { |
| 569 | 573 | return flashTo( |
| @@ -578,15 +582,17 @@ export async function editPage(ctx: Ctx): Promise<Response> { | ||
| 578 | 582 | }, |
| 579 | 583 | ); |
| 580 | 584 | } |
| 581 | return await editorPage( | |
| 582 | ctx, | |
| 583 | scope, | |
| 584 | await blankDraft(ctx, scope, ref, { | |
| 585 | const content = new TextDecoder().decode(blob.value.body); | |
| 586 | const draft = await timePhaseAsync(ctx.request, "refs", () => | |
| 587 | blankDraft(ctx, scope, ref, { | |
| 585 | 588 | path, |
| 586 | content: new TextDecoder().decode(blob.value.body), | |
| 589 | content, | |
| 587 | 590 | creating: false, |
| 588 | 591 | }), |
| 589 | 592 | ); |
| 593 | return await timePhaseAsync(ctx.request, "render", () => | |
| 594 | editorPage(ctx, scope, draft), | |
| 595 | ); | |
| 590 | 596 | } |
| 591 | 597 | |
| 592 | 598 | /** |
test/edit-timing.test.ts added +61
| @@ -0,0 +1,61 @@ | ||
| 1 | /** `GET /:owner/:name/edit/:ref/*path` names session, repo, blob, refs and render in Server-Timing (#3499). */ | |
| 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 { 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-edit-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 | await commitToBranch(app, u.value, made.value, { | |
| 35 | message: "first", | |
| 36 | files: [{ path: "README.md", content: "# forge\n" }], | |
| 37 | }); | |
| 38 | servers = await serve(app); | |
| 39 | }); | |
| 40 | afterAll(() => { | |
| 41 | servers.stop(); | |
| 42 | app.close(); | |
| 43 | rmSync(root, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | test("GET the file editor names its phases", async () => { | |
| 47 | const signin = await fetch(`${servers.url}/signin`, { | |
| 48 | method: "POST", | |
| 49 | headers: { origin: servers.url }, | |
| 50 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 51 | redirect: "manual", | |
| 52 | }); | |
| 53 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 54 | const res = await fetch(`${servers.url}/alice/forge/edit/main/README.md`, { headers: { cookie } }); | |
| 55 | const body = await res.text(); | |
| 56 | expect(res.status).toBe(200); | |
| 57 | expect(body).toContain("Editing a file"); | |
| 58 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 59 | for (const name of ["app", "session", "repo", "blob", "refs", "render"]) | |
| 60 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 61 | }); |
new above
offline