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`] or
4//! [`serve_with_registry`]. Pass [`WebRtcSignaling`] when the carrier can
5//! answer `webrtc.signal/1`; otherwise that method is refused with
6//! `unsupported_transport`.
7//!
8//! The shared WebRTC answerer lives in [`webrtc`]: wire a [`webrtc::WebRtcAnswerer`]
9//! with a carrier-specific [`webrtc::WebRtcCarrier`] (STUN, portmap, clock,
10//! session bind). Iroh/p2p-only pieces (QUIC `ProtocolHandler`,
11//! gossip STUN discovery adapters) stay in the carrier apps. Live-session
12//! revalidation is shared here via [`SessionRegistry`].
13
14#![deny(missing_docs)]
15
16mod messages;
17mod registry;
18mod revalidate;
19mod serve;
20mod stream_wire;
21mod traits;
22mod tunnel;
23mod types;
24pub mod webrtc;
25mod wire;
26
27pub use messages::{OpenMode, OpenOk};
28pub use registry::{SessionClose, SessionRegistry, REVOKED_CLOSE_CODE, SUPERSEDED_CLOSE_CODE};
29pub use revalidate::{is_definitive_refusal_code, verify_refusal_code, RevalidateReport};
30pub use serve::{serve, serve_with_registry};
31pub use traits::{BoxRecv, BoxSend, HostBus, SessionStreams, WebRtcSignaling};
32pub use types::{
33    IngressSocket, Refusal, Session, SessionEnd, SessionPath, PATH_HEADER, SESSION_HEADER,
34};
35pub use webrtc::{
36    ByteBudget, MapRefusal, MappedSessionPort, NoSessionPortMapper, NullWebRtcCarrier,
37    SessionPortMapper, StaticWebRtcCarrier, WebRtcAnswerer, WebRtcCarrier, WebRtcHandle,
38    SESSION_BYTES,
39};
40pub use wire::{
41    decode_request, decode_response, encode_request, encode_response, peek_method, FrameError,
42    ResponseError, ALPN, DEVICE_CALL_METHOD, HTTP_REQUEST_METHOD, MAX_FRAME_BYTES,
43    RESPONSE_STATUS_ERR, RESPONSE_STATUS_OK, SESSION_OPEN_METHOD, SESSION_RENEW_METHOD,
44    WEBRTC_SIGNAL_METHOD, WS_STREAM_METHOD,
45};
46
47/// Capability names the host bus is expected to map.
48pub mod caps {
49    /// `core.device_session.verify`
50    pub const VERIFY: &str = "core.device_session.verify";
51    /// `core.device_session.renew`
52    pub const RENEW: &str = "core.device_session.renew";
53    /// `core.device_session.admit_renewal`
54    pub const ADMIT_RENEWAL: &str = "core.device_session.admit_renewal";
55}
56
57/// Encode an `http.request/1` body for tests and carriers.
58pub fn encode_http_tunnel_request(
59    method: &str,
60    path: &str,
61    headers: std::collections::HashMap<String, String>,
62    body: &[u8],
63) -> Vec<u8> {
64    use base64::{engine::general_purpose::STANDARD, Engine as _};
65    let request = stream_wire::StreamRequest {
66        method: method.into(),
67        path: path.into(),
68        headers,
69        body_b64: STANDARD.encode(body),
70    };
71    stream_wire::encode_request(&request).expect("http tunnel request encodes")
72}