import { createServer } from 'node:http'; import { readFile } from 'node:fs/promises'; import { extname, resolve, sep } from 'node:path'; // Instrument built pages locally; neither the harness nor its UI ships to visitors. const root = resolve('dist'); const port = Number(process.env.BENCHMARK_PORT || 4322); const mime = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.svg': 'image/svg+xml', '.png': 'image/png', '.gif': 'image/gif', '.avif': 'image/avif', '.woff2': 'font/woff2', '.xml': 'application/xml', '.txt': 'text/plain', }; const harness = await readFile(new URL('./render-benchmark.js', import.meta.url)); createServer(async (request, response) => { try { const path = decodeURIComponent(new URL(request.url, `http://127.0.0.1:${port}`).pathname); if (path === '/__render-benchmark.js') { response.writeHead(200, { 'Content-Type': mime['.js'] }).end(harness); return; } if (path === '/__baseline/') { response.writeHead(200, { 'Content-Type': mime['.html'] }).end(`
No site styles, artwork, graph, or animation.
Measure the site`); return; } const file = resolve(root, `.${path.endsWith('/') ? `${path}index.html` : path}`); if (!file.startsWith(root + sep)) { response.writeHead(403).end(); return; } let body = await readFile(file); if (extname(file) === '.html') body = Buffer.from( body.toString().replace('', ''), ); response .writeHead(200, { 'Content-Type': mime[extname(file)] || 'application/octet-stream', 'Cache-Control': 'no-store', }) .end(body); } catch { response.writeHead(404).end('Not found'); } }).listen(port, '127.0.0.1', () => console.log(`Rendering benchmark: http://127.0.0.1:${port}/`));