Skip to content

Commit 003888a

Browse files
authoredAug 26, 2020
fix: accept http client instance (#43)
* fix: accept http client instance Instead of the http client being a dependency of this module, accept a pre-configured http client instance at construction time. This resolves the weird dependency loop and means we can remove this module from the no-hoist config in ipfs. BREAKING CHANGE: - An instance of ipfs http client is now a required argument * chore: update node versions * chore: update readme * chore: restore peer deps
1 parent cdedac2 commit 003888a

File tree

5 files changed

+54
-58
lines changed

5 files changed

+54
-58
lines changed
 

‎.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ stages:
66
- cov
77

88
node_js:
9-
- '10'
10-
- '12'
9+
- 'lts/*'
10+
- 'node'
1111

1212
os:
1313
- linux

‎README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Requires access to `/api/v0/dht/findprovs` and `/api/v0/refs` HTTP API endpoints
1818

1919
## Requirements
2020

21-
`libp2p-delegated-content-routing` leverages the `ipfs-http-client` library and requires it as a peer dependency, as such, both must be installed in order for this module to work properly.
21+
`libp2p-delegated-content-routing` leverages the `ipfs-http-client` library and requires an instance of it as a constructor argument.
2222

2323
```sh
2424
npm install ipfs-http-client libp2p-delegated-content-routing
@@ -28,14 +28,15 @@ npm install ipfs-http-client libp2p-delegated-content-routing
2828

2929
```js
3030
const DelegatedContentRouting = require('libp2p-delegated-content-routing')
31+
const ipfsHttpClient = require('ipfs-http-client')
3132

3233
// default is to use ipfs.io
33-
const routing = new DelegatedContentRouing(peerId, {
34+
const routing = new DelegatedContentRouing(peerId, ipfsHttpClient({
3435
// use default api settings
3536
protocol: 'https',
3637
port: 443,
3738
host: 'ipfs.io'
38-
})
39+
}))
3940
const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv')
4041

4142
for await (const { id, multiaddrs } of routing.findProviders(cid)) {

‎package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818
"coverage": "aegir coverage"
1919
},
2020
"devDependencies": {
21-
"aegir": "^25.1.0",
21+
"aegir": "^26.0.0",
2222
"go-ipfs": "^0.6.0",
2323
"ipfs-http-client": "^46.0.0",
24-
"ipfs-utils": "^2.4.0",
24+
"ipfs-utils": "^3.0.0",
2525
"ipfsd-ctl": "^5.0.0",
2626
"it-drain": "^1.0.0",
2727
"peer-id": "^0.14.0",
2828
"uint8arrays": "^1.1.0"
2929
},
30-
"peerDependencies": {
31-
"ipfs-http-client": "^46.0.0"
32-
},
3330
"dependencies": {
3431
"debug": "^4.1.1",
3532
"it-all": "^1.0.0",
3633
"multiaddr": "^8.0.0",
3734
"p-defer": "^3.0.0",
3835
"p-queue": "^6.2.1"
3936
},
37+
"peerDependencies": {
38+
"ipfs-http-client": "^46.0.0"
39+
},
40+
"browser": {
41+
"go-ipfs": false
42+
},
4043
"contributors": [
4144
"Jacob Heun <jacobheun@gmail.com>",
4245
"Vasco Santos <vasco.santos@moxy.studio>",

‎src/index.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const debug = require('debug')
44
const PeerId = require('peer-id')
5-
const createFindProvs = require('ipfs-http-client/src/dht/find-provs')
6-
const createRefs = require('ipfs-http-client/src/refs')
75

86
const { default: PQueue } = require('p-queue')
97
const all = require('it-all')
@@ -13,12 +11,6 @@ const log = debug('libp2p-delegated-content-routing')
1311
log.error = debug('libp2p-delegated-content-routing:error')
1412

1513
const DEFAULT_TIMEOUT = 30e3 // 30 second default
16-
const DEFAULT_IPFS_API = {
17-
protocol: 'https',
18-
port: 443,
19-
host: 'node0.delegate.ipfs.io'
20-
}
21-
2214
const CONCURRENT_HTTP_REQUESTS = 4
2315

2416
/**
@@ -29,16 +21,18 @@ class DelegatedContentRouting {
2921
* Create a new DelegatedContentRouting instance.
3022
*
3123
* @param {PeerID} peerId - the id of the node that is using this routing.
32-
* @param {object} [api] - (Optional) the api endpoint of the delegated node to use.
24+
* @param {object} [client] - an instance of the ipfs-http-client module
3325
*/
34-
constructor (peerId, api) {
26+
constructor (peerId, client) {
3527
if (peerId == null) {
3628
throw new Error('missing self peerId')
3729
}
3830

39-
this.api = Object.assign({}, DEFAULT_IPFS_API, api)
40-
this.dht = { findProvs: createFindProvs(this.api) }
41-
this.refs = createRefs(this.api)
31+
if (client == null) {
32+
throw new Error('missing ipfs http client')
33+
}
34+
35+
this._client = client
4236
this.peerId = peerId
4337

4438
// limit concurrency to avoid request flood in web browser
@@ -50,7 +44,14 @@ class DelegatedContentRouting {
5044
this._httpQueueRefs = new PQueue(Object.assign({}, concurrency, {
5145
concurrency: 2
5246
}))
53-
log(`enabled DelegatedContentRouting via ${this.api.protocol}://${this.api.host}:${this.api.port}`)
47+
48+
const {
49+
protocol,
50+
host,
51+
port
52+
} = client.getEndpointConfig()
53+
54+
log(`enabled DelegatedContentRouting via ${protocol}://${host}:${port}`)
5455
}
5556

5657
/**
@@ -80,7 +81,7 @@ class DelegatedContentRouting {
8081
try {
8182
await onStart.promise
8283

83-
for await (const { id, addrs } of this.dht.findProvs(key, {
84+
for await (const { id, addrs } of this._client.dht.findProvs(key, {
8485
numProviders: options.numProviders,
8586
timeout: options.timeout
8687
})) {
@@ -113,7 +114,7 @@ class DelegatedContentRouting {
113114
const keyString = `${key}`
114115
log('provide starts:', keyString)
115116
const results = await this._httpQueueRefs.add(() =>
116-
all(this.refs(keyString, { recursive: false }))
117+
all(this._client.refs(keyString, { recursive: false }))
117118
)
118119
log('provide finished:', keyString, results)
119120
}

‎test/index.spec.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
const { expect } = require('aegir/utils/chai')
55
const { createFactory } = require('ipfsd-ctl')
6-
const { CID } = require('ipfs-http-client')
6+
const ipfsHttpClient = require('ipfs-http-client')
7+
const { CID } = ipfsHttpClient
78
const PeerId = require('peer-id')
89
const all = require('it-all')
910
const drain = require('it-drain')
@@ -76,37 +77,27 @@ describe('DelegatedContentRouting', function () {
7677
expect(() => new DelegatedContentRouting()).to.throw()
7778
})
7879

79-
it('should default to https://node0.delegate.ipfs.io as the delegate', () => {
80-
const router = new DelegatedContentRouting(selfId)
81-
82-
expect(router.api).to.include({
83-
protocol: 'https',
84-
port: 443,
85-
host: 'node0.delegate.ipfs.io'
86-
})
87-
})
88-
89-
it('should allow for just specifying the host', () => {
90-
const router = new DelegatedContentRouting(selfId, {
91-
host: 'other.ipfs.io'
92-
})
93-
94-
expect(router.api).to.include({
95-
protocol: 'https',
96-
port: 443,
97-
host: 'other.ipfs.io'
98-
})
80+
it('should require ipfs http client', () => {
81+
expect(() => new DelegatedContentRouting(selfId)).to.throw()
9982
})
10083

101-
it('should allow for overriding the api', () => {
102-
const api = {
84+
it('should accept an http api client instance at construction time', () => {
85+
const client = ipfsHttpClient({
10386
protocol: 'http',
10487
port: 8000,
10588
host: 'localhost'
106-
}
107-
const router = new DelegatedContentRouting(selfId, api)
89+
})
90+
const router = new DelegatedContentRouting(selfId, client)
91+
92+
expect(router).to.have.property('_client')
93+
.that.has.property('getEndpointConfig')
94+
.that.is.a('function')
10895

109-
expect(router.api).to.include(api)
96+
expect(router._client.getEndpointConfig()).to.deep.include({
97+
protocol: 'http:',
98+
port: '8000',
99+
host: 'localhost'
100+
})
110101
})
111102
})
112103

@@ -127,11 +118,11 @@ describe('DelegatedContentRouting', function () {
127118

128119
it('should be able to find providers through the delegate node', async function () {
129120
const opts = delegateNode.apiAddr.toOptions()
130-
const routing = new DelegatedContentRouting(selfId, {
121+
const routing = new DelegatedContentRouting(selfId, ipfsHttpClient({
131122
protocol: 'http',
132123
port: opts.port,
133124
host: opts.host
134-
})
125+
}))
135126

136127
const providers = await all(routing.findProviders(cid, { numProviders: 2 }))
137128

@@ -143,11 +134,11 @@ describe('DelegatedContentRouting', function () {
143134

144135
it('should be able to specify a timeout', async () => {
145136
const opts = delegateNode.apiAddr.toOptions()
146-
const routing = new DelegatedContentRouting(selfId, {
137+
const routing = new DelegatedContentRouting(selfId, ipfsHttpClient({
147138
protocol: 'http',
148139
port: opts.port,
149140
host: opts.host
150-
})
141+
}))
151142

152143
const providers = await all(routing.findProviders(cid, { numProviders: 2, timeout: 5e3 }))
153144

@@ -158,11 +149,11 @@ describe('DelegatedContentRouting', function () {
158149
describe('provide', () => {
159150
it('should be able to register as a content provider to the delegate node', async () => {
160151
const opts = delegateNode.apiAddr.toOptions()
161-
const contentRouter = new DelegatedContentRouting(selfId, {
152+
const contentRouter = new DelegatedContentRouting(selfId, ipfsHttpClient({
162153
protocol: 'http',
163154
port: opts.port,
164155
host: opts.host
165-
})
156+
}))
166157

167158
const { cid } = await selfNode.api.add(uint8ArrayFromString(`hello-${Math.random()}`))
168159

0 commit comments

Comments
 (0)
Please sign in to comment.