Skip to content

Commit 8fe6d18

Browse files
authored
fix: remove use of assert module (#95)
The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.
1 parent 7d67c6e commit 8fe6d18

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/index.js

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

3-
const assert = require('assert')
43
const debug = require('debug')
54
const debugName = 'libp2p:floodsub'
65
const log = debug(debugName)
@@ -19,6 +18,24 @@ const { multicodec } = require('./config')
1918

2019
const ensureArray = utils.ensureArray
2120

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+
2239
/**
2340
* FloodSub (aka dumbsub is an implementation of pubsub focused on
2441
* delivering an API for Publish/Subscribe, but with no CastTree Forming
@@ -36,12 +53,11 @@ class FloodSub extends BaseProtocol {
3653
* @constructor
3754
*/
3855
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+
}
4059

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)
4561

4662
super({
4763
debugName: debugName,
@@ -229,7 +245,9 @@ class FloodSub extends BaseProtocol {
229245
* @returns {Promise<void>}
230246
*/
231247
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+
}
233251

234252
log('publish', topics, messages)
235253

@@ -268,7 +286,9 @@ class FloodSub extends BaseProtocol {
268286
* @returns {void}
269287
*/
270288
subscribe (topics) {
271-
assert(this.started, 'FloodSub is not started')
289+
if (!this.started) {
290+
throw new Error('FloodSub is not started')
291+
}
272292

273293
topics = ensureArray(topics)
274294
topics.forEach((topic) => this.subscriptions.add(topic))
@@ -296,7 +316,9 @@ class FloodSub extends BaseProtocol {
296316
* @returns {void}
297317
*/
298318
unsubscribe (topics) {
299-
assert(this.started, 'FloodSub is not started')
319+
if (!this.started) {
320+
throw new Error('FloodSub is not started')
321+
}
300322

301323
topics = ensureArray(topics)
302324

@@ -319,7 +341,9 @@ class FloodSub extends BaseProtocol {
319341
* @returns {Array<String>}
320342
*/
321343
getTopics () {
322-
assert(this.started, 'FloodSub is not started')
344+
if (!this.started) {
345+
throw new Error('FloodSub is not started')
346+
}
323347

324348
return Array.from(this.subscriptions)
325349
}

0 commit comments

Comments
 (0)