import { createReadStream, existsSync, statSync } from "node:fs"; import { createServer } from "node:http"; import { extname, join, normalize } from "node:path"; const packagedRoot = join(process.cwd(), "public"); const root = existsSync(join(packagedRoot, "index.html")) ? packagedRoot : join(process.cwd(), "out"); const port = Number(process.env.APP_PORT || 3200); const mimeTypes = { ".css": "text/css; charset=utf-8", ".html": "text/html; charset=utf-8", ".ico": "image/x-icon", ".js": "text/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".png": "image/png", ".rsc": "text/x-component", ".svg": "image/svg+xml", ".webp": "image/webp", }; createServer((request, response) => { const pathname = decodeURIComponent(new URL(request.url || "/", "http://localhost").pathname); const relativePath = normalize(pathname).replace(/^([/\\])+/, ""); let filePath = join(root, relativePath || "index.html"); if (!filePath.startsWith(root) || !existsSync(filePath)) filePath = join(root, "index.html"); if (existsSync(filePath) && statSync(filePath).isDirectory()) filePath = join(filePath, "index.html"); if (!existsSync(filePath)) { response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); response.end("Not found"); return; } response.writeHead(200, { "Content-Type": mimeTypes[extname(filePath)] || "application/octet-stream", "Cache-Control": filePath.endsWith("index.html") ? "no-cache" : "public, max-age=31536000, immutable", }); if (request.method === "HEAD") response.end(); else createReadStream(filePath).pipe(response); }).listen(port, "0.0.0.0", () => { console.log(`Pixel bead pattern web listening on ${port}`); });