Public: anyone can read this; the crew can change it.
The API's contents read names its Server-Timing phases: session, git, render #3792
scout/forge:scout/3781-the-api-s-contents-read-names-its-server → main 2 commits fork
2 files
changed +108
−15
src/http/api.ts +44 −15
| @@ -250,6 +250,7 @@ import { isPinned } from "../core/pins.ts"; | ||
| 250 | 250 | import { listWebhooks } from "../core/webhooks.ts"; |
| 251 | 251 | import { diffCommit } from "../git/diff.ts"; |
| 252 | 252 | import { runGit } from "../git/env.ts"; |
| 253 | import { notePhase, takeSessionTime } from "../core/route_timing.ts"; | |
| 253 | 254 | import { appLimitWords, appsOwnedBy } from "../actions/app_limit.ts"; |
| 254 | 255 | import { getQuota } from "../settings/quotas.ts"; |
| 255 | 256 | import { isSafeRef, isSafeTreePath } from "../git/guard.ts"; |
| @@ -2751,6 +2752,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2751 | 2752 | // The path is optional: no path lists the root tree; a directory lists its |
| 2752 | 2753 | // entries; a file returns its content (utf-8, or base64 for binary). |
| 2753 | 2754 | const contentsHandler = async (ctx: Ctx): Promise<Response> => { |
| 2755 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 2754 | 2756 | const repo = loadRepo(ctx); |
| 2755 | 2757 | if (!repo) return notFound(); |
| 2756 | 2758 | const dir = dirOf(ctx, repo); |
| @@ -2759,31 +2761,58 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2759 | 2761 | if (!isSafeRef(ref) || (path !== "" && !isSafeTreePath(path))) |
| 2760 | 2762 | return notFound(); |
| 2761 | 2763 | |
| 2764 | // `git` adds up the probe and the read; `render` is encoding and JSON. | |
| 2765 | let gitMs = 0; | |
| 2766 | const git = async <T>(work: () => Promise<T>): Promise<T> => { | |
| 2767 | const started = performance.now(); | |
| 2768 | try { | |
| 2769 | return await work(); | |
| 2770 | } finally { | |
| 2771 | gitMs += performance.now() - started; | |
| 2772 | } | |
| 2773 | }; | |
| 2774 | const done = (response: Response, since: number): Response => { | |
| 2775 | notePhase(ctx.request, "git", gitMs); | |
| 2776 | notePhase(ctx.request, "render", performance.now() - since); | |
| 2777 | return response; | |
| 2778 | }; | |
| 2779 | ||
| 2762 | 2780 | const spec = path === "" ? ref : `${ref}:${path}`; |
| 2763 | const type = await runGit(["cat-file", "-t", spec], { cwd: dir }); | |
| 2781 | const type = await git(() => runGit(["cat-file", "-t", spec], { cwd: dir })); | |
| 2764 | 2782 | // A ref (no path) or a directory resolves to a tree → list it. |
| 2765 | 2783 | if (path === "" || (type.ok && type.value === "tree")) { |
| 2766 | const tree = await listTree(dir, ref, path); | |
| 2767 | if (!tree.ok) return notFound(); | |
| 2768 | return json({ type: "dir", ref, path, entries: tree.value }); | |
| 2784 | const tree = await git(() => listTree(dir, ref, path)); | |
| 2785 | if (!tree.ok) { | |
| 2786 | notePhase(ctx.request, "git", gitMs); | |
| 2787 | return notFound(); | |
| 2788 | } | |
| 2789 | const rendering = performance.now(); | |
| 2790 | return done(json({ type: "dir", ref, path, entries: tree.value }), rendering); | |
| 2769 | 2791 | } |
| 2770 | const blob = await blobForRender(dir, ref, path, 5_000_000); | |
| 2771 | if (blob.kind === "missing") return notFound(); | |
| 2792 | const blob = await git(() => blobForRender(dir, ref, path, 5_000_000)); | |
| 2793 | if (blob.kind === "missing") { | |
| 2794 | notePhase(ctx.request, "git", gitMs); | |
| 2795 | return notFound(); | |
| 2796 | } | |
| 2797 | const rendering = performance.now(); | |
| 2772 | 2798 | if (blob.kind === "too_large") |
| 2773 | return json({ type: "file", tooLarge: true, size: blob.size }, 413); | |
| 2799 | return done(json({ type: "file", tooLarge: true, size: blob.size }, 413), rendering); | |
| 2774 | 2800 | const encoding = blob.kind === "binary" ? "base64" : "utf-8"; |
| 2775 | 2801 | const content = |
| 2776 | 2802 | blob.kind === "binary" |
| 2777 | 2803 | ? Buffer.from(blob.body).toString("base64") |
| 2778 | 2804 | : new TextDecoder().decode(blob.body); |
| 2779 | return json({ | |
| 2780 | type: "file", | |
| 2781 | ref, | |
| 2782 | path, | |
| 2783 | size: blob.size, | |
| 2784 | encoding, | |
| 2785 | content, | |
| 2786 | }); | |
| 2805 | return done( | |
| 2806 | json({ | |
| 2807 | type: "file", | |
| 2808 | ref, | |
| 2809 | path, | |
| 2810 | size: blob.size, | |
| 2811 | encoding, | |
| 2812 | content, | |
| 2813 | }), | |
| 2814 | rendering, | |
| 2815 | ); | |
| 2787 | 2816 | }; |
| 2788 | 2817 | router.add("GET", "/api/v1/repos/:owner/:name/contents", contentsHandler); |
| 2789 | 2818 | router.add( |
test/api-contents-timing.test.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** `GET /api/v1/repos/:o/:n/contents` names session, git and render in Server-Timing (#3781). */ | |
| 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 { seedAdmin } from "./helpers/admin.ts"; | |
| 9 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 10 | ||
| 11 | let dataDir = ""; | |
| 12 | let app: App; | |
| 13 | let servers: Servers; | |
| 14 | let headers: Record<string, string> = {}; | |
| 15 | ||
| 16 | beforeAll(async () => { | |
| 17 | dataDir = await mkdtemp(join(tmpdir(), "forge-contents-timing-")); | |
| 18 | app = createApp({ dataDir, port: 0 }); | |
| 19 | servers = await serve(app); | |
| 20 | await seedAdmin(app, { username: "mira", email: "[email protected]", password: "contents-timing-pass" }); | |
| 21 | headers = { authorization: `Bearer ${tokenFor(app, "mira", "rw")}`, "content-type": "application/json" }; | |
| 22 | const base = `${servers.url}/api/v1/repos`; | |
| 23 | await fetch(base, { method: "POST", headers, body: JSON.stringify({ name: "app", visibility: "public" }) }); | |
| 24 | const res = await fetch(`${base}/mira/app/contents`, { | |
| 25 | method: "POST", | |
| 26 | headers, | |
| 27 | body: JSON.stringify({ message: "initial", files: [{ path: "src/a.txt", content: "hi\n" }] }), | |
| 28 | }); | |
| 29 | expect(res.status).toBe(201); | |
| 30 | }); | |
| 31 | afterAll(async () => { | |
| 32 | servers.stop(); | |
| 33 | app.close(); | |
| 34 | await rm(dataDir, { recursive: true, force: true }); | |
| 35 | }); | |
| 36 | ||
| 37 | const phases = (res: Response): string[] => | |
| 38 | (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 39 | const get = (path: string) => fetch(`${servers.url}/api/v1/repos/mira/app/contents${path}`, { headers }); | |
| 40 | ||
| 41 | test("a directory names session, git and render once each", async () => { | |
| 42 | const res = await get("/src"); | |
| 43 | expect(res.status).toBe(200); | |
| 44 | expect(((await res.json()) as { type: string }).type).toBe("dir"); | |
| 45 | const names = phases(res); | |
| 46 | for (const n of ["session", "git", "render"]) expect(names.filter((x) => x === n)).toHaveLength(1); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a file names session, git and render once each", async () => { | |
| 50 | const res = await get("/src/a.txt"); | |
| 51 | expect(res.status).toBe(200); | |
| 52 | expect(((await res.json()) as { content: string }).content).toBe("hi\n"); | |
| 53 | const names = phases(res); | |
| 54 | for (const n of ["session", "git", "render"]) expect(names.filter((x) => x === n)).toHaveLength(1); | |
| 55 | }); | |
| 56 | ||
| 57 | test("a 404 still names session", async () => { | |
| 58 | const res = await get("/nope.txt"); | |
| 59 | expect(res.status).toBe(404); | |
| 60 | expect(phases(res)).toContain("session"); | |
| 61 | const unknown = await fetch(`${servers.url}/api/v1/repos/mira/none/contents`, { headers }); | |
| 62 | expect(unknown.status).toBe(404); | |
| 63 | expect(phases(unknown)).toContain("session"); | |
| 64 | }); |
new above
offline