@invinite-org/chartlang-host-worker
v1.4.2
Published
Web Worker ScriptHost for the browser
Readme
@invinite-org/chartlang-host-worker
experimental
Browser-default ScriptHost for chartlang. Boots a Web Worker, loads a
compiled script bundle, and round-trips CandleEvent / RunnerEmissions
across a structured-clone-safe postMessage protocol.
Install
pnpm add @invinite-org/chartlang-host-workerPublic surface
createWorkerHost(opts) → ScriptHost— main-side factory; the canvas2d reference adapter (Task 10) consumes the returned host.DEFAULT_LIMITS— Phase-1HostLimitsdefaults (maxCpuMsPerStep: 50,maxHeapBytes: 64 MiB,maxRingBufferBars: 5000,maxLoadTimeoutMs: 30000).idbStateStore(opts)from@invinite-org/chartlang-host-worker/idb— IndexedDBPersistentStateStorefor browser warm starts.- Persists one snapshot per
StateStoreKey; default cap is 50 MiB. - Oldest snapshots are evicted first when writes exceed the cap.
@invinite-org/chartlang-host-worker/worker-boot— bundled worker entry for Vite / bundlernew Worker(new URL(...))patterns. Side-effect only.- Types:
ScriptHost,HostLimits,WorkerLike,HostCompiledScript,HostToWorker,WorkerToHost,CreateWorkerHostOpts.
Minimum-viable API call
import { createWorkerHost } from "@invinite-org/chartlang-host-worker";
// host: ScriptHost — passed to an adapter at construction time.Vite worker URL
const url = new URL(
"@invinite-org/chartlang-host-worker/worker-boot",
import.meta.url,
);
const worker = new Worker(url, { type: "module" });
const host = createWorkerHost({ capabilities, workerLike: worker });Stability
Phase 1 ships the postMessage wire protocol + a measurement-based CPU
watchdog (step-overshoot reports observed elapsed ms; no preemption).
Deferred to Phase 5 (per PLAN §19):
- Worker-side CSP enforcement.
- Hard heap caps (
maxHeapBytesis advisory today — no browser API exposes a reliable per-worker heap limit). - Real preemption via
setInterruptHandler(lands with the QuickJS host). - Fingerprint-only globals (
Math.random,Date.now, …) — Phase 1 relies on the compiler'sforbiddenConstructspass to scrub these from the bundle before it ever reaches a worker.
Docs
See docs/hosts/worker.md.
License
MIT
