From 36e37ab3e2df1bc9c8867cda04287656439fcaaf Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 15 Nov 2023 11:58:24 -0800 Subject: [PATCH 1/5] Added setworkerurl --- src/lib.rs | 6 ++++++ src/tasks/worker.js | 2 +- src/tasks/worker_handle.rs | 8 +++++++- src/utils.rs | 11 ++++++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d4d88c287d..075c4d34624 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,3 +40,9 @@ 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: JsString) { + crate::utils::set_worker_url(url); +} 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..0b67208d61e 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -148,11 +148,17 @@ 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 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(), diff --git a/src/utils.rs b/src/utils.rs index 38cd4814e03..3ce949bef15 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::{ num::NonZeroUsize, }; -use js_sys::{JsString, Promise}; +use js_sys::{JsString, Promise, global}; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{Window, WorkerGlobalScope}; @@ -252,3 +252,12 @@ pub(crate) fn js_record_of_strings(obj: &js_sys::Object) -> Result Date: Wed, 15 Nov 2023 12:03:14 -0800 Subject: [PATCH 2/5] Fixed setWorkerUrl --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 075c4d34624..0beb6755684 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,6 +43,6 @@ fn on_start() { #[wasm_bindgen(js_name = setWorkerUrl)] -pub fn set_worker_url(url: JsString) { +pub fn set_worker_url(url: js_sys::JsString) { crate::utils::set_worker_url(url); } From bebfd613a2fa34ef6733565d73b558df35d5abfd Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 15 Nov 2023 12:04:09 -0800 Subject: [PATCH 3/5] Fixed suggestion --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index 3ce949bef15..031cab8128f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -259,5 +259,5 @@ pub(crate) fn set_worker_url(url: JsString) { &global(), &JsString::from("customWorkerUrl"), &url, - ); + ).expect("Can't set worker url in the global JS scope"); } From 6cee243801d95587243ea56e0519024eac7f30ab Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 15 Nov 2023 12:04:31 -0800 Subject: [PATCH 4/5] Fixed linting --- src/lib.rs | 1 - src/tasks/worker_handle.rs | 11 +++++++++-- src/utils.rs | 10 +++------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0beb6755684..925f6778d4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,6 @@ fn on_start() { console_error_panic_hook::set_once(); } - #[wasm_bindgen(js_name = setWorkerUrl)] pub fn set_worker_url(url: js_sys::JsString) { crate::utils::set_worker_url(url); diff --git a/src/tasks/worker_handle.rs b/src/tasks/worker_handle.rs index 0b67208d61e..198aaf1a930 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -155,8 +155,15 @@ static WORKER_URL: Lazy = Lazy::new(|| { } 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()); + 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 script = include_str!("worker.js").replace("$IMPORT_META_URL", &import_url); diff --git a/src/utils.rs b/src/utils.rs index 031cab8128f..a925ab9b0db 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::{ num::NonZeroUsize, }; -use js_sys::{JsString, Promise, global}; +use js_sys::{global, JsString, Promise}; use wasm_bindgen::{JsCast, JsValue}; use web_sys::{Window, WorkerGlobalScope}; @@ -253,11 +253,7 @@ pub(crate) fn js_record_of_strings(obj: &js_sys::Object) -> Result Date: Tue, 21 Nov 2023 21:46:06 +0800 Subject: [PATCH 5/5] Store the custom worker URL in a `static` variable --- src/lib.rs | 6 +++++- src/tasks/worker_handle.rs | 19 ++++--------------- src/utils.rs | 7 +------ 3 files changed, 10 insertions(+), 22 deletions(-) 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