From 143d43047fb057911936a6afef2c6ad8f9541d6d Mon Sep 17 00:00:00 2001 From: Jose Daniel Hernandez Date: Mon, 20 Nov 2023 10:36:51 -0600 Subject: [PATCH] fix(websocket-websys): Add support for different WASM environments Add support different WASM environments (such as workers, NodeJS) that don't have a `window` global. This is done by introducing a `WebContext` `enum` that abstracts and detects the `Window` vs the `WorkerGlobalScope` API. This is done due to the `web-sys` lack of interes to support this (see discussion in [this issue](https://github.com/rustwasm/wasm-bindgen/issues/1046)). --- Cargo.lock | 2 +- Cargo.toml | 2 +- transports/websocket-websys/CHANGELOG.md | 7 +++++ transports/websocket-websys/Cargo.toml | 4 +-- transports/websocket-websys/src/lib.rs | 37 +++++++++++++++--------- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f54623dca55..696220a0818f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3336,7 +3336,7 @@ dependencies = [ [[package]] name = "libp2p-websocket-websys" -version = "0.3.0" +version = "0.3.1" dependencies = [ "bytes", "futures", diff --git a/Cargo.toml b/Cargo.toml index 27a8214ed6fc..fb32310723db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ libp2p-webrtc = { version = "0.6.1-alpha", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.1.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.2.0-alpha", path = "transports/webrtc-websys" } libp2p-websocket = { version = "0.43.0", path = "transports/websocket" } -libp2p-websocket-websys = { version = "0.3.0", path = "transports/websocket-websys" } +libp2p-websocket-websys = { version = "0.3.1", path = "transports/websocket-websys" } libp2p-webtransport-websys = { version = "0.2.0", path = "transports/webtransport-websys" } libp2p-yamux = { version = "0.45.0", path = "muxers/yamux" } multiaddr = "0.18.0" diff --git a/transports/websocket-websys/CHANGELOG.md b/transports/websocket-websys/CHANGELOG.md index 17c253cb80a7..d473bcc6e0a7 100644 --- a/transports/websocket-websys/CHANGELOG.md +++ b/transports/websocket-websys/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.3.1 + +- Add support for different WASM environments by introducing a `WebContext` that + detects and abstracts the `Window` vs the `WorkerGlobalScope` API. See [PR 4889]. + +[PR 4889]: https://github.com/libp2p/rust-libp2p/pull/4889 + ## 0.3.0 diff --git a/transports/websocket-websys/Cargo.toml b/transports/websocket-websys/Cargo.toml index 6a6cef5f8bcf..ffa2892e8388 100644 --- a/transports/websocket-websys/Cargo.toml +++ b/transports/websocket-websys/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-websocket-websys" edition = "2021" rust-version = "1.60.0" description = "WebSocket for libp2p under WASM environment" -version = "0.3.0" +version = "0.3.1" authors = ["Vince Vasta "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -20,7 +20,7 @@ parking_lot = "0.12.1" send_wrapper = "0.6.0" thiserror = "1.0.50" wasm-bindgen = "0.2.88" -web-sys = { version = "0.3.65", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket", "Window"] } +web-sys = { version = "0.3.65", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket", "Window", "WorkerGlobalScope"] } # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/transports/websocket-websys/src/lib.rs b/transports/websocket-websys/src/lib.rs index b4f7566f95ea..0e4c66fca5ea 100644 --- a/transports/websocket-websys/src/lib.rs +++ b/transports/websocket-websys/src/lib.rs @@ -20,6 +20,8 @@ //! Libp2p websocket transports built on [web-sys](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html). +mod web_context; + use bytes::BytesMut; use futures::task::AtomicWaker; use futures::{future::Ready, io, prelude::*}; @@ -35,7 +37,17 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; use std::{pin::Pin, task::Context, task::Poll}; use wasm_bindgen::{prelude::*, JsCast}; -use web_sys::{window, CloseEvent, Event, MessageEvent, WebSocket}; +use web_sys::{CloseEvent, Event, MessageEvent, WebSocket}; + +use crate::web_context::WebContext; + +/// Arbitrary, maximum amount we are willing to buffer before we throttle our user. +const MAX_BUFFER: usize = 1024 * 1024; + +thread_local! { + /// Global web context for abstracting the `Window` vs the `WorkerGlobalScope` API + static GLOBAL_WEB_CONTEXT: WebContext = WebContext::new(); +} /// A Websocket transport that can be used in a wasm environment. /// @@ -61,9 +73,6 @@ pub struct Transport { _private: (), } -/// Arbitrary, maximum amount we are willing to buffer before we throttle our user. -const MAX_BUFFER: usize = 1024 * 1024; - impl libp2p_core::Transport for Transport { type Output = Connection; type Error = Error; @@ -300,13 +309,14 @@ impl Connection { } } }); - let buffered_amount_low_interval = window() - .expect("to have a window") - .set_interval_with_callback_and_timeout_and_arguments( - on_buffered_amount_low_closure.as_ref().unchecked_ref(), - 100, // Chosen arbitrarily and likely worth tuning. Due to low impact of the /ws transport, no further effort was invested at the time. - &Array::new(), - ) + let buffered_amount_low_interval = GLOBAL_WEB_CONTEXT + .with(|g| { + g.set_interval_with_callback_and_timeout_and_arguments( + on_buffered_amount_low_closure.as_ref().unchecked_ref(), + 100, // Chosen arbitrarily and likely worth tuning. Due to low impact of the /ws transport, no further effort was invested at the time. + &Array::new(), + ) + }) .expect("to be able to set an interval"); Self { @@ -439,8 +449,7 @@ impl Drop for Connection { .close_with_code_and_reason(GO_AWAY_STATUS_CODE, "connection dropped"); } - window() - .expect("to have a window") - .clear_interval_with_handle(self.inner.buffered_amount_low_interval) + GLOBAL_WEB_CONTEXT + .with(|g| g.clear_interval_with_handle(self.inner.buffered_amount_low_interval)); } }