diff --git a/index.js b/index.js index 7ae4473..06770bf 100644 --- a/index.js +++ b/index.js @@ -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) } @@ -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) } diff --git a/test.js b/test.js index 91f2063..ffe9ed8 100644 --- a/test.js +++ b/test.js @@ -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) { @@ -266,4 +267,27 @@ tape('close', function(t) { t.ok(true, 'should forward close') t.end() }) -}) \ No newline at end of file +}) + +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')) +})