Skip to content

Commit

Permalink
fix: add compatability with [email protected] streams
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 8, 2016
1 parent 048ec46 commit 2977545
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ var destroyer = function(self, end) {
}
}

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 end = function(ws, fn) {
if (!ws) return fn()
if (ws._writableState && ws._writableState.finished) return fn()
Expand Down Expand Up @@ -166,8 +180,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
12 changes: 10 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,14 @@ tape('close is bubbled up on both ends - destroy on listener', function(t) {
tape('close is bubbled up on both ends - destroy on dialer', function(t) {
var listener
var counter = 0

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

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

socket.on('close', count)
dup.on('close', count)
})
Expand All @@ -314,6 +318,8 @@ tape('close is bubbled up on both ends - destroy on dialer', function(t) {
var socket = tcp.connect(listener.address())
var dup = duplexify(socket, socket)

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

socket.on('close', count)
dup.on('close', count)

Expand All @@ -323,7 +329,9 @@ tape('close is bubbled up on both ends - destroy on dialer', function(t) {

function count() {
if (++counter === 4) {
return listener.close(t.end)
return listener.close(function() {
t.ok(true)
})
}
}
})

0 comments on commit 2977545

Please sign in to comment.