diff --git a/src/lib.rs b/src/lib.rs index 4d4d88c287d..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 { @@ -40,3 +44,8 @@ pub fn wat2wasm(wat: String) -> Result { fn on_start() { console_error_panic_hook::set_once(); } + +#[wasm_bindgen(js_name = setWorkerUrl)] +pub fn set_worker_url(url: js_sys::JsString) { + *CUSTOM_WORKER_URL.lock().unwrap() = Some(url.into()); +} diff --git a/src/tasks/worker.js b/src/tasks/worker.js index ee3c4550642..c3de472984d 100644 --- a/src/tasks/worker.js +++ b/src/tasks/worker.js @@ -15,7 +15,7 @@ let handleMessage = async data => { globalThis.onmessage = async ev => { if (ev.data.type == "init") { const { memory, module, id } = ev.data; - const imported = await import("$IMPORT_META_URL"); + const imported = await import(new URL("$IMPORT_META_URL", self.location.origin)); // HACK: How we load our imports will change depending on how the code // is deployed. If we are being used in "wasm-pack test" then we can diff --git a/src/tasks/worker_handle.rs b/src/tasks/worker_handle.rs index d154c0ca80a..e429f35b641 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -150,9 +150,11 @@ static WORKER_URL: Lazy = Lazy::new(|| { static IMPORT_META_URL: String; } - tracing::trace!(import_url = IMPORT_META_URL.as_str()); + 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_META_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(),