Skip to content

Commit 403c859

Browse files
committed
fix: allow routings to use helia components
Accept factory functions as routings, not just implementations.
1 parent 992fa2f commit 403c859

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

packages/routers/src/delegated-http-routing.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,24 @@ class DelegatedHTTPRouter implements Routing {
100100
/**
101101
* 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.
102102
*/
103-
export function delegatedHTTPRouting (url: string | URL, init?: DelegatedRoutingV1HttpApiClientInit): Routing {
104-
const config = init ?? delegatedHTTPRoutingDefaults()
105-
return new DelegatedHTTPRouter(new URL(url), config)
103+
export function delegatedHTTPRouting (init: DelegatedRoutingV1HttpApiClientInit & { url: string | URL }): (components: any) => Routing
104+
/**
105+
* @deprecated Use `delegatedHTTPRouting(init)` instead
106+
*/
107+
export function delegatedHTTPRouting (url: string | URL, init?: DelegatedRoutingV1HttpApiClientInit): Routing
108+
export function delegatedHTTPRouting (url: string | URL | (DelegatedRoutingV1HttpApiClientInit & { url: string | URL }), init?: DelegatedRoutingV1HttpApiClientInit): Routing | ((components: any) => Routing) {
109+
if (typeof url === 'string' || url instanceof URL) {
110+
return new DelegatedHTTPRouter({
111+
logger: defaultLogger()
112+
}, {
113+
...delegatedHTTPRoutingDefaults(),
114+
...init,
115+
url: new URL(url)
116+
})
117+
}
118+
119+
return (components: any) => new DelegatedHTTPRouter(components, {
120+
...delegatedHTTPRoutingDefaults(),
121+
...url
122+
})
106123
}

packages/utils/src/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
136136
* Routers perform operations such as looking up content providers,
137137
* information about network peers or getting/putting records.
138138
*/
139-
routers?: Array<Partial<Routing>>
139+
routers?: Array<Partial<Routing> | ((components: any) => Partial<Routing>)>
140140

141141
/**
142142
* During provider lookups, peers can be returned from routing implementations
@@ -232,20 +232,26 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
232232
}
233233

234234
this.routing = components.routing = new RoutingClass(components, {
235-
routers: (init.routers ?? []).flatMap((router: any) => {
235+
routers: (init.routers ?? []).flatMap((router: Partial<Routing> | ((components: any) => Partial<Routing>)) => {
236+
if (typeof router === 'function') {
237+
router = router(components)
238+
}
239+
236240
// if the router itself is a router
237241
const routers = [
238242
router
239243
]
240244

241245
// if the router provides a libp2p-style ContentRouter
242-
if (router[contentRoutingSymbol] != null) {
243-
routers.push(router[contentRoutingSymbol])
246+
const contentRouting = asContentRouting(router)
247+
if (contentRouting != null) {
248+
routers.push(contentRouting)
244249
}
245250

246251
// if the router provides a libp2p-style PeerRouter
247-
if (router[peerRoutingSymbol] != null) {
248-
routers.push(router[peerRoutingSymbol])
252+
const peerRouting = asPeerRouting(router)
253+
if (peerRouting != null) {
254+
routers.push(peerRouting)
249255
}
250256

251257
return routers
@@ -318,3 +324,11 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
318324
this.log('gc finished')
319325
}
320326
}
327+
328+
function asContentRouting (obj?: any): ContentRouting | undefined {
329+
return obj?.[contentRoutingSymbol]
330+
}
331+
332+
function asPeerRouting (obj?: any): PeerRouting | undefined {
333+
return obj?.[peerRoutingSymbol]
334+
}

0 commit comments

Comments
 (0)