Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(backup): hash the container with WASM SHA-256
Elian Doran30dc3dfd0c1c
diff --git a/packages/trilium-backup-container/package.json b/packages/trilium-backup-container/package.jsonindex 3140e48a17..af7a1fd86f 100644--- a/packages/trilium-backup-container/package.json+++ b/packages/trilium-backup-container/package.json@@ -14,7 +14,8 @@"test": "vitest"},"dependencies": {- "@noble/hashes": "^2.2.0"+ "@noble/hashes": "^2.2.0",+ "hash-wasm": "^4.12.0"},"keywords": ["backup",diff --git a/packages/trilium-backup-container/src/backend-node.ts b/packages/trilium-backup-container/src/backend-node.tsindex f3502440fe..710145f532 100644--- a/packages/trilium-backup-container/src/backend-node.ts+++ b/packages/trilium-backup-container/src/backend-node.ts@@ -26,7 +26,7 @@ export const nodeBackend: ContainerBackend = {return randomBytes(size);},- createSha256() {+ async createSha256() {const hash = createHash("sha256");return {diff --git a/packages/trilium-backup-container/src/backend-web.ts b/packages/trilium-backup-container/src/backend-web.tsindex d74fde7b29..4f937750ef 100644--- a/packages/trilium-backup-container/src/backend-web.ts+++ b/packages/trilium-backup-container/src/backend-web.ts@@ -1,5 +1,5 @@import { scryptAsync } from "@noble/hashes/scrypt.js";-import { sha256 } from "@noble/hashes/sha2.js";+import { createSHA256 } from "hash-wasm";import type { ByteSource, ContainerBackend } from "./backend.js";import { concatBytes } from "./bytes.js";@@ -8,9 +8,11 @@ import { KEY_BYTES, TAG_BYTES } from "./format.js";/*** The browser backend: AES-256-GCM and randomness from WebCrypto, gzip from the platform's- * `CompressionStream`, and the two primitives WebCrypto does not offer, scrypt and incremental- * SHA-256, from `@noble/hashes`. Runs under Node just as well, which is how the cross-backend- * tests exercise it.+ * `CompressionStream`, and the two primitives WebCrypto does not offer from elsewhere — scrypt+ * from `@noble/hashes`, incremental SHA-256 from `hash-wasm`. The hash is WASM rather than+ * `@noble`'s pure JS because it runs over every payload byte and paced a phone's backup at about+ * 32 MB/s; scrypt stays JS because it runs once per container. Runs under Node just as well,+ * which is how the cross-backend tests exercise it.** WebCrypto's `subtle` interface only exists in secure contexts (HTTPS, localhost, workers of* either), so this backend cannot serve a page loaded over plain HTTP.@@ -23,15 +25,18 @@ export const webBackend: ContainerBackend = {return bytes;},- createSha256() {- const hash = sha256.create();+ async createSha256() {+ // hash-wasm compiles the module once and reuses it, so only the first call pays for+ // compilation; each call gets its own hasher state.+ const hasher = await createSHA256();+ hasher.init();return {update(chunk: Uint8Array) {- hash.update(chunk);+ hasher.update(chunk);},digest() {- return hash.digest();+ return hasher.digest("binary");}};},diff --git a/packages/trilium-backup-container/src/backend.ts b/packages/trilium-backup-container/src/backend.tsindex 6c0ffc0a25..1126cdf5c4 100644--- a/packages/trilium-backup-container/src/backend.ts+++ b/packages/trilium-backup-container/src/backend.ts@@ -14,7 +14,11 @@ export interface ByteSink {end(): Promise<void>;}-/** Incremental SHA-256, which WebCrypto cannot provide and both directions need. */+/**+ * Incremental SHA-256, which WebCrypto cannot provide and both directions need. Created+ * asynchronously because the web backend instantiates a WASM hasher; the streaming itself+ * is synchronous.+ */export interface StreamingHash {update(chunk: Uint8Array): void;digest(): Uint8Array;@@ -30,7 +34,7 @@ export interface StreamingHash {*/export interface ContainerBackend {randomBytes(size: number): Uint8Array;- createSha256(): StreamingHash;+ createSha256(): Promise<StreamingHash>;/*** Derives the 32-byte file key with scrypt. The passphrase arrives already NFC-normalised* and UTF-8 encoded. Throws whatever the platform throws; the core wraps it.diff --git a/packages/trilium-backup-container/src/read.ts b/packages/trilium-backup-container/src/read.tsindex 2aeb1a6553..59282005c3 100644--- a/packages/trilium-backup-container/src/read.ts+++ b/packages/trilium-backup-container/src/read.ts@@ -415,7 +415,7 @@ async function* readFrames(found: FoundTrailer): AsyncGenerator<Uint8Array> {const aad = aadOf(header);- const hash = backend.createSha256();+ const hash = await backend.createSha256();let counter = 0;for (;;) {@@ -486,7 +486,7 @@ async function* readPlainPayload(backend: ContainerBackend,found: FoundTrailer): AsyncGenerator<Uint8Array> {- const hash = backend.createSha256();+ const hash = await backend.createSha256();let held = EMPTY;for (;;) {diff --git a/packages/trilium-backup-container/src/write.ts b/packages/trilium-backup-container/src/write.tsindex 981e0add1d..9184cc42ae 100644--- a/packages/trilium-backup-container/src/write.ts+++ b/packages/trilium-backup-container/src/write.ts@@ -163,7 +163,7 @@ export async function writeContainer(payload = encryptFrames(backend, key, header.encryption.noncePrefix, aad, payload);}- const hash = backend.createSha256();+ const hash = await backend.createSha256();let payloadBytes = 0;for await (const chunk of payload) {hash.update(chunk);diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2aa951911b..b6565aefcf 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1392,6 +1392,9 @@ importers:'@noble/hashes':specifier: ^2.2.0version: 2.4.0+ hash-wasm:+ specifier: ^4.12.0+ version: 4.12.0packages/trilium-core:dependencies:@@ -7860,6 +7863,9 @@ packages:resolution: {integrity: sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==}engines: {node: '>= 0.4'}+ resolution: {integrity: sha512-+/2B2rYLb48I/evdOIhP+K/DD2ca2fgBjp6O+GBEnCDk2e4rpeXIK8GvIyRPjTezgmWn9gmKwkQjjx6BtqDHVQ==}+resolution: {integrity: sha512-iZyKG96/JwPz1N55vj2Ie2vXbhu440zfUfJvSwEqEbeLluk7NnapfGqa7LH0mOsnDxTF85Mx8/dyR6HfqcbmbQ==}engines: {node: '>=20'}@@ -20266,6 +20272,8 @@ snapshots:dependencies:has-symbols: 1.1.0+ [email protected]: {}+dependencies:hookified: 1.15.1