Skip to content

Commit

Permalink
Fix the types in node.[m]js
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Dec 10, 2024
1 parent 7ca38c5 commit 8e879b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 9 additions & 5 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ function loadModuleSync() {
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});

return initInstance(instance);
initInstance(instance);

// @ts-expect-error: Typescript doesn't know what the instance exports exactly
return instance.exports;
}

/**
Expand All @@ -81,27 +84,28 @@ function loadModuleSync() {
*/
async function loadModuleAsync() {
const bytes = await readFile(filename);
const instantiatedSource = await WebAssembly.instantiate(bytes, {
const { instance } = await WebAssembly.instantiate(bytes, {
// @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});

return initInstance(instantiatedSource.instance);
initInstance(instance);

// @ts-expect-error: Typescript doesn't know what the instance exports exactly
return instance.exports;
}

/**
* Initializes the WASM module and returns the exports from the WASM module.
*
* @param {WebAssembly.Instance} instance
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}
*/
function initInstance(instance) {
if (initialised) throw new Error("initInstance called twice");
bindings.__wbg_set_wasm(instance.exports);
// @ts-expect-error: Typescript doesn't know what the instance exports exactly
instance.exports.__wbindgen_start();
initialised = true;
return instance.exports;
}

/**
Expand Down
16 changes: 10 additions & 6 deletions node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let initialised = false;
*
* It will throw if there is an attempt to load the module asynchronously running
*
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}
*/
function loadModuleSync() {
if (modPromise) throw new Error("The WASM module is being loaded asynchronously but hasn't finished");
Expand All @@ -71,13 +71,16 @@ function loadModuleSync() {
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});

return initInstance(instance);
initInstance(instance);

// @ts-expect-error: Typescript doesn't know what the module exports are
return instance.exports;
}

/**
* Loads and instantiates the WASM module asynchronously
*
* @returns {Promise<typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")>}
* @returns {Promise<typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")>}
*/
async function loadModuleAsync() {
const bytes = await readFile(filename);
Expand All @@ -86,22 +89,23 @@ async function loadModuleAsync() {
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});

return initInstance(instance);
initInstance(instance);

// @ts-expect-error: Typescript doesn't know what the module exports are
return instance.exports;
}

/**
* Initializes the WASM module and returns the exports from the WASM module.
*
* @param {WebAssembly.Instance} instance
* @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}
*/
function initInstance(instance) {
if (initialised) throw new Error("initInstance called twice");
bindings.__wbg_set_wasm(instance.exports);
// @ts-expect-error: Typescript doesn't know what the module exports are
instance.exports.__wbindgen_start();
initialised = true;
return instance.exports;
}

/**
Expand Down

0 comments on commit 8e879b4

Please sign in to comment.