2019-10-21 16:53:58 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const duplexPair = require('it-pair/duplex')
|
|
|
|
const abortable = require('abortable-iterator')
|
|
|
|
const AbortController = require('abort-controller')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns both sides of a mocked MultiaddrConnection
|
2020-10-06 14:59:43 +02:00
|
|
|
*
|
2019-10-21 16:53:58 +02:00
|
|
|
* @param {object} options
|
2020-10-06 14:59:43 +02:00
|
|
|
* @param {Multiaddr[]} options.addrs - Should contain two addresses for the local and remote peer
|
|
|
|
* @param {PeerId} options.remotePeer - The peer that is being "dialed"
|
2019-10-21 16:53:58 +02:00
|
|
|
* @returns {{inbound:MultiaddrConnection, outbound:MultiaddrConnection}}
|
|
|
|
*/
|
|
|
|
module.exports = function mockMultiaddrConnPair ({ addrs, remotePeer }) {
|
|
|
|
const controller = new AbortController()
|
2019-11-29 16:41:08 +01:00
|
|
|
const [localAddr, remoteAddr] = addrs
|
2019-10-21 16:53:58 +02:00
|
|
|
|
|
|
|
const [inbound, outbound] = duplexPair()
|
2019-11-29 16:41:08 +01:00
|
|
|
outbound.localAddr = localAddr
|
|
|
|
outbound.remoteAddr = remoteAddr.encapsulate(`/p2p/${remotePeer.toB58String()}`)
|
2019-10-21 16:53:58 +02:00
|
|
|
outbound.timeline = {
|
|
|
|
open: Date.now()
|
|
|
|
}
|
|
|
|
outbound.close = () => {
|
|
|
|
outbound.timeline.close = Date.now()
|
|
|
|
controller.abort()
|
|
|
|
}
|
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
inbound.localAddr = remoteAddr
|
|
|
|
inbound.remoteAddr = localAddr
|
2019-10-21 16:53:58 +02:00
|
|
|
inbound.timeline = {
|
|
|
|
open: Date.now()
|
|
|
|
}
|
|
|
|
inbound.close = () => {
|
|
|
|
inbound.timeline.close = Date.now()
|
|
|
|
controller.abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the sources abortable so we can close them easily
|
|
|
|
inbound.source = abortable(inbound.source, controller.signal)
|
|
|
|
outbound.source = abortable(outbound.source, controller.signal)
|
|
|
|
|
|
|
|
return { inbound, outbound }
|
|
|
|
}
|