Skip to content

Commit c77d8de

Browse files
authored
fix: remove use of assert module (libp2p#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
1 parent 6203109 commit c77d8de

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

src/connection/connection.js

+47-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,56 @@
22

33
const PeerId = require('peer-id')
44
const multiaddr = require('multiaddr')
5-
65
const withIs = require('class-is')
7-
8-
const assert = require('assert')
96
const errCode = require('err-code')
107
const Status = require('./status')
118

9+
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
10+
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
11+
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
12+
}
13+
14+
if (!PeerId.isPeerId(localPeer)) {
15+
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
16+
}
17+
18+
if (!PeerId.isPeerId(remotePeer)) {
19+
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
20+
}
21+
22+
if (typeof newStream !== 'function') {
23+
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS')
24+
}
25+
26+
if (typeof close !== 'function') {
27+
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS')
28+
}
29+
30+
if (typeof getStreams !== 'function') {
31+
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS')
32+
}
33+
34+
if (!stat) {
35+
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS')
36+
}
37+
38+
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') {
39+
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS')
40+
}
41+
42+
if (!stat.timeline) {
43+
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS')
44+
}
45+
46+
if (!stat.timeline.open) {
47+
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
48+
}
49+
50+
if (!stat.timeline.upgraded) {
51+
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
52+
}
53+
}
54+
1255
/**
1356
* An implementation of the js-libp2p connection.
1457
* Any libp2p transport should use an upgrader to return this connection.
@@ -33,17 +76,7 @@ class Connection {
3376
* @param {string} [properties.stat.encryption] connection encryption method identifier.
3477
*/
3578
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
36-
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
37-
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
38-
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
39-
assert(typeof newStream === 'function', 'new stream must be a function')
40-
assert(typeof close === 'function', 'close must be a function')
41-
assert(typeof getStreams === 'function', 'getStreams must be a function')
42-
assert(stat, 'connection metadata object must be provided')
43-
assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
44-
assert(stat.timeline, 'connection timeline object must be provided in the stat object')
45-
assert(stat.timeline.open, 'connection open timestamp must be provided')
46-
assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')
79+
validateArgs(localAddr, localPeer, remotePeer, newStream, close, getStreams, stat)
4780

4881
/**
4982
* Connection identifier.

src/topology/multicodec-topology.js

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

3-
const assert = require('assert')
43
const withIs = require('class-is')
54

65
const Topology = require('./index')
@@ -24,12 +23,21 @@ class MulticodecTopology extends Topology {
2423
}) {
2524
super({ min, max, handlers })
2625

27-
assert(multicodecs, 'one or more multicodec should be provided')
28-
assert(handlers, 'the handlers should be provided')
29-
assert(handlers.onConnect && typeof handlers.onConnect === 'function',
30-
'the \'onConnect\' handler must be provided')
31-
assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function',
32-
'the \'onDisconnect\' handler must be provided')
26+
if (!multicodecs) {
27+
throw new Error('one or more multicodec should be provided')
28+
}
29+
30+
if (!handlers) {
31+
throw new Error('the handlers should be provided')
32+
}
33+
34+
if (typeof handlers.onConnect !== 'function') {
35+
throw new Error('the \'onConnect\' handler must be provided')
36+
}
37+
38+
if (typeof handlers.onDisconnect !== 'function') {
39+
throw new Error('the \'onDisconnect\' handler must be provided')
40+
}
3341

3442
this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
3543
this._registrar = undefined

0 commit comments

Comments
 (0)