Skip to content

Commit 8306051

Browse files
committed
stream: add readableDidRead
Adds readableDidRead to streams and applies usage to http, http2 and quic. PR-URL: #36820 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 4a9fcb3 commit 8306051

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎lib/_http_incoming.js‎

Lines changed: 1 addition & 0 deletions
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‎

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

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

‎lib/internal/streams/readable.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ function ReadableState(options, stream, isDuplex) {
171171
// If true, a maybeReadMore has been scheduled.
172172
this.readingMore=false;
173173

174+
this.didRead=false;
175+
174176
this.decoder=null;
175177
this.encoding=null;
176178
if(options&&options.encoding){
@@ -542,6 +544,8 @@ Readable.prototype.read = function(n) {
542544
if(ret!==null)
543545
this.emit('data',ret);
544546

547+
state.didRead=true;
548+
545549
returnret;
546550
};
547551

@@ -845,7 +849,9 @@ function pipeOnDrain(src, dest) {
845849

846850
if((!state.awaitDrainWriters||state.awaitDrainWriters.size===0)&&
847851
EE.listenerCount(src,'data')){
852+
// TODO(ronag): Call resume() instead?
848853
state.flowing=true;
854+
state.didRead=true;
849855
flow(src);
850856
}
851857
};
@@ -993,6 +999,7 @@ Readable.prototype.resume = function() {
993999
functionresume(stream,state){
9941000
if(!state.resumeScheduled){
9951001
state.resumeScheduled=true;
1002+
state.didRead=true;
9961003
process.nextTick(resume_,stream,state);
9971004
}
9981005
}
@@ -1177,6 +1184,13 @@ ObjectDefineProperties(Readable.prototype, {
11771184
}
11781185
},
11791186

1187+
readableDidRead: {
1188+
enumerable: false,
1189+
get: function(){
1190+
returnthis._readableState.didRead;
1191+
}
1192+
},
1193+
11801194
readableHighWaterMark: {
11811195
enumerable: false,
11821196
get: function(){
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
constReadable=require('stream').Readable;
5+
6+
{
7+
constreadable=newReadable({
8+
read: ()=>{}
9+
});
10+
11+
assert.strictEqual(readable.readableDidRead,false);
12+
readable.read();
13+
assert.strictEqual(readable.readableDidRead,true);
14+
}
15+
16+
{
17+
constreadable=newReadable({
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)