use http::{HeaderName, HeaderValue};
use n0_error::stack_error;
#[cfg(feature = "server")]
pub(crate) const WEBSOCKET_UPGRADE_PROTOCOL: &str = "websocket";
#[cfg(feature = "server")] pub(crate) const SUPPORTED_WEBSOCKET_VERSION: &str = "13";
pub const RELAY_PATH: &str = "/relay";
pub const RELAY_PROBE_PATH: &str = "/ping";
pub const CLIENT_AUTH_HEADER: HeaderName = HeaderName::from_static("x-iroh-relay-client-auth-v1");
#[cfg(any(wasm_browser, feature = "server"))]
pub(crate) const AUTH_TOKEN_URL_QUERY_PARAM: &str = "token";
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
strum::EnumString,
strum::Display,
strum::IntoStaticStr,
)]
#[cfg_attr(test, derive(strum::EnumCount, test_strategy::Arbitrary))]
#[strum(parse_err_ty = UnsupportedRelayProtocolVersion, parse_err_fn = strum_err_fn)]
#[non_exhaustive]
pub enum ProtocolVersion {
#[strum(serialize = "iroh-relay-v1")]
V1,
#[default]
#[strum(serialize = "iroh-relay-v2")]
V2,
}
impl ProtocolVersion {
pub const ALL: &'static [Self] = &[Self::V2, Self::V1];
pub fn all() -> impl Iterator<Item = &'static str> {
Self::ALL.iter().map(ProtocolVersion::to_str)
}
pub fn all_joined() -> String {
Self::all().collect::<Vec<_>>().join(", ")
}
pub fn all_as_header_value() -> HeaderValue {
HeaderValue::from_bytes(Self::all_joined().as_bytes()).expect("valid header name")
}
pub fn to_str(&self) -> &'static str {
self.into()
}
pub fn match_from_str(s: &str) -> Option<Self> {
Self::try_from(s).ok()
}
pub fn to_header_value(&self) -> HeaderValue {
HeaderValue::from_static(self.to_str())
}
}
#[stack_error(derive)]
#[error("Relay protocol version is not supported")]
pub struct UnsupportedRelayProtocolVersion;
fn strum_err_fn(_item: &str) -> UnsupportedRelayProtocolVersion {
UnsupportedRelayProtocolVersion::new()
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use strum::EnumCount;
use super::*;
#[test]
fn all_is_exhaustive() {
assert_eq!(ProtocolVersion::ALL.len(), ProtocolVersion::COUNT);
for &v in ProtocolVersion::ALL {
assert_eq!(ProtocolVersion::from_str(v.to_str()).unwrap(), v);
}
}
}