|
| 1 | +import { createReadStream, existsSync } from "node:fs"; |
| 2 | +import { stat } from "node:fs/promises"; |
| 3 | +import { createServer } from "node:http"; |
| 4 | +import { extname, join, normalize, resolve } from "node:path"; |
| 5 | + |
| 6 | +const port = Number.parseInt(process.env.PORT ?? "4173", 10); |
| 7 | +const host = process.env.HOST ?? "127.0.0.1"; |
| 8 | +const distRoot = resolve(process.cwd(), "dist"); |
| 9 | + |
| 10 | +const mimeTypes = { |
| 11 | + ".css": "text/css; charset=utf-8", |
| 12 | + ".gif": "image/gif", |
| 13 | + ".html": "text/html; charset=utf-8", |
| 14 | + ".ico": "image/x-icon", |
| 15 | + ".jpeg": "image/jpeg", |
| 16 | + ".jpg": "image/jpeg", |
| 17 | + ".js": "text/javascript; charset=utf-8", |
| 18 | + ".json": "application/json; charset=utf-8", |
| 19 | + ".mp3": "audio/mpeg", |
| 20 | + ".png": "image/png", |
| 21 | + ".svg": "image/svg+xml", |
| 22 | + ".ttf": "font/ttf", |
| 23 | + ".txt": "text/plain; charset=utf-8", |
| 24 | + ".wav": "audio/wav", |
| 25 | + ".webp": "image/webp", |
| 26 | + ".woff": "font/woff", |
| 27 | + ".woff2": "font/woff2", |
| 28 | +}; |
| 29 | + |
| 30 | +const sendFile = async (filePath, response) => { |
| 31 | + const fileInfo = await stat(filePath); |
| 32 | + const extension = extname(filePath).toLowerCase(); |
| 33 | + |
| 34 | + response.writeHead(200, { |
| 35 | + "Content-Length": fileInfo.size, |
| 36 | + "Content-Type": mimeTypes[extension] ?? "application/octet-stream", |
| 37 | + "Cache-Control": "no-store", |
| 38 | + }); |
| 39 | + |
| 40 | + createReadStream(filePath).pipe(response); |
| 41 | +}; |
| 42 | + |
| 43 | +const server = createServer(async (request, response) => { |
| 44 | + const method = request.method ?? "GET"; |
| 45 | + if (!["GET", "HEAD"].includes(method)) { |
| 46 | + response.writeHead(405, { "Content-Type": "text/plain; charset=utf-8" }); |
| 47 | + response.end("Method Not Allowed"); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const requestUrl = new URL(request.url ?? "/", `http://${host}:${port}`); |
| 53 | + const requestPath = decodeURIComponent(requestUrl.pathname === "/" ? "/index.html" : requestUrl.pathname); |
| 54 | + const candidatePath = normalize(join(distRoot, requestPath)); |
| 55 | + const safePath = candidatePath.startsWith(distRoot) ? candidatePath : join(distRoot, "index.html"); |
| 56 | + const filePath = existsSync(safePath) ? safePath : join(distRoot, "index.html"); |
| 57 | + |
| 58 | + try { |
| 59 | + if (method === "HEAD") { |
| 60 | + const fileInfo = await stat(filePath); |
| 61 | + const extension = extname(filePath).toLowerCase(); |
| 62 | + response.writeHead(200, { |
| 63 | + "Content-Length": fileInfo.size, |
| 64 | + "Content-Type": mimeTypes[extension] ?? "application/octet-stream", |
| 65 | + "Cache-Control": "no-store", |
| 66 | + }); |
| 67 | + response.end(); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + await sendFile(filePath, response); |
| 73 | + } catch { |
| 74 | + response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); |
| 75 | + response.end("Not Found"); |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +server.listen(port, host, () => { |
| 80 | + console.log(`Serving dist at http://${host}:${port}`); |
| 81 | +}); |
0 commit comments