Skip to main content

Crate kkrpc

Crate kkrpc 

Source
Expand description

§kkrpc-interop

Rust client/server library for kkrpc JSON-mode interop.

This crate implements the kkrpc message protocol using JSON only, enabling cross-language RPC between Rust and TypeScript/JavaScript.

§Features

  • JSON-mode request/response compatible with kkrpc serialization.version = "json"
  • stdio and WebSocket transports with a shared Transport trait
  • Callback support using __callback__<id> tokens
  • Property access (get/set) for remote object manipulation
  • Thread-safe implementation using Arc and Mutex

§Quick Start

§Client Example

use kkrpc::{Client, StdioTransport, Arg};
use serde_json::json;
use std::process::{Command, Stdio};
use std::sync::Arc;

// Spawn a server process
let mut child = Command::new("bun")
    .arg("server.ts")
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn()
    .expect("spawn server");

let stdout = child.stdout.take().expect("stdout");
let stdin = child.stdin.take().expect("stdin");

// Create transport and client
let transport = StdioTransport::new(stdout, stdin);
let client = Arc::new(Client::new(Arc::new(transport)));

// Call remote method
let result = client.call(
    "math.add",
    vec![Arg::Value(json!(1)), Arg::Value(json!(2))]
).expect("call failed");

println!("Result: {}", result);

§Server Example

use kkrpc::{Server, RpcApi, StdioTransport, Arg};
use serde_json::Value;
use std::sync::Arc;

// Create API
let mut api = RpcApi::new();
api.register_method("math.add", Arc::new(|args: Vec<Arg>| {
    let a = match &args[0] {
        Arg::Value(v) => v.as_i64().unwrap_or(0),
        _ => 0,
    };
    let b = match &args[1] {
        Arg::Value(v) => v.as_i64().unwrap_or(0),
        _ => 0,
    };
    Value::from(a + b)
}));

// Start server
let transport = Arc::new(StdioTransport::new(
    std::io::stdin(),
    std::io::stdout()
));
let _server = Server::new(transport, api);

// Keep server running
loop {
    std::thread::park();
}

§Protocol

The kkrpc protocol uses JSON messages with the following structure:

§Request

{
  "id": "uuid",
  "method": "math.add",
  "args": [1, 2],
  "type": "request",
  "version": "json"
}

§Response

{
  "id": "uuid",
  "method": "",
  "args": {"result": 3},
  "type": "response",
  "version": "json"
}

§Property Get

{
  "id": "uuid",
  "path": ["settings", "theme"],
  "type": "get",
  "version": "json"
}

§Callback

Callbacks are encoded as __callback__<id> strings and invoked via:

{
  "id": "uuid",
  "method": "callback-id",
  "args": ["payload"],
  "type": "callback",
  "version": "json"
}

Structs§

Client
RPC client for making remote procedure calls.
RpcApi
API registry for the RPC server.
RpcError
Error type for RPC operations.
Server
RPC server that handles incoming requests.
StdioTransport
stdio-based transport implementation.
WebSocketTransport
WebSocket transport implementation.

Enums§

Arg
Argument type for RPC method calls.

Constants§

CALLBACK_PREFIX
Prefix for callback identifiers in the protocol.

Traits§

Transport
Transport layer abstraction for the RPC protocol.

Functions§

generate_uuid
Generate a UUID for request/callback identification.

Type Aliases§

Handler
Handler type for RPC methods.