Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Centralize web wasm loading

Manav Rathi72aaa3369aa7
diff --git a/web/apps/ensu/src/services/wasm.ts b/web/apps/ensu/src/services/wasm.ts
index 84f8de9ac6..2a93e303e7 100644
--- a/web/apps/ensu/src/services/wasm.ts
+++ b/web/apps/ensu/src/services/wasm.ts
@@ -5,6 +5,8 @@
* when needed (login/crypto flows), while Tauri uses native Rust bindings.
*/
+import { loadEnteWasm } from "ente-wasm/load";
+
export type EnteWasmModule = typeof import("ente-wasm");
export interface EncryptedBox {
@@ -303,7 +305,7 @@ export const enteWasm = async (): Promise<EnteCryptoAdapter> => {
if (isTauriRuntime()) {
return createTauriAdapter();
}
- const wasm = await import("ente-wasm");
+ const wasm = await loadEnteWasm();
return createWasmAdapter(wasm);
})();
@@ -313,7 +315,6 @@ export const enteWasm = async (): Promise<EnteCryptoAdapter> => {
export const ensureCryptoInit = async () => {
if (_cryptoInitDone) return;
const wasm = await enteWasm();
- // No-op for the pure Rust backend, but keeps the API symmetric.
await wasm.crypto_init();
_cryptoInitDone = true;
};
diff --git a/web/apps/locker/src/services/crypto.ts b/web/apps/locker/src/services/crypto.ts
index 4739c4d547..570e526264 100644
--- a/web/apps/locker/src/services/crypto.ts
+++ b/web/apps/locker/src/services/crypto.ts
@@ -15,7 +15,7 @@ import type {
EncryptedFile,
KeyPair,
} from "ente-base/crypto/types";
-import { ensureCryptoInit, enteWasm } from "./wasm";
+import { ensureWasmCryptoInit, loadEnteWasm } from "ente-wasm/load";
const shouldFallbackToLegacyBlobDecrypt = (error: unknown): boolean => {
if (!error || typeof error !== "object") {
@@ -70,8 +70,8 @@ export const decryptBox = async (
box: EncryptedBox,
key: Uint8Array | string,
): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_decrypt_box(
toB64String(box.encryptedData),
toB64String(box.nonce),
@@ -98,8 +98,8 @@ export const decryptMetadataJSON = async (
blob: EncryptedBlob,
key: Uint8Array | string,
): Promise<unknown> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const encryptedData = toB64String(blob.encryptedData);
const decryptionHeader = toB64String(blob.decryptionHeader);
const keyB64 = toB64String(key);
@@ -131,8 +131,8 @@ export const boxSealOpen = async (
encryptedData: string,
keyPair: KeyPair,
): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_box_seal_open(
encryptedData,
keyPair.publicKey,
@@ -144,8 +144,8 @@ export const boxSeal = async (
dataB64: string,
publicKeyB64: string,
): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_box_seal(dataB64, publicKeyB64);
};
@@ -163,14 +163,14 @@ export const boxSeal = async (
* Generate a random 32-byte key (base64).
*/
export const generateKey = async (): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_generate_key();
};
export const md5Base64 = async (data: Uint8Array): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_md5_base64(data);
};
@@ -182,8 +182,8 @@ export const encryptBox = async (
dataB64: string,
keyB64: string,
): Promise<{ encryptedData: string; nonce: string }> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const result = wasm.crypto_encrypt_box(dataB64, keyB64);
return { encryptedData: result.encrypted_data, nonce: result.nonce };
};
@@ -196,8 +196,8 @@ export const encryptBlob = async (
dataB64: string,
keyB64: string,
): Promise<{ encryptedData: string; decryptionHeader: string }> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const result = wasm.crypto_encrypt_blob(dataB64, keyB64);
return {
encryptedData: result.encrypted_data,
@@ -236,8 +236,8 @@ export interface StreamDecryptorHandle {
export const createStreamEncryptor =
async (): Promise<StreamEncryptorHandle> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const encryptor = new wasm.CryptoStreamEncryptor();
return {
@@ -253,8 +253,8 @@ export const createStreamDecryptor = async (
decryptionHeader: string,
keyB64: string,
): Promise<StreamDecryptorHandle> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const decryptor = new wasm.CryptoStreamDecryptor(decryptionHeader, keyB64);
return {
@@ -290,8 +290,8 @@ export interface EncryptedFileResult {
export const encryptFileStream = async (
dataB64: string,
): Promise<EncryptedFileResult> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const result = wasm.crypto_encrypt_stream(dataB64);
return {
encryptedData: result.encrypted_data,
@@ -311,8 +311,8 @@ export const encryptFileStreamWithKey = async (
dataB64: string,
keyB64: string,
): Promise<EncryptedFileResult> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const result = wasm.crypto_encrypt_stream_with_key(dataB64, keyB64);
return {
encryptedData: result.encrypted_data,
@@ -336,8 +336,8 @@ export const decryptStreamBytes = async (
file: EncryptedFile,
key: Uint8Array | string,
): Promise<Uint8Array> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const plaintextB64 = wasm.crypto_decrypt_stream(
toB64String(file.encryptedData),
toB64String(file.decryptionHeader),
diff --git a/web/apps/locker/src/services/wasm.ts b/web/apps/locker/src/services/wasm.ts
deleted file mode 100644
index a027d241d0..0000000000
--- a/web/apps/locker/src/services/wasm.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * Lazy loader for the `ente-wasm` package.
- *
- * We keep this behind a dynamic import so that the WASM bundle is only loaded
- * when needed (when crypto operations are first invoked).
- *
- * This follows the same pattern as `apps/ensu/src/services/wasm.ts` but
- * without the Tauri adapter since Locker is web-only.
- */
-
-type EnteWasmModule = typeof import("ente-wasm");
-
-let _wasmPromise: Promise<EnteWasmModule> | undefined;
-let _cryptoInitDone = false;
-
-/**
- * Return the lazily loaded `ente-wasm` module.
- *
- * The module is loaded once on first call and the same promise is reused for
- * subsequent calls.
- */
-export const enteWasm = async (): Promise<EnteWasmModule> => {
- _wasmPromise ??= import("ente-wasm");
- return _wasmPromise;
-};
-
-/**
- * Ensure the WASM crypto backend has been initialised.
- *
- * This is a no-op for the pure-Rust backend but keeps the API symmetric with
- * other implementations.
- */
-export const ensureCryptoInit = async () => {
- if (_cryptoInitDone) return;
- const wasm = await enteWasm();
- wasm.crypto_init();
- _cryptoInitDone = true;
-};
diff --git a/web/packages/accounts-rs/services/crypto.ts b/web/packages/accounts-rs/services/crypto.ts
index 26c2efdc4b..495613f1dc 100644
--- a/web/packages/accounts-rs/services/crypto.ts
+++ b/web/packages/accounts-rs/services/crypto.ts
@@ -1,4 +1,4 @@
-import { ensureCryptoInit, enteWasm } from "./wasm";
+import { ensureWasmCryptoInit, loadEnteWasm } from "ente-wasm/load";
export const deriveKeyInsufficientMemoryErrorMessage =
"Failed to derive key (insufficient memory)";
@@ -84,8 +84,8 @@ export const deriveKey = async (
opsLimit: number,
memLimit: number,
) => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.auth_derive_kek(password, saltB64, memLimit, opsLimit);
};
@@ -99,9 +99,9 @@ const normalizeDerivedKey = (result: WasmDerivedKey): DerivedKey => ({
export const deriveSensitiveKey = async (
password: string,
): Promise<DerivedKey> => {
- await ensureCryptoInit();
+ await ensureWasmCryptoInit();
try {
- const wasm = await enteWasm();
+ const wasm = await loadEnteWasm();
return normalizeDerivedKey(
wasm.auth_generate_sensitive_kek(password) as WasmDerivedKey,
);
@@ -113,9 +113,9 @@ export const deriveSensitiveKey = async (
export const deriveInteractiveKey = async (
password: string,
): Promise<DerivedKey> => {
- await ensureCryptoInit();
+ await ensureWasmCryptoInit();
try {
- const wasm = await enteWasm();
+ const wasm = await loadEnteWasm();
return normalizeDerivedKey(
wasm.auth_generate_interactive_kek(password) as WasmDerivedKey,
);
@@ -125,14 +125,14 @@ export const deriveInteractiveKey = async (
};
export const generateKey = async (): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_generate_key();
};
export const generateKeyPair = async (): Promise<KeyPair> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const keyPair = wasm.crypto_generate_keypair();
return { publicKey: keyPair.public_key, privateKey: keyPair.secret_key };
};
@@ -141,8 +141,8 @@ export const encryptBox = async (
dataB64: string,
keyB64: string,
): Promise<EncryptedBox> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const box = wasm.crypto_encrypt_box(dataB64, keyB64);
return { encryptedData: box.encrypted_data, nonce: box.nonce };
};
@@ -151,8 +151,8 @@ export const decryptBox = async (
box: EncryptedBox,
keyB64: string,
): Promise<string> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.crypto_decrypt_box(box.encryptedData, box.nonce, keyB64);
};
@@ -160,8 +160,8 @@ export const boxSealOpenBytes = async (
encryptedData: string,
keyPair: KeyPair,
): Promise<Uint8Array> => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return fromB64(
wasm.crypto_box_seal_open(
encryptedData,
@@ -177,8 +177,8 @@ export const deriveSubKeyBytes = async (
subKeyID: number,
context: string,
) => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return fromB64(
wasm.crypto_derive_subkey(
keyB64,
@@ -193,8 +193,8 @@ export const generateSRPSetupAttributesRust = async (
kekB64: string,
srpUserID: string,
) => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
const setup = wasm.auth_generate_srp_setup(
kekB64,
srpUserID,
@@ -207,13 +207,13 @@ export const generateSRPSetupAttributesRust = async (
};
export const recoveryKeyFromMnemonicOrHex = async (value: string) => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.auth_recovery_key_from_mnemonic_or_hex(value);
};
export const recoveryKeyToMnemonicRust = async (recoveryKey: string) => {
- await ensureCryptoInit();
- const wasm = await enteWasm();
+ await ensureWasmCryptoInit();
+ const wasm = await loadEnteWasm();
return wasm.auth_recovery_key_to_mnemonic(recoveryKey);
};
diff --git a/web/packages/accounts-rs/services/srp.ts b/web/packages/accounts-rs/services/srp.ts
index 123804ffd7..12929ae76f 100644
--- a/web/packages/accounts-rs/services/srp.ts
+++ b/web/packages/accounts-rs/services/srp.ts
@@ -10,6 +10,7 @@ import {
} from "ente-base/http";
import { apiURL } from "ente-base/origins";
import { ensure } from "ente-utils/ensure";
+import { loadEnteWasm } from "ente-wasm/load";
import { v4 as uuidv4 } from "uuid";
import { z } from "zod";
import { saveSRPAttributes } from "./accounts-db";
@@ -17,7 +18,6 @@ import {
RemoteSRPVerificationResponse,
type EmailOrSRPVerificationResponse,
} from "./user";
-import { enteWasm } from "./wasm";
export interface SRPAttributes {
srpUserID: string;
@@ -105,7 +105,7 @@ const createSRPSession = async (
srpUserID: string,
loginSubKey: string,
) => {
- const wasm = await enteWasm();
+ const wasm = await loadEnteWasm();
return new wasm.SrpSession(srpUserID, srpSalt, loginSubKey);
};
diff --git a/web/packages/accounts-rs/services/wasm.ts b/web/packages/accounts-rs/services/wasm.ts
deleted file mode 100644
index 50088c7b73..0000000000
--- a/web/packages/accounts-rs/services/wasm.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-type EnteWasmModule = typeof import("ente-wasm");
-
-let wasmPromise: Promise<EnteWasmModule> | undefined;
-let initDone = false;
-
-export const enteWasm = async (): Promise<EnteWasmModule> => {
- wasmPromise ??= import("ente-wasm");
- return wasmPromise;
-};
-
-export const ensureCryptoInit = async () => {
- if (initDone) return;
- const wasm = await enteWasm();
- wasm.crypto_init();
- initDone = true;
-};
diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.ts
index 5dc2a54ae7..fec7a7927d 100644
--- a/web/packages/contacts/index.ts
+++ b/web/packages/contacts/index.ts
@@ -2,7 +2,8 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import log from "ente-base/log";
import { apiOrigin } from "ente-base/origins";
import { savedAuthToken } from "ente-base/token";
-import { contacts_open_ctx, type ContactsCtxHandle } from "ente-wasm";
+import type { ContactsCtxHandle } from "ente-wasm";
+import { loadEnteWasm } from "ente-wasm/load";
import { useCallback, useEffect, useMemo, useSyncExternalStore } from "react";
import {
saveContactDisplayRecords,
@@ -305,6 +306,7 @@ const syncContacts = async ({
if (!isCurrentSession(sessionKey, generation)) {
return;
}
+ const { contacts_open_ctx } = await loadEnteWasm();
const openedCtx = await contacts_open_ctx({
baseUrl: baseURL,
authToken,
diff --git a/web/packages/wasm/load.ts b/web/packages/wasm/load.ts
new file mode 100644
index 0000000000..2d6c559068
--- /dev/null
+++ b/web/packages/wasm/load.ts
@@ -0,0 +1,26 @@
+type EnteWasmModule = typeof import("ente-wasm");
+
+let wasmPromise: Promise<EnteWasmModule> | undefined;
+let cryptoInitDone = false;
+
+/**
+ * Load `ente-wasm` once and return the shared module instance.
+ *
+ * The first call performs the dynamic import. Later calls reuse the same
+ * promise, so all consumers share one loader path.
+ */
+export const loadEnteWasm = async (): Promise<EnteWasmModule> => {
+ wasmPromise ??= import("ente-wasm");
+ return wasmPromise;
+};
+
+/**
+ * Ensure the WASM crypto backend has been initialised exactly once for the
+ * current page runtime.
+ */
+export const ensureWasmCryptoInit = async () => {
+ if (cryptoInitDone) return;
+ const wasm = await loadEnteWasm();
+ wasm.crypto_init();
+ cryptoInitDone = true;
+};
diff --git a/web/packages/wasm/package.json b/web/packages/wasm/package.json
index 99879089f8..29dd1be9b5 100644
--- a/web/packages/wasm/package.json
+++ b/web/packages/wasm/package.json
@@ -6,12 +6,17 @@
".": {
"types": "./pkg/ente_wasm.d.ts",
"import": "./pkg/ente_wasm.js"
+ },
+ "./load": {
+ "types": "./load.ts",
+ "import": "./load.ts"
}
},
"module": "./pkg/ente_wasm.js",
"types": "./pkg/ente_wasm.d.ts",
"files": [
- "pkg"
+ "pkg",
+ "load.ts"
],
"scripts": {
"build": "wasm-pack build --target bundler",