Skip to content

Commit

Permalink
feat: enable customising delegated http router (#654)
Browse files Browse the repository at this point in the history
Allow passing delegated routing client options, e.g. IPIP-484 filters, to the delegated HTTP router.

---------

Co-authored-by: Daniel N <[email protected]>
Co-authored-by: Alex Potsides <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 8364296 commit 693c82d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/routers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"test:electron-main": "aegir test -t electron-main"
},
"dependencies": {
"@helia/delegated-routing-v1-http-api-client": "^4.0.0",
"@helia/delegated-routing-v1-http-api-client": "^4.1.0",
"@helia/interface": "^5.0.0",
"@libp2p/peer-id": "^5.0.0",
"@libp2p/interface": "^2.0.0",
Expand All @@ -72,5 +72,8 @@
"it-drain": "^3.0.5",
"sinon-ts": "^2.0.0"
},
"browser": {
"./dist/src/utils/delegated-http-routing-defaults.js": "./dist/src/utils/delegated-http-routing-defaults.browser.js"
},
"sideEffects": false
}
12 changes: 7 additions & 5 deletions packages/routers/src/delegated-http-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import map from 'it-map'
import { CID } from 'multiformats/cid'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import type { DelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
import { delegatedHTTPRoutingDefaults } from './utils/delegated-http-routing-defaults.js'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
import type { Provider, Routing, RoutingOptions } from '@helia/interface'
import type { PeerId, PeerInfo } from '@libp2p/interface'
import type { Version } from 'multiformats'
Expand All @@ -20,8 +21,8 @@ function isIPNSKey (key: Uint8Array): boolean {
class DelegatedHTTPRouter implements Routing {
private readonly client: DelegatedRoutingV1HttpApiClient

constructor (url: URL) {
this.client = createDelegatedRoutingV1HttpApiClient(url)
constructor (url: URL, init: DelegatedRoutingV1HttpApiClientInit = {}) {
this.client = createDelegatedRoutingV1HttpApiClient(url, init)
}

async provide (cid: CID, options?: RoutingOptions | undefined): Promise<void> {
Expand Down Expand Up @@ -94,6 +95,7 @@ class DelegatedHTTPRouter implements Routing {
/**
* 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.
*/
export function delegatedHTTPRouting (url: string | URL): Routing {
return new DelegatedHTTPRouter(new URL(url))
export function delegatedHTTPRouting (url: string | URL, init?: DelegatedRoutingV1HttpApiClientInit): Routing {
const config = init ?? delegatedHTTPRoutingDefaults()
return new DelegatedHTTPRouter(new URL(url), config)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'

export function delegatedHTTPRoutingDefaults (): DelegatedRoutingV1HttpApiClientInit {
return {
filterProtocols: ['unknown', 'transport-bitswap', 'transport-ipfs-gateway-http'],
filterAddrs: ['https', 'webtransport', 'webrtc', 'webrtc-direct', 'wss']
}
}
8 changes: 8 additions & 0 deletions packages/routers/src/utils/delegated-http-routing-defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'

export function delegatedHTTPRoutingDefaults (): DelegatedRoutingV1HttpApiClientInit {
return {
filterProtocols: ['unknown', 'transport-bitswap', 'transport-ipfs-gateway-http'],
filterAddrs: ['https', 'tcp', 'webrtc', 'webrtc-direct', 'wss']
}
}

0 comments on commit 693c82d

Please sign in to comment.