// A stand-in hub at :4801: /v1 and the stream from the fixtures, the page itself from a real hub // with the hub address in its network diagram replaced. Capture each page with headless chromium. import {readFileSync, existsSync} from 'fs' // Any hub serves the page's own files; nothing of its data is read. const HUB = process.env.MIDAS_HUB ?? 'http://10.44.0.1:4791', DIR = process.env.OUT ?? 'fake' Bun.serve({ port: Number(process.env.PORT ?? 4801), async fetch(req, server) { const u = new URL(req.url) if (u.pathname === '/v1/stream') { server.upgrade(req); return } if (u.pathname.startsWith('/v1/')) { const base = u.pathname.replace(/[^a-zA-Z0-9]+/g, '_') + (u.searchParams.get('use') ? '_use' : '') + (u.searchParams.get('step') ? `_${u.searchParams.get('step')}` : '') const f = `${DIR}/${base}.json` if (existsSync(f)) return new Response(readFileSync(f), {headers: {'content-type': 'application/json'}}) console.log('MISS', u.pathname + u.search); return Response.json(u.pathname.includes('search') ? [] : {}, {status: 404}) } // PAGE=../../web/dist: the page as built here, for a change not on any hub yet. if (process.env.PAGE) { const f = `${process.env.PAGE}${u.pathname}` return new Response(Bun.file(existsSync(f) && !f.endsWith('/') ? f : `${process.env.PAGE}/index.html`)) } const r = await fetch(HUB + u.pathname + u.search) const h = new Headers(r.headers); h.delete('content-encoding'); h.delete('content-length') const ct = r.headers.get("content-type") ?? "" if (/javascript|html/.test(ct)) return new Response((await r.text()).replaceAll("10.44.0.1", "10.10.0.1"), {status: r.status, headers: h}) return new Response(await r.arrayBuffer(), {status: r.status, headers: h}) }, websocket: { open(ws) { for (const m of JSON.parse(readFileSync(`${DIR}/stream.json`, 'utf8'))) ws.send(m) }, message() {}, }, })