From 929e4d9c9a09947c2ec86ee14f6d312b1bda1a16 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 26 Mar 2013 22:43:53 -0700 Subject: [PATCH] stream: Emit readable on ended streams via read(0) cc: @mjijackson --- lib/_stream_readable.js | 2 +- test/simple/test-stream-readable-event.js | 24 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 2f4d8a4963..e30a327f9d 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -236,7 +236,7 @@ Readable.prototype.read = function(n) { // the 'readable' event and move on. if (n === 0 && state.needReadable && - state.length >= state.highWaterMark) { + (state.length >= state.highWaterMark || state.ended)) { emitReadable(this); return null; } diff --git a/test/simple/test-stream-readable-event.js b/test/simple/test-stream-readable-event.js index be17f0455f..fdbcb48103 100644 --- a/test/simple/test-stream-readable-event.js +++ b/test/simple/test-stream-readable-event.js @@ -80,3 +80,27 @@ var Readable = require('stream').Readable; console.log('ok 2'); }); })(); + +(function third() { + // Third test, not reading when the stream has not passed + // the highWaterMark but *has* reached EOF. + var r = new Readable({ + highWaterMark: 30 + }); + + // This triggers a 'readable' event, which is lost. + r.push(new Buffer('blerg')); + r.push(null); + + var caughtReadable = false; + setTimeout(function() { + r.on('readable', function() { + caughtReadable = true; + }); + }); + + process.on('exit', function() { + assert(caughtReadable); + console.log('ok 3'); + }); +})();