node-device-session 7.6.216

Shared server for /econ-v1/device-session/1 behind transport-neutral traits (C-5)
Documentation
//! C-5 transport and host-bus traits.

use crate::messages::OpenOk;
use crate::types::{Refusal, Session};
use async_trait::async_trait;
use std::io;
use tokio::io::{AsyncRead, AsyncWrite};

/// Boxed write half of one bidirectional stream.
pub type BoxSend = Box<dyn AsyncWrite + Send + Unpin>;
/// Boxed read half of one bidirectional stream.
pub type BoxRecv = Box<dyn AsyncRead + Send + Unpin>;

/// One authenticated peer connection (handshake already proved [`Self::peer_key`]).
#[async_trait]
pub trait SessionStreams: Send + Sync {
    /// Ed25519 public key proven by the transport handshake.
    fn peer_key(&self) -> [u8; 32];

    /// Accept the next bidirectional stream.
    async fn accept_bi(&self) -> io::Result<(BoxSend, BoxRecv)>;

    /// Close the connection with an application code and reason.
    fn close(&self, code: u32, reason: &[u8]);

    /// Whether the peer has already closed.
    fn is_closed(&self) -> bool {
        false
    }
}

/// The app's bus to the host for device-session capabilities.
#[async_trait]
pub trait HostBus: Send + Sync {
    /// `core.device_session.verify` — full open with a credential.
    async fn verify(&self, credential: &str, endpoint_id: &str) -> Result<Session, Refusal>;

    /// `core.device_session.renew` — one-shot credential renewal, then close.
    async fn renew(&self, endpoint_id: &str) -> Result<Session, Refusal>;

    /// `core.device_session.admit_renewal` — open a renewal-only session.
    async fn admit_renewal(&self, endpoint_id: &str) -> Result<Session, Refusal>;

    /// Generic capability invoke for `device.call/1` (stamped payload already applied).
    async fn invoke(
        &self,
        capability: &str,
        payload: serde_json::Value,
    ) -> Result<serde_json::Value, Refusal>;
}

/// Carrier-owned WebRTC signaling for `webrtc.signal/1`.
///
/// The shared server has no carrier Connection type; when the carrier can
/// answer WebRTC (iroh today), it passes [`Some`] into [`crate::serve`] and
/// receives the signaling stream exactly as `node-app-iroh`'s device-session
/// server does. When [`None`], the server refuses with `unsupported_transport`.
#[async_trait]
pub trait WebRtcSignaling: Send + Sync {
    /// Handle one `webrtc.signal/1` stream on an opened session.
    ///
    /// `send`/`recv` already peeked the method prefix; the body (SDP / ICE
    /// frames) starts at the next byte. Own the halves until the signaling
    /// session ends.
    async fn handle(
        &self,
        identity: &OpenOk,
        peer: [u8; 32],
        send: BoxSend,
        recv: BoxRecv,
    );
}