js-libp2p-utils/test/stream-to-ma-conn.spec.js
Alex Potsides a2ea68f319
chore: update deps (#9)
* chore: update deps

Pulls in the latest multiaddrs that replaces node Buffers with
Uint8Arrays

BREAKING CHANGES:

- The multiaddr dep of this module uses Uint8Arrays and may not be
  compatible with previous versions

* chore: remove gh url
2020-08-07 17:40:19 +02:00

53 lines
1.4 KiB
JavaScript

/* eslint-env mocha */
'use strict'
const { expect } = require('aegir/utils/chai')
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()
})
})