mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-08 08:43:09 +00:00
Each protocol implementation have some common functionality. Implements those in `BaseProtocol`, to be extended by each protocol implementation.
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import type { Stream } from "@libp2p/interface-connection";
|
|
import type { Libp2p } from "@libp2p/interface-libp2p";
|
|
import type { PeerId } from "@libp2p/interface-peer-id";
|
|
import { Peer, PeerStore } from "@libp2p/interface-peer-store";
|
|
import {
|
|
getPeersForProtocol,
|
|
selectConnection,
|
|
selectPeerForProtocol,
|
|
} from "@waku/utils";
|
|
|
|
/**
|
|
* A class with predefined helpers, to be used as a base to implement Waku
|
|
* Protocols.
|
|
*/
|
|
export class BaseProtocol {
|
|
constructor(public multicodec: string, public libp2p: Libp2p) {}
|
|
|
|
/**
|
|
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
|
* the class protocol. Waku may or may not be currently connected to these
|
|
* peers.
|
|
*/
|
|
async peers(): Promise<Peer[]> {
|
|
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
|
}
|
|
|
|
get peerStore(): PeerStore {
|
|
return this.libp2p.peerStore;
|
|
}
|
|
|
|
protected async getPeer(peerId?: PeerId): Promise<Peer> {
|
|
const { peer } = await selectPeerForProtocol(
|
|
this.peerStore,
|
|
[this.multicodec],
|
|
peerId
|
|
);
|
|
return peer;
|
|
}
|
|
protected async newStream(peer: Peer): Promise<Stream> {
|
|
const connections = this.libp2p.getConnections(peer.id);
|
|
const connection = selectConnection(connections);
|
|
if (!connection) {
|
|
throw new Error("Failed to get a connection to the peer");
|
|
}
|
|
|
|
return connection.newStream(this.multicodec);
|
|
}
|
|
}
|