Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setworkerurl #335

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading