Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

chore: always import nncrypto dynamically

thecodrr09942e7bc8b6
diff --git a/apps/web/packages/nncryptoworker/src/worker.ts b/apps/web/packages/nncryptoworker/src/worker.ts
index bbb195de0..0e2a9e6d8 100644
--- a/apps/web/packages/nncryptoworker/src/worker.ts
+++ b/apps/web/packages/nncryptoworker/src/worker.ts
@@ -1,20 +1,59 @@
-import { NNCrypto } from "nncrypto";
-import { SerializedKey } from "nncrypto/dist/src/types";
+import {
+ Cipher,
+ OutputFormat,
+ Plaintext,
+ SerializedKey,
+} from "nncrypto/dist/src/types";
import { expose } from "comlink";
import WorkerStream from "./workerstream";
+import { NNCrypto } from "nncrypto";
-const crypto = new NNCrypto();
+var crypto: NNCrypto | null = null;
+async function loadNNCrypto(): Promise<NNCrypto> {
+ if (crypto) return crypto;
+ const { NNCrypto } = await import("nncrypto");
+ return (crypto = new NNCrypto());
+}
const module = {
- exportKey: crypto.exportKey.bind(crypto),
- deriveKey: crypto.deriveKey.bind(crypto),
- encrypt: crypto.encrypt.bind(crypto),
- decrypt: crypto.decrypt.bind(crypto),
- hash: crypto.hash.bind(crypto),
- createEncryptionStream: (id: string, key: SerializedKey) => {
+ exportKey: async function (password: string, salt: string) {
+ const crypto = await loadNNCrypto();
+ return crypto.exportKey(password, salt);
+ },
+ deriveKey: async function (password: string, salt: string) {
+ const crypto = await loadNNCrypto();
+ return crypto.deriveKey(password, salt);
+ },
+ hash: async function (password: string, salt: string) {
+ const crypto = await loadNNCrypto();
+ return crypto.hash(password, salt);
+ },
+ encrypt: async function (
+ key: SerializedKey,
+ plaintext: Plaintext,
+ outputFormat?: OutputFormat
+ ) {
+ const crypto = await loadNNCrypto();
+ return crypto.encrypt(key, plaintext, outputFormat);
+ },
+ decrypt: async function (
+ key: SerializedKey,
+ cipherData: Cipher,
+ outputFormat?: OutputFormat
+ ) {
+ const crypto = await loadNNCrypto();
+ return crypto.decrypt(key, cipherData, outputFormat);
+ },
+ createEncryptionStream: async function (id: string, key: SerializedKey) {
+ const crypto = await loadNNCrypto();
return crypto.createEncryptionStream(key, new WorkerStream(id));
},
- createDecryptionStream: (id: string, iv: string, key: SerializedKey) => {
+ createDecryptionStream: async function (
+ id: string,
+ iv: string,
+ key: SerializedKey
+ ) {
+ const crypto = await loadNNCrypto();
return crypto.createDecryptionStream(iv, key, new WorkerStream(id));
},
};
diff --git a/apps/web/packages/nncryptoworker/tsconfig.json b/apps/web/packages/nncryptoworker/tsconfig.json
index b7103a824..03b98b865 100644
--- a/apps/web/packages/nncryptoworker/tsconfig.json
+++ b/apps/web/packages/nncryptoworker/tsconfig.json
@@ -27,7 +27,8 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
- "module": "commonjs",
+ "module": "esnext",
+ "moduleResolution": "node",
// "module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */