From 7a7bfc2f3355ae01fbede1b16ea7ddde7eb0c098 Mon Sep 17 00:00:00 2001 From: Sarah Vaupel Date: Sat, 1 Feb 2020 18:23:57 +0100 Subject: [PATCH] chore(storage-manager): proper-ish debug logs --- .../lib/storage-manager/storage-manager.js | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/frontend/src/lib/storage-manager/storage-manager.js b/frontend/src/lib/storage-manager/storage-manager.js index d93b6efd2..df91229d4 100644 --- a/frontend/src/lib/storage-manager/storage-manager.js +++ b/frontend/src/lib/storage-manager/storage-manager.js @@ -24,6 +24,8 @@ export class StorageManager { _encryptionKey = {}; constructor(namespace, version, options) { + this._debugLog('constructor', namespace, version, options); + this.namespace = namespace; this.version = semver.valid(version); @@ -54,6 +56,8 @@ export class StorageManager { } save(key, value, options=this._options) { + this._debugLog('save', key, value, options); + if (!key) { throw new Error('StorageManager.save called with invalid key'); } @@ -83,6 +87,8 @@ export class StorageManager { } load(key, options=this._options) { + this._debugLog('load', key, options); + if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { throw new Error('StorageManager.load called with unsupported location option'); } @@ -117,6 +123,8 @@ export class StorageManager { } remove(key, options=this._options) { + this._debugLog('remove', key, options); + if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { throw new Error('StorageManager.load called with unsupported location option'); } @@ -153,6 +161,8 @@ export class StorageManager { } clear(options) { + this._debugLog('clear', options); + if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { throw new Error('StorageManager.clear called with unsupported location option'); } @@ -176,10 +186,12 @@ export class StorageManager { _getFromLocalStorage(options=this._options) { + this._debugLog('_getFromLocalStorage', options); + let state; try { - state = JSON.parse(window.localStorage.getItem(this.namespace)); + state = JSON.parse(window.localStorage.getItem(this.namespace) || null); } catch { state = null; } @@ -199,6 +211,8 @@ export class StorageManager { } _saveToLocalStorage(state) { + this._debugLog('_saveToLocalStorage', state); + if (!state) return this._clearLocalStorage(); @@ -214,11 +228,15 @@ export class StorageManager { } _clearLocalStorage() { + this._debugLog('_clearLocalStorage'); + window.localStorage.removeItem(this.namespace); } _getFromWindow() { + this._debugLog('_getFromWindow'); + if (!this._global || !this._global.App) return {}; @@ -229,6 +247,8 @@ export class StorageManager { } _saveToWindow(value) { + this._debugLog('_saveToWindow', value); + if (!this._global || !this._global.App) { return console.error('StorageManager._saveToWindow called when window.App is not available'); } @@ -243,6 +263,8 @@ export class StorageManager { } _clearWindow() { + this._debugLog('_clearWindow'); + if (!this._global || !this._global.App) { return console.error('StorageManager._saveToWindow called when window.App is not available'); } @@ -254,11 +276,12 @@ export class StorageManager { _getFromSessionStorage(options=this._options) { - console.log('_getFromSessionStorage with args', options); + this._debugLog('_getFromSessionStorage', options); + let state; try { - state = JSON.parse(window.sessionStorage.getItem(this.namespace)); + state = JSON.parse(window.sessionStorage.getItem(this.namespace) || null); } catch { state = null; } @@ -278,6 +301,8 @@ export class StorageManager { } _saveToSessionStorage(state) { + this._debugLog('_saveToSessionStorage', state); + if (!state) return this._clearSessionStorage(); @@ -293,23 +318,30 @@ export class StorageManager { } _clearSessionStorage() { + this._debugLog('_clearSessionStorage'); + window.sessionStorage.removeItem(this.namespace); } _getFromStorage(storage, location, options=this._options) { + this._debugLog('_getFromStorage', storage, location, options); + const encryption = options.encryption && (options.encryption.all || options.encryption[location]); if (encryption && storage.encryption) { - return { ...storage, ...JSON.parse(decrypt(storage.encryption.ciphertext, this._encryptionKey[location]) || '{}') }; + return { ...storage, ...JSON.parse(decrypt(storage.encryption.ciphertext, this._encryptionKey[location]) || null) }; } else { return storage; } } _updateStorage(storage, update, location, options=this._options) { + this._debugLog('_updateStorage', storage, update, location, options); + const encryption = options.encryption && (options.encryption.all || options.encryption[location]); if (encryption && storage.encryption) { - const updatedDecryptedStorage = { ...JSON.parse(decrypt(storage.encryption.ciphertext, this._encryptionKey[location]) || '{}'), ...update }; + const updatedDecryptedStorage = { ...JSON.parse(decrypt(storage.encryption.ciphertext, this._encryptionKey[location]) || null), ...update }; + console.log('updatedDecryptedStorage', updatedDecryptedStorage); return { ...storage, encryption: { ...storage.encryption, ciphertext: encrypt(JSON.stringify(updatedDecryptedStorage), this._encryptionKey[location]) } }; } else { return { ...storage, ...update }; @@ -317,6 +349,8 @@ export class StorageManager { } _requestStorageKey(options=this._options) { + this._debugLog('_requestStorageKey', options); + if (!(options && options.location && options.encryption)) throw new Error('Storage Manager cannot request storage key with unsupported options!'); @@ -347,12 +381,16 @@ export class StorageManager { }).catch(console.error); } + _debugLog(fName, ...args) { + console.log(`[DEBUGLOG] StorageManager.${fName}`, { args: args, instance: this }); + } + } // TODO debug unnecessary calls of encrypt function encrypt(plaintext, key) { - console.log('args encrypt', plaintext, key); + console.log('encrypt', plaintext, key); if (!plaintext) return ''; if (!key) throw new Error('Cannot encrypt plaintext without a valid key!'); @@ -367,12 +405,14 @@ function encrypt(plaintext, key) { sodium.crypto_secretbox_easy(cipherB, plaintextB, nonceB, keyB); - return cipherB; + const result = cipherB; + console.log('encrypt result', result); + return result; } // TODO debug unnecessary calls of decrypt function decrypt(ciphertext, key) { - console.log('args decrypt', ciphertext, key); + console.log('decrypt', ciphertext, key); if (!ciphertext) return ''; if (!key) throw new Error('Cannot decrypt ciphertext without a valid key!'); @@ -387,5 +427,7 @@ function decrypt(ciphertext, key) { sodium.crypto_secretbox_open_easy(plaintextB, cipherB, nonceB, keyB); - return plaintextB.toString(); + const result = plaintextB.toString(); + console.log('decrypt result', result); + return result; }