fix: remove use of assert module (#561)

* fix: remove use of assert module

The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.

* chore: fix linting

* chore: export invalid param code

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
Alex Potsides 2020-02-14 13:42:23 +00:00 committed by GitHub
parent 0882dcea3b
commit a8984c6cd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 20 deletions

View File

@ -1,11 +1,15 @@
'use strict' 'use strict'
const assert = require('assert') const errcode = require('err-code')
const mergeOptions = require('merge-options') const mergeOptions = require('merge-options')
const LatencyMonitor = require('latency-monitor').default const LatencyMonitor = require('latency-monitor').default
const debug = require('debug')('libp2p:connection-manager') const debug = require('debug')('libp2p:connection-manager')
const retimer = require('retimer') const retimer = require('retimer')
const {
ERR_INVALID_PARAMETERS
} = require('../errors')
const defaultOptions = { const defaultOptions = {
maxConnections: Infinity, maxConnections: Infinity,
minConnections: 0, minConnections: 0,
@ -38,10 +42,9 @@ class ConnectionManager {
this._registrar = libp2p.registrar this._registrar = libp2p.registrar
this._peerId = libp2p.peerInfo.id.toB58String() this._peerId = libp2p.peerInfo.id.toB58String()
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options) this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
assert( if (this._options.maxConnections < this._options.minConnections) {
this._options.maxConnections > this._options.minConnections, throw errcode(new Error('Connection Manager maxConnections must be greater than minConnections'), ERR_INVALID_PARAMETERS)
'Connection Manager maxConnections must be greater than minConnections' }
)
debug('options: %j', this._options) debug('options: %j', this._options)

View File

@ -20,6 +20,7 @@ exports.codes = {
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED', ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
ERR_INVALID_KEY: 'ERR_INVALID_KEY', ERR_INVALID_KEY: 'ERR_INVALID_KEY',
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE', ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
ERR_INVALID_PEER: 'ERR_INVALID_PEER', ERR_INVALID_PEER: 'ERR_INVALID_PEER',
ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE', ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
ERR_TIMEOUT: 'ERR_TIMEOUT', ERR_TIMEOUT: 'ERR_TIMEOUT',

View File

@ -1,6 +1,6 @@
'use strict' 'use strict'
const assert = require('assert') const errcode = require('err-code')
const debug = require('debug') const debug = require('debug')
const log = debug('libp2p:peer-store') const log = debug('libp2p:peer-store')
log.error = debug('libp2p:peer-store:error') log.error = debug('libp2p:peer-store:error')
@ -9,6 +9,9 @@ const { EventEmitter } = require('events')
const PeerId = require('peer-id') const PeerId = require('peer-id')
const PeerInfo = require('peer-info') const PeerInfo = require('peer-info')
const {
ERR_INVALID_PARAMETERS
} = require('../errors')
/** /**
* Responsible for managing known peers, as well as their addresses and metadata * Responsible for managing known peers, as well as their addresses and metadata
@ -46,7 +49,9 @@ class PeerStore extends EventEmitter {
* @return {PeerInfo} * @return {PeerInfo}
*/ */
put (peerInfo, options = { silent: false }) { 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 let peer
// Already know the peer? // Already know the peer?
@ -67,7 +72,9 @@ class PeerStore extends EventEmitter {
* @return {PeerInfo} * @return {PeerInfo}
*/ */
add (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 // Create new instance and add values to it
const newPeerInfo = new PeerInfo(peerInfo.id) const newPeerInfo = new PeerInfo(peerInfo.id)
@ -105,7 +112,10 @@ class PeerStore extends EventEmitter {
* @return {PeerInfo} * @return {PeerInfo}
*/ */
update (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 id = peerInfo.id.toB58String()
const recorded = this.peers.get(id) const recorded = this.peers.get(id)
@ -207,7 +217,9 @@ class PeerStore extends EventEmitter {
* @returns {void} * @returns {void}
*/ */
replace (peerInfo) { 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.remove(peerInfo.id.toB58String())
this.add(peerInfo) this.add(peerInfo)

View File

@ -1,10 +1,13 @@
'use strict' 'use strict'
const pipe = require('it-pipe') const pipe = require('it-pipe')
const assert = require('assert') const errcode = require('err-code')
const duplexPair = require('it-pair/duplex') const duplexPair = require('it-pair/duplex')
const crypto = require('libp2p-crypto') const crypto = require('libp2p-crypto')
const Errors = require('./errors') const Errors = require('./errors')
const {
ERR_INVALID_PARAMETERS
} = require('../errors')
const { const {
createBoxStream, createBoxStream,
createUnboxStream, createUnboxStream,
@ -40,7 +43,9 @@ class Protector {
* @returns {*} A protected duplex iterable * @returns {*} A protected duplex iterable
*/ */
async protect (connection) { async protect (connection) {
assert(connection, Errors.NO_HANDSHAKE_CONNECTION) if (!connection) {
throw errcode(new Error(Errors.NO_HANDSHAKE_CONNECTION), ERR_INVALID_PARAMETERS)
}
// Exchange nonces // Exchange nonces
log('protecting the connection') log('protecting the connection')

View File

@ -1,10 +1,13 @@
'use strict' 'use strict'
const assert = require('assert')
const debug = require('debug') const debug = require('debug')
const errcode = require('err-code')
const log = debug('libp2p:peer-store') const log = debug('libp2p:peer-store')
log.error = debug('libp2p:peer-store:error') log.error = debug('libp2p:peer-store:error')
const {
ERR_INVALID_PARAMETERS
} = require('./errors')
const Topology = require('libp2p-interfaces/src/topology') const Topology = require('libp2p-interfaces/src/topology')
const { Connection } = require('libp2p-interfaces/src/connection') const { Connection } = require('libp2p-interfaces/src/connection')
const PeerInfo = require('peer-info') const PeerInfo = require('peer-info')
@ -71,8 +74,13 @@ class Registrar {
* @returns {void} * @returns {void}
*/ */
onConnect (peerInfo, conn) { onConnect (peerInfo, conn) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') if (!PeerInfo.isPeerInfo(peerInfo)) {
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection') 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 id = peerInfo.id.toB58String()
const storedConn = this.connections.get(id) const storedConn = this.connections.get(id)
@ -93,7 +101,9 @@ class Registrar {
* @returns {void} * @returns {void}
*/ */
onDisconnect (peerInfo, connection, error) { 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() const id = peerInfo.id.toB58String()
let storedConn = this.connections.get(id) let storedConn = this.connections.get(id)
@ -116,7 +126,9 @@ class Registrar {
* @returns {Connection} * @returns {Connection}
*/ */
getConnection (peerInfo) { 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()) const connections = this.connections.get(peerInfo.id.toB58String())
// Return the first, open connection // Return the first, open connection
@ -132,9 +144,9 @@ class Registrar {
* @return {string} registrar identifier * @return {string} registrar identifier
*/ */
register (topology) { register (topology) {
assert( if (!Topology.isTopology(topology)) {
Topology.isTopology(topology), throw errcode(new Error('topology must be an instance of interfaces/topology'), ERR_INVALID_PARAMETERS)
'topology must be an instance of interfaces/topology') }
// Create topology // Create topology
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now() const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()