mirror of
https://github.com/fluencelabs/js-libp2p-utils
synced 2025-06-26 09:51:44 +00:00
feat: stream to multiaddr connection converter (#2)
This commit is contained in:
56
test/stream-to-ma-conn.spec.js
Normal file
56
test/stream-to-ma-conn.spec.js
Normal file
@ -0,0 +1,56 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
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()
|
||||
const localAddr = multiaddr('/ip4/101.45.75.219/tcp/6000')
|
||||
const remoteAddr = multiaddr('/ip4/100.46.74.201/tcp/6002')
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user