-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathutils.ts
45 lines (37 loc) · 1.54 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { DNS, HTTP, HTTPS } from '@multiformats/multiaddr-matcher'
import { multiaddrToUri } from '@multiformats/multiaddr-to-uri'
import { TrustlessGateway } from './trustless-gateway.js'
import type { Routing } from '@helia/interface'
import type { ComponentLogger } from '@libp2p/interface'
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
import type { CID } from 'multiformats/cid'
export function filterNonHTTPMultiaddrs (multiaddrs: Multiaddr[], allowInsecure: boolean, allowLocal: boolean): Multiaddr[] {
return multiaddrs.filter(ma => {
if (HTTPS.matches(ma) || (allowInsecure && HTTP.matches(ma))) {
if (allowLocal) {
return true
}
if (DNS.matches(ma)) {
return true
}
return isPrivateIp(ma.toOptions().host) === false
}
return false
})
}
export async function * findHttpGatewayProviders (cid: CID, routing: Routing, logger: ComponentLogger, allowInsecure: boolean, allowLocal: boolean, options?: AbortOptions): AsyncGenerator<TrustlessGateway> {
for await (const provider of routing.findProviders(cid, options)) {
// require http(s) addresses
const httpAddresses = filterNonHTTPMultiaddrs(provider.multiaddrs, allowInsecure, allowLocal)
if (httpAddresses.length === 0) {
continue
}
// take first address?
// /ip4/x.x.x.x/tcp/31337/http
// /ip4/x.x.x.x/tcp/31337/https
// etc
const uri = multiaddrToUri(httpAddresses[0])
yield new TrustlessGateway(uri, logger)
}
}