Expand description
§xmachines-machine-node
Typed Node.js native addons (napi-rs) for one machine each: the machine’s guards, actions, delays and invoked actors are Rust compiled into the addon (no call into JavaScript per guard), and its events are checked against the machine’s generated types at the boundary. It is the Node counterpart of xmachines-machine-wasi, and like every Node-API addon, Bun and Deno load it too.
§When to use it
Use it when a machine and its implementations are known at build time and JavaScript should only drive actors: the addon runs on the JavaScript thread and never calls into JavaScript. For machines loaded at run time, with implementations in JavaScript, use the generic addon of xmachines-runtime-node.
§The API
The addon’s API is the typed world xmachines:machine (wit/machine.wit of xmachines-machine-wasi) in camelCase:
function machineInterface(): string // "xmachines:machine@0.2.0"
namespace info {
function id(): string
function version(): string | null
function definition(): string
function schemas(): string
}
class Actor {
static start(options: string, now: number): Actor
static restore(options: string, snapshot: string): Actor
send(event: string, now: number): void
sendTo(address: string, event: string, now: number): void
advance(now: number): void
nextDeadline(): number | null
snapshot(): string
persist(): string
takeEmitted(): string[]
observeSystemEmitted(): void
unobserveSystemEmitted(): void
takeSystemEmitted(): string[]
takeLogs(): string[]
pendingEffects(): string[]
resolveEffect(id: string, event?: string | null): void
stop(): void
}An implementation that panics fails the call that ran it with the panic’s message, and the actor is unusable afterwards, since the step it interrupted is half done.
§Example
A machine crate implements xmachines_machine::TypedMachine (the same implementation a WASI component of the machine uses) and calls export_machine! in a cdylib crate with napi_build::setup() in its build script:
use serde_json::Value;
use xmachines_machine::TypedMachine;
use xmachines_runtime::{Machine, Registry};
pub struct Toggle;
impl TypedMachine for Toggle {
type Context = Value;
type Event = Value;
const DEFINITION: &'static str = r#"{
"id": "toggle", "initial": "off",
"states": { "off": { "on": { "toggle": { "target": "on" } } }, "on": {} }
}"#;
const SCHEMAS: &'static str = "{}";
fn machine() -> Result<Machine<Value, Value>, String> {
Machine::from_json_str(Self::DEFINITION, &Registry::new()).map_err(|e| e.to_string())
}
}
xmachines_machine_node::export_machine!(Toggle);
// A cdylib has no `main`; this one lets the example build as a test.
fn main() {}With codegen, the implementations are the generated trait’s, as in examples/checkout-node, which exports the TypedMachine of examples/checkout-wasi. From JavaScript:
const addon = { exports: {} };
process.dlopen(addon, './target/release/libcheckout_node.so');
const { Actor, info, machineInterface } = addon.exports;
console.log(machineInterface(), info.id()); // xmachines:machine@0.2.0 checkout
const actor = Actor.start('{}', 0);
actor.send(JSON.stringify({ type: 'checkout', total: 120 }), 10);
console.log(JSON.parse(actor.snapshot()).value); // paying§Notes
- The machine crate needs no
napiornapi-deriveof its own: the macro brings them from this crate. It needsnapi-buildin its build script, as every napi-rs addon does, for the link arguments of its platform (-undefined dynamic_lookupon macOS,-z nodeletewith glibc). - The macro puts the addon’s items in a module of its own, so they do not clash with the crate’s. An addon exports one machine (its
Actorandinfoare the addon’s), andxmachines-runtime-node’s generic API has the sameActorname, so the two do not share an addon.
§Contract
The addon is the Node binding of the WIT package xmachines:machine@0.2.0, which machineInterface() reports (MACHINE_INTERFACE), with the time in milliseconds as a number and values as the JSON text the package’s JSON Schemas describe.
§Pins
napi-rs 3 (napi with Node-API version 8, napi-derive 3).
§Related crates
xmachines-machine: the contract of a typed machine.xmachines-machine-wasi: the same machine as a WASI component.xmachines-build: the generated module a machine crate implements.
§License
MIT
Macros§
- export_
machine - Exports the addon of a
TypedMachine:machineInterface, theinfonamespace and theActorclass, in a module of their own.
Structs§
- Node
Actor - The actor behind an addon’s
Actorclass: aJsonActorwith the JavaScript side’s numbers and errors, retired by a panic.
Constants§
- MACHINE_
INTERFACE - The interface a machine’s addon implements: the WIT package of the typed WASI component, whose Node binding the addon is.