diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index a35d36f5..93b3c99c 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -1,11 +1,15 @@ 'use strict' -const assert = require('assert') +const errcode = require('err-code') const mergeOptions = require('merge-options') const LatencyMonitor = require('latency-monitor').default const debug = require('debug')('libp2p:connection-manager') const retimer = require('retimer') +const { + ERR_INVALID_PARAMETERS +} = require('../errors') + const defaultOptions = { maxConnections: Infinity, minConnections: 0, @@ -38,10 +42,9 @@ class ConnectionManager { this._registrar = libp2p.registrar this._peerId = libp2p.peerInfo.id.toB58String() this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options) - assert( - this._options.maxConnections > this._options.minConnections, - 'Connection Manager maxConnections must be greater than minConnections' - ) + if (this._options.maxConnections < this._options.minConnections) { + throw errcode(new Error('Connection Manager maxConnections must be greater than minConnections'), ERR_INVALID_PARAMETERS) + } debug('options: %j', this._options) diff --git a/src/errors.js b/src/errors.js index 89ca3439..32b21ff0 100644 --- a/src/errors.js +++ b/src/errors.js @@ -20,6 +20,7 @@ exports.codes = { ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED', ERR_INVALID_KEY: 'ERR_INVALID_KEY', ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE', + ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS', ERR_INVALID_PEER: 'ERR_INVALID_PEER', ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE', ERR_TIMEOUT: 'ERR_TIMEOUT', diff --git a/src/peer-store/index.js b/src/peer-store/index.js index 51ac5991..3fe4954e 100644 --- a/src/peer-store/index.js +++ b/src/peer-store/index.js @@ -1,6 +1,6 @@ 'use strict' -const assert = require('assert') +const errcode = require('err-code') const debug = require('debug') const log = debug('libp2p:peer-store') log.error = debug('libp2p:peer-store:error') @@ -9,6 +9,9 @@ const { EventEmitter } = require('events') const PeerId = require('peer-id') const PeerInfo = require('peer-info') +const { + ERR_INVALID_PARAMETERS +} = require('../errors') /** * Responsible for managing known peers, as well as their addresses and metadata @@ -46,7 +49,9 @@ class PeerStore extends EventEmitter { * @return {PeerInfo} */ put (peerInfo, options = { silent: false }) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } let peer // Already know the peer? @@ -67,7 +72,9 @@ class PeerStore extends EventEmitter { * @return {PeerInfo} */ add (peerInfo) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } // Create new instance and add values to it const newPeerInfo = new PeerInfo(peerInfo.id) @@ -105,7 +112,10 @@ class PeerStore extends EventEmitter { * @return {PeerInfo} */ update (peerInfo) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } + const id = peerInfo.id.toB58String() const recorded = this.peers.get(id) @@ -207,7 +217,9 @@ class PeerStore extends EventEmitter { * @returns {void} */ replace (peerInfo) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } this.remove(peerInfo.id.toB58String()) this.add(peerInfo) diff --git a/src/pnet/index.js b/src/pnet/index.js index 8cd8c334..b1a70ac3 100644 --- a/src/pnet/index.js +++ b/src/pnet/index.js @@ -1,10 +1,13 @@ 'use strict' const pipe = require('it-pipe') -const assert = require('assert') +const errcode = require('err-code') const duplexPair = require('it-pair/duplex') const crypto = require('libp2p-crypto') const Errors = require('./errors') +const { + ERR_INVALID_PARAMETERS +} = require('../errors') const { createBoxStream, createUnboxStream, @@ -40,7 +43,9 @@ class Protector { * @returns {*} A protected duplex iterable */ async protect (connection) { - assert(connection, Errors.NO_HANDSHAKE_CONNECTION) + if (!connection) { + throw errcode(new Error(Errors.NO_HANDSHAKE_CONNECTION), ERR_INVALID_PARAMETERS) + } // Exchange nonces log('protecting the connection') diff --git a/src/registrar.js b/src/registrar.js index 45118c09..cc245485 100644 --- a/src/registrar.js +++ b/src/registrar.js @@ -1,10 +1,13 @@ 'use strict' -const assert = require('assert') const debug = require('debug') +const errcode = require('err-code') const log = debug('libp2p:peer-store') log.error = debug('libp2p:peer-store:error') +const { + ERR_INVALID_PARAMETERS +} = require('./errors') const Topology = require('libp2p-interfaces/src/topology') const { Connection } = require('libp2p-interfaces/src/connection') const PeerInfo = require('peer-info') @@ -71,8 +74,13 @@ class Registrar { * @returns {void} */ onConnect (peerInfo, conn) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') - assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } + + if (!Connection.isConnection(conn)) { + throw errcode(new Error('conn must be an instance of interface-connection'), ERR_INVALID_PARAMETERS) + } const id = peerInfo.id.toB58String() const storedConn = this.connections.get(id) @@ -93,7 +101,9 @@ class Registrar { * @returns {void} */ onDisconnect (peerInfo, connection, error) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } const id = peerInfo.id.toB58String() let storedConn = this.connections.get(id) @@ -116,7 +126,9 @@ class Registrar { * @returns {Connection} */ getConnection (peerInfo) { - assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') + if (!PeerInfo.isPeerInfo(peerInfo)) { + throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + } const connections = this.connections.get(peerInfo.id.toB58String()) // Return the first, open connection @@ -132,9 +144,9 @@ class Registrar { * @return {string} registrar identifier */ register (topology) { - assert( - Topology.isTopology(topology), - 'topology must be an instance of interfaces/topology') + if (!Topology.isTopology(topology)) { + throw errcode(new Error('topology must be an instance of interfaces/topology'), ERR_INVALID_PARAMETERS) + } // Create topology const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()