Skip to content
This repository was archived by the owner on Mar 11, 2020. It is now read-only.

Commit 0105e4c

Browse files
committed
chore: add basic discovery module for compliance spec
1 parent e690d28 commit 0105e4c

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"devDependencies": {
4141
"aegir": "^20.3.1",
4242
"chai": "^4.2.0",
43-
"dirty-chai": "^2.0.1"
43+
"dirty-chai": "^2.0.1",
44+
"peer-id": "^0.13.3",
45+
"peer-info": "^0.17.0"
4446
},
4547
"engines": {
4648
"node": ">=10.0.0",

test/compliance.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const tests = require('../src')
5+
const MockDiscovery = require('./mock-discovery')
6+
7+
describe('compliance tests', () => {
8+
tests({
9+
setup () {
10+
return new MockDiscovery()
11+
}
12+
})
13+
})

test/mock-discovery.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const { EventEmitter } = require('events')
4+
5+
const PeerId = require('peer-id')
6+
const PeerInfo = require('peer-info')
7+
8+
/**
9+
* Emits 'peer' events on discovery.
10+
*/
11+
class MockDiscovery extends EventEmitter {
12+
/**
13+
* Constructs a new Bootstrap.
14+
*
15+
* @param {Object} options
16+
* @param {number} options.discoveryDelay - the delay to find a peer (in milli-seconds)
17+
*/
18+
constructor (options = {}) {
19+
super()
20+
21+
this.options = options
22+
this._isRunning = false
23+
this._timer = null
24+
}
25+
26+
start () {
27+
this._isRunning = true
28+
this._discoverPeer()
29+
}
30+
31+
stop () {
32+
clearTimeout(this._timer)
33+
this._isRunning = false
34+
}
35+
36+
async _discoverPeer () {
37+
if (!this._isRunning) return
38+
39+
const peerId = await PeerId.create({ bits: 512 })
40+
const peerInfo = new PeerInfo(peerId)
41+
42+
this._timer = setTimeout(() => {
43+
this.emit('peer', peerInfo)
44+
}, this.options.discoveryDelay || 1000)
45+
}
46+
}
47+
48+
module.exports = MockDiscovery

test/peer-discovery.spec.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)