# node-device-session
Shared server for the `/econ-v1/device-session/1` wire protocol (C-5 / ADR-100).
Carriers (`node-app-iroh`, `node-app-p2p`) implement [`SessionStreams`] and
[`HostBus`], then call [`serve`]. This crate has **no** iroh (or other carrier)
dependency.
## Public API
- `ALPN` — `b"/econ-v1/device-session/1"`
- `SessionStreams` / `HostBus` / `WebRtcSignaling`
- `serve(conn, bus, ingress, path, webrtc)` — pass `Some(hook)` when the
carrier answers `webrtc.signal/1`; `None` refuses with `unsupported_transport`.
`wire/1` is refused (`wire_unavailable`) unless configured via options
- `serve_with_registry(..., registry)` — same as `serve`, plus live-session
registration for periodic revalidation
- `serve_with_options(..., ServeOptions { registry, wire, .. })` — additive;
configure [`WireSocket`] and/or registry without changing the classic signatures
- `SessionRegistry` / `revalidate` — close revoked / expired / invalid sessions
- `webrtc::WebRtcAnswerer` + `webrtc::WebRtcCarrier` — shared str0m answerer;
`WebRtcAnswerer::with_wire_socket` enables DataChannel `wire/1`
- `WireSocket` / `wire_socket_path` / `splice_to_wire_socket` — node-wire
(ADR-310) splice helpers for carriers
Wire methods: `session.open/1`, `session.renew/1`, `device.call/1`,
`http.request/1`, `ws.stream/1`, `webrtc.signal/1`, `wire/1`.
## Live-session revalidation (once a minute)
A device that is revoked, unpaired or expired after it connected must lose its
live session within a minute on every carrier. Wire it like this:
1. Hold one shared [`SessionRegistry`] in the carrier app.
2. Accept connections with [`serve_with_registry`] (plain [`serve`] is unchanged
and does not register).
3. In `WebRtcCarrier::bind_session`, call
`registry.bind_webrtc(&identity.device_id, handle.clone())` so a later
revoke also tears down the WebRTC path.
4. Expose a capability such as `<app>.device.revalidate` and register a cron
row whose trigger is that capability on schedule `0 * * * * *` (every
minute). The handler calls `registry.revalidate(&bus).await`.
Only definitive verify refusals (`device_revoked`, `expired`,
`invalid_credential`, `endpoint_mismatch`) close a session.
`verifier_unavailable`, timeouts and other errors leave it open.
## Wiring the shared WebRTC answerer
The answerer owns signaling, ICE (str0m), DataChannel framing, and the
`http.request/1` / `ws.stream/1` / `wire/1` tunnels over those channels. A
carrier only supplies what is carrier-specific via [`WebRtcCarrier`]:
| STUN server list (no TURN) | `stun_servers()` |
| Optional ICE UDP bind | `bind_addr()` |
| Router / UPnP / relay portmap | `port_mapper()` + `mapping_description()` |
| Clock for str0m / ICE | `now()` (default: `Instant::now()`) |
| Bind handle into session registry | `bind_session(...)` |
Iroh-only adapters (gossip discovery → STUN list, `RelaySessionPortMapper`)
stay in `node-app-iroh` and implement the trait. p2p does the same with its
own STUN / portmap sources. Neither carrier reimplements the answerer.
```rust
use node_device_session::{
serve_with_options, wire_socket_path, IngressSocket, ServeOptions, SessionPath,
StaticWebRtcCarrier, WebRtcAnswerer, WireSocket,
};
use std::sync::Arc;
// Inside the carrier's ProtocolHandler / accept path:
let ingress = Arc::new(IngressSocket { path: Some(sock.into()) });
let wire = Arc::new(WireSocket {
path: Some(wire_socket_path(|k| std::env::var(k).ok())),
});
let carrier = StaticWebRtcCarrier {
stun: stun_from_discovery.await, // carrier-owned
bind: None,
mapper: my_port_mapper, // Arc<dyn SessionPortMapper>
mapping_description: "iroh-webrtc", // or "p2p-webrtc"
};
let webrtc = Arc::new(
WebRtcAnswerer::new(ingress.clone(), carrier).with_wire_socket(wire.clone()),
);
let mut options = ServeOptions::default();
options.wire = (*wire).clone();
serve_with_options(
streams,
bus,
(*ingress).clone(),
SessionPath::Relay,
Some(webrtc),
options,
)
.await?;
```
Carriers that do not need `wire/1` keep calling
`serve` / `serve_with_registry` and `WebRtcAnswerer::new` unchanged.
For revocation / replace, override `WebRtcCarrier::bind_session` to store the
[`WebRtcHandle`] in the carrier's session registry and return `false` when the
session cannot accept WebRTC. Call `handle.close()` on revoke.
### `wire/1` (node-wire)
After a **full** `session.open/1`, a bi-stream (QUIC) or DataChannel (WebRTC)
labelled `wire/1` splices onto the configured [`WireSocket`] with the ADR-310
preamble (`carrier` = `relay` / `webrtc`, no `peer`). Renewal-only sessions
refuse with `renewal_only` and never touch the socket. Carriers that pass no
wire path (the default) keep prior HTTP/WS-only behaviour; `wire/1` is refused
with `wire_unavailable`.
Wire protocol for `webrtc.signal/1` is unchanged.