2019-09-27 11:48:45 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
|
2020-04-06 20:20:27 +02:00
|
|
|
const multiaddr = require('multiaddr')
|
2019-09-27 11:48:45 +02:00
|
|
|
const PeerId = require('peer-id')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits 'peer' events on discovery.
|
|
|
|
*/
|
|
|
|
class MockDiscovery extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* Constructs a new Bootstrap.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {number} options.discoveryDelay - the delay to find a peer (in milli-seconds)
|
|
|
|
*/
|
|
|
|
constructor (options = {}) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.options = options
|
|
|
|
this._isRunning = false
|
|
|
|
this._timer = null
|
|
|
|
}
|
|
|
|
|
|
|
|
start () {
|
|
|
|
this._isRunning = true
|
|
|
|
this._discoverPeer()
|
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
|
|
|
clearTimeout(this._timer)
|
|
|
|
this._isRunning = false
|
|
|
|
}
|
|
|
|
|
|
|
|
async _discoverPeer () {
|
|
|
|
if (!this._isRunning) return
|
|
|
|
|
|
|
|
const peerId = await PeerId.create({ bits: 512 })
|
|
|
|
|
|
|
|
this._timer = setTimeout(() => {
|
2020-04-06 20:20:27 +02:00
|
|
|
this.emit('peer', {
|
|
|
|
id: peerId,
|
2021-04-09 14:18:10 +03:00
|
|
|
multiaddrs: [new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
2020-04-06 20:20:27 +02:00
|
|
|
})
|
2019-09-27 11:48:45 +02:00
|
|
|
}, this.options.discoveryDelay || 1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MockDiscovery
|