diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 1601823ca5f..f9ae412a976 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -426,6 +426,7 @@ Writable.prototype.end = function(chunk, encoding, cb) { function needFinish(stream, state) { return (state.ending && state.length === 0 && + state.buffer.length === 0 && !state.finished && !state.writing); } diff --git a/test/simple/test-stream2-writable.js b/test/simple/test-stream2-writable.js index 704100c0da4..3767ce140e7 100644 --- a/test/simple/test-stream2-writable.js +++ b/test/simple/test-stream2-writable.js @@ -391,3 +391,15 @@ test('finish does not come before sync _write cb', function(t) { }); w.end(); }); + +test('finish is emitted if last chunk is empty', function(t) { + var w = new W(); + w._write = function(chunk, e, cb) { + process.nextTick(cb); + }; + w.on('finish', function() { + t.end(); + }); + w.write(Buffer(1)); + w.end(Buffer(0)); +});