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
20 changes: 8 additions & 12 deletions packages/fallback-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { Peer, Provider, Router, RoutingOptions } from '@helia/interface'
import type { Version } from 'multiformats'

export const DEFAULT_TRUSTLESS_GATEWAYS = [
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
'https://trustless-gateway.link',

// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
'https://4everland.io'
]

export interface FallbackRouterInit {
gateways?: Array<URL | string>
/**
* Where to send requests
*/
gateways: Array<URL | string>

/**
* Whether to shuffle the list of gateways
*
Expand Down Expand Up @@ -58,8 +54,8 @@ class FallbackRouter implements Router {
private readonly gateways: Peer[]
private readonly shuffle: boolean

constructor (init: FallbackRouterInit = {}) {
this.gateways = (init.gateways ?? DEFAULT_TRUSTLESS_GATEWAYS).map(url => toPeerInfo(url))
constructor (init: FallbackRouterInit) {
this.gateways = init.gateways.map(url => toPeerInfo(url)) ?? []
this.shuffle = init.shuffle ?? true
}

Expand Down Expand Up @@ -87,6 +83,6 @@ class FallbackRouter implements Router {
/**
* Returns a static list of HTTP Gateways as providers
*/
export function fallbackRouter (init: FallbackRouterInit = {}): Router {
export function fallbackRouter (init: FallbackRouterInit): Router {
return new FallbackRouter(init)
}
18 changes: 13 additions & 5 deletions packages/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ Pass it to other modules like @helia/unixfs to fetch files from the distributed
```typescript
import { withHTTP } from '@helia/http'
import { unixfs } from '@helia/unixfs'
import { createHelia } from 'helia'
import { createHeliaLight } from 'helia'
import { CID } from 'multiformats/cid'

const helia = await withHTTP(createHelia()).start()
const helia = await withHTTP(createHeliaLight(), {
delegatedRouters: [
'https://delegated-ipfs.dev'
],
recursiveGateways: [
'https://trustless-gateway.link',
'https://4everland.io'
]
}).start()

const fs = unixfs(helia)
fs.cat(CID.parse('bafyFoo'))
Expand All @@ -55,14 +63,14 @@ fs.cat(CID.parse('bafyFoo'))
It's possible to manually configure your node without using this module.

```typescript
import { createHelia } from 'helia'
import { createHeliaLight } from 'helia'
import { trustlessGatewayBlockBroker } from '@helia/trustless-gateway-client'
import { fallbackRouter } from '@helia/fallback-router'
import { delegatedHTTPRouter } from '@helia/delegated-http-routing-client'
import { unixfs } from '@helia/unixfs'
import { CID } from 'multiformats/cid'

const helia = await createHelia({
const helia = await createHeliaLight({
blockBrokers: [
trustlessGatewayBlockBroker()
],
Expand All @@ -71,7 +79,7 @@ const helia = await createHelia({
url: 'https://delegated-ipfs.dev'
}),
fallbackRouter({
gateways: ['https://cloudflare-ipfs.com', 'https://ipfs.io']
gateways: ['https://trustless-gateway.link', 'https://4everland.io']
})
]
}).start()
Expand Down
84 changes: 64 additions & 20 deletions packages/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
* ```typescript
* import { withHTTP } from '@helia/http'
* import { unixfs } from '@helia/unixfs'
* import { createHelia } from 'helia'
* import { createHeliaLight } from 'helia'
* import { CID } from 'multiformats/cid'
*
* const helia = await withHTTP(createHelia()).start()
* const helia = await withHTTP(createHeliaLight(), {
* delegatedRouters: [
* 'https://delegated-ipfs.dev'
* ],
* recursiveGateways: [
* 'https://trustless-gateway.link',
* 'https://4everland.io'
* ]
* }).start()
*
* const fs = unixfs(helia)
* fs.cat(CID.parse('bafyFoo'))
Expand All @@ -25,14 +33,14 @@
* It's possible to manually configure your node without using this module.
*
* ```typescript
* import { createHelia } from 'helia'
* import { createHeliaLight } from 'helia'
* import { trustlessGatewayBlockBroker } from '@helia/trustless-gateway-client'
* import { fallbackRouter } from '@helia/fallback-router'
* import { delegatedHTTPRouter } from '@helia/delegated-http-routing-client'
* import { unixfs } from '@helia/unixfs'
* import { CID } from 'multiformats/cid'
*
* const helia = await createHelia({
* const helia = await createHeliaLight({
* blockBrokers: [
* trustlessGatewayBlockBroker()
* ],
Expand All @@ -41,7 +49,7 @@
* url: 'https://delegated-ipfs.dev'
* }),
* fallbackRouter({
* gateways: ['https://cloudflare-ipfs.com', 'https://ipfs.io']
* gateways: ['https://trustless-gateway.link', 'https://4everland.io']
* })
* ]
* }).start()
Expand All @@ -54,31 +62,67 @@
import { delegatedHTTPRouter } from '@helia/delegated-routing-client'
import { fallbackRouter } from '@helia/fallback-router'
import { trustlessGatewayBlockBroker } from '@helia/trustless-gateway-client'
import type { BlockBroker, Helia, Router } from '@helia/interface'
import type { Helia } from '@helia/interface'
import type { TrustlessGatewayBlockBrokerInit } from '@helia/trustless-gateway-client'

export const DEFAULT_TRUSTLESS_GATEWAYS = [
// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
'https://trustless-gateway.link',

// 2023-10-03: IPNS, Origin, and Block/CAR support from https://ipfs.github.io/public-gateway-checker/
'https://4everland.io'
]

export interface HTTPOptions {
routers?: Router[]
blockBrokers?: BlockBroker[]
/**
* Delegated routers are servers that make routing requests on behalf of peers
* with less capable network connectivity.
*
* @see https://specs.ipfs.tech/routing/http-routing-v1/
* @default ['https://delegated-ipfs.dev']
*/
delegatedRouters?: string[]

/**
* A recursive gateway is one that will fetch content on behalf of peers with
* less capable network connectivity. For example it may fetch content from a
* node that supports transport(s) which the requesting peer does not.
*
* These are used as fallback routers which will always claim to be providers
* of a given block.
*
* @see https://docs.ipfs.tech/concepts/ipfs-gateway/#recursive-vs-non-recursive-gateways
*/
recursiveGateways?: string[]

/**
* Init arg passed to the trustless gateway block broker
*
* @see https://docs.ipfs.tech/reference/http/gateway/#trusted-vs-trustless
*/
trustlessGatewayBlockBrokerInit?: TrustlessGatewayBlockBrokerInit
}

/**
* Augment a Helia node with HTTP routers and block brokers
*/
export function withHTTP <H extends Helia> (helia: H, init?: HTTPOptions): H {
init?.routers ?? [
fallbackRouter(),
delegatedHTTPRouter({
url: 'https://delegated-ipfs.dev'
})
].forEach(router => {
helia.addRouter(router)
init?.delegatedRouters ?? [
'https://delegated-ipfs.dev'
].forEach(url => {
helia.addRouter(delegatedHTTPRouter({
url
}))
})

init?.blockBrokers ?? [
trustlessGatewayBlockBroker()
].forEach(broker => {
helia.addBlockBroker(broker)
})
helia.addRouter(fallbackRouter({
gateways: init?.recursiveGateways ?? DEFAULT_TRUSTLESS_GATEWAYS
}))

// add trustless gateway block broker
if (!helia.hasBlockBroker('trustless-gateway')) {
helia.addBlockBroker(trustlessGatewayBlockBroker(init?.trustlessGatewayBlockBrokerInit))
}

return helia
}
Loading