/** A short-lived, local page for a change the user will apply. */ import { createServer, type Server } from "node:http"; import { randomBytes } from "node:crypto"; import type { AddressInfo } from "node:net"; export interface Delivery { title: string; diagnosis: string; path: string; location: string; code: string; verification: string; } type Feedback = { kind: "understood" | "adjust"; comment: string; title: string }; const MAX_BODY = 8_192; const MAX_PAGES = 20; function escapeHtml(value: string): string { return value.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'", })[c] ?? c); } function page(item: Delivery): string { const title = escapeHtml(item.title); return ` ${title} · longhand
✍ longhand

${title}

A proposal for you to apply. This page has not modified any files.

What is happening and why

${escapeHtml(item.diagnosis)}

Where to change it

${escapeHtml(item.path)}${escapeHtml(item.location)}

Code to paste

${escapeHtml(item.code)}

How to verify

${escapeHtml(item.verification)}

Feedback

Confirm that the proposal makes sense or request a change. Your feedback will appear in the Pi conversation.

`; } export class DeliveryServer { private server: Server | undefined; private pages = new Map(); private readonly onFeedback: (feedback: Feedback) => void; constructor(onFeedback: (feedback: Feedback) => void) { this.onFeedback = onFeedback; } async publish(item: Delivery): Promise { if (!this.server) { const server = createServer(async (req, res) => { const url = new URL(req.url ?? "/", "http://localhost"); const token = url.searchParams.get("session") ?? ""; const entry = this.pages.get(token); res.setHeader("Cache-Control", "no-store"); res.setHeader("X-Content-Type-Options", "nosniff"); if (url.pathname !== "/" || !entry) { res.writeHead(404).end("Page unavailable"); return; } if (req.method === "GET") { res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; base-uri 'none'; form-action 'none'" }).end(page(entry)); return; } if (req.method !== "POST") { res.writeHead(405).end(); return; } if (req.headers.origin && req.headers.origin !== `http://localhost:${(server.address() as AddressInfo).port}` && req.headers.origin !== `http://127.0.0.1:${(server.address() as AddressInfo).port}`) { res.writeHead(403).end(); return; } let body = ""; for await (const chunk of req) { body += chunk; if (body.length > MAX_BODY) { res.writeHead(413).end(); return; } } try { const input = JSON.parse(body); if ((input.kind !== "understood" && input.kind !== "adjust") || typeof input.comment !== "string" || input.comment.length > 4_000 || (input.kind === "adjust" && !input.comment.trim())) throw new Error(); this.onFeedback({ kind: input.kind, comment: input.comment.trim(), title: entry.title }); res.writeHead(204).end(); } catch { res.writeHead(400).end("Invalid feedback"); } }); try { await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); }); this.server = server; } catch (err) { server.close(); throw err; } } const token = randomBytes(24).toString("hex"); this.pages.set(token, item); if (this.pages.size > MAX_PAGES) this.pages.delete(this.pages.keys().next().value!); return `http://localhost:${(this.server.address() as AddressInfo).port}/?session=${token}`; } async close(): Promise { this.pages.clear(); const server = this.server; this.server = undefined; if (server) await new Promise((resolve) => server.close(() => resolve())); } }