// Plan2Code Web Console - serving the page's own files: the allow-list, types, caching and the accent colours. import fs from "node:fs"; import path from "node:path"; import crypto from "node:crypto"; import { PUBLIC_DIR } from "./files.mjs"; import { ACCENTS, DEFAULT_ACCENT } from "./public/palette.js"; /* ------------------------------------------------------------ mime types */ export const TYPES = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml", ".mp3": "audio/mpeg", ".png": "image/png", }; // A hardcoded allow-list, not a filesystem lookup. Nothing to traverse. export const SERVABLE = new Set([ "app.js", "answers.js", "skills.js", "spec-states.js", "state.js", "doc-files.js", "send-state.js", "mention-box.js", "uploads.js", "sound.js", "looks.js", "selectors.js", "app.css", "render.js", "palette.js", "labels.js", "boot.js", "chat.js", "help.js", "starters.js", "workspace.js", "meter.js", "subagents.js", "mentions.js", "favicon.js", "git-board.js", "git-board-view.js", "pr-url.js", "start-stop.mp3", "next.mp3", "insert.mp3", "sleeping.mp3", "bootup.mp3", "start-skill.mp3", "favicon.svg", "favicon.png", "screens/core.js", "screens/ending.js", "screens/docs.js", "screens/cards.js", "screens/starting.js", "screens/dashboard.js", "screens/git-host.js", "screens/looks.js", "screens/cleanup.js", "screens/help-dialog.js", "screens/workspace.js", "screens/ask.js", "net.js", "vendor/marked.esm.js", ]); /* ------------------------------------------------------------ stylesheet */ // The page asks for one /app.css; it is these files under public/styles/, // joined in this order, which is the one place the cascade order is stated. // Layers first, then one file per screen, then responsive and print last so // its overrides still come after everything they override. The files are not // on SERVABLE: they reach the page only through /app.css. export const CSS_ORDER = ["tokens.css", "shell.css", "planny.css", "pieces.css", "dashboard.css", "poses.css", "docs.css", "ask.css", "ending.css", "help.css", "looks.css", "git.css", "responsive.css"]; // The files joined with one newline between them (each file's own last newline // is that one), as the server serves them before the accent rules. The test // helper joins through this too, so a test can never read a different order. export function joinedStyles() { const parts = CSS_ORDER.map((f) => fs.readFileSync(path.join(PUBLIC_DIR, "styles", f), "utf8").replace(/\n$/, "")); return Buffer.from(parts.join("\n") + "\n"); } /* ---------------------------------------------------------------- assets */ // Read once per server, from memory after that. Held for the server's whole // life on purpose: a skill reinstalled under a running session must not hand // one page a new app.js beside the old answers.js it imports. // // Revalidated rather than stored: `no-cache` plus an ETag makes a reload a // round of 304s instead of 400KB again. Every session is a new port, and so a // new origin with a cold cache, which is why the first load is made cheap by // other means (preload hints, the inline boot state) rather than by caching. const assets = new Map(); export const etagOf = (body) => `"${crypto.createHash("sha1").update(body).digest("base64url").slice(0, 20)}"`; export function asset(rel) { let a = assets.get(rel); if (!a) { let body = rel === "app.css" ? joinedStyles() : fs.readFileSync(path.join(PUBLIC_DIR, rel)); if (rel === "app.css") body = Buffer.concat([body, Buffer.from(accentCss())]); const etag = etagOf(body); a = { body, etag, type: TYPES[path.extname(rel)] || "application/octet-stream" }; assets.set(rel, a); } return a; } // The saved highlight color, painted from the very first frame. The page's // script owns the colors once it runs (applyLooks clears the attribute these // hang off), but until then the only way to avoid flashing the default ocean // at someone who picked violet is a stylesheet rule the server can switch on // with an attribute: the CSP forbids inline styles, and the colors live in // palette.js, so they are generated from there rather than copied into CSS. function accentCss() { const vars = (shades) => ["--accent", "--accent-soft", "--accent-ink", "--accent-line"].map((n, i) => `${n}:${shades[i]}`).join(";"); let css = "\n/* Generated by server.mjs from palette.js: the saved accent before app.js runs. */\n"; for (const [id, a] of Object.entries(ACCENTS)) { if (id === DEFAULT_ACCENT) continue; const sel = `:root[data-looks-accent="${id}"]`; css += `${sel}{${vars(a.light)}}\n`; css += `@media (prefers-color-scheme: dark){${sel}:not([data-theme="light"]){${vars(a.dark)}}}\n`; css += `${sel}[data-theme="dark"]{${vars(a.dark)}}\n`; } return css; }