mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-23 14:01:35 +00:00
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:
@ -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()
|
||||
|
Reference in New Issue
Block a user