Public: anyone can read this; the crew can change it.
A preview's look picture names its Server-Timing phases: session, look, read #5384
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/5371-a-preview-s-look-picture-names-its-serve → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +88
−8
src/web/pages/looks.ts +15 −8
| @@ -31,6 +31,7 @@ import { canEnterApp, canSeeOwnerFacts, type Repo } from "../../core/repos.ts"; | ||
| 31 | 31 | import type { Ctx } from "../../http/context.ts"; |
| 32 | 32 | import { notFound } from "../../http/respond.ts"; |
| 33 | 33 | import { keepLook } from "../../looks/service.ts"; |
| 34 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 34 | 35 | import { loadRepo, repoBase } from "../chrome.ts"; |
| 35 | 36 | import { html, when, type Html } from "../html.ts"; |
| 36 | 37 | import { card, disclosure, icon, sha, time } from "../ui.ts"; |
| @@ -65,21 +66,27 @@ export function thumbLook( | ||
| 65 | 66 | // really a slot (`lookSlot`): the kind alone for the home page, the page |
| 66 | 67 | // and the kind for any other. |
| 67 | 68 | export function lookImage(ctx: Ctx): Response { |
| 69 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 68 | 70 | const repo = loadRepo(ctx); |
| 69 | 71 | const env = ctx.params.env ?? ""; |
| 70 | 72 | const commit = ctx.params.commit ?? ""; |
| 71 | 73 | const slot = ctx.params.kind ?? ""; |
| 72 | 74 | if (!repo || !isEnvName(env) || !/^[0-9a-f]{40}$/.test(commit) || slot === "") |
| 73 | 75 | return notFound(); |
| 74 | const record = getAppByRepo(ctx.app.db, repo.id); | |
| 75 | if (!record) return notFound(); | |
| 76 | const look = looksForEnv(ctx.app.db, record.id, env, "any").find( | |
| 77 | (l) => l.commitOid === commit && lookSlot(l.page, l.kind, l.theme) === slot, | |
| 78 | ); | |
| 79 | if (look === undefined || look.error !== null) return notFound(); | |
| 80 | if (!mayFetchLook(ctx, repo, record, look)) return notFound(); | |
| 76 | const found = timePhase(ctx.request, "look", () => { | |
| 77 | const record = getAppByRepo(ctx.app.db, repo.id); | |
| 78 | if (!record) return undefined; | |
| 79 | const look = looksForEnv(ctx.app.db, record.id, env, "any").find( | |
| 80 | (l) => l.commitOid === commit && lookSlot(l.page, l.kind, l.theme) === slot, | |
| 81 | ); | |
| 82 | if (look === undefined || look.error !== null) return undefined; | |
| 83 | if (!mayFetchLook(ctx, repo, record, look)) return undefined; | |
| 84 | return look; | |
| 85 | }); | |
| 86 | if (found === undefined) return notFound(); | |
| 87 | const look = found; | |
| 81 | 88 | const path = join(ctx.app.config.dataDir, look.path); |
| 82 | if (!existsSync(path)) return notFound(); | |
| 89 | if (!timePhase(ctx.request, "read", () => existsSync(path))) return notFound(); | |
| 83 | 90 | const headers = { |
| 84 | 91 | "content-type": path.endsWith(".jpg") ? "image/jpeg" : "image/png", |
| 85 | 92 | // Named by this taking (`lookVersion`), so a retake at the same commit |
test/look-image-timing.test.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** `/:owner/:name/looks/:env/:commit/:kind.png` names session, look and read in Server-Timing (#5371). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } 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 { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | import { recordLook } from "../src/core/looks.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { proveAll } from "./helpers/verified.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | ||
| 17 | const COMMIT = "a".repeat(40); | |
| 18 | let root = ""; | |
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | root = mkdtempSync(join(tmpdir(), "forge-look-image-timing-")); | |
| 24 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 25 | const u = await createUser(app.db, { | |
| 26 | origin: "seed", | |
| 27 | trusted: true, | |
| 28 | username: "alice", | |
| 29 | email: "[email protected]", | |
| 30 | password: "a-good-passphrase", | |
| 31 | adminIfFirst: true, | |
| 32 | }); | |
| 33 | if (!u.ok) throw new Error(u.error); | |
| 34 | proveAll(app); | |
| 35 | const made = await createRepo(app, { id: u.value.id, username: "alice" } as never, { name: "forge" }); | |
| 36 | if (!made.ok) throw new Error(made.error); | |
| 37 | const record = insertApp(app.db, { repoId: made.value.id, slug: "garden", access: "public" }); | |
| 38 | if (!record.ok) throw new Error(record.error); | |
| 39 | const path = `looks/${record.value.id}/production/${COMMIT}-thumb.png`; | |
| 40 | mkdirSync(join(root, "looks", record.value.id, "production"), { recursive: true }); | |
| 41 | writeFileSync(join(root, path), "png"); | |
| 42 | recordLook(app.db, { | |
| 43 | appId: record.value.id, | |
| 44 | env: "production", | |
| 45 | commitOid: COMMIT, | |
| 46 | kind: "thumb", | |
| 47 | path, | |
| 48 | takenAs: "nobody", | |
| 49 | }); | |
| 50 | servers = await serve(app); | |
| 51 | }); | |
| 52 | afterAll(() => { | |
| 53 | servers.stop(); | |
| 54 | app.close(); | |
| 55 | rmSync(root, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("GET a look picture names its phases", async () => { | |
| 59 | const signin = await fetch(`${servers.url}/signin`, { | |
| 60 | method: "POST", | |
| 61 | headers: { origin: servers.url }, | |
| 62 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 63 | redirect: "manual", | |
| 64 | }); | |
| 65 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 66 | const res = await fetch(`${servers.url}/alice/forge/looks/production/${COMMIT}/thumb.png`, { | |
| 67 | headers: { cookie }, | |
| 68 | }); | |
| 69 | expect(res.status).toBe(200); | |
| 70 | const parts = (res.headers.get("server-timing") ?? "").split(", "); | |
| 71 | for (const name of ["app", "session", "look", "read"]) | |
| 72 | expect(parts.find((p) => p.startsWith(`${name};`))).toMatch(/^[a-z]+;dur=\d+\.\d$/); | |
| 73 | }); |
new above
offline
const look = foundonly renames the value. Destructure or returnfounddirectly, or name the closure resultlook.