Skip to content
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
2 changes: 1 addition & 1 deletion packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@chainsafe/libp2p-noise": "^17.0.0",
"@chainsafe/libp2p-yamux": "^8.0.0",
"@helia/block-brokers": "^5.0.7",
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
"@helia/interface": "^6.0.2",
"@helia/routers": "^4.0.3",
"@helia/utils": "^2.2.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@helia/block-brokers": "^5.0.7",
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
"@helia/interface": "^6.0.2",
"@helia/routers": "^4.0.3",
"@helia/utils": "^2.2.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/routers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
"test:electron-main": "aegir test -t electron-main"
},
"dependencies": {
"@helia/delegated-routing-v1-http-api-client": "^5.0.0",
"@helia/delegated-routing-v1-http-api-client": "^5.1.2",
"@helia/interface": "^6.0.2",
"@libp2p/interface": "^3.1.0",
"@libp2p/logger": "^6.2.0",
"@libp2p/peer-id": "^6.0.3",
"@multiformats/uri-to-multiaddr": "^10.0.0",
"ipns": "^10.1.2",
Expand Down
32 changes: 25 additions & 7 deletions packages/routers/src/delegated-http-routing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
import { delegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
import { NotFoundError } from '@libp2p/interface'
import { defaultLogger } from '@libp2p/logger'
import { marshalIPNSRecord, multihashFromIPNSRoutingKey, unmarshalIPNSRecord } from 'ipns'
import first from 'it-first'
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 { delegatedHTTPRoutingDefaults } from './utils/delegated-http-routing-defaults.js'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit } from '@helia/delegated-routing-v1-http-api-client'
import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientComponents, 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 @@ -21,8 +22,8 @@ function isIPNSKey (key: Uint8Array): boolean {
class DelegatedHTTPRouter implements Routing {
private readonly client: DelegatedRoutingV1HttpApiClient

constructor (url: URL, init: DelegatedRoutingV1HttpApiClientInit = {}) {
this.client = createDelegatedRoutingV1HttpApiClient(url, init)
constructor (components: DelegatedRoutingV1HttpApiClientComponents, init: DelegatedRoutingV1HttpApiClientInit & { url: string | URL }) {
this.client = delegatedRoutingV1HttpApiClient(init)(components)
}

async provide (cid: CID, options?: RoutingOptions): Promise<void> {
Expand Down Expand Up @@ -100,7 +101,24 @@ 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, init?: DelegatedRoutingV1HttpApiClientInit): Routing {
const config = init ?? delegatedHTTPRoutingDefaults()
return new DelegatedHTTPRouter(new URL(url), config)
export function delegatedHTTPRouting (init: DelegatedRoutingV1HttpApiClientInit & { url: string | URL }): (components: any) => Routing
/**
* @deprecated Use `delegatedHTTPRouting(init)` instead
*/
export function delegatedHTTPRouting (url: string | URL, init?: DelegatedRoutingV1HttpApiClientInit): Routing
export function delegatedHTTPRouting (url: string | URL | (DelegatedRoutingV1HttpApiClientInit & { url: string | URL }), init?: DelegatedRoutingV1HttpApiClientInit): Routing | ((components: any) => Routing) {
if (typeof url === 'string' || url instanceof URL) {
return new DelegatedHTTPRouter({
logger: defaultLogger()
}, {
...delegatedHTTPRoutingDefaults(),
...init,
url: new URL(url)
})
}

return (components: any) => new DelegatedHTTPRouter(components, {
...delegatedHTTPRoutingDefaults(),
...url
})
}
28 changes: 21 additions & 7 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { BlockStorageInit } from './storage.js'
import type { Await, CodecLoader, GCOptions, HasherLoader, Helia as HeliaInterface, HeliaEvents, Routing } from '@helia/interface'
import type { BlockBroker } from '@helia/interface/blocks'
import type { Pins } from '@helia/interface/pins'
import type { ComponentLogger, Libp2p, Logger, Metrics } from '@libp2p/interface'
import type { ComponentLogger, ContentRouting, Libp2p, Logger, Metrics, PeerRouting } from '@libp2p/interface'
import type { KeychainInit } from '@libp2p/keychain'
import type { DNS } from '@multiformats/dns'
import type { Blockstore } from 'interface-blockstore'
Expand Down Expand Up @@ -136,7 +136,7 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
* Routers perform operations such as looking up content providers,
* information about network peers or getting/putting records.
*/
routers?: Array<Partial<Routing>>
routers?: Array<Partial<Routing> | ((components: any) => Partial<Routing>)>

/**
* During provider lookups, peers can be returned from routing implementations
Expand Down Expand Up @@ -232,20 +232,26 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
}

this.routing = components.routing = new RoutingClass(components, {
routers: (init.routers ?? []).flatMap((router: any) => {
routers: (init.routers ?? []).flatMap((router: Partial<Routing> | ((components: any) => Partial<Routing>)) => {
if (typeof router === 'function') {
router = router(components)
}

// if the router itself is a router
const routers = [
router
]

// if the router provides a libp2p-style ContentRouter
if (router[contentRoutingSymbol] != null) {
routers.push(router[contentRoutingSymbol])
const contentRouting = asContentRouting(router)
if (contentRouting != null) {
routers.push(contentRouting)
}

// if the router provides a libp2p-style PeerRouter
if (router[peerRoutingSymbol] != null) {
routers.push(router[peerRoutingSymbol])
const peerRouting = asPeerRouting(router)
if (peerRouting != null) {
routers.push(peerRouting)
}

return routers
Expand Down Expand Up @@ -318,3 +324,11 @@ export class Helia<T extends Libp2p> implements HeliaInterface<T> {
this.log('gc finished')
}
}

function asContentRouting (obj?: any): ContentRouting | undefined {
return obj?.[contentRoutingSymbol]
}

function asPeerRouting (obj?: any): PeerRouting | undefined {
return obj?.[peerRoutingSymbol]
}