2
2
3
3
const PeerId = require ( 'peer-id' )
4
4
const multiaddr = require ( 'multiaddr' )
5
-
6
5
const withIs = require ( 'class-is' )
7
-
8
- const assert = require ( 'assert' )
9
6
const errCode = require ( 'err-code' )
10
7
const Status = require ( './status' )
11
8
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
+
12
55
/**
13
56
* An implementation of the js-libp2p connection.
14
57
* Any libp2p transport should use an upgrader to return this connection.
@@ -33,17 +76,7 @@ class Connection {
33
76
* @param {string } [properties.stat.encryption] connection encryption method identifier.
34
77
*/
35
78
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 )
47
80
48
81
/**
49
82
* Connection identifier.
0 commit comments