Files

29 lines
1.5 KiB
JavaScript

// Local-only static host for regression tests. Unknown URLs keep a real 404 status.
import { createServer } from "node:http";
import { readFile, stat } from "node:fs/promises";
import path from "node:path";
const root = path.resolve("out");
const types = { ".html": "text/html; charset=utf-8", ".js": "application/javascript", ".css": "text/css", ".json": "application/json", ".txt": "text/plain; charset=utf-8", ".xml": "application/xml", ".svg": "image/svg+xml", ".webp": "image/webp", ".png": "image/png", ".jpg": "image/jpeg", ".woff2": "font/woff2" };
createServer(async (request, response) => {
try {
const url = new URL(request.url, "http://127.0.0.1");
let file = path.resolve(root, `.${decodeURIComponent(url.pathname)}`);
if (file !== root && !file.startsWith(root + path.sep)) {
response.writeHead(403).end(); return;
}
if ((await stat(file)).isDirectory()) {
if (!url.pathname.endsWith("/")) {
response.writeHead(308, { Location: `${url.pathname}/${url.search}` }).end(); return;
}
file = path.join(file, "index.html");
}
const body = await readFile(file);
response.writeHead(200, { "Content-Type": types[path.extname(file)] || "application/octet-stream", "Cache-Control": "no-store" });
response.end(body);
} catch {
response.writeHead(404, { "Content-Type": "text/html; charset=utf-8" });
response.end(await readFile(path.join(root, "404.html")));
}
}).listen(4173, "127.0.0.1", () => console.log("Static export: http://127.0.0.1:4173"));