node-app-sdk-rust
Status:
1.0.0-experimental— API surface is frozen pending validation by at least two first-party packages. Breaking changes between1.0.0-experimental.*releases are possible. Pin to an exact version in production.
Rust SDK for building Node-App native plugins as shared libraries (.so / .dylib / .dll). Node-Apps run inside the Node Lightning-powered marketplace daemon and communicate with the host through a stable C ABI.
What it gives you
- A
NodeApptrait — implement it on your plugin type to get HTTP, event, and capability handling. - The
declare_node_app!macro — generates all FFI boilerplate (vtable, panic guards, JSON serialization) from your trait impl. - Helpers for talking to the host:
log_info!,invoke_capability,publish_event,get_config,get_storage,set_storage. - Distributed-trace propagation across capability invocations (thread-local span context).
- Hard limits matching the host:
MAX_CAPABILITY_RESPONSE_SIZE(16 MiB),MAX_EVENT_NAME_LEN(256 B),MAX_EVENT_DATA_LEN(64 KiB).
Quick start
Add to your Cargo.toml:
[]
= "my-node-app"
= "0.1.0"
= "2021"
[]
= ["cdylib"] # required: host loads via libloading
[]
= "1.0.0-experimental"
= "1.0"
src/lib.rs:
use *;
;
declare_node_app!;
Build:
# Output: target/release/libmy_node_app.so (Linux), .dylib (macOS), .dll (Windows)
Drop the resulting shared library into the host's app directory alongside a manifest.json. The host discovers it on startup or via app.discover.
Calling host capabilities
use ;
let response = invoke_capability?;
Trace context (trace_id / span_id / trace_depth) is propagated automatically when invoking from inside handle_capability — you don't need to thread it manually.
Publishing events
use publish_event;
publish_event?;
Event names must be namespaced with your app name (my-app.*); the host rejects un-namespaced events.
Threads your app starts (ADR-314)
Your app runs inside the node process, so every thread you start is a thread of
the node, and nothing separates it from the node's own threads by default. The
host exports two hooks — node_host_thread_bind_v1 / node_host_thread_unbind_v1 —
that register a thread with the app for its whole life, which is what makes the
app's memory and CPU figures complete. Threads that never bind are reported as
unattributed: their cost lands on "the node" and your app looks cheaper than
it is.
Start threads through the SDK and they bind themselves.
std threads
let handle = spawn?;
# Ok::
For a name or a custom stack (for example 512 KiB on a Pi Zero 2 W), use
thread::Builder — the same knobs as std::thread::Builder, with the
bind/unbind wrapped around the closure. thread::spawn is
Builder::new().spawn(…).
use Builder;
let handle = new
.name
.stack_size
.spawn?;
# Ok::
tokio
= { = "1.0.0-experimental", = ["tokio"] }
use Builder;
let runtime = new_multi_thread
.worker_threads
.thread_name
.enable_all
.build?;
# Ok::
Every worker thread and every blocking thread binds on the way in and unbinds on
the way out. on_thread_start / on_thread_stop still run your callbacks, and
they run charged to the app (bind first, unbind last).
Both helpers resolve the hooks once with dlsym. On an older host that does not
export them there is nothing to resolve: the closure still runs and nothing else
happens, so the same app binary works on old and new hosts. Threads you start
with plain std::thread::spawn, or with tokio::runtime::Builder directly,
keep working — they are simply not attributed to the app.
Shutdown
When the host stops the app, the SDK does these steps in sequence:
- It refuses new calls with error code
-11(ERROR_SHUTTING_DOWN). - It calls
NodeApp::begin_shutdown(&self)at once, while earlier calls can still run. - It calls
NodeApp::shutdown(&mut self)after all in-flight calls return.
Override begin_shutdown to tell slow work to stop (cancel a token, set a flag). Keep it short and do not block in it. See ADR-083.
ABI stability
This SDK targets Node Host API v1. The C ABI is defined by node-app-api (re-exported here) and the canonical C header lives at core/host-abi-v1/include/node-host-api-v1.h in the host repo. Apps declare their API version in NodeAppMetadata.api_version; the host refuses to load apps with mismatched versions.
License
Licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.