From 53a8d44f69413e0b5fc36b38f0ab0374bae2336e Mon Sep 17 00:00:00 2001 From: emad-salah Date: Thu, 4 Nov 2021 13:31:58 +0100 Subject: [PATCH 1/2] Use instantiateGun instead of logoff and catch listener errors --- src/routes.js | 52 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/routes.js b/src/routes.js index 47fe3806..e5418542 100644 --- a/src/routes.js +++ b/src/routes.js @@ -506,7 +506,7 @@ module.exports = async ( await recreateLnServices() if (GunDB.isAuthenticated()) { - GunDB.logoff() + GunDB.instantiateGun() } const publicKey = await GunDB.authenticate(alias, password) @@ -2296,27 +2296,39 @@ module.exports = async ( : publicKey ? gun.user(publicKey) : gun - keys.forEach(key => (node = node.get(key))) logger.info(`fetching: ${keys}`) - return new Promise((res, rej) => { - const listener = data => { - logger.info(`got res for: ${keys}`) - logger.info(data || 'falsey data (does not get logged)') - if (publicKeyForDecryption) { - GunWriteRPC.deepDecryptIfNeeded( - data, - publicKeyForDecryption, - epubForDecryption - ) - .then(res) - .catch(rej) - } else { - res(data) - } - } + keys.forEach(key => (node = node.get(key))) - if (type === 'once') node.once(listener) - if (type === 'load') node.load(listener) + if (!publicKeyForDecryption || !epubForDecryption) { + logger.warn('[GUN] Missing public key for decryption!', { + publicKeyForDecryption, + epubForDecryption + }) + } + + return new Promise((res, rej) => { + try { + const listener = data => { + logger.info(`got res for: ${keys}`) + logger.info(data || 'falsey data (does not get logged)') + if (publicKeyForDecryption) { + GunWriteRPC.deepDecryptIfNeeded( + data, + publicKeyForDecryption, + epubForDecryption + ) + .then(res) + .catch(rej) + } else { + res(data) + } + } + + if (type === 'once') node.once(listener) + if (type === 'load') node.load(listener) + } catch (err) { + logger.error('Gun Fetch Error:', err) + } }) }) } From 9e3b78546adc014e39674067c3825f241ffa5a93 Mon Sep 17 00:00:00 2001 From: emad-salah Date: Thu, 4 Nov 2021 13:32:40 +0100 Subject: [PATCH 2/2] Remove `user.leave()` --- services/gunDB/Mediator/index.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index ced3416c..1cfd24d2 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -392,15 +392,11 @@ const authenticate = async (alias, pass, __user) => { } } -const logoff = () => { - user.leave() -} - const instantiateGun = () => { const Config = require('../config') - if (user) { - user.leave() - } + // if (user) { + // user.leave() + // } // @ts-ignore user = null if (gun) { @@ -523,7 +519,7 @@ const register = async (alias, pass) => { module.exports = { authenticate, - logoff, + instantiateGun, isAuthenticated, isAuthenticating, isRegistering,