chore(storage-manager): proper-ish debug logs

This commit is contained in:
Sarah Vaupel 2020-02-01 18:23:57 +01:00 committed by Gregor Kleen
parent 596541e079
commit 7a7bfc2f33

View File

@ -24,6 +24,8 @@ export class StorageManager {
_encryptionKey = {}; _encryptionKey = {};
constructor(namespace, version, options) { constructor(namespace, version, options) {
this._debugLog('constructor', namespace, version, options);
this.namespace = namespace; this.namespace = namespace;
this.version = semver.valid(version); this.version = semver.valid(version);
@ -54,6 +56,8 @@ export class StorageManager {
} }
save(key, value, options=this._options) { save(key, value, options=this._options) {
this._debugLog('save', key, value, options);
if (!key) { if (!key) {
throw new Error('StorageManager.save called with invalid key'); throw new Error('StorageManager.save called with invalid key');
} }
@ -83,6 +87,8 @@ export class StorageManager {
} }
load(key, options=this._options) { load(key, options=this._options) {
this._debugLog('load', key, options);
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
throw new Error('StorageManager.load called with unsupported location option'); throw new Error('StorageManager.load called with unsupported location option');
} }
@ -117,6 +123,8 @@ export class StorageManager {
} }
remove(key, options=this._options) { remove(key, options=this._options) {
this._debugLog('remove', key, options);
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
throw new Error('StorageManager.load called with unsupported location option'); throw new Error('StorageManager.load called with unsupported location option');
} }
@ -153,6 +161,8 @@ export class StorageManager {
} }
clear(options) { clear(options) {
this._debugLog('clear', options);
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) { if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
throw new Error('StorageManager.clear called with unsupported location option'); throw new Error('StorageManager.clear called with unsupported location option');
} }
@ -176,10 +186,12 @@ export class StorageManager {
_getFromLocalStorage(options=this._options) { _getFromLocalStorage(options=this._options) {
this._debugLog('_getFromLocalStorage', options);
let state; let state;
try { try {
state = JSON.parse(window.localStorage.getItem(this.namespace)); state = JSON.parse(window.localStorage.getItem(this.namespace) || null);
} catch { } catch {
state = null; state = null;
} }
@ -199,6 +211,8 @@ export class StorageManager {
} }
_saveToLocalStorage(state) { _saveToLocalStorage(state) {
this._debugLog('_saveToLocalStorage', state);
if (!state) if (!state)
return this._clearLocalStorage(); return this._clearLocalStorage();
@ -214,11 +228,15 @@ export class StorageManager {
} }
_clearLocalStorage() { _clearLocalStorage() {
this._debugLog('_clearLocalStorage');
window.localStorage.removeItem(this.namespace); window.localStorage.removeItem(this.namespace);
} }
_getFromWindow() { _getFromWindow() {
this._debugLog('_getFromWindow');
if (!this._global || !this._global.App) if (!this._global || !this._global.App)
return {}; return {};
@ -229,6 +247,8 @@ export class StorageManager {
} }
_saveToWindow(value) { _saveToWindow(value) {
this._debugLog('_saveToWindow', value);
if (!this._global || !this._global.App) { if (!this._global || !this._global.App) {
return console.error('StorageManager._saveToWindow called when window.App is not available'); return console.error('StorageManager._saveToWindow called when window.App is not available');
} }
@ -243,6 +263,8 @@ export class StorageManager {
} }
_clearWindow() { _clearWindow() {
this._debugLog('_clearWindow');
if (!this._global || !this._global.App) { if (!this._global || !this._global.App) {
return console.error('StorageManager._saveToWindow called when window.App is not available'); return console.error('StorageManager._saveToWindow called when window.App is not available');
} }
@ -254,11 +276,12 @@ export class StorageManager {
_getFromSessionStorage(options=this._options) { _getFromSessionStorage(options=this._options) {
console.log('_getFromSessionStorage with args', options); this._debugLog('_getFromSessionStorage', options);
let state; let state;
try { try {
state = JSON.parse(window.sessionStorage.getItem(this.namespace)); state = JSON.parse(window.sessionStorage.getItem(this.namespace) || null);
} catch { } catch {
state = null; state = null;
} }
@ -278,6 +301,8 @@ export class StorageManager {
} }
_saveToSessionStorage(state) { _saveToSessionStorage(state) {
this._debugLog('_saveToSessionStorage', state);
if (!state) if (!state)
return this._clearSessionStorage(); return this._clearSessionStorage();
@ -293,23 +318,30 @@ export class StorageManager {
} }
_clearSessionStorage() { _clearSessionStorage() {
this._debugLog('_clearSessionStorage');
window.sessionStorage.removeItem(this.namespace); window.sessionStorage.removeItem(this.namespace);
} }
_getFromStorage(storage, location, options=this._options) { _getFromStorage(storage, location, options=this._options) {
this._debugLog('_getFromStorage', storage, location, options);
const encryption = options.encryption && (options.encryption.all || options.encryption[location]); const encryption = options.encryption && (options.encryption.all || options.encryption[location]);
if (encryption && storage.encryption) { 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 { } else {
return storage; return storage;
} }
} }
_updateStorage(storage, update, location, options=this._options) { _updateStorage(storage, update, location, options=this._options) {
this._debugLog('_updateStorage', storage, update, location, options);
const encryption = options.encryption && (options.encryption.all || options.encryption[location]); const encryption = options.encryption && (options.encryption.all || options.encryption[location]);
if (encryption && storage.encryption) { 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]) } }; return { ...storage, encryption: { ...storage.encryption, ciphertext: encrypt(JSON.stringify(updatedDecryptedStorage), this._encryptionKey[location]) } };
} else { } else {
return { ...storage, ...update }; return { ...storage, ...update };
@ -317,6 +349,8 @@ export class StorageManager {
} }
_requestStorageKey(options=this._options) { _requestStorageKey(options=this._options) {
this._debugLog('_requestStorageKey', options);
if (!(options && options.location && options.encryption)) if (!(options && options.location && options.encryption))
throw new Error('Storage Manager cannot request storage key with unsupported options!'); throw new Error('Storage Manager cannot request storage key with unsupported options!');
@ -347,12 +381,16 @@ export class StorageManager {
}).catch(console.error); }).catch(console.error);
} }
_debugLog(fName, ...args) {
console.log(`[DEBUGLOG] StorageManager.${fName}`, { args: args, instance: this });
}
} }
// TODO debug unnecessary calls of encrypt // TODO debug unnecessary calls of encrypt
function encrypt(plaintext, key) { function encrypt(plaintext, key) {
console.log('args encrypt', plaintext, key); console.log('encrypt', plaintext, key);
if (!plaintext) return ''; if (!plaintext) return '';
if (!key) throw new Error('Cannot encrypt plaintext without a valid key!'); 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); 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 // TODO debug unnecessary calls of decrypt
function decrypt(ciphertext, key) { function decrypt(ciphertext, key) {
console.log('args decrypt', ciphertext, key); console.log('decrypt', ciphertext, key);
if (!ciphertext) return ''; if (!ciphertext) return '';
if (!key) throw new Error('Cannot decrypt ciphertext without a valid key!'); 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); sodium.crypto_secretbox_open_easy(plaintextB, cipherB, nonceB, keyB);
return plaintextB.toString(); const result = plaintextB.toString();
console.log('decrypt result', result);
return result;
} }