Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// createKernel()'s Node platform: file paths and file: URLs are read from// disk (a distro path becomes a read-only "file" spec), disk and network// specs are built by nodeDevices (devices.ts), and// worker mode runs on worker_threads (worker.ts). The bundled// kernel's URL is passed in by the entry (node.ts) so that// `new URL(..., import.meta.url)` stays in the entry module itself (a shared// chunk lives in dist/_chunks/, which would resolve next to the chunk).
import { readFile } from "node:fs/promises";import { setFlagsFromString } from "node:v8";import { resolve } from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";import type { DiskSpec } from "../core/devices.ts";import type { Platform } from "../core/instance.ts";import { BootError } from "../core/load.ts";import { nodeDevices } from "./devices.ts";import { loadTailscale } from "../core/tailscale-load.ts";import { spawnNodeCpu } from "./cpu.ts";import { spawnNodeSampler } from "./sampler.ts";import { spawnNodeWorker } from "./worker.ts";
/** A file: URL, or a string without a URL scheme (a path). */function localPath(src: URL | string): string | undefined { if (src instanceof URL) return src.protocol === "file:" ? fileURLToPath(src) : undefined; if (src.startsWith("file:")) return fileURLToPath(src); return /^[a-z][a-z0-9+.-]+:/i.test(src) ? undefined : src;}
/** Platform.readFile for Node: file: URLs and paths from disk, anything else fetched. */export function nodeReadFile(bundledPath?: string): NonNullable<Platform["readFile"]> { return (src) => { const path = localPath(src); if (path === undefined) return undefined; return readFile(path).then((b) => new Uint8Array(b.buffer, b.byteOffset, b.byteLength), (e: NodeJS.ErrnoException) => { const hint = e.code === "ENOENT" && path === bundledPath ? " (the bundled kernel is missing; in a source checkout, build it with kernel/build.sh base)" : ""; throw new BootError("download", `cannot read ${path}: ${e.message}${hint}`, { cause: e }); }); };}
/** A distro path, file: URL or http(s) URL as a read-only disk spec. */export function nodeDistroSpec(src: string | URL): DiskSpec { const path = localPath(src); if (path !== undefined) return { kind: "file", path, readOnly: true }; const url = String(src); if (!/^https?:/i.test(url)) throw new BootError("boot", `distro: ${url}: not a file path or an http(s) URL`); return { kind: "http", url, readOnly: true };}
// V8 reads a module's compilationHints section only with this flag; kwemu's// JIT host adds one to modules that proved hot (js/core/jit.ts, tierUp), so// that they run TurboFan code at once instead of Liftoff until a background// tier-up. V8's flags are the process's: the workers (the kernel's, each// CPU's) see it too. Other engines ignore the section.setFlagsFromString("--experimental-wasm-compilation-hints");
export function nodePlatform(bundledKernel: URL): Platform { return { defaultKernel: () => bundledKernel, readFile: nodeReadFile(fileURLToPath(bundledKernel)), spawnWorker: spawnNodeWorker, spawnCpu: spawnNodeCpu, spawnSampler: spawnNodeSampler, absoluteUrl: (src) => pathToFileURL(resolve(src)).href, devices: nodeDevices, distroSpec: nodeDistroSpec, // net.tailscale: the client is a chunk of its own, imported only then. tailscale: loadTailscale, };}