// Minimal zero-dependency static file server. // Serves the repository root so the test app at /test-app/index.html can import // the built SDK bundle from /dist/imagekit.esm.js. Used by Playwright's webServer. import { createServer } from "node:http"; import { readFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import { dirname, join, normalize, extname } from "node:path"; const __dirname = dirname(fileURLToPath(import.meta.url)); const ROOT = join(__dirname, ".."); const PORT = Number(process.env.PORT) || 4321; const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json; charset=utf-8", ".map": "application/json; charset=utf-8", ".svg": "image/svg+xml", }; const server = createServer(async (req, res) => { try { let pathname = decodeURIComponent(new URL(req.url, `http://localhost`).pathname); if (pathname === "/") pathname = "/test-app/index.html"; // Resolve and prevent path traversal outside ROOT. const filePath = normalize(join(ROOT, pathname)); if (!filePath.startsWith(ROOT)) { res.writeHead(403).end("Forbidden"); return; } const data = await readFile(filePath); res.writeHead(200, { "content-type": MIME[extname(filePath)] || "application/octet-stream" }); res.end(data); } catch { res.writeHead(404).end("Not found"); } }); server.listen(PORT, () => { console.log(`Static server running at http://localhost:${PORT}`); });