Skip to content

Commit

Permalink
Merge pull request wasmerio#399 from wasmerio/sdk-78-pass-the-import-…
Browse files Browse the repository at this point in the history
…url-to-the-worker-bootstrap-script-via-the

Pass the import URL to the worker bootstrap script via the `init` message
  • Loading branch information
Michael Bryan authored Jan 8, 2024
2 parents dbc77c2 + 29ea682 commit 195e921
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/tasks/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ let handleMessage = async data => {

globalThis.onmessage = async ev => {
if (ev.data.type == "init") {
const { memory, module, id } = ev.data;
const { memory, module, id, import_url } = ev.data;
const imported = await import(
new URL("$IMPORT_META_URL", self.location.origin)
new URL(import_url, self.location.origin)
);

// HACK: How we load our imports will change depending on how the code
Expand Down
18 changes: 14 additions & 4 deletions src/tasks/worker_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ fn init_message(id: u32) -> Result<JsValue, JsValue> {
js_sys::Reflect::set(&msg, &JsString::from("type"), &JsString::from("init"))?;
js_sys::Reflect::set(&msg, &JsString::from("memory"), &wasm_bindgen::memory())?;
js_sys::Reflect::set(&msg, &JsString::from("id"), &JsValue::from(id))?;
js_sys::Reflect::set(
&msg,
&JsString::from("import_url"),
&JsValue::from(import_meta_url()),
)?;
js_sys::Reflect::set(
&msg,
&JsString::from("module"),
Expand All @@ -141,8 +146,9 @@ fn init_message(id: u32) -> Result<JsValue, JsValue> {
Ok(msg.into())
}

/// A data URL containing our worker's bootstrap script.
static WORKER_URL: Lazy<String> = Lazy::new(|| {
/// The URL used by the bootstrapping script to import the `wasm-bindgen` glue
/// code.
fn import_meta_url() -> String {
#[wasm_bindgen]
#[allow(non_snake_case)]
extern "C" {
Expand All @@ -152,9 +158,13 @@ static WORKER_URL: Lazy<String> = Lazy::new(|| {

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);
import_url.to_string()
}

/// A data URL containing our worker's bootstrap script.
static WORKER_URL: Lazy<String> = Lazy::new(|| {
let script = include_str!("worker.js");

let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(
Array::from_iter([Uint8Array::from(script.as_bytes())]).as_ref(),
Expand Down
2 changes: 1 addition & 1 deletion tests/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initialized = (async () => {
})();

describe("In-Memory Directory", function () {
this.beforeAll(async () => await initialized);
this.timeout("60s").beforeAll(async () => await initialized);

it("read empty dir", async () => {
const dir = new Directory();
Expand Down

0 comments on commit 195e921

Please sign in to comment.