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

Commit 6a95834

Browse files
committed
feat: add types
1 parent 7fd26cf commit 6a95834

25 files changed

+727
-249
lines changed

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
"scripts": {
1313
"lint": "aegir lint",
1414
"build": "aegir build",
15-
"pregenerate:types": "rimraf './src/**/*.d.ts'",
16-
"generate:types": "tsc --build",
1715
"test": "aegir test",
1816
"test:node": "aegir test --target node",
1917
"test:browser": "aegir test --target browser",
18+
"test:types": "aegir ts -p check",
2019
"prepublishOnly": "npm run generate:types",
2120
"release": "aegir release -t node -t browser",
2221
"release-minor": "aegir release --type minor -t node -t browser",
23-
"release-major": "aegir release --type major -t node -t browser"
22+
"release-major": "aegir release --type major -t node -t browser",
23+
"remove:types": "rimraf './src/**/*.d.ts'"
2424
},
2525
"repository": {
2626
"type": "git",
@@ -63,13 +63,13 @@
6363
"protons": "^2.0.0",
6464
"sinon": "^9.0.2",
6565
"streaming-iterables": "^5.0.2",
66+
"typescript": "^4.1.2",
6667
"uint8arrays": "^1.1.0"
6768
},
6869
"devDependencies": {
69-
"aegir": "^25.0.0",
70+
"aegir": "^29.2.0",
7071
"it-handshake": "^1.0.1",
71-
"rimraf": "^3.0.2",
72-
"typescript": "^4.0.5"
72+
"rimraf": "^3.0.2"
7373
},
7474
"contributors": [
7575
"Alan Shaw <alan.shaw@protocol.ai>",

src/connection/connection.d.ts

+162-64
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
export = Connection;
2+
/**
3+
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
4+
*/
5+
/**
6+
* @typedef {Object} ConectionStat
7+
* @property {string} direction - connection establishment direction ("inbound" or "outbound").
8+
* @property {object} timeline - connection relevant events timestamp.
9+
* @property {number} timeline.open - connection opening timestamp.
10+
* @property {number} [timeline.upgraded] - connection upgraded timestamp.
11+
* @property {number} [timeline.close] - connection upgraded timestamp.
12+
* @property {string} [multiplexer] - connection multiplexing identifier.
13+
* @property {string} [encryption] - connection encryption method identifier.
14+
*
15+
* @typedef {Object} ConnectionOptions
16+
* @property {multiaddr} [localAddr] - local multiaddr of the connection if known.
17+
* @property {multiaddr} remoteAddr - remote multiaddr of the connection.
18+
* @property {PeerId} localPeer - local peer-id.
19+
* @property {PeerId} remotePeer - remote peer-id.
20+
* @property {(protocols: string|string[]) => Promise<{stream: MuxedStream, protocol: string}>} newStream - new stream muxer function.
21+
* @property {() => Promise<void>} close - close raw connection function.
22+
* @property {() => MuxedStream[]} getStreams - get streams from muxer function.
23+
* @property {ConectionStat} stat - metadata of the connection.
24+
*/
225
/**
326
* An implementation of the js-libp2p connection.
427
* Any libp2p transport should use an upgrader to return this connection.
@@ -12,149 +35,224 @@ declare class Connection {
1235
*/
1336
static isConnection(other: any): other is Connection;
1437
/**
15-
* Creates an instance of Connection.
16-
* @param {object} properties properties of the connection.
17-
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known.
18-
* @param {multiaddr} [properties.remoteAddr] remote multiaddr of the connection.
19-
* @param {PeerId} properties.localPeer local peer-id.
20-
* @param {PeerId} properties.remotePeer remote peer-id.
21-
* @param {function} properties.newStream new stream muxer function.
22-
* @param {function} properties.close close raw connection function.
23-
* @param {function(): Stream[]} properties.getStreams get streams from muxer function.
24-
* @param {object} properties.stat metadata of the connection.
25-
* @param {string} properties.stat.direction connection establishment direction ("inbound" or "outbound").
26-
* @param {object} properties.stat.timeline connection relevant events timestamp.
27-
* @param {string} properties.stat.timeline.open connection opening timestamp.
28-
* @param {string} properties.stat.timeline.upgraded connection upgraded timestamp.
29-
* @param {string} [properties.stat.multiplexer] connection multiplexing identifier.
30-
* @param {string} [properties.stat.encryption] connection encryption method identifier.
31-
*/
32-
constructor({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }: {
33-
localAddr: multiaddr | undefined;
34-
remoteAddr: multiaddr | undefined;
35-
localPeer: PeerId;
36-
remotePeer: PeerId;
37-
newStream: Function;
38-
close: Function;
39-
getStreams: () => any[];
40-
stat: {
41-
direction: string;
42-
timeline: {
43-
open: string;
44-
upgraded: string;
45-
};
46-
multiplexer: string | undefined;
47-
encryption: string | undefined;
48-
};
49-
});
38+
* An implementation of the js-libp2p connection.
39+
* Any libp2p transport should use an upgrader to return this connection.
40+
*
41+
* @class
42+
* @param {ConnectionOptions} options
43+
*/
44+
constructor({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }: ConnectionOptions);
5045
/**
5146
* Connection identifier.
5247
*/
5348
id: string;
5449
/**
5550
* Observed multiaddr of the local peer
5651
*/
57-
localAddr: multiaddr | undefined;
52+
localAddr: import("multiaddr") | undefined;
5853
/**
5954
* Observed multiaddr of the remote peer
6055
*/
61-
remoteAddr: multiaddr | undefined;
56+
remoteAddr: import("multiaddr");
6257
/**
6358
* Local peer id.
6459
*/
65-
localPeer: PeerId;
60+
localPeer: import("peer-id");
6661
/**
6762
* Remote peer id.
6863
*/
69-
remotePeer: PeerId;
64+
remotePeer: import("peer-id");
7065
/**
7166
* Connection metadata.
7267
*/
7368
_stat: {
7469
status: "open";
70+
/**
71+
* - connection establishment direction ("inbound" or "outbound").
72+
*/
7573
direction: string;
74+
/**
75+
* - connection relevant events timestamp.
76+
*/
7677
timeline: {
77-
open: string;
78-
upgraded: string;
78+
open: number;
79+
upgraded: number | undefined;
80+
close: number | undefined;
7981
};
82+
/**
83+
* - connection multiplexing identifier.
84+
*/
8085
multiplexer?: string | undefined;
86+
/**
87+
* - connection encryption method identifier.
88+
*/
8189
encryption?: string | undefined;
8290
};
8391
/**
8492
* Reference to the new stream function of the multiplexer
8593
*/
86-
_newStream: Function;
94+
_newStream: (protocols: string | string[]) => Promise<{
95+
stream: MuxedStream;
96+
protocol: string;
97+
}>;
8798
/**
8899
* Reference to the close function of the raw connection
89100
*/
90-
_close: Function;
101+
_close: () => Promise<void>;
91102
/**
92103
* Reference to the getStreams function of the muxer
93104
*/
94-
_getStreams: () => any[];
105+
_getStreams: () => MuxedStream[];
95106
/**
96107
* Connection streams registry
97108
*/
98109
registry: Map<any, any>;
99110
/**
100111
* User provided tags
112+
*
101113
* @type {string[]}
102114
*/
103115
tags: string[];
104116
get [Symbol.toStringTag](): string;
105117
/**
106118
* Get connection metadata
119+
*
107120
* @this {Connection}
108121
*/
109122
get stat(): {
110123
status: "open";
124+
/**
125+
* - connection establishment direction ("inbound" or "outbound").
126+
*/
111127
direction: string;
128+
/**
129+
* - connection relevant events timestamp.
130+
*/
112131
timeline: {
113-
open: string;
114-
upgraded: string;
132+
open: number;
133+
upgraded: number | undefined;
134+
close: number | undefined;
115135
};
136+
/**
137+
* - connection multiplexing identifier.
138+
*/
116139
multiplexer?: string | undefined;
140+
/**
141+
* - connection encryption method identifier.
142+
*/
117143
encryption?: string | undefined;
118144
};
119145
/**
120146
* Get all the streams of the muxer.
147+
*
121148
* @this {Connection}
122149
*/
123-
get streams(): any[];
150+
get streams(): import("../stream-muxer/types").MuxedStream[];
124151
/**
125152
* Create a new stream from this connection
126-
* @param {string[]} protocols intended protocol for the stream
127-
* @return {Promise<{stream: Stream, protocol: string}>} with muxed+multistream-selected stream and selected protocol
153+
*
154+
* @param {string|string[]} protocols - intended protocol for the stream
155+
* @returns {Promise<{stream: MuxedStream, protocol: string}>} with muxed+multistream-selected stream and selected protocol
128156
*/
129-
newStream(protocols: string[]): Promise<{
130-
stream: any;
157+
newStream(protocols: string | string[]): Promise<{
158+
stream: MuxedStream;
131159
protocol: string;
132160
}>;
133161
/**
134162
* Add a stream when it is opened to the registry.
135-
* @param {*} muxedStream a muxed stream
136-
* @param {object} properties the stream properties to be registered
137-
* @param {string} properties.protocol the protocol used by the stream
138-
* @param {object} properties.metadata metadata of the stream
139-
* @return {void}
163+
*
164+
* @param {MuxedStream} muxedStream - a muxed stream
165+
* @param {object} properties - the stream properties to be registered
166+
* @param {string} properties.protocol - the protocol used by the stream
167+
* @param {object} properties.metadata - metadata of the stream
168+
* @returns {void}
140169
*/
141-
addStream(muxedStream: any, { protocol, metadata }: {
170+
addStream(muxedStream: MuxedStream, { protocol, metadata }: {
142171
protocol: string;
143172
metadata: object;
144173
}): void;
145174
/**
146175
* Remove stream registry after it is closed.
147-
* @param {string} id identifier of the stream
176+
*
177+
* @param {string} id - identifier of the stream
148178
*/
149179
removeStream(id: string): void;
150180
/**
151181
* Close the connection.
152-
* @return {Promise<void>}
182+
*
183+
* @returns {Promise<void>}
153184
*/
154185
close(): Promise<void>;
155-
_closing: any;
156-
get [connectionSymbol](): boolean;
186+
_closing: void | undefined;
187+
}
188+
declare namespace Connection {
189+
export { MuxedStream, ConectionStat, ConnectionOptions };
157190
}
158-
import multiaddr = require("multiaddr");
159-
import PeerId = require("peer-id");
160-
declare const connectionSymbol: unique symbol;
191+
type MuxedStream = {
192+
close: () => void;
193+
abort: () => void;
194+
reset: () => void;
195+
sink: (source: Uint8Array) => Promise<Uint8Array>;
196+
source: () => AsyncIterable<Uint8Array>;
197+
timeline: import("../stream-muxer/types").MuxedTimeline;
198+
id: string;
199+
};
200+
type ConnectionOptions = {
201+
/**
202+
* - local multiaddr of the connection if known.
203+
*/
204+
localAddr?: import("multiaddr") | undefined;
205+
/**
206+
* - remote multiaddr of the connection.
207+
*/
208+
remoteAddr: import("multiaddr");
209+
/**
210+
* - local peer-id.
211+
*/
212+
localPeer: import("peer-id");
213+
/**
214+
* - remote peer-id.
215+
*/
216+
remotePeer: import("peer-id");
217+
/**
218+
* - new stream muxer function.
219+
*/
220+
newStream: (protocols: string | string[]) => Promise<{
221+
stream: MuxedStream;
222+
protocol: string;
223+
}>;
224+
/**
225+
* - close raw connection function.
226+
*/
227+
close: () => Promise<void>;
228+
/**
229+
* - get streams from muxer function.
230+
*/
231+
getStreams: () => MuxedStream[];
232+
/**
233+
* - metadata of the connection.
234+
*/
235+
stat: ConectionStat;
236+
};
237+
type ConectionStat = {
238+
/**
239+
* - connection establishment direction ("inbound" or "outbound").
240+
*/
241+
direction: string;
242+
/**
243+
* - connection relevant events timestamp.
244+
*/
245+
timeline: {
246+
open: number;
247+
upgraded: number | undefined;
248+
close: number | undefined;
249+
};
250+
/**
251+
* - connection multiplexing identifier.
252+
*/
253+
multiplexer?: string | undefined;
254+
/**
255+
* - connection encryption method identifier.
256+
*/
257+
encryption?: string | undefined;
258+
};

0 commit comments

Comments
 (0)