From b44fd99e56cb9debb955c3584c27cf96bfe4a289 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Jul 2020 15:36:55 -0400 Subject: [PATCH] simpler dup alias check --- .env.example | 1 - services/gunDB/Mediator/index.js | 94 +++++++++---------------- services/gunDB/config.js | 3 - services/gunDB/contact-api/SimpleGUN.ts | 4 +- 4 files changed, 38 insertions(+), 64 deletions(-) diff --git a/.env.example b/.env.example index ad9e0eb7..65c31d1c 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,3 @@ MS_TO_TOKEN_EXPIRATION=4500000 DISABLE_SHOCK_ENCRYPTION=false CACHE_HEADERS_MANDATORY=true SHOCK_CACHE=true -DISABLE_PEER_ALIAS_CHECK = false diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 5a2c06ff..bfd8764b 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -12,10 +12,8 @@ require('gun/lib/open') // @ts-ignore require('gun/lib/load') const debounce = require('lodash/debounce') -const size = require('lodash/size') const Encryption = require('../../../utils/encryptionStore') -const { DISABLE_PEER_ALIAS_CHECK } = require('../config') const Key = require('../contact-api/key') /** @type {import('../contact-api/SimpleGUN').ISEA} */ @@ -1280,73 +1278,51 @@ const register = async (alias, pass) => { /** * Peers provided to gun. */ - const currPeers = gun._.opt.peers + const peers = Object.values(gun._.opt.peers) - if (DISABLE_PEER_ALIAS_CHECK) { - if (size(currPeers)) { - logger.info( - `Unexpected: disabled peer alias check while peers were specified.` - ) - throw new Error( - `Unexpected: disabled peer alias check while peers were specified.` - ) - } - - logger.warn( - `DISABLE_PEER_ALIAS_CHECK true, use only for testing purposes with no peers connected` - ) - } else { - if (size(currPeers) === 0) { - logger.info( - `Unexpected: Duplicate alias check enabled but Gun has no peers set. If you're testing gun without peers set DISABLE_PEER_ALIAS_CHECK to true.` - ) - throw new Error( - `Unexpected: Duplicate alias check enabled but Gun has no peers set.` - ) - } - - /** - * Of those, how many are actually connected - */ - const connectedPeers = Object.values(currPeers).filter(p => !!p.wire).length - - if (connectedPeers === 0) { - throw new Error( - `No connected peers, therefore cannot check for duplicate aliases.` - ) - } // else it's connected to at least one - } - - // this import is done here to avoid circular dependency hell - const { timeout5 } = require('../contact-api/utils') - - let userData = await timeout5( - new Promise(res => { - gun.get(`~@${alias}`).once(ud => res(ud)) - }) + const theresPeers = peers.length > 0 + const atLeastOneIsConnected = peers.some( + p => p.wire && p.wire.readyState === 1 ) - if (userData) { + if (theresPeers && !atLeastOneIsConnected) { throw new Error( - 'The given alias has been used before, use an unique alias instead.' + 'No connected to any peers for checking of duplicate aliases' ) } - await new Promise(res => setTimeout(res, 300)) + if (theresPeers && atLeastOneIsConnected) { + // this import is done here to avoid circular dependency hell + const { timeout5 } = require('../contact-api/utils') - userData = await timeout5( - new Promise(res => { - gun.get(`~@${alias}`).once(ud => res(ud), { - // https://github.com/amark/gun/pull/971#issue-438630761 - wait: 1500 + let userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud)) }) - }) - ) - - if (userData) { - throw new Error( - 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead.' + ) + } + + await new Promise(res => setTimeout(res, 300)) + + userData = await timeout5( + new Promise(res => { + gun.get(`~@${alias}`).once(ud => res(ud), { + // https://github.com/amark/gun/pull/971#issue-438630761 + wait: 1500 + }) + }) + ) + + if (userData) { + throw new Error( + 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' + ) + } } _isRegistering = true diff --git a/services/gunDB/config.js b/services/gunDB/config.js index 424cc53c..44dab137 100644 --- a/services/gunDB/config.js +++ b/services/gunDB/config.js @@ -23,6 +23,3 @@ exports.MS_TO_TOKEN_EXPIRATION = Number( ) exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true' - -exports.DISABLE_PEER_ALIAS_CHECK = - process.env.DISABLE_PEER_ALIAS_CHECK === 'true' diff --git a/services/gunDB/contact-api/SimpleGUN.ts b/services/gunDB/contact-api/SimpleGUN.ts index 1cef7225..3a9cd77b 100644 --- a/services/gunDB/contact-api/SimpleGUN.ts +++ b/services/gunDB/contact-api/SimpleGUN.ts @@ -33,7 +33,9 @@ export type Callback = (ack: Ack) => void export interface Peer { url: string id: string - wire: Record + wire?: { + readyState: number + } } export interface Soul {