chore: config types and dependencies update (#904)

BREAKING CHANGES:

top level types were updated, multiaddr@9.0.0 is used, dialer and keychain internal property names changed and connectionManager minPeers is not supported anymore
This commit is contained in:
Vasco Santos
2021-04-15 09:40:02 +02:00
committed by GitHub
parent 8e1743cac4
commit 8506414ea1
114 changed files with 4600 additions and 828 deletions

View File

@ -5,10 +5,12 @@ const log = Object.assign(debug('libp2p:upgrader'), {
error: debug('libp2p:upgrader:err')
})
const errCode = require('err-code')
// @ts-ignore multistream-select does not export types
const Multistream = require('multistream-select')
const { Connection } = require('libp2p-interfaces/src/connection')
const PeerId = require('peer-id')
const { pipe } = require('it-pipe')
// @ts-ignore mutable-proxy does not export types
const mutableProxy = require('mutable-proxy')
const { codes } = require('./errors')
@ -19,7 +21,8 @@ const { codes } = require('./errors')
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').Muxer} Muxer
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
* @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/
/**
@ -36,8 +39,8 @@ class Upgrader {
* @param {import('./metrics')} [options.metrics]
* @param {Map<string, Crypto>} [options.cryptos]
* @param {Map<string, MuxerFactory>} [options.muxers]
* @param {(Connection) => void} options.onConnection - Called when a connection is upgraded
* @param {(Connection) => void} options.onConnectionEnd
* @param {(connection: Connection) => void} options.onConnection - Called when a connection is upgraded
* @param {(connection: Connection) => void} options.onConnectionEnd
*/
constructor ({
localPeer,
@ -51,6 +54,7 @@ class Upgrader {
this.metrics = metrics
this.cryptos = cryptos
this.muxers = muxers
/** @type {import("./pnet") | null} */
this.protector = null
this.protocols = new Map()
this.onConnection = onConnection
@ -216,16 +220,18 @@ class Upgrader {
Muxer,
remotePeer
}) {
/** @type {import("libp2p-interfaces/src/stream-muxer/types").Muxer} */
let muxer
let newStream
// eslint-disable-next-line prefer-const
let connection
/** @type {Connection} */
let connection // eslint-disable-line prefer-const
if (Muxer) {
// Create the muxer
muxer = new Muxer({
// Run anytime a remote stream is created
onStream: async muxedStream => {
if (!connection) return
const mss = new Multistream.Listener(muxedStream)
try {
const { stream, protocol } = await mss.handle(Array.from(this.protocols.keys()))
@ -243,7 +249,7 @@ class Upgrader {
}
})
newStream = async protocols => {
newStream = async (/** @type {string | string[]} */ protocols) => {
log('%s: starting new stream on %s', direction, protocols)
const muxedStream = muxer.newStream()
const mss = new Multistream.Dialer(muxedStream)
@ -302,12 +308,12 @@ class Upgrader {
encryption: cryptoProtocol
},
newStream: newStream || errConnectionNotMultiplexed,
getStreams: () => muxer ? muxer.streams : errConnectionNotMultiplexed,
close: async (err) => {
getStreams: () => muxer ? muxer.streams : errConnectionNotMultiplexed(),
close: async (/** @type {Error | undefined} */ err) => {
await maConn.close(err)
// Ensure remaining streams are aborted
if (muxer) {
muxer.streams.map(stream => stream.abort(err))
muxer.streams.map(stream => stream.abort())
}
}
})
@ -371,7 +377,7 @@ class Upgrader {
* @private
* @async
* @param {PeerId} localPeer - The initiators PeerId
* @param {*} connection
* @param {MultiaddrConnection} connection
* @param {PeerId} remotePeerId
* @param {Map<string, Crypto>} cryptos
* @returns {Promise<CryptoResult>} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used