xmachines-machine-node 1.1.0

Typed Node.js native addons (napi-rs) for one XState v6 machine each, with its guards and actions in Rust inside the addon
Documentation
# 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`](https://docs.rs/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`](https://docs.rs/xmachines-runtime-node).

## The API

The addon's API is the typed world `xmachines:machine` (`wit/machine.wit` of `xmachines-machine-wasi`) in camelCase:

```ts
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:

```rust,no_run
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`](https://gitlab.com/xmachin-es/xmachines-rs/-/tree/main/examples/checkout-node), which exports the `TypedMachine` of `examples/checkout-wasi`. From JavaScript:

```js
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 `napi` or `napi-derive` of its own: the macro brings them from this crate. It needs `napi-build` in its build script, as every napi-rs addon does, for the link arguments of its platform (`-undefined dynamic_lookup` on macOS, `-z nodelete` with 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 `Actor` and `info` are the addon's), and `xmachines-runtime-node`'s generic API has the same `Actor` name, 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`]https://docs.rs/xmachines-machine: the contract of a typed machine.
- [`xmachines-machine-wasi`]https://docs.rs/xmachines-machine-wasi: the same machine as a WASI component.
- [`xmachines-build`]https://docs.rs/xmachines-build: the generated module a machine crate implements.

## License

MIT