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
Changes from 1 commit
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
10 changes: 5 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,7 @@ 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 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 +20,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 +94,6 @@ 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 {
return new DelegatedHTTPRouter(new URL(url), init)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably also need to update the createHelia functions to support passing these through right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the normal helia, i .e. HeliaP2P, we don't even instantitate the delegatedHTTPRouter, so not relevant:

const helia = new HeliaP2P({
...init,
libp2p,
datastore,
blockstore,
blockBrokers: init.blockBrokers ?? [
trustlessGateway(),
bitswap()
],
routers: init.routers ?? [
libp2pRouting(libp2p),
httpGatewayRouting()
],
metrics: libp2p.metrics
})

For @helia/http, we do use the delegatedHTTPRouter: https://github.com/ipfs/helia/blob/f904041bdd2643fabd204485825d4761fc44a83f/packages/http/src/index.ts/#L79-L82

But if you want to cutsomise the delegatedHTTPRouter options, you can just instantiate it yourself and pass it in via init.router (a bit like how we do it in the example)

Loading