Skip to content

Commit

Permalink
Merge pull request wasmerio#335 from wasmerio/wasix-workerurl
Browse files Browse the repository at this point in the history
Added setworkerurl
  • Loading branch information
Michael Bryan authored Nov 21, 2023
2 parents 30f7a45 + 816e16a commit 3bb2711
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<Mutex<Option<String>>> = Lazy::new(Mutex::default);

#[wasm_bindgen]
pub fn wat2wasm(wat: String) -> Result<js_sys::Uint8Array, utils::Error> {
Expand All @@ -40,3 +44,8 @@ pub fn wat2wasm(wat: String) -> Result<js_sys::Uint8Array, utils::Error> {
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());
}
2 changes: 1 addition & 1 deletion src/tasks/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/worker_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ static WORKER_URL: Lazy<String> = 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(),
Expand Down

0 comments on commit 3bb2711

Please sign in to comment.