diff --git a/index.js b/index.js index c0f1459..d474e8c 100644 --- a/index.js +++ b/index.js @@ -20,8 +20,12 @@ class DuplexSocket extends Duplex { } _write(chunk, encoding, callback) { - this[kOtherSide][kCallback] = callback; - this[kOtherSide].push(chunk); + if (chunk.length) { + this[kOtherSide][kCallback] = callback; + this[kOtherSide].push(chunk); + } else { + callback(); + } } _final(callback) { diff --git a/test/index.js b/test/index.js index f5b800f..3150524 100644 --- a/test/index.js +++ b/test/index.js @@ -4,7 +4,7 @@ const DuplexPair = require('../'); const assert = require('assert'); describe('DuplexPair', function() { - it('passed data through', function() { + it('passes data through', function() { const pair = new DuplexPair({ encoding: 'utf8' }); pair.socket1.write('Hello'); assert.strictEqual(pair.socket1.read(), null); @@ -18,4 +18,24 @@ describe('DuplexPair', function() { pair.socket2.end(); assert.strictEqual(pair.socket1.read(), null); }); + + it('does not deadlock when writing empty chunks', function(done) { + const pair = new DuplexPair({ encoding: 'utf8' }); + + pair.socket2.resume(); + pair.socket2.on('end', function() { + pair.socket2.write('Hello'); + pair.socket2.write(''); + pair.socket2.end(); + }); + + pair.socket1.on('data', function(chunk) { + assert.strictEqual(chunk, 'Hello'); + }); + pair.socket1.on('end', function() { + done(); + }); + + pair.socket1.end(); + }); });