diff --git a/SPEC/DHT.md b/SPEC/DHT.md index 3e5bad829..aa5a654be 100644 --- a/SPEC/DHT.md +++ b/SPEC/DHT.md @@ -13,11 +13,11 @@ ##### Go **WIP** -##### JavaScript - `ipfs.dht.findpeer(peerId, [callback])` +##### JavaScript - `ipfs.dht.findPeer(peerId, [callback])` Where `peerId` is a IPFS/libp2p Id from [PeerId](https://github.com/libp2p/js-peer-id) type. -`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an object containing `responses` as an array of peer responses. In this case, as we are looking for a particular peer, there will be only one response. This response is composed by the peerId, as well as an array with its adresses. +`callback` must follow `function (err, peerInfo) {}` signature, where `err` is an error if the operation was not successful. `peerInfo` is an object of type `PeerInfo`. If no `callback` is passed, a promise is returned. @@ -26,10 +26,10 @@ If no `callback` is passed, a promise is returned. ```JavaScript var id = PeerId.create() -ipfs.dht.findpeer(id, function (err, res) { +ipfs.dht.findPeer(id, function (err, peerInfo) { // peerInfo will contain the multiaddrs of that peer - const id = res.responses[0].id - const addrs = res.responses[0].addrs + const id = peerInfo.id + const addrs = peerInfo.multiaddrs }) ``` @@ -41,23 +41,24 @@ A great source of [examples][] can be found in the tests for this API. ##### Go **WIP** -##### JavaScript - `ipfs.dht.findprovs(hash, [options], [callback])` +##### JavaScript - `ipfs.dht.findProvs(hash, [options], [callback])` Where `hash` is a multihash. `options` an optional object with the following properties - `timeout` - a maximum timeout in milliseconds + - `maxNumProviders` - a maximum number of providers to find -`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an object containing `responses` as an array of peer responses. Each entry of this array is composed by the peerId, as well as an array with its adresses. +`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` is an array of type `[PeerInfo]`. Each entry of this array is composed by the peerId, as well as an array with its adresses. If no `callback` is passed, a promise is returned. **Example:** ```JavaScript -ipfs.dht.findprovs(multihash, function (err, res) {}) +ipfs.dht.findProvs(multihash, function (err, res) {}) -ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, res) {}) +ipfs.dht.findProvs(multihash, { timeout: 4000 }, function (err, res) {}) ``` A great source of [examples][] can be found in the tests for this API. diff --git a/js/src/dht/findpeer.js b/js/src/dht/findpeer.js index 5c09b7370..980a26890 100644 --- a/js/src/dht/findpeer.js +++ b/js/src/dht/findpeer.js @@ -5,12 +5,14 @@ const { spawnNodesWithId } = require('../utils/spawn') const { getDescribe, getIt, expect } = require('../utils/mocha') const { connect } = require('../utils/swarm') +const checkAll = (bits) => string => bits.every(bit => string.includes(bit)) + module.exports = (createCommon, options) => { const describe = getDescribe(options) const it = getIt(options) const common = createCommon() - describe('.dht.findpeer', function () { + describe('.dht.findPeer', function () { this.timeout(80 * 1000) let nodeA @@ -42,16 +44,20 @@ module.exports = (createCommon, options) => { }) it('should find other peers', (done) => { - nodeA.dht.findpeer(nodeB.peerId.id, (err, res) => { + nodeA.dht.findPeer(nodeB.peerId.id, (err, res) => { expect(err).to.not.exist() - expect(res.responses[0].id).to.be.eql(nodeB.peerId.id) - expect(res.responses[0].addrs).to.deep.include(nodeB.peerId.addresses[0]) + + const id = res.id.toB58String() + const addrs = res.multiaddrs.toArray().map((ma) => ma.toString()) + + expect(id).to.be.eql(nodeB.peerId.id) + expect(nodeB.peerId.addresses[0]).to.satisfy(checkAll([addrs[0]])) done() }) }) it('should fail to find other peer if peer does not exist', (done) => { - nodeA.dht.findpeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => { + nodeA.dht.findPeer('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ', (err, peer) => { expect(err).to.exist() expect(peer).to.not.exist() done() diff --git a/js/src/dht/findprovs.js b/js/src/dht/findprovs.js index b6bc342bb..2179d097e 100644 --- a/js/src/dht/findprovs.js +++ b/js/src/dht/findprovs.js @@ -24,7 +24,7 @@ module.exports = (createCommon, options) => { const it = getIt(options) const common = createCommon() - describe('.dht.findprovs', function () { + describe('.dht.findProvs', function () { let nodeA let nodeB @@ -61,9 +61,9 @@ module.exports = (createCommon, options) => { (cid, cb) => { nodeB.dht.provide(cid, (err) => cb(err, cid)) }, - (cid, cb) => nodeA.dht.findprovs(cid, cb), + (cid, cb) => nodeA.dht.findProvs(cid, cb), (provs, cb) => { - expect(provs.responses.map((p) => p.id)) + expect(provs.map((p) => p.id.toB58String())) .to.eql([nodeB.peerId.id]) cb() } @@ -72,11 +72,11 @@ module.exports = (createCommon, options) => { it('should take options to override timeout config', function (done) { const options = { - timeout: 1 + maxTimeout: 1 } waterfall([ (cb) => fakeCid(cb), - (cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => { + (cidV0, cb) => nodeA.dht.findProvs(cidV0, options, (err) => { expect(err).to.exist() cb(null) }) diff --git a/js/src/dht/query.js b/js/src/dht/query.js index 05bf432ea..05bfd8ddb 100644 --- a/js/src/dht/query.js +++ b/js/src/dht/query.js @@ -59,7 +59,7 @@ module.exports = (createCommon, options) => { if (skipped) return clearTimeout(timeoutId) expect(err).to.not.exist() - expect(peers.map((p) => p.ID)).to.include(nodeB.peerId.id) + expect(peers.map((p) => p.id.toB58String())).to.include(nodeB.peerId.id) done() }) })