Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: customize ipns dnsResolvers #445

Merged
merged 14 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]
}
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved

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
25 changes: 25 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,29 @@ describe('@helia/verifed-fetch', () => {
await expect(resp.text()).to.eventually.equal('hello world')
})
})

describe('custom dns-resolvers', () => {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
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]
})
// ignoring error of walking the CID because we haven't actually added the block to the blockstore
await expect(verifiedFetch.fetch('ipns://mydomain.com', { onProgress })).to.eventually.be.rejected
.with.property('code', 'ERR_EXPECTED_ERR_CODE')
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 }])
})
})
})
Loading