From 8d30ac1ffca3899e40939f2fb1fcddfe028773ef Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Fri, 5 Jan 2024 17:55:54 +0800 Subject: [PATCH 1/2] refactor: Pass the bootstrap script's import URL in via the `init` message, rather than doing `string.replace()` on the script --- src/tasks/worker.js | 4 ++-- src/tasks/worker_handle.rs | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/tasks/worker.js b/src/tasks/worker.js index 992137210ef..18c5b979566 100644 --- a/src/tasks/worker.js +++ b/src/tasks/worker.js @@ -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 diff --git a/src/tasks/worker_handle.rs b/src/tasks/worker_handle.rs index 74934662f26..7ff6a6e5fa3 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -132,6 +132,11 @@ fn init_message(id: u32) -> Result { 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"), @@ -141,8 +146,9 @@ fn init_message(id: u32) -> Result { Ok(msg.into()) } -/// A data URL containing our worker's bootstrap script. -static WORKER_URL: Lazy = 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" { @@ -152,9 +158,13 @@ static WORKER_URL: Lazy = 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 = 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(), From 29ea68289b3e1af1df11e75ffc7199cf9f18c6a7 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Fri, 5 Jan 2024 17:56:06 +0800 Subject: [PATCH 2/2] Explicitly add a timeout to the directory tests --- tests/directory.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/directory.test.ts b/tests/directory.test.ts index 3bc69b0ffc0..9dfc3955ab5 100644 --- a/tests/directory.test.ts +++ b/tests/directory.test.ts @@ -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();