pi-mono/packages/chord/test/examples/41-workers/worker.ts
Mario Zechner 7ac1a9e3a0 feat(chord): split the worker base at dynamic imports; workers/child entry
buildWorkerBase bundles with esbuild code splitting into one private directory: the entry
and its static chunks load at each fork, a lazily imported module and the packages it
imports only when imported. @earendil-works/chord/workers/child exports the worker's side
(workerMain, processLock) without esbuild.
2026-11-05 08:49:17 +01:00

22 lines
947 B
TypeScript

// The worker process: a host at the session's address, connected to the server, loading the plugins it was shipped.
import { BACKGROUND_CONTEXT } from "../../../src/context/index.ts";
import { createHost } from "../../../src/host/index.ts";
import * as chord from "../../../src/index.ts";
import { workerMain } from "../../../src/node/workers/child.ts";
workerMain(
async ({ address, parent, plugins, onClose }, context) => {
const host = createHost({ address, principal: { kind: "worker" }, onError: console.error });
onClose(() => host.close(BACKGROUND_CONTEXT));
// Connect first: the server sees the worker started; then load.
host.connect(parent, { principal: { kind: "server" }, trusted: true }, context);
await plugins.load(host, { kind: "worker", core: [] }, context);
return {};
},
{
hostPackages: { "@earendil-works/chord": chord },
redact: (error) => ({ message: String(error) }),
onError: console.error,
},
);