Skip to content

Commit

Permalink
Update index.js with some small bug fixes (#15)
Browse files Browse the repository at this point in the history
This code needs a complete refactor, but this is at least fixing a couple glaring issues.
  • Loading branch information
devgeeks authored Jun 25, 2023
1 parent c7be9b6 commit 082981c
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
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';
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);
Expand All @@ -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);
Expand All @@ -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);
},
});
}
Expand All @@ -85,6 +81,5 @@ export default class Echidnajs {
close() {
this.remote.close();
this.pouch.close();
// console.log('closed');
}
}

0 comments on commit 082981c

Please sign in to comment.