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

Commit

Permalink
chore: fix linting and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 15, 2023
1 parent 8bc0ae9 commit 6e61063
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { ipniContentRouting } from '@libp2p/ipni-content-routing'

const node = await createLibp2p({
peerRouting: [
ipniContentRouting(new URL('https://cid.contact'))
ipniContentRouting('https://cid.contact')
]
//.. other config
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"@libp2p/interfaces": "^3.3.1",
"@libp2p/logger": "^2.0.7",
"@libp2p/peer-id": "^2.0.3",
"@multiformats/multiaddr": "^12.1.2",
"any-signal": "^4.1.1",
"browser-readablestream-to-it": "^2.0.2",
"iterable-ndjson": "^1.1.0",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IpniResponseItem {
export interface IpniContentRoutingInit {
/**
* A concurrency limit to avoid request flood in web browser (default: 4)
*
* @see https://github.com/libp2p/js-libp2p-delegated-content-routing/issues/12
*/
concurrentRequests?: number
Expand All @@ -50,21 +51,21 @@ const defaultValues = {
class IpniContentRouting implements ContentRouting, Startable {
private started: boolean
private readonly httpQueue: PQueue
private shutDownController: AbortController
private clientUrl: URL
private timeout: number
private readonly shutDownController: AbortController
private readonly clientUrl: URL
private readonly timeout: number

/**
* Create a new DelegatedContentRouting instance
*/
constructor (url: URL, init: IpniContentRoutingInit = {}) {
constructor (url: string | URL, init: IpniContentRoutingInit = {}) {
log('enabled IPNI routing via', url)
this.started = false
this.shutDownController = new AbortController()
this.httpQueue = new PQueue({
concurrency: init.concurrentRequests ?? defaultValues.concurrentRequests
})
this.clientUrl = url
this.clientUrl = url instanceof URL ? url : new URL(url)
this.timeout = init.timeout ?? defaultValues.timeout
}

Expand Down Expand Up @@ -114,7 +115,6 @@ class IpniContentRouting implements ContentRouting, Startable {
}
} catch (err) {
log.error('findProviders errored:', err)
throw err
} finally {
signal.clear()
onFinish.resolve()
Expand Down Expand Up @@ -153,6 +153,6 @@ class IpniContentRouting implements ContentRouting, Startable {
}
}

export function ipniContentRouting (url: URL, init: IpniContentRoutingInit = {}): () => ContentRouting {
export function ipniContentRouting (url: string | URL, init: IpniContentRoutingInit = {}): () => ContentRouting {
return () => new IpniContentRouting(url, init)
}
63 changes: 58 additions & 5 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { ipniContentRouting } from '../src/index.js'
import { CID } from 'multiformats/cid'
import all from 'it-all'

if (process.env.ECHO_SERVER == null) {
throw new Error('Echo server not configured correctly')
}

const serverUrl = process.env.ECHO_SERVER

describe('IPNIContentRouting', function () {
it('should find providers', async () => {
if (process.env.ECHO_SERVER == null) {
throw new Error('Echo server not configured correctly')
}

const providers = [{
Metadata: 'gBI=',
ContextID: '',
Expand All @@ -36,7 +38,7 @@ describe('IPNIContentRouting', function () {
body: providers.map(prov => JSON.stringify(prov)).join('\n')
})

const routing = ipniContentRouting(new URL(process.env.ECHO_SERVER))()
const routing = ipniContentRouting(serverUrl)()

const provs = await all(routing.findProviders(cid))
expect(provs.map(prov => ({
Expand All @@ -47,4 +49,55 @@ describe('IPNIContentRouting', function () {
addrs: prov.Provider.Addrs
})))
})

it('should handle non-json input', async () => {
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')

// load providers for the router to fetch
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
method: 'POST',
body: 'not json'
})

const routing = ipniContentRouting(serverUrl)()

const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})

it('should handle bad input providers', async () => {
const providers = [{
Metadata: 'gBI=',
Provider: {
Bad: 'field'
}
}, {
Metadata: 'gBI=',
ContextID: '',
Another: {
Bad: 'field'
}
}]

const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')

// load providers for the router to fetch
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
method: 'POST',
body: providers.map(prov => JSON.stringify(prov)).join('\n')
})

const routing = ipniContentRouting(serverUrl)()

const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})

it('should handle empty input', async () => {
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
const routing = ipniContentRouting(serverUrl)()

const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})
})

0 comments on commit 6e61063

Please sign in to comment.