148
148
*
149
149
* ```typescript
150
150
* import { createVerifiedFetch } from '@helia/verified-fetch'
151
- * import { dnsJsonOverHttps, dnsOverHttps } from '@helia/ipns/ dns- resolvers'
151
+ * import { dnsJsonOverHttps, dnsOverHttps } from '@multiformats/ dns/ resolvers'
152
152
*
153
153
* const fetch = await createVerifiedFetch({
154
154
* gateways: ['https://trustless-gateway.link'],
160
160
* })
161
161
* ```
162
162
*
163
+ * @example Customizing DNS per-TLD resolvers
164
+ *
165
+ * DNS resolvers can be configured to only service DNS queries for specific
166
+ * TLDs:
167
+ *
168
+ * ```typescript
169
+ * import { createVerifiedFetch } from '@helia/verified-fetch'
170
+ * import { dnsJsonOverHttps, dnsOverHttps } from '@multiformats/dns/resolvers'
171
+ *
172
+ * const fetch = await createVerifiedFetch({
173
+ * gateways: ['https://trustless-gateway.link'],
174
+ * routers: ['http://delegated-ipfs.dev'],
175
+ * dnsResolvers: {
176
+ * // this resolver will only be used for `.com` domains (note - this could
177
+ * // also be an array of resolvers)
178
+ * 'com.': dnsJsonOverHttps('https://my-dns-resolver.example.com/dns-json'),
179
+ * // this resolver will be used for everything else (note - this could
180
+ * // also be an array of resolvers)
181
+ * '.': dnsOverHttps('https://my-dns-resolver.example.com/dns-query')
182
+ * }
183
+ * })
184
+ * ```
185
+ *
163
186
* ### IPLD codec handling
164
187
*
165
188
* IPFS supports several data formats (typically referred to as codecs) which are included in the CID. `@helia/verified-fetch` attempts to abstract away some of the details for easier consumption.
569
592
import { trustlessGateway } from '@helia/block-brokers'
570
593
import { createHeliaHTTP } from '@helia/http'
571
594
import { delegatedHTTPRouting } from '@helia/routers'
595
+ import { dns } from '@multiformats/dns'
572
596
import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js'
573
597
import type { Helia } from '@helia/interface'
574
- import type { DNSResolver , IPNSRoutingEvents , ResolveDnsLinkProgressEvents , ResolveProgressEvents } from '@helia/ipns'
598
+ import type { ResolveDNSLinkProgressEvents } from '@helia/ipns'
575
599
import type { GetEvents } from '@helia/unixfs'
600
+ import type { DNSResolvers , DNS } from '@multiformats/dns'
601
+ import type { DNSResolver } from '@multiformats/dns/resolvers'
576
602
import type { CID } from 'multiformats/cid'
577
603
import type { ProgressEvent , ProgressOptions } from 'progress-events'
578
604
@@ -618,7 +644,7 @@ export interface CreateVerifiedFetchInit {
618
644
*
619
645
* @default [dnsJsonOverHttps('https://mozilla.cloudflare-dns.com/dns-query'),dnsJsonOverHttps('https://dns.google/resolve')]
620
646
*/
621
- dnsResolvers ?: DNSResolver [ ]
647
+ dnsResolvers ?: DNSResolver [ ] | DNSResolvers
622
648
}
623
649
624
650
export interface CreateVerifiedFetchOptions {
@@ -651,7 +677,7 @@ export type BubbledProgressEvents =
651
677
// unixfs
652
678
GetEvents |
653
679
// ipns
654
- ResolveProgressEvents | ResolveDnsLinkProgressEvents | IPNSRoutingEvents
680
+ ResolveDNSLinkProgressEvents
655
681
656
682
export type VerifiedFetchProgressEvents =
657
683
ProgressEvent < 'verified-fetch:request:start' , CIDDetail > |
@@ -674,20 +700,19 @@ export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledP
674
700
* Create and return a Helia node
675
701
*/
676
702
export async function createVerifiedFetch ( init ?: Helia | CreateVerifiedFetchInit , options ?: CreateVerifiedFetchOptions ) : Promise < VerifiedFetch > {
677
- let dnsResolvers : DNSResolver [ ] | undefined
678
703
if ( ! isHelia ( init ) ) {
679
- dnsResolvers = init ?. dnsResolvers
680
704
init = await createHeliaHTTP ( {
681
705
blockBrokers : [
682
706
trustlessGateway ( {
683
707
gateways : init ?. gateways
684
708
} )
685
709
] ,
686
- routers : ( init ?. routers ?? [ 'https://delegated-ipfs.dev' ] ) . map ( ( routerUrl ) => delegatedHTTPRouting ( routerUrl ) )
710
+ routers : ( init ?. routers ?? [ 'https://delegated-ipfs.dev' ] ) . map ( ( routerUrl ) => delegatedHTTPRouting ( routerUrl ) ) ,
711
+ dns : createDns ( init ?. dnsResolvers )
687
712
} )
688
713
}
689
714
690
- const verifiedFetchInstance = new VerifiedFetchClass ( { helia : init } , { dnsResolvers , ... options } )
715
+ const verifiedFetchInstance = new VerifiedFetchClass ( { helia : init } , options )
691
716
async function verifiedFetch ( resource : Resource , options ?: VerifiedFetchInit ) : Promise < Response > {
692
717
return verifiedFetchInstance . fetch ( resource , options )
693
718
}
@@ -707,3 +732,19 @@ function isHelia (obj: any): obj is Helia {
707
732
obj ?. stop != null &&
708
733
obj ?. start != null
709
734
}
735
+
736
+ function createDns ( resolvers ?: DNSResolver [ ] | DNSResolvers ) : DNS | undefined {
737
+ if ( resolvers == null ) {
738
+ return
739
+ }
740
+
741
+ if ( Array . isArray ( resolvers ) ) {
742
+ return dns ( {
743
+ resolvers : {
744
+ '.' : resolvers
745
+ }
746
+ } )
747
+ }
748
+
749
+ return dns ( { resolvers } )
750
+ }
0 commit comments