// Minimal static file server for the Playwright harness. Serves the repo root
// so the harness page, the shipped ES modules under resources/js, and Mermaid
// from node_modules all load same-origin (native module imports need that).
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { extname, join, normalize } from 'node:path';
const root = process.cwd();
const port = Number(process.env.PORT || 5178);
const TYPES = {
'.html': 'text/html',
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.map': 'application/json',
};
createServer(async (req, res) => {
try {
const pathname = new URL(req.url, 'http://localhost').pathname;
const safe = normalize(decodeURIComponent(pathname)).replace(/^(\.\.[/\\])+/, '');
const body = await readFile(join(root, safe));
res.writeHead(200, { 'content-type': TYPES[extname(safe)] || 'application/octet-stream' });
res.end(body);
} catch {
res.writeHead(404);
res.end('not found');
}
}).listen(port, () => console.log(`truss e2e static server on ${port}`));
|