mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-24 16:52:22 +00:00
fix: remove use of assert module (#34)
* 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: pr comments
This commit is contained in:
parent
6203109751
commit
c77d8de2e7
@ -2,13 +2,56 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const withIs = require('class-is')
|
||||
|
||||
const assert = require('assert')
|
||||
const errCode = require('err-code')
|
||||
const Status = require('./status')
|
||||
|
||||
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
|
||||
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
|
||||
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!PeerId.isPeerId(localPeer)) {
|
||||
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!PeerId.isPeerId(remotePeer)) {
|
||||
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof newStream !== 'function') {
|
||||
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof close !== 'function') {
|
||||
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof getStreams !== 'function') {
|
||||
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat) {
|
||||
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') {
|
||||
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline) {
|
||||
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline.open) {
|
||||
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline.upgraded) {
|
||||
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of the js-libp2p connection.
|
||||
* Any libp2p transport should use an upgrader to return this connection.
|
||||
@ -33,17 +76,7 @@ class Connection {
|
||||
* @param {string} [properties.stat.encryption] connection encryption method identifier.
|
||||
*/
|
||||
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
||||
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
|
||||
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
|
||||
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
|
||||
assert(typeof newStream === 'function', 'new stream must be a function')
|
||||
assert(typeof close === 'function', 'close must be a function')
|
||||
assert(typeof getStreams === 'function', 'getStreams must be a function')
|
||||
assert(stat, 'connection metadata object must be provided')
|
||||
assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
|
||||
assert(stat.timeline, 'connection timeline object must be provided in the stat object')
|
||||
assert(stat.timeline.open, 'connection open timestamp must be provided')
|
||||
assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')
|
||||
validateArgs(localAddr, localPeer, remotePeer, newStream, close, getStreams, stat)
|
||||
|
||||
/**
|
||||
* Connection identifier.
|
||||
|
@ -1,6 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const withIs = require('class-is')
|
||||
|
||||
const Topology = require('./index')
|
||||
@ -24,12 +23,21 @@ class MulticodecTopology extends Topology {
|
||||
}) {
|
||||
super({ min, max, handlers })
|
||||
|
||||
assert(multicodecs, 'one or more multicodec should be provided')
|
||||
assert(handlers, 'the handlers should be provided')
|
||||
assert(handlers.onConnect && typeof handlers.onConnect === 'function',
|
||||
'the \'onConnect\' handler must be provided')
|
||||
assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function',
|
||||
'the \'onDisconnect\' handler must be provided')
|
||||
if (!multicodecs) {
|
||||
throw new Error('one or more multicodec should be provided')
|
||||
}
|
||||
|
||||
if (!handlers) {
|
||||
throw new Error('the handlers should be provided')
|
||||
}
|
||||
|
||||
if (typeof handlers.onConnect !== 'function') {
|
||||
throw new Error('the \'onConnect\' handler must be provided')
|
||||
}
|
||||
|
||||
if (typeof handlers.onDisconnect !== 'function') {
|
||||
throw new Error('the \'onDisconnect\' handler must be provided')
|
||||
}
|
||||
|
||||
this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
|
||||
this._registrar = undefined
|
||||
|
Loading…
x
Reference in New Issue
Block a user