libp2p-pubsub
Version:
180 lines • 5.96 kB
TypeScript
/// <reference types="node" />
import debug from 'debug';
import { EventEmitter } from 'events';
import Queue from 'p-queue';
import { RPC, IRPC } from './message/rpc.js';
import { PeerStreams } from './peer-streams.js';
import type { PeerId } from 'libp2p-interfaces/peer-id';
import type { Registrar, IncomingStreamEvent } from 'libp2p-interfaces/registrar';
import type { Connection } from 'libp2p-interfaces/connection';
import type BufferList from 'bl';
import type { PubSub, Message, StrictNoSign, StrictSign, PubsubOptions } from 'libp2p-interfaces/pubsub';
import type { Startable } from 'libp2p-interfaces';
export interface TopicValidator {
(topic: string, message: Message): Promise<void>;
}
/**
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
* and specifies the API that pubsub routers should have.
*/
export declare abstract class PubsubBaseProtocol extends EventEmitter implements PubSub, Startable {
peerId: PeerId;
started: boolean;
/**
* Map of topics to which peers are subscribed to
*/
topics: Map<string, Set<string>>;
/**
* List of our subscriptions
*/
subscriptions: Set<string>;
/**
* Map of peer streams
*/
peers: Map<string, PeerStreams>;
/**
* The signature policy to follow by default
*/
globalSignaturePolicy: StrictNoSign | StrictSign;
/**
* If router can relay received messages, even if not subscribed
*/
canRelayMessage: boolean;
/**
* if publish should emit to self, if subscribed
*/
emitSelf: boolean;
/**
* Topic validator map
*
* Keyed by topic
* Topic validators are functions with the following input:
*/
topicValidators: Map<string, TopicValidator>;
queue: Queue;
registrar: Registrar;
protected log: debug.Debugger & {
err: debug.Debugger;
};
protected multicodecs: string[];
protected _libp2p: any;
private _registrarId;
constructor(props: PubsubOptions);
/**
* Register the pubsub protocol onto the libp2p node.
*
* @returns {void}
*/
start(): void;
/**
* Unregister the pubsub protocol and the streams with other peers will be closed.
*
* @returns {void}
*/
stop(): void;
isStarted(): boolean;
/**
* On an inbound stream opened
*/
protected _onIncomingStream({ protocol, stream, connection }: IncomingStreamEvent): void;
/**
* Registrar notifies an established connection with pubsub protocol
*/
protected _onPeerConnected(peerId: PeerId, conn: Connection): Promise<void>;
/**
* Registrar notifies a closing connection with pubsub protocol
*/
protected _onPeerDisconnected(peerId: PeerId, conn?: Connection): void;
/**
* Notifies the router that a peer has been connected
*/
protected _addPeer(peerId: PeerId, protocol: string): PeerStreams;
/**
* Notifies the router that a peer has been disconnected
*/
protected _removePeer(peerId: PeerId): PeerStreams | undefined;
/**
* Responsible for processing each RPC message received by other peers.
*/
_processMessages(idB58Str: string, stream: AsyncIterable<Uint8Array | BufferList>, peerStreams: PeerStreams): Promise<void>;
/**
* Handles an rpc request from a peer
*/
_processRpc(idB58Str: string, peerStreams: PeerStreams, rpc: RPC): Promise<boolean>;
/**
* Handles a subscription change from a peer
*/
_processRpcSubOpt(id: string, subOpt: RPC.ISubOpts): void;
/**
* Handles an message from a peer
*/
_processRpcMessage(msg: Message): Promise<void>;
/**
* Emit a message from a peer
*/
_emitMessage(message: Message): void;
/**
* The default msgID implementation
* Child class can override this.
*/
getMsgId(msg: Message): import("multiformats/hashes/hasher").Await<Uint8Array>;
/**
* Whether to accept a message from a peer
* Override to create a graylist
*/
_acceptFrom(id: string): boolean;
/**
* Decode Uint8Array into an RPC object.
* This can be override to use a custom router protobuf.
*/
_decodeRpc(bytes: Uint8Array): RPC;
/**
* Encode RPC object into a Uint8Array.
* This can be override to use a custom router protobuf.
*/
_encodeRpc(rpc: IRPC): Uint8Array;
/**
* Send an rpc object to a peer
*/
_sendRpc(id: string, rpc: IRPC): void;
/**
* Send subscriptions to a peer
*/
_sendSubscriptions(id: string, topics: string[], subscribe: boolean): void;
/**
* Validates the given message. The signature will be checked for authenticity.
* Throws an error on invalid messages
*/
validate(message: Message): Promise<void>;
/**
* Normalizes the message and signs it, if signing is enabled.
* Should be used by the routers to create the message to send.
*/
protected _buildMessage(message: Message): Promise<Message>;
/**
* Get a list of the peer-ids that are subscribed to one topic.
*/
getSubscribers(topic: string): string[];
/**
* Publishes messages to all subscribed peers
*/
publish(topic: string, message: Uint8Array): Promise<void>;
/**
* Overriding the implementation of publish should handle the appropriate algorithms for the publish/subscriber implementation.
* For example, a Floodsub implementation might simply publish each message to each topic for every peer
*/
abstract _publish(message: Message): Promise<void>;
/**
* Subscribes to a given topic.
*/
subscribe(topic: string): void;
/**
* Unsubscribe from the given topic.
*/
unsubscribe(topic: string): void;
/**
* Get the list of topics which the peer is subscribed to.
*/
getTopics(): string[];
}
//# sourceMappingURL=index.d.ts.map