diff --git a/examples/node/example.mjs b/examples/node/example.mjs index 6c41c339..ba1797ff 100644 --- a/examples/node/example.mjs +++ b/examples/node/example.mjs @@ -28,13 +28,13 @@ process.stdin.on("data", data => stdin?.write(encoder.encode(data))); // Replace __filename with "/Users/syrusakbary/Development/wasmer-js/node_modules/.deno/web-worker@1.3.0/node_modules/web-worker/node.js" async function run() { - // await init({log: "trace"}); - await init(); + await init({log: "trace"}); + // await init(); -let cowsay = await Wasmer.fromRegistry("python/python") +let cowsay = await Wasmer.fromRegistry("cowsay") -// let instance = await cowsay.entrypoint.run({args: ["hello world"]}); -let instance = await cowsay.entrypoint.run({args: []}); +let instance = await cowsay.entrypoint.run({args: ["hello world"]}); +// let instance = await cowsay.entrypoint.run({args: []}); await connectStreams(instance); // const output = await instance.wait(); diff --git a/src-js/index.ts b/src-js/index.ts index 6c52af8d..17df9ca7 100644 --- a/src-js/index.ts +++ b/src-js/index.ts @@ -7,6 +7,7 @@ import load, { ThreadPoolWorker, initializeLogger, setWorkerUrl, + setSDKUrl, } from "../pkg/wasmer_js"; export type WasmerRegistryConfig = { @@ -18,6 +19,7 @@ export type WasmerInitInput = { module?: InitInput | Promise; memory?: WebAssembly.Memory; workerUrl?: string | URL; + sdkUrl?: string | URL; log?: string; } & WasmerRegistryConfig; @@ -62,6 +64,9 @@ export const init = async ( if (initValue.workerUrl) { setWorkerUrl(initValue.workerUrl.toString()); } + if (initValue.sdkUrl) { + setSDKUrl(initValue.sdkUrl.toString()); + } return output; }; diff --git a/src-js/node.ts b/src-js/node.ts index 8d19dda8..400e913f 100644 --- a/src-js/node.ts +++ b/src-js/node.ts @@ -1,5 +1,7 @@ export * from "./"; +// import { Worker } from "node:worker_threads"; import Worker from "web-worker"; +import { fileURLToPath } from 'node:url'; import { init as load, InitOutput, WasmerInitInput, VolumeTree } from "./"; import fs from "node:fs/promises"; import path from "node:path"; @@ -22,6 +24,12 @@ export const init = async ( const path = new URL("wasmer_js_bg.wasm", import.meta.url).pathname; initValue.module = await fs.readFile(path); } + if (!initValue.workerUrl) { + initValue.workerUrl = fileURLToPath(new URL("worker.mjs", import.meta.url)); + } + if (!initValue.sdkUrl) { + initValue.sdkUrl = fileURLToPath(new URL("node.mjs", import.meta.url)); + } return load(initValue); }; diff --git a/src-js/worker.js b/src-js/worker.js index 43b9ca84..2b9241de 100644 --- a/src-js/worker.js +++ b/src-js/worker.js @@ -14,8 +14,8 @@ let handleMessage = async data => { globalThis.onmessage = async ev => { if (ev.data.type == "init") { - const { memory, module, id, import_url } = ev.data; - const imported = await import(import_url); + const { memory, module, id, sdkUrl } = ev.data; + const imported = await import(sdkUrl); // 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 a4f3ff59..78dd4a90 100644 --- a/src/tasks/worker_handle.rs +++ b/src/tasks/worker_handle.rs @@ -24,8 +24,7 @@ impl WorkerHandle { pub(crate) fn spawn(worker_id: u32, sender: Scheduler) -> Result { let name = format!("worker-{worker_id}"); - // let worker_url = WORKER_URL; - let worker_url = "/Users/syrusakbary/Development/wasmer-js/dist/worker.mjs"; + let worker_url = worker_url(); let worker = web_sys::Worker::new_with_options( &worker_url, web_sys::WorkerOptions::new().name(&name).type_(web_sys::WorkerType::Module), @@ -136,8 +135,8 @@ fn init_message(id: u32) -> Result { 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()), + &JsString::from("sdkUrl"), + &JsValue::from(sdk_url()), )?; js_sys::Reflect::set( &msg, @@ -148,24 +147,26 @@ fn init_message(id: u32) -> Result { Ok(msg.into()) } -/// 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" { - #[wasm_bindgen(js_namespace = ["import", "meta"], js_name = url)] - static IMPORT_META_URL: String; - } +/// The URL used by the bootstrapping script to import the Wasmer SDK. +fn sdk_url() -> String { + let sdk_url = crate::CUSTOM_SDK_URL.lock().unwrap(); + let sdk_url = sdk_url.as_deref().unwrap_or(DEFAULT_WORKER_URL.as_str()); + + sdk_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()); - import_url.to_string() +/// The URL user for the worker. +fn worker_url() -> String { + let worker_url = crate::CUSTOM_WORKER_URL.lock().unwrap(); + let worker_url = worker_url.as_deref().unwrap_or(DEFAULT_WORKER_URL.as_str()); + + worker_url.to_string() } + /// A data URL containing our worker's bootstrap script. -static WORKER_URL: Lazy = Lazy::new(|| { +static DEFAULT_WORKER_URL: Lazy = Lazy::new(|| { let script = include_str!("../../src-js/worker.js"); let blob = web_sys::Blob::new_with_u8_array_sequence_and_options( @@ -174,5 +175,5 @@ static WORKER_URL: Lazy = Lazy::new(|| { ) .unwrap(); - web_sys::Url::create_object_url_with_blob(&blob).unwrap() + web_sys::Url::create_object_url_with_blob(&blob).unwrap_or("worker.mjs".to_string()) });