From 082981c1abbbf38b672ce386868b974b3f15f660 Mon Sep 17 00:00:00 2001 From: tommy-carlos williams Date: Sun, 25 Jun 2023 10:28:28 +1000 Subject: [PATCH] Update index.js with some small bug fixes (#15) This code needs a complete refactor, but this is at least fixing a couple glaring issues. --- src/index.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 53e1896..ac164b6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ import nacl from 'tweetnacl'; -// import naclUtil from 'tweetnacl-util'; import {encode as encodeUTF8, decode as decodeUTF8} from '@stablelib/utf8'; import {decode as decodeBase64, encode as encodeBase64} from '@stablelib/base64'; import sha256 from 'fast-sha256'; @@ -7,8 +6,6 @@ import TransformPouch from 'transform-pouch'; import PouchDB from 'pouchdb'; import PouchDBFind from 'pouchdb-find'; -// nacl.util = naclUtil; - function encrypt(text, nonce, key) { const encText = nacl.secretbox(encodeUTF8(text), nonce, key); return encodeBase64(encText); @@ -35,7 +32,6 @@ export default class Echidnajs { constructor({username, passphrase, salt, rounds = 100000}) { if (!username || !passphrase || !salt) { throw new Error('Missing required options'); - return false; } PouchDB.plugin(PouchDBFind); PouchDB.plugin(TransformPouch); @@ -53,21 +49,21 @@ export default class Echidnajs { } }); newDoc.nonce = encodeBase64(nonce); - return Object.assign(doc, newDoc); + return Object.assign({}, doc, newDoc); }, outgoing(doc) { const keys = Object.keys(doc); const newDoc = {}; - let error; + const errors = []; keys.forEach((field) => { if (field !== '_id' && field !== '_rev' && field !== 'nonce') { newDoc[field] = decrypt(doc[field], decodeBase64(doc.nonce), key); if (newDoc[field] === undefined) { - error = new Error('Failed to decrypt'); + errors.push(new Error('Failed to decrypt')); } } }); - return error || Object.assign(doc, newDoc); + return errors?.length ? errors : Object.assign({}, doc, newDoc); }, }); } @@ -85,6 +81,5 @@ export default class Echidnajs { close() { this.remote.close(); this.pouch.close(); - // console.log('closed'); } }