Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_attemptMuxerUpgrade (connection, b58Id, callback, abort) {
const muxers = Object.keys(this.switch.muxers)
if (muxers.length === 0) {
return callback(new Error('no muxers available'))
}
const msDialer = new multistream.Dialer()
handleSafe(msDialer, connection, (err) => {
if (err) {
return callback(new Error('multistream not supported'))
}
// 1. try to handshake in one of the muxers available
// 2. if succeeds
// - add the muxedConn to the list of muxedConns
// - add incomming new streams to connHandler
const nextMuxer = (key) => {
log('selecting %s', key)
selectSafe(msDialer, key, (err, _conn) => {
if (err) {
if (muxers.length === 0) {
return callback(new Error('could not upgrade to stream muxing'))
}
_performProtocolHandshake (connection, callback) {
// If there is no protocol set yet, don't perform the handshake
if (!this.protocol) {
callback()
}
const msDialer = new multistream.Dialer()
handleSafe(msDialer, connection, (err) => {
if (err) {
return callback(err)
}
selectSafe(msDialer, this.protocol, (err, _conn) => {
if (err) {
log(`could not perform protocol handshake: `, err)
return callback(err)
}
const conn = observeConnection(null, this.protocol, _conn, this.switch.observer)
callback(null, conn)
}, callback)
}, callback)
}
}
_onUpgrading () {
const muxers = Object.keys(this.switch.muxers)
this.log('upgrading connection to %s', this.theirB58Id)
if (muxers.length === 0) {
return this._state('stop')
}
const msDialer = new multistream.Dialer()
msDialer.handle(this.conn, (err) => {
if (err) {
return this._didUpgrade(err)
}
// 1. try to handshake in one of the muxers available
// 2. if succeeds
// - add the muxedConn to the list of muxedConns
// - add incomming new streams to connHandler
const nextMuxer = (key) => {
this.log('selecting %s', key)
msDialer.select(key, (err, _conn) => {
if (err) {
if (muxers.length === 0) {
return this._didUpgrade(err)
}
_protocolHandshake (protocol, connection, callback) {
const msDialer = new multistream.Dialer()
msDialer.handle(connection, (err) => {
if (err) {
return callback(err, null)
}
msDialer.select(protocol, (err, _conn) => {
if (err) {
this.log('could not perform protocol handshake:', err)
return callback(err, null)
}
const conn = observeConnection(null, protocol, _conn, this.switch.observer)
this.log('successfully performed handshake of %s to %s', protocol, this.theirB58Id)
this.emit('connection', conn)
callback(null, conn)
})
async _multiplexOutbound (connection, muxers) {
const dialer = new Multistream.Dialer(connection)
const protocols = Array.from(muxers.keys())
log('outbound selecting muxer %s', protocols)
try {
const { stream, protocol } = await dialer.select(protocols)
log('%s selected as muxer protocol', protocol)
const Muxer = muxers.get(protocol)
return { stream, Muxer }
} catch (err) {
throw errCode(err, codes.ERR_MUXER_UNAVAILABLE)
}
}
conn.getPeerInfo = async (callback) => {
const conn = muxedConn.newStream()
const ms = new multistream.Dialer()
callback = once(callback)
let results
try {
await msHandle(ms, conn)
const msConn = await msSelect(ms, identify.multicodec)
results = await identifyDialer(msConn, cryptoPI)
} catch (err) {
return muxedConn.end(() => {
callback(err, null)
})
}
const { peerInfo } = results
if (peerInfo) {
async function multiplexOutbound (connection, muxers) {
const dialer = new MSS.Dialer(connection)
const protocols = Array.from(muxers.keys())
log('outbound selecting muxer %s', protocols)
const { stream, protocol } = await dialer.select(protocols)
log('%s selected as muxer protocol', protocol)
const Muxer = muxers.get(protocol)
if (stream) return { stream, Muxer }
throw new Error('All muxing failed')
}
conn.getPeerInfo = async (callback) => {
const conn = muxedConn.newStream()
const ms = new multistream.Dialer()
callback = once(callback)
let results
try {
await msHandle(ms, conn)
const msConn = await msSelect(ms, identify.multicodec)
results = await identifyDialer(msConn, cryptoPI)
} catch (err) {
return muxedConn.end(() => {
callback(err, null)
})
}
const { peerInfo } = results
if (peerInfo) {
_encryptConnection (connection, callback) {
const msDialer = new multistream.Dialer()
handleSafe(msDialer, connection, (err) => {
if (err) {
return callback(err)
}
const myId = this.switch._peerInfo.id
log('selecting crypto: %s', this.switch.crypto.tag)
selectSafe(msDialer, this.switch.crypto.tag, (err, _conn) => {
if (err) {
return callback(err)
}
const conn = observeConnection(null, this.switch.crypto.tag, _conn, this.switch.observer)
const encryptedConnection = this.switch.crypto.encrypt(myId, conn, this.peerInfo.id, (err) => {