// Serves public/ on http://localhost:5173 for working on the app without a Sandbar-hosted copy. // The app then needs the server's address, and the server must allow this origin: // SANDBAR_APP_ORIGIN=http://localhost:5173 import { createServer } from 'node:http'; import { readFile } from 'node:fs/promises'; import { extname, join, normalize } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = fileURLToPath(new URL('../public/', import.meta.url)); const types = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml', '.png': 'image/png', '.webmanifest': 'application/manifest+json' }; const port = Number(process.env.PORT || 5173); createServer(async (req, res) => { let p = normalize(decodeURIComponent(new URL(req.url, 'http://x').pathname)).replace(/^([/\])+/, ''); if (p === '' || p.endsWith('/')) p += 'index.html'; if (p.includes('..')) { res.writeHead(400).end(); return; } try { const body = await readFile(join(root, p)); res.writeHead(200, { 'Content-Type': types[extname(p)] || 'application/octet-stream', 'Cache-Control': 'no-cache' }).end(body); } catch { res.writeHead(404).end('not found'); } }).listen(port, () => console.log(`http://localhost:${port}`));