SpinShare Referee Bot refbot.ellite.dev/overlay

update http.js to serve files correctly both in dev and prod

+14 -3
+14 -3
state/http.js
··· 32 32 const url = new URL(req.url, `http://${req.headers.host}`); 33 33 34 34 res.setHeader('Access-Control-Allow-Origin', '*'); 35 + res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); 36 + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); 37 + 38 + if (req.method === 'OPTIONS') { 39 + res.writeHead(204); 40 + res.end(); 41 + return; 42 + } 35 43 36 44 if (req.method === 'GET' && url.pathname === '/state') { 37 45 const snapshot = buildStateSnapshot(); 38 - res.writeHead(200, { 'Content-Type': 'application/json' }); 46 + res.setHeader('Content-Type', 'application/json'); 47 + res.writeHead(200); 39 48 res.end(JSON.stringify(snapshot)); 40 49 return; 41 50 } 42 51 43 - const overlayPath = path.join(__dirname, '../overlay', url.pathname === '/' ? 'index.html' : url.pathname); 52 + const strippedPath = url.pathname.replace(/^\/overlay(\/|$)/, '/') || '/'; 53 + const overlayPath = path.join(__dirname, '../overlay', strippedPath === '/' ? 'index.html' : strippedPath); 44 54 if (fs.existsSync(overlayPath)) { 45 55 const ext = path.extname(overlayPath); 46 56 const contentTypes = { ··· 51 61 '.jpg': 'image/jpeg', 52 62 '.svg': 'image/svg+xml', 53 63 }; 54 - res.writeHead(200, { 'Content-Type': contentTypes[ext] ?? 'application/octet-stream' }); 64 + res.setHeader('Content-Type', contentTypes[ext] ?? 'application/octet-stream'); 65 + res.writeHead(200); 55 66 fs.createReadStream(overlayPath).pipe(res); 56 67 return; 57 68 }