Skip to content

Commit

Permalink
feat(iroh-net-report): Compile and run in wasm/browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jan 20, 2025
1 parent d4ef90b commit 942d4ac
Show file tree
Hide file tree
Showing 11 changed files with 750 additions and 97 deletions.
25 changes: 24 additions & 1 deletion Cargo.lock

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

34 changes: 26 additions & 8 deletions iroh-net-report/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,48 @@ bytes = "1.7"
derive_more = { version = "1.0.0", features = ["display"] }
futures-buffered = "0.2.8"
futures-lite = "2.3"
hickory-resolver = "=0.25.0-alpha.4"
iroh-base = { version = "0.31.0", path = "../iroh-base", default-features = false, features = ["relay"] }
iroh-metrics = { version = "0.31", default-features = false }
iroh-relay = { version = "0.31", path = "../iroh-relay" }
netwatch = { version = "0.3" }
portmapper = { version = "0.3", default-features = false }
quinn = { package = "iroh-quinn", version = "0.12.0" }
iroh-relay = { version = "0.31", path = "../iroh-relay", default-features = false }
quinn = { package = "iroh-quinn", version = "0.12.0", default-features = false }
rand = "0.8"
reqwest = { version = "0.12", default-features = false }
reqwest = { version = "0.12", default-features = false, features = ["stream"] }
rustls = { version = "0.23", default-features = false }
surge-ping = "0.8.0"
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["sync", "time", "macros", "rt"] }
tokio = { version = "1", default-features = false, features = ["sync", "time", "macros"] }
tokio-util = { version = "0.7.12", default-features = false }
tracing = "0.1"
url = { version = "2.4" }

# non-wasm-in-browser dependencies
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
hickory-resolver = "=0.25.0-alpha.4"
netwatch = { version = "0.3" }
portmapper = { version = "0.3", default-features = false }
surge-ping = "0.8.0"
tokio = { version = "1", default-features = false, features = ["sync", "time", "macros", "rt"] }

# wasm-in-browser dependencies
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
futures-util = "0.3"
js-sys = "0.3"
pin-project = "1"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-time = "1"
send_wrapper = "0.6"

[dev-dependencies]
iroh-relay = { path = "../iroh-relay", features = ["test-utils", "server"] }
iroh-test = { path = "../iroh-test" }
pretty_assertions = "1.4"
quinn = { package = "iroh-quinn", version = "0.12.0" }
testresult = "0.4.0"
tokio = { version = "1", default-features = false, features = ["test-util"] }

[build-dependencies]
cfg_aliases = { version = "0.2" }

[features]
default = ["metrics"]
metrics = ["iroh-metrics/metrics", "portmapper/metrics"]
Expand Down
9 changes: 9 additions & 0 deletions iroh-net-report/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use cfg_aliases::cfg_aliases;

fn main() {
// Setup cfg aliases
cfg_aliases! {
// Convenience aliases
wasm_browser: { all(target_family = "wasm", target_os = "unknown") },
}
}
2 changes: 1 addition & 1 deletion iroh-net-report/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// Contains all timeouts that we use in `iroh-net-report`.
pub(crate) mod timeouts {
use std::time::Duration;
use iroh_relay::time::Duration;

// Timeouts for net_report

Expand Down
9 changes: 5 additions & 4 deletions iroh-net-report/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fmt::Write, net::IpAddr};
use anyhow::Result;
use futures_lite::{Future, StreamExt};
use hickory_resolver::{IntoName, TokioResolver};
use iroh_relay::time;

use crate::defaults::timeouts::DNS_TIMEOUT;

Expand Down Expand Up @@ -66,12 +67,12 @@ pub(crate) trait ResolverExt {

impl ResolverExt for TokioResolver {
async fn lookup_ipv4<N: IntoName>(&self, host: N) -> Result<impl Iterator<Item = IpAddr>> {
let addrs = tokio::time::timeout(DNS_TIMEOUT, self.ipv4_lookup(host)).await??;
let addrs = time::timeout(DNS_TIMEOUT, self.ipv4_lookup(host)).await??;
Ok(addrs.into_iter().map(|ip| IpAddr::V4(ip.0)))
}

async fn lookup_ipv6<N: IntoName>(&self, host: N) -> Result<impl Iterator<Item = IpAddr>> {
let addrs = tokio::time::timeout(DNS_TIMEOUT, self.ipv6_lookup(host)).await??;
let addrs = time::timeout(DNS_TIMEOUT, self.ipv6_lookup(host)).await??;
Ok(addrs.into_iter().map(|ip| IpAddr::V6(ip.0)))
}

Expand Down Expand Up @@ -154,10 +155,10 @@ async fn stagger_call<T, F: Fn() -> Fut, Fut: Future<Output = Result<T>>>(
// NOTE: we add the 0 delay here to have a uniform set of futures. This is more performant than
// using alternatives that allow futures of different types.
for delay in std::iter::once(&0u64).chain(delays_ms) {
let delay = std::time::Duration::from_millis(*delay);
let delay = time::Duration::from_millis(*delay);
let fut = f();
let staggered_fut = async move {
tokio::time::sleep(delay).await;
time::sleep(delay).await;
fut.await
};
calls.push(staggered_fut)
Expand Down
Loading

0 comments on commit 942d4ac

Please sign in to comment.