mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-16 15:03:10 +00:00
32 lines
816 B
TypeScript
32 lines
816 B
TypeScript
import type { Peer, PeerStore } from "@libp2p/interface-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 async function getPeersForProtocol(
|
|
peerStore: PeerStore,
|
|
protocols: string[]
|
|
): Promise<Peer[]> {
|
|
const peers: Peer[] = [];
|
|
await peerStore.forEach((peer) => {
|
|
for (let i = 0; i < protocols.length; i++) {
|
|
if (peer.protocols.includes(protocols[i])) {
|
|
peers.push(peer);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
return peers;
|
|
}
|