Files
nym/common/wasm/utils/src/lib.rs
T
mfahampshire 43a1bd38e8 Max/smolmix wasm (#6784)
* Mod gitignore + license trimming + comment trimming

* Big rewrite

* SURB inputs + DNS button in internal-dev

* Make ipr addr optional

* Accidentatly omitted files from rewrite commit

* Makefile + readme

* Comment rewrite

* Optimisation comment

* Replace manual waker map with
      smoltcp built-ins + adaptive poll

* Comments

* Extract socket creation helpers into stream.rs

* Cleanup comments

* Comment

* Comment notes and restrict ciphersuites wrt rustls-rustcrypto

* Dep. hack fix for demo + add clearnet fetch() for contrast

* Stripped down devtester

* Fix Clippy arg (fatfingered deletion)

* CodeRabbit catches

* Cargofmt

* Review nits: bridge logs, fetch early-return, static port counter, copyright years, README + Cargo + headless.js tidying

* PHONY + taskset override, switch internal-dev/tests to pnpm, fix wasm-pack out-dir

* Gate codec tests behind the codec feature for no-default-features builds

* IPv6 addr/route on smoltcp iface + configurable DNS resolvers via TunnelOpts

* DNS GUI inputs, close stale WS on reconnect, worker init guards + ws-send warning, Playwright listener cleanup, pnpm-lock in internal-dev

* Fix lp -> lp-data after rebase

* Revert nym-lp/nym-lp-data feature-gating left over from rebase

* Lift getrandom wasm_js cfg to workspace .cargo/config.toml so cargo check -p smolmix-wasm works from any CWD

* temp will amend git message

* Auto-discover IPR when none specified + 'Use random IPR' checkbox in internal-dev

* smolmix_tracker + State machine + ready_tunnel gate + getTunnelState JS surface

* Mirror red display() entries to console.error

* Add left out package-lock

* Reactor clock + yield_now + atomic seq + gateway-storage errors

* setupMixTunnel gate + MTU 1980 + http::Uri cleanup

* Review pass + fix test + clippy

* restore axum 0.8 bump from borked earlier merge

* Feature gating (dns/fetch/socket) + TunnelOptsBuilder + pnpm bypass

* Cont. with review comments

* tokio Nofity reactor wakes + cancellation + setup polishing

* Notify wakes + inner pattern + close_notify + util

* Tunable tunnelopts

* Fix tired commit

* CI prep

* Lint + Clippy

* coderabbit u32 fix

* nits + runtime debugging + expose in internal-dev

* remove redudant default-features

* Remove more redundant default-features
2026-05-28 15:57:10 +00:00

150 lines
4.5 KiB
Rust

// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use std::sync::atomic::{AtomicBool, Ordering};
use wasm_bindgen::prelude::*;
#[cfg(feature = "websocket")]
pub mod websocket;
#[cfg(feature = "crypto")]
pub mod crypto;
pub mod error;
#[doc(hidden)]
pub static DEBUG_LOGGING_ENABLED: AtomicBool = AtomicBool::new(false);
/// Enable or disable `debug_log!` / `debug_error!` output at runtime.
///
/// Consumers call this from their own init path when their own `debug`
/// (or equivalent) feature is on. The default is `false`, so the macros
/// compile to a single relaxed-load + branch and otherwise no-op.
///
/// Also exposed to JS as `setDebugLogging(enabled: boolean)` for ad-hoc
/// toggling without rebuilding.
#[wasm_bindgen(js_name = "setDebugLogging")]
pub fn set_debug_logging(enabled: bool) {
DEBUG_LOGGING_ENABLED.store(enabled, Ordering::Relaxed);
}
/// Read the current debug-logging state. Used by the macros below.
#[inline]
pub fn debug_logging_enabled() -> bool {
DEBUG_LOGGING_ENABLED.load(Ordering::Relaxed)
}
// will cause messages to be written as if console.log("...") was called
#[macro_export]
macro_rules! console_log {
($($t:tt)*) => ($crate::log(&format_args!($($t)*).to_string()))
}
// will cause messages to be written as if console.debug("...") was called
#[macro_export]
macro_rules! console_debug {
($($t:tt)*) => ($crate::debug(&format_args!($($t)*).to_string()))
}
// will cause messages to be written as if console.info("...") was called
#[macro_export]
macro_rules! console_info {
($($t:tt)*) => ($crate::info(&format_args!($($t)*).to_string()))
}
// will cause messages to be written as if console.warn("...") was called
#[macro_export]
macro_rules! console_warn {
($($t:tt)*) => ($crate::warn(&format_args!($($t)*).to_string()))
}
// will cause messages to be written as if console.error("...") was called
#[macro_export]
macro_rules! console_error {
($($t:tt)*) => ($crate::error(&format_args!($($t)*).to_string()))
}
/// `console.log` gated behind the runtime [`DEBUG_LOGGING_ENABLED`] flag.
///
/// Format-args evaluation stays inside the `if` arm, so it's skipped at
/// runtime when logging is off. Consumers turn this on via
/// [`set_debug_logging`] from their own init path (typically when their
/// own `debug` feature is enabled).
#[macro_export]
macro_rules! debug_log {
($($t:tt)*) => {{
if $crate::debug_logging_enabled() {
$crate::console_log!($($t)*);
}
}};
}
/// `console.error` gated behind the runtime [`DEBUG_LOGGING_ENABLED`] flag.
/// See [`debug_log!`] for semantics.
#[macro_export]
macro_rules! debug_error {
($($t:tt)*) => {{
if $crate::debug_logging_enabled() {
$crate::console_error!($($t)*);
}
}};
}
/// Hex preview of a buffer, truncated with ` ...` when over `max_bytes`.
/// Useful for `console.log`-style binary debug output.
pub fn hex_preview(buf: &[u8], max_bytes: usize) -> String {
let len = buf.len().min(max_bytes);
let hex: String = buf[..len]
.iter()
.map(|b| format!("{b:02x}"))
.collect::<Vec<_>>()
.join(" ");
if buf.len() > max_bytes {
format!("{hex} ...")
} else {
hex
}
}
#[wasm_bindgen]
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn debug(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn info(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn warn(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn error(s: &str);
}
#[cfg(feature = "sleep")]
pub async fn sleep(ms: i32) -> Result<(), wasm_bindgen::JsValue> {
let promise = js_sys::Promise::new(&mut |yes, _| {
let win = web_sys::window().expect("no window available!");
win.set_timeout_with_callback_and_timeout_and_arguments_0(&yes, ms)
.unwrap();
});
let js_fut = wasm_bindgen_futures::JsFuture::from(promise);
js_fut.await?;
Ok(())
}