simpler dup alias check

This commit is contained in:
Daniel Lugo 2020-07-17 15:36:55 -04:00
parent bc03211ecc
commit b44fd99e56
4 changed files with 38 additions and 64 deletions

View file

@ -4,4 +4,3 @@ MS_TO_TOKEN_EXPIRATION=4500000
DISABLE_SHOCK_ENCRYPTION=false DISABLE_SHOCK_ENCRYPTION=false
CACHE_HEADERS_MANDATORY=true CACHE_HEADERS_MANDATORY=true
SHOCK_CACHE=true SHOCK_CACHE=true
DISABLE_PEER_ALIAS_CHECK = false

View file

@ -12,10 +12,8 @@ require('gun/lib/open')
// @ts-ignore // @ts-ignore
require('gun/lib/load') require('gun/lib/load')
const debounce = require('lodash/debounce') const debounce = require('lodash/debounce')
const size = require('lodash/size')
const Encryption = require('../../../utils/encryptionStore') const Encryption = require('../../../utils/encryptionStore')
const { DISABLE_PEER_ALIAS_CHECK } = require('../config')
const Key = require('../contact-api/key') const Key = require('../contact-api/key')
/** @type {import('../contact-api/SimpleGUN').ISEA} */ /** @type {import('../contact-api/SimpleGUN').ISEA} */
@ -1280,43 +1278,20 @@ const register = async (alias, pass) => {
/** /**
* Peers provided to gun. * Peers provided to gun.
*/ */
const currPeers = gun._.opt.peers const peers = Object.values(gun._.opt.peers)
if (DISABLE_PEER_ALIAS_CHECK) { const theresPeers = peers.length > 0
if (size(currPeers)) { const atLeastOneIsConnected = peers.some(
logger.info( p => p.wire && p.wire.readyState === 1
`Unexpected: disabled peer alias check while peers were specified.`
) )
if (theresPeers && !atLeastOneIsConnected) {
throw new Error( throw new Error(
`Unexpected: disabled peer alias check while peers were specified.` 'No connected to any peers for checking of duplicate aliases'
) )
} }
logger.warn( if (theresPeers && atLeastOneIsConnected) {
`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 // this import is done here to avoid circular dependency hell
const { timeout5 } = require('../contact-api/utils') const { timeout5 } = require('../contact-api/utils')
@ -1348,6 +1323,7 @@ const register = async (alias, pass) => {
'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)' 'The given alias has been used before, use an unique alias instead. (Caught at 2nd try)'
) )
} }
}
_isRegistering = true _isRegistering = true

View file

@ -23,6 +23,3 @@ exports.MS_TO_TOKEN_EXPIRATION = Number(
) )
exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true' exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true'
exports.DISABLE_PEER_ALIAS_CHECK =
process.env.DISABLE_PEER_ALIAS_CHECK === 'true'

View file

@ -33,7 +33,9 @@ export type Callback = (ack: Ack) => void
export interface Peer { export interface Peer {
url: string url: string
id: string id: string
wire: Record<string, unknown> wire?: {
readyState: number
}
} }
export interface Soul { export interface Soul {