-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport): Add WebTransport for WASM environments
Use `web-sys::WebTransport` and provide a `Transport`.
- Loading branch information
Showing
14 changed files
with
1,051 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## 0.43.0 - unreleased | ||
|
||
* Initial implementation of WebTranport trasnport that uses web-sys. [PR 4015] | ||
|
||
[PR 4015]: https://github.com/libp2p/rust-libp2p/pull/4015 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[package] | ||
name = "libp2p-websys-webtransport" | ||
edition = "2021" | ||
rust-version = { workspace = true } | ||
description = "WebTransport for libp2p under WASM environment" | ||
version = "0.43.0" | ||
authors = [ | ||
"Yiannis Marangos <[email protected]>", | ||
"oblique <[email protected]>", | ||
] | ||
license = "MIT" | ||
repository = "https://github.com/libp2p/rust-libp2p" | ||
keywords = ["peer-to-peer", "libp2p", "networking"] | ||
categories = ["network-programming", "asynchronous"] | ||
|
||
[dependencies] | ||
futures = "0.3.28" | ||
js-sys = "0.3.63" | ||
libp2p-core = { workspace = true } | ||
libp2p-identity = { workspace = true } | ||
libp2p-noise = { workspace = true } | ||
log = "0.4.18" | ||
send_wrapper = { version = "0.6.0", features = ["futures"] } | ||
thiserror = "1.0.4" | ||
wasm-bindgen = "0.2.86" | ||
wasm-bindgen-futures = "0.4.36" | ||
web-sys = { version = "0.3.63", features = [ | ||
"ReadableStreamDefaultReader", | ||
"WebTransport", | ||
"WebTransportBidirectionalStream", | ||
"WebTransportHash", | ||
"WebTransportOptions", | ||
"WebTransportReceiveStream", | ||
"WebTransportSendStream", | ||
"WritableStreamDefaultWriter", | ||
] } | ||
|
||
[dev-dependencies] | ||
getrandom = { version = "0.2.9", features = ["js"] } | ||
multibase = "0.9.1" | ||
wasm-bindgen-test = "0.3.36" | ||
|
||
# Passing arguments to the docsrs builder in order to properly document cfg's. | ||
# More information: https://docs.rs/about/builds#cross-compiling | ||
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs", "--cfg", "web_sys_unstable_apis"] | ||
rustc-args = ["--cfg", "docsrs", "--cfg", "web_sys_unstable_apis"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use futures::FutureExt; | ||
use js_sys::Promise; | ||
use send_wrapper::SendWrapper; | ||
use std::task::{ready, Context, Poll}; | ||
use wasm_bindgen::JsValue; | ||
use wasm_bindgen_futures::JsFuture; | ||
|
||
/// Convenient wrapper to poll a promise to completion. | ||
pub(crate) struct CachedJsPromise { | ||
promise: Option<SendWrapper<JsFuture>>, | ||
} | ||
|
||
impl CachedJsPromise { | ||
/// Creates new uninitialized promise. | ||
pub(crate) fn new() -> Self { | ||
CachedJsPromise { promise: None } | ||
} | ||
|
||
/// Initialize promise if needed and then poll. | ||
/// | ||
/// If promise is not initialized then call `init` and initialize it. | ||
pub(crate) fn maybe_init_and_poll<F>( | ||
&mut self, | ||
cx: &mut Context, | ||
init: F, | ||
) -> Poll<Result<JsValue, JsValue>> | ||
where | ||
F: FnOnce() -> Promise, | ||
{ | ||
if self.promise.is_none() { | ||
self.promise = Some(SendWrapper::new(JsFuture::from(init()))); | ||
} | ||
|
||
self.poll(cx) | ||
} | ||
|
||
/// Poll an already initialized promise. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If promise is not initialized then it panics. Use `maybe_init_and_poll` | ||
/// if unsure. | ||
pub(crate) fn poll(&mut self, cx: &mut Context) -> Poll<Result<JsValue, JsValue>> { | ||
let val = ready!(self | ||
.promise | ||
.as_mut() | ||
.expect("CachedJsPromise not initialized") | ||
.poll_unpin(cx)); | ||
|
||
// Future finished, drop it | ||
self.promise.take(); | ||
|
||
Poll::Ready(val) | ||
} | ||
|
||
/// Checks if promise is already running | ||
pub(crate) fn is_active(&self) -> bool { | ||
self.promise.is_some() | ||
} | ||
} |
Oops, something went wrong.