Skip to content

Commit afa5068

Browse files
Add knownNodes (#91)
* Add knownNodes * addNode reverse order * Check for defined opts.nodes * Improved test
1 parent a5ddb29 commit afa5068

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class DHT extends EventEmitter {
7777
this.io.networkInterfaces.on('change', (interfaces) => this._onnetworkchange(interfaces))
7878

7979
if (opts.nodes) {
80-
for (const node of opts.nodes) this.addNode(node)
80+
for (let i = opts.nodes.length - 1; i >= 0; i--) {
81+
this.addNode(opts.nodes[i])
82+
}
8183
}
8284
}
8385

test.js

+16
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,22 @@ test('bootstrap with unreachable suggested-IP and fallback to DNS (reachable)',
665665
b.destroy()
666666
})
667667

668+
test('Populate DHT with options.nodes', async function (t) {
669+
const a = new DHT({ bootstrap: [] })
670+
await a.ready()
671+
const b = new DHT({ bootstrap: [] })
672+
await b.ready()
673+
const nodes = [{ host: '127.0.0.1', port: a.address().port }, { host: '127.0.0.1', port: b.address().port }]
674+
675+
const c = new DHT({ nodes, bootstrap: [] })
676+
await c.ready()
677+
t.alike(c.toArray(), nodes)
678+
679+
a.destroy()
680+
b.destroy()
681+
c.destroy()
682+
})
683+
668684
test('peer ids do not retain a slab', async function (t) {
669685
const swarm = await makeSwarm(2, t)
670686
t.is(swarm[1].id.buffer.byteLength, 32)

0 commit comments

Comments
 (0)