Public: anyone can read this; the crew can change it.
/status answers under 6 KB and under 50 ms from a cache of at most 10 s #13352
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13348-status-answers-under-6-kb-and-under-50-m → main 4 commits fork
4 files
changed +92
−21
src/web/layout.ts −1
| @@ -754,7 +754,6 @@ function footer(ctx: Ctx, own?: Html): Html { | ||
| 754 | 754 | <span class="spacer"></span> |
| 755 | 755 | <a href="/docs">Docs</a> |
| 756 | 756 | <a href="/pricing">Pricing</a> |
| 757 | <a href="/privacy">Privacy</a> | |
| 758 | 757 | <a href="/security">Security</a> |
| 759 | 758 | <a href="/changelog">Changelog</a> |
| 760 | 759 | <a href="/status">Status</a> |
src/web/pages/status.ts +60 −17
| @@ -1,17 +1,57 @@ | ||
| 1 | 1 | /** |
| 2 | * GET /status: is the forge working, in one sentence and three rows (#13211). | |
| 3 | * Signed out, always 200. Asked with `Accept: application/json` it answers the | |
| 4 | * same words as data. Server-rendered in the shell, held 10 s (`statusWords`). | |
| 2 | * GET /status: is the forge working, in one sentence and three rows (#13211, #13348). | |
| 3 | * Signed out, always 200, under 6 KB, no script, no polling. Asked with | |
| 4 | * `Accept: application/json` it answers the same words as data. | |
| 5 | * | |
| 6 | * It is its own small document, not the shell: the shell's head, menus, palette and | |
| 7 | * scripts weigh more than the whole answer is allowed to. The words are held 10 s | |
| 8 | * (`statusWords`); the markup built from them is held with them, so a second request | |
| 9 | * inside the window builds nothing. | |
| 5 | 10 | */ |
| 6 | 11 | |
| 7 | import { statusJson, statusWords, WORD } from "../../core/status_words.ts"; | |
| 12 | import { statusJson, statusWords, WORD, type StatusWords } from "../../core/status_words.ts"; | |
| 8 | 13 | import type { Ctx } from "../../http/context.ts"; |
| 9 | 14 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 15 | import { stylesheetAsset } from "../asset.ts"; | |
| 10 | 16 | import { html } from "../html.ts"; |
| 11 | import { page } from "../layout.ts"; | |
| 12 | 17 | import { badge } from "../ui.ts"; |
| 13 | 18 | import { statusRow } from "./security.ts"; |
| 14 | 19 | |
| 20 | const tone = { ok: "success", slow: "attn", down: "danger" } as const; | |
| 21 | ||
| 22 | /** How many times the markup was built: a test reads it to see the hold at work. */ | |
| 23 | export const statusBuilds = { count: 0 }; | |
| 24 | const built = new WeakMap<StatusWords, string>(); | |
| 25 | ||
| 26 | function markup(words: StatusWords): string { | |
| 27 | const kept = built.get(words); | |
| 28 | if (kept !== undefined) return kept; | |
| 29 | statusBuilds.count += 1; | |
| 30 | const rows = words.rows.map((r) => statusRow(r.name, badge(WORD[r.state], { tone: tone[r.state] }))); | |
| 31 | const text = html`<!doctype html> | |
| 32 | <html lang="en"> | |
| 33 | <head> | |
| 34 | <meta charset="utf-8"> | |
| 35 | <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| 36 | <meta name="color-scheme" content="light dark"> | |
| 37 | <title>Status · forge</title> | |
| 38 | <link rel="stylesheet" href="${stylesheetAsset().path}"> | |
| 39 | </head> | |
| 40 | <body> | |
| 41 | <main class="shell" id="main"> | |
| 42 | <div class="page-head"><h1>${words.sentence}</h1></div> | |
| 43 | <dl class="status-rows">${rows}</dl> | |
| 44 | </main> | |
| 45 | <footer class="foot"><a href="/">forge</a><span class="spacer"></span><a href="/status">Status</a><a href="/privacy">Privacy</a></footer> | |
| 46 | </body> | |
| 47 | </html>`.text; | |
| 48 | built.set(words, text); | |
| 49 | return text; | |
| 50 | } | |
| 51 | ||
| 52 | const CSP = | |
| 53 | "default-src 'none'; style-src 'self'; img-src 'self' data:; form-action 'none'; base-uri 'none'; frame-ancestors 'none'"; | |
| 54 | ||
| 15 | 55 | export function statusPage(ctx: Ctx): Response { |
| 16 | 56 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 17 | 57 | const words = statusWords(ctx.app); |
| @@ -25,16 +65,19 @@ export function statusPage(ctx: Ctx): Response { | ||
| 25 | 65 | headers: { "cache-control": "public, max-age=10", vary: "accept", "content-type": "application/json" }, |
| 26 | 66 | }), |
| 27 | 67 | ); |
| 28 | return timePhase(ctx.request, "render", () => { | |
| 29 | const tone = { ok: "success", slow: "attn", down: "danger" } as const; | |
| 30 | const rows = words.rows.map((r) => statusRow(r.name, badge(WORD[r.state], { tone: tone[r.state] }))); | |
| 31 | const res = page({ | |
| 32 | ctx, | |
| 33 | title: "Status", | |
| 34 | body: html`<div class="page-head"><h1>${words.sentence}</h1></div> | |
| 35 | <dl class="status-rows">${rows}</dl>`, | |
| 36 | }); | |
| 37 | res.headers.set("vary", "accept"); | |
| 38 | return res; | |
| 39 | }); | |
| 68 | return timePhase( | |
| 69 | ctx.request, | |
| 70 | "render", | |
| 71 | () => | |
| 72 | new Response(markup(words), { | |
| 73 | headers: { | |
| 74 | "content-type": "text/html; charset=utf-8", | |
| 75 | "cache-control": "public, max-age=10", | |
| 76 | vary: "accept", | |
| 77 | "content-security-policy": CSP, | |
| 78 | "x-content-type-options": "nosniff", | |
| 79 | "referrer-policy": "same-origin", | |
| 80 | }, | |
| 81 | }), | |
| 82 | ); | |
| 40 | 83 | } |
test/security-page.test.ts +2 −2
| @@ -75,9 +75,9 @@ test("a contact is escaped, and only an http(s) one becomes a link", () => { | ||
| 75 | 75 | expect(securityBody("https://f.example/sec").text).toContain('href="https://f.example/sec"'); |
| 76 | 76 | }); |
| 77 | 77 | |
| 78 | test("the foot of a shared page links /security beside /privacy", async () => { | |
| 78 | test("the foot of a shared page links /security in the foot, before /privacy", async () => { | |
| 79 | 79 | const text = await (await fetch(`${servers.url}/pricing`)).text(); |
| 80 | expect(text).toMatch(/<a href="\/privacy">Privacy<\/a>\s*<a href="\/security">Security<\/a>/); | |
| 80 | expect(text).toMatch(/<a href="\/security">Security<\/a>[^]*<a href="\/privacy">Privacy<\/a>/); | |
| 81 | 81 | }); |
| 82 | 82 | |
| 83 | 83 | test("the terminal's Security screen carries every claim, wrapped to its width", () => { |
test/status-page.test.ts +30 −1
| @@ -5,6 +5,7 @@ import { join } from "node:path"; | ||
| 5 | 5 | import { createApp, type App } from "../src/app.ts"; |
| 6 | 6 | import { serve, type Servers } from "../src/http/server.ts"; |
| 7 | 7 | import { statusJson, statusOf, statusReuse, statusWords, type StatusInput } from "../src/core/status_words.ts"; |
| 8 | import { statusBuilds } from "../src/web/pages/status.ts"; | |
| 8 | 9 | import { createForgeApp, forgeCommands, Session } from "../src/tui/forge.ts"; |
| 9 | 10 | import { statusLines } from "../src/tui/screens/status.ts"; |
| 10 | 11 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -113,7 +114,35 @@ describe("GET /status", () => { | ||
| 113 | 114 | expect(text).not.toContain("<li>pages"); |
| 114 | 115 | expect(r.headers.get("content-security-policy")).toBeTruthy(); |
| 115 | 116 | expect(r.headers.get("x-content-type-options")).toBe("nosniff"); |
| 116 | expect(text.length).toBeLessThan(24_576); | |
| 117 | expect(Buffer.byteLength(text)).toBeLessThan(6_144); | |
| 118 | expect(text).not.toMatch(/<script/i); | |
| 119 | expect((text.match(/<div class="status-row"/g) ?? []).length).toBeLessThanOrEqual(5); | |
| 120 | }); | |
| 121 | ||
| 122 | test("shows no internal name, host, path or error text", async () => { | |
| 123 | const text = await (await fetch(`${base}/status`)).text(); | |
| 124 | expect(text).not.toMatch(/127\.0\.0\.1|localhost|\/data\/|ECONN|Error|stack|\.sock|secret/i); | |
| 125 | }); | |
| 126 | ||
| 127 | test("a second request inside the hold does not rebuild the page", async () => { | |
| 128 | const was = statusReuse.ms; | |
| 129 | statusReuse.ms = 10_000; | |
| 130 | try { | |
| 131 | const fresh = createApp({ dataDir: join(dir, "hold"), port: 0 }); | |
| 132 | const s = await serve(fresh); | |
| 133 | try { | |
| 134 | const url = `http://127.0.0.1:${s.port}/status`; | |
| 135 | const before = statusBuilds.count; | |
| 136 | const a = await (await fetch(url)).text(); | |
| 137 | const b = await (await fetch(url)).text(); | |
| 138 | expect(a).toBe(b); | |
| 139 | expect(statusBuilds.count - before).toBe(1); | |
| 140 | } finally { | |
| 141 | await s.stop(); | |
| 142 | } | |
| 143 | } finally { | |
| 144 | statusReuse.ms = was; | |
| 145 | } | |
| 117 | 146 | }); |
| 118 | 147 | |
| 119 | 148 | test("JSON carries the same words as the page, in a small body", async () => { |
new above
offline