Skip to content

armgit / ente

Public Imported from GitHub
0

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

Simplify

Manav Rathi75b3d2354fd1
diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts
index 2606b33976..2b007391c7 100644
--- a/web/packages/base/crypto/ente-impl.ts
+++ b/web/packages/base/crypto/ente-impl.ts
@@ -1,6 +1,10 @@
/** Careful when adding add other imports! */
import * as libsodium from "./libsodium";
+// Trivial proxies to the actual implementation.
+//
+// See: [Note: Using libsodium in worker thread]
+
export const _toB64 = libsodium.toB64;
export const _toB64URLSafe = libsodium.toB64URLSafe;
export const _fromB64 = libsodium.fromB64;
@@ -9,8 +13,8 @@ export const _fromHex = libsodium.fromHex;
export const _generateKey = libsodium.generateKey;
export const _generateBlobOrStreamKey = libsodium.generateBlobOrStreamKey;
export const _encryptBox = libsodium.encryptBox;
-export const _encryptBlob = libsodium.encryptBlob;
export const _encryptBlobBytes = libsodium.encryptBlobBytes;
+export const _encryptBlob = libsodium.encryptBlob;
export const _encryptMetadataJSON = libsodium.encryptMetadataJSON;
export const _encryptStreamBytes = libsodium.encryptStreamBytes;
export const _initChunkEncryption = libsodium.initChunkEncryption;
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index 49b2842a92..5368a270c3 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -56,7 +56,6 @@
*/
import { ComlinkWorker } from "ente-base/worker/comlink-worker";
import { type StateAddress } from "libsodium-wrappers-sumo";
-import { assertionFailed } from "../assert";
import { inWorker } from "../env";
import * as ei from "./ente-impl";
import type {
@@ -91,20 +90,6 @@ export const createComlinkCryptoWorker = () =>
new Worker(new URL("worker.ts", import.meta.url)),
);
-/**
- * Some of the potentially CPU intensive functions below have not yet been
- * needed on the main thread, and for these we don't have a corresponding
- * sharedCryptoWorker method.
- *
- * This assertion will let us know when we need to implement them. This will
- * gracefully degrade in production: the functionality will work, just that the
- * crypto operations will happen on the main thread itself.
- */
-const assertInWorker = <T>(x: T): T => {
- if (!inWorker()) assertionFailed("Currently only usable in a web worker");
- return x;
-};
-
/**
* Convert bytes ({@link Uint8Array}) to a base64 string.
*/
@@ -207,7 +192,9 @@ export const encryptBlob = (data: BytesOrB64, key: BytesOrB64) =>
* Use {@link decryptBlob} or {@link decryptBlobBytes} to decrypt the result.
*/
export const encryptBlobBytes = (data: BytesOrB64, key: BytesOrB64) =>
- assertInWorker(ei._encryptBlobBytes(data, key));
+ inWorker()
+ ? ei._encryptBlobBytes(data, key)
+ : sharedWorker().then((w) => w.encryptBlobBytes(data, key));
/**
* Encrypt the given data using chunked streaming encryption, but process all
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index cd5a465b9c..5f5002cdbd 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -23,18 +23,18 @@ export class CryptoWorker {
encryptBox = ei._encryptBox;
encryptBlobBytes = ei._encryptBlobBytes;
encryptBlob = ei._encryptBlob;
+ encryptMetadataJSON = ei._encryptMetadataJSON;
encryptStreamBytes = ei._encryptStreamBytes;
initChunkEncryption = ei._initChunkEncryption;
encryptStreamChunk = ei._encryptStreamChunk;
- encryptMetadataJSON = ei._encryptMetadataJSON;
decryptBoxBytes = ei._decryptBoxBytes;
decryptBox = ei._decryptBox;
decryptBlobBytes = ei._decryptBlobBytes;
decryptBlob = ei._decryptBlob;
+ decryptMetadataJSON = ei._decryptMetadataJSON;
decryptStreamBytes = ei._decryptStreamBytes;
initChunkDecryption = ei._initChunkDecryption;
decryptStreamChunk = ei._decryptStreamChunk;
- decryptMetadataJSON = ei._decryptMetadataJSON;
chunkHashInit = ei._chunkHashInit;
chunkHashUpdate = ei._chunkHashUpdate;
chunkHashFinal = ei._chunkHashFinal;