/* eslint-disable no-console */ import { createReadStream, statSync } from 'node:fs' import http from 'node:http' import path from 'node:path' // Tiny static-file server that serves the canonical Crewli prototype // HTML so Playwright visual baselines can render it in real Chromium. // // Why not http-server / serve / similar? // -------------------------------------- // They'd add ~5 MB and another supply-chain hop for a 30-line problem. // The prototype directory has 9 files; we serve them with mime types // for .html, .css, .js, .jsx (text/babel handles via the HTML loader). // Node's built-in http + fs is sufficient. // // Started by playwright-ct.config.ts via webServer config. const ROOT = path.resolve( process.cwd(), '..', '..', 'resources', 'Crewli - Artist Timetable Management', ) const PORT = Number(process.env.PROTOTYPE_PORT ?? 5179) const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'application/javascript; charset=utf-8', '.jsx': 'text/babel; charset=utf-8', '.mjs': 'application/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.json': 'application/json; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.ico': 'image/x-icon', } const server = http.createServer((req, res) => { // Strip query / hash before path resolution. const urlPath = (req.url ?? '/').split('?')[0].split('#')[0] const safe = path.normalize(urlPath).replace(/^(\.\.[\\/])+/, '') const filePath = path.join(ROOT, safe === '/' ? 'crewli-timetable.html' : safe) // Reject path traversal. if (!filePath.startsWith(ROOT)) { res.statusCode = 403 res.end('Forbidden') return } try { const stat = statSync(filePath) if (stat.isDirectory()) { res.statusCode = 404 res.end('Not found') return } const ext = path.extname(filePath).toLowerCase() res.setHeader('Content-Type', MIME[ext] ?? 'application/octet-stream') res.setHeader('Cache-Control', 'no-store') createReadStream(filePath).pipe(res) } catch { res.statusCode = 404 res.end(`Not found: ${urlPath}`) } }) server.listen(PORT, '127.0.0.1', () => { console.log(`[prototype-server] http://127.0.0.1:${PORT}/ -> ${ROOT}`) })