1- import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
1+ import { delegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
22import { NotFoundError } from '@libp2p/interface'
3+ import { defaultLogger } from '@libp2p/logger'
34import { marshalIPNSRecord , multihashFromIPNSRoutingKey , unmarshalIPNSRecord } from 'ipns'
45import first from 'it-first'
56import map from 'it-map'
67import { CID } from 'multiformats/cid'
78import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
89import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
910import { delegatedHTTPRoutingDefaults } from './utils/delegated-http-routing-defaults.js'
10- import type { DelegatedRoutingV1HttpApiClient , DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
11+ import type { DelegatedRoutingV1HttpApiClient , DelegatedRoutingV1HttpApiClientComponents , DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
1112import type { Provider , Routing , RoutingOptions } from '@helia/interface'
1213import type { PeerId , PeerInfo } from '@libp2p/interface'
1314import type { Version } from 'multiformats'
@@ -21,8 +22,8 @@ function isIPNSKey (key: Uint8Array): boolean {
2122class DelegatedHTTPRouter implements Routing {
2223 private readonly client : DelegatedRoutingV1HttpApiClient
2324
24- constructor ( url : URL , init : DelegatedRoutingV1HttpApiClientInit = { } ) {
25- this . client = createDelegatedRoutingV1HttpApiClient ( url , init )
25+ constructor ( components : DelegatedRoutingV1HttpApiClientComponents , init : DelegatedRoutingV1HttpApiClientInit & { url : string | URL } ) {
26+ this . client = delegatedRoutingV1HttpApiClient ( init ) ( components )
2627 }
2728
2829 async provide ( cid : CID , options ?: RoutingOptions ) : Promise < void > {
@@ -100,7 +101,24 @@ class DelegatedHTTPRouter implements Routing {
100101/**
101102 * Creates a Helia Router that connects to an endpoint that supports the [Delegated Routing V1 HTTP API](https://specs.ipfs.tech/routing/http-routing-v1/) spec.
102103 */
103- export function delegatedHTTPRouting ( url : string | URL , init ?: DelegatedRoutingV1HttpApiClientInit ) : Routing {
104- const config = init ?? delegatedHTTPRoutingDefaults ( )
105- return new DelegatedHTTPRouter ( new URL ( url ) , config )
104+ export function delegatedHTTPRouting ( init : DelegatedRoutingV1HttpApiClientInit & { url : string | URL } ) : ( components : any ) => Routing
105+ /**
106+ * @deprecated Use `delegatedHTTPRouting(init)` instead
107+ */
108+ export function delegatedHTTPRouting ( url : string | URL , init ?: DelegatedRoutingV1HttpApiClientInit ) : Routing
109+ export function delegatedHTTPRouting ( url : string | URL | ( DelegatedRoutingV1HttpApiClientInit & { url : string | URL } ) , init ?: DelegatedRoutingV1HttpApiClientInit ) : Routing | ( ( components : any ) => Routing ) {
110+ if ( typeof url === 'string' || url instanceof URL ) {
111+ return new DelegatedHTTPRouter ( {
112+ logger : defaultLogger ( )
113+ } , {
114+ ...delegatedHTTPRoutingDefaults ( ) ,
115+ ...init ,
116+ url : new URL ( url )
117+ } )
118+ }
119+
120+ return ( components : any ) => new DelegatedHTTPRouter ( components , {
121+ ...delegatedHTTPRoutingDefaults ( ) ,
122+ ...url
123+ } )
106124}
0 commit comments