forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: always reset awaitDrain when emitting data
The complicated `awaitDrain` machinery can be made a bit slimmer, and more correct, by just resetting the value each time `stream.emit('data')` is called. By resetting the value before emitting the data chunk, and seeing whether any pipe destinations return `.write() === false`, we always end up in a consistent state and don’t need to worry about odd situations (like `dest.write(chunk)` emitting more data). PR-URL: nodejs#18516 Fixes: nodejs#18484 Fixes: nodejs#18512 Refs: nodejs#18515 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
- Loading branch information
Showing
2 changed files
with
38 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const stream = require('stream'); | ||
|
||
function test(throwCodeInbetween) { | ||
// Check that a pipe does not stall if .read() is called unexpectedly | ||
// (i.e. the stream is not resumed by the pipe). | ||
|
||
const n = 1000; | ||
let counter = n; | ||
const rs = stream.Readable({ | ||
objectMode: true, | ||
read: common.mustCallAtLeast(() => { | ||
if (--counter >= 0) | ||
rs.push({ counter }); | ||
else | ||
rs.push(null); | ||
}, n) | ||
}); | ||
|
||
const ws = stream.Writable({ | ||
objectMode: true, | ||
write: common.mustCall((data, enc, cb) => { | ||
setImmediate(cb); | ||
}, n) | ||
}); | ||
|
||
setImmediate(() => throwCodeInbetween(rs, ws)); | ||
|
||
rs.pipe(ws); | ||
} | ||
|
||
test((rs) => rs.read()); | ||
test((rs) => rs.resume()); | ||
test(() => 0); |