Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit de85eb6

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

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/index.js

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

3-
const assert = require('assert')
43
const { EventEmitter } = require('events')
54
const errcode = require('err-code')
65

@@ -67,7 +66,10 @@ class KadDHT extends EventEmitter {
6766
randomWalk = {}
6867
}) {
6968
super()
70-
assert(dialer, 'libp2p-kad-dht requires an instance of Dialer')
69+
70+
if (!dialer) {
71+
throw new Error('libp2p-kad-dht requires an instance of Dialer')
72+
}
7173

7274
/**
7375
* Local reference to the libp2p dialer instance

src/message/index.js

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

3-
const assert = require('assert')
43
const PeerInfo = require('peer-info')
54
const PeerId = require('peer-id')
65
const protons = require('protons')
@@ -21,8 +20,8 @@ class Message {
2120
* @param {number} level
2221
*/
2322
constructor (type, key, level) {
24-
if (key) {
25-
assert(Buffer.isBuffer(key))
23+
if (key && !Buffer.isBuffer(key)) {
24+
throw new Error('Key must be a buffer')
2625
}
2726

2827
this.type = type

src/random-walk.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const crypto = require('libp2p-crypto')
44
const multihashing = require('multihashing-async')
55
const PeerId = require('peer-id')
6-
const assert = require('assert')
76
const AbortController = require('abort-controller')
87
const errcode = require('err-code')
98
const times = require('p-times')
@@ -23,7 +22,9 @@ class RandomWalk {
2322
* @param {DHT} options.dht
2423
*/
2524
constructor (dht, options) {
26-
assert(dht, 'Random Walk needs an instance of the Kademlia DHT')
25+
if (!dht) {
26+
throw new Error('Random Walk needs an instance of the Kademlia DHT')
27+
}
2728

2829
this._kadDHT = dht
2930
this._options = {

test/random-walk.spec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const expect = chai.expect
88
const sinon = require('sinon')
99
const RandomWalk = require('../src/random-walk')
1010
const { defaultRandomWalk } = require('../src/constants')
11-
const { AssertionError } = require('assert')
1211

1312
const TestDHT = require('./utils/test-dht')
1413
const {
@@ -35,7 +34,7 @@ describe('Random Walk', () => {
3534

3635
describe('configuration', () => {
3736
it('should use require a dht', () => {
38-
expect(() => new RandomWalk()).to.throw(AssertionError)
37+
expect(() => new RandomWalk()).to.throw()
3938
})
4039

4140
it('should use defaults', () => {

0 commit comments

Comments
 (0)