Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit edb1852

Browse files
committed
chore(dht): add API to allow options in findProvs()
As discussed in: ipfs/js-ipfs#1322 (comment) License: MIT Signed-off-by: Pascal Precht <[email protected]>
1 parent 7ed2b58 commit edb1852

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

SPEC/DHT.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ A great source of [examples][] can be found in the tests for this API.
3939
4040
##### `Go` **WIP**
4141

42-
##### `JavaScript` - ipfs.dht.findprovs(hash, [callback])
42+
##### `JavaScript` - ipfs.dht.findprovs(hash, [options], callback])
4343

4444
Where `hash` is a multihash.
4545

46+
`options` an optional object with the following properties
47+
- `timeout` - a maximum timeout in milliseconds
48+
4649
`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)
4750

4851
If no `callback` is passed, a promise is returned.
@@ -51,6 +54,8 @@ If no `callback` is passed, a promise is returned.
5154

5255
```JavaScript
5356
ipfs.dht.findprovs(multihash, function (err, peerInfos) {})
57+
58+
ipfs.dht.findprovs(multihash, { timeout: 4000 }, function (err, peerInfos) {})
5459
```
5560

5661
A great source of [examples][] can be found in the tests for this API.

js/src/dht/findprovs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const multihashing = require('multihashing-async')
5+
const promisify = require('util').promisify
6+
const Crypto = require('crypto')
47
const waterfall = require('async/waterfall')
58
const CID = require('cids')
69
const { spawnNodesWithId } = require('../utils/spawn')
710
const { getDescribe, getIt, expect } = require('../utils/mocha')
811
const { connect } = require('../utils/swarm')
912

13+
function fakeCid (cb) {
14+
const bytes = Crypto.randomBytes(Math.round(Math.random() * 1000))
15+
multihashing(bytes, 'sha2-256', (err, mh) => {
16+
if (err) {
17+
cb(err)
18+
}
19+
cb(new CID(0, 'dag-pb', mh))
20+
})
21+
}
22+
1023
module.exports = (createCommon, options) => {
1124
const describe = getDescribe(options)
1225
const it = getIt(options)
@@ -54,5 +67,18 @@ module.exports = (createCommon, options) => {
5467
}
5568
], done)
5669
})
70+
71+
it('should take options to override timout config', function (done) {
72+
const options = {
73+
timeout: 1
74+
}
75+
waterfall([
76+
(cb) => fakeCid(cb),
77+
(cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => {
78+
expect(err).to.exist()
79+
cb(null)
80+
}),
81+
], done)
82+
});
5783
})
5884
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"chai": "^4.1.2",
3939
"cids": "~0.5.3",
4040
"concat-stream": "^1.6.2",
41+
"crypto": "^1.0.1",
4142
"dirty-chai": "^2.0.1",
4243
"hat": "0.0.3",
4344
"ipfs-block": "~0.7.1",

0 commit comments

Comments
 (0)