Skip to content

Commit d452054

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

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/index.js

+32-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 EventEmitter = require('events')
65
const errcode = require('err-code')
@@ -16,6 +15,25 @@ const {
1615
verifySignature
1716
} = require('./message/sign')
1817

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+
1937
/**
2038
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
2139
*/
@@ -41,15 +59,19 @@ class PubsubBaseProtocol extends EventEmitter {
4159
signMessages = true,
4260
strictSigning = true
4361
}) {
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)
5375

5476
super()
5577

0 commit comments

Comments
 (0)