|
| 1 | +import { createReadStream, existsSync, statSync } from "node:fs"; |
| 2 | +import { createServer } from "node:http"; |
| 3 | +import { extname, join, normalize, resolve } from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { createCommandBoardServer } from "../commandboard-api/dist/index.js"; |
| 6 | + |
| 7 | +const appDirectory = fileURLToPath(new URL(".", import.meta.url)); |
| 8 | +const distDirectory = resolve(appDirectory, "dist"); |
| 9 | +const indexFile = join(distDirectory, "index.html"); |
| 10 | +const apiServer = createCommandBoardServer(); |
| 11 | +const port = Number(process.env.PORT ?? 4173); |
| 12 | + |
| 13 | +const mimeTypes = { |
| 14 | + ".css": "text/css; charset=utf-8", |
| 15 | + ".html": "text/html; charset=utf-8", |
| 16 | + ".ico": "image/x-icon", |
| 17 | + ".js": "text/javascript; charset=utf-8", |
| 18 | + ".json": "application/json; charset=utf-8", |
| 19 | + ".png": "image/png", |
| 20 | + ".svg": "image/svg+xml", |
| 21 | + ".webmanifest": "application/manifest+json; charset=utf-8" |
| 22 | +}; |
| 23 | + |
| 24 | +createServer((request, response) => { |
| 25 | + const url = new URL(request.url ?? "/", `http://${request.headers.host ?? "localhost"}`); |
| 26 | + |
| 27 | + if (url.pathname === "/health" || url.pathname.startsWith("/api/")) { |
| 28 | + apiServer.emit("request", request, response); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (request.method !== "GET" && request.method !== "HEAD") { |
| 33 | + response.writeHead(405, { allow: "GET, HEAD" }); |
| 34 | + response.end("Method not allowed"); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const file = resolveStaticPath(url.pathname); |
| 39 | + if (!file) { |
| 40 | + response.writeHead(403); |
| 41 | + response.end("Forbidden"); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + sendFile(file, request.method === "HEAD", response); |
| 46 | +}).listen(port, () => { |
| 47 | + console.log(`CommandBoard.run PWA listening on http://localhost:${port}`); |
| 48 | +}); |
| 49 | + |
| 50 | +function resolveStaticPath(pathname) { |
| 51 | + const decodedPath = decodeURIComponent(pathname); |
| 52 | + const normalizedPath = normalize(decodedPath).replace(/^(\.\.[/\\])+/, ""); |
| 53 | + let candidate = join(distDirectory, normalizedPath); |
| 54 | + |
| 55 | + if (!candidate.startsWith(distDirectory)) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + if (existsSync(candidate) && statSync(candidate).isDirectory()) { |
| 60 | + candidate = join(candidate, "index.html"); |
| 61 | + } |
| 62 | + |
| 63 | + if (existsSync(candidate) && statSync(candidate).isFile()) { |
| 64 | + return candidate; |
| 65 | + } |
| 66 | + |
| 67 | + return indexFile; |
| 68 | +} |
| 69 | + |
| 70 | +function sendFile(file, headOnly, response) { |
| 71 | + if (!existsSync(file)) { |
| 72 | + response.writeHead(500, { "content-type": "text/plain; charset=utf-8" }); |
| 73 | + response.end("Build output missing. Run `npm run build` before `npm start`."); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const extension = extname(file); |
| 78 | + response.writeHead(200, { |
| 79 | + "cache-control": extension === ".html" ? "no-store" : "public, max-age=31536000, immutable", |
| 80 | + "content-type": mimeTypes[extension] ?? "application/octet-stream" |
| 81 | + }); |
| 82 | + |
| 83 | + if (headOnly) { |
| 84 | + response.end(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + createReadStream(file).pipe(response); |
| 89 | +} |
0 commit comments