// Demo page logic for demo/app.html: boot one app from dist/fixtures, the same way
// slopbox (examples/slopbox) does for its catalog — but with none of slopbox's UI. This
// exists only to back tests/vite.spec.ts and tests/sw-shadow.spec.ts, which used to
// drive the full slopbox page for the same assertions (a dev server rendering in an
// iframe, and the page's Service Worker controller).
import { boot } from "@nibling/kernel";
const params = new URLSearchParams(location.search);
const consoleEl = document.getElementById("console");
const frame = document.getElementById("app-frame");
const dec = new TextDecoder();
function log(text) {
consoleEl.textContent += text;
}
async function run(name) {
const [runtime, catalog] = await Promise.all([
boot(),
fetch("/dist/fixtures/index.json").then((res) => res.json()),
]);
const app = catalog.find((a) => a.name === name);
if (!app) throw new Error(`There is no app named "${name}".`);
const res = await fetch(`/dist/fixtures/${encodeURIComponent(name)}${app.install ? ".src" : ""}.snap`);
if (!res.ok) throw new Error(`fetching the snapshot for ${name} failed: ${res.status}`);
runtime.mountSnapshot(await res.arrayBuffer());
if (app.install) await runtime.install();
if (app.persist.length) await runtime.persist({ key: `app:${name}`, paths: app.persist.map((p) => `/app/${p}`) });
const proc = runtime.exec(app.entry, { env: app.env });
proc.stdout.pipeTo(new WritableStream({ write: (chunk) => log(dec.decode(chunk, { stream: true })) })).catch(() => {});
proc.stderr.pipeTo(new WritableStream({ write: (chunk) => log(dec.decode(chunk, { stream: true })) })).catch(() => {});
runtime.onListening((_port, url) => {
frame.src = url;
frame.hidden = false;
});
}
const app = params.get("app");
if (app) run(app).catch((e) => log(`BOOT FAILED: ${e?.message ?? String(e)}\n`));