Browse Source

Switch to `udx.lookup()` (#47)

master
Kasper Isager Dalsgarð 2 years ago
committed by GitHub
parent
commit
a3b85408cd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      index.js
  2. 13
      lib/query.js
  3. 2
      package.json

33
index.js

@ -1,4 +1,3 @@
const dns = require('dns')
const { EventEmitter } = require('events')
const Table = require('kademlia-routing-table')
const TOS = require('time-ordered-set')
@ -571,27 +570,27 @@ class DHT extends EventEmitter {
return true
}
_resolveBootstrapNodes (done) {
if (!this.bootstrapNodes.length) return done([])
let missing = this.bootstrapNodes.length
const nodes = []
async * _resolveBootstrapNodes () {
for (const node of this.bootstrapNodes) {
dns.lookup(node.host, { family: 4 }, (_, host) => {
if (host) nodes.push({ id: peer.id(host, node.port), host, port: node.port })
if (--missing === 0) done(nodes)
})
let address
try {
address = await this.udx.lookup(node.host, { family: 4 })
} catch {
continue
}
yield {
id: peer.id(address.host, node.port),
host: address.host,
port: node.port
}
}
}
async _addBootstrapNodes (nodes) {
return new Promise((resolve) => {
this._resolveBootstrapNodes(function (bootstrappers) {
nodes.push(...bootstrappers)
resolve()
})
})
for await (const node of this._resolveBootstrapNodes()) {
nodes.push(node)
}
}
async _checkIfFirewalled (natSampler = new NatSampler()) {

13
lib/query.js

@ -95,16 +95,15 @@ module.exports = class Query extends Readable {
}
}
_open (cb) {
async _open (cb) {
this._addFromTable()
if (this._pending.length >= this.k) return cb(null)
this.dht._resolveBootstrapNodes((bootstrapNodes) => {
for (const node of bootstrapNodes) {
this._addPending(node, null)
}
cb(null)
})
for await (const node of this.dht._resolveBootstrapNodes()) {
this._addPending(node, null)
}
cb(null)
}
_isCloser (id) {

2
package.json

@ -14,7 +14,7 @@
"sodium-universal": "^3.0.4",
"streamx": "^2.10.3",
"time-ordered-set": "^1.0.2",
"udx-native": "^1.1.0"
"udx-native": "^1.2.0"
},
"devDependencies": {
"brittle": "^2.3.1",

Loading…
Cancel
Save