node-app-api 5.20.0

Shared types and C ABI definitions for the Node-App host API v1
Documentation
//! Host context passed to native apps during initialization
//!
//! The NodeAppContext provides callback function pointers that apps
//! use to interact with the host (logging, config, storage, capabilities).

use std::ffi::c_char;
use std::os::raw::c_void;

use crate::ffi::FfiResult;

/// Host context provided to native apps.
///
/// Contains function pointers for callbacks into the host.
/// The context pointer is valid for the lifetime of the app.
#[repr(C)]
pub struct NodeAppContext {
    /// Opaque pointer to host-side state (passed as first arg to callbacks)
    pub host_data: *const c_void,
    /// Log a message at the specified level.
    /// `level`: 0=trace, 1=debug, 2=info, 3=warn, 4=error
    /// `message`: null-terminated UTF-8 string
    pub host_log: unsafe extern "C" fn(host_data: *const c_void, level: u32, message: *const c_char),
    /// Get a configuration value by key.
    /// Returns null if key not found. Caller must NOT free the returned pointer.
    pub host_get_config:
        unsafe extern "C" fn(host_data: *const c_void, key: *const c_char) -> *const c_char,
    /// Set a storage value (scoped to this app).
    /// Both key and value are null-terminated UTF-8 strings.
    pub host_set_storage: unsafe extern "C" fn(
        host_data: *const c_void,
        key: *const c_char,
        value: *const c_char,
    ),
    /// Get a storage value by key (scoped to this app).
    /// Returns null if key not found. Caller must NOT free the returned pointer.
    pub host_get_storage:
        unsafe extern "C" fn(host_data: *const c_void, key: *const c_char) -> *const c_char,
    /// Invoke a capability on the host via the capability router.
    /// `request_json` is a pointer to UTF-8 JSON bytes (CapabilityRequest) of length `request_len`.
    /// Returns FfiResult with CapabilityResponse JSON in `data`/`data_len`.
    /// The caller must free the returned data using the host's free function.
    pub host_invoke_capability: unsafe extern "C" fn(
        host_data: *const c_void,
        request_json: *const u8,
        request_len: usize,
    ) -> FfiResult,
    /// Publish a domain event to the host event bus.
    /// `event_name` is a pointer to UTF-8 bytes of length `event_name_len` (max 256 bytes).
    /// `event_data` is a pointer to UTF-8 JSON bytes of length `event_data_len` (max 64KB).
    /// Returns 0 on success, negative error code on failure.
    /// Fire-and-forget semantics: the event is queued asynchronously.
    pub host_publish_event: unsafe extern "C" fn(
        host_data: *const c_void,
        event_name: *const u8,
        event_name_len: usize,
        event_data: *const u8,
        event_data_len: usize,
    ) -> i32,
}