@@ -64,7 +64,7 @@ class Upgrader {
64
64
async upgradeInbound ( maConn ) {
65
65
let encryptedConn
66
66
let remotePeer
67
- let muxedConnection
67
+ let upgradedConn
68
68
let Muxer
69
69
let cryptoProtocol
70
70
let setPeer
@@ -94,7 +94,11 @@ class Upgrader {
94
94
} = await this . _encryptInbound ( this . localPeer , protectedConn , this . cryptos ) )
95
95
96
96
// Multiplex the connection
97
- ; ( { stream : muxedConnection , Muxer } = await this . _multiplexInbound ( encryptedConn , this . muxers ) )
97
+ if ( this . muxers . size ) {
98
+ ( { stream : upgradedConn , Muxer } = await this . _multiplexInbound ( encryptedConn , this . muxers ) )
99
+ } else {
100
+ upgradedConn = encryptedConn
101
+ }
98
102
} catch ( err ) {
99
103
log . error ( 'Failed to upgrade inbound connection' , err )
100
104
await maConn . close ( err )
@@ -113,7 +117,7 @@ class Upgrader {
113
117
cryptoProtocol,
114
118
direction : 'inbound' ,
115
119
maConn,
116
- muxedConnection ,
120
+ upgradedConn ,
117
121
Muxer,
118
122
remotePeer
119
123
} )
@@ -135,7 +139,7 @@ class Upgrader {
135
139
136
140
let encryptedConn
137
141
let remotePeer
138
- let muxedConnection
142
+ let upgradedConn
139
143
let cryptoProtocol
140
144
let Muxer
141
145
let setPeer
@@ -165,7 +169,11 @@ class Upgrader {
165
169
} = await this . _encryptOutbound ( this . localPeer , protectedConn , remotePeerId , this . cryptos ) )
166
170
167
171
// Multiplex the connection
168
- ; ( { stream : muxedConnection , Muxer } = await this . _multiplexOutbound ( encryptedConn , this . muxers ) )
172
+ if ( this . muxers . size ) {
173
+ ( { stream : upgradedConn , Muxer } = await this . _multiplexOutbound ( encryptedConn , this . muxers ) )
174
+ } else {
175
+ upgradedConn = encryptedConn
176
+ }
169
177
} catch ( err ) {
170
178
log . error ( 'Failed to upgrade outbound connection' , err )
171
179
await maConn . close ( err )
@@ -183,7 +191,7 @@ class Upgrader {
183
191
cryptoProtocol,
184
192
direction : 'outbound' ,
185
193
maConn,
186
- muxedConnection ,
194
+ upgradedConn ,
187
195
Muxer,
188
196
remotePeer
189
197
} )
@@ -196,7 +204,7 @@ class Upgrader {
196
204
* @param {string } cryptoProtocol The crypto protocol that was negotiated
197
205
* @param {string } direction One of ['inbound', 'outbound']
198
206
* @param {MultiaddrConnection } maConn The transport layer connection
199
- * @param {* } muxedConnection A duplex connection returned from multiplexer selection
207
+ * @param {* } upgradedConn A duplex connection returned from multiplexer selection
200
208
* @param {Muxer } Muxer The muxer to be used for muxing
201
209
* @param {PeerId } remotePeer The peer the connection is with
202
210
* @returns {Connection }
@@ -205,10 +213,34 @@ class Upgrader {
205
213
cryptoProtocol,
206
214
direction,
207
215
maConn,
208
- muxedConnection ,
216
+ upgradedConn ,
209
217
Muxer,
210
218
remotePeer
211
219
} ) {
220
+ if ( ! Muxer ) {
221
+ // Create the connection
222
+ maConn . timeline . upgraded = Date . now ( )
223
+
224
+ const connection = new Connection ( {
225
+ localAddr : maConn . localAddr ,
226
+ remoteAddr : maConn . remoteAddr ,
227
+ localPeer : this . localPeer ,
228
+ remotePeer : remotePeer ,
229
+ stat : {
230
+ direction,
231
+ timeline : maConn . timeline ,
232
+ encryption : cryptoProtocol
233
+ } ,
234
+ newStream : ( ) => { throw new Error ( 'connection is not multiplexed' ) } ,
235
+ getStreams : ( ) => { throw new Error ( 'connection is not multiplexed' ) } ,
236
+ close : err => maConn . close ( err )
237
+ } )
238
+
239
+ this . onConnection ( connection )
240
+
241
+ return connection
242
+ }
243
+
212
244
// Create the muxer
213
245
const muxer = new Muxer ( {
214
246
// Run anytime a remote stream is created
@@ -245,7 +277,7 @@ class Upgrader {
245
277
}
246
278
247
279
// Pipe all data through the muxer
248
- pipe ( muxedConnection , muxer , muxedConnection )
280
+ pipe ( upgradedConn , muxer , upgradedConn )
249
281
250
282
maConn . timeline . upgraded = Date . now ( )
251
283
const timelineProxy = new Proxy ( maConn . timeline , {
0 commit comments