dht-rpc 6.12.1 → 6.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -3
  2. package/index.js +29 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -110,7 +110,7 @@ Options include:
110
110
 
111
111
  ``` js
112
112
  {
113
- // A list of bootstrap nodes
113
+ // A list of bootstrap nodes. Optionally prefix a suggested-IP, i.e. '192.168.1.10@bootstrap-node.com:24242'
114
114
  bootstrap: [ 'bootstrap-node.com:24242', ... ],
115
115
  // Optionally pass in array of { host, port } to add to the routing table if you know any peers
116
116
  nodes: [{ host, port }, ...],
@@ -339,9 +339,9 @@ Shutdown the DHT node.
339
339
 
340
340
  Boolean indicating if this has been destroyed.
341
341
 
342
- #### `node.toArray()`
342
+ #### `node.toArray([options])`
343
343
 
344
- Get the routing table peers out as an array of `{ host, port }`
344
+ Get the routing table peers out as an array of `{ host, port }`. Use `options.limit` to get a subset.
345
345
 
346
346
  #### `node.addNode({ host, port })`
347
347
 
package/index.js CHANGED
@@ -175,8 +175,10 @@ class DHT extends EventEmitter {
175
175
  })
176
176
  }
177
177
 
178
- toArray () {
179
- return this.nodes.toArray().map(({ host, port }) => ({ host, port }))
178
+ toArray (opts) {
179
+ const limit = (opts && opts.limit)
180
+ if (limit === 0) return []
181
+ return this.nodes.toArray(limit).map(({ host, port }) => ({ host, port }))
180
182
  }
181
183
 
182
184
  ready () {
@@ -659,18 +661,34 @@ class DHT extends EventEmitter {
659
661
  }
660
662
 
661
663
  async * _resolveBootstrapNodes () {
662
- for (const node of this.bootstrapNodes) {
663
- let address
664
- try {
665
- address = await this.udx.lookup(node.host, { family: 4 })
666
- } catch {
667
- continue
664
+ for (let { host, port } of this.bootstrapNodes) {
665
+ let doLookup = false
666
+
667
+ if (host.indexOf('@') === -1) {
668
+ doLookup = true
669
+ } else {
670
+ const [suggestedIP, fallbackHost] = host.split('@')
671
+ try {
672
+ await this.ping({ host: suggestedIP, port })
673
+ host = suggestedIP
674
+ } catch {
675
+ host = fallbackHost
676
+ doLookup = true
677
+ }
678
+ }
679
+
680
+ if (doLookup) {
681
+ try {
682
+ host = UDX.isIPv4(host) ? host : (await this.udx.lookup(host, { family: 4 })).host
683
+ } catch {
684
+ continue
685
+ }
668
686
  }
669
687
 
670
688
  yield {
671
- id: peer.id(address.host, node.port),
672
- host: address.host,
673
- port: node.port
689
+ id: peer.id(host, port),
690
+ host,
691
+ port
674
692
  }
675
693
  }
676
694
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dht-rpc",
3
- "version": "6.12.1",
3
+ "version": "6.14.0",
4
4
  "description": "Make RPC calls over a Kademlia based DHT",
5
5
  "main": "index.js",
6
6
  "files": [