@libp2p/devtools-metrics 1.0.0-d101aac4b → 1.0.1-21fe841f2

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.
@@ -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
+ }