Docs & wasm

This commit is contained in:
Alex Emmet 2026-06-25 12:28:14 +02:00
commit be4b76dcd5
7 changed files with 953 additions and 6 deletions

View file

@ -90,7 +90,7 @@ impl WasmClient {
#[wasm_bindgen]
pub async fn connect(&mut self, config: &ConnectionConfig) -> Result<(), JsValue> {
self.set_state(ConnectionState::Connecting);
let transport = WasmTransport::connect(&config.url).await?;
let transport = WasmTransport::connect(&config.url, config.server_certificate_hashes.clone()).await?;
let inner = transport.inner().clone();
let version_str = format!("{}", PROTOCOL_VERSION);
@ -157,7 +157,7 @@ impl WasmClient {
.add_typed_default(DataType::Signature, DataValue::Bytes(signature))
.to_bytes();
let transport = WasmTransport::connect(&config.url).await?;
let transport = WasmTransport::connect(&config.url, config.server_certificate_hashes.clone()).await?;
let inner = transport.inner().clone();
transport.send_frame(&frame).await?;
@ -249,7 +249,7 @@ impl WasmClient {
.add_typed_default(DataType::Signature, DataValue::Bytes(signature))
.to_bytes();
let transport = WasmTransport::connect(&config.url).await?;
let transport = WasmTransport::connect(&config.url, config.server_certificate_hashes.clone()).await?;
let inner = transport.inner().clone();
transport.send_frame(&frame).await?;

View file

@ -1,7 +1,7 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::WebTransport;
use web_sys::{WebTransport, WebTransportHash, WebTransportOptions};
use crate::error::js_error;
@ -11,8 +11,28 @@ pub struct WasmTransport {
}
impl WasmTransport {
pub async fn connect(url: &str) -> Result<Self, JsValue> {
let transport = WebTransport::new(url)?;
pub async fn connect(url: &str, cert_hashes: Option<Vec<String>>) -> Result<Self, JsValue> {
let transport = match cert_hashes {
Some(hashes) => {
let opts = WebTransportOptions::new();
let mut wt_hashes = Vec::new();
for h in hashes {
if let Some((algo, hex_val)) = h.split_once(':') {
if let Ok(bytes) = hex::decode(hex_val) {
let hash = WebTransportHash::new();
hash.set_algorithm(algo);
hash.set_value_u8_array(&js_sys::Uint8Array::from(&bytes[..]));
wt_hashes.push(hash);
}
}
}
if !wt_hashes.is_empty() {
opts.set_server_certificate_hashes(&wt_hashes);
}
WebTransport::new_with_options(url, &opts)?
}
None => WebTransport::new(url)?,
};
JsFuture::from(transport.ready()).await
.map_err(|e| js_error(&format!("WebTransport ready failed: {:?}", e)))?;
Ok(Self { inner: transport })