Skip to content

Commit 8d7c110

Browse files
committed
fix: upgrader should not need muxers
1 parent a39889c commit 8d7c110

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

src/upgrader.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Upgrader {
6464
async upgradeInbound (maConn) {
6565
let encryptedConn
6666
let remotePeer
67-
let muxedConnection
67+
let upgradedConn
6868
let Muxer
6969
let cryptoProtocol
7070
let setPeer
@@ -94,7 +94,11 @@ class Upgrader {
9494
} = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))
9595

9696
// 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+
}
98102
} catch (err) {
99103
log.error('Failed to upgrade inbound connection', err)
100104
await maConn.close(err)
@@ -113,7 +117,7 @@ class Upgrader {
113117
cryptoProtocol,
114118
direction: 'inbound',
115119
maConn,
116-
muxedConnection,
120+
upgradedConn,
117121
Muxer,
118122
remotePeer
119123
})
@@ -135,7 +139,7 @@ class Upgrader {
135139

136140
let encryptedConn
137141
let remotePeer
138-
let muxedConnection
142+
let upgradedConn
139143
let cryptoProtocol
140144
let Muxer
141145
let setPeer
@@ -165,7 +169,11 @@ class Upgrader {
165169
} = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))
166170

167171
// 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+
}
169177
} catch (err) {
170178
log.error('Failed to upgrade outbound connection', err)
171179
await maConn.close(err)
@@ -183,7 +191,7 @@ class Upgrader {
183191
cryptoProtocol,
184192
direction: 'outbound',
185193
maConn,
186-
muxedConnection,
194+
upgradedConn,
187195
Muxer,
188196
remotePeer
189197
})
@@ -196,7 +204,7 @@ class Upgrader {
196204
* @param {string} cryptoProtocol The crypto protocol that was negotiated
197205
* @param {string} direction One of ['inbound', 'outbound']
198206
* @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
200208
* @param {Muxer} Muxer The muxer to be used for muxing
201209
* @param {PeerId} remotePeer The peer the connection is with
202210
* @returns {Connection}
@@ -205,10 +213,34 @@ class Upgrader {
205213
cryptoProtocol,
206214
direction,
207215
maConn,
208-
muxedConnection,
216+
upgradedConn,
209217
Muxer,
210218
remotePeer
211219
}) {
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+
212244
// Create the muxer
213245
const muxer = new Muxer({
214246
// Run anytime a remote stream is created
@@ -245,7 +277,7 @@ class Upgrader {
245277
}
246278

247279
// Pipe all data through the muxer
248-
pipe(muxedConnection, muxer, muxedConnection)
280+
pipe(upgradedConn, muxer, upgradedConn)
249281

250282
maConn.timeline.upgraded = Date.now()
251283
const timelineProxy = new Proxy(maConn.timeline, {

test/upgrading/upgrader.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ describe('Upgrader', () => {
116116
expect(result).to.eql([hello])
117117
})
118118

119+
it('should upgrade with only crypto', async () => {
120+
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
121+
122+
// No available muxers
123+
const muxers = new Map()
124+
sinon.stub(localUpgrader, 'muxers').value(muxers)
125+
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
126+
127+
const cryptos = new Map([[Crypto.protocol, Crypto]])
128+
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
129+
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
130+
131+
const connections = await Promise.all([
132+
localUpgrader.upgradeOutbound(outbound),
133+
remoteUpgrader.upgradeInbound(inbound)
134+
])
135+
136+
expect(connections).to.have.length(2)
137+
})
138+
119139
it('should use a private connection protector when provided', async () => {
120140
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
121141

0 commit comments

Comments
 (0)