Skip to content

Commit c69dab9

Browse files
committed
stream: add readableDidRead
Adds readableDidRead to streams and applies usage to http, http2 and quic. PR-URL: nodejs#36820 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 785bc53 commit c69dab9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

lib/_http_incoming.js

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function IncomingMessage(socket) {
8787
this.statusMessage = null;
8888
this.client = socket;
8989

90+
// TODO: Deprecate and remove.
9091
this._consuming = false;
9192
// Flag for when we decide that this message cannot possibly be
9293
// read by the user, so there's no point continuing to handle it.

lib/_http_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ function resOnFinish(req, res, socket, state, server) {
804804
// If the user never called req.read(), and didn't pipe() or
805805
// .resume() or .on('data'), then we call req._dump() so that the
806806
// bytes will be pulled off the wire.
807-
if (!req._consuming && !req._readableState.resumeScheduled)
807+
if (!req.readableDidRead)
808808
req._dump();
809809

810810
// Make sure the requestTimeout is cleared before finishing.

lib/internal/streams/readable.js

+14
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ function ReadableState(options, stream, isDuplex) {
167167
// If true, a maybeReadMore has been scheduled.
168168
this.readingMore = false;
169169

170+
this.didRead = false;
171+
170172
this.decoder = null;
171173
this.encoding = null;
172174
if (options && options.encoding) {
@@ -522,6 +524,8 @@ Readable.prototype.read = function(n) {
522524
if (ret !== null)
523525
this.emit('data', ret);
524526

527+
state.didRead = true;
528+
525529
return ret;
526530
};
527531

@@ -825,7 +829,9 @@ function pipeOnDrain(src, dest) {
825829

826830
if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) &&
827831
EE.listenerCount(src, 'data')) {
832+
// TODO(ronag): Call resume() instead?
828833
state.flowing = true;
834+
state.didRead = true;
829835
flow(src);
830836
}
831837
};
@@ -973,6 +979,7 @@ Readable.prototype.resume = function() {
973979
function resume(stream, state) {
974980
if (!state.resumeScheduled) {
975981
state.resumeScheduled = true;
982+
state.didRead = true;
976983
process.nextTick(resume_, stream, state);
977984
}
978985
}
@@ -1183,6 +1190,13 @@ ObjectDefineProperties(Readable.prototype, {
11831190
}
11841191
},
11851192

1193+
readableDidRead: {
1194+
enumerable: false,
1195+
get: function() {
1196+
return this._readableState.didRead;
1197+
}
1198+
},
1199+
11861200
readableHighWaterMark: {
11871201
enumerable: false,
11881202
get: function() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const Readable = require('stream').Readable;
5+
6+
{
7+
const readable = new Readable({
8+
read: () => {}
9+
});
10+
11+
assert.strictEqual(readable.readableDidRead, false);
12+
readable.read();
13+
assert.strictEqual(readable.readableDidRead, true);
14+
}
15+
16+
{
17+
const readable = new Readable({
18+
read: () => {}
19+
});
20+
21+
assert.strictEqual(readable.readableDidRead, false);
22+
readable.resume();
23+
assert.strictEqual(readable.readableDidRead, true);
24+
}

0 commit comments

Comments
 (0)