feat: add types

This commit is contained in:
Vasco Santos
2020-12-01 12:24:51 +01:00
parent 7fd26cf6b9
commit 6a95834570
25 changed files with 726 additions and 248 deletions

92
src/pubsub/index.d.ts vendored
View File

@ -13,28 +13,25 @@ export = PubsubBaseProtocol;
* @type import('peer-id')
*/
/**
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
* and specifies the API that pubsub routers should have.
*/
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
* and specifies the API that pubsub routers should have.
*/
declare class PubsubBaseProtocol {
/**
* @param {Object} props
* @param {String} props.debugName log namespace
* @param {Array<string>|string} props.multicodecs protocol identificers to connect
* @param {string} props.debugName - log namespace
* @param {Array<string>|string} props.multicodecs - protocol identificers to connect
* @param {Libp2p} props.libp2p
* @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] defines how signatures should be handled
* @param {boolean} [props.canRelayMessage = false] if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] if publish should emit to self, if subscribed
* @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
* @param {boolean} [props.canRelayMessage = false] - if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] - if publish should emit to self, if subscribed
* @abstract
*/
constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: {
debugName: string;
multicodecs: Array<string> | string;
libp2p: any;
globalSignaturePolicy: {
StrictSign: "StrictSign";
StrictNoSign: string;
} | undefined;
globalSignaturePolicy: any;
canRelayMessage: boolean | undefined;
emitSelf: boolean | undefined;
});
@ -58,6 +55,7 @@ declare class PubsubBaseProtocol {
topics: Map<string, Set<string>>;
/**
* List of our subscriptions
*
* @type {Set<string>}
*/
subscriptions: Set<string>;
@ -75,16 +73,19 @@ declare class PubsubBaseProtocol {
globalSignaturePolicy: string;
/**
* If router can relay received messages, even if not subscribed
*
* @type {boolean}
*/
canRelayMessage: boolean;
/**
* if publish should emit to self, if subscribed
*
* @type {boolean}
*/
emitSelf: boolean;
/**
* Topic validator function
*
* @typedef {function(string, InMessage): Promise<void>} validator
*/
/**
@ -92,45 +93,52 @@ declare class PubsubBaseProtocol {
*
* Keyed by topic
* Topic validators are functions with the following input:
*
* @type {Map<string, validator>}
*/
topicValidators: Map<string, (arg0: string, arg1: InMessage) => Promise<void>>;
_registrarId: any;
/**
* On an inbound stream opened.
*
* @private
* @param {Object} props
* @param {string} props.protocol
* @param {DuplexIterableStream} props.stream
* @param {Connection} props.connection connection
* @param {Connection} props.connection - connection
*/
private _onIncomingStream;
/**
* Registrar notifies an established connection with pubsub protocol.
*
* @private
* @param {PeerId} peerId remote peer-id
* @param {Connection} conn connection to the peer
* @param {PeerId} peerId - remote peer-id
* @param {Connection} conn - connection to the peer
*/
private _onPeerConnected;
/**
* Registrar notifies a closing connection with pubsub protocol.
*
* @private
* @param {PeerId} peerId peerId
* @param {Error} err error for connection end
* @param {PeerId} peerId - peerId
* @param {Error} err - error for connection end
*/
private _onPeerDisconnected;
/**
* 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;
/**
* Notifies the router that a peer has been connected
*
* @private
* @param {PeerId} peerId
* @param {string} protocol
@ -139,6 +147,7 @@ declare class PubsubBaseProtocol {
private _addPeer;
/**
* Notifies the router that a peer has been disconnected.
*
* @private
* @param {PeerId} peerId
* @returns {PeerStreams | undefined}
@ -146,47 +155,54 @@ declare class PubsubBaseProtocol {
private _removePeer;
/**
* Responsible for processing each RPC message received by other peers.
* @param {string} idB58Str peer id string in base58
* @param {DuplexIterableStream} stream inbound stream
* @param {PeerStreams} peerStreams PubSub peer
*
* @param {string} idB58Str - peer id string in base58
* @param {DuplexIterableStream} stream - inbound stream
* @param {PeerStreams} peerStreams - PubSub peer
* @returns {Promise<void>}
*/
_processMessages(idB58Str: string, stream: any, peerStreams: PeerStreams): Promise<void>;
_processMessages(idB58Str: string, stream: any, peerStreams: import("./peer-streams")): Promise<void>;
/**
* Handles an rpc request from a peer
* @param {String} idB58Str
*
* @param {string} idB58Str
* @param {PeerStreams} peerStreams
* @param {RPC} rpc
* @returns {boolean}
*/
_processRpc(idB58Str: string, peerStreams: PeerStreams, rpc: any): boolean;
_processRpc(idB58Str: string, peerStreams: import("./peer-streams"), rpc: any): boolean;
/**
* Handles a subscription change from a peer
*
* @param {string} id
* @param {RPC.SubOpt} subOpt
*/
_processRpcSubOpt(id: string, subOpt: any): void;
/**
* Handles an message from a peer
*
* @param {InMessage} msg
* @returns {Promise<void>}
*/
_processRpcMessage(msg: InMessage): Promise<void>;
/**
* Emit a message from a peer
*
* @param {InMessage} message
*/
_emitMessage(message: InMessage): void;
/**
* The default msgID implementation
* Child class can override this.
* @param {RPC.Message} msg the message object
*
* @param {RPC.Message} msg - the message object
* @returns {Uint8Array} message id as bytes
*/
getMsgId(msg: any): Uint8Array;
/**
* Whether to accept a message from a peer
* Override to create a graylist
*
* @override
* @param {string} id
* @returns {boolean}
@ -195,6 +211,7 @@ declare class PubsubBaseProtocol {
/**
* Decode Uint8Array into an RPC object.
* This can be override to use a custom router protobuf.
*
* @param {Uint8Array} bytes
* @returns {RPC}
*/
@ -202,28 +219,32 @@ declare class PubsubBaseProtocol {
/**
* Encode RPC object into a Uint8Array.
* This can be override to use a custom router protobuf.
*
* @param {RPC} rpc
* @returns {Uint8Array}
*/
_encodeRpc(rpc: any): Uint8Array;
/**
* Send an rpc object to a peer
* @param {string} id peer id
*
* @param {string} id - peer id
* @param {RPC} rpc
* @returns {void}
*/
_sendRpc(id: string, rpc: any): void;
/**
* Send subscroptions to a peer
* @param {string} id peer id
*
* @param {string} id - peer id
* @param {string[]} topics
* @param {boolean} subscribe set to false for unsubscriptions
* @param {boolean} subscribe - set to false for unsubscriptions
* @returns {void}
*/
_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
*
* @param {InMessage} message
* @returns {Promise<void>}
*/
@ -231,6 +252,7 @@ declare class PubsubBaseProtocol {
/**
* Normalizes the message and signs it, if signing is enabled.
* Should be used by the routers to create the message to send.
*
* @private
* @param {Message} message
* @returns {Promise<Message>}
@ -238,12 +260,14 @@ declare class PubsubBaseProtocol {
private _buildMessage;
/**
* Get a list of the peer-ids that are subscribed to one topic.
*
* @param {string} topic
* @returns {Array<string>}
*/
getSubscribers(topic: string): Array<string>;
/**
* Publishes messages to all subscribed peers
*
* @override
* @param {string} topic
* @param {Buffer} message
@ -253,6 +277,7 @@ declare class PubsubBaseProtocol {
/**
* 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
* @param {InMessage} message
* @returns {Promise<void>}
@ -261,6 +286,7 @@ declare class PubsubBaseProtocol {
_publish(message: InMessage): Promise<void>;
/**
* Subscribes to a given topic.
*
* @abstract
* @param {string} topic
* @returns {void}
@ -268,6 +294,7 @@ declare class PubsubBaseProtocol {
subscribe(topic: string): void;
/**
* Unsubscribe from the given topic.
*
* @override
* @param {string} topic
* @returns {void}
@ -275,8 +302,9 @@ declare class PubsubBaseProtocol {
unsubscribe(topic: string): void;
/**
* Get the list of topics which the peer is subscribed to.
*
* @override
* @returns {Array<String>}
* @returns {Array<string>}
*/
getTopics(): Array<string>;
}
@ -293,10 +321,12 @@ type InMessage = {
signature?: Uint8Array | undefined;
key?: Uint8Array | undefined;
};
import PeerStreams = require("./peer-streams");
/**
* @type {typeof import('./message')}
*/
declare const message: typeof import('./message');
import utils = require("./utils");
import { SignaturePolicy } from "./signature-policy";
declare const utils: typeof import("./utils");
declare const SignaturePolicy: {
StrictSign: "StrictSign";
StrictNoSign: string;
};

View File

@ -37,18 +37,18 @@ const {
*/
/**
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
* and specifies the API that pubsub routers should have.
*/
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
* and specifies the API that pubsub routers should have.
*/
class PubsubBaseProtocol extends EventEmitter {
/**
* @param {Object} props
* @param {String} props.debugName log namespace
* @param {Array<string>|string} props.multicodecs protocol identificers to connect
* @param {string} props.debugName - log namespace
* @param {Array<string>|string} props.multicodecs - protocol identificers to connect
* @param {Libp2p} props.libp2p
* @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] defines how signatures should be handled
* @param {boolean} [props.canRelayMessage = false] if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] if publish should emit to self, if subscribed
* @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
* @param {boolean} [props.canRelayMessage = false] - if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] - if publish should emit to self, if subscribed
* @abstract
*/
constructor ({
@ -98,6 +98,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* List of our subscriptions
*
* @type {Set<string>}
*/
this.subscriptions = new Set()
@ -123,18 +124,21 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* If router can relay received messages, even if not subscribed
*
* @type {boolean}
*/
this.canRelayMessage = canRelayMessage
/**
* if publish should emit to self, if subscribed
*
* @type {boolean}
*/
this.emitSelf = emitSelf
/**
* Topic validator function
*
* @typedef {function(string, InMessage): Promise<void>} validator
*/
/**
@ -142,6 +146,7 @@ class PubsubBaseProtocol extends EventEmitter {
*
* Keyed by topic
* Topic validators are functions with the following input:
*
* @type {Map<string, validator>}
*/
this.topicValidators = new Map()
@ -156,6 +161,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Register the pubsub protocol onto the libp2p node.
*
* @returns {void}
*/
start () {
@ -185,6 +191,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Unregister the pubsub protocol and the streams with other peers will be closed.
*
* @returns {void}
*/
stop () {
@ -206,11 +213,12 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* On an inbound stream opened.
*
* @private
* @param {Object} props
* @param {string} props.protocol
* @param {DuplexIterableStream} props.stream
* @param {Connection} props.connection connection
* @param {Connection} props.connection - connection
*/
_onIncomingStream ({ protocol, stream, connection }) {
const peerId = connection.remotePeer
@ -223,9 +231,10 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies an established connection with pubsub protocol.
*
* @private
* @param {PeerId} peerId remote peer-id
* @param {Connection} conn connection to the peer
* @param {PeerId} peerId - remote peer-id
* @param {Connection} conn - connection to the peer
*/
async _onPeerConnected (peerId, conn) {
const idB58Str = peerId.toB58String()
@ -245,9 +254,10 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies a closing connection with pubsub protocol.
*
* @private
* @param {PeerId} peerId peerId
* @param {Error} err error for connection end
* @param {PeerId} peerId - peerId
* @param {Error} err - error for connection end
*/
_onPeerDisconnected (peerId, err) {
const idB58Str = peerId.toB58String()
@ -258,6 +268,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Notifies the router that a peer has been connected
*
* @private
* @param {PeerId} peerId
* @param {string} protocol
@ -288,6 +299,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Notifies the router that a peer has been disconnected.
*
* @private
* @param {PeerId} peerId
* @returns {PeerStreams | undefined}
@ -318,9 +330,10 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Responsible for processing each RPC message received by other peers.
* @param {string} idB58Str peer id string in base58
* @param {DuplexIterableStream} stream inbound stream
* @param {PeerStreams} peerStreams PubSub peer
*
* @param {string} idB58Str - peer id string in base58
* @param {DuplexIterableStream} stream - inbound stream
* @param {PeerStreams} peerStreams - PubSub peer
* @returns {Promise<void>}
*/
async _processMessages (idB58Str, stream, peerStreams) {
@ -343,7 +356,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Handles an rpc request from a peer
* @param {String} idB58Str
*
* @param {string} idB58Str
* @param {PeerStreams} peerStreams
* @param {RPC} rpc
* @returns {boolean}
@ -379,6 +393,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Handles a subscription change from a peer
*
* @param {string} id
* @param {RPC.SubOpt} subOpt
*/
@ -402,6 +417,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Handles an message from a peer
*
* @param {InMessage} msg
* @returns {Promise<void>}
*/
@ -426,6 +442,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Emit a message from a peer
*
* @param {InMessage} message
*/
_emitMessage (message) {
@ -439,7 +456,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* The default msgID implementation
* Child class can override this.
* @param {RPC.Message} msg the message object
*
* @param {RPC.Message} msg - the message object
* @returns {Uint8Array} message id as bytes
*/
getMsgId (msg) {
@ -457,6 +475,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Whether to accept a message from a peer
* Override to create a graylist
*
* @override
* @param {string} id
* @returns {boolean}
@ -468,6 +487,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Decode Uint8Array into an RPC object.
* This can be override to use a custom router protobuf.
*
* @param {Uint8Array} bytes
* @returns {RPC}
*/
@ -478,6 +498,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Encode RPC object into a Uint8Array.
* This can be override to use a custom router protobuf.
*
* @param {RPC} rpc
* @returns {Uint8Array}
*/
@ -487,7 +508,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Send an rpc object to a peer
* @param {string} id peer id
*
* @param {string} id - peer id
* @param {RPC} rpc
* @returns {void}
*/
@ -504,9 +526,10 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Send subscroptions to a peer
* @param {string} id peer id
*
* @param {string} id - peer id
* @param {string[]} topics
* @param {boolean} subscribe set to false for unsubscriptions
* @param {boolean} subscribe - set to false for unsubscriptions
* @returns {void}
*/
_sendSubscriptions (id, topics, subscribe) {
@ -518,6 +541,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Validates the given message. The signature will be checked for authenticity.
* Throws an error on invalid messages
*
* @param {InMessage} message
* @returns {Promise<void>}
*/
@ -564,6 +588,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Normalizes the message and signs it, if signing is enabled.
* Should be used by the routers to create the message to send.
*
* @private
* @param {Message} message
* @returns {Promise<Message>}
@ -586,6 +611,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Get a list of the peer-ids that are subscribed to one topic.
*
* @param {string} topic
* @returns {Array<string>}
*/
@ -607,6 +633,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Publishes messages to all subscribed peers
*
* @override
* @param {string} topic
* @param {Buffer} message
@ -640,6 +667,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* 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
* @param {InMessage} message
* @returns {Promise<void>}
@ -651,6 +679,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Subscribes to a given topic.
*
* @abstract
* @param {string} topic
* @returns {void}
@ -668,6 +697,7 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Unsubscribe from the given topic.
*
* @override
* @param {string} topic
* @returns {void}
@ -685,8 +715,9 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Get the list of topics which the peer is subscribed to.
*
* @override
* @returns {Array<String>}
* @returns {Array<string>}
*/
getTopics () {
if (!this.started) {

View File

@ -1,4 +1,5 @@
declare const rpcProto: any;
declare const topicDescriptorProto: any;
export const RPC: any;
export { rpcProto as rpc, topicDescriptorProto as td };
export var rpc: any;
export var td: any;
export var RPC: any;
export var Message: any;
export var SubOpts: any;

View File

@ -13,12 +13,12 @@ export function messagePublicKey(message: any): Promise<any>;
* @param {Message} message
* @returns {Promise<Message>}
*/
export function signMessage(peerId: PeerId, message: any): Promise<any>;
export function signMessage(peerId: import("peer-id"), message: any): Promise<any>;
export const SignPrefix: any;
/**
* Verifies the signature of the given message
*
* @param {InMessage} message
* @returns {Promise<Boolean>}
* @returns {Promise<boolean>}
*/
export function verifySignature(message: any): Promise<boolean>;
import PeerId = require("peer-id");

View File

@ -31,8 +31,9 @@ async function signMessage (peerId, message) {
/**
* Verifies the signature of the given message
*
* @param {InMessage} message
* @returns {Promise<Boolean>}
* @returns {Promise<boolean>}
*/
async function verifySignature (message) {
// Get message sans the signature

View File

@ -16,7 +16,7 @@ export = PeerStreams;
*/
declare class PeerStreams {
/**
* @param {object} properties properties of the PeerStreams.
* @param {object} properties - properties of the PeerStreams.
* @param {PeerId} properties.id
* @param {string} properties.protocol
*/
@ -30,34 +30,40 @@ declare class PeerStreams {
id: import('peer-id');
/**
* Established protocol
*
* @type {string}
*/
protocol: string;
/**
* The raw outbound stream, as retrieved from conn.newStream
*
* @private
* @type {DuplexIterableStream}
*/
private _rawOutboundStream;
/**
* The raw inbound stream, as retrieved from the callback from libp2p.handle
*
* @private
* @type {DuplexIterableStream}
*/
private _rawInboundStream;
/**
* An AbortController for controlled shutdown of the inbound stream
*
* @private
* @type {typeof AbortController}
*/
private _inboundAbortController;
/**
* Write stream -- its preferable to use the write method
*
* @type {import('it-pushable').Pushable<Uint8Array>>}
*/
outboundStream: import('it-pushable').Pushable<Uint8Array>;
/**
* Read stream
*
* @type {DuplexIterableStream}
*/
inboundStream: DuplexIterableStream;
@ -97,6 +103,7 @@ declare class PeerStreams {
attachOutboundStream(stream: any): Promise<void>;
/**
* Closes the open connection to peer
*
* @returns {void}
*/
close(): void;

View File

@ -30,7 +30,7 @@ log.error = debug('libp2p-pubsub:peer-streams:error')
*/
class PeerStreams extends EventEmitter {
/**
* @param {object} properties properties of the PeerStreams.
* @param {object} properties - properties of the PeerStreams.
* @param {PeerId} properties.id
* @param {string} properties.protocol
*/
@ -43,34 +43,40 @@ class PeerStreams extends EventEmitter {
this.id = id
/**
* Established protocol
*
* @type {string}
*/
this.protocol = protocol
/**
* The raw outbound stream, as retrieved from conn.newStream
*
* @private
* @type {DuplexIterableStream}
*/
this._rawOutboundStream = null
/**
* The raw inbound stream, as retrieved from the callback from libp2p.handle
*
* @private
* @type {DuplexIterableStream}
*/
this._rawInboundStream = null
/**
* An AbortController for controlled shutdown of the inbound stream
*
* @private
* @type {typeof AbortController}
*/
this._inboundAbortController = null
/**
* Write stream -- its preferable to use the write method
*
* @type {import('it-pushable').Pushable<Uint8Array>>}
*/
this.outboundStream = null
/**
* Read stream
*
* @type {DuplexIterableStream}
*/
this.inboundStream = null
@ -179,6 +185,7 @@ class PeerStreams extends EventEmitter {
/**
* Closes the open connection to peer
*
* @returns {void}
*/
close () {

View File

@ -91,7 +91,7 @@ exports.ensureArray = (maybeArray) => {
* @template {Object} T
* @param {T} message
* @param {string} [peerId]
* @return {T & {from?: string, peerId?: string }}
* @returns {T & {from?: string, peerId?: string }}
*/
exports.normalizeInRpcMessage = (message, peerId) => {
const m = Object.assign({}, message)
@ -108,7 +108,7 @@ exports.normalizeInRpcMessage = (message, peerId) => {
* @template {Object} T
*
* @param {T} message
* @return {T & {from?: Uint8Array, data?: Uint8Array}}
* @returns {T & {from?: Uint8Array, data?: Uint8Array}}
*/
exports.normalizeOutRpcMessage = (message) => {
const m = Object.assign({}, message)