diff --git a/package.json b/package.json index f606b2a..235b508 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/socket-to-conn.js b/src/socket-to-conn.js index d819995..f8a8195 100644 --- a/src/socket-to-conn.js +++ b/src/socket-to-conn.js @@ -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) diff --git a/test/node.js b/test/node.js index 7247986..6c54cde 100644 --- a/test/node.js +++ b/test/node.js @@ -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)