Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const mss = new Multistream.Dialer(muxedStream)
try {
const { stream, protocol } = await mss.select(protocols)
return { stream: { ...muxedStream, ...stream }, protocol }
} catch (err) {
throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
}
}
// 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
], (err) => {
if (err) {
// we don't return the error here,
// since multistream select don't expect one
callback()
return log(err)
}
const peerInfo = new PeerInfo(this.utils.peerIdFromId(msg.srcPeer.id))
msg.srcPeer.addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
const newConn = new Connection(sh.rest())
newConn.setPeerInfo(peerInfo)
setImmediate(() => this.emit('connection', newConn))
callback(newConn)
})
}
// Pipe all data through the muxer
pipe(muxedConnection, muxer, muxedConnection)
maConn.timeline.upgraded = Date.now()
const timelineProxy = new Proxy(maConn.timeline, {
set: (...args) => {
if (args[1] === 'close' && args[2]) {
this.onConnectionEnd(connection)
}
return Reflect.set(...args)
}
})
// Create the connection
const connection = new Connection({
localAddr: maConn.localAddr,
remoteAddr: maConn.remoteAddr,
localPeer: this.localPeer,
remotePeer: remotePeer,
stat: {
direction,
timeline: timelineProxy,
multiplexer: Muxer.multicodec,
encryption: cryptoProtocol
},
newStream,
getStreams: () => muxer.streams,
close: err => maConn.close(err)
})
this.onConnection(connection)
log('work:cancel')
// clean up already done dials
pull(empty(), conn)
// If we can close the connection, do it
if (typeof conn.close === 'function') {
return conn.close((_) => callback(null))
}
return callback(null)
}
// one is enough
token.cancel = true
log('work:success')
const proxyConn = new Connection()
proxyConn.setInnerConn(conn)
callback(null, { multiaddr: addr, conn: conn })
})
}
setPeerInfo.call(connection, pi)
resolve(pi)
}
})
})
const stream = {
source: pull(
connection,
observer.incoming(transport, protocol, peerInfo)),
sink: pull(
observer.outgoing(transport, protocol, peerInfo),
connection)
}
return new Connection(stream, connection)
}