2020-02-15 09:43:12 +01:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
2020-08-07 16:40:19 +01:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2020-02-15 09:43:12 +01:00
|
|
|
const pair = require('it-pair')
|
|
|
|
const pipe = require('it-pipe')
|
|
|
|
const { collect } = require('streaming-iterables')
|
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
const streamToMaConn = require('../src/stream-to-ma-conn')
|
|
|
|
|
|
|
|
describe('Convert stream into a multiaddr connection', () => {
|
|
|
|
it('converts a stream and adds the provided metadata', () => {
|
|
|
|
const stream = pair()
|
2021-04-09 18:12:25 +03:00
|
|
|
const localAddr = new multiaddr.Multiaddr('/ip4/101.45.75.219/tcp/6000')
|
|
|
|
const remoteAddr = new multiaddr.Multiaddr('/ip4/100.46.74.201/tcp/6002')
|
2020-02-15 09:43:12 +01:00
|
|
|
|
|
|
|
const maConn = streamToMaConn({
|
|
|
|
stream,
|
|
|
|
localAddr,
|
|
|
|
remoteAddr
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(maConn).to.exist()
|
|
|
|
expect(maConn.sink).to.exist()
|
|
|
|
expect(maConn.source).to.exist()
|
|
|
|
expect(maConn.localAddr).to.eql(localAddr)
|
|
|
|
expect(maConn.remoteAddr).to.eql(remoteAddr)
|
|
|
|
expect(maConn.timeline).to.exist()
|
|
|
|
expect(maConn.timeline.open).to.exist()
|
|
|
|
expect(maConn.timeline.close).to.not.exist()
|
|
|
|
|
|
|
|
maConn.close()
|
|
|
|
expect(maConn.timeline.close).to.exist()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('can stream data over the multiaddr connection', async () => {
|
|
|
|
const stream = pair()
|
|
|
|
const maConn = streamToMaConn({ stream })
|
|
|
|
|
|
|
|
const data = 'hey'
|
|
|
|
const streamData = await pipe(
|
|
|
|
[data],
|
|
|
|
maConn,
|
|
|
|
collect
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(streamData).to.eql([data])
|
|
|
|
// underlying stream end closes the connection
|
|
|
|
expect(maConn.timeline.close).to.exist()
|
|
|
|
})
|
|
|
|
})
|