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;
26mod wire_stream;
27
28pub use messages::{OpenMode, OpenOk};
29pub use registry::{SessionClose, SessionRegistry, REVOKED_CLOSE_CODE, SUPERSEDED_CLOSE_CODE};
30pub use revalidate::{is_definitive_refusal_code, verify_refusal_code, RevalidateReport};
31pub use serve::{serve, serve_with_options, serve_with_registry, ServeOptions};
32pub use traits::{BoxRecv, BoxSend, HostBus, SessionStreams, WebRtcSignaling};
33pub use types::{
34    IngressSocket, Refusal, Session, SessionEnd, SessionPath, WireSocket, PATH_HEADER,
35    SESSION_HEADER,
36};
37pub use webrtc::{
38    ByteBudget, MapRefusal, MappedSessionPort, NoSessionPortMapper, NullWebRtcCarrier,
39    SessionPortMapper, StaticWebRtcCarrier, WebRtcAnswerer, WebRtcCarrier, WebRtcHandle,
40    SESSION_BYTES,
41};
42pub use wire::{
43    decode_request, decode_response, encode_request, encode_response, peek_method, FrameError,
44    ResponseError, ALPN, DEVICE_CALL_METHOD, HTTP_REQUEST_METHOD, MAX_FRAME_BYTES,
45    RESPONSE_STATUS_ERR, RESPONSE_STATUS_OK, SESSION_OPEN_METHOD, SESSION_RENEW_METHOD,
46    WEBRTC_SIGNAL_METHOD, WIRE_METHOD, WS_STREAM_METHOD,
47};
48pub use wire_stream::{
49    preamble_line, splice_to_wire_socket, wire_socket_path, DEFAULT_WIRE_SOCKET, MAX_PREAMBLE,
50};
51
52/// Capability names the host bus is expected to map.
53pub mod caps {
54    /// `core.device_session.verify`
55    pub const VERIFY: &str = "core.device_session.verify";
56    /// `core.device_session.renew`
57    pub const RENEW: &str = "core.device_session.renew";
58    /// `core.device_session.admit_renewal`
59    pub const ADMIT_RENEWAL: &str = "core.device_session.admit_renewal";
60}
61
62/// Encode an `http.request/1` body for tests and carriers.
63pub fn encode_http_tunnel_request(
64    method: &str,
65    path: &str,
66    headers: std::collections::HashMap<String, String>,
67    body: &[u8],
68) -> Vec<u8> {
69    use base64::{engine::general_purpose::STANDARD, Engine as _};
70    let request = stream_wire::StreamRequest {
71        method: method.into(),
72        path: path.into(),
73        headers,
74        body_b64: STANDARD.encode(body),
75    };
76    stream_wire::encode_request(&request).expect("http tunnel request encodes")
77}