summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-03-09 18:05:39 -0800
committerisaacs <i@izs.me>2013-03-10 11:08:22 -0700
commit327b6e3e1de88ba1db83142d53b3a57f0d06d519 (patch)
treebdd519bbef7ce32b9442fa49df9346e742c8b708 /lib
parentcd2b9f542c34ce59d2df7820a6237f7911c702f3 (diff)
downloadandroid-node-v8-327b6e3e1de88ba1db83142d53b3a57f0d06d519.tar.gz
android-node-v8-327b6e3e1de88ba1db83142d53b3a57f0d06d519.tar.bz2
android-node-v8-327b6e3e1de88ba1db83142d53b3a57f0d06d519.zip
stream: Don't emit 'end' unless read() called
This solves the problem of calling `readable.pipe(writable)` after the readable stream has already emitted 'end', as often is the case when writing simple HTTP proxies. The spirit of streams2 is that things will work properly, even if you don't set them up right away on the first tick. This approach breaks down, however, because pipe()ing from an ended readable will just do nothing. No more data will ever arrive, and the writable will hang open forever never being ended. However, that does not solve the case of adding a `on('end')` listener after the stream has received the EOF chunk, if it was the first chunk received (and thus, length was 0, and 'end' got emitted). So, with this, we defer the 'end' event emission until the read() function is called. Also, in pipe(), if the source has emitted 'end' already, we call the cleanup/onend function on nextTick. Piping from an already-ended stream is thus the same as piping from a stream that is in the process of ending. Updates many tests that were relying on 'end' coming immediately, even though they never read() from the req. Fix #4942
Diffstat (limited to 'lib')
-rw-r--r--lib/_stream_readable.js39
-rw-r--r--lib/http.js14
2 files changed, 31 insertions, 22 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index 41cf8e89cc..b09694c071 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -49,6 +49,12 @@ function ReadableState(options, stream) {
this.endEmitted = false;
this.reading = false;
+ // In streams that never have any data, and do push(null) right away,
+ // the consumer can miss the 'end' event if they do some I/O before
+ // consuming the stream. So, we don't emit('end') until some reading
+ // happens.
+ this.calledRead = false;
+
// a flag to be able to tell if the onwrite cb is called immediately,
// or on a later tick. We set this to true at first, becuase any
// actions that shouldn't happen until "later" should generally also
@@ -218,6 +224,7 @@ function howMuchToRead(n, state) {
// you can override either this method, or the async _read(n) below.
Readable.prototype.read = function(n) {
var state = this._readableState;
+ state.calledRead = true;
var nOrig = n;
if (typeof n !== 'number' || n > 0)
@@ -430,13 +437,15 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
}
state.pipesCount += 1;
- if ((!pipeOpts || pipeOpts.end !== false) &&
- dest !== process.stdout &&
- dest !== process.stderr) {
- src.once('end', onend);
- } else {
- src.once('end', cleanup);
- }
+ var doEnd = (!pipeOpts || pipeOpts.end !== false) &&
+ dest !== process.stdout &&
+ dest !== process.stderr;
+
+ var endFn = doEnd ? onend : cleanup;
+ if (state.endEmitted)
+ process.nextTick(endFn);
+ else
+ src.once('end', endFn);
dest.on('unpipe', onunpipe);
function onunpipe(readable) {
@@ -853,12 +862,12 @@ function endReadable(stream) {
if (state.length > 0)
throw new Error('endReadable called on non-empty stream');
- if (state.endEmitted)
- return;
- state.ended = true;
- state.endEmitted = true;
- process.nextTick(function() {
- stream.readable = false;
- stream.emit('end');
- });
+ if (!state.endEmitted && state.calledRead) {
+ state.ended = true;
+ state.endEmitted = true;
+ process.nextTick(function() {
+ stream.readable = false;
+ stream.emit('end');
+ });
+ }
}
diff --git a/lib/http.js b/lib/http.js
index 0f7ea853c0..2bceaa0152 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -416,13 +416,13 @@ IncomingMessage.prototype._addHeaderLine = function(field, value) {
// Call this instead of resume() if we want to just
// dump all the data to /dev/null
IncomingMessage.prototype._dump = function() {
- if (this._dumped)
- return;
-
- this._dumped = true;
- if (this.socket.parser) this.socket.parser.incoming = null;
- this.push(null);
- readStart(this.socket);
+ if (!this._dumped) {
+ this._dumped = true;
+ if (this.socket.parser) this.socket.parser.incoming = null;
+ this.push(null);
+ readStart(this.socket);
+ this.read();
+ }
};