mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 18:09:29 +08:00
js: read file: URLs and paths in the Node entry's loadKernelModule() and loadBytes() (#139)
This commit is contained in:
17
js/node.ts
17
js/node.ts
@@ -18,6 +18,7 @@
|
||||
// URLs below stay relative to dist/node.mjs.
|
||||
|
||||
import { createKernelOn, type CreateKernelOptions, type KernelHandle, type KernelInstance, type WorkerOption } from "./core/instance.ts";
|
||||
import { loadBytes as coreLoadBytes, loadKernelModule as coreLoadKernelModule, type BytesSource, type KernelSource, type LoadOptions } from "./core/load.ts";
|
||||
import type { WorkerKernel } from "./core/worker-host.ts";
|
||||
import { distroFetcher, type FetchDistroOptions } from "./node/distro-fetch.ts";
|
||||
import { nodePlatform } from "./node/platform.ts";
|
||||
@@ -41,6 +42,22 @@ export const bundledKernelUrl: URL = new URL("./unkernel.wasm", import.meta.url)
|
||||
|
||||
const platform = nodePlatform(bundledKernelUrl);
|
||||
|
||||
/**
|
||||
* Compile a kernel from any KernelSource, as createKernel() does: file
|
||||
* paths and file: URLs (such as bundledKernelUrl) are read from disk,
|
||||
* other URLs fetched. The Module can be passed to any number of
|
||||
* createKernel({ kernel }) calls; `smp: true` gives the SMP kernel (for
|
||||
* `cpus` > 1) from a bundle that carries its patch.
|
||||
*/
|
||||
export function loadKernelModule(src: KernelSource, opts: LoadOptions = {}): Promise<WebAssembly.Module> {
|
||||
return coreLoadKernelModule(src, { readFile: platform.readFile, ...opts });
|
||||
}
|
||||
|
||||
/** Bytes of an initramfs (or any file) from any BytesSource; file paths and file: URLs are read from disk. */
|
||||
export function loadBytes(src: BytesSource, opts: LoadOptions = {}): Promise<Uint8Array> {
|
||||
return coreLoadBytes(src, { readFile: platform.readFile, ...opts });
|
||||
}
|
||||
|
||||
/**
|
||||
* The distro `name` ("alpine", "debian": the names in the release's
|
||||
* distros.json) as a local image file for createKernel({ distro }). In a
|
||||
|
||||
@@ -25,7 +25,7 @@ import { createServer } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { availableParallelism, tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { Worker } from "node:worker_threads";
|
||||
|
||||
const spec = process.env.UNKERNEL_DIST ? "unkernel" : "../js/node.ts";
|
||||
@@ -293,6 +293,26 @@ describe("api", { concurrency: CONCURRENCY }, () => {
|
||||
assert.equal(bytes.byteLength, readFileSync(file).byteLength, "the caller's bytes are not detached");
|
||||
});
|
||||
|
||||
test("loadKernelModule() and loadBytes() read file: URLs and paths from disk; one Module boots several kernels, inline and in a worker, the SMP one on 2 CPUs", async (t) => {
|
||||
const url = testKernel ? pathToFileURL(testKernel) : bundledKernelUrl;
|
||||
const up = await sdk.loadKernelModule(url);
|
||||
assert.ok(up instanceof WebAssembly.Module);
|
||||
assert.ok(await sdk.loadKernelModule(String(url)) instanceof WebAssembly.Module, "a file: URL string");
|
||||
assert.equal(Buffer.compare(await sdk.loadBytes(url), kernelBytes), 0);
|
||||
assert.equal(Buffer.compare(await sdk.loadBytes(path.relative(process.cwd(), kernelFile)), kernelBytes), 0, "a relative path");
|
||||
await assert.rejects(sdk.loadKernelModule("/nonexistent/kernel.wasm"),
|
||||
(e: Error) => e instanceof sdk.BootError && e.stage === "download" && /cannot read \/nonexistent/.test(e.message));
|
||||
const smp = hasSmpPatch ? await sdk.loadKernelModule(url, { smp: true }) : undefined;
|
||||
const runs = [{ kernel: up, cpus: 1, worker: false }, { kernel: up, cpus: 1, worker: false }, { kernel: up, cpus: 1, worker: true },
|
||||
...(smp ? [{ kernel: smp, cpus: 2, worker: false }] : [])];
|
||||
await Promise.all(runs.map(async (o) => {
|
||||
const vm = await boot(t, { ...o, log: () => {} });
|
||||
const { stdout, exitCode } = await vm.exec("nproc");
|
||||
assert.equal(exitCode, 0);
|
||||
assert.equal(stdout.trim(), String(o.cpus), `${o.cpus} CPU(s)${o.worker ? " in a worker" : ""}`);
|
||||
}));
|
||||
});
|
||||
|
||||
test("worker mode rejects device objects (naming the spec instead)", async (t) => {
|
||||
const disk = { size: 1 << 20, read: () => {} } as unknown as import("../js/node.ts").BlockDevice;
|
||||
await assert.rejects(createKernel({ worker: true, disks: [{ kind: "file", path: "/nonexistent" }, disk] }), (e: Error) =>
|
||||
|
||||
Reference in New Issue
Block a user