diff --git a/apps/web/src/interfaces/crypto.js b/apps/web/src/interfaces/crypto.js index 5f314b7a8..8841acec6 100644 --- a/apps/web/src/interfaces/crypto.js +++ b/apps/web/src/interfaces/crypto.js @@ -47,15 +47,34 @@ class Crypto { class CryptoWorker { constructor() { this.isReady = false; + this.initializing = false; + this.promiseQueue = []; } async _initialize() { - if (this.isReady) return; + if (this.isReady || this.initializing) + return await new Promise((resolve, reject) => { + this.promiseQueue.push({ resolve, reject }); + }); + + this.initializing = true; this.worker = new Worker("crypto.worker.js"); const buffer = Buffer.allocUnsafe(32); crypto.getRandomValues(buffer); const message = { seed: buffer.buffer }; - await this._communicate("load", message, [message.seed], false); - this.isReady = true; + + try { + await this._communicate("load", message, [message.seed], false); + this.isReady = true; + + this.promiseQueue.forEach(({ resolve }) => resolve()); + } catch (e) { + this.isReady = false; + + this.promiseQueue.forEach(({ reject }) => reject(e)); + } finally { + this.initializing = false; + this.promiseQueue = []; + } } _communicate(type, data, transferables = [], init = true) { diff --git a/apps/web/src/interfaces/storage.js b/apps/web/src/interfaces/storage.js index 2f3e4b62f..d6e364e9d 100644 --- a/apps/web/src/interfaces/storage.js +++ b/apps/web/src/interfaces/storage.js @@ -41,12 +41,10 @@ async function deriveCryptoKey(name, data) { if (!password) throw new Error("Invalid data provided to deriveCryptoKey."); const keyData = await crypto.deriveKey(password, salt, true); - console.log("keyData", keyData); if (window.indexedDB) { const pbkdfKey = await derivePBKDF2Key(password); await write(name, pbkdfKey); const cipheredKey = await aesEncrypt(pbkdfKey, keyData); - console.log("cipheredKey", cipheredKey); await write(`${name}@_k`, cipheredKey); } else { await write(`${name}@_k`, keyData);