Skip to content

Commit a8984c6

Browse files
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>
1 parent 0882dce commit a8984c6

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

src/connection-manager/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
'use strict'
22

3-
const assert = require('assert')
3+
const errcode = require('err-code')
44
const mergeOptions = require('merge-options')
55
const LatencyMonitor = require('latency-monitor').default
66
const debug = require('debug')('libp2p:connection-manager')
77
const retimer = require('retimer')
88

9+
const {
10+
ERR_INVALID_PARAMETERS
11+
} = require('../errors')
12+
913
const defaultOptions = {
1014
maxConnections: Infinity,
1115
minConnections: 0,
@@ -38,10 +42,9 @@ class ConnectionManager {
3842
this._registrar = libp2p.registrar
3943
this._peerId = libp2p.peerInfo.id.toB58String()
4044
this._options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, options)
41-
assert(
42-
this._options.maxConnections > this._options.minConnections,
43-
'Connection Manager maxConnections must be greater than minConnections'
44-
)
45+
if (this._options.maxConnections < this._options.minConnections) {
46+
throw errcode(new Error('Connection Manager maxConnections must be greater than minConnections'), ERR_INVALID_PARAMETERS)
47+
}
4548

4649
debug('options: %j', this._options)
4750

src/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports.codes = {
2020
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
2121
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
2222
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
23+
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
2324
ERR_INVALID_PEER: 'ERR_INVALID_PEER',
2425
ERR_MUXER_UNAVAILABLE: 'ERR_MUXER_UNAVAILABLE',
2526
ERR_TIMEOUT: 'ERR_TIMEOUT',

src/peer-store/index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const assert = require('assert')
3+
const errcode = require('err-code')
44
const debug = require('debug')
55
const log = debug('libp2p:peer-store')
66
log.error = debug('libp2p:peer-store:error')
@@ -9,6 +9,9 @@ const { EventEmitter } = require('events')
99

1010
const PeerId = require('peer-id')
1111
const PeerInfo = require('peer-info')
12+
const {
13+
ERR_INVALID_PARAMETERS
14+
} = require('../errors')
1215

1316
/**
1417
* Responsible for managing known peers, as well as their addresses and metadata
@@ -46,7 +49,9 @@ class PeerStore extends EventEmitter {
4649
* @return {PeerInfo}
4750
*/
4851
put (peerInfo, options = { silent: false }) {
49-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
52+
if (!PeerInfo.isPeerInfo(peerInfo)) {
53+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
54+
}
5055

5156
let peer
5257
// Already know the peer?
@@ -67,7 +72,9 @@ class PeerStore extends EventEmitter {
6772
* @return {PeerInfo}
6873
*/
6974
add (peerInfo) {
70-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
75+
if (!PeerInfo.isPeerInfo(peerInfo)) {
76+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
77+
}
7178

7279
// Create new instance and add values to it
7380
const newPeerInfo = new PeerInfo(peerInfo.id)
@@ -105,7 +112,10 @@ class PeerStore extends EventEmitter {
105112
* @return {PeerInfo}
106113
*/
107114
update (peerInfo) {
108-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
115+
if (!PeerInfo.isPeerInfo(peerInfo)) {
116+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
117+
}
118+
109119
const id = peerInfo.id.toB58String()
110120
const recorded = this.peers.get(id)
111121

@@ -207,7 +217,9 @@ class PeerStore extends EventEmitter {
207217
* @returns {void}
208218
*/
209219
replace (peerInfo) {
210-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
220+
if (!PeerInfo.isPeerInfo(peerInfo)) {
221+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
222+
}
211223

212224
this.remove(peerInfo.id.toB58String())
213225
this.add(peerInfo)

src/pnet/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

33
const pipe = require('it-pipe')
4-
const assert = require('assert')
4+
const errcode = require('err-code')
55
const duplexPair = require('it-pair/duplex')
66
const crypto = require('libp2p-crypto')
77
const Errors = require('./errors')
8+
const {
9+
ERR_INVALID_PARAMETERS
10+
} = require('../errors')
811
const {
912
createBoxStream,
1013
createUnboxStream,
@@ -40,7 +43,9 @@ class Protector {
4043
* @returns {*} A protected duplex iterable
4144
*/
4245
async protect (connection) {
43-
assert(connection, Errors.NO_HANDSHAKE_CONNECTION)
46+
if (!connection) {
47+
throw errcode(new Error(Errors.NO_HANDSHAKE_CONNECTION), ERR_INVALID_PARAMETERS)
48+
}
4449

4550
// Exchange nonces
4651
log('protecting the connection')

src/registrar.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

3-
const assert = require('assert')
43
const debug = require('debug')
4+
const errcode = require('err-code')
55
const log = debug('libp2p:peer-store')
66
log.error = debug('libp2p:peer-store:error')
77

8+
const {
9+
ERR_INVALID_PARAMETERS
10+
} = require('./errors')
811
const Topology = require('libp2p-interfaces/src/topology')
912
const { Connection } = require('libp2p-interfaces/src/connection')
1013
const PeerInfo = require('peer-info')
@@ -71,8 +74,13 @@ class Registrar {
7174
* @returns {void}
7275
*/
7376
onConnect (peerInfo, conn) {
74-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
75-
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')
77+
if (!PeerInfo.isPeerInfo(peerInfo)) {
78+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
79+
}
80+
81+
if (!Connection.isConnection(conn)) {
82+
throw errcode(new Error('conn must be an instance of interface-connection'), ERR_INVALID_PARAMETERS)
83+
}
7684

7785
const id = peerInfo.id.toB58String()
7886
const storedConn = this.connections.get(id)
@@ -93,7 +101,9 @@ class Registrar {
93101
* @returns {void}
94102
*/
95103
onDisconnect (peerInfo, connection, error) {
96-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
104+
if (!PeerInfo.isPeerInfo(peerInfo)) {
105+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
106+
}
97107

98108
const id = peerInfo.id.toB58String()
99109
let storedConn = this.connections.get(id)
@@ -116,7 +126,9 @@ class Registrar {
116126
* @returns {Connection}
117127
*/
118128
getConnection (peerInfo) {
119-
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
129+
if (!PeerInfo.isPeerInfo(peerInfo)) {
130+
throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS)
131+
}
120132

121133
const connections = this.connections.get(peerInfo.id.toB58String())
122134
// Return the first, open connection
@@ -132,9 +144,9 @@ class Registrar {
132144
* @return {string} registrar identifier
133145
*/
134146
register (topology) {
135-
assert(
136-
Topology.isTopology(topology),
137-
'topology must be an instance of interfaces/topology')
147+
if (!Topology.isTopology(topology)) {
148+
throw errcode(new Error('topology must be an instance of interfaces/topology'), ERR_INVALID_PARAMETERS)
149+
}
138150

139151
// Create topology
140152
const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()

0 commit comments

Comments
 (0)