diff --git a/packages/fallback-router/src/index.ts b/packages/fallback-router/src/index.ts index d4c9563ae..fcd35919a 100644 --- a/packages/fallback-router/src/index.ts +++ b/packages/fallback-router/src/index.ts @@ -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 + /** + * Where to send requests + */ + gateways: Array + /** * Whether to shuffle the list of gateways * @@ -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 } @@ -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) } diff --git a/packages/http/README.md b/packages/http/README.md index 6205a5cbc..fbb5813f7 100644 --- a/packages/http/README.md +++ b/packages/http/README.md @@ -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')) @@ -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() ], @@ -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() diff --git a/packages/http/src/index.ts b/packages/http/src/index.ts index 9784d4112..7fe0b2ec0 100644 --- a/packages/http/src/index.ts +++ b/packages/http/src/index.ts @@ -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')) @@ -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() * ], @@ -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() @@ -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 (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 }