feat: change privates members to protected

This commit is contained in:
Irakli Gozalishvili 2020-12-01 10:58:19 -08:00
parent 7fd26cf6b9
commit 46c98e9cf6
No known key found for this signature in database
GPG Key ID: C80F9B292FB470DE
6 changed files with 108 additions and 83 deletions

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

@ -1,43 +1,14 @@
export = PubsubBaseProtocol;
/**
* @typedef {Object} InMessage
* @property {string} [from]
* @property {string} receivedFrom
* @property {string[]} topicIDs
* @property {Uint8Array} [seqno]
* @property {Uint8Array} data
* @property {Uint8Array} [signature]
* @property {Uint8Array} [key]
*
* @typedef PeerId
* @type import('peer-id')
*/
/**
* 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 {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
* @abstract
* @param {Options} options
*/
constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: {
debugName: string;
multicodecs: Array<string> | string;
libp2p: any;
globalSignaturePolicy: {
StrictSign: "StrictSign";
StrictNoSign: string;
} | undefined;
canRelayMessage: boolean | undefined;
emitSelf: boolean | undefined;
});
constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: Options);
log: any;
/**
* @type {Array<string>}
@ -98,27 +69,34 @@ declare class PubsubBaseProtocol {
_registrarId: any;
/**
* On an inbound stream opened.
* @private
*
* @protected
* @param {Object} props
* @param {string} props.protocol
* @param {DuplexIterableStream} props.stream
* @param {Connection} props.connection connection
*/
private _onIncomingStream;
protected _onIncomingStream({ protocol, stream, connection }: {
protocol: string;
stream: any;
connection: any;
}): void;
/**
* Registrar notifies an established connection with pubsub protocol.
* @private
*
* @protected
* @param {PeerId} peerId remote peer-id
* @param {Connection} conn connection to the peer
*/
private _onPeerConnected;
protected _onPeerConnected(peerId: PeerId, conn: any): Promise<void>;
/**
* Registrar notifies a closing connection with pubsub protocol.
* @private
*
* @protected
* @param {PeerId} peerId peerId
* @param {Error} err error for connection end
*/
private _onPeerDisconnected;
protected _onPeerDisconnected(peerId: PeerId, err: Error): void;
/**
* Register the pubsub protocol onto the libp2p node.
* @returns {void}
@ -131,19 +109,21 @@ declare class PubsubBaseProtocol {
stop(): void;
/**
* Notifies the router that a peer has been connected
* @private
*
* @protected
* @param {PeerId} peerId
* @param {string} protocol
* @returns {PeerStreams}
*/
private _addPeer;
protected _addPeer(peerId: PeerId, protocol: string): PeerStreams;
/**
* Notifies the router that a peer has been disconnected.
* @private
*
* @protected
* @param {PeerId} peerId
* @returns {PeerStreams | undefined}
*/
private _removePeer;
protected _removePeer(peerId: PeerId): PeerStreams | undefined;
/**
* Responsible for processing each RPC message received by other peers.
* @param {string} idB58Str peer id string in base58
@ -231,11 +211,12 @@ 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
*
* @protected
* @param {Message} message
* @returns {Promise<Message>}
*/
private _buildMessage;
protected _buildMessage(message: any): Promise<any>;
/**
* Get a list of the peer-ids that are subscribed to one topic.
* @param {string} topic
@ -281,7 +262,7 @@ declare class PubsubBaseProtocol {
getTopics(): Array<string>;
}
declare namespace PubsubBaseProtocol {
export { message, utils, SignaturePolicy, InMessage, PeerId };
export { message, utils, SignaturePolicy, Options, InMessage, PeerId, SignaturePolicyType };
}
type PeerId = import("peer-id");
type InMessage = {
@ -294,9 +275,33 @@ type InMessage = {
key?: Uint8Array | undefined;
};
import PeerStreams = require("./peer-streams");
type Options = {
/**
* - log namespace
*/
debugName?: string | undefined;
/**
* - protocol identificers to connect
*/
multicodecs?: string | string[] | undefined;
libp2p: any;
/**
* - defines how signatures should be handled
*/
globalSignaturePolicy?: "StrictSign" | "StrictNoSign" | undefined;
/**
* - if can relay messages not subscribed
*/
canRelayMessage?: boolean | undefined;
/**
* - if publish should emit to self, if subscribed
*/
emitSelf?: boolean | undefined;
};
/**
* @type {typeof import('./message')}
*/
declare const message: typeof import('./message');
import utils = require("./utils");
import { SignaturePolicy } from "./signature-policy";
type SignaturePolicyType = "StrictSign" | "StrictNoSign";

View File

@ -22,34 +22,14 @@ const {
verifySignature
} = require('./message/sign')
/**
* @typedef {Object} InMessage
* @property {string} [from]
* @property {string} receivedFrom
* @property {string[]} topicIDs
* @property {Uint8Array} [seqno]
* @property {Uint8Array} data
* @property {Uint8Array} [signature]
* @property {Uint8Array} [key]
*
* @typedef PeerId
* @type import('peer-id')
*/
/**
* 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 {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
* @abstract
* @param {Options} options
*/
constructor ({
debugName,
@ -206,7 +186,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* On an inbound stream opened.
* @private
*
* @protected
* @param {Object} props
* @param {string} props.protocol
* @param {DuplexIterableStream} props.stream
@ -223,7 +204,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies an established connection with pubsub protocol.
* @private
*
* @protected
* @param {PeerId} peerId remote peer-id
* @param {Connection} conn connection to the peer
*/
@ -245,7 +227,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Registrar notifies a closing connection with pubsub protocol.
* @private
*
* @protected
* @param {PeerId} peerId peerId
* @param {Error} err error for connection end
*/
@ -258,7 +241,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Notifies the router that a peer has been connected
* @private
*
* @protected
* @param {PeerId} peerId
* @param {string} protocol
* @returns {PeerStreams}
@ -288,7 +272,8 @@ class PubsubBaseProtocol extends EventEmitter {
/**
* Notifies the router that a peer has been disconnected.
* @private
*
* @protected
* @param {PeerId} peerId
* @returns {PeerStreams | undefined}
*/
@ -564,7 +549,8 @@ 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
*
* @protected
* @param {Message} message
* @returns {Promise<Message>}
*/
@ -697,6 +683,27 @@ class PubsubBaseProtocol extends EventEmitter {
}
}
/**
* @typedef {Object} Options
* @property {string} [debugName] - log namespace
* @property {string[]|string} [multicodecs] - protocol identificers to connect
* @property {Libp2p} libp2p
* @property {SignaturePolicyType} [globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
* @property {boolean} [canRelayMessage = false] - if can relay messages not subscribed
* @property {boolean} [emitSelf = false] - if publish should emit to self, if subscribed
*
* @typedef {Object} InMessage
* @property {string} [from]
* @property {string} receivedFrom
* @property {string[]} topicIDs
* @property {Uint8Array} [seqno]
* @property {Uint8Array} data
* @property {Uint8Array} [signature]
* @property {Uint8Array} [key]
*
* @typedef {import('peer-id')} PeerId
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
*/
module.exports = PubsubBaseProtocol
module.exports.message = message
module.exports.utils = utils

View File

@ -35,22 +35,25 @@ declare class PeerStreams {
protocol: string;
/**
* The raw outbound stream, as retrieved from conn.newStream
* @private
*
* @protected
* @type {DuplexIterableStream}
*/
private _rawOutboundStream;
protected _rawOutboundStream: DuplexIterableStream;
/**
* The raw inbound stream, as retrieved from the callback from libp2p.handle
* @private
*
* @protected
* @type {DuplexIterableStream}
*/
private _rawInboundStream;
protected _rawInboundStream: DuplexIterableStream;
/**
* An AbortController for controlled shutdown of the inbound stream
* @private
*
* @protected
* @type {typeof AbortController}
*/
private _inboundAbortController;
protected _inboundAbortController: typeof AbortController;
/**
* Write stream -- its preferable to use the write method
* @type {import('it-pushable').Pushable<Uint8Array>>}
@ -108,5 +111,6 @@ type DuplexIterableStream = {
sink: Sink;
source: () => AsyncIterator<Uint8Array>;
};
import AbortController = require("abort-controller");
type PeerId = import("peer-id");
type Sink = (source: Uint8Array) => Promise<Uint8Array>;

View File

@ -48,19 +48,22 @@ class PeerStreams extends EventEmitter {
this.protocol = protocol
/**
* The raw outbound stream, as retrieved from conn.newStream
* @private
*
* @protected
* @type {DuplexIterableStream}
*/
this._rawOutboundStream = null
/**
* The raw inbound stream, as retrieved from the callback from libp2p.handle
* @private
*
* @protected
* @type {DuplexIterableStream}
*/
this._rawInboundStream = null
/**
* An AbortController for controlled shutdown of the inbound stream
* @private
*
* @protected
* @type {typeof AbortController}
*/
this._inboundAbortController = null

View File

@ -1,4 +1,5 @@
export type SignaturePolicyType = "StrictSign" | "StrictNoSign";
export namespace SignaturePolicy {
const StrictSign: 'StrictSign';
const StrictNoSign: string;
const StrictNoSign: 'StrictNoSign';
}

View File

@ -4,7 +4,7 @@
* Enum for Signature Policy
* Details how message signatures are produced/consumed
*/
exports.SignaturePolicy = {
const SignaturePolicy = {
/**
* On the producing side:
* * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
@ -24,5 +24,10 @@ exports.SignaturePolicy = {
* * Propagate only if the fields are absent, reject otherwise.
* * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
*/
StrictNoSign: /** @type {'StrictNoSign'} */ 'StrictNoSign'
StrictNoSign: /** @type {'StrictNoSign'} */ ('StrictNoSign')
}
exports.SignaturePolicy = SignaturePolicy
/**
* @typedef {SignaturePolicy[keyof SignaturePolicy]} SignaturePolicyType
*/