logos-messaging-js/src/lib/waku_relay/get_relay_peers.ts
Franck Royer 92a76b3be0
Support multiple protocol id for relay
Support id of latest nim-waku release 0.4 and current master.
2021-07-21 15:43:30 +10:00

49 lines
1.2 KiB
TypeScript

import Gossipsub from 'libp2p-gossipsub';
import { shuffle } from 'libp2p-gossipsub/src/utils';
import { RelayCodecs } from './index';
/**
* Given a topic, returns up to count peers subscribed to that topic
* that pass an optional filter function
*
* @param {Gossipsub} router
* @param {String} topic
* @param {Number} count
* @param {Function} [filter] a function to filter acceptable peers
* @returns {Set<string>}
*
*/
export function getRelayPeers(
router: Gossipsub,
topic: string,
count: number,
filter: (id: string) => boolean = (): boolean => true
): Set<string> {
const peersInTopic = router.topics.get(topic);
if (!peersInTopic) {
return new Set();
}
// Adds all peers using our protocol
// that also pass the filter function
let peers: string[] = [];
peersInTopic.forEach((id: string) => {
const peerStreams = router.peers.get(id);
if (!peerStreams) {
return;
}
if (RelayCodecs.includes(peerStreams.protocol) && filter(id)) {
peers.push(id);
}
});
// Pseudo-randomly shuffles peers
peers = shuffle(peers);
if (count > 0 && peers.length > count) {
peers = peers.slice(0, count);
}
return new Set(peers);
}