Files
unkernel/js/web.ts
Pooya Parsa b5b75b53de Run WASI programs from the guest on the host's wasm engine (prototype)
binfmt_misc hands a wasm module to kwwasi (kernel/guest/kwwasi.c), which
connects to the host over AF_VSOCK port 1021 and serves the program's
file I/O with its own descriptors, cwd and credentials. The host
(js/core/wasi-exec.ts, serveWasiPrograms(), the CLI's --wasi) compiles
the module once and runs each program in a worker of its own
(js/core/wasi-program.ts) with WASI preview 1, or preview 0 adapted.
Requests cross through a SharedArrayBuffer where one can be used, else
with JSPI and messages. kwwasi sends a module only when the host has not
compiled it for that file, resolves paths against the cwd or / by what
exists, and maps open flags from their asm-generic values to the
guest's.

alpine.sh builds kwwasi and kw-binfmt.sh registers it; no image with
them has been built yet, so test/wasi-binfmt.ts brings kwwasi in through
a mount.
2026-11-19 07:08:32 +00:00

49 lines
2.6 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// unkernel for browsers: the universal SDK plus HTTP disks with an
// IndexedDB overlay, memory sizing for the device, and the bundled kernel
// fetched (and compiled while it downloads) from next to this module.
//
// import { createKernel, wispBackend, DEFAULT_WISP_SERVERS } from "unkernel";
// const vm = await createKernel({ net: wispBackend(DEFAULT_WISP_SERVERS),
// onProgress: (p) => console.log(p) });
// vm.onOutput((b) => term.write(b));
// term.onData((d) => vm.write(d));
//
// Needs JSPI (WebAssembly.Suspending/promising); checkSupport() tells.
import { createKernelOn, type CreateKernelOptions, type KernelHandle, type KernelInstance, type WorkerOption } from "./core/instance.ts";
import type { WorkerKernel } from "./core/worker-host.ts";
import { webPlatform } from "./web/platform.ts";
export * from "./core/index.ts";
export { DATA_DISK_NAME, DATA_DISK_VERSION, IdbStore, dataDiskStore, importDataDisk, openDataDisk, openHttpDisk, resetDataDisk, resetHttpDisk } from "./web/disk.ts";
export type { DataDiskNameOptions, HttpDiskOptions, IdbStoreOptions, OpenDataDiskOptions } from "./web/disk.ts";
export { DEFAULT_MEMORY_FALLBACK_MIB, autoMemoryMiB, isMobile } from "./web/memory.ts";
export { webCpuBlocker } from "./web/cpu.ts";
export { serveWasiPrograms } from "./web/wasi.ts";
/**
* The bundled kernel: vmlinux32.wasm with the Alpine root file system and
* its command line in custom sections, next to this module (bundlers that
* understand `new URL(..., import.meta.url)` emit it as an asset).
*/
export const bundledKernelUrl: URL = new URL("./unkernel-alpine.wasm", import.meta.url);
const platform = webPlatform(bundledKernelUrl);
/**
* Load, compile and boot a kernel (default: the bundled Alpine) and return
* the running instance. Rejects with a BootError if it cannot start.
* Memory defaults to autoMemoryMiB() with DEFAULT_MEMORY_FALLBACK_MIB. With
* `worker: true` the kernel is loaded, compiled and run in a Web Worker and
* the result is a WorkerKernel (experimental; disks and the network as
* specs, see WorkerOption).
*/
export function createKernel(opts: CreateKernelOptions & { worker: true }): Promise<WorkerKernel>;
export function createKernel(opts?: CreateKernelOptions & { worker?: false }): Promise<KernelInstance>;
export function createKernel(opts?: CreateKernelOptions & WorkerOption): Promise<KernelHandle>;
export function createKernel(opts: CreateKernelOptions & WorkerOption = {}): Promise<KernelHandle> {
return createKernelOn(platform, opts);
}