1
1
'use strict'
2
2
3
- const assert = require ( 'assert' )
4
3
const debug = require ( 'debug' )
5
4
const debugName = 'libp2p:floodsub'
6
5
const log = debug ( debugName )
@@ -19,6 +18,24 @@ const { multicodec } = require('./config')
19
18
20
19
const ensureArray = utils . ensureArray
21
20
21
+ function validateRegistrar ( registrar ) {
22
+ if ( typeof registrar !== 'object' ) {
23
+ throw new Error ( 'a registrar object is required' )
24
+ }
25
+
26
+ if ( typeof registrar . handle !== 'function' ) {
27
+ throw new Error ( 'a handle function must be provided in registrar' )
28
+ }
29
+
30
+ if ( typeof registrar . register !== 'function' ) {
31
+ throw new Error ( 'a register function must be provided in registrar' )
32
+ }
33
+
34
+ if ( typeof registrar . unregister !== 'function' ) {
35
+ throw new Error ( 'a unregister function must be provided in registrar' )
36
+ }
37
+ }
38
+
22
39
/**
23
40
* FloodSub (aka dumbsub is an implementation of pubsub focused on
24
41
* delivering an API for Publish/Subscribe, but with no CastTree Forming
@@ -36,12 +53,11 @@ class FloodSub extends BaseProtocol {
36
53
* @constructor
37
54
*/
38
55
constructor ( peerInfo , registrar , options = { } ) {
39
- assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peer info must be an instance of `peer-info`' )
56
+ if ( ! PeerInfo . isPeerInfo ( peerInfo ) ) {
57
+ throw new Error ( 'peer info must be an instance of `peer-info`' )
58
+ }
40
59
41
- // registrar handling
42
- assert ( registrar && typeof registrar . handle === 'function' , 'a handle function must be provided in registrar' )
43
- assert ( registrar && typeof registrar . register === 'function' , 'a register function must be provided in registrar' )
44
- assert ( registrar && typeof registrar . unregister === 'function' , 'a unregister function must be provided in registrar' )
60
+ validateRegistrar ( registrar )
45
61
46
62
super ( {
47
63
debugName : debugName ,
@@ -229,7 +245,9 @@ class FloodSub extends BaseProtocol {
229
245
* @returns {Promise<void> }
230
246
*/
231
247
async publish ( topics , messages ) {
232
- assert ( this . started , 'FloodSub is not started' )
248
+ if ( ! this . started ) {
249
+ throw new Error ( 'FloodSub is not started' )
250
+ }
233
251
234
252
log ( 'publish' , topics , messages )
235
253
@@ -268,7 +286,9 @@ class FloodSub extends BaseProtocol {
268
286
* @returns {void }
269
287
*/
270
288
subscribe ( topics ) {
271
- assert ( this . started , 'FloodSub is not started' )
289
+ if ( ! this . started ) {
290
+ throw new Error ( 'FloodSub is not started' )
291
+ }
272
292
273
293
topics = ensureArray ( topics )
274
294
topics . forEach ( ( topic ) => this . subscriptions . add ( topic ) )
@@ -296,7 +316,9 @@ class FloodSub extends BaseProtocol {
296
316
* @returns {void }
297
317
*/
298
318
unsubscribe ( topics ) {
299
- assert ( this . started , 'FloodSub is not started' )
319
+ if ( ! this . started ) {
320
+ throw new Error ( 'FloodSub is not started' )
321
+ }
300
322
301
323
topics = ensureArray ( topics )
302
324
@@ -319,7 +341,9 @@ class FloodSub extends BaseProtocol {
319
341
* @returns {Array<String> }
320
342
*/
321
343
getTopics ( ) {
322
- assert ( this . started , 'FloodSub is not started' )
344
+ if ( ! this . started ) {
345
+ throw new Error ( 'FloodSub is not started' )
346
+ }
323
347
324
348
return Array . from ( this . subscriptions )
325
349
}
0 commit comments