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: https://github.com/nodejs/node/pull/8200 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
f4aa2c2c93
commit
a72a331536
@ -963,10 +963,11 @@ function parseHeader(stream, callback) {
|
|||||||
header += split.shift();
|
header += split.shift();
|
||||||
const remaining = split.join('\n\n');
|
const remaining = split.join('\n\n');
|
||||||
const buf = Buffer.from(remaining, 'utf8');
|
const buf = Buffer.from(remaining, 'utf8');
|
||||||
|
stream.removeListener('error', callback);
|
||||||
|
// set the readable listener before unshifting
|
||||||
|
stream.removeListener('readable', onReadable);
|
||||||
if (buf.length)
|
if (buf.length)
|
||||||
stream.unshift(buf);
|
stream.unshift(buf);
|
||||||
stream.removeListener('error', callback);
|
|
||||||
stream.removeListener('readable', onReadable);
|
|
||||||
// now the body of the message can be read from the stream.
|
// now the body of the message can be read from the stream.
|
||||||
callback(null, header, stream);
|
callback(null, header, stream);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user