Skip to content

Commit 494f482

Browse files
committed
feat(wasm-ext): Add web-sys WebTransport
1 parent 7965a1e commit 494f482

11 files changed

Lines changed: 849 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 60 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libp2p/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ full = [
4444
"wasm-bindgen",
4545
"wasm-ext",
4646
"wasm-ext-websocket",
47+
"wasm-ext-webtransport",
4748
"webrtc",
4849
"websocket",
4950
"yamux",
@@ -81,6 +82,7 @@ uds = ["dep:libp2p-uds"]
8182
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen", "libp2p-gossipsub?/wasm-bindgen"]
8283
wasm-ext = ["dep:libp2p-wasm-ext"]
8384
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
85+
wasm-ext-webtransport = ["wasm-ext", "libp2p-wasm-ext?/webtransport"]
8486
webrtc = ["dep:libp2p-webrtc", "libp2p-webrtc?/pem"]
8587
websocket = ["dep:libp2p-websocket"]
8688
yamux = ["dep:libp2p-yamux"]

transports/wasm-ext/Cargo.toml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,40 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
1111
categories = ["network-programming", "asynchronous"]
1212

1313
[dependencies]
14+
thiserror = { version = "1.0.4", optional = true }
1415
futures = "0.3.28"
1516
js-sys = "0.3.63"
17+
web-sys = { version = "0.3.63", optional = true }
1618
libp2p-core = { workspace = true }
17-
parity-send-wrapper = "0.1.0"
19+
libp2p-identity = { workspace = true, optional = true }
20+
libp2p-noise = { workspace = true, optional = true }
21+
send_wrapper = { version = "0.6.0", features = ["futures"] }
1822
wasm-bindgen = "0.2.86"
1923
wasm-bindgen-futures = "0.4.36"
2024

2125
[features]
2226
websocket = []
27+
webtransport = [
28+
"thiserror",
29+
"libp2p-identity",
30+
"libp2p-noise",
31+
"dep:web-sys",
32+
"web-sys?/ReadableStreamDefaultReader",
33+
"web-sys?/WritableStreamDefaultWriter",
34+
"web-sys?/WebTransport",
35+
"web-sys?/WebTransportHash",
36+
"web-sys?/WebTransportOptions",
37+
"web-sys?/WebTransportBidirectionalStream",
38+
"web-sys?/WebTransportReceiveStream",
39+
"web-sys?/WebTransportSendStream",
40+
]
2341

24-
# Passing arguments to the docsrs builder in order to properly document cfg's.
42+
[dev-dependencies]
43+
multibase = "0.9.1"
44+
wasm-bindgen-test = "0.3.36"
45+
getrandom = { version = "0.2.9", features = ["js"] }
46+
47+
# Passing arguments to the docsrs builder in order to properly document cfg's.
2548
# More information: https://docs.rs/about/builds#cross-compiling
2649
[package.metadata.docs.rs]
2750
all-features = true

transports/wasm-ext/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ use libp2p_core::{
4040
transport::{ListenerId, TransportError, TransportEvent},
4141
Multiaddr, Transport,
4242
};
43-
use parity_send_wrapper::SendWrapper;
43+
use send_wrapper::SendWrapper;
4444
use std::{collections::VecDeque, error, fmt, io, mem, pin::Pin, task::Context, task::Poll};
4545
use wasm_bindgen::{prelude::*, JsCast};
4646
use wasm_bindgen_futures::JsFuture;
4747

48+
#[cfg(feature = "webtransport")]
49+
pub mod webtransport;
50+
51+
#[cfg(test)]
52+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
53+
4854
/// Contains the definition that one must match on the JavaScript side.
4955
pub mod ffi {
5056
use wasm_bindgen::prelude::*;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod cached_js_promise;
2+
mod connection;
3+
mod error;
4+
mod substream;
5+
mod transport;
6+
mod utils;
7+
8+
pub use self::connection::Connection;
9+
pub use self::error::Error;
10+
pub use self::substream::Substream;
11+
pub use self::transport::{Config, Transport};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use futures::FutureExt;
2+
use js_sys::Promise;
3+
use send_wrapper::SendWrapper;
4+
use std::task::{ready, Context, Poll};
5+
use wasm_bindgen::JsValue;
6+
use wasm_bindgen_futures::JsFuture;
7+
8+
pub struct CachedJsPromise {
9+
cached: Option<SendWrapper<JsFuture>>,
10+
}
11+
12+
impl CachedJsPromise {
13+
pub fn new() -> Self {
14+
CachedJsPromise { cached: None }
15+
}
16+
17+
pub fn maybe_init_and_poll<F>(
18+
&mut self,
19+
cx: &mut Context,
20+
init: F,
21+
) -> Poll<Result<JsValue, JsValue>>
22+
where
23+
F: FnOnce() -> Promise,
24+
{
25+
if self.cached.is_none() {
26+
self.cached = Some(SendWrapper::new(JsFuture::from(init())));
27+
}
28+
29+
self.poll(cx)
30+
}
31+
32+
pub fn poll(&mut self, cx: &mut Context) -> Poll<Result<JsValue, JsValue>> {
33+
let val = ready!(self
34+
.cached
35+
.as_mut()
36+
.expect("CachedJsPromise not initialized")
37+
.poll_unpin(cx));
38+
39+
// Future finished, drop it
40+
self.cached.take();
41+
42+
Poll::Ready(val)
43+
}
44+
45+
pub fn is_active(&self) -> bool {
46+
self.cached.is_some()
47+
}
48+
}

0 commit comments

Comments
 (0)