Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586// Static dev server for the slopbox example. Mirrors @nibling/kernel's own// scripts/serve.mjs in shape (the header policy is the package's: contentSecurityPolicy// and headersFor from @nibling/kernel/server); this file only decides which files on// disk answer which URL, on a different port (8932) so both servers can run at once.//// Four roots, one page:// / this package's own files (index.html, slopbox.js, its CSS, …)// /dist/demo/ this package's own build output (terminal.js, shell.js, bashkit.wasm)// /dist/runtime/ @nibling/kernel's BUILT package, read through the workspace link// at node_modules/@nibling/kernel/dist/runtime/ — this is what a real// consumer gets from `import "@nibling/kernel"`.// /dist/fixtures/ @nibling/kernel's built demo fixtures (catalog + .snap files).// NOT shipped in the published package (see its package.json// "files"); this reaches across the monorepo straight into// packages/kernel/dist/fixtures, since demo/apps/ and// scripts/fixtures.mjs are shared by ~9 of the runtime's own specs// and stay there. This path only works inside this monorepo// checkout — a standalone consumer of @nibling/kernel would build// its own fixtures (or ship its own app snapshots) instead.import { createServer } from "node:http";import { readFile } from "node:fs/promises";import { extname, join, normalize, resolve } from "node:path";import { fileURLToPath } from "node:url";import { contentSecurityPolicy, headersFor } from "@nibling/kernel/server";
const ROOT = resolve(fileURLToPath(new URL(".", import.meta.url)));const RUNTIME_DIST = resolve(ROOT, "node_modules/@nibling/kernel/dist/runtime");const RUNTIME_FIXTURES = resolve(ROOT, "../../packages/kernel/dist/fixtures");const PORT = Number(process.env.PORT ?? 8932);
const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".txt": "text/plain; charset=utf-8", ".json": "application/json", ".wasm": "application/wasm", ".snap": "application/octet-stream",};
// This whole server exists to serve one page, so it keeps the inline-styles CSP// exception the slopbox page needs everywhere (xterm writes runtime CSS — theme// colours, cell metrics — into <style> elements it creates, with no nonce option and// content that changes with theme and size, so no hash can cover it). It needs// 'unsafe-inline' script-src too, for the page's inline <script type="importmap">.//// The runtime SW registers with scope "/" (it must intercept /~/ at the origin root).// Its default location is beside host.js at /dist/runtime/sw.js, outside that scope, so// it needs Service-Worker-Allowed: /.// The npm gateways the build named (build.mjs) join connect-src.const npmGateways = await import("./dist/demo/npm-gateways.js").then((m) => m.GATEWAYS, () => []);const HEADERS = { csp: () => contentSecurityPolicy({ inlineScripts: true, inlineStyles: true, registry: true, npmGateways }), serviceWorkerAllowed: (path) => path === "/dist/runtime/sw.js",};
const server = createServer(async (req, res) => { const url = new URL(req.url ?? "/", `http://localhost:${PORT}`); let path = decodeURIComponent(url.pathname); if (path === "/") path = "/index.html";
let file; if (path.startsWith("/dist/runtime/")) file = join(RUNTIME_DIST, path.slice("/dist/runtime/".length)); else if (path.startsWith("/dist/fixtures/")) file = join(RUNTIME_FIXTURES, path.slice("/dist/fixtures/".length)); else file = join(ROOT, path); file = normalize(file);
const allowedRoot = file.startsWith(RUNTIME_DIST) || file.startsWith(RUNTIME_FIXTURES) || file.startsWith(ROOT); if (!allowedRoot) { res.writeHead(403).end("forbidden"); return; } try { const data = await readFile(file); const headers = { "Content-Type": MIME[extname(file)] ?? "application/octet-stream", ...headersFor(path, HEADERS) }; res.writeHead(200, headers).end(data); } catch { res.writeHead(404).end("not found"); }});
server.listen(PORT, () => { console.log(`@nibling/example-slopbox dev server → http://localhost:${PORT}/`);});