Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should fail if crypto fails', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
const muxers = new Map([[Muxer.multicodec, Muxer]])
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const crypto = {
tag: '/insecure',
secureInbound: () => { throw new Error('Boom') },
secureOutbound: () => { throw new Error('Boom') }
}
const cryptos = new Map([[crypto.tag, crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
// Wait for the results of each side of the connection
const results = await pSettle([
localUpgrader.upgradeOutbound(outbound),
// Pipe all data through the muxer
pipe(maConn, muxer, maConn)
maConn.timeline.upgraded = Date.now()
// Create the connection
const connection = new Connection({
localAddr: maConn.localAddr,
remoteAddr: maConn.remoteAddr,
localPeer: localPeer,
remotePeer: remotePeer,
stat: {
direction,
timeline: maConn.timeline,
multiplexer: Muxer.multicodec,
encryption: 'N/A'
},
newStream,
getStreams: () => muxer.streams,
close: err => maConn.close(err)
})
return connection
}
setTimeout(() => {
const peerB = switchA._peerBook.get(switchB._peerInfo.id.toB58String())
const peerA = switchB._peerBook.get(switchA._peerInfo.id.toB58String())
expect(Array.from(peerB.protocols)).to.eql([
multiplex.multicodec,
identify.multicodec
])
expect(Array.from(peerA.protocols)).to.eql([
multiplex.multicodec,
identify.multicodec,
'/id-test/1.0.0'
])
done()
}, 500)
})
setTimeout(() => {
const peerB = switchA._peerBook.get(switchB._peerInfo.id.toB58String())
const peerA = switchB._peerBook.get(switchA._peerInfo.id.toB58String())
expect(Array.from(peerB.protocols)).to.eql([
multiplex.multicodec,
identify.multicodec
])
expect(Array.from(peerA.protocols)).to.eql([
multiplex.multicodec,
identify.multicodec,
'/id-test/1.0.0'
])
done()
}, 500)
})
const listener = direct.createListener({ config: {} }, (conn) => {
console.log('[listener] Got connection')
const muxer = mplex.listener(conn)
muxer.on('stream', (stream) => {
console.log('[listener] Got stream')
pull(
stream,
pull.drain((data) => {
console.log('[listener] Received:')
console.log(data.toString())
})
)
})
})
direct.dial(maddr, { config: {} }, (err, conn) => {
if (err) {
console.log(`[dialer] Failed to open connection: ${err}`)
}
console.log('[dialer] Opened connection')
const muxer = mplex.dialer(conn)
const stream = muxer.newStream((err) => {
console.log('[dialer] Opened stream')
if (err) throw err
})
pull(
pull.values(['hey, how is it going. I am the dialer']),
stream
)
})
}
const listener = tcp.createServer((socket) => {
let muxer = mplex.listener(toPull.duplex(socket))
muxer.on('stream', (stream) => {
readWrite(stream)
})
for (let i = 0; i < 100; i++) {
muxer.newStream((err, stream) => {
if (err) {
throw err
}
readWrite(stream)
})
}
socket.on('close', () => {
listener.close()
})
})