Files
unkernel/js/node/worker.ts

28 lines
1.0 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// Start the kernel worker (worker-entry.ts) on a worker_threads Worker, for
// createKernel({ worker: true }) (core/worker-host.ts).
//
// From source, Node starts the worker from worker-entry.ts itself. `pnpm
// build` (build.config.ts) bundles worker-entry.ts into one minified ES
// module and replaces the URL of worker-entry.ts below (the expression
// must appear exactly once in this file) with a data: URL of that code,
// so the published package has no worker file to find and nothing for a
// downstream bundler to resolve.
import { Worker } from "node:worker_threads";
import type { WorkerEndpoint } from "../core/worker-protocol.ts";
export function spawnNodeWorker(): WorkerEndpoint {
const w = new Worker(new URL("./worker-entry.ts", import.meta.url));
return {
post: (msg, transfer) => w.postMessage(msg, transfer),
listen(h) {
w.on("message", h.message);
w.on("error", h.error);
w.on("exit", h.exit);
},
terminate: () => void w.terminate(),
};
}