Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const from = {
port: value.port || query.node.port,
host: query.node.host
}
if (!(from.port > 0 && from.port < 65536)) return cb(new Error('Invalid port'))
const localRecord = value.localAddress
const remoteRecord = peers.encode([from])
const remoteCache = query.target.toString('hex')
const localCache = localRecord &&
remoteCache + '@local.' + localRecord.slice(0, 2).toString('hex')
const localSuffix = localRecord && localRecord.slice(2)
if (query.type === DHT.QUERY) {
const local = localCache ? filter(this._peers.get(localCache, 64), localSuffix) : []
const remote = filter(this._peers.get(remoteCache, 128 - local.length), remoteRecord)
this.emit('lookup', query.target, from)
return cb(null, {
peers: remote.length ? Buffer.concat(remote) : null,
localPeers: local.length ? Buffer.concat(local) : null
})
}
if (value.unannounce) {
this._peers.remove(remoteCache, remoteRecord)
if (localRecord) this._peers.remove(localCache, localSuffix)
this.emit('unannounce', query.target, from)
} else {
this._peers.add(remoteCache, remoteRecord)
if (localRecord) this._peers.add(localCache, localSuffix)