Files
unkernel/js/web.ts

99 lines
5.7 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_FENCE, DATA_DISK_NAME, DATA_DISK_VERSION, DataDiskSpaceError, IdbStore, clearMovedDataDisk, dataDiskInfo, dataDiskStore, fenceDataDisk, importDataDisk, migrateDataDisk, openDataDisk, openDistroDisk, openHttpDisk, resetDataDisk, resetHttpDisk } from "./web/disk.ts";
export type { ClearMovedDataDiskOptions, DataDiskBackend, DataDiskInfo, DataDiskNameOptions, HttpDiskOptions, IdbStoreOptions, MigrateDataDiskOptions, OpenDataDiskOptions } from "./web/disk.ts";
export { OpfsUnavailableError, opfsAvailable, opfsBlocker, opfsDiskBlocker } from "./web/opfs.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 { createAudioIn } from "./web/audio-in.ts";
export type { AudioIn, AudioInOptions, AudioInState, AudioInStats } from "./web/audio-in.ts";
export { createCamera } from "./web/camera.ts";
export type { Camera, CameraInStats, CameraOptions, CameraState } from "./web/camera.ts";
export type { CameraColour, CameraEncoderStats } from "./web/camera-encoder.ts";
export { requestUserMedia, stopUserMedia } from "./web/user-media.ts";
export type { UserMediaResult } from "./web/user-media.ts";
export { webCpuBlocker } from "./web/cpu.ts";
export { serveWasiPrograms } from "./web/wasi.ts";
export { WebSerialBackend, serialPortLabel, webSerial, webSerialBackend, webSerialBlocker } from "./web/serial.ts";
export type { WebSerial, WebSerialBackendOptions, WebSerialPort } from "./web/serial.ts";
export { WEB_BLUETOOTH_BLOCKLIST, WEB_BLUETOOTH_SERVICES, WebBluetoothCentral, gattErrorFromDom, webBluetooth, webBluetoothAvailable, webBluetoothBlocker } from "./web/bluetooth.ts";
export type { WebBluetooth, WebBluetoothCentralOptions, WebBluetoothDevice } from "./web/bluetooth.ts";
export { WebUsbBackend, openWebUsbByKey, usbDeviceLabel, webUsb, webUsbBackend, webUsbBlocker, webUsbKey } from "./web/usb.ts";
export type { WebUsb, WebUsbDevice } from "./web/usb.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 next to this module. Imported only when
* `gl` asks for it.
*/
export const bundledGlUrl: URL = new URL("./virgl.mjs", import.meta.url);
/**
* The GL renderer's wasm (virgl.wasm), next to this module. Named here, not
* only by virgl.mjs, so that bundlers emit it too (they copy virgl.mjs as an
* asset and do not look inside it); the SDK hands it to the module.
*/
export const bundledGlWasmUrl: URL = new URL("./virgl.wasm", 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, { module: bundledGlUrl, wasm: bundledGlWasmUrl }, {
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);
}