Skip to content

Commit

Permalink
feat(verified-fetch): customize ipns dnsResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Feb 21, 2024
1 parent 8db7792 commit 77c127a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipns as heliaIpns, type IPNS } from '@helia/ipns'
import { ipns as heliaIpns, type DNSResolver, type IPNS } from '@helia/ipns'
import { dnsJsonOverHttps } from '@helia/ipns/dns-resolvers'
import { unixfs as heliaUnixFs, type UnixFS as HeliaUnixFs, type UnixFSStats } from '@helia/unixfs'
import { code as dagCborCode } from '@ipld/dag-cbor'
Expand Down Expand Up @@ -32,6 +32,7 @@ interface VerifiedFetchComponents {
*/
interface VerifiedFetchInit {
contentTypeParser?: ContentTypeParser
dnsResolvers?: DNSResolver[]
}

interface FetchHandlerFunctionArg {
Expand Down Expand Up @@ -86,7 +87,7 @@ export class VerifiedFetch {
this.helia = helia
this.log = helia.logger.forComponent('helia:verified-fetch')
this.ipns = ipns ?? heliaIpns(helia, {
resolvers: [
resolvers: init?.dnsResolvers ?? [
dnsJsonOverHttps('https://mozilla.cloudflare-dns.com/dns-query'),
dnsJsonOverHttps('https://dns.google/resolve')
]
Expand Down
27 changes: 27 additions & 0 deletions packages/verified-fetch/test/verified-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,31 @@ describe('@helia/verifed-fetch', () => {
await expect(resp.text()).to.eventually.equal('hello world')
})
})

describe('custom dns-resolvers', () => {
it('uses custom dnsResolvers if provided', async () => {
const customResolver1 = Sinon.stub()
const customResolver2 = Sinon.stub()
const onProgress = Sinon.stub()

customResolver1.rejects(new Error('Could not resolve PeerId "mydomain.com"'))
customResolver2.returns(Promise.resolve('/ipfs/QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg'))

const verifiedFetch = new VerifiedFetch({
helia
}, {
dnsResolvers: [customResolver1, customResolver2]
})
try {
await verifiedFetch.fetch('ipns://mydomain.com', { onProgress })
} catch {
// ignoring error of walking the CID because we haven't actually added the block to the blockstore
}
expect(customResolver1.callCount).to.equal(1)
expect(customResolver1.getCall(0).args).to.deep.equal(['mydomain.com', { onProgress }])
await expect(customResolver1.getCall(0).returnValue).to.eventually.be.rejectedWith('Could not resolve PeerId "mydomain.com"')
expect(customResolver2.callCount).to.equal(1)
expect(customResolver2.getCall(0).args).to.deep.equal(['mydomain.com', { onProgress }])
})
})
})

0 comments on commit 77c127a

Please sign in to comment.