Files
dbtool-cli-v1/gui/preview-server.mjs
Paperclip CTO a28dab4cd9
Some checks failed
release-smoke / macos-13 / x86_64-apple-darwin (push) Has been cancelled
release-smoke / ubuntu-latest / x86_64-unknown-linux-gnu (push) Has been cancelled
release-smoke / windows-latest / x86_64-pc-windows-msvc (push) Has been cancelled
feat(usable): package gui-host validation snapshot
Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-03-31 10:21:36 +00:00

34 lines
1.1 KiB
JavaScript

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}`);
});