diff --git a/package.json b/package.json index f9678d9..632b81c 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "abortable-iterator": "^3.0.0", "chai": "^4.2.0", "chai-checkmark": "^1.0.1", - "class-is": "^1.1.0", "debug": "^4.1.1", "delay": "^4.3.0", "detect-node": "^2.0.4", @@ -70,7 +69,7 @@ "aegir": "^25.0.0", "it-handshake": "^1.0.1", "rimraf": "^3.0.2", - "typescript": "3.7.5" + "typescript": "^4.1.2" }, "contributors": [ "Alan Shaw ", diff --git a/src/connection/connection.d.ts b/src/connection/connection.d.ts index f74e296..41c289f 100644 --- a/src/connection/connection.d.ts +++ b/src/connection/connection.d.ts @@ -1,10 +1,16 @@ -declare const _exports: typeof Connection; -export = _exports; +export = Connection; /** * An implementation of the js-libp2p connection. * Any libp2p transport should use an upgrader to return this connection. */ declare class Connection { + /** + * Checks if the given value is a `Connection` instance. + * + * @param {any} other + * @returns {other is Connection} + */ + static isConnection(other: any): other is Connection; /** * Creates an instance of Connection. * @param {object} properties properties of the connection. @@ -24,10 +30,10 @@ declare class Connection { * @param {string} [properties.stat.encryption] connection encryption method identifier. */ constructor({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }: { - localAddr?: import("multiaddr"); - remoteAddr?: import("multiaddr"); - localPeer: import("peer-id"); - remotePeer: import("peer-id"); + localAddr: multiaddr; + remoteAddr: multiaddr; + localPeer: PeerId; + remotePeer: PeerId; newStream: Function; close: Function; getStreams: () => any[]; @@ -37,35 +43,35 @@ declare class Connection { open: string; upgraded: string; }; - multiplexer?: string; - encryption?: string; + multiplexer: string; + encryption: string; }; }); /** * Connection identifier. */ - id: any; + id: string; /** * Observed multiaddr of the local peer */ - localAddr: import("multiaddr"); + localAddr: multiaddr; /** * Observed multiaddr of the remote peer */ - remoteAddr: import("multiaddr"); + remoteAddr: multiaddr; /** * Local peer id. */ - localPeer: import("peer-id"); + localPeer: PeerId; /** * Remote peer id. */ - remotePeer: import("peer-id"); + remotePeer: PeerId; /** * Connection metadata. */ _stat: { - status: string; + status: "open"; direction: string; timeline: { open: string; @@ -95,12 +101,13 @@ declare class Connection { * @type {string[]} */ tags: string[]; + get [Symbol.toStringTag](): string; /** * Get connection metadata * @this {Connection} */ get stat(): { - status: string; + status: "open"; direction: string; timeline: { open: string; @@ -133,7 +140,7 @@ declare class Connection { */ addStream(muxedStream: any, { protocol, metadata }: { protocol: string; - metadata: any; + metadata: object; }): void; /** * Remove stream registry after it is closed. @@ -146,4 +153,8 @@ declare class Connection { */ close(): Promise; _closing: any; + get [connectionSymbol](): boolean; } +import multiaddr = require("multiaddr"); +import PeerId = require("peer-id"); +declare const connectionSymbol: unique symbol; diff --git a/src/connection/status.d.ts b/src/connection/status.d.ts index fc476fa..cef75f2 100644 --- a/src/connection/status.d.ts +++ b/src/connection/status.d.ts @@ -1,3 +1,3 @@ -export declare const OPEN: string; -export declare const CLOSING: string; -export declare const CLOSED: string; +export const OPEN: 'open'; +export const CLOSING: 'closing'; +export const CLOSED: 'closed'; diff --git a/src/pubsub/errors.d.ts b/src/pubsub/errors.d.ts index 4bbf5b3..b5349d1 100644 --- a/src/pubsub/errors.d.ts +++ b/src/pubsub/errors.d.ts @@ -1,11 +1,11 @@ export namespace codes { - export const ERR_INVALID_SIGNATURE_POLICY: string; - export const ERR_UNHANDLED_SIGNATURE_POLICY: string; - export const ERR_MISSING_SIGNATURE: string; - export const ERR_MISSING_SEQNO: string; - export const ERR_INVALID_SIGNATURE: string; - export const ERR_UNEXPECTED_FROM: string; - export const ERR_UNEXPECTED_SIGNATURE: string; - export const ERR_UNEXPECTED_KEY: string; - export const ERR_UNEXPECTED_SEQNO: string; + const ERR_INVALID_SIGNATURE_POLICY: string; + const ERR_UNHANDLED_SIGNATURE_POLICY: string; + const ERR_MISSING_SIGNATURE: string; + const ERR_MISSING_SEQNO: string; + const ERR_INVALID_SIGNATURE: string; + const ERR_UNEXPECTED_FROM: string; + const ERR_UNEXPECTED_SIGNATURE: string; + const ERR_UNEXPECTED_KEY: string; + const ERR_UNEXPECTED_SEQNO: string; } diff --git a/src/pubsub/index.d.ts b/src/pubsub/index.d.ts index f2a8d1c..a327bf7 100644 --- a/src/pubsub/index.d.ts +++ b/src/pubsub/index.d.ts @@ -29,11 +29,14 @@ declare class PubsubBaseProtocol { */ constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: { debugName: string; - multicodecs: string | string[]; + multicodecs: Array | string; libp2p: any; - globalSignaturePolicy?: any; - canRelayMessage?: boolean; - emitSelf?: boolean; + globalSignaturePolicy: { + StrictSign: "StrictSign"; + StrictNoSign: string; + }; + canRelayMessage: boolean; + emitSelf: boolean; }); log: any; /** @@ -91,7 +94,7 @@ declare class PubsubBaseProtocol { * Topic validators are functions with the following input: * @type {Map} */ - topicValidators: Map; + topicValidators: Map Promise>; _registrarId: any; /** * On an inbound stream opened. @@ -101,25 +104,21 @@ declare class PubsubBaseProtocol { * @param {DuplexIterableStream} props.stream * @param {Connection} props.connection connection */ - _onIncomingStream({ protocol, stream, connection }: { - protocol: string; - stream: any; - connection: any; - }): void; + private _onIncomingStream; /** * Registrar notifies an established connection with pubsub protocol. * @private * @param {PeerId} peerId remote peer-id * @param {Connection} conn connection to the peer */ - _onPeerConnected(peerId: import("peer-id"), conn: any): Promise; + private _onPeerConnected; /** * Registrar notifies a closing connection with pubsub protocol. * @private * @param {PeerId} peerId peerId * @param {Error} err error for connection end */ - _onPeerDisconnected(peerId: import("peer-id"), err: Error): void; + private _onPeerDisconnected; /** * Register the pubsub protocol onto the libp2p node. * @returns {void} @@ -137,14 +136,14 @@ declare class PubsubBaseProtocol { * @param {string} protocol * @returns {PeerStreams} */ - _addPeer(peerId: import("peer-id"), protocol: string): import("./peer-streams"); + private _addPeer; /** * Notifies the router that a peer has been disconnected. * @private * @param {PeerId} peerId * @returns {PeerStreams | undefined} */ - _removePeer(peerId: import("peer-id")): import("./peer-streams"); + private _removePeer; /** * Responsible for processing each RPC message received by other peers. * @param {string} idB58Str peer id string in base58 @@ -152,7 +151,7 @@ declare class PubsubBaseProtocol { * @param {PeerStreams} peerStreams PubSub peer * @returns {Promise} */ - _processMessages(idB58Str: string, stream: any, peerStreams: import("./peer-streams")): Promise; + _processMessages(idB58Str: string, stream: any, peerStreams: PeerStreams): Promise; /** * Handles an rpc request from a peer * @param {String} idB58Str @@ -160,7 +159,7 @@ declare class PubsubBaseProtocol { * @param {RPC} rpc * @returns {boolean} */ - _processRpc(idB58Str: string, peerStreams: import("./peer-streams"), rpc: any): boolean; + _processRpc(idB58Str: string, peerStreams: PeerStreams, rpc: any): boolean; /** * Handles a subscription change from a peer * @param {string} id @@ -236,13 +235,13 @@ declare class PubsubBaseProtocol { * @param {Message} message * @returns {Promise} */ - _buildMessage(message: any): Promise; + private _buildMessage; /** * Get a list of the peer-ids that are subscribed to one topic. * @param {string} topic * @returns {Array} */ - getSubscribers(topic: string): string[]; + getSubscribers(topic: string): Array; /** * Publishes messages to all subscribed peers * @override @@ -279,16 +278,12 @@ declare class PubsubBaseProtocol { * @override * @returns {Array} */ - getTopics(): string[]; + getTopics(): Array; } declare namespace PubsubBaseProtocol { export { message, utils, SignaturePolicy, InMessage, PeerId }; } type PeerId = import("peer-id"); -/** - * Topic validator function - */ -type validator = (arg0: string, arg1: InMessage) => Promise; type InMessage = { from?: string; receivedFrom: string; @@ -298,12 +293,10 @@ type InMessage = { signature?: Uint8Array; key?: Uint8Array; }; +import PeerStreams = require("./peer-streams"); /** * @type {typeof import('./message')} */ declare const message: typeof import('./message'); -declare const utils: typeof import("./utils"); -declare const SignaturePolicy: { - StrictSign: string; - StrictNoSign: string; -}; +import utils = require("./utils"); +import { SignaturePolicy } from "./signature-policy"; diff --git a/src/pubsub/message/index.d.ts b/src/pubsub/message/index.d.ts index 62df1ce..de14b7b 100644 --- a/src/pubsub/message/index.d.ts +++ b/src/pubsub/message/index.d.ts @@ -1,5 +1,4 @@ -export var rpc: any; -export var td: any; -export var RPC: any; -export var Message: any; -export var SubOpts: any; +declare const rpcProto: any; +declare const topicDescriptorProto: any; +export const RPC: any; +export { rpcProto as rpc, topicDescriptorProto as td }; diff --git a/src/pubsub/message/sign.d.ts b/src/pubsub/message/sign.d.ts index 3e1bee2..0750e01 100644 --- a/src/pubsub/message/sign.d.ts +++ b/src/pubsub/message/sign.d.ts @@ -13,7 +13,7 @@ export function messagePublicKey(message: any): Promise; * @param {Message} message * @returns {Promise} */ -export function signMessage(peerId: import("peer-id"), message: any): Promise; +export function signMessage(peerId: PeerId, message: any): Promise; export const SignPrefix: any; /** * Verifies the signature of the given message @@ -21,3 +21,4 @@ export const SignPrefix: any; * @returns {Promise} */ export function verifySignature(message: any): Promise; +import PeerId = require("peer-id"); diff --git a/src/pubsub/peer-streams.d.ts b/src/pubsub/peer-streams.d.ts index 632fe2c..00d1523 100644 --- a/src/pubsub/peer-streams.d.ts +++ b/src/pubsub/peer-streams.d.ts @@ -21,7 +21,7 @@ declare class PeerStreams { * @param {string} properties.protocol */ constructor({ id, protocol }: { - id: import("peer-id"); + id: PeerId; protocol: string; }); /** @@ -38,19 +38,19 @@ declare class PeerStreams { * @private * @type {DuplexIterableStream} */ - _rawOutboundStream: DuplexIterableStream; + private _rawOutboundStream; /** * The raw inbound stream, as retrieved from the callback from libp2p.handle * @private * @type {DuplexIterableStream} */ - _rawInboundStream: DuplexIterableStream; + private _rawInboundStream; /** * An AbortController for controlled shutdown of the inbound stream * @private * @type {typeof AbortController} */ - _inboundAbortController: typeof AbortController; + private _inboundAbortController; /** * Write stream -- its preferable to use the write method * @type {import('it-pushable').Pushable>} @@ -106,8 +106,7 @@ declare namespace PeerStreams { } type DuplexIterableStream = { sink: Sink; - source: () => AsyncIterator; + source: () => AsyncIterator; }; -declare const AbortController: typeof import("abort-controller"); -type Sink = (source: Uint8Array) => Promise; type PeerId = import("peer-id"); +type Sink = (source: Uint8Array) => Promise; diff --git a/src/pubsub/signature-policy.d.ts b/src/pubsub/signature-policy.d.ts index cf04e15..f8b07a1 100644 --- a/src/pubsub/signature-policy.d.ts +++ b/src/pubsub/signature-policy.d.ts @@ -1,4 +1,4 @@ export namespace SignaturePolicy { - export const StrictSign: string; - export const StrictNoSign: string; + const StrictSign: 'StrictSign'; + const StrictNoSign: string; } diff --git a/src/pubsub/utils.d.ts b/src/pubsub/utils.d.ts index f0ae093..1cc14a4 100644 --- a/src/pubsub/utils.d.ts +++ b/src/pubsub/utils.d.ts @@ -1,7 +1,13 @@ export function randomSeqno(): Uint8Array; export function msgId(from: string, seqno: Uint8Array): Uint8Array; export function noSignMsgId(data: Uint8Array): Uint8Array; -export function anyMatch(a: any[] | Set, b: any[] | Set): boolean; -export function ensureArray(maybeArray: any): any[]; -export function normalizeInRpcMessage(message: any, peerId: string): any; -export function normalizeOutRpcMessage(message: any): any; +export function anyMatch(a: Set | any[], b: Set | any[]): boolean; +export function ensureArray(maybeArray: T | T[]): T[]; +export function normalizeInRpcMessage(message: T, peerId?: string): T & { + from?: string; + peerId?: string; +}; +export function normalizeOutRpcMessage(message: T): T & { + from?: Uint8Array; + data?: Uint8Array; +}; diff --git a/src/topology/index.d.ts b/src/topology/index.d.ts index d2e1141..70241f4 100644 --- a/src/topology/index.d.ts +++ b/src/topology/index.d.ts @@ -1,10 +1,16 @@ -declare const _exports: Topology; -export = _exports; +export = Topology; declare class Topology { + /** + * Checks if the given value is a Topology instance. + * + * @param {any} other + * @returns {other is Topology} + */ + static isTopology(other: any): other is Topology; /** * @param {Object} props - * @param {number} props.min minimum needed connections (default: 0) - * @param {number} props.max maximum needed connections (default: Infinity) + * @param {number} [props.min] minimum needed connections (default: 0) + * @param {number} [props.max] maximum needed connections (default: Infinity) * @param {Object} [props.handlers] * @param {function} [props.handlers.onConnect] protocol "onConnect" handler * @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler @@ -13,9 +19,9 @@ declare class Topology { constructor({ min, max, handlers }: { min: number; max: number; - handlers?: { - onConnect?: Function; - onDisconnect?: Function; + handlers: { + onConnect: Function; + onDisconnect: Function; }; }); min: number; @@ -27,6 +33,7 @@ declare class Topology { * @type {Set} */ peers: Set; + get [Symbol.toStringTag](): string; set registrar(arg: any); _registrar: any; /** @@ -39,4 +46,6 @@ declare class Topology { * @returns {void} */ disconnect(peerId: import("peer-id")): void; + get [topologySymbol](): boolean; } +declare const topologySymbol: unique symbol; diff --git a/src/topology/multicodec-topology.d.ts b/src/topology/multicodec-topology.d.ts index 2038926..06d16f5 100644 --- a/src/topology/multicodec-topology.d.ts +++ b/src/topology/multicodec-topology.d.ts @@ -1,5 +1,12 @@ export = MulticodecTopology; -declare class MulticodecTopology { +declare class MulticodecTopology extends Topology { + /** + * Checks if the given value is a `MulticodecTopology` instance. + * + * @param {any} other + * @returns {other is MulticodecTopology} + */ + static isMulticodecTopology(other: any): other is MulticodecTopology; /** * @param {Object} props * @param {number} [props.min] minimum needed connections (default: 0) @@ -11,16 +18,15 @@ declare class MulticodecTopology { * @constructor */ constructor({ min, max, multicodecs, handlers }: { - min?: number; - max?: number; - multicodecs: string[]; + min: number; + max: number; + multicodecs: Array; handlers: { onConnect: Function; onDisconnect: Function; }; }); multicodecs: string[]; - _registrar: any; /** * Check if a new peer support the multicodecs for this topology. * @param {Object} props @@ -29,7 +35,7 @@ declare class MulticodecTopology { */ _onProtocolChange({ peerId, protocols }: { peerId: any; - protocols: string[]; + protocols: Array; }): void; /** * Verify if a new connected peer has a topology multicodec and call _onConnect. @@ -37,15 +43,17 @@ declare class MulticodecTopology { * @returns {void} */ _onPeerConnect(connection: any): void; - set registrar(arg: any); /** * Update topology. * @param {Array<{id: PeerId, multiaddrs: Array, protocols: Array}>} peerDataIterable * @returns {void} */ - _updatePeers(peerDataIterable: { + _updatePeers(peerDataIterable: Array<{ id: any; - multiaddrs: any[]; - protocols: string[]; - }[]): void; + multiaddrs: Array; + protocols: Array; + }>): void; + get [multicodecTopologySymbol](): boolean; } +import Topology = require("."); +declare const multicodecTopologySymbol: unique symbol;