| 1 | import { enable, disable } from '@libp2p/logger' |
| 2 | import { peerIdFromString } from '@libp2p/peer-id' |
| 3 | import { multiaddr } from '@multiformats/multiaddr' |
| 4 | import { gatherCapabilities } from '../utils/gather-capabilities.js' |
| 5 | import { getComponent } from '../utils/get-implementation.ts' |
| 6 | import { getPeers } from '../utils/get-peers.js' |
| 7 | import { getPubSub } from '../utils/get-pubsub.js' |
| 8 | import { getSelf } from '../utils/get-self.js' |
| 9 | import type { MetricsRPC } from './index.js' |
| 10 | import type { InspectorMetricsComponents } from '../index.js' |
| 11 | import type { Identify } from '@libp2p/identify' |
| 12 | import type { PeerId } from '@libp2p/interface' |
| 13 | import type { Ping } from '@libp2p/ping' |
| 14 | import type { Multiaddr } from '@multiformats/multiaddr' |
| 15 | |
| 16 | function toPeerIdOrMultiaddr (peerIdOrMultiaddr: string): PeerId | Multiaddr { |
| 17 | let peer: PeerId | Multiaddr |
| 18 | |
| 19 | try { |
| 20 | peer = peerIdFromString(peerIdOrMultiaddr) |
| 21 | } catch { |
| 22 | peer = multiaddr(peerIdOrMultiaddr) |
| 23 | } |
| 24 | |
| 25 | return peer |
| 26 | } |
| 27 | |
| 28 | export function metricsRpc (components: InspectorMetricsComponents): MetricsRPC { |
| 29 | const log = components.logger.forComponent('libp2p:devtools-metrics:metrics-rpc') |
| 30 | let debug = globalThis.localStorage?.getItem('debug') ?? globalThis.process?.env?.DEBUG ?? '' |
| 31 | |
| 32 | return { |
| 33 | init: async () => { |
| 34 | return { |
| 35 | self: await getSelf(components), |
| 36 | peers: await getPeers(components, log), |
| 37 | debug, |
| 38 | capabilities: gatherCapabilities(components) |
| 39 | } |
| 40 | }, |
| 41 | setDebug: async (namespace?) => { |
| 42 | if (namespace?.length != null && namespace?.length > 0) { |
| 43 | enable(namespace) |
| 44 | globalThis.localStorage?.setItem('debug', namespace) |
| 45 | } else { |
| 46 | disable() |
| 47 | globalThis.localStorage?.removeItem('debug') |
| 48 | } |
| 49 | |
| 50 | debug = namespace ?? '' |
| 51 | }, |
| 52 | openConnection: async (peerIdOrMultiaddr, options?) => { |
| 53 | const peer = toPeerIdOrMultiaddr(peerIdOrMultiaddr) |
| 54 | const conn = await components.connectionManager.openConnection(peer, options) |
| 55 | |
| 56 | return { |
| 57 | id: conn.id, |
| 58 | remoteAddr: conn.remoteAddr, |
| 59 | remotePeer: conn.remotePeer, |
| 60 | direction: conn.direction, |
| 61 | timeline: conn.timeline, |
| 62 | multiplexer: conn.multiplexer, |
| 63 | encryption: conn.encryption, |
| 64 | status: conn.status, |
| 65 | limits: conn.limits, |
| 66 | rtt: conn.rtt |
| 67 | } |
| 68 | }, |
| 69 | closeConnection: async (peerId, options?) => { |
| 70 | await Promise.all( |
| 71 | components.connectionManager.getConnections(peerId) |
| 72 | .map(async connection => { |
| 73 | try { |
| 74 | await connection.close(options) |
| 75 | } catch (err: any) { |
| 76 | connection.abort(err) |
| 77 | } |
| 78 | }) |
| 79 | ) |
| 80 | }, |
| 81 | contentRouting: components.contentRouting, |
| 82 | peerRouting: components.peerRouting, |
| 83 | pubsub: { |
| 84 | async getTopics (component) { |
| 85 | return getPubSub(component, components).getTopics() |
| 86 | }, |
| 87 | async subscribe (component, topic) { |
| 88 | getPubSub(component, components).subscribe(topic) |
| 89 | }, |
| 90 | async unsubscribe (component, topic) { |
| 91 | getPubSub(component, components).unsubscribe(topic) |
| 92 | }, |
| 93 | async publish (component, topic, message) { |
| 94 | await getPubSub(component, components).publish(topic, message) |
| 95 | }, |
| 96 | async getSubscribers (component: string, topic: string) { |
| 97 | return getPubSub(component, components).getSubscribers(topic) |
| 98 | } |
| 99 | }, |
| 100 | ping: async (component, peerIdOrMultiaddr, options) => { |
| 101 | const ping = getComponent<Ping>(component, components, '@libp2p/ping') |
| 102 | const peer = toPeerIdOrMultiaddr(peerIdOrMultiaddr) |
| 103 | |
| 104 | return ping.ping(peer, options) |
| 105 | }, |
| 106 | identify: async (component, peerIdOrMultiaddr, options) => { |
| 107 | const identify = getComponent<Identify>(component, components, '@libp2p/identify') |
| 108 | const peer = toPeerIdOrMultiaddr(peerIdOrMultiaddr) |
| 109 | const connection = await components.connectionManager.openConnection(peer, options) |
| 110 | |
| 111 | return identify.identify(connection, options) |
| 112 | } |
| 113 | } |
| 114 | } |