1
1
'use strict'
2
2
3
- const assert = require ( 'assert' )
4
3
const debug = require ( 'debug' )
5
4
const EventEmitter = require ( 'events' )
6
5
const errcode = require ( 'err-code' )
@@ -16,6 +15,25 @@ const {
16
15
verifySignature
17
16
} = require ( './message/sign' )
18
17
18
+ function validateRegistrar ( registrar ) {
19
+ // registrar handling
20
+ if ( typeof registrar !== 'object' ) {
21
+ throw new Error ( 'a registrar object is required' )
22
+ }
23
+
24
+ if ( typeof registrar . handle !== 'function' ) {
25
+ throw new Error ( 'a handle function must be provided in registrar' )
26
+ }
27
+
28
+ if ( typeof registrar . register !== 'function' ) {
29
+ throw new Error ( 'a register function must be provided in registrar' )
30
+ }
31
+
32
+ if ( typeof registrar . unregister !== 'function' ) {
33
+ throw new Error ( 'a unregister function must be provided in registrar' )
34
+ }
35
+ }
36
+
19
37
/**
20
38
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
21
39
*/
@@ -41,15 +59,19 @@ class PubsubBaseProtocol extends EventEmitter {
41
59
signMessages = true ,
42
60
strictSigning = true
43
61
} ) {
44
- assert ( debugName && typeof debugName === 'string' , 'a debugname `string` is required' )
45
- assert ( multicodecs , 'multicodecs are required' )
46
- assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peer info must be an instance of `peer-info`' )
47
-
48
- // registrar handling
49
- assert ( registrar && typeof registrar === 'object' , 'a registrar object is required' )
50
- assert ( typeof registrar . handle === 'function' , 'a handle function must be provided in registrar' )
51
- assert ( typeof registrar . register === 'function' , 'a register function must be provided in registrar' )
52
- assert ( typeof registrar . unregister === 'function' , 'a unregister function must be provided in registrar' )
62
+ if ( typeof debugName !== 'string' ) {
63
+ throw new Error ( 'a debugname `string` is required' )
64
+ }
65
+
66
+ if ( ! multicodecs ) {
67
+ throw new Error ( 'multicodecs are required' )
68
+ }
69
+
70
+ if ( ! PeerInfo . isPeerInfo ( peerInfo ) ) {
71
+ throw new Error ( 'peer info must be an instance of `peer-info`' )
72
+ }
73
+
74
+ validateRegistrar ( registrar )
53
75
54
76
super ( )
55
77
0 commit comments