🌳
pt0/ipfsF/ipfsPeersRegistryAI.mts
1import { ipfsApiUrl as hetzn1IpfsApiUrl, ipfsGatewayUrl as hetzn1IpfsGatewayUrl } from '../klustersF/hetzn1/ipfsConstantsAI.mjs'
9export type IpfsHost = {apiUrl: string, gatewayUrl: string, p2pAddr?: string, cluster_name: string}
11const allIpfsHosts: IpfsHost[] = [
12 {apiUrl: hetzn1IpfsApiUrl, gatewayUrl: hetzn1IpfsGatewayUrl, p2pAddr: '/dns4/kube.hetzn1.demokluster.com/tcp/30202', cluster_name: 'hetzn1'},
13 {apiUrl: harv2demoIpfsApiUrl, gatewayUrl: harv2demoIpfsGatewayUrl, p2pAddr: '/dns4/kube.harv2.demokluster.com/tcp/31119', cluster_name: 'harv2demo'},
14 {apiUrl: harv3demoIpfsApiUrl, gatewayUrl: harv3demoIpfsGatewayUrl, p2pAddr: '/dns4/kube.harv3.demokluster.com/tcp/30202', cluster_name: 'harv3demo'},
17// hetzn1 is the primary/default host (uploads land here, gateway links point here)
18export const primaryIpfsHost = allIpfsHosts[0]
19export const primaryIpfsApiUrl = primaryIpfsHost.apiUrl
20export const primaryIpfsGatewayUrl = primaryIpfsHost.gatewayUrl
22export const getActiveIpfsHosts = (): IpfsHost[] => allIpfsHosts.filter(h => {
23 const reachability = reachabilityFor(h.cluster_name)
24 if (reachability) { betLog(chalkYellow('âš  ipfsHostSkipped'), {host: new URL(h.apiUrl).hostname, cluster: h.cluster_name, reason: reachability}); return false }
25 return true
26})
28export const getActiveIpfsHostsForLan = async (): Promise<IpfsHost[]> => {
29 const hosts = getActiveIpfsHosts()
30 return Promise.all(hosts.map(async h => {
31 const apiUrl = await rewriteHostForLan({str: h.apiUrl, cluster_name: h.cluster_name})
32 const gatewayUrl = await rewriteHostForLan({str: h.gatewayUrl, cluster_name: h.cluster_name})
33 return {...h, apiUrl, gatewayUrl}
34 }))
37const getPeerIdFromApi = async (apiUrl: string): Promise<string | null> => {
38 try {
39 const resp = await fetch(`${apiUrl}/api/v0/id`, {method: 'POST'})
40 if (!resp.ok) return null
41 const {ID} = await resp.json()
42 return ID
43 } catch { return null }
46export const getOtherPeersForApiUrl = async (selfApiUrl: string) => {
47 const others = (await getActiveIpfsHostsForLan()).filter(p => p.apiUrl !== selfApiUrl && p.p2pAddr)
48 const peers: {ID: string, Addrs: string[]}[] = []
49 for (const {apiUrl, p2pAddr} of others) {
50 const peerId = await getPeerIdFromApi(apiUrl)
51 if (peerId) peers.push({ID: peerId, Addrs: [p2pAddr!]})
52 else betLog(chalkYellow('âš  peerIdLookupFail'), {host: new URL(apiUrl).hostname})
53 }
54 return peers