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

Commit 535b1ec

Browse files
alanshawdaviddias
authored andcommitted
docs: Adds missing docs for dht.provide (#221)
* adds missing docs for dht.provide As far as I can tell from the [tests](https://github.com/ipfs/interface-ipfs-core/blob/f5f73070c37f374e0516185b8241622d514d09ae/js/src/dht.js#L124) and [`js-ipfs`](https://github.com/ipfs/js-ipfs/blob/e4d2a15143431a9af37a275a149621738c91aa00/src/core/components/dht.js#L119) (follows through to [`js-ipfs-repo`](https://github.com/ipfs/js-ipfs-repo/blob/c30a7f170a631cc6298c06340bd92321b6fe1cd7/src/blockstore.js#L136) `.has` which expects a CID) it just takes a CID. `js-ipfs` implementation also suggests it can [take an array](https://github.com/ipfs/js-ipfs/blob/e4d2a15143431a9af37a275a149621738c91aa00/src/core/components/dht.js#L109). * adds tests for multiple CID and non CID args * fix up the tests, add test for no provide if not local * adds test for CIDv1
1 parent 5bb90ac commit 535b1ec

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

SPEC/DHT.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ ipfs.dht.get(key, function (err, value) {})
7171

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

74+
#### `provide`
75+
76+
> Announce to the network that you are providing given values.
77+
78+
##### `Go` **WIP**
79+
80+
##### `JavaScript` - ipfs.dht.provide(cid, [callback])
81+
82+
Where `cid` is a CID or array of CIDs.
83+
84+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
85+
86+
If no `callback` is passed, a promise is returned.
87+
88+
**Example:**
89+
90+
```JavaScript
91+
ipfs.dht.provide(cid, function (err) {})
92+
```
93+
94+
A great source of [examples][] can be found in the tests for this API.
95+
7496
#### `put`
7597

7698
> Store a value on the DHT

js/src/dht.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,65 @@ module.exports = (common) => {
131131

132132
describe('.provide', () => {
133133
it('regular', (done) => {
134-
// TODO recheck this test, should it provide or not if block is not available? go-ipfs does provide.
134+
nodeC.files.add(Buffer.from('test'), (err, res) => {
135+
if (err) return done(err)
136+
137+
nodeC.dht.provide(new CID(res[0].hash), (err) => {
138+
expect(err).to.not.exist()
139+
done()
140+
})
141+
})
142+
})
143+
144+
it('should not provide if block not found locally', (done) => {
135145
const cid = new CID('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ')
136146

137-
nodeC.dht.provide(cid, done)
147+
nodeC.dht.provide(cid, (err) => {
148+
expect(err).to.exist()
149+
expect(err.message).to.include('not found locally')
150+
done()
151+
})
152+
})
153+
154+
it('allows multiple CIDs to be passed', (done) => {
155+
nodeC.files.add([Buffer.from('t0'), Buffer.from('t1')], (err, res) => {
156+
if (err) return done(err)
157+
158+
nodeC.dht.provide([
159+
new CID(res[0].hash),
160+
new CID(res[1].hash)
161+
], (err) => {
162+
expect(err).to.not.exist()
163+
done()
164+
})
165+
})
166+
})
167+
168+
it('should provide a CIDv1', (done) => {
169+
nodeC.files.add(Buffer.from('test'), { 'cid-version': 1 }, (err, res) => {
170+
if (err) return done(err)
171+
172+
const cid = new CID(res[0].hash)
173+
174+
nodeC.dht.provide(cid, (err) => {
175+
expect(err).to.not.exist()
176+
done()
177+
})
178+
})
179+
})
180+
181+
it('errors on non CID arg', (done) => {
182+
nodeC.dht.provide({}, (err) => {
183+
expect(err).to.exist()
184+
done()
185+
})
186+
})
187+
188+
it('errors on array containing non CID arg', (done) => {
189+
nodeC.dht.provide([{}], (err) => {
190+
expect(err).to.exist()
191+
done()
192+
})
138193
})
139194

140195
it.skip('recursive', () => {})

0 commit comments

Comments
 (0)