Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit e7d2d7f

Browse files
authored
fix: return pk when found (#273)
Instead of waiting for the query to end, just return the public key when we find it.
1 parent f7d9ec6 commit e7d2d7f

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/kad-dht.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -483,26 +483,20 @@ class KadDHT extends EventEmitter {
483483
this._log('getPublicKey %p', peer)
484484

485485
// try the node directly
486-
let pk
487-
488486
for await (const event of this._peerRouting.getPublicKeyFromNode(peer, options)) {
489487
if (event.name === 'VALUE') {
490-
pk = crypto.keys.unmarshalPublicKey(event.value)
488+
return crypto.keys.unmarshalPublicKey(event.value)
491489
}
492490
}
493491

494-
if (!pk) {
495-
// try dht directly
496-
const pkKey = utils.keyForPublicKey(peer)
492+
// search the dht
493+
const pkKey = utils.keyForPublicKey(peer)
497494

498-
for await (const event of this.get(pkKey, options)) {
499-
if (event.name === 'VALUE') {
500-
pk = crypto.keys.unmarshalPublicKey(event.value)
501-
}
495+
for await (const event of this.get(pkKey, options)) {
496+
if (event.name === 'VALUE') {
497+
return crypto.keys.unmarshalPublicKey(event.value)
502498
}
503499
}
504-
505-
return pk
506500
}
507501

508502
async refreshRoutingTable () {

0 commit comments

Comments
 (0)