From 46c98e9cf630b55dd18f341840a3bccd5eb58442 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 1 Dec 2020 10:58:19 -0800 Subject: [PATCH] feat: change privates members to protected --- src/pubsub/index.d.ts | 93 +++++++++++++++++--------------- src/pubsub/index.js | 61 +++++++++++---------- src/pubsub/peer-streams.d.ts | 16 +++--- src/pubsub/peer-streams.js | 9 ++-- src/pubsub/signature-policy.d.ts | 3 +- src/pubsub/signature-policy.js | 9 +++- 6 files changed, 108 insertions(+), 83 deletions(-) diff --git a/src/pubsub/index.d.ts b/src/pubsub/index.d.ts index cbb6881..b2b26c8 100644 --- a/src/pubsub/index.d.ts +++ b/src/pubsub/index.d.ts @@ -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} 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; - 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} @@ -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; /** * 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} */ - private _buildMessage; + protected _buildMessage(message: any): Promise; /** * 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; } 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"; diff --git a/src/pubsub/index.js b/src/pubsub/index.js index a763fed..ef99aec 100644 --- a/src/pubsub/index.js +++ b/src/pubsub/index.js @@ -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} 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} */ @@ -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 diff --git a/src/pubsub/peer-streams.d.ts b/src/pubsub/peer-streams.d.ts index 00d1523..21be210 100644 --- a/src/pubsub/peer-streams.d.ts +++ b/src/pubsub/peer-streams.d.ts @@ -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>} @@ -108,5 +111,6 @@ type DuplexIterableStream = { sink: Sink; source: () => AsyncIterator; }; +import AbortController = require("abort-controller"); type PeerId = import("peer-id"); type Sink = (source: Uint8Array) => Promise; diff --git a/src/pubsub/peer-streams.js b/src/pubsub/peer-streams.js index a41e1d8..6cb6d04 100644 --- a/src/pubsub/peer-streams.js +++ b/src/pubsub/peer-streams.js @@ -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 diff --git a/src/pubsub/signature-policy.d.ts b/src/pubsub/signature-policy.d.ts index f8b07a1..576c28c 100644 --- a/src/pubsub/signature-policy.d.ts +++ b/src/pubsub/signature-policy.d.ts @@ -1,4 +1,5 @@ +export type SignaturePolicyType = "StrictSign" | "StrictNoSign"; export namespace SignaturePolicy { const StrictSign: 'StrictSign'; - const StrictNoSign: string; + const StrictNoSign: 'StrictNoSign'; } diff --git a/src/pubsub/signature-policy.js b/src/pubsub/signature-policy.js index d81d8b3..7bfb193 100644 --- a/src/pubsub/signature-policy.js +++ b/src/pubsub/signature-policy.js @@ -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 + */