diff --git a/babel.config.js b/babel.config.cjs similarity index 100% rename from babel.config.js rename to babel.config.cjs diff --git a/index-wasm-esm.mjs b/index-wasm-esm.js similarity index 100% rename from index-wasm-esm.mjs rename to index-wasm-esm.js diff --git a/index.mjs b/index.cjs similarity index 68% rename from index.mjs rename to index.cjs index b792f114f..9b27f4e02 100644 --- a/index.mjs +++ b/index.cjs @@ -15,21 +15,16 @@ // @ts-check /** - * This is the entrypoint on non-node ESM environments. + * This is the entrypoint on non-node CommonJS environments. * `asyncLoad` will load the WASM module using a `fetch` call. */ -import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; +const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); -const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url); +const moduleUrl = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm"); -// 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 -// wait until `initAsync` is called). (Also, Safari 14 doesn't support top-level `await`.) -// -// However, having done so, there is no way to synchronously load the WASM if the user ends -// up using the bindings before calling `initAsync` (unlike under Node.js), so we just throw -// an error. +// We want to throw an error if the user tries to use the bindings before +// calling `initAsync`. bindings.__wbg_set_wasm( new Proxy( {}, @@ -44,7 +39,7 @@ bindings.__wbg_set_wasm( ); /** - * Stores a promise of the `loadModuleAsync` call + * Stores a promise of the `loadModule` call * @type {Promise | null} */ let modPromise = null; @@ -72,10 +67,13 @@ async function loadModuleAsync() { * * @returns {Promise} */ -export async function initAsync() { +async function initAsync() { if (!modPromise) modPromise = loadModuleAsync(); await modPromise; } -// Re-export everything from the generated javascript wrappers -export * from "./pkg/matrix_sdk_crypto_wasm_bg.js"; +module.exports = { + // Re-export everything from the generated javascript wrappers + ...bindings, + initAsync, +}; diff --git a/index.js b/index.js index 9b27f4e02..b792f114f 100644 --- a/index.js +++ b/index.js @@ -15,16 +15,21 @@ // @ts-check /** - * This is the entrypoint on non-node CommonJS environments. + * This is the entrypoint on non-node ESM environments. * `asyncLoad` will load the WASM module using a `fetch` call. */ -const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); +import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; -const moduleUrl = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm"); +const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url); -// We want to throw an error if the user tries to use the bindings before -// calling `initAsync`. +// 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 +// wait until `initAsync` is called). (Also, Safari 14 doesn't support top-level `await`.) +// +// However, having done so, there is no way to synchronously load the WASM if the user ends +// up using the bindings before calling `initAsync` (unlike under Node.js), so we just throw +// an error. bindings.__wbg_set_wasm( new Proxy( {}, @@ -39,7 +44,7 @@ bindings.__wbg_set_wasm( ); /** - * Stores a promise of the `loadModule` call + * Stores a promise of the `loadModuleAsync` call * @type {Promise | null} */ let modPromise = null; @@ -67,13 +72,10 @@ async function loadModuleAsync() { * * @returns {Promise} */ -async function initAsync() { +export async function initAsync() { if (!modPromise) modPromise = loadModuleAsync(); await modPromise; } -module.exports = { - // Re-export everything from the generated javascript wrappers - ...bindings, - initAsync, -}; +// Re-export everything from the generated javascript wrappers +export * from "./pkg/matrix_sdk_crypto_wasm_bg.js"; diff --git a/node.mjs b/node.cjs similarity index 81% rename from node.mjs rename to node.cjs index 65c9cca4d..190b4fd2e 100644 --- a/node.mjs +++ b/node.cjs @@ -15,16 +15,16 @@ // @ts-check /** - * This is the entrypoint on node-compatible ESM environments. + * This is the entrypoint on node-compatible CommonJS environments. * `asyncLoad` will use `fs.readFile` to load the WASM module. */ -import { fileURLToPath } from "node:url"; -import { readFileSync } from "node:fs"; -import { readFile } from "node:fs/promises"; -import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; +const { readFileSync } = require("node:fs"); +const { readFile } = require("node:fs/promises"); +const path = require("node:path"); +const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); -const filename = fileURLToPath(new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url)); +const filename = path.join(__dirname, "pkg/matrix_sdk_crypto_wasm_bg.wasm"); // In node environments, we want to automatically load the WASM module // synchronously if the consumer did not call `initAsync`. To do so, we install @@ -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.ts")} + * @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")} */ function loadModuleSync() { if (modPromise) throw new Error("The WASM module is being loaded asynchronously but hasn't finished"); @@ -73,14 +73,14 @@ function loadModuleSync() { initInstance(instance); - // @ts-expect-error: Typescript doesn't know what the module exports are + // @ts-expect-error: Typescript doesn't know what the instance exports exactly return instance.exports; } /** * Loads and instantiates the WASM module asynchronously * - * @returns {Promise} + * @returns {Promise} */ async function loadModuleAsync() { const bytes = await readFile(filename); @@ -91,7 +91,7 @@ async function loadModuleAsync() { initInstance(instance); - // @ts-expect-error: Typescript doesn't know what the module exports are + // @ts-expect-error: Typescript doesn't know what the instance exports exactly return instance.exports; } @@ -103,7 +103,7 @@ async function loadModuleAsync() { 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 + // @ts-expect-error: Typescript doesn't know what the instance exports exactly instance.exports.__wbindgen_start(); initialised = true; } @@ -115,11 +115,14 @@ function initInstance(instance) { * * @returns {Promise} */ -export async function initAsync() { +async function initAsync() { if (initialised) return; if (!modPromise) modPromise = loadModuleAsync(); await modPromise; } -// Re-export everything from the generated javascript wrappers -export * from "./pkg/matrix_sdk_crypto_wasm_bg.js"; +module.exports = { + // Re-export everything from the generated javascript wrappers + ...bindings, + initAsync, +}; diff --git a/node.js b/node.js index 190b4fd2e..65c9cca4d 100644 --- a/node.js +++ b/node.js @@ -15,16 +15,16 @@ // @ts-check /** - * This is the entrypoint on node-compatible CommonJS environments. + * This is the entrypoint on node-compatible ESM environments. * `asyncLoad` will use `fs.readFile` to load the WASM module. */ -const { readFileSync } = require("node:fs"); -const { readFile } = require("node:fs/promises"); -const path = require("node:path"); -const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs"); +import { fileURLToPath } from "node:url"; +import { readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js"; -const filename = path.join(__dirname, "pkg/matrix_sdk_crypto_wasm_bg.wasm"); +const filename = fileURLToPath(new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url)); // In node environments, we want to automatically load the WASM module // synchronously if the consumer did not call `initAsync`. To do so, we install @@ -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"); @@ -73,14 +73,14 @@ function loadModuleSync() { initInstance(instance); - // @ts-expect-error: Typescript doesn't know what the instance exports exactly + // @ts-expect-error: Typescript doesn't know what the module exports are return instance.exports; } /** * Loads and instantiates the WASM module asynchronously * - * @returns {Promise} + * @returns {Promise} */ async function loadModuleAsync() { const bytes = await readFile(filename); @@ -91,7 +91,7 @@ async function loadModuleAsync() { initInstance(instance); - // @ts-expect-error: Typescript doesn't know what the instance exports exactly + // @ts-expect-error: Typescript doesn't know what the module exports are return instance.exports; } @@ -103,7 +103,7 @@ async function loadModuleAsync() { 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 + // @ts-expect-error: Typescript doesn't know what the module exports are instance.exports.__wbindgen_start(); initialised = true; } @@ -115,14 +115,11 @@ function initInstance(instance) { * * @returns {Promise} */ -async function initAsync() { +export async function initAsync() { if (initialised) return; if (!modPromise) modPromise = loadModuleAsync(); await modPromise; } -module.exports = { - // Re-export everything from the generated javascript wrappers - ...bindings, - initAsync, -}; +// Re-export everything from the generated javascript wrappers +export * from "./pkg/matrix_sdk_crypto_wasm_bg.js"; diff --git a/package.json b/package.json index cbd7a27a0..4ce4ddaa1 100644 --- a/package.json +++ b/package.json @@ -18,21 +18,22 @@ "ruma", "nio" ], + "type": "module", "exports": { ".": { "matrix-org:wasm-esm": { "types": "./index.d.ts", - "default": "./index-wasm-esm.mjs" + "default": "./index-wasm-esm.js" }, "require": { "types": "./index.d.ts", - "node": "./node.js", - "default": "./index.js" + "node": "./node.cjs", + "default": "./index.cjs" }, "import": { "types": "./index.d.ts", - "node": "./node.mjs", - "default": "./index.mjs" + "node": "./node.js", + "default": "./index.js" } } },