From eb1be425b8c7e39d6b436cc72109cff01e99ccf3 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Fri, 20 Jul 2018 15:29:28 +0200 Subject: [PATCH] chore(dht): add API to allow options in `findProvs()` As discussed in: https://github.com/ipfs/js-ipfs/issues/1322#issuecomment-385336102 License: MIT Signed-off-by: Pascal Precht --- SPEC/DHT.md | 7 ++++++- js/src/dht/findprovs.js | 25 +++++++++++++++++++++++++ package.json | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/SPEC/DHT.md b/SPEC/DHT.md index fc9927ce..7be88b09 100644 --- a/SPEC/DHT.md +++ b/SPEC/DHT.md @@ -39,10 +39,13 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.dht.findprovs(hash, [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 + `callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` is an array of objects of type [PeerInfo](https://github.com/libp2p/js-peer-info) If no `callback` is passed, a promise is returned. @@ -51,6 +54,8 @@ If no `callback` is passed, a promise is returned. ```JavaScript ipfs.dht.findprovs(multihash, function (err, peerInfos) {}) + +ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, peerInfos) {}) ``` A great source of [examples][] can be found in the tests for this API. diff --git a/js/src/dht/findprovs.js b/js/src/dht/findprovs.js index 04e6385c..9103f0a5 100644 --- a/js/src/dht/findprovs.js +++ b/js/src/dht/findprovs.js @@ -1,12 +1,24 @@ /* eslint-env mocha */ 'use strict' +const multihashing = require('multihashing-async') +const Crypto = require('crypto') const waterfall = require('async/waterfall') const CID = require('cids') const { spawnNodesWithId } = require('../utils/spawn') const { getDescribe, getIt, expect } = require('../utils/mocha') const { connect } = require('../utils/swarm') +function fakeCid (cb) { + const bytes = Crypto.randomBytes(Math.round(Math.random() * 1000)) + multihashing(bytes, 'sha2-256', (err, mh) => { + if (err) { + cb(err) + } + cb(null, new CID(0, 'dag-pb', mh)) + }) +} + module.exports = (createCommon, options) => { const describe = getDescribe(options) const it = getIt(options) @@ -54,5 +66,18 @@ module.exports = (createCommon, options) => { } ], done) }) + + it('should take options to override timout config', function (done) { + const options = { + timeout: 1 + } + waterfall([ + (cb) => fakeCid(cb), + (cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => { + expect(err).to.exist() + cb(null) + }), + ], done) + }); }) } diff --git a/package.json b/package.json index b2924686..ca70a8e3 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "chai": "^4.1.2", "cids": "~0.5.3", "concat-stream": "^1.6.2", + "crypto": "^1.0.1", "dirty-chai": "^2.0.1", "hat": "0.0.3", "ipfs-block": "~0.7.1",