mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-25 01:42:25 +00:00
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:
parent
5b59fc3a47
commit
3bf66d08e4
@ -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",
|
||||
|
@ -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)
|
||||
|
10
test/node.js
10
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user