From 2cb3d349d8e582e9092c8426c92b990d4cb18800 Mon Sep 17 00:00:00 2001 From: Danilo Bassi Date: Thu, 15 May 2025 09:44:57 -0300 Subject: [PATCH 1/2] Add more JS/TS Framework support by adding the ability to choose the URL of wasm files. Signed-off-by: Danilo Bassi --- index.cjs | 12 +++++++----- index.d.ts | 4 ++-- index.mjs | 12 +++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/index.cjs b/index.cjs index 9b27f4e02..cddec2aad 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} 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} [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..46946b2b7 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} [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): 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..442c75dfe 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} 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} [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; } From 97c512d4fd34d87b65109d35a09b3faddd168bb7 Mon Sep 17 00:00:00 2001 From: Danilo Bassi Date: Fri, 16 May 2025 09:55:02 -0300 Subject: [PATCH 2/2] Fix: initAsync param can be string or URL Signed-off-by: Danilo Bassi --- index.cjs | 4 ++-- index.d.ts | 4 ++-- index.mjs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.cjs b/index.cjs index cddec2aad..4a6cc27e7 100644 --- a/index.cjs +++ b/index.cjs @@ -47,7 +47,7 @@ let modPromise = null; /** * Loads and instantiates the WASM module asynchronously * - * @param {URL} url - The URL to fetch the WebAssembly module from + * @param {URL | string} url - The URL to fetch the WebAssembly module from * @returns {Promise} */ async function loadModuleAsync(url) { @@ -66,7 +66,7 @@ async function loadModuleAsync(url) { * * Returns a promise which will resolve once the other methods are ready. * - * @param {URL} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. + * @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(url = defaultURL) { diff --git a/index.d.ts b/index.d.ts index 46946b2b7..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} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. + * @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(url?: URL): 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 442c75dfe..cf58971c7 100644 --- a/index.mjs +++ b/index.mjs @@ -52,7 +52,7 @@ let modPromise = null; /** * Loads and instantiates the WASM module asynchronously * - * @param {URL} url - The URL to fetch the WebAssembly module from + * @param {URL | string} url - The URL to fetch the WebAssembly module from * @returns {Promise} */ async function loadModuleAsync(url) { @@ -71,7 +71,7 @@ async function loadModuleAsync(url) { * * Returns a promise which will resolve once the other methods are ready. * - * @param {URL} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used. + * @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(url = defaultURL) {