import { createServer } from "node:http"; import { readFile } from "node:fs/promises"; import { extname, join, normalize } from "node:path"; const root = new URL("./", import.meta.url); const port = Number(process.env.GUI_PREVIEW_PORT || 4173); const contentTypes = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", }; const server = createServer(async (req, res) => { const requestPath = req.url === "/" ? "/index.html" : req.url || "/index.html"; const safePath = normalize(requestPath).replace(/^(\.\.[/\\])+/, ""); const filePath = new URL(`.${safePath}`, root); try { const body = await readFile(filePath); const type = contentTypes[extname(filePath.pathname)] || "text/plain; charset=utf-8"; res.writeHead(200, { "Content-Type": type }); res.end(body); } catch { const fallback = await readFile(join(root.pathname, "index.html")); res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); res.end(fallback); } }); server.listen(port, "127.0.0.1", () => { console.log(`GUI prototype server running at http://127.0.0.1:${port}`); });