From accdcf7ce3f7f100c2f34b623f22d06950be67ec Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 28 Nov 2024 16:58:26 +0100 Subject: [PATCH] Typecheck JS files --- index.js | 17 ++++++++++++----- index.mjs | 13 ++++++++++--- node.js | 9 ++++++++- node.mjs | 8 +++++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 8f8ef08ea..e17e4ee17 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// @ts-check + // This is the entrypoint on non-node CommonJS environments. // `asyncLoad` will load the WASM module using a `fetch` call. const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); @@ -32,13 +34,14 @@ bindings.__wbg_set_wasm( ), ); +/** @type {WebAssembly.Module} */ let mod; async function loadModule() { if (mod) return mod; if (typeof WebAssembly.compileStreaming === "function") { mod = await WebAssembly.compileStreaming(fetch(moduleUrl)); - return mod; + return; } // Fallback to fetch and compile @@ -47,15 +50,19 @@ async function loadModule() { throw new Error(`Failed to fetch wasm module: ${moduleUrl}`); } const bytes = await response.arrayBuffer(); - mod = await WebAssembly.compile(response); - return mod; + mod = await WebAssembly.compile(bytes); } async function initAsync() { - const module = await loadModule(); - const instance = new WebAssembly.Instance(module, { + await loadModule(); + + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + // @ts-expect-error: Typescript doesn't know what the instance exports exactly + const instance = new WebAssembly.Instance(mod, { + // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); + bindings.__wbg_set_wasm(instance.exports); instance.exports.__wbindgen_start(); } diff --git a/index.mjs b/index.mjs index 5c868c2e9..7d49dad3a 100644 --- a/index.mjs +++ b/index.mjs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// @ts-check + // This is the entrypoint on non-node ESM environments (such as Element Web). // `asyncLoad` will load the WASM module using a `fetch` call. import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; @@ -33,13 +35,14 @@ bindings.__wbg_set_wasm( const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url); +/** @type {WebAssembly.Module} */ let mod; async function loadModule() { if (mod) return mod; if (typeof WebAssembly.compileStreaming === "function") { mod = await WebAssembly.compileStreaming(fetch(moduleUrl)); - return mod; + return; } // Fallback to fetch and compile @@ -49,14 +52,18 @@ async function loadModule() { } const bytes = await response.arrayBuffer(); mod = await WebAssembly.compile(bytes); - return mod; } export async function initAsync() { - const mod = await loadModule(); + await loadModule(); + + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { + // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); + bindings.__wbg_set_wasm(instance.exports); instance.exports.__wbindgen_start(); } diff --git a/node.js b/node.js index 6f26a32fc..6ae3cf557 100644 --- a/node.js +++ b/node.js @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// @ts-check + // This is the entrypoint on node-compatible CommonJS environments. // `asyncLoad` will use `fs.readFile` to load the WASM module. const { readFileSync } = require("node:fs"); @@ -30,12 +32,13 @@ bindings.__wbg_set_wasm( { get(_target, prop) { loadModuleSync(); - return initInstance(mod)[prop]; + return initInstance()[prop]; }, }, ), ); +/** @type {WebAssembly.Module} */ let mod; function loadModuleSync() { if (mod) return mod; @@ -50,9 +53,13 @@ async function loadModule() { } function initInstance() { + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { + // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); + bindings.__wbg_set_wasm(instance.exports); instance.exports.__wbindgen_start(); return instance.exports; diff --git a/node.mjs b/node.mjs index 3866e7bea..1324d6f0a 100644 --- a/node.mjs +++ b/node.mjs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +// @ts-check + // This is the entrypoint on node-compatible ESM environments. // `asyncLoad` will use `fs.readFile` to load the WASM module. import { fileURLToPath } from "node:url"; @@ -30,12 +32,13 @@ bindings.__wbg_set_wasm( { get(_target, prop) { loadModuleSync(); - return initInstance(mod)[prop]; + return initInstance()[prop]; }, }, ), ); +/** @type {WebAssembly.Module} */ let mod; function loadModuleSync() { if (mod) return mod; @@ -50,7 +53,10 @@ async function loadModule() { } function initInstance() { + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { + // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type "./matrix_sdk_crypto_wasm_bg.js": bindings, }); bindings.__wbg_set_wasm(instance.exports);