SDK

Serve mode

Run an HTTP server inside the VM and reach it from a real iframe — a service worker injects browser requests into the in-VM socket and streams the response back out.

Register the bridge

javascript
import { ServeBridge, startServer } from "@userland-run/nano-sdk";

const bridge = await ServeBridge.register({
  injector: nano.virtualServer,
  swUrl: "/nano-sw.js", // the ./service-worker export
});

The service worker intercepts requests under its scope and forwards them through the injector into the VM.

Start a server

javascript
nano.fs.writeFile("/srv/server.js", SERVER_SOURCE);
const server = await startServer(nano, { node: ["/srv/server.js"] }, {
  readyPattern: /listening/i,
  onReady: () => console.log("ready"),
});

startServer launches the process, resolves once output matches readyPattern (default /listening/i), and returns { stop }. It rejects if the process exits before becoming ready.

Preview it

javascript
iframe.src = bridge.previewUrl(8080, "/");
// …later
server.stop();

Parsing responses

parseHttpResponse(bytes) turns a raw HTTP/1.1 response into { status, statusText, headers, body }, handling chunked transfer-encoding for you.

This is the same request path as the web demo's live preview — see the Virtual server page for the emulator side.