Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rs/web-transport-wasm/examples/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub async fn run(url: String, hash: String) -> Result<Array, JsValue> {
}

check!("echo round trip", echo_round_trip);
check!("no subprotocol reports None", no_subprotocol_is_none);
check!(
"dropped pending opens release stream credit",
dropped_pending_opens_release_credit
Expand Down Expand Up @@ -132,6 +133,19 @@ async fn echo_round_trip(session: Session) -> Result<String, Error> {
expect(got == "hello harness", format!("echoed {got:?}"))
}

/// `connect` above offers no subprotocols, so the browser negotiates none and
/// reports `""`. The trait contract says an unnegotiated protocol is `None`, and a
/// `Some("")` reaching a caller gets matched against their protocol table as if the
/// peer had chosen something.
async fn no_subprotocol_is_none(session: Session) -> Result<String, Error> {
let protocol = session.protocol();

expect(
protocol.is_none(),
format!("protocol() returned {protocol:?}"),
)
}

/// Dropping a handle cannot cancel the browser promise returned by open. Churn
/// past the peer's stream limit, then prove those abandoned opens were reset and
/// returned their credit rather than blocking the next real stream forever.
Expand Down
11 changes: 9 additions & 2 deletions rs/web-transport-wasm/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,15 @@ impl Session {
pub fn new(inner: WebTransport, url: Url) -> Self {
// TODO use the web_sys bindings when updated.
// Until then, we try to access the protocol property on the inner object.
//
// The browser reports `""` when no subprotocol was negotiated, which is what
// `None` means here. Normalizing at the capture site keeps every reader —
// the inherent getter and both trait impls — from having to know that, and
// stops `Some("")` being matched against an application's protocol table.
let protocol = Reflect::get(&inner, &"protocol".into())
.ok()
.and_then(|p| p.as_string());
.and_then(|p| p.as_string())
.filter(|p| !p.is_empty());

Self {
shared: Rc::new(Shared {
Expand Down Expand Up @@ -556,7 +562,8 @@ impl Session {
&self.shared.url
}

/// Return the application protocol used to create the session.
/// Return the subprotocol the server selected, or `None` when none was
/// negotiated.
pub fn protocol(&self) -> Option<&str> {
self.shared.protocol.as_deref()
}
Expand Down
2 changes: 2 additions & 0 deletions rs/web-transport/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl Session {
}

/// Return the application protocol used to create the session.
///
/// This is the negotiated subprotocol, or `None` when none was negotiated.
pub fn protocol(&self) -> Option<&str> {
self.0.protocol()
}
Expand Down