Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable customising delegated http router #654

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
}
}
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']
}
}
Loading