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

Commit 99f74f5

Browse files
0x-r4bbitalanshaw
authored andcommitted
feat(dht): add API to allow options in findprovs() (#337)
* feat(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]> * fix: typo in test name License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 0640d03 commit 99f74f5

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

SPEC/DHT.md

+6-1
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

+25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const multihashing = require('multihashing-async')
5+
const Crypto = require('crypto')
46
const waterfall = require('async/waterfall')
57
const CID = require('cids')
68
const { spawnNodesWithId } = require('../utils/spawn')
79
const { getDescribe, getIt, expect } = require('../utils/mocha')
810
const { connect } = require('../utils/swarm')
911

12+
function fakeCid (cb) {
13+
const bytes = Crypto.randomBytes(Math.round(Math.random() * 1000))
14+
multihashing(bytes, 'sha2-256', (err, mh) => {
15+
if (err) {
16+
cb(err)
17+
}
18+
cb(null, new CID(0, 'dag-pb', mh))
19+
})
20+
}
21+
1022
module.exports = (createCommon, options) => {
1123
const describe = getDescribe(options)
1224
const it = getIt(options)
@@ -54,5 +66,18 @@ module.exports = (createCommon, options) => {
5466
}
5567
], done)
5668
})
69+
70+
it('should take options to override timeout config', function (done) {
71+
const options = {
72+
timeout: 1
73+
}
74+
waterfall([
75+
(cb) => fakeCid(cb),
76+
(cidV0, cb) => nodeA.dht.findprovs(cidV0, options, (err) => {
77+
expect(err).to.exist()
78+
cb(null)
79+
})
80+
], done)
81+
})
5782
})
5883
}

package.json

+1
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)