Public: anyone can read this; the crew can change it.
A connected origin's repository browse pages name their Server-Timing phases: session, the origin read, render #3607
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3604-a-connected-origin-s-repository-browse-p → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +98
−4
src/web/pages/browse.ts +23 −4
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | import { mirrorJobFor, startMirrorJob } from "../../actions/origins.ts"; |
| 29 | 29 | import { forkOrigin } from "../../actions/contribute.ts"; |
| 30 | 30 | import { getOriginFor } from "../../core/origins.ts"; |
| 31 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | |
| 31 | 32 | import { isSafeOid, isSafeRef, isSafeTreePath } from "../../git/guard.ts"; |
| 32 | 33 | import type { Ctx } from "../../http/context.ts"; |
| 33 | 34 | import type { MirrorJob, MirrorStage } from "../../origins/jobs.ts"; |
| @@ -325,6 +326,7 @@ function sourceQuery( | ||
| 325 | 326 | * able to spend somebody's GitHub credential just because it can read a repo. |
| 326 | 327 | */ |
| 327 | 328 | async function browseScope(ctx: Ctx): Promise<RepoScope | Response> { |
| 329 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 328 | 330 | const denied = accountOnly(ctx); |
| 329 | 331 | if (denied) return denied; |
| 330 | 332 | const originId = ctx.params.id ?? ""; |
| @@ -333,7 +335,9 @@ async function browseScope(ctx: Ctx): Promise<RepoScope | Response> { | ||
| 333 | 335 | if (origin === undefined) return notFoundPage(ctx); |
| 334 | 336 | const source = sourceFor(ctx.app, ctx.auth.user, { originId, fullName }); |
| 335 | 337 | if (!source.ok) return notFoundPage(ctx); |
| 338 | const reading = performance.now(); | |
| 336 | 339 | const scope = await originScope(origin, source.value); |
| 340 | notePhase(ctx.request, "repo", performance.now() - reading); | |
| 337 | 341 | if (scope === undefined) return unreachablePage(ctx, origin.webUrl); |
| 338 | 342 | return scope; |
| 339 | 343 | } |
| @@ -400,10 +404,18 @@ export async function browseHome(ctx: Ctx): Promise<Response> { | ||
| 400 | 404 | const scope = await browseScope(ctx); |
| 401 | 405 | if (scope instanceof Response) return scope; |
| 402 | 406 | const ref = refOf(ctx, scope); |
| 407 | const reading = performance.now(); | |
| 403 | 408 | const [refs, entries] = await Promise.all([ |
| 404 | refSetFrom(scope.source), | |
| 405 | scope.source.tree(ref, ""), | |
| 409 | refSetFrom(scope.source).then((r) => { | |
| 410 | notePhase(ctx.request, "refs", performance.now() - reading); | |
| 411 | return r; | |
| 412 | }), | |
| 413 | scope.source.tree(ref, "").then((r) => { | |
| 414 | notePhase(ctx.request, "tree", performance.now() - reading); | |
| 415 | return r; | |
| 416 | }), | |
| 406 | 417 | ]); |
| 418 | const rendering = performance.now(); | |
| 407 | 419 | const body = await renderCode(scope, { |
| 408 | 420 | refs, |
| 409 | 421 | ref, |
| @@ -411,7 +423,7 @@ export async function browseHome(ctx: Ctx): Promise<Response> { | ||
| 411 | 423 | entries: entries.ok ? entries.value : [], |
| 412 | 424 | rail: aboutRail(scope), |
| 413 | 425 | }); |
| 414 | return browsePage(ctx, scope, { | |
| 426 | const response = browsePage(ctx, scope, { | |
| 415 | 427 | title: titleFor(scope), |
| 416 | 428 | header: scopeHeader(ctx, scope, "code", { |
| 417 | 429 | branchCount: refs.branches.length, |
| @@ -419,6 +431,8 @@ export async function browseHome(ctx: Ctx): Promise<Response> { | ||
| 419 | 431 | body: html`${body}${codeMount(scope, ref)}${finderDialog()}`, |
| 420 | 432 | scriptJs: codeJs, |
| 421 | 433 | }); |
| 434 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 435 | return response; | |
| 422 | 436 | } |
| 423 | 437 | |
| 424 | 438 | // GET /origins/:id/r/:owner/:name/paths/:ref |
| @@ -426,13 +440,18 @@ export async function browsePaths(ctx: Ctx): Promise<Response> { | ||
| 426 | 440 | const scope = await browseScope(ctx); |
| 427 | 441 | if (scope instanceof Response) return notFound(); |
| 428 | 442 | const ref = refOf(ctx, scope); |
| 443 | const reading = performance.now(); | |
| 429 | 444 | const listed = await scope.source.paths(ref); |
| 445 | notePhase(ctx.request, "paths", performance.now() - reading); | |
| 430 | 446 | if (!listed.ok) return notFound(); |
| 431 | return json( | |
| 447 | const rendering = performance.now(); | |
| 448 | const response = json( | |
| 432 | 449 | { ref, paths: listed.value.paths, truncated: listed.value.truncated }, |
| 433 | 450 | 200, |
| 434 | 451 | { "cache-control": "private, max-age=30" }, |
| 435 | 452 | ); |
| 453 | notePhase(ctx.request, "render", performance.now() - rendering); | |
| 454 | return response; | |
| 436 | 455 | } |
| 437 | 456 | |
| 438 | 457 | // GET /origins/:id/r/:owner/:name/branches |
test/browse-origin-timing.test.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** A connected origin's repository browse pages name their phases in Server-Timing. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | import { listOrigins } from "../src/core/origins.ts"; | |
| 9 | import { getUserByName } from "../src/core/users.ts"; | |
| 10 | import { forgetProviders } from "../src/origins/providers/index.ts"; | |
| 11 | import { clearViewCache } from "../src/view/cache.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | import { newFakeState, serveFakeForge, type FakeForge } from "./fake-origin.ts"; | |
| 14 | import { adminCookie } from "./helpers/admin.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | const TOKEN = "octo-origin-secret-7c31"; | |
| 19 | const SHA = "a".repeat(40); | |
| 20 | ||
| 21 | let dataDir = ""; | |
| 22 | let app: App, servers: Servers, fake: FakeForge; | |
| 23 | let cookie = "", browse = ""; | |
| 24 | ||
| 25 | beforeAll(async () => { | |
| 26 | dataDir = await mkdtemp(join(tmpdir(), "forge-borigtime-")); | |
| 27 | const state = newFakeState("forgejo", "octo", TOKEN); | |
| 28 | state.repos.push({ | |
| 29 | id: 41, | |
| 30 | fullName: "octo/widgets", | |
| 31 | cloneUrl: "http://127.0.0.1:1/octo/widgets.git", | |
| 32 | htmlUrl: "http://127.0.0.1:1/octo/widgets", | |
| 33 | defaultBranch: "main", | |
| 34 | }); | |
| 35 | state.files.set("octo/widgets", new Map([["README.md", "# widgets\n"]])); | |
| 36 | state.branches.set("octo/widgets", [{ name: "main", sha: SHA }]); | |
| 37 | fake = serveFakeForge(state); | |
| 38 | app = createApp({ dataDir, port: 0 }); | |
| 39 | servers = await serve(app); | |
| 40 | delete process.env.FORGE_GITHUB_CLIENT_ID; | |
| 41 | cookie = await adminCookie(app, { username: "alice", email: "[email protected]", password: "a-long-web-passphrase" }); | |
| 42 | forgetProviders(dataDir); | |
| 43 | const connected = await fetch(`${servers.url}/settings/origins/token`, { | |
| 44 | method: "POST", | |
| 45 | headers: { cookie, origin: new URL(servers.url).origin }, | |
| 46 | body: new URLSearchParams({ host: fake.url, token: TOKEN }), | |
| 47 | redirect: "manual", | |
| 48 | }); | |
| 49 | expect(connected.status).toBe(303); | |
| 50 | const id = listOrigins(app.db, getUserByName(app.db, "alice")!.id)[0]!.id; | |
| 51 | browse = `/origins/${id}/r/octo/widgets`; | |
| 52 | clearViewCache(); | |
| 53 | }); | |
| 54 | ||
| 55 | afterAll(async () => { | |
| 56 | fake.stop(); | |
| 57 | await servers.stop(); | |
| 58 | await rm(dataDir, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | const names = (r: Response): string[] => | |
| 62 | (r.headers.get("server-timing") ?? "").split(",").map((p) => p.trim().split(/[;=]/)[0]!); | |
| 63 | ||
| 64 | test("the repository page names session, the origin reads, and render", async () => { | |
| 65 | const r = await fetch(`${servers.url}${browse}`, { headers: { cookie } }); | |
| 66 | expect(r.status).toBe(200); | |
| 67 | expect(names(r)).toEqual(expect.arrayContaining(["session", "repo", "refs", "tree", "render"])); | |
| 68 | }); | |
| 69 | ||
| 70 | test("the paths page names session, the origin reads, and render", async () => { | |
| 71 | clearViewCache(); | |
| 72 | const r = await fetch(`${servers.url}${browse}/paths/main`, { headers: { cookie } }); | |
| 73 | expect(r.status).toBe(200); | |
| 74 | expect(names(r)).toEqual(expect.arrayContaining(["session", "repo", "paths", "render"])); | |
| 75 | }); |
new above
offline
This
rendertimes only the JSON.stringify of a small object, so it is near zero. Consider dropping it from the paths route.