@@ -4,7 +4,6 @@ const PeerId = require('peer-id')
4
4
const multiaddr = require ( 'multiaddr' )
5
5
6
6
const withIs = require ( 'class-is' )
7
- const Stream = require ( './stream' )
8
7
9
8
const assert = require ( 'assert' )
10
9
const errCode = require ( 'err-code' )
@@ -23,6 +22,7 @@ class Connection {
23
22
* @param {PeerId } properties.remotePeer remote peer-id.
24
23
* @param {function } properties.newStream new stream muxer function.
25
24
* @param {function } properties.close close raw connection function.
25
+ * @param {function } properties.getStreams get streams from muxer function.
26
26
* @param {Object } properties.stat metadata of the connection.
27
27
* @param {string } properties.stat.direction connection establishment direction ("inbound" or "outbound").
28
28
* @param {Object } properties.stat.timeline connection relevant events timestamp.
@@ -31,13 +31,14 @@ class Connection {
31
31
* @param {string } [properties.stat.multiplexer] connection multiplexing identifier.
32
32
* @param {string } [properties.stat.encryption] connection encryption method identifier.
33
33
*/
34
- constructor ( { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, stat } ) {
34
+ constructor ( { localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams , stat } ) {
35
35
assert ( multiaddr . isMultiaddr ( localAddr ) , 'localAddr must be an instance of multiaddr' )
36
36
assert ( multiaddr . isMultiaddr ( remoteAddr ) , 'remoteAddr must be an instance of multiaddr' )
37
37
assert ( PeerId . isPeerId ( localPeer ) , 'localPeer must be an instance of peer-id' )
38
38
assert ( PeerId . isPeerId ( remotePeer ) , 'remotePeer must be an instance of peer-id' )
39
39
assert ( typeof newStream === 'function' , 'new stream must be a function' )
40
40
assert ( typeof close === 'function' , 'close must be a function' )
41
+ assert ( typeof getStreams === 'function' , 'getStreams must be a function' )
41
42
assert ( stat , 'connection metadata object must be provided' )
42
43
assert ( stat . direction === 'inbound' || stat . direction === 'outbound' , 'direction must be "inbound" or "outbound"' )
43
44
assert ( stat . timeline , 'connection timeline object must be provided in the stat object' )
@@ -92,9 +93,14 @@ class Connection {
92
93
this . _close = close
93
94
94
95
/**
95
- * Connection streams
96
+ * Reference to the getStreams function of the muxer
96
97
*/
97
- this . _streams = [ ]
98
+ this . _getStreams = getStreams
99
+
100
+ /**
101
+ * Connection streams registry
102
+ */
103
+ this . streamRegistry = new Map ( )
98
104
99
105
/**
100
106
* User provided tags
@@ -111,17 +117,24 @@ class Connection {
111
117
}
112
118
113
119
/**
114
- * Get all the streams associated with the connection.
115
- * @return {Array<Stream> }
120
+ * Get all the streams of the muxer.
121
+ * @return {Array<*> }
122
+ */
123
+ get streams ( ) {
124
+ return this . _getStreams ( )
125
+ }
126
+
127
+ /**
128
+ * Get stream registry map with stream metadata indexed by id.
116
129
*/
117
- getStreams ( ) {
118
- return this . _streams
130
+ get registry ( ) {
131
+ return this . streamRegistry
119
132
}
120
133
121
134
/**
122
135
* Create a new stream from this connection
123
136
* @param {string[] } protocols intended protocol for the stream
124
- * @return {Stream } new muxed+multistream-selected stream
137
+ * @return {* } new muxed+multistream-selected stream
125
138
*/
126
139
async newStream ( protocols ) {
127
140
if ( this . stat . status === 'closing' ) {
@@ -134,40 +147,37 @@ class Connection {
134
147
135
148
if ( ! Array . isArray ( protocols ) ) protocols = [ protocols ]
136
149
137
- const { stream : duplexStream , protocol } = await this . _newStream ( protocols )
150
+ const { stream : muxedStream , protocol } = await this . _newStream ( protocols )
138
151
139
- return this . addStream ( {
140
- stream : duplexStream ,
141
- protocol,
142
- direction : 'outbound'
143
- } )
152
+ this . addStream ( muxedStream , protocol )
153
+ return muxedStream
144
154
}
145
155
146
156
/**
147
- * Add an inbound stream when it is opened.
148
- * @param {object } options
149
- * @param {* } options.stream an Iterable Duplex stream
150
- * @param {string } options.protocol the protocol the stream is using
151
- * @param {string } [options.direction = 'inbound'] stream establishment direction ("inbound" or "outbound")
152
- * @return {Stream } new stream within the connection
157
+ * Add a stream when it is opened to the registry.
158
+ * @param {* } muxedStream a muxed stream
159
+ * @param {string } protocol the protocol the stream is using
160
+ * @param {Array<String> } tags tags of the stream
161
+ * @return {void }
153
162
*/
154
- addStream ( { stream, protocol, direction = 'inbound' } ) {
155
- assert ( direction === 'inbound' || direction === 'outbound' , 'direction must be "inbound" or "outbound"' )
156
-
157
- const s = new Stream ( {
158
- iterableDuplex : stream ,
159
- conn : this ,
160
- direction,
163
+ addStream ( muxedStream , protocol , tags = [ ] ) {
164
+ // Add metadata for the stream
165
+ this . streamRegistry . set ( muxedStream . id , {
166
+ tags,
161
167
protocol
162
168
} )
169
+ }
163
170
164
- this . _streams . push ( s )
165
-
166
- return s
171
+ /**
172
+ * Remove stream registry after it is closed.
173
+ * @param {string } id identifier of the stream
174
+ */
175
+ removeStream ( id ) {
176
+ this . streamRegistry . delete ( id )
167
177
}
168
178
169
179
/**
170
- * Close the connection, as well as all its associated streams .
180
+ * Close the connection.
171
181
* @return {Promise }
172
182
*/
173
183
async close ( ) {
@@ -184,9 +194,6 @@ class Connection {
184
194
// Close raw connection
185
195
this . _closing = await this . _close ( )
186
196
187
- // All streams closed
188
- this . _streams . map ( ( stream ) => stream . close ( ) )
189
-
190
197
this . _stat . timeline . close = Date . now ( )
191
198
this . stat . status = 'closed'
192
199
}
0 commit comments