Public: anyone can read this; the crew can change it.
The edge Worker answers a browser with a static "the forge is restarting" page when the body is unreachable #16689
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16259-the-edge-worker-answers-a-browser-with-a → main 0 commits fork 1 thread · 1 unresolved
2 files
changed +72
−1
infra/forge-cloudflare/worker.js +32 −1
| @@ -17,17 +17,48 @@ export function forward(request, bodyUrl) { | ||
| 17 | 17 | return new Request(to, init); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | const CSP = | |
| 21 | "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"; | |
| 22 | ||
| 23 | /** A browser asking for a page: a GET whose Accept names text/html. */ | |
| 24 | function wantsPage(request) { | |
| 25 | return request.method === "GET" && (request.headers.get("accept") || "").includes("text/html"); | |
| 26 | } | |
| 27 | ||
| 28 | /** The one self-contained page: no script, inline style, says when the edge last heard the body. */ | |
| 29 | export function restartingPage(heard) { | |
| 30 | const when = heard | |
| 31 | ? `The edge last heard from it at <time datetime="${heard}">${heard}</time>.` | |
| 32 | : "The edge has not heard from it since it started."; | |
| 33 | const html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>The forge is restarting</title><style>body{font:16px/1.5 system-ui,sans-serif;max-width:32rem;margin:20vh auto;padding:0 1rem;color:#222}h1{font-size:1.4rem}@media(prefers-color-scheme:dark){body{background:#111;color:#ddd}}</style></head><body><h1>The forge is restarting</h1><p>It will be back in a moment; this page is safe to reload.</p><p>${when}</p></body></html>`; | |
| 34 | return new Response(html, { | |
| 35 | status: 503, | |
| 36 | headers: { | |
| 37 | "content-type": "text/html; charset=utf-8", | |
| 38 | "retry-after": "5", | |
| 39 | "cache-control": "no-store", | |
| 40 | "content-security-policy": CSP, | |
| 41 | "x-content-type-options": "nosniff", | |
| 42 | "referrer-policy": "same-origin", | |
| 43 | }, | |
| 44 | }); | |
| 45 | } | |
| 46 | ||
| 20 | 47 | export class Forge { |
| 48 | heard = null; | |
| 21 | 49 | constructor(state, env) { |
| 22 | 50 | this.env = env; |
| 23 | 51 | } |
| 24 | 52 | async fetch(request) { |
| 25 | 53 | try { |
| 26 | return await fetch(forward(request, this.env.BODY_URL)); | |
| 54 | const response = await fetch(forward(request, this.env.BODY_URL)); | |
| 55 | this.heard = new Date().toISOString(); | |
| 56 | return response; | |
| 27 | 57 | } catch { |
| 28 | 58 | const { pathname } = new URL(request.url); |
| 29 | 59 | if (pathname === "/healthz") |
| 30 | 60 | return Response.json({ ok: false, body: "unreachable" }, { status: 503 }); |
| 61 | if (wantsPage(request)) return restartingPage(this.heard); | |
| 31 | 62 | return new Response("the forge's body is not answering\n", { status: 502 }); |
| 32 | 63 | } |
| 33 | 64 | } |
test/edge-restarting.test.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | // @ts-expect-error plain JS, deployed as is | |
| 3 | import { Forge } from "../infra/forge-cloudflare/worker.js"; | |
| 4 | ||
| 5 | const page = (path = "/") => | |
| 6 | new Request(`https://h${path}`, { headers: { accept: "text/html" } }); | |
| 7 | ||
| 8 | test("a browser GET with the body unreachable gets the restarting page", async () => { | |
| 9 | const f = new Forge({}, { BODY_URL: "http://127.0.0.1:1" }); | |
| 10 | const r = await f.fetch(page("/anything")); | |
| 11 | expect(r.status).toBe(503); | |
| 12 | expect(r.headers.get("retry-after")).toBe("5"); | |
| 13 | expect(r.headers.get("content-type")).toContain("text/html"); | |
| 14 | expect(r.headers.get("content-security-policy")).toContain("default-src 'none'"); | |
| 15 | expect(r.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 16 | expect(r.headers.get("referrer-policy")).toBe("same-origin"); | |
| 17 | const html = await r.text(); | |
| 18 | expect(html).toContain("The forge is restarting"); | |
| 19 | expect(html).toContain("has not heard from it"); | |
| 20 | expect(html).not.toContain("<script"); | |
| 21 | }); | |
| 22 | ||
| 23 | test("the page says when the edge last heard from the body", async () => { | |
| 24 | const body = Bun.serve({ port: 0, fetch: () => new Response("ok") }); | |
| 25 | const f = new Forge({}, { BODY_URL: `http://127.0.0.1:${body.port}` }); | |
| 26 | expect((await f.fetch(page())).status).toBe(200); | |
| 27 | body.stop(true); | |
| 28 | const html = await (await f.fetch(page())).text(); | |
| 29 | expect(html).toMatch(/last heard from it at <time datetime="\d{4}-\d\d-\d\dT/); | |
| 30 | }); | |
| 31 | ||
| 32 | test("healthz and non-browser requests are unchanged", async () => { | |
| 33 | const f = new Forge({}, { BODY_URL: "http://127.0.0.1:1" }); | |
| 34 | const h = await f.fetch(page("/healthz")); | |
| 35 | expect(h.status).toBe(503); | |
| 36 | expect(await h.json()).toEqual({ ok: false, body: "unreachable" }); | |
| 37 | expect((await f.fetch(new Request("https://h/mcp"))).status).toBe(502); | |
| 38 | const post = new Request("https://h/x", { method: "POST", body: "a", headers: { accept: "text/html" } }); | |
| 39 | expect((await f.fetch(post)).status).toBe(502); | |
| 40 | }); |
new above
offline
Next: show a relative time ('2 minutes ago') as the visible text and keep the ISO string in datetime. This does not block the approval.