Skip to content

Commit a72a331

Browse files
Zwbjasnell
Zwb
authored andcommitted
doc: fix onReadable reentry after unshift called
In example parseHeader, stream listen **readable** event, if call stream.unshift(buf) before stream.removeListener('readable', onReadable), readable event will be emited before removeListener, so callback will not been called correctlly. After change to ```js stream.removeListener('error', callback); stream.removeListener('readable', onReadable); if (buf.length) stream.unshift(buf); ``` It solves this problem. PR-URL: #8200 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent f4aa2c2 commit a72a331

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

doc/api/stream.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,11 @@ function parseHeader(stream, callback) {
963963
header += split.shift();
964964
const remaining = split.join('\n\n');
965965
const buf = Buffer.from(remaining, 'utf8');
966-
if (buf.length)
967-
stream.unshift(buf);
968966
stream.removeListener('error', callback);
967+
// set the readable listener before unshifting
969968
stream.removeListener('readable', onReadable);
969+
if (buf.length)
970+
stream.unshift(buf);
970971
// now the body of the message can be read from the stream.
971972
callback(null, header, stream);
972973
} else {

0 commit comments

Comments
 (0)