import Bun from 'bun'; import path from 'path'; import browser from 'browser-detect'; import { buildHTML } from './html'; import { isGame } from './isGame'; Bun.serve({ async fetch(req) { const url = new URL(req.url); const pathname = path.basename(url.pathname); const gameParam = url.searchParams.get('game'); const game = (gameParam && await isGame(gameParam)) ? gameParam : 'index'; switch (pathname) { case '': case '/': case 'index.html': try { const detectedBrowser = browser(req.headers.get('user-agent') ?? ''); const html = await buildHTML( game, { production: url.searchParams.get('production') === 'true', // to debug production builds mobile: detectedBrowser.mobile || url.searchParams.get('mobile') === 'true', local: true, } ); if (html) { return new Response(html, { headers: { 'Content-Type': 'text/html;charset=utf-8' } }); } } catch (e) { console.error(e); return new Response(`Error building HTML: ${e}`, { status: 500 }); } return new Response(`Error building HTML`, { status: 500 }); default: console.log(`[Dev Server] requested unknown pathname: ${pathname}`); return new Response(null, { status: 404 }); } } })