Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

fix: typedefs for MulticodecTopology #73

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint": "aegir lint",
"build": "aegir build",
"pregenerate:types": "rimraf './src/**/*.d.ts'",
"generate:types": "tsc",
"generate:types": "tsc --build",
"test": "aegir test",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -70,7 +69,7 @@
"aegir": "^25.0.0",
"it-handshake": "^1.0.1",
"rimraf": "^3.0.2",
"typescript": "3.7.5"
"typescript": "^4.0.5"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
Expand Down
51 changes: 31 additions & 20 deletions src/connection/connection.d.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 | undefined;
remoteAddr: multiaddr | undefined;
localPeer: PeerId;
remotePeer: PeerId;
newStream: Function;
close: Function;
getStreams: () => any[];
Expand All @@ -37,42 +43,42 @@ declare class Connection {
open: string;
upgraded: string;
};
multiplexer?: string;
encryption?: string;
multiplexer: string | undefined;
encryption: string | undefined;
};
});
/**
* Connection identifier.
*/
id: any;
id: string;
/**
* Observed multiaddr of the local peer
*/
localAddr: import("multiaddr");
localAddr: multiaddr | undefined;
/**
* Observed multiaddr of the remote peer
*/
remoteAddr: import("multiaddr");
remoteAddr: multiaddr | undefined;
/**
* 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;
upgraded: string;
};
multiplexer?: string;
encryption?: string;
multiplexer?: string | undefined;
encryption?: string | undefined;
};
/**
* Reference to the new stream function of the multiplexer
Expand All @@ -95,19 +101,20 @@ 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;
upgraded: string;
};
multiplexer?: string;
encryption?: string;
multiplexer?: string | undefined;
encryption?: string | undefined;
};
/**
* Get all the streams of the muxer.
Expand All @@ -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.
Expand All @@ -146,4 +153,8 @@ declare class Connection {
*/
close(): Promise<void>;
_closing: any;
get [connectionSymbol](): boolean;
}
import multiaddr = require("multiaddr");
import PeerId = require("peer-id");
declare const connectionSymbol: unique symbol;
28 changes: 22 additions & 6 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict'
/* eslint-disable valid-jsdoc */

const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const withIs = require('class-is')
const errCode = require('err-code')
const Status = require('./status')

const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection')

function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
Expand Down Expand Up @@ -138,6 +140,24 @@ class Connection {
this.tags = []
}

get [Symbol.toStringTag] () {
return 'Connection'
}

get [connectionSymbol] () {
return true
}

/**
* Checks if the given value is a `Connection` instance.
*
* @param {any} other
* @returns {other is Connection}
*/
static isConnection (other) {
return Boolean(other && other[connectionSymbol])
}

/**
* Get connection metadata
* @this {Connection}
Expand Down Expand Up @@ -227,8 +247,4 @@ class Connection {
}
}

/**
* @module
* @type {typeof Connection}
*/
module.exports = withIs(Connection, { className: 'Connection', symbolName: '@libp2p/interface-connection/connection' })
module.exports = Connection
6 changes: 3 additions & 3 deletions src/connection/status.d.ts
Original file line number Diff line number Diff line change
@@ -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';
6 changes: 3 additions & 3 deletions src/connection/status.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = {
OPEN: 'open',
CLOSING: 'closing',
CLOSED: 'closed'
OPEN: /** @type {'open'} */('open'),
CLOSING: /** @type {'closing'} */('closing'),
CLOSED: /** @type {'closed'} */('closed')
}
18 changes: 9 additions & 9 deletions src/pubsub/errors.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
57 changes: 25 additions & 32 deletions src/pubsub/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ declare class PubsubBaseProtocol {
*/
constructor({ debugName, multicodecs, libp2p, globalSignaturePolicy, canRelayMessage, emitSelf }: {
debugName: string;
multicodecs: string | string[];
multicodecs: Array<string> | string;
libp2p: any;
globalSignaturePolicy?: any;
canRelayMessage?: boolean;
emitSelf?: boolean;
globalSignaturePolicy: {
StrictSign: "StrictSign";
StrictNoSign: string;
} | undefined;
canRelayMessage: boolean | undefined;
emitSelf: boolean | undefined;
});
log: any;
/**
Expand Down Expand Up @@ -91,7 +94,7 @@ declare class PubsubBaseProtocol {
* Topic validators are functions with the following input:
* @type {Map<string, validator>}
*/
topicValidators: Map<string, validator>;
topicValidators: Map<string, (arg0: string, arg1: InMessage) => Promise<void>>;
_registrarId: any;
/**
* On an inbound stream opened.
Expand All @@ -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<void>;
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}
Expand All @@ -137,30 +136,30 @@ 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
* @param {DuplexIterableStream} stream inbound stream
* @param {PeerStreams} peerStreams PubSub peer
* @returns {Promise<void>}
*/
_processMessages(idB58Str: string, stream: any, peerStreams: import("./peer-streams")): Promise<void>;
_processMessages(idB58Str: string, stream: any, peerStreams: PeerStreams): Promise<void>;
/**
* Handles an rpc request from a peer
* @param {String} idB58Str
* @param {PeerStreams} peerStreams
* @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
Expand Down Expand Up @@ -236,13 +235,13 @@ declare class PubsubBaseProtocol {
* @param {Message} message
* @returns {Promise<Message>}
*/
_buildMessage(message: any): Promise<any>;
private _buildMessage;
/**
* Get a list of the peer-ids that are subscribed to one topic.
* @param {string} topic
* @returns {Array<string>}
*/
getSubscribers(topic: string): string[];
getSubscribers(topic: string): Array<string>;
/**
* Publishes messages to all subscribed peers
* @override
Expand Down Expand Up @@ -279,31 +278,25 @@ declare class PubsubBaseProtocol {
* @override
* @returns {Array<String>}
*/
getTopics(): string[];
getTopics(): Array<string>;
}
declare namespace PubsubBaseProtocol {
export { message, utils, SignaturePolicy, InMessage, PeerId };
}
type PeerId = import("peer-id");
/**
* Topic validator function
*/
type validator = (arg0: string, arg1: InMessage) => Promise<void>;
type InMessage = {
from?: string;
from?: string | undefined;
receivedFrom: string;
topicIDs: string[];
seqno?: Uint8Array;
seqno?: Uint8Array | undefined;
data: Uint8Array;
signature?: Uint8Array;
key?: Uint8Array;
signature?: Uint8Array | undefined;
key?: Uint8Array | undefined;
};
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";
1 change: 1 addition & 0 deletions src/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
/* eslint-disable valid-jsdoc */

const debug = require('debug')
const EventEmitter = require('events')
Expand Down
Loading