Browse Source

feat: Add getNodes() and addNodes() methods.

v4
Aurélien Ooms 5 years ago
committed by Mathias Buus
parent
commit
730a9066b9
  1. 8
      index.js
  2. 55
      test.js

8
index.js

@ -405,6 +405,14 @@ class DHT extends EventEmitter {
if (cb) cb()
})
}
getNodes () {
return this.nodes.toArray().map(({ id, host, port }) => ({ id, host, port }))
}
addNodes (nodes) {
for (const { id, host, port } of nodes) this._addNode(id, { host, port })
}
}
exports.id = () => randomBytes(32)

55
test.js

@ -246,6 +246,61 @@ tape('persistent', function (t) {
})
})
tape('getNodes', function (t) {
bootstrap(function (port, node) {
const a = dht({ bootstrap: port })
const b = dht({ bootstrap: port })
a.ready(function () {
b.ready(function () {
const aNodes = a.getNodes()
const bNodes = b.getNodes()
t.deepEqual([{ id: b.id, host: '127.0.0.1', port: b.address().port }], aNodes)
t.deepEqual([{ id: a.id, host: '127.0.0.1', port: a.address().port }], bNodes)
b.destroy()
a.destroy()
node.destroy()
t.end()
})
})
})
})
tape('addNodes', function (t) {
bootstrap(function (port, node) {
const a = dht({ bootstrap: port })
const b = dht({ bootstrap: [] })
b.listen() // https://github.com/hyperswarm/dht/issues/22
a.command('hello', {
query (data, callback) {
t.same(data.value, null, 'expected data')
callback(null, Buffer.from('world'))
}
})
a.ready(function () {
b.ready(function () {
setTimeout(function () {
const bNodes = b.getNodes()
t.deepEqual(bNodes, [{ id: a.id, host: '127.0.0.1', port: a.address().port }])
b.query('hello', a.id, function (err, responses) {
t.error(err, 'no errors')
t.same(responses.length, 1, 'one response')
t.same(responses[0].value, Buffer.from('world'), 'responded')
const aNodes = a.getNodes()
t.deepEqual(aNodes, [{ id: b.id, host: '127.0.0.1', port: b.address().port }])
b.destroy()
a.destroy()
node.destroy()
t.end()
})
}, 1)
})
b.addNodes([{ id: a.id, host: '127.0.0.1', port: a.address().port }])
})
})
})
function bootstrap (done) {
const node = dht({
ephemeral: true

Loading…
Cancel
Save