From b259ac76d6c5cfd2364cb794480ddcd4d0799677 Mon Sep 17 00:00:00 2001 From: Vladislav Hristov Date: Thu, 28 Jun 2018 11:34:14 +0300 Subject: [PATCH] Code optimization --- lib/Client.js | 26 ++++++++++---------------- lib/storages/fsKeysStorage.js | 2 +- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/Client.js b/lib/Client.js index 78963ca..36c1696 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -125,9 +125,13 @@ module.exports = class Client { } async keys() { - const keysString = await this._readKeys(); + try { + const keysString = await this._readKeys(); - return keysString ? new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))) : null; + return new Keys(JSON.parse(this.keyEncryptor.decrypt(keysString))); + } catch (err) { + return null; + } } _generateKeys() { @@ -143,21 +147,11 @@ module.exports = class Client { await this._writeKeys(keysObject); } - async _readKeys() { - try { - const keys = await this.keyStorage.read(); - - return keys; - } catch (err) { - return null; - } + _readKeys() { + return this.keyStorage.read(); } - async _writeKeys(keysObject) { - try { - await this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); - } catch (err) { - throw err; - } + _writeKeys(keysObject) { + return this.keyStorage.write(this.keyEncryptor.encrypt(stringifyKeys(keysObject.keys))); } }; diff --git a/lib/storages/fsKeysStorage.js b/lib/storages/fsKeysStorage.js index a54835a..683a170 100644 --- a/lib/storages/fsKeysStorage.js +++ b/lib/storages/fsKeysStorage.js @@ -9,7 +9,7 @@ module.exports = (pathToFile) => { write(data) { return new Promise((resolve, reject) => { fs.writeFile(path, data, { encoding: 'utf8' }, (error) => { - if (error) throw error; + if (error) reject(error); return resolve(); });