mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-10 17:53:09 +00:00
23 lines
662 B
TypeScript
23 lines
662 B
TypeScript
import Libp2p from 'libp2p';
|
|
import { Peer } from 'libp2p/src/peer-store';
|
|
|
|
/**
|
|
* Returns a pseudo-random peer that supports the given protocol.
|
|
* Useful for protocols such as store and light push
|
|
*/
|
|
export function selectRandomPeer(peers: Peer[]): Peer | undefined {
|
|
if (peers.length === 0) return;
|
|
|
|
const index = Math.round(Math.random() * (peers.length - 1));
|
|
return peers[index];
|
|
}
|
|
|
|
/**
|
|
* Returns the list of peers that supports the given protocol.
|
|
*/
|
|
export function getPeersForProtocol(libp2p: Libp2p, protocol: string): Peer[] {
|
|
return Array.from(libp2p.peerStore.peers.values()).filter((peer) =>
|
|
peer.protocols.includes(protocol)
|
|
);
|
|
}
|