diff --git a/index.cjs b/index.cjs index 9b27f4e02..4a6cc27e7 100644 --- a/index.cjs +++ b/index.cjs @@ -21,7 +21,7 @@ const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); -const moduleUrl = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm"); +const defaultURL = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm"); // We want to throw an error if the user tries to use the bindings before // calling `initAsync`. @@ -47,10 +47,11 @@ let modPromise = null; /** * Loads and instantiates the WASM module asynchronously * + * @param {URL | string} url - The URL to fetch the WebAssembly module from * @returns {Promise} */ -async function loadModuleAsync() { - const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), { +async function loadModuleAsync(url) { + const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); @@ -65,10 +66,11 @@ async function loadModuleAsync() { * * Returns a promise which will resolve once the other methods are ready. * + * @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. * @returns {Promise} */ -async function initAsync() { - if (!modPromise) modPromise = loadModuleAsync(); +async function initAsync(url = defaultURL) { + if (!modPromise) modPromise = loadModuleAsync(url); await modPromise; } diff --git a/index.d.ts b/index.d.ts index 24b2fd4ae..12b211ed7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -43,10 +43,10 @@ export * from "./pkg/matrix_sdk_crypto_wasm.js"; * Load the WebAssembly module in the background, if it has not already been loaded. * * Returns a promise which will resolve once the other methods are ready. - * + * @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. * @returns {Promise} */ -export declare function initAsync(): Promise; +export declare function initAsync(url?: URL | string): Promise; // The auto-generated typescript definitions are a good start, but could do with tightening up in a lot of areas. // The following is a manually-curated set of typescript definitions. diff --git a/index.mjs b/index.mjs index b792f114f..cf58971c7 100644 --- a/index.mjs +++ b/index.mjs @@ -21,7 +21,7 @@ import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; -const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url); +const defaultURL = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url); // Although we could simply instantiate the WASM at import time with a top-level `await`, // we avoid that, to make it easier for callers to delay loading the WASM (and instead @@ -52,10 +52,11 @@ let modPromise = null; /** * Loads and instantiates the WASM module asynchronously * + * @param {URL | string} url - The URL to fetch the WebAssembly module from * @returns {Promise} */ -async function loadModuleAsync() { - const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), { +async function loadModuleAsync(url) { + const { instance } = await WebAssembly.instantiateStreaming(fetch(url), { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); @@ -70,10 +71,11 @@ async function loadModuleAsync() { * * Returns a promise which will resolve once the other methods are ready. * + * @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. * @returns {Promise} */ -export async function initAsync() { - if (!modPromise) modPromise = loadModuleAsync(); +export async function initAsync(url = defaultURL) { + if (!modPromise) modPromise = loadModuleAsync(url); await modPromise; }