Skip to content

Commit

Permalink
fix: add compatability with node@6.3.0 streams
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
dignifiedquire committed Jul 8, 2016
1 parent 0ecf524 commit acf662f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ var end = function(ws, fn) {
fn()
}

var getStateLength = function(state) {
if (state.buffer.length) {
// Since node 6.3.0 state.buffer is a BufferList not an array
if (state.buffer.head) {
return state.buffer.head.data.length
}

return state.buffer[0].length
}

return state.length
}

var toStreams2 = function(rs) {
return new (stream.Readable)({objectMode:true, highWaterMark:16}).wrap(rs)
}
Expand Down Expand Up @@ -158,7 +171,7 @@ Duplexify.prototype._forward = function() {
var data
var state = this._readable2._readableState

while ((data = this._readable2.read(state.buffer.length ? state.buffer[0].length : state.length)) !== null) {
while ((data = this._readable2.read(getStateLength(state))) !== null) {
this._drained = this.push(data)
}

Expand Down
26 changes: 25 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var tape = require('tape')
var through = require('through2')
var concat = require('concat-stream')
var tcp = require('net')
var duplexify = require('./')

tape('passthrough', function(t) {
Expand Down Expand Up @@ -266,4 +267,27 @@ tape('close', function(t) {
t.ok(true, 'should forward close')
t.end()
})
})
})

tape('works with node native streams (tcp)', function(t) {
var socket
t.plan(1)

var listener = tcp.createServer(function(socket) {
var dup = duplexify(socket, socket)

dup.once('data', function(chunk) {
t.same(chunk, Buffer('hello world'))
listener.close()
socket.end()
t.end()
})
})

listener.listen(0)

socket = tcp.connect(listener.address())
var dup = duplexify(socket, socket)

dup.write(Buffer('hello world'))
})

0 comments on commit acf662f

Please sign in to comment.