diff --git a/Cargo.lock b/Cargo.lock index 39b78d46..a66192cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5741,6 +5741,7 @@ dependencies = [ "async-std", "async_executors", "cfg-if 1.0.0", + "console_error_panic_hook", "eyre", "futures-util", "jni", @@ -5766,6 +5767,7 @@ dependencies = [ "tokio-util", "tracing", "tracing-android", + "tracing-wasm", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", diff --git a/veilid-tools/Cargo.toml b/veilid-tools/Cargo.toml index 9d306626..2095866f 100644 --- a/veilid-tools/Cargo.toml +++ b/veilid-tools/Cargo.toml @@ -9,7 +9,7 @@ license = "LGPL-2.0-or-later OR MPL-2.0 OR (MIT AND BSD-3-Clause)" crate-type = ["rlib"] [features] -default = [ "rt-tokio", "tracing" ] +default = [ "tracing" ] rt-async-std = [ "async-std", "async_executors/async_std", ] rt-tokio = [ "tokio", "tokio-util", "async_executors/tokio_tp", "async_executors/tokio_io", "async_executors/tokio_timer", ] @@ -54,14 +54,13 @@ async_executors = { version = "^0", default-features = false, features = [ "bind async-lock = "^2" send_wrapper = { version = "^0.6", features = ["futures"] } - # Dependencies for Android [target.'cfg(target_os = "android")'.dependencies] jni = "^0" jni-sys = "^0" ndk = { version = "^0", features = ["trace"] } ndk-glue = { version = "^0", features = ["logger"] } -tracing-android = { version = "^0" } +tracing-android = { version = "^0", optional = true } # Dependencies for Windows # [target.'cfg(target_os = "windows")'.dependencies] @@ -81,9 +80,12 @@ serial_test = "^0" simplelog = { version = "^0", features=["test"] } [target.'cfg(target_arch = "wasm32")'.dev-dependencies] +console_error_panic_hook = "^0" wasm-bindgen-test = "^0" wee_alloc = "^0" wasm-logger = "^0" +tracing-wasm = { version = "^0" } +parking_lot = { version = "^0", features = ["wasm-bindgen"]} ### BUILD OPTIONS diff --git a/veilid-tools/src/random.rs b/veilid-tools/src/random.rs index 2f395b55..9c3d9fa7 100644 --- a/veilid-tools/src/random.rs +++ b/veilid-tools/src/random.rs @@ -28,6 +28,8 @@ impl RngCore for VeilidRng { cfg_if! { if #[cfg(target_arch = "wasm32")] { + use js_sys::Math; + pub fn random_bytes(dest: &mut [u8]) -> EyreResult<()> { let len = dest.len(); let u32len = len / 4; diff --git a/veilid-tools/src/sleep.rs b/veilid-tools/src/sleep.rs index c0d4a899..31c458c5 100644 --- a/veilid-tools/src/sleep.rs +++ b/veilid-tools/src/sleep.rs @@ -3,7 +3,7 @@ use std::time::Duration; cfg_if! { if #[cfg(target_arch = "wasm32")] { - use async_executors::Bindgen; + use async_executors::{Bindgen, Timer}; pub async fn sleep(millis: u32) { Bindgen.sleep(Duration::from_millis(millis.into())).await diff --git a/veilid-tools/src/tests/common/test_host_interface.rs b/veilid-tools/src/tests/common/test_host_interface.rs index 2d73c399..8f31a953 100644 --- a/veilid-tools/src/tests/common/test_host_interface.rs +++ b/veilid-tools/src/tests/common/test_host_interface.rs @@ -295,7 +295,7 @@ pub async fn test_sleep() { if #[cfg(target_arch = "wasm32")] { let t1 = Date::now(); - intf::sleep(1000).await; + sleep(1000).await; let t2 = Date::now(); assert!((t2-t1) >= 1000.0); diff --git a/veilid-tools/src/timeout.rs b/veilid-tools/src/timeout.rs index 07858381..1ca2df26 100644 --- a/veilid-tools/src/timeout.rs +++ b/veilid-tools/src/timeout.rs @@ -7,7 +7,7 @@ cfg_if! { where F: Future, { - match select(Box::pin(intf::sleep(dur_ms)), Box::pin(f)).await { + match select(Box::pin(sleep(dur_ms)), Box::pin(f)).await { Either::Left((_x, _b)) => Err(TimeoutError()), Either::Right((y, _a)) => Ok(y), } diff --git a/veilid-tools/src/timestamp.rs b/veilid-tools/src/timestamp.rs index af042327..4b5c187e 100644 --- a/veilid-tools/src/timestamp.rs +++ b/veilid-tools/src/timestamp.rs @@ -5,7 +5,7 @@ cfg_if! { use js_sys::Date; pub fn get_timestamp() -> u64 { - if utils::is_browser() { + if is_browser() { return (Date::now() * 1000.0f64) as u64; } else { panic!("WASM requires browser environment"); diff --git a/veilid-tools/src/wasm.rs b/veilid-tools/src/wasm.rs index 34408dbf..bfecbf9b 100644 --- a/veilid-tools/src/wasm.rs +++ b/veilid-tools/src/wasm.rs @@ -1,6 +1,7 @@ use super::*; use core::sync::atomic::{AtomicI8, Ordering}; use js_sys::{global, Reflect}; +use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { @@ -27,21 +28,21 @@ pub fn is_browser() -> bool { res } -// pub fn is_browser_https() -> bool { -// static CACHE: AtomicI8 = AtomicI8::new(-1); -// let cache = CACHE.load(Ordering::Relaxed); -// if cache != -1 { -// return cache != 0; -// } +pub fn is_browser_https() -> bool { + static CACHE: AtomicI8 = AtomicI8::new(-1); + let cache = CACHE.load(Ordering::Relaxed); + if cache != -1 { + return cache != 0; + } -// let res = js_sys::eval("window.location.protocol === 'https'") -// .map(|res| res.is_truthy()) -// .unwrap_or_default(); + let res = js_sys::eval("window.location.protocol === 'https'") + .map(|res| res.is_truthy()) + .unwrap_or_default(); -// CACHE.store(res as i8, Ordering::Relaxed); + CACHE.store(res as i8, Ordering::Relaxed); -// res -// } + res +} #[derive(ThisError, Debug, Clone, Eq, PartialEq)] #[error("JsValue error")] diff --git a/veilid-tools/tests/web.rs b/veilid-tools/tests/web.rs index 7e7c3c5f..6e4ba0f8 100644 --- a/veilid-tools/tests/web.rs +++ b/veilid-tools/tests/web.rs @@ -2,7 +2,7 @@ #![cfg(target_arch = "wasm32")] use veilid_tools::tests::common::*; -use veilid_tools::xx::*; +use veilid_tools::*; use wasm_bindgen_test::*; wasm_bindgen_test_configure!(run_in_browser); @@ -15,21 +15,18 @@ static SETUP_ONCE: Once = Once::new(); pub fn setup() -> () { SETUP_ONCE.call_once(|| { console_error_panic_hook::set_once(); - let mut builder = tracing_wasm::WASMLayerConfigBuilder::new(); - builder.set_report_logs_in_timings(false); - builder.set_max_level(Level::TRACE); - builder.set_console_config(tracing_wasm::ConsoleConfig::ReportWithConsoleColor); - tracing_wasm::set_as_global_default_with_config(builder.build()); + cfg_if! { + if #[cfg(feature = "tracing")] { + let mut builder = tracing_wasm::WASMLayerConfigBuilder::new(); + builder.set_report_logs_in_timings(false); + builder.set_max_level(Level::TRACE); + builder.set_console_config(tracing_wasm::ConsoleConfig::ReportWithConsoleColor); + tracing_wasm::set_as_global_default_with_config(builder.build()); + } + } }); } -#[wasm_bindgen_test] -async fn run_test_dht_key() { - setup(); - - test_dht_key::test_all().await; -} - #[wasm_bindgen_test] async fn run_test_host_interface() { setup(); @@ -37,48 +34,6 @@ async fn run_test_host_interface() { test_host_interface::test_all().await; } -#[wasm_bindgen_test] -async fn run_test_veilid_tools() { - setup(); - - test_veilid_tools::test_all().await; -} - -#[wasm_bindgen_test] -async fn run_test_config() { - setup(); - - test_veilid_config::test_all().await; -} - -#[wasm_bindgen_test] -async fn run_test_connection_table() { - setup(); - - test_connection_table::test_all().await; -} - -#[wasm_bindgen_test] -async fn run_test_table_store() { - setup(); - - test_table_store::test_all().await; -} - -#[wasm_bindgen_test] -async fn run_test_crypto() { - setup(); - - test_crypto::test_all().await; -} - -#[wasm_bindgen_test] -async fn run_test_envelope_receipt() { - setup(); - - test_envelope_receipt::test_all().await; -} - #[wasm_bindgen_test] async fn run_test_async_tag_lock() { setup();