diff --git a/index.js b/index.js index 223aaae..e32d979 100644 --- a/index.js +++ b/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) diff --git a/test.js b/test.js index a237a93..907f2f8 100644 --- a/test.js +++ b/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