mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-02-27 03:23:20 +00:00
* upgrade libp2p version, partially update protocols, rename to IBaseProtocol * complete transition for protocols * complete transition of connection maanger * finish sdk * complete core * complete relay * complete peer-exchange * complete dns-discovery * add components field to Libp2p interface and use it in core * add type hack for Libp2p creation: * finish waku node test * complete relay test * complete peer exchange * complete dns peer discovery test * add missing dependency to relay * fix new peer store integration * improve initialization of pubsub * add catch for missing peer * update test and remove extra dependency * prevent error throw * fix edge case with peerStore * fix peer exchange * fix protocols used * fix test with another evnet * bump libp2p and interfaces * add missing package * fix peer-exchange problem * prefer libp2p peerDiscovery for integration tests * fix import * increate timeout * return test against Test fleet * remove await for peer:update * increase timeout * add await for peerStore * comment event for testing * fix lint * remove bind * fix stub * decouple to separate test case * move back to explicit build * remove only * do not test event
62 lines
1.9 KiB
TypeScript
62 lines
1.9 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 type { IBaseProtocol, Libp2pComponents } from "@waku/interfaces";
|
|
import {
|
|
getPeersForProtocol,
|
|
selectConnection,
|
|
selectPeerForProtocol,
|
|
} from "@waku/utils/libp2p";
|
|
|
|
/**
|
|
* A class with predefined helpers, to be used as a base to implement Waku
|
|
* Protocols.
|
|
*/
|
|
export class BaseProtocol implements IBaseProtocol {
|
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
|
|
|
constructor(public multicodec: string, private components: Libp2pComponents) {
|
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
|
components.events
|
|
);
|
|
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
|
|
components.events
|
|
);
|
|
}
|
|
|
|
public get peerStore(): PeerStore {
|
|
return this.components.peerStore;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
public async peers(): Promise<Peer[]> {
|
|
return getPeersForProtocol(this.peerStore, [this.multicodec]);
|
|
}
|
|
|
|
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.components.connectionManager.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);
|
|
}
|
|
}
|