Skip to content

Commit 693c82d

Browse files
2colorachingbrain
andauthored
feat: enable customising delegated http router (#654)
Allow passing delegated routing client options, e.g. IPIP-484 filters, to the delegated HTTP router. --------- Co-authored-by: Daniel N <2color@users.noreply.github.com> Co-authored-by: Alex Potsides <alex@achingbrain.net>
1 parent 8364296 commit 693c82d

4 files changed

+27
-6
lines changed

packages/routers/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"test:electron-main": "aegir test -t electron-main"
5555
},
5656
"dependencies": {
57-
"@helia/delegated-routing-v1-http-api-client": "^4.0.0",
57+
"@helia/delegated-routing-v1-http-api-client": "^4.1.0",
5858
"@helia/interface": "^5.0.0",
5959
"@libp2p/peer-id": "^5.0.0",
6060
"@libp2p/interface": "^2.0.0",
@@ -72,5 +72,8 @@
7272
"it-drain": "^3.0.5",
7373
"sinon-ts": "^2.0.0"
7474
},
75+
"browser": {
76+
"./dist/src/utils/delegated-http-routing-defaults.js": "./dist/src/utils/delegated-http-routing-defaults.browser.js"
77+
},
7578
"sideEffects": false
7679
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import map from 'it-map'
66
import { CID } from 'multiformats/cid'
77
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
88
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9-
import type { DelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
9+
import { delegatedHTTPRoutingDefaults } from './utils/delegated-http-routing-defaults.js'
10+
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
1011
import type { Provider, Routing, RoutingOptions } from '@helia/interface'
1112
import type { PeerId, PeerInfo } from '@libp2p/interface'
1213
import type { Version } from 'multiformats'
@@ -20,8 +21,8 @@ function isIPNSKey (key: Uint8Array): boolean {
2021
class DelegatedHTTPRouter implements Routing {
2122
private readonly client: DelegatedRoutingV1HttpApiClient
2223

23-
constructor (url: URL) {
24-
this.client = createDelegatedRoutingV1HttpApiClient(url)
24+
constructor (url: URL, init: DelegatedRoutingV1HttpApiClientInit = {}) {
25+
this.client = createDelegatedRoutingV1HttpApiClient(url, init)
2526
}
2627

2728
async provide (cid: CID, options?: RoutingOptions | undefined): Promise<void> {
@@ -94,6 +95,7 @@ class DelegatedHTTPRouter implements Routing {
9495
/**
9596
* 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.
9697
*/
97-
export function delegatedHTTPRouting (url: string | URL): Routing {
98-
return new DelegatedHTTPRouter(new URL(url))
98+
export function delegatedHTTPRouting (url: string | URL, init?: DelegatedRoutingV1HttpApiClientInit): Routing {
99+
const config = init ?? delegatedHTTPRoutingDefaults()
100+
return new DelegatedHTTPRouter(new URL(url), config)
99101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
2+
3+
export function delegatedHTTPRoutingDefaults (): DelegatedRoutingV1HttpApiClientInit {
4+
return {
5+
filterProtocols: ['unknown', 'transport-bitswap', 'transport-ipfs-gateway-http'],
6+
filterAddrs: ['https', 'webtransport', 'webrtc', 'webrtc-direct', 'wss']
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
2+
3+
export function delegatedHTTPRoutingDefaults (): DelegatedRoutingV1HttpApiClientInit {
4+
return {
5+
filterProtocols: ['unknown', 'transport-bitswap', 'transport-ipfs-gateway-http'],
6+
filterAddrs: ['https', 'tcp', 'webrtc', 'webrtc-direct', 'wss']
7+
}
8+
}

0 commit comments

Comments
 (0)