Something went wrong. Try again.
A terminal UI for Libby libbui.vinnymac.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145// Serves dist/ for libbui.vinnymac.dev, behind a Cloudflare tunnel. No// dependencies: node:http and the file system, loopback only.//// PORT=5179 node server.mjsimport { createReadStream } from "node:fs";import { stat } from "node:fs/promises";import { createServer } from "node:http";import { extname, join, normalize, sep } from "node:path";import { fileURLToPath } from "node:url";
const ROOT = process.env.LIBBUI_WEB_ROOT ? `${process.env.LIBBUI_WEB_ROOT.replace(/\/$/, "")}/` : fileURLToPath(new URL("./dist/", import.meta.url));const PORT = Number(process.env.PORT ?? 5179);const HOST = process.env.HOST ?? "127.0.0.1";
const TYPES = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".svg": "image/svg+xml", ".wasm": "application/wasm", ".json": "application/json", ".txt": "text/plain; charset=utf-8", ".png": "image/png",};
const SECURITY = { // The kernel and its JIT compile WebAssembly; xterm.js sets inline styles. "content-security-policy": [ "default-src 'self'", "script-src 'self' 'wasm-unsafe-eval'", "style-src 'self' 'unsafe-inline'", "img-src 'self' data:", "font-src 'self'", "connect-src 'self'", "worker-src 'self' blob:", "object-src 'none'", "base-uri 'none'", "form-action 'none'", "frame-ancestors 'none'", ].join("; "), "cross-origin-opener-policy": "same-origin", "cross-origin-embedder-policy": "require-corp", "cross-origin-resource-policy": "same-origin", "referrer-policy": "no-referrer", "permissions-policy": "camera=(), microphone=(), geolocation=(), interest-cohort=()", "x-content-type-options": "nosniff", "strict-transport-security": "max-age=31536000",};
/** Codings the client accepts, without those it refuses with `q=0`. */function acceptedEncodings(header) { const accepted = new Set(); for (const part of String(header ?? "").split(",")) { const [name, ...params] = part.trim().toLowerCase().split(";"); const q = params.map((p) => p.trim()).find((p) => p.startsWith("q=")); if (name && !(q && Number(q.slice(2)) === 0)) accepted.add(name); } return accepted;}
function send(res, status, body) { res.writeHead(status, { ...SECURITY, "content-type": "text/plain; charset=utf-8" }); res.end(body);}
const server = createServer(async (req, res) => { if (req.method !== "GET" && req.method !== "HEAD") { send(res, 405, "method not allowed\n"); return; } let pathname; try { pathname = decodeURIComponent(new URL(req.url ?? "/", "http://x").pathname); } catch { send(res, 400, "bad request\n"); return; } if (pathname.endsWith("/")) pathname += "index.html"; const file = normalize(join(ROOT, pathname)); // Inside the root, and no dotfiles (the root itself may sit under ~/.local). if (!file.startsWith(ROOT) || file.slice(ROOT.length).split(sep).some((part) => part.startsWith("."))) { send(res, 404, "not found\n"); return; } let info; try { info = await stat(file); } catch { send(res, 404, "not found\n"); return; } if (!info.isFile()) { send(res, 404, "not found\n"); return; } // Precompressed siblings from scripts/compress.mjs, when the client takes them. const accepts = acceptedEncodings(req.headers["accept-encoding"]); let body = file; let size = info.size; let encoding; for (const [token, suffix] of [["br", ".br"], ["gzip", ".gz"]]) { if (!accepts.has(token)) continue; const packed = await stat(file + suffix).catch(() => undefined); if (packed?.isFile()) { body = file + suffix; size = packed.size; encoding = token; break; } } const hashed = pathname.startsWith("/assets/"); res.writeHead(200, { ...SECURITY, "content-type": TYPES[extname(file)] ?? "application/octet-stream", "content-length": size, // The page names its assets by hash, so it must be fresh after a deploy. "cache-control": hashed ? "public, max-age=31536000, immutable" : extname(file) === ".html" ? "no-cache" : "public, max-age=300", vary: "accept-encoding", // Lets the page show download progress when the body is compressed. "x-decoded-length": info.size, ...(encoding ? { "content-encoding": encoding } : {}), }); if (req.method === "HEAD") { res.end(); return; } createReadStream(body) .on("error", (error) => { console.error(error); res.destroy(error); }) .pipe(res);});
server.listen(PORT, HOST, () => { console.log(`libbui.vinnymac.dev on http://${HOST}:${PORT}`);});