Skip to content

Commit 2cb3d34

Browse files
committed
Add more JS/TS Framework support by adding the ability to choose the URL of wasm files.
Signed-off-by: Danilo Bassi <[email protected]>
1 parent 3f74785 commit 2cb3d34

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

index.cjs

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs");
2323

24-
const moduleUrl = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm");
24+
const defaultURL = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm");
2525

2626
// We want to throw an error if the user tries to use the bindings before
2727
// calling `initAsync`.
@@ -47,10 +47,11 @@ let modPromise = null;
4747
/**
4848
* Loads and instantiates the WASM module asynchronously
4949
*
50+
* @param {URL} url - The URL to fetch the WebAssembly module from
5051
* @returns {Promise<void>}
5152
*/
52-
async function loadModuleAsync() {
53-
const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), {
53+
async function loadModuleAsync(url) {
54+
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
5455
// @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
5556
"./matrix_sdk_crypto_wasm_bg.js": bindings,
5657
});
@@ -65,10 +66,11 @@ async function loadModuleAsync() {
6566
*
6667
* Returns a promise which will resolve once the other methods are ready.
6768
*
69+
* @param {URL} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
6870
* @returns {Promise<void>}
6971
*/
70-
async function initAsync() {
71-
if (!modPromise) modPromise = loadModuleAsync();
72+
async function initAsync(url = defaultURL) {
73+
if (!modPromise) modPromise = loadModuleAsync(url);
7274
await modPromise;
7375
}
7476

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export * from "./pkg/matrix_sdk_crypto_wasm.js";
4343
* Load the WebAssembly module in the background, if it has not already been loaded.
4444
*
4545
* Returns a promise which will resolve once the other methods are ready.
46-
*
46+
* @param {URL} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
4747
* @returns {Promise<void>}
4848
*/
49-
export declare function initAsync(): Promise<void>;
49+
export declare function initAsync(url?: URL): Promise<void>;
5050

5151
// The auto-generated typescript definitions are a good start, but could do with tightening up in a lot of areas.
5252
// The following is a manually-curated set of typescript definitions.

index.mjs

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js";
2323

24-
const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url);
24+
const defaultURL = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url);
2525

2626
// Although we could simply instantiate the WASM at import time with a top-level `await`,
2727
// we avoid that, to make it easier for callers to delay loading the WASM (and instead
@@ -52,10 +52,11 @@ let modPromise = null;
5252
/**
5353
* Loads and instantiates the WASM module asynchronously
5454
*
55+
* @param {URL} url - The URL to fetch the WebAssembly module from
5556
* @returns {Promise<void>}
5657
*/
57-
async function loadModuleAsync() {
58-
const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), {
58+
async function loadModuleAsync(url) {
59+
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
5960
// @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
6061
"./matrix_sdk_crypto_wasm_bg.js": bindings,
6162
});
@@ -70,10 +71,11 @@ async function loadModuleAsync() {
7071
*
7172
* Returns a promise which will resolve once the other methods are ready.
7273
*
74+
* @param {URL} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
7375
* @returns {Promise<void>}
7476
*/
75-
export async function initAsync() {
76-
if (!modPromise) modPromise = loadModuleAsync();
77+
export async function initAsync(url = defaultURL) {
78+
if (!modPromise) modPromise = loadModuleAsync(url);
7779
await modPromise;
7880
}
7981

0 commit comments

Comments
 (0)