@libp2p/devtools-metrics 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/rpc/rpc.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { enable, disable } from '@libp2p/logger'
2
2
  import { peerIdFromString } from '@libp2p/peer-id'
3
3
  import { multiaddr } from '@multiformats/multiaddr'
4
+ import { gatherCapabilities } from '../utils/gather-capabilities.js'
4
5
  import { getPeers } from '../utils/get-peers.js'
6
+ import { getPubSub } from '../utils/get-pubsub.js'
5
7
  import { getSelf } from '../utils/get-self.js'
6
8
  import type { MetricsRPC } from './index.js'
7
9
  import type { DevToolsMetricsComponents } from '../index.js'
8
10
  import type { PeerId } from '@libp2p/interface'
9
- import type { OpenConnectionOptions } from '@libp2p/interface-internal'
10
11
  import type { Multiaddr } from '@multiformats/multiaddr'
11
- import type { AbortOptions } from 'it-pushable'
12
12
 
13
13
  export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
14
14
  const log = components.logger.forComponent('libp2p:devtools-metrics:metrics-rpc')
@@ -18,10 +18,11 @@ export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
18
18
  return {
19
19
  self: await getSelf(components),
20
20
  peers: await getPeers(components, log),
21
- debug: localStorage.getItem('debug') ?? ''
21
+ debug: localStorage.getItem('debug') ?? '',
22
+ capabilities: gatherCapabilities(components)
22
23
  }
23
24
  },
24
- setDebug: async (namespace?: string) => {
25
+ setDebug: async (namespace?) => {
25
26
  if (namespace?.length != null && namespace?.length > 0) {
26
27
  enable(namespace)
27
28
  localStorage.setItem('debug', namespace)
@@ -30,7 +31,7 @@ export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
30
31
  localStorage.removeItem('debug')
31
32
  }
32
33
  },
33
- openConnection: async (peerIdOrMultiaddr: string, options?: OpenConnectionOptions) => {
34
+ openConnection: async (peerIdOrMultiaddr, options?) => {
34
35
  let peer: PeerId | Multiaddr
35
36
 
36
37
  try {
@@ -41,7 +42,7 @@ export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
41
42
 
42
43
  await components.connectionManager.openConnection(peer, options)
43
44
  },
44
- closeConnection: async (peerId: PeerId, options?: AbortOptions) => {
45
+ closeConnection: async (peerId, options?) => {
45
46
  await Promise.all(
46
47
  components.connectionManager.getConnections(peerId)
47
48
  .map(async connection => {
@@ -54,6 +55,23 @@ export function metricsRpc (components: DevToolsMetricsComponents): MetricsRPC {
54
55
  )
55
56
  },
56
57
  contentRouting: components.contentRouting,
57
- peerRouting: components.peerRouting
58
+ peerRouting: components.peerRouting,
59
+ pubsub: {
60
+ async getTopics (component) {
61
+ return getPubSub(component, components).getTopics()
62
+ },
63
+ async subscribe (component, topic) {
64
+ getPubSub(component, components).subscribe(topic)
65
+ },
66
+ async unsubscribe (component, topic) {
67
+ getPubSub(component, components).unsubscribe(topic)
68
+ },
69
+ async publish (component, topic, message) {
70
+ await getPubSub(component, components).publish(topic, message)
71
+ },
72
+ async getSubscribers (component: string, topic: string) {
73
+ return getPubSub(component, components).getSubscribers(topic)
74
+ }
75
+ }
58
76
  }
59
77
  }
@@ -0,0 +1,9 @@
1
+ import { gatherCapabilities } from './gather-capabilities.js'
2
+
3
+ export function findCapability (capability: string, components: any): any | undefined {
4
+ for (const [name, capabilities] of Object.entries(gatherCapabilities(components))) {
5
+ if (capabilities.includes(capability)) {
6
+ return components[name]
7
+ }
8
+ }
9
+ }
@@ -0,0 +1,14 @@
1
+ import { serviceCapabilities } from '@libp2p/interface'
2
+
3
+ export function gatherCapabilities (components: any): Record<string, string[]> {
4
+ const capabilities: Record<string, string[]> = {}
5
+ const services: Record<string, any> = components.components ?? components
6
+
7
+ Object.entries(services).forEach(([name, component]) => {
8
+ if (component?.[serviceCapabilities] != null && Array.isArray(component[serviceCapabilities])) {
9
+ capabilities[name] = component[serviceCapabilities]
10
+ }
11
+ })
12
+
13
+ return capabilities
14
+ }
@@ -0,0 +1,12 @@
1
+ import { InvalidParametersError, isPubSub } from '@libp2p/interface'
2
+ import type { PubSub } from '@libp2p/interface'
3
+
4
+ export function getPubSub (component: string, components: any): PubSub {
5
+ const pubsub = components[component]
6
+
7
+ if (!isPubSub(pubsub)) {
8
+ throw new InvalidParametersError(`Component ${component} did not implement the PubSub interface`)
9
+ }
10
+
11
+ return pubsub
12
+ }