From 29680618ffc0b72c36a57e74008e5ff1abbc9dfa Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 28 Jan 2020 17:28:23 -0400 Subject: [PATCH] avoid commonjs require race conditions --- services/gunDB/contact-api/actions.js | 2 +- services/gunDB/contact-api/events.js | 5 ++--- services/gunDB/contact-api/getters.js | 2 +- services/gunDB/contact-api/utils/index.js | 16 ++++++++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index f8b38adf..50716dee 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -1053,7 +1053,7 @@ const saveSeedBackup = async (mnemonicPhrase, user, SEA) => { * @returns {Promise} */ const disconnect = async pub => { - const user = p(getUser()) + const user = p(require('../Mediator').getUser()) if (!(await Utils.successfulHandshakeAlreadyExists(pub))) { throw new Error('No handshake exists for this pub') } diff --git a/services/gunDB/contact-api/events.js b/services/gunDB/contact-api/events.js index c50ad89d..4dd845bf 100644 --- a/services/gunDB/contact-api/events.js +++ b/services/gunDB/contact-api/events.js @@ -3,8 +3,6 @@ */ const debounce = require('lodash/debounce') -const { getGun } = require('../Mediator/index') - const Actions = require('./actions') const ErrorCode = require('./errorCode') const Getters = require('./getters') @@ -636,7 +634,8 @@ const onChats = (cb, gun, user, SEA) => { if (!userWithDisconnectionListeners.has(recipientPK)) { userWithDisconnectionListeners.add(recipientPK) - getGun() + require('../Mediator') + .getGun() .user(recipientPK) .get(Key.OUTGOINGS) .get(incomingFeedID) diff --git a/services/gunDB/contact-api/getters.js b/services/gunDB/contact-api/getters.js index d0cc8baa..d5ace3c1 100644 --- a/services/gunDB/contact-api/getters.js +++ b/services/gunDB/contact-api/getters.js @@ -22,7 +22,7 @@ exports.currentOrderAddress = async (pub) => { * @returns {Promise} */ exports.userToIncomingID = async (pub) => { - const incomingID = await getUser().get(Key.USER_TO_INCOMING).get(pub).then() + const incomingID = await require('../Mediator').getUser().get(Key.USER_TO_INCOMING).get(pub).then() if (typeof incomingID === 'string') return incomingID diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 8c749f12..4d523907 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -1,7 +1,6 @@ /** * @format */ -const { getUser, mySEA: SEA, getGun } = require('../../Mediator') const ErrorCode = require('../errorCode') const Key = require('../key') @@ -21,8 +20,11 @@ const delay = ms => new Promise(res => setTimeout(res, ms)) * @returns {Promise} */ const mySecret = () => { - const user = getUser() - return SEA.secret(user._.sea.epub, user._.sea) + const user = require('../../Mediator/index').getUser() + return require('../../Mediator/index').mySEA.secret( + user._.sea.epub, + user._.sea + ) } /** @@ -159,13 +161,14 @@ const successfulHandshakeAlreadyExists = async recipientPub => { * @returns {Promise} */ const recipientToOutgoingID = async recipientPub => { - const maybeEncryptedOutgoingID = await getUser() + const maybeEncryptedOutgoingID = await require('../../Mediator/index') + .getUser() .get(Key.RECIPIENT_TO_OUTGOING) .get(recipientPub) .then() if (typeof maybeEncryptedOutgoingID === 'string') { - const outgoingID = await SEA.decrypt( + const outgoingID = await require('../../Mediator/index').mySEA.decrypt( maybeEncryptedOutgoingID, await mySecret() ) @@ -306,7 +309,8 @@ const defaultName = pub => 'anon' + pub.slice(0, 8) * @returns {Promise} */ const didDisconnect = async (pub, incomingID) => { - const feed = await getGun() + const feed = await require('../../Mediator/index') + .getGun() .user(pub) .get(Key.OUTGOINGS) .get(incomingID)