mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-14 17:51:22 +00:00
fix: expose the muxed stream interface on inbound streams
This commit is contained in:
@ -232,7 +232,7 @@ class Upgrader {
|
|||||||
log('%s: incoming stream opened on %s', direction, protocol)
|
log('%s: incoming stream opened on %s', direction, protocol)
|
||||||
if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol })
|
if (this.metrics) this.metrics.trackStream({ stream, remotePeer, protocol })
|
||||||
connection.addStream(muxedStream, { protocol })
|
connection.addStream(muxedStream, { protocol })
|
||||||
this._onStream({ connection, stream, protocol })
|
this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error(err)
|
log.error(err)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
const { Buffer } = require('buffer')
|
const { Buffer } = require('buffer')
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
|
chai.use(require('chai-as-promised'))
|
||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const Muxer = require('libp2p-mplex')
|
const Muxer = require('libp2p-mplex')
|
||||||
@ -401,6 +402,40 @@ describe('libp2p.upgrader', () => {
|
|||||||
expect(libp2p.upgrader.protocols.get('/echo/1.0.1')).to.equal(echoHandler)
|
expect(libp2p.upgrader.protocols.get('/echo/1.0.1')).to.equal(echoHandler)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return muxed streams', async () => {
|
||||||
|
const remotePeer = peers[1]
|
||||||
|
libp2p = new Libp2p({
|
||||||
|
peerId: peers[0],
|
||||||
|
modules: {
|
||||||
|
transport: [Transport],
|
||||||
|
streamMuxer: [Muxer],
|
||||||
|
connEncryption: [Crypto]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const echoHandler = () => {}
|
||||||
|
libp2p.handle(['/echo/1.0.0'], echoHandler)
|
||||||
|
|
||||||
|
const remoteUpgrader = new Upgrader({
|
||||||
|
localPeer: remotePeer,
|
||||||
|
muxers: new Map([[Muxer.multicodec, Muxer]]),
|
||||||
|
cryptos: new Map([[Crypto.protocol, Crypto]])
|
||||||
|
})
|
||||||
|
remoteUpgrader.protocols.set('/echo/1.0.0', echoHandler)
|
||||||
|
|
||||||
|
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
|
||||||
|
const [localConnection] = await Promise.all([
|
||||||
|
libp2p.upgrader.upgradeOutbound(outbound),
|
||||||
|
remoteUpgrader.upgradeInbound(inbound)
|
||||||
|
])
|
||||||
|
sinon.spy(remoteUpgrader, '_onStream')
|
||||||
|
|
||||||
|
const { stream } = await localConnection.newStream(['/echo/1.0.0'])
|
||||||
|
expect(stream).to.include.keys(['id', 'close', 'reset', 'timeline'])
|
||||||
|
|
||||||
|
const [arg0] = remoteUpgrader._onStream.getCall(0).args
|
||||||
|
expect(arg0.stream).to.include.keys(['id', 'close', 'reset', 'timeline'])
|
||||||
|
})
|
||||||
|
|
||||||
it('should emit connect and disconnect events', async () => {
|
it('should emit connect and disconnect events', async () => {
|
||||||
const remotePeer = peers[1]
|
const remotePeer = peers[1]
|
||||||
libp2p = new Libp2p({
|
libp2p = new Libp2p({
|
||||||
|
Reference in New Issue
Block a user