/** * Shared static host + Chrome discovery for browser runners and the MCP session. * * CLI (for probes and for web/tools/manual/capture.mjs --origin): * node tools/static-host.mjs [--root ] [--port ] [--document ] * Prints ORIGIN=http://127.0.0.1: and stays up until killed. --root may * be relative (to the current directory) or absolute; it is resolved here. */ import fs from "node:fs"; import http from "node:http"; import path from "node:path"; import { fileURLToPath } from "node:url"; export const CHROME_CANDIDATES = ["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable", "/opt/google/chrome/google-chrome"]; export function findChrome() { const override = process.env.FREECAD_CHROME; if (override) return fs.existsSync(override) ? override : null; return CHROME_CANDIDATES.find((candidate) => fs.existsSync(candidate)) ?? null; } export const ISOLATION_HEADERS = { "cross-origin-opener-policy": "same-origin", "cross-origin-embedder-policy": "require-corp", }; // Chrome refuses to render when the response is octet-stream, // so every type the shell actually loads has to be listed explicitly. const CONTENT_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", ".json": "application/json", ".wasm": "application/wasm", ".svg": "image/svg+xml", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".ico": "image/x-icon", ".woff": "font/woff", ".woff2": "font/woff2", ".ttf": "font/ttf", ".txt": "text/plain; charset=utf-8", ".map": "application/json", ".md": "text/markdown; charset=utf-8", }; /** * Serve a built dist directory on an ephemeral loopback port with COOP/COEP. * Returns { server, origin, close }. */ export async function startStaticHost(distRoot, defaultDocument = "index.html", port = 0) { // Resolve once: with a relative root the traversal guard below compared an // absolute file path against a relative prefix and answered 403 for every // request. const resolvedRoot = path.resolve(distRoot); if (!fs.existsSync(path.join(resolvedRoot, defaultDocument))) throw new Error(`${resolvedRoot}/${defaultDocument} is missing; run npm run build first`); const server = http.createServer((request, response) => { const requestPath = decodeURIComponent(new URL(request.url ?? "/", "http://127.0.0.1").pathname); if (requestPath === "/favicon.ico") { response.writeHead(204, ISOLATION_HEADERS); response.end(); return; } const relative = requestPath === "/" ? defaultDocument : requestPath.replace(/^\/+/, ""); const filePath = path.resolve(resolvedRoot, relative); if (filePath !== resolvedRoot && !filePath.startsWith(`${resolvedRoot}${path.sep}`)) { response.writeHead(403); response.end("forbidden"); return; } if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { response.writeHead(404); response.end("not found"); return; } response.writeHead(200, { "content-type": CONTENT_TYPES[path.extname(filePath)] ?? "application/octet-stream", "cache-control": "no-store", ...ISOLATION_HEADERS }); fs.createReadStream(filePath).pipe(response); }); await new Promise((resolve, reject) => { server.once("error", reject); server.listen(port, "127.0.0.1", resolve); }); return { server, origin: `http://127.0.0.1:${server.address().port}`, root: resolvedRoot, close: () => server.close() }; } /* --- CLI ---------------------------------------------------------------- */ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const argv = process.argv.slice(2); const option = (name, fallback) => { const i = argv.indexOf(name); return i >= 0 && i + 1 < argv.length ? argv[i + 1] : fallback; }; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const distRoot = option("--root", path.join(repoRoot, "web", "dist")); const document = option("--document", "index.html"); const port = Number(option("--port", "0")); const host = await startStaticHost(distRoot, document, port); console.log(`ORIGIN=${host.origin}`); console.log(`ROOT=${host.root}`); console.log(`DOCUMENT=${document}`); const stop = () => { host.close(); process.exit(0); }; process.on("SIGINT", stop); process.on("SIGTERM", stop); }