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 all 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;
}
Loading