Skip to content

Commit

Permalink
Typecheck JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Nov 29, 2024
1 parent 0d699f6 commit accdcf7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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();
}
Expand Down
13 changes: 10 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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();
}
Expand Down
9 changes: 8 additions & 1 deletion node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit accdcf7

Please sign in to comment.