mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-24 22:42:25 +00:00
fix: #68
This commit is contained in:
parent
17daae05ca
commit
a19e68af68
@ -51,6 +51,7 @@
|
||||
"multiaddr": "^5.0.0",
|
||||
"pre-commit": "^1.2.2",
|
||||
"pull-goodbye": "0.0.2",
|
||||
"pull-reader": "^1.2.9",
|
||||
"pull-stream": "^3.6.7"
|
||||
},
|
||||
"contributors": [
|
||||
|
@ -4,9 +4,32 @@ const Connection = require('interface-connection').Connection
|
||||
const includes = require('lodash.includes')
|
||||
const multiaddr = require('multiaddr')
|
||||
const os = require('os')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
function noop () {}
|
||||
|
||||
function handle (data) {
|
||||
if (Buffer.isBuffer(data)) return data
|
||||
try {
|
||||
return Buffer.from(data)
|
||||
} catch (e) {
|
||||
return Buffer.from('')
|
||||
}
|
||||
}
|
||||
|
||||
function safe (conn) {
|
||||
return {
|
||||
sink: pull(
|
||||
pull.map(handle),
|
||||
conn.sink
|
||||
),
|
||||
source: pull(
|
||||
conn.source,
|
||||
pull.map(handle)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const createServer = require('pull-ws/server') || noop
|
||||
|
||||
module.exports = (options, handler) => {
|
||||
@ -16,7 +39,7 @@ module.exports = (options, handler) => {
|
||||
return callback(null, [])
|
||||
}
|
||||
|
||||
handler(new Connection(socket))
|
||||
handler(new Connection(safe(socket)))
|
||||
})
|
||||
|
||||
let listeningMultiaddr
|
||||
|
10
test/node.js
10
test/node.js
@ -172,11 +172,11 @@ describe('dial', () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
source: pull.values([Buffer.from('hey')]),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
done()
|
||||
})
|
||||
})
|
||||
@ -193,11 +193,11 @@ describe('dial', () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
source: pull.values([Buffer.from('hey')]),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
done()
|
||||
})
|
||||
})
|
||||
@ -221,7 +221,7 @@ describe('dont crash', () => {
|
||||
pull(conn, reader) // simulate mss-listener
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
reader.read(1, console.log)
|
||||
reader.read(1, err => err ? done(err) : false)
|
||||
})
|
||||
|
||||
afterEach((done) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user