-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add capability detection to metrics-devtools (#2708)
Adds the ability to find configured services that possess a given capability and interact with that service via rpc. Starts with a configured PubSub service.
- Loading branch information
1 parent
6ccbb06
commit 4fd7eb2
Showing
7 changed files
with
136 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { gatherCapabilities } from './gather-capabilities.js' | ||
|
||
export function findCapability (capability: string, components: any): any | undefined { | ||
for (const [name, capabilities] of Object.entries(gatherCapabilities(components))) { | ||
if (capabilities.includes(capability)) { | ||
return components[name] | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/metrics-devtools/src/utils/gather-capabilities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { serviceCapabilities } from '@libp2p/interface' | ||
|
||
export function gatherCapabilities (components: any): Record<string, string[]> { | ||
const capabilities: Record<string, string[]> = {} | ||
const services: Record<string, any> = components.components ?? components | ||
|
||
Object.entries(services).forEach(([name, component]) => { | ||
if (component?.[serviceCapabilities] != null && Array.isArray(component[serviceCapabilities])) { | ||
capabilities[name] = component[serviceCapabilities] | ||
} | ||
}) | ||
|
||
return capabilities | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { InvalidParametersError, isPubSub } from '@libp2p/interface' | ||
import type { PubSub } from '@libp2p/interface' | ||
|
||
export function getPubSub (component: string, components: any): PubSub { | ||
const pubsub = components[component] | ||
|
||
if (!isPubSub(pubsub)) { | ||
throw new InvalidParametersError(`Component ${component} did not implement the PubSub interface`) | ||
} | ||
|
||
return pubsub | ||
} |