Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: add readableDidRead #36820

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ function IncomingMessage(socket) {
this.statusMessage = null;
this.client = socket;

// TODO: Deprecate and remove.
this._consuming = false;
// Flag for when we decide that this message cannot possibly be
// read by the user, so there's no point continuing to handle it.
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
@@ -802,7 +802,7 @@ function resOnFinish(req, res, socket, state, server) {
// If the user never called req.read(), and didn't pipe() or
// .resume() or .on('data'), then we call req._dump() so that the
// bytes will be pulled off the wire.
if (!req._consuming && !req._readableState.resumeScheduled)
ronag marked this conversation as resolved.
Show resolved Hide resolved
if (!req.readableDidRead)
req._dump();

// Make sure the requestTimeout is cleared before finishing.
14 changes: 14 additions & 0 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
@@ -167,6 +167,8 @@ function ReadableState(options, stream, isDuplex) {
// If true, a maybeReadMore has been scheduled.
this.readingMore = false;

this.didRead = false;

this.decoder = null;
this.encoding = null;
if (options && options.encoding) {
@@ -520,6 +522,8 @@ Readable.prototype.read = function(n) {
if (ret !== null)
this.emit('data', ret);

state.didRead = true;

return ret;
};

@@ -823,7 +827,9 @@ function pipeOnDrain(src, dest) {

if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) &&
EE.listenerCount(src, 'data')) {
// TODO(ronag): Call resume() instead?
state.flowing = true;
state.didRead = true;
flow(src);
}
};
@@ -971,6 +977,7 @@ Readable.prototype.resume = function() {
function resume(stream, state) {
if (!state.resumeScheduled) {
state.resumeScheduled = true;
state.didRead = true;
process.nextTick(resume_, stream, state);
}
}
@@ -1181,6 +1188,13 @@ ObjectDefineProperties(Readable.prototype, {
}
},

readableDidRead: {
enumerable: false,
get: function() {
return this._readableState.didRead;
}
},

readableHighWaterMark: {
enumerable: false,
get: function() {
24 changes: 24 additions & 0 deletions test/parallel/test-stream-readable-didRead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
require('../common');
const assert = require('assert');
const Readable = require('stream').Readable;

{
const readable = new Readable({
read: () => {}
});

assert.strictEqual(readable.readableDidRead, false);
readable.read();
assert.strictEqual(readable.readableDidRead, true);
}

{
const readable = new Readable({
read: () => {}
});

assert.strictEqual(readable.readableDidRead, false);
readable.resume();
assert.strictEqual(readable.readableDidRead, true);
}