Files
unkernel/js/web.ts
Pooya Parsa d80c033106 net: drop the default Wisp server from the SDK
The SDK named the project's Wisp server (DEFAULT_WISP_SERVERS), and the
Node entry sent Origin: http://localhost to pass that server's allow
list (NODE_WISP_HEADERS). The server accepts only the page's origins,
so both are gone: wispBackend() takes the caller's server, and Node
sends handshake headers only when `headers` asks for them. The page
keeps wss://wisp.unkernel.dev:6680/ as its own default (web/src/net.ts).

The live Wisp test takes its server from UNKERNEL_TEST_WISP.
2026-11-27 22:24:02 +00:00

78 lines
4.0 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, Web Audio output, memory sizing for the device, and
// the bundled kernel fetched (and compiled while it downloads) from next to
// this module.
// Distro images are not downloaded here (GitHub release assets cannot be
// fetched cross-origin): host alpine.erofs (from a release, with its
// distros.json) yourself and pass its URL, read lazily with Range requests.
//
// import { createKernel, wispBackend } from "unkernel";
// const vm = await createKernel({ distro: "/distros/alpine.erofs",
// net: wispBackend("wss://wisp.example/"), onProgress: (p) => console.log(p) });
// vm.onOutput((b) => term.write(b));
// term.onData((d) => vm.write(d));
//
// Needs JSPI (WebAssembly.Suspending/promising); checkSupport() tells.
// `gl: true` gives the guest GL through WebGL2 (virgl.mjs, loaded only then).
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, openDistroDisk, 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 { createAudioOut } from "./web/audio.ts";
export type { AudioOut, AudioOutOptions, AudioOutState, AudioOutStats } from "./web/audio.ts";
export { webCpuBlocker } from "./web/cpu.ts";
export { serveWasiPrograms } from "./web/wasi.ts";
/**
* The bundled kernel: vmlinux32.wasm with the base initramfs, its command
* line, the data disk template and the patch to the SMP kernel 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.wasm", import.meta.url);
/**
* The guest GL renderer (createKernel({ gl: true })): virglrenderer compiled
* to wasm, an emscripten module with virgl.wasm next to it, both next to
* this module. Imported only when `gl` asks for it.
*/
export const bundledGlUrl: URL = new URL("./virgl.mjs", import.meta.url);
/**
* The guest Vulkan renderer's SPIR-V to WGSL translator
* (createKernel({ vk: true })), next to this module. Fetched only at the
* guest's first Vulkan instance.
*/
export const bundledVkShaderUrl: URL = new URL("./kwvk-naga.wasm", import.meta.url);
const platform = webPlatform(bundledKernelUrl, bundledGlUrl, {
shaderWasm: bundledVkShaderUrl,
load: async () => {
const [host, shader] = await Promise.all([import("./core/vk-host.ts"), import("./core/vk-shader.ts")]);
return { VkRenderer: host.VkRenderer, loadVkShader: shader.loadVkShader as (wasm: never) => Promise<unknown> };
},
});
/**
* Load, compile and boot a kernel (default: the bundled one, which boots
* `distro`, or without one the base shell) 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);
}