From 302bb9005891aa06b70a5f354bfac6b2d5a3c3b8 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 30 Apr 2021 13:20:12 +0200 Subject: [PATCH] fix: event emitter and interfaces types for discovery and routing (#934) --- package.json | 4 ++-- src/address-manager/index.js | 2 +- src/connection-manager/index.js | 2 +- src/connection-manager/latency-monitor.js | 2 +- src/connection-manager/visibility-change-emitter.js | 2 +- src/content-routing/index.js | 2 +- src/index.js | 12 ++++++------ src/metrics/stats.js | 4 ++-- src/peer-routing.js | 2 +- src/peer-store/index.js | 2 +- src/upgrader.js | 7 ++++++- 11 files changed, 23 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 0d7dd61ebf..7431e5756a 100644 --- a/package.json +++ b/package.json @@ -104,11 +104,11 @@ "it-pipe": "^1.1.0", "it-take": "1.0.0", "libp2p-crypto": "^0.19.4", - "libp2p-interfaces": "^0.10.3", + "libp2p-interfaces": "^0.10.4", "libp2p-utils": "^0.3.1", "mafmt": "^9.0.0", "merge-options": "^3.0.4", - "moving-average": "^1.0.0", + "@vascosantos/moving-average": "^1.1.0", "multiaddr": "^9.0.1", "multicodec": "^3.0.1", "multihashing-async": "^2.1.2", diff --git a/src/address-manager/index.js b/src/address-manager/index.js index bc4024c4be..25de94b61f 100644 --- a/src/address-manager/index.js +++ b/src/address-manager/index.js @@ -1,6 +1,6 @@ 'use strict' -const EventEmitter = require('events') +const { EventEmitter } = require('events') const { Multiaddr } = require('multiaddr') const PeerId = require('peer-id') diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 985d26f145..7a865127b0 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -11,7 +11,7 @@ const LatencyMonitor = require('./latency-monitor') // @ts-ignore retimer does not have types const retimer = require('retimer') -const EventEmitter = require('events') +const { EventEmitter } = require('events') const PeerId = require('peer-id') diff --git a/src/connection-manager/latency-monitor.js b/src/connection-manager/latency-monitor.js index 5253c2ae71..6c3061b91b 100644 --- a/src/connection-manager/latency-monitor.js +++ b/src/connection-manager/latency-monitor.js @@ -5,7 +5,7 @@ * This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE) */ -const EventEmitter = require('events') +const { EventEmitter } = require('events') const VisibilityChangeEmitter = require('./visibility-change-emitter') const debug = require('debug')('latency-monitor:LatencyMonitor') diff --git a/src/connection-manager/visibility-change-emitter.js b/src/connection-manager/visibility-change-emitter.js index 9efb6ffb33..ebe5e7d076 100644 --- a/src/connection-manager/visibility-change-emitter.js +++ b/src/connection-manager/visibility-change-emitter.js @@ -6,7 +6,7 @@ */ 'use strict' -const EventEmitter = require('events') +const { EventEmitter } = require('events') const debug = require('debug')('latency-monitor:VisibilityChangeEmitter') diff --git a/src/content-routing/index.js b/src/content-routing/index.js index 44f1d9c580..00211f71ff 100644 --- a/src/content-routing/index.js +++ b/src/content-routing/index.js @@ -16,7 +16,7 @@ const { pipe } = require('it-pipe') * @typedef {import('peer-id')} PeerId * @typedef {import('multiaddr').Multiaddr} Multiaddr * @typedef {import('cids')} CID - * @typedef {import('libp2p-interfaces/src/content-routing/types')} ContentRoutingModule + * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule */ /** diff --git a/src/index.js b/src/index.js index deccf80aaf..999bcb2685 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const debug = require('debug') const log = Object.assign(debug('libp2p'), { error: debug('libp2p:err') }) -const EventEmitter = require('events') +const { EventEmitter } = require('events') const errCode = require('err-code') const PeerId = require('peer-id') @@ -40,9 +40,9 @@ const { updateSelfPeerRecord } = require('./record/utils') * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream * @typedef {import('libp2p-interfaces/src/transport/types').TransportFactory} TransportFactory * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory - * @typedef {import('libp2p-interfaces/src/content-routing/types')} ContentRoutingModule - * @typedef {import('libp2p-interfaces/src/peer-discovery/types')} PeerDiscoveryModule - * @typedef {import('libp2p-interfaces/src/peer-routing/types')} PeerRoutingModule + * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule + * @typedef {import('libp2p-interfaces/src/peer-discovery/types').PeerDiscoveryFactory} PeerDiscoveryFactory + * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule * @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto * @typedef {import('libp2p-interfaces/src/pubsub')} Pubsub * @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions @@ -100,7 +100,7 @@ const { updateSelfPeerRecord } = require('./record/utils') * @property {TransportFactory[]} transport * @property {MuxerFactory[]} streamMuxer * @property {Crypto[]} connEncryption - * @property {PeerDiscoveryModule[]} [peerDiscovery] + * @property {PeerDiscoveryFactory[]} [peerDiscovery] * @property {PeerRoutingModule[]} [peerRouting] * @property {ContentRoutingModule[]} [contentRouting] * @property {Object} [dht] @@ -714,7 +714,7 @@ class Libp2p extends EventEmitter { */ async _setupPeerDiscovery () { /** - * @param {PeerDiscoveryModule} DiscoveryService + * @param {PeerDiscoveryFactory} DiscoveryService */ const setupService = (DiscoveryService) => { let config = { diff --git a/src/metrics/stats.js b/src/metrics/stats.js index 8d76140031..23056fac2a 100644 --- a/src/metrics/stats.js +++ b/src/metrics/stats.js @@ -1,9 +1,9 @@ // @ts-nocheck 'use strict' -const EventEmitter = require('events') +const { EventEmitter } = require('events') const { BigNumber: Big } = require('bignumber.js') -const MovingAverage = require('moving-average') +const MovingAverage = require('@vascosantos/moving-average') const retimer = require('retimer') class Stats extends EventEmitter { diff --git a/src/peer-routing.js b/src/peer-routing.js index e280936182..0ff6f7b8ce 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -25,7 +25,7 @@ const { /** * @typedef {import('peer-id')} PeerId * @typedef {import('multiaddr').Multiaddr} Multiaddr - * @typedef {import('libp2p-interfaces/src/peer-routing/types')} PeerRoutingModule + * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule */ /** diff --git a/src/peer-store/index.js b/src/peer-store/index.js index 0f2f989f79..b3df1bbb94 100644 --- a/src/peer-store/index.js +++ b/src/peer-store/index.js @@ -2,7 +2,7 @@ const errcode = require('err-code') -const EventEmitter = require('events') +const { EventEmitter } = require('events') const PeerId = require('peer-id') const AddressBook = require('./address-book') diff --git a/src/upgrader.js b/src/upgrader.js index cdf4706c46..8ee6c65412 100644 --- a/src/upgrader.js +++ b/src/upgrader.js @@ -137,7 +137,12 @@ class Upgrader { * @returns {Promise} */ async upgradeOutbound (maConn) { - const remotePeerId = PeerId.createFromB58String(maConn.remoteAddr.getPeerId()) + const idStr = maConn.remoteAddr.getPeerId() + if (!idStr) { + throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR) + } + + const remotePeerId = PeerId.createFromB58String(idStr) let encryptedConn let remotePeer