Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 6e61063

Browse files
committed
chore: fix linting and add tests
1 parent 8bc0ae9 commit 6e61063

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { ipniContentRouting } from '@libp2p/ipni-content-routing'
3838

3939
const node = await createLibp2p({
4040
peerRouting: [
41-
ipniContentRouting(new URL('https://cid.contact'))
41+
ipniContentRouting('https://cid.contact')
4242
]
4343
//.. other config
4444
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"@libp2p/interfaces": "^3.3.1",
142142
"@libp2p/logger": "^2.0.7",
143143
"@libp2p/peer-id": "^2.0.3",
144+
"@multiformats/multiaddr": "^12.1.2",
144145
"any-signal": "^4.1.1",
145146
"browser-readablestream-to-it": "^2.0.2",
146147
"iterable-ndjson": "^1.1.0",

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface IpniResponseItem {
2929
export interface IpniContentRoutingInit {
3030
/**
3131
* A concurrency limit to avoid request flood in web browser (default: 4)
32+
*
3233
* @see https://github.com/libp2p/js-libp2p-delegated-content-routing/issues/12
3334
*/
3435
concurrentRequests?: number
@@ -50,21 +51,21 @@ const defaultValues = {
5051
class IpniContentRouting implements ContentRouting, Startable {
5152
private started: boolean
5253
private readonly httpQueue: PQueue
53-
private shutDownController: AbortController
54-
private clientUrl: URL
55-
private timeout: number
54+
private readonly shutDownController: AbortController
55+
private readonly clientUrl: URL
56+
private readonly timeout: number
5657

5758
/**
5859
* Create a new DelegatedContentRouting instance
5960
*/
60-
constructor (url: URL, init: IpniContentRoutingInit = {}) {
61+
constructor (url: string | URL, init: IpniContentRoutingInit = {}) {
6162
log('enabled IPNI routing via', url)
6263
this.started = false
6364
this.shutDownController = new AbortController()
6465
this.httpQueue = new PQueue({
6566
concurrency: init.concurrentRequests ?? defaultValues.concurrentRequests
6667
})
67-
this.clientUrl = url
68+
this.clientUrl = url instanceof URL ? url : new URL(url)
6869
this.timeout = init.timeout ?? defaultValues.timeout
6970
}
7071

@@ -114,7 +115,6 @@ class IpniContentRouting implements ContentRouting, Startable {
114115
}
115116
} catch (err) {
116117
log.error('findProviders errored:', err)
117-
throw err
118118
} finally {
119119
signal.clear()
120120
onFinish.resolve()
@@ -153,6 +153,6 @@ class IpniContentRouting implements ContentRouting, Startable {
153153
}
154154
}
155155

156-
export function ipniContentRouting (url: URL, init: IpniContentRoutingInit = {}): () => ContentRouting {
156+
export function ipniContentRouting (url: string | URL, init: IpniContentRoutingInit = {}): () => ContentRouting {
157157
return () => new IpniContentRouting(url, init)
158158
}

test/index.spec.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { ipniContentRouting } from '../src/index.js'
66
import { CID } from 'multiformats/cid'
77
import all from 'it-all'
88

9+
if (process.env.ECHO_SERVER == null) {
10+
throw new Error('Echo server not configured correctly')
11+
}
12+
13+
const serverUrl = process.env.ECHO_SERVER
14+
915
describe('IPNIContentRouting', function () {
1016
it('should find providers', async () => {
11-
if (process.env.ECHO_SERVER == null) {
12-
throw new Error('Echo server not configured correctly')
13-
}
14-
1517
const providers = [{
1618
Metadata: 'gBI=',
1719
ContextID: '',
@@ -36,7 +38,7 @@ describe('IPNIContentRouting', function () {
3638
body: providers.map(prov => JSON.stringify(prov)).join('\n')
3739
})
3840

39-
const routing = ipniContentRouting(new URL(process.env.ECHO_SERVER))()
41+
const routing = ipniContentRouting(serverUrl)()
4042

4143
const provs = await all(routing.findProviders(cid))
4244
expect(provs.map(prov => ({
@@ -47,4 +49,55 @@ describe('IPNIContentRouting', function () {
4749
addrs: prov.Provider.Addrs
4850
})))
4951
})
52+
53+
it('should handle non-json input', async () => {
54+
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
55+
56+
// load providers for the router to fetch
57+
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
58+
method: 'POST',
59+
body: 'not json'
60+
})
61+
62+
const routing = ipniContentRouting(serverUrl)()
63+
64+
const provs = await all(routing.findProviders(cid))
65+
expect(provs).to.be.empty()
66+
})
67+
68+
it('should handle bad input providers', async () => {
69+
const providers = [{
70+
Metadata: 'gBI=',
71+
Provider: {
72+
Bad: 'field'
73+
}
74+
}, {
75+
Metadata: 'gBI=',
76+
ContextID: '',
77+
Another: {
78+
Bad: 'field'
79+
}
80+
}]
81+
82+
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
83+
84+
// load providers for the router to fetch
85+
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
86+
method: 'POST',
87+
body: providers.map(prov => JSON.stringify(prov)).join('\n')
88+
})
89+
90+
const routing = ipniContentRouting(serverUrl)()
91+
92+
const provs = await all(routing.findProviders(cid))
93+
expect(provs).to.be.empty()
94+
})
95+
96+
it('should handle empty input', async () => {
97+
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
98+
const routing = ipniContentRouting(serverUrl)()
99+
100+
const provs = await all(routing.findProviders(cid))
101+
expect(provs).to.be.empty()
102+
})
50103
})

0 commit comments

Comments
 (0)