Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat(dht): add API to allow options in findprovs() (#337)
Browse files Browse the repository at this point in the history
* feat(dht): add API to allow options in `findprovs()`

As discussed in: #1322 (comment)

License: MIT
Signed-off-by: Pascal Precht <pascal.precht@gmail.com>

* fix: typo in test name

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
0x-r4bbit authored and alanshaw committed Aug 2, 2018
1 parent 0640d03 commit 99f74f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion SPEC/DHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions js/src/dht/findprovs.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -54,5 +66,18 @@ module.exports = (createCommon, options) => {
}
], done)
})

it('should take options to override timeout 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)
})
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 99f74f5

Please sign in to comment.