Skip to content

Commit

Permalink
feat(transport): Add WebTransport for WASM environments
Browse files Browse the repository at this point in the history
Use `web-sys::WebTransport` and provide a `Transport`.
  • Loading branch information
oblique committed Jun 3, 2023
1 parent e2e6816 commit 231c032
Show file tree
Hide file tree
Showing 15 changed files with 1,099 additions and 7 deletions.
39 changes: 38 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ concurrency:

env:
SEGMENT_DOWNLOAD_TIMEOUT_MINS: 2 # Fail cache download after 2 minutes.
# This is needed until web_sys::WebTransport gets stabilized
RUSTFLAGS: --cfg=web_sys_unstable_apis

jobs:
test:
Expand Down Expand Up @@ -42,9 +44,42 @@ jobs:
shared-key: stable-cache
save-if: false

- name: Find manifest directory
id: find-manifest
run: |
MANIFEST_PATH="$(cargo metadata --format-version=1 --no-deps | jq -r '.packages | map(select(.name == "'"$CRATE"'") | .manifest_path) | first')"
MANIFEST_DIR="$(dirname "$MANIFEST_PATH")"
echo "manifest_dir=${MANIFEST_DIR}"
echo "manifest_dir=${MANIFEST_DIR}" >> $GITHUB_OUTPUT
# If crate has wasm-bindgen-test as a dependency, then we assume
# that it has WASM tests
- name: Check if crate has any WASM tests
id: check-wasm
run: |
HAS_WASM_TEST=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages | map(select(.name == "'"$CRATE"'") | .dependencies[].name | contains("wasm-bindgen-test")) | any')
echo "has_wasm_test=${HAS_WASM_TEST}"
echo "has_wasm_test=${HAS_WASM_TEST}" >> $GITHUB_OUTPUT
- name: Install wasm-pack
if: steps.check-wasm.outputs.has_wasm_test == 'true'
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | bash

- name: Run all tests
run: cargo test --package "$CRATE" --all-features

- name: Run all tests (Chrome/WASM)
if: steps.check-wasm.outputs.has_wasm_test == 'true'
# `wasm-pack` does not support `--package`
working-directory: ${{ steps.find-manifest.outputs.manifest_dir }}
run: wasm-pack test --chrome --headless --all-features

- name: Run all tests (Firefox/WASM)
if: steps.check-wasm.outputs.has_wasm_test == 'true'
# `wasm-pack` does not support `--package`
working-directory: ${{ steps.find-manifest.outputs.manifest_dir }}
run: wasm-pack test --firefox --headless --all-features

- name: Check if we compile without any features activated
run: cargo build --package "$CRATE" --no-default-features

Expand Down Expand Up @@ -76,7 +111,7 @@ jobs:
run: |
PACKAGE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .version')
SPECIFIED_VERSION=$(tomlq "workspace.dependencies.$CRATE.version" --file ./Cargo.toml)
echo "Package version: $PACKAGE_VERSION";
echo "Specified version: $SPECIFIED_VERSION";
Expand Down Expand Up @@ -284,6 +319,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: dtolnay/rust-toolchain@stable

- id: cargo-metadata
run: |
WORKSPACE_MEMBERS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(select(.publish == null) | .name)')
Expand Down
71 changes: 67 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ members = [
"transports/wasm-ext",
"transports/webrtc",
"transports/websocket",
"transports/webtransport-websys",
]
resolver = "2"

Expand Down Expand Up @@ -94,8 +95,12 @@ libp2p-uds = { version = "0.39.0", path = "transports/uds" }
libp2p-wasm-ext = { version = "0.40.0", path = "transports/wasm-ext" }
libp2p-webrtc = { version = "0.5.0-alpha", path = "transports/webrtc" }
libp2p-websocket = { version = "0.42.0", path = "transports/websocket" }
libp2p-webtransport-websys = { version = "0.43.0", path = "transports/webtransport-websys" }
libp2p-yamux = { version = "0.44.0", path = "muxers/yamux" }
multistream-select = { version = "0.13.0", path = "misc/multistream-select" }
quick-protobuf-codec = { version = "0.2.0", path = "misc/quick-protobuf-codec" }
quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" }
rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" }

[patch.crates-io]
multiaddr = { git = "https://github.com/oblique/rust-multiaddr", branch = "v0.17-webtransport" }
9 changes: 7 additions & 2 deletions libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ full = [
"wasm-ext-websocket",
"webrtc",
"websocket",
"webtransport-websys",
"yamux",
]

Expand Down Expand Up @@ -83,6 +84,7 @@ wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
webrtc = ["dep:libp2p-webrtc", "libp2p-webrtc?/pem"]
websocket = ["dep:libp2p-websocket"]
webtransport-websys = ["dep:libp2p-webtransport-websys"]
yamux = ["dep:libp2p-yamux"]

[dependencies]
Expand Down Expand Up @@ -128,6 +130,9 @@ libp2p-uds = { workspace = true, optional = true }
libp2p-webrtc = { workspace = true, optional = true }
libp2p-websocket = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
libp2p-webtransport-websys = { workspace = true, optional = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
async-trait = "0.1"
Expand All @@ -143,5 +148,5 @@ libp2p-tcp = { workspace = true, features = ["tokio"] }
# More information: https://docs.rs/about/builds#cross-compiling
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs", "--cfg", "web_sys_unstable_apis"]
rustc-args = ["--cfg", "docsrs", "--cfg", "web_sys_unstable_apis"]
5 changes: 5 additions & 0 deletions libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ pub mod webrtc {
#[cfg(not(target_arch = "wasm32"))]
#[doc(inline)]
pub use libp2p_websocket as websocket;
#[cfg(feature = "webtransport-websys")]
#[cfg(target_arch = "wasm32")]
#[cfg_attr(docsrs, doc(cfg(feature = "webtransport-websys")))]
#[doc(inline)]
pub use libp2p_webtransport_websys as webtransport_websys;
#[cfg(feature = "yamux")]
#[doc(inline)]
pub use libp2p_yamux as yamux;
Expand Down
5 changes: 5 additions & 0 deletions transports/webtransport-websys/CHANGELOG.md
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
48 changes: 48 additions & 0 deletions transports/webtransport-websys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "libp2p-webtransport-websys"
edition = "2021"
rust-version = { workspace = true }
description = "WebTransport for libp2p under WASM environment"
version = "0.43.0"
authors = [
"Yiannis Marangos <yiannis@eiger.co>",
"oblique <psyberbits@gmail.com>",
]
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"]
Loading

0 comments on commit 231c032

Please sign in to comment.