node-app-sdk-rust 7.7.3

Rust SDK for building Node-App native plugins (cdylib) — Lightning-powered marketplace nodes
Documentation

node-app-sdk-rust

Status: 1.0.0-experimental — API surface is frozen pending validation by at least two first-party packages. Breaking changes between 1.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 NodeApp trait — 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:

[package]
name = "my-node-app"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]      # required: host loads via libloading

[dependencies]
node-app-sdk-rust = "1.0.0-experimental"
serde_json = "1.0"

src/lib.rs:

use node_app_sdk_rust::*;

#[derive(Default)]
pub struct MyApp;

impl NodeApp for MyApp {
    fn metadata() -> NodeAppInfo {
        NodeAppInfo {
            name: "my-app".into(),
            version: "0.1.0".into(),
            author: "Me".into(),
            description: "Hello-world Node-App".into(),
            capabilities: vec!["http_handler".into()],
        }
    }

    fn handle_request(&self, _req: AppRequest) -> Result<AppResponse, NodeAppError> {
        Ok(AppResponse {
            status: 200,
            headers: Default::default(),
            body: serde_json::json!({ "hello": "world" }),
        })
    }
}

declare_node_app!(MyApp);

Build:

cargo build --release
# 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 node_app_sdk_rust::{invoke_capability, CapabilityRequest};

let response = invoke_capability(&CapabilityRequest {
    id: uuid::Uuid::new_v4().to_string(),
    capability: "core.storage.get".into(),
    payload: serde_json::json!({ "key": "user_pref" }),
    caller_node_id: None,
    trace_id: None,
    span_id: None,
    parent_span_id: None,
    trace_depth: None,
})?;

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 node_app_sdk_rust::publish_event;

publish_event(
    "my-app.something_happened",
    &serde_json::json!({ "user_id": 42 }),
)?;

Event names must be namespaced with your app name (my-app.*); the host rejects un-namespaced events.

Replicated collections

A list a stage keeps a device-side copy of (ui.data.collections) is served by a <app>.<collection>.pull capability. Serve it with the separate crate node-app-replica (sdk/replica), which keeps the versions, epoch and tombstones in your own database and answers the node's pull; it does not depend on this SDK.

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 = node_app_sdk_rust::thread::spawn(|| {
    // …runs charged to this app…
})?;
# Ok::<(), std::io::Error>(())

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 node_app_sdk_rust::thread::Builder;

let handle = Builder::new()
    .name("my-app-worker".into())
    .stack_size(512 * 1024)
    .spawn(|| {
        // …long-lived work charged to this app…
    })?;
# Ok::<(), std::io::Error>(())

tokio

node-app-sdk-rust = { version = "1.0.0-experimental", features = ["tokio"] }
use node_app_sdk_rust::runtime::Builder;

let runtime = Builder::new_multi_thread()
    .worker_threads(4)
    .thread_name("my-app")
    .enable_all()
    .build()?;
# Ok::<(), std::io::Error>(())

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:

  1. It refuses new calls with error code -11 (ERROR_SHUTTING_DOWN).
  2. It calls NodeApp::begin_shutdown(&self) at once, while earlier calls can still run.
  3. 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.