Skip to content

Commit cc90a4d

Browse files
authored
fix: allow routings to use helia components (#900)
Accept factory functions as routings, not just implementations.
1 parent 11f16f7 commit cc90a4d

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

packages/helia/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"@chainsafe/libp2p-noise": "^17.0.0",
5252
"@chainsafe/libp2p-yamux": "^8.0.0",
5353
"@helia/block-brokers": "^5.0.7",
54-
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
54+
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
5555
"@helia/interface": "^6.0.2",
5656
"@helia/routers": "^4.0.3",
5757
"@helia/utils": "^2.2.3",

packages/http/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@helia/block-brokers": "^5.0.7",
51-
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
51+
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
5252
"@helia/interface": "^6.0.2",
5353
"@helia/routers": "^4.0.3",
5454
"@helia/utils": "^2.2.3",

packages/routers/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
"test:electron-main": "aegir test -t electron-main"
4848
},
4949
"dependencies": {
50-
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
50+
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
5151
"@helia/interface": "^6.0.2",
5252
"@libp2p/interface": "^3.1.0",
53+
"@libp2p/logger": "^6.2.0",
5354
"@libp2p/peer-id": "^6.0.3",
5455
"@multiformats/uri-to-multiaddr": "^10.0.0",
5556
"ipns": "^10.1.2",

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
1+
import { delegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
22
import { NotFoundError } from '@libp2p/interface'
3+
import { defaultLogger } from '@libp2p/logger'
34
import { marshalIPNSRecord, multihashFromIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
45
import first from 'it-first'
56
import map from 'it-map'
67
import { CID } from 'multiformats/cid'
78
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
89
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
910
import { 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'
1112
import type { Provider, Routing, RoutingOptions } from '@helia/interface'
1213
import type { PeerId, PeerInfo } from '@libp2p/interface'
1314
import type { Version } from 'multiformats'
@@ -21,8 +22,8 @@ function isIPNSKey (key: Uint8Array): boolean {
2122
class 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
}

packages/utils/src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { BlockStorageInit } from './storage.js'
2121
import type { Await, CodecLoader, GCOptions, HasherLoader, Helia as HeliaInterface, HeliaEvents, Routing } from '@helia/interface'
2222
import type { BlockBroker } from '@helia/interface/blocks'
2323
import type { Pins } from '@helia/interface/pins'
24-
import type { ComponentLogger, Libp2p, Logger, Metrics } from '@libp2p/interface'
24+
import type { ComponentLogger, ContentRouting, Libp2p, Logger, Metrics, PeerRouting } from '@libp2p/interface'
2525
import type { KeychainInit } from '@libp2p/keychain'
2626
import type { DNS } from '@multiformats/dns'
2727
import type { Blockstore } from 'interface-blockstore'
@@ -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)