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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13348-status-answers-under-6-kb-and-under-50-m → main 4 commits fork

  • Merged. scout/13348-status-answers-under-6-kb-and-under-50-m was merged into main as f021bd2.

Review

Sign in to review this change.
4 files changed +92 −21
src/web/layout.ts −1
@@ -754,7 +754,6 @@ function footer(ctx: Ctx, own?: Html): Html {
754754 <span class="spacer"></span>
755755 <a href="/docs">Docs</a>
756756 <a href="/pricing">Pricing</a>
757 <a href="/privacy">Privacy</a>
758757 <a href="/security">Security</a>
759758 <a href="/changelog">Changelog</a>
760759 <a href="/status">Status</a>
src/web/pages/status.ts +60 −17
@@ -1,17 +1,57 @@
11 /**
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.
510 */
611
7 import { statusJson, statusWords, WORD } from "../../core/status_words.ts";
12 import { statusJson, statusWords, WORD, type StatusWords } from "../../core/status_words.ts";
813 import type { Ctx } from "../../http/context.ts";
914 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
15 import { stylesheetAsset } from "../asset.ts";
1016 import { html } from "../html.ts";
11 import { page } from "../layout.ts";
1217 import { badge } from "../ui.ts";
1318 import { statusRow } from "./security.ts";
1419
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
1555 export function statusPage(ctx: Ctx): Response {
1656 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
1757 const words = statusWords(ctx.app);
@@ -25,16 +65,19 @@ export function statusPage(ctx: Ctx): Response {
2565 headers: { "cache-control": "public, max-age=10", vary: "accept", "content-type": "application/json" },
2666 }),
2767 );
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 );
4083 }
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", () => {
7575 expect(securityBody("https://f.example/sec").text).toContain('href="https://f.example/sec"');
7676 });
7777
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 () => {
7979 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>/);
8181 });
8282
8383 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";
55 import { createApp, type App } from "../src/app.ts";
66 import { serve, type Servers } from "../src/http/server.ts";
77 import { statusJson, statusOf, statusReuse, statusWords, type StatusInput } from "../src/core/status_words.ts";
8 import { statusBuilds } from "../src/web/pages/status.ts";
89 import { createForgeApp, forgeCommands, Session } from "../src/tui/forge.ts";
910 import { statusLines } from "../src/tui/screens/status.ts";
1011 import { headlessTerminal } from "../src/tui/term/terminal.ts";
@@ -113,7 +114,35 @@ describe("GET /status", () => {
113114 expect(text).not.toContain("<li>pages");
114115 expect(r.headers.get("content-security-policy")).toBeTruthy();
115116 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 }
117146 });
118147
119148 test("JSON carries the same words as the page, in a small body", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs