|
| 1 | +import { peerIdSymbol } from '@libp2p/interface' |
| 2 | +import { uriToMultiaddr } from '@multiformats/uri-to-multiaddr' |
| 3 | +import { CID } from 'multiformats/cid' |
| 4 | +import { identity } from 'multiformats/hashes/identity' |
| 5 | +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' |
| 6 | +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' |
| 7 | +import type { Provider, Routing, RoutingOptions } from '@helia/interface' |
| 8 | +import type { PeerId, PeerInfo } from '@libp2p/interface' |
| 9 | +import type { MultihashDigest, Version } from 'multiformats' |
| 10 | + |
| 11 | +export interface HTTPGatwayRouterInit { |
| 12 | + gateways?: Array<URL | string> |
| 13 | +} |
| 14 | + |
| 15 | +// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv |
| 16 | +const TRANSPORT_IPFS_GATEWAY_HTTP_CODE = 0x0920 |
| 17 | +const inspect = Symbol.for('nodejs.util.inspect.custom') |
| 18 | + |
| 19 | +class URLPeerId implements PeerId { |
| 20 | + readonly type = 'url' |
| 21 | + readonly multihash: MultihashDigest |
| 22 | + readonly privateKey?: Uint8Array |
| 23 | + readonly publicKey?: Uint8Array |
| 24 | + readonly url: string |
| 25 | + |
| 26 | + constructor (url: URL) { |
| 27 | + this.url = url.toString() |
| 28 | + this.multihash = identity.digest(uint8ArrayFromString(this.url)) |
| 29 | + } |
| 30 | + |
| 31 | + [inspect] (): string { |
| 32 | + return `PeerId(${this.url})` |
| 33 | + } |
| 34 | + |
| 35 | + readonly [peerIdSymbol] = true |
| 36 | + |
| 37 | + toString (): string { |
| 38 | + return this.toCID().toString() |
| 39 | + } |
| 40 | + |
| 41 | + toCID (): CID { |
| 42 | + return CID.createV1(TRANSPORT_IPFS_GATEWAY_HTTP_CODE, this.multihash) |
| 43 | + } |
| 44 | + |
| 45 | + toBytes (): Uint8Array { |
| 46 | + return this.toCID().bytes |
| 47 | + } |
| 48 | + |
| 49 | + equals (other?: PeerId | Uint8Array | string): boolean { |
| 50 | + if (other == null) { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + if (other instanceof Uint8Array) { |
| 55 | + other = uint8ArrayToString(other) |
| 56 | + } |
| 57 | + |
| 58 | + return other.toString() === this.toString() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function toPeerInfo (url: string | URL): PeerInfo { |
| 63 | + url = url.toString() |
| 64 | + |
| 65 | + return { |
| 66 | + id: new URLPeerId(new URL(url)), |
| 67 | + multiaddrs: [ |
| 68 | + uriToMultiaddr(url) |
| 69 | + ] |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class HTTPGatwayRouter implements Partial<Routing> { |
| 74 | + private readonly gateways: PeerInfo[] |
| 75 | + |
| 76 | + constructor (init: HTTPGatwayRouterInit = {}) { |
| 77 | + this.gateways = (init.gateways ?? []).map(url => toPeerInfo(url)) |
| 78 | + } |
| 79 | + |
| 80 | + async * findProviders (cid: CID<unknown, number, number, Version>, options?: RoutingOptions | undefined): AsyncIterable<Provider> { |
| 81 | + yield * this.gateways.map(info => ({ |
| 82 | + ...info, |
| 83 | + protocols: ['transport-ipfs-gateway-http'] |
| 84 | + })) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns a static list of HTTP Gateways as providers |
| 90 | + */ |
| 91 | +export function httpGatewayRouting (init: HTTPGatwayRouterInit = {}): Partial<Routing> { |
| 92 | + return new HTTPGatwayRouter(init) |
| 93 | +} |
0 commit comments