fix: deduplicate muxers on the same stream
opening multiple channels on multiple muxers causes channels to either close or reset, see [here](https://gist.github.com/Nazeh/a3f1d24b597913303afcb6c568f4b042)
This commit is contained in:
parent
67b29c4a46
commit
844eac028d
3
index.js
3
index.js
|
@ -243,6 +243,9 @@ class Channel {
|
|||
|
||||
module.exports = class Protomux {
|
||||
constructor (stream, { alloc } = {}) {
|
||||
if (stream.protomux) return stream.protomux
|
||||
stream.protomux = this
|
||||
|
||||
this.isProtomux = true
|
||||
this.stream = stream
|
||||
this.corked = 0
|
||||
|
|
46
test.js
46
test.js
|
@ -265,6 +265,52 @@ test('pipeline close and rejections', function (t) {
|
|||
}
|
||||
})
|
||||
|
||||
test('deduplicate muxers', function (t) {
|
||||
const sa = new SecretStream(true)
|
||||
const sb = new SecretStream(false)
|
||||
|
||||
replicate({ stream: sa }, { stream: sb })
|
||||
|
||||
const a = new Protomux(sa)
|
||||
const foo = a.createChannel({
|
||||
protocol: 'foo',
|
||||
onopen () { t.pass('a remote opened') }
|
||||
})
|
||||
|
||||
foo.open()
|
||||
|
||||
foo.addMessage({
|
||||
encoding: c.string,
|
||||
onmessage (message) { t.is(message, 'hello foo') }
|
||||
})
|
||||
|
||||
const bfoo = new Protomux(sb).createChannel({ protocol: 'foo' })
|
||||
|
||||
// Another Protomux instance for another protocol
|
||||
const a2 = new Protomux(sa)
|
||||
const bar = a2.createChannel({
|
||||
protocol: 'bar',
|
||||
onopen () { t.pass('a remote opened') }
|
||||
})
|
||||
|
||||
bar.open()
|
||||
|
||||
bar.addMessage({
|
||||
encoding: c.string,
|
||||
onmessage (message) { t.is(message, 'hello bar') }
|
||||
})
|
||||
|
||||
const bbar = new Protomux(sb).createChannel({ protocol: 'bar' })
|
||||
|
||||
t.plan(4)
|
||||
|
||||
bfoo.open()
|
||||
bfoo.addMessage({ encoding: c.string }).send('hello foo')
|
||||
|
||||
bbar.open()
|
||||
bbar.addMessage({ encoding: c.string }).send('hello bar')
|
||||
})
|
||||
|
||||
function replicate (a, b) {
|
||||
a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream)
|
||||
}
|
||||
|
|
Reference in New Issue