1
1
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
+ */
2
25
/**
3
26
* An implementation of the js-libp2p connection.
4
27
* Any libp2p transport should use an upgrader to return this connection.
@@ -12,149 +35,224 @@ declare class Connection {
12
35
*/
13
36
static isConnection ( other : any ) : other is Connection ;
14
37
/**
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 ) ;
50
45
/**
51
46
* Connection identifier.
52
47
*/
53
48
id : string ;
54
49
/**
55
50
* Observed multiaddr of the local peer
56
51
*/
57
- localAddr : multiaddr | undefined ;
52
+ localAddr : import ( " multiaddr" ) | undefined ;
58
53
/**
59
54
* Observed multiaddr of the remote peer
60
55
*/
61
- remoteAddr : multiaddr | undefined ;
56
+ remoteAddr : import ( " multiaddr" ) ;
62
57
/**
63
58
* Local peer id.
64
59
*/
65
- localPeer : PeerId ;
60
+ localPeer : import ( "peer-id" ) ;
66
61
/**
67
62
* Remote peer id.
68
63
*/
69
- remotePeer : PeerId ;
64
+ remotePeer : import ( "peer-id" ) ;
70
65
/**
71
66
* Connection metadata.
72
67
*/
73
68
_stat : {
74
69
status : "open" ;
70
+ /**
71
+ * - connection establishment direction ("inbound" or "outbound").
72
+ */
75
73
direction : string ;
74
+ /**
75
+ * - connection relevant events timestamp.
76
+ */
76
77
timeline : {
77
- open : string ;
78
- upgraded : string ;
78
+ open : number ;
79
+ upgraded : number | undefined ;
80
+ close : number | undefined ;
79
81
} ;
82
+ /**
83
+ * - connection multiplexing identifier.
84
+ */
80
85
multiplexer ?: string | undefined ;
86
+ /**
87
+ * - connection encryption method identifier.
88
+ */
81
89
encryption ?: string | undefined ;
82
90
} ;
83
91
/**
84
92
* Reference to the new stream function of the multiplexer
85
93
*/
86
- _newStream : Function ;
94
+ _newStream : ( protocols : string | string [ ] ) => Promise < {
95
+ stream : MuxedStream ;
96
+ protocol : string ;
97
+ } > ;
87
98
/**
88
99
* Reference to the close function of the raw connection
89
100
*/
90
- _close : Function ;
101
+ _close : ( ) => Promise < void > ;
91
102
/**
92
103
* Reference to the getStreams function of the muxer
93
104
*/
94
- _getStreams : ( ) => any [ ] ;
105
+ _getStreams : ( ) => MuxedStream [ ] ;
95
106
/**
96
107
* Connection streams registry
97
108
*/
98
109
registry : Map < any , any > ;
99
110
/**
100
111
* User provided tags
112
+ *
101
113
* @type {string[] }
102
114
*/
103
115
tags : string [ ] ;
104
116
get [ Symbol . toStringTag ] ( ) : string ;
105
117
/**
106
118
* Get connection metadata
119
+ *
107
120
* @this {Connection}
108
121
*/
109
122
get stat ( ) : {
110
123
status : "open" ;
124
+ /**
125
+ * - connection establishment direction ("inbound" or "outbound").
126
+ */
111
127
direction : string ;
128
+ /**
129
+ * - connection relevant events timestamp.
130
+ */
112
131
timeline : {
113
- open : string ;
114
- upgraded : string ;
132
+ open : number ;
133
+ upgraded : number | undefined ;
134
+ close : number | undefined ;
115
135
} ;
136
+ /**
137
+ * - connection multiplexing identifier.
138
+ */
116
139
multiplexer ?: string | undefined ;
140
+ /**
141
+ * - connection encryption method identifier.
142
+ */
117
143
encryption ?: string | undefined ;
118
144
} ;
119
145
/**
120
146
* Get all the streams of the muxer.
147
+ *
121
148
* @this {Connection}
122
149
*/
123
- get streams ( ) : any [ ] ;
150
+ get streams ( ) : import ( "../stream-muxer/types" ) . MuxedStream [ ] ;
124
151
/**
125
152
* 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
128
156
*/
129
- newStream ( protocols : string [ ] ) : Promise < {
130
- stream : any ;
157
+ newStream ( protocols : string | string [ ] ) : Promise < {
158
+ stream : MuxedStream ;
131
159
protocol : string ;
132
160
} > ;
133
161
/**
134
162
* 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 }
140
169
*/
141
- addStream ( muxedStream : any , { protocol, metadata } : {
170
+ addStream ( muxedStream : MuxedStream , { protocol, metadata } : {
142
171
protocol : string ;
143
172
metadata : object ;
144
173
} ) : void ;
145
174
/**
146
175
* Remove stream registry after it is closed.
147
- * @param {string } id identifier of the stream
176
+ *
177
+ * @param {string } id - identifier of the stream
148
178
*/
149
179
removeStream ( id : string ) : void ;
150
180
/**
151
181
* Close the connection.
152
- * @return {Promise<void> }
182
+ *
183
+ * @returns {Promise<void> }
153
184
*/
154
185
close ( ) : Promise < void > ;
155
- _closing : any ;
156
- get [ connectionSymbol ] ( ) : boolean ;
186
+ _closing : void | undefined ;
187
+ }
188
+ declare namespace Connection {
189
+ export { MuxedStream , ConectionStat , ConnectionOptions } ;
157
190
}
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