mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-15 22:43:11 +00:00
Use new packages and fix most compilation errors. Remaining error to be dealt with in waku.ts.
35 lines
853 B
TypeScript
35 lines
853 B
TypeScript
import { Peer } from "@libp2p/interface-peer-store";
|
|
import { Libp2p } from "libp2p";
|
|
|
|
/**
|
|
* Returns a pseudo-random peer that supports the given protocol.
|
|
* Useful for protocols such as store and light push
|
|
*/
|
|
export async function selectRandomPeer(
|
|
peers: Peer[]
|
|
): Promise<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(
|
|
libp2p: Libp2p,
|
|
protocols: string[]
|
|
): Promise<Peer[]> {
|
|
const peers: Peer[] = [];
|
|
await libp2p.peerStore.forEach((peer) => {
|
|
for (let i = 0; i < protocols.length; i++) {
|
|
if (peer.protocols.includes(protocols[i])) {
|
|
peers.push(peer);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
return peers;
|
|
}
|