fix: support bufferlist usage (#97)

several it-* modules leverage bufferlist, but ws does not. We need to convert buffer lists to buffers before handing the data off to ws for transmission

License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
Jacob Heun 2019-10-30 14:23:50 +01:00 committed by GitHub
parent 5b59fc3a47
commit 3bf66d08e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -52,6 +52,7 @@
"devDependencies": {
"abort-controller": "^3.0.0",
"aegir": "^20.3.1",
"bl": "^4.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"interface-transport": "^0.7.0",

View File

@ -20,7 +20,12 @@ module.exports = (socket, options = {}) => {
}
try {
await socket.sink(source)
await socket.sink((async function * () {
for await (const chunk of source) {
// Convert BufferList to Buffer
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
}
})())
} catch (err) {
if (err.type !== 'aborted') {
log.error(err)

View File

@ -13,6 +13,7 @@ const multiaddr = require('multiaddr')
const goodbye = require('it-goodbye')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const BufferList = require('bl/BufferList')
const WS = require('../src')
@ -301,6 +302,15 @@ describe('dial', () => {
expect(result).to.be.eql([Buffer.from('hey')])
})
it('dial and use BufferList', async () => {
const conn = await ws.dial(ma)
const s = goodbye({ source: [new BufferList('hey')], sink: collect })
const result = await pipe(s, conn, s)
expect(result).to.be.eql([Buffer.from('hey')])
})
it('dial with p2p Id', async () => {
const ma = multiaddr('/ip6/::1/tcp/9091/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
const conn = await ws.dial(ma)