mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 00:39:40 +08:00
The guest joins a tailnet from Node or the page: ts2021 control over WebSocket, DERP relays, WireGuard and the packet filter in JS, loaded as a separate chunk only when the option is used. SDK net option, CLI --tailscale* flags and a page panel. Relay only; exit nodes, IPv6 and real peer traffic are not covered yet.
66 lines
2.9 KiB
TypeScript
66 lines
2.9 KiB
TypeScript
// 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 { 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 };
|
|
}
|
|
|
|
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,
|
|
};
|
|
}
|