diff --git a/src/lib.rs b/src/lib.rs index 925f6778d4d..dbc9cd580af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,8 @@ mod tasks; mod utils; mod ws; +use std::sync::Mutex; + pub use crate::{ container::{Container, Manifest, Volume}, facade::{SpawnConfig, Wasmer, WasmerConfig}, @@ -25,10 +27,12 @@ pub use crate::{ runtime::Runtime, }; +use once_cell::sync::Lazy; use wasm_bindgen::prelude::wasm_bindgen; pub(crate) const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); pub(crate) const DEFAULT_RUST_LOG: &[&str] = &["warn"]; +pub(crate) static CUSTOM_WORKER_URL: Lazy>> = Lazy::new(Mutex::default); #[wasm_bindgen] pub fn wat2wasm(wat: String) -> Result { @@ -43,5 +47,5 @@ fn on_start() { #[wasm_bindgen(js_name = setWorkerUrl)] pub fn set_worker_url(url: js_sys::JsString) { - crate::utils::set_worker_url(url); + *CUSTOM_WORKER_URL.lock().unwrap() = Some(url.into()); } diff --git a/src/tasks/worker_handle.rs b/src/tasks/worker_handle.rs index 198aaf1a930..e429f35b641 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -148,24 +148,13 @@ static WORKER_URL: Lazy = Lazy::new(|| { extern "C" { #[wasm_bindgen(js_namespace = ["import", "meta"], js_name = url)] static IMPORT_META_URL: String; - - #[wasm_bindgen(js_namespace = ["globalThis"], js_name = customWorkerUrl)] - static CUSTOM_WORKER_URL: Option; - } - tracing::trace!(import_url = IMPORT_META_URL.as_str()); - tracing::trace!( - custom_worker_url = CUSTOM_WORKER_URL - .as_ref() - .unwrap_or(&"".to_string()) - .as_str() - ); - let import_url: String = CUSTOM_WORKER_URL - .to_owned() - .unwrap_or(IMPORT_META_URL.to_string()); + let import_url = crate::CUSTOM_WORKER_URL.lock().unwrap(); + let import_url = import_url.as_deref().unwrap_or(IMPORT_META_URL.as_str()); + tracing::trace!(import_url); - let script = include_str!("worker.js").replace("$IMPORT_META_URL", &import_url); + let script = include_str!("worker.js").replace("$IMPORT_META_URL", import_url); let blob = web_sys::Blob::new_with_u8_array_sequence_and_options( Array::from_iter([Uint8Array::from(script.as_bytes())]).as_ref(), diff --git a/src/utils.rs b/src/utils.rs index a925ab9b0db..38cd4814e03 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::{ num::NonZeroUsize, }; -use js_sys::{global, JsString, Promise}; +use js_sys::{JsString, Promise}; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{Window, WorkerGlobalScope}; @@ -252,8 +252,3 @@ pub(crate) fn js_record_of_strings(obj: &js_sys::Object) -> Result