Skip to content

Commit

Permalink
fix: remove use of assert module (#37)
Browse files Browse the repository at this point in the history
The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.
  • Loading branch information
achingbrain committed Feb 14, 2020
1 parent 21d3017 commit d452054
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const debug = require('debug')
const EventEmitter = require('events')
const errcode = require('err-code')
Expand All @@ -16,6 +15,25 @@ const {
verifySignature
} = require('./message/sign')

function validateRegistrar (registrar) {
// registrar handling
if (typeof registrar !== 'object') {
throw new Error('a registrar object is required')
}

if (typeof registrar.handle !== 'function') {
throw new Error('a handle function must be provided in registrar')
}

if (typeof registrar.register !== 'function') {
throw new Error('a register function must be provided in registrar')
}

if (typeof registrar.unregister !== 'function') {
throw new Error('a unregister function must be provided in registrar')
}
}

/**
* PubsubBaseProtocol handles the peers and connections logic for pubsub routers
*/
Expand All @@ -41,15 +59,19 @@ class PubsubBaseProtocol extends EventEmitter {
signMessages = true,
strictSigning = true
}) {
assert(debugName && typeof debugName === 'string', 'a debugname `string` is required')
assert(multicodecs, 'multicodecs are required')
assert(PeerInfo.isPeerInfo(peerInfo), 'peer info must be an instance of `peer-info`')

// registrar handling
assert(registrar && typeof registrar === 'object', 'a registrar object is required')
assert(typeof registrar.handle === 'function', 'a handle function must be provided in registrar')
assert(typeof registrar.register === 'function', 'a register function must be provided in registrar')
assert(typeof registrar.unregister === 'function', 'a unregister function must be provided in registrar')
if (typeof debugName !== 'string') {
throw new Error('a debugname `string` is required')
}

if (!multicodecs) {
throw new Error('multicodecs are required')
}

if (!PeerInfo.isPeerInfo(peerInfo)) {
throw new Error('peer info must be an instance of `peer-info`')
}

validateRegistrar(registrar)

super()

Expand Down

0 comments on commit d452054

Please sign in to comment.