Expand description
The nomux extension SDK for the daemon part of an extension.
The daemon part of an extension is a separate process on the Host. The nomux daemon (the host) starts the process when a client needs it and stops it when it is idle. The host and the extension exchange protobuf messages on the standard input and the standard output of the process.
An extension does these steps:
- It makes an
Extensionwith the identifier and the version of itsextension.toml. - It adds each method with
Extension::method. The argument and the result of a method are protobuf messages. - It calls
Extension::run. The function does the handshake, runs the methods that the host calls and returns when the host tells the extension to stop.
In a method, Call::host gives the Host. With it, the extension
emits events (Host::emit), calls host services (Host::call) and
reads the permissions that the user approved (Host::permissions).
§Example
An extension with one method and one event. Add prost to the
dependencies of the extension, with the same version as this crate uses:
the prost::Message derive macro needs it.
use nomux_extension::{Call, CallError, Extension, RunError};
/// The argument and the result of the `echo` method.
#[derive(Clone, PartialEq, prost::Message)]
struct Text {
#[prost(string, tag = "1")]
text: String,
}
/// The payload of the `echoed` event.
#[derive(Clone, PartialEq, prost::Message)]
struct Echoed {
#[prost(uint64, tag = "1")]
length: u64,
}
fn echo(call: &Call, input: Text) -> Result<Text, CallError> {
if input.text.is_empty() {
return Err(CallError::failed("the text is empty"));
}
let length = input.text.len() as u64;
call.host().emit("echoed", &Echoed { length })?;
Ok(input)
}
fn main() -> Result<(), RunError> {
Extension::new("nomux.echo", env!("CARGO_PKG_VERSION"))
.method("echo", echo)
.run()
}§Threads and the async runtime
The crate has no async runtime. An extension starts when a client needs
it and stops when it is idle, thus it must be small and start fast.
Extension::run blocks the thread that calls it and runs the methods on
that thread, one at a time. Thus a method does not need a lock for the
state of the extension.
One more thread reads the messages from the host. While a method runs,
that thread answers health requests and records cancellations. A method
that can run for a long time examines Call::is_cancelled between its
steps.
§Rules for an extension
- The standard output carries the protocol. Do not print to it. Write logs to the standard error.
- A panic in a method stops the process. The nomux daemon continues to run and reports the failure of the extension.
§Other parts of the crate
wire::v1: the messages of the protocol.prostgenerates these types from the schemas of theextension-apirepository.frame: the framing. A host uses it to speak with an extension.manifest(featuremanifest): the types and the validator ofextension.toml. An extension does not need it.
Versions earlier than 1.0.0 are pre-releases: interfaces can change between minor versions.
Re-exports§
pub use api_version::ApiRequirement;pub use api_version::ApiVersion;pub use api_version::InvalidApiRequirement;pub use wire::v1::ErrorCode;pub use wire::v1::Permissions;
Modules§
- frame
- The framing of the extension protocol.
- manifest
- The manifest of an extension package:
extension.toml. - wire
- The messages of the nomux extension protocol.
Structs§
- ApiRequirement
- The range of extension API versions that an extension can use.
- Call
- The context of one call. Each method gets it as its first argument.
- Call
Error - The failure of one call.
- Extension
- The daemon part of an extension.
- Host
- The nomux daemon that started the extension.
- Invalid
ApiRequirement - The text is not a caret requirement.
Enums§
- RunError
- The reason why
crate::Extension::runstopped before the host sent a shutdown request.