Skip to content

Commit d36b085

Browse files
author
Alan Shaw
authoredFeb 4, 2020
chore: update ipfs-http-client dep (#23)
* chore: update ipfs-http-client dep * chore: update ipfs-http-client dep * chore: update ipfs-http-client dep * fix: specify number of providers to retrieve
1 parent f672b84 commit d36b085

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed
 

‎package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@
2121
"aegir": "^20.4.1",
2222
"chai": "^4.2.0",
2323
"cids": "^0.7.1",
24-
"go-ipfs-dep": "~0.4.22",
24+
"go-ipfs-dep": "^0.4.23",
2525
"ipfsd-ctl": "~0.47.4",
26-
"peer-id": "~0.13.5"
26+
"peer-id": "^0.13.7"
2727
},
2828
"dependencies": {
29-
"it-all": "^1.0.0",
3029
"debug": "^4.1.1",
31-
"ipfs-http-client": "^40.0.1",
30+
"ipfs-http-client": "^42.0.0",
31+
"it-all": "^1.0.0",
3232
"multiaddr": "^7.2.1",
33-
"p-queue": "^6.2.1"
33+
"p-defer": "^3.0.0",
34+
"p-queue": "^6.2.1",
35+
"peer-info": "^0.17.1"
3436
},
3537
"contributors": [
3638
"Alan Shaw <alan.shaw@protocol.ai>",

‎src/index.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict'
22

33
const debug = require('debug')
4-
5-
const dht = require('ipfs-http-client/src/dht')
6-
const refs = require('ipfs-http-client/src/refs')
7-
const getEndpointConfig = require('ipfs-http-client/src/get-endpoint-config')
4+
const PeerId = require('peer-id')
5+
const PeerInfo = require('peer-info')
6+
const createFindProvs = require('ipfs-http-client/src/dht/find-provs')
7+
const createRefs = require('ipfs-http-client/src/refs')
88

99
const { default: PQueue } = require('p-queue')
1010
const all = require('it-all')
11+
const defer = require('p-defer')
1112

1213
const log = debug('libp2p-delegated-content-routing')
1314
log.error = debug('libp2p-delegated-content-routing:error')
@@ -36,9 +37,9 @@ class DelegatedContentRouting {
3637
throw new Error('missing self peerId')
3738
}
3839

39-
this.api = Object.assign({}, getEndpointConfig()(), DEFAULT_IPFS_API, api)
40-
this.dht = dht(this.api)
41-
this.refs = refs(this.api)
40+
this.api = Object.assign({}, DEFAULT_IPFS_API, api)
41+
this.dht = { findProvs: createFindProvs(this.api) }
42+
this.refs = createRefs(this.api)
4243
this.peerId = peerId
4344

4445
// limit concurrency to avoid request flood in web browser
@@ -64,18 +65,35 @@ class DelegatedContentRouting {
6465
* @returns {AsyncIterable<PeerInfo>}
6566
*/
6667
async * findProviders (key, options = {}) {
67-
const keyString = key.toBaseEncodedString()
68-
log('findProviders starts: ' + keyString)
68+
const keyString = `${key}`
69+
log('findProviders starts:', keyString)
6970
options.timeout = options.timeout || DEFAULT_TIMEOUT
7071

71-
const results = await this._httpQueue.add(() => this.dht.findProvs(key, {
72-
timeout: `${options.timeout}ms` // The api requires specification of the time unit (s/ms)
73-
}))
72+
const onStart = defer()
73+
const onFinish = defer()
74+
75+
this._httpQueue.add(() => {
76+
onStart.resolve()
77+
return onFinish.promise
78+
})
79+
80+
try {
81+
await onStart.promise
82+
83+
const providers = this.dht.findProvs(key, {
84+
numProviders: options.numProviders,
85+
timeout: `${options.timeout}ms` // The api requires specification of the time unit (s/ms)
86+
})
7487

75-
for (let i = 0; i < results.length; i++) {
76-
yield results[i]
88+
for await (const { id, addrs } of providers) {
89+
const peerInfo = new PeerInfo(PeerId.createFromCID(id))
90+
addrs.forEach(addr => peerInfo.multiaddrs.add(addr))
91+
yield peerInfo
92+
}
93+
} finally {
94+
onFinish.resolve()
95+
log('findProviders finished:', keyString)
7796
}
78-
log('findProviders finished: ' + keyString)
7997
}
8098

8199
/**
@@ -90,12 +108,12 @@ class DelegatedContentRouting {
90108
* @returns {Promise<void>}
91109
*/
92110
async provide (key) {
93-
const keyString = key.toBaseEncodedString()
94-
log('provide starts: ' + keyString)
111+
const keyString = `${key}`
112+
log('provide starts:', keyString)
95113
const results = await this._httpQueueRefs.add(() =>
96114
all(this.refs(keyString, { recursive: false }))
97115
)
98-
log('provide finished: ', keyString, results)
116+
log('provide finished:', keyString, results)
99117
}
100118
}
101119

‎test/index.spec.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ describe('DelegatedContentRouting', function () {
7575
const router = new DelegatedContentRouting(selfId)
7676

7777
expect(router.api).to.include({
78-
'api-path': '/api/v0',
7978
protocol: 'https',
8079
port: 443,
8180
host: 'node0.delegate.ipfs.io'
@@ -88,7 +87,6 @@ describe('DelegatedContentRouting', function () {
8887
})
8988

9089
expect(router.api).to.include({
91-
'api-path': '/api/v0',
9290
protocol: 'https',
9391
port: 443,
9492
host: 'other.ipfs.io'
@@ -97,7 +95,6 @@ describe('DelegatedContentRouting', function () {
9795

9896
it('should allow for overriding the api', () => {
9997
const api = {
100-
'api-path': '/api/v1',
10198
protocol: 'http',
10299
port: 8000,
103100
host: 'localhost'
@@ -124,7 +121,7 @@ describe('DelegatedContentRouting', function () {
124121
host: opts.host
125122
})
126123

127-
const providers = await all(routing.findProviders(cid))
124+
const providers = await all(routing.findProviders(cid, { numProviders: 2 }))
128125

129126
// We should get the bootstrap node as provider
130127
// The delegate node is not included, because it is handling the requests
@@ -141,7 +138,7 @@ describe('DelegatedContentRouting', function () {
141138
})
142139

143140
const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv')
144-
const providers = await all(routing.findProviders(cid, { timeout: 5e3 }))
141+
const providers = await all(routing.findProviders(cid, { numProviders: 2, timeout: 5e3 }))
145142

146143
expect(providers.map((p) => p.id.toB58String())).to.include(bootstrapId.id, 'Did not include bootstrap node')
147144
})

0 commit comments

Comments
 (0)