@tinyactors/scxmljs
v0.6.0
Published
A correctness-first SCXML 1.0 interpreter (ECMAScript data model) for the browser and Node, with custom elements for interactive statechart diagrams.
Maintainers
Readme
@tinyactors/scxmljs
Run SCXML statecharts in the browser, in Node and in Bun, and show them
running. The interpreter implements SCXML 1.0 with the ECMAScript data model and passes all 160
automatic mandatory W3C conformance tests. Two custom elements draw running charts: <scxml-view>
shows a whole chart and needs no JavaScript, and <scxml-explorer> explores large, multi-machine
systems one level at a time. They are plain custom elements, so they work with any framework.
Full-resolution video (WebM, 2880×1800) · High-resolution screenshot (PNG) · the explorer with the optional Tinyactors theme
Docs & demos: https://scxmljs.tinyactors.dev: guides, live demos and the API reference.
Install
npm install @tinyactors/scxmljsNode 22.3 or later, Bun, or any current browser. The package is ESM only.
Quick start
A chart, traffic-light.scxml:
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" datamodel="ecmascript" name="traffic-light" initial="on">
<datamodel>
<data id="cycles" expr="0"/>
</datamodel>
<state id="on" initial="red">
<transition event="power.off" target="off"/>
<state id="red">
<onentry><send id="timer" event="timer" delay="3s"/></onentry>
<onexit><cancel sendid="timer"/></onexit>
<transition event="timer" target="green"/>
</state>
<state id="green">
<onentry><send id="timer" event="timer" delay="3s"/></onentry>
<onexit><cancel sendid="timer"/></onexit>
<transition event="timer" target="yellow"/>
</state>
<state id="yellow">
<onentry><send id="timer" event="timer" delay="1s"/></onentry>
<onexit><cancel sendid="timer"/></onexit>
<transition event="timer" target="red">
<assign location="cycles" expr="cycles + 1"/>
</transition>
</state>
</state>
<final id="off">
<donedata><param name="cycles" expr="cycles"/></donedata>
</final>
</scxml>Draw it and run it, with no JavaScript of your own. Import the element once (through your bundler, or an import map):
<script type="module">import "@tinyactors/scxmljs/view";</script>
<scxml-view src="traffic-light.scxml"></scxml-view>The element loads, validates, runs and draws the chart. Active states are highlighted, and each transition label is a button that sends its event.

Explore a running system with <scxml-explorer>: a tree of states, a focus on one level of
the chart (each state with what it does, how it leaves, and buttons to send its events while
it's active), the events the machine accepts right now, and playback controls.
import { createSession, PlaybackClock } from "@tinyactors/scxmljs/trusted";
import "@tinyactors/scxmljs/explorer";
const source = await (await fetch("traffic-light.scxml")).text();
const clock = new PlaybackClock({ speed: 0.5 }); // adds pause, step and speed controls
const session = await createSession(source, { clock });
document.querySelector("scxml-explorer")!.attach({ session, clock });
session.start();Run it headless, for example in Node or in tests:
import { readFile } from "node:fs/promises";
import { createSession } from "@tinyactors/scxmljs";
const source = await readFile("traffic-light.scxml", "utf8");
const session = await createSession(source); // outside browsers, the library parses the XML itself
session.start();
console.log(session.activeStateIds().join(" "));
session.send("timer");
await session.settled();
console.log(session.activeStateIds().join(" "));
session.send("power.off");
console.log(JSON.stringify(await session.done));
session.dispose();on red
on green
{"cycles":0}The getting-started guide walks through all three.
Entry points
| Import | What | Size (min + gzip) |
|---|---|---|
| @tinyactors/scxmljs | the interpreter, sandboxed: chart code runs in QuickJS (WebAssembly) | 25 KB, then 290 KB loaded on demand |
| @tinyactors/scxmljs/trusted | the same API, trusted: chart code runs in the host's own JavaScript engine | 23 KB |
| @tinyactors/scxmljs/view | <scxml-view>: a whole chart, drawn and running, zero-JS | 57 KB, plus the engine it loads |
| @tinyactors/scxmljs/explorer | <scxml-explorer> and its view-model: explore a running system | 30 KB |
| @tinyactors/scxmljs/data | <scxml-data>: show, edit and compose a value an XML Schema describes (the view's event forms) | 9 KB |
| @tinyactors/scxmljs/themes/tinyactors.css | an optional theme stylesheet | 0.3 KB |
Sizes are for a browser bundle of each entry on its own (mise run size in the repository).
Sandboxed or trusted?
Both interpreter entry points have the same API and pass the same conformance suite. They differ
in where the chart's ECMAScript (<script>, expr, cond) runs:
- Sandboxed (
@tinyactors/scxmljs, the default): one QuickJS context per session. The chart can't see the page or other sessions, runaway scripts are interrupted, and memory is capped. Use it for charts that users write, paste or upload. - Trusted (
@tinyactors/scxmljs/trusted): a separate realm of the host engine per session (a hidden iframe in browsers, anode:vmcontext in Node and Bun). No WebAssembly to download, but the chart can reach the page and a loop can't be interrupted. Use it for charts you wrote.
Details: sandboxed vs trusted and SECURITY.md.
Documentation
- Getting started
- Elements:
<scxml-view>,<scxml-explorer>, event data and<scxml-data>, theming, custom UI and translations, large charts, frameworks - Interpreter: sandboxed vs trusted, driving charts from the page, custom I/O processors, custom invokers, playback and stepping, testing, Node and servers
- Shipping: bundling, CDNs and classic scripts, Content-Security-Policy
- Reference: conformance report, deviations from the spec, measurements, API reference
Support
| | Status | |---|---| | Chrome / Edge (Chromium) | tested in CI (Chromium 153) | | Firefox | tested in CI (Firefox 155) | | Safari | tested in CI with WebKit 26.6 (Safari's engine, not Safari itself) | | Node | 22.3 or later; tested in CI on 26.10, by hand on 22.3 | | Bun | tested in CI (1.4) | | Deno | tested in CI (2.9) |
The support table lists what each environment needs.
Stability
The package is at 0.x. Breaking changes can happen in minor versions until 1.0, and every one is
listed in the changelog. Everything the entry points export is public API; anything
else (and anything marked @internal) is not.
Contributions
This project doesn't accept outside contributions. Please report security problems as described in SECURITY.md.
License
MIT. QuickJS and quickjs-emscripten are MIT-licensed too; see THIRD_PARTY_NOTICES.

