| 1 | import { InvalidParametersError } from '@libp2p/interface' |
| 2 | import type { GossipSub } from '@libp2p/gossipsub' |
| 3 | |
| 4 | function isGossipSub (obj?: any): obj is GossipSub { |
| 5 | if (obj == null) { |
| 6 | return false |
| 7 | } |
| 8 | |
| 9 | return typeof obj.publish === 'function' && |
| 10 | typeof obj.getSubscribers === 'function' && |
| 11 | typeof obj.unsubscribe === 'function' && |
| 12 | typeof obj.subscribe === 'function' |
| 13 | } |
| 14 | |
| 15 | export function getPubSub (component: string, components: any): GossipSub { |
| 16 | const pubsub = components[component] |
| 17 | |
| 18 | if (!isGossipSub(pubsub)) { |
| 19 | throw new InvalidParametersError(`Component ${component} did not implement the PubSub interface`) |
| 20 | } |
| 21 | |
| 22 | return pubsub |
| 23 | } |