Skip to main content

node_device_session/
lib.rs

1//! Shared device-session server (C-5): transport-neutral `/econ-v1/device-session/1`.
2//!
3//! Carriers implement [`SessionStreams`] + [`HostBus`] and call [`serve`].
4//! Pass [`WebRtcSignaling`] when the carrier can answer `webrtc.signal/1`
5//! (iroh); otherwise that method is refused with `unsupported_transport`.
6//! Iroh-specific pieces (QUIC `ProtocolHandler`, WebRTC media path, session
7//! registry/push) stay in `node-app-iroh`.
8
9#![deny(missing_docs)]
10
11mod messages;
12mod serve;
13mod stream_wire;
14mod traits;
15mod tunnel;
16mod types;
17mod wire;
18
19pub use messages::{OpenMode, OpenOk};
20pub use serve::serve;
21pub use traits::{BoxRecv, BoxSend, HostBus, SessionStreams, WebRtcSignaling};
22pub use types::{
23    IngressSocket, Refusal, Session, SessionEnd, SessionPath, SESSION_HEADER, PATH_HEADER,
24};
25pub use wire::{
26    decode_request, decode_response, encode_request, encode_response, peek_method, FrameError,
27    ResponseError, ALPN, DEVICE_CALL_METHOD, HTTP_REQUEST_METHOD, MAX_FRAME_BYTES,
28    RESPONSE_STATUS_ERR, RESPONSE_STATUS_OK, SESSION_OPEN_METHOD, SESSION_RENEW_METHOD,
29    WEBRTC_SIGNAL_METHOD, WS_STREAM_METHOD,
30};
31
32/// Capability names the host bus is expected to map.
33pub mod caps {
34    /// `core.device_session.verify`
35    pub const VERIFY: &str = "core.device_session.verify";
36    /// `core.device_session.renew`
37    pub const RENEW: &str = "core.device_session.renew";
38    /// `core.device_session.admit_renewal`
39    pub const ADMIT_RENEWAL: &str = "core.device_session.admit_renewal";
40}
41
42/// Encode an `http.request/1` body for tests and carriers.
43pub fn encode_http_tunnel_request(
44    method: &str,
45    path: &str,
46    headers: std::collections::HashMap<String, String>,
47    body: &[u8],
48) -> Vec<u8> {
49    use base64::{engine::general_purpose::STANDARD, Engine as _};
50    let request = stream_wire::StreamRequest {
51        method: method.into(),
52        path: path.into(),
53        headers,
54        body_b64: STANDARD.encode(body),
55    };
56    stream_wire::encode_request(&request).expect("http tunnel request encodes")
57}