Skip to content

Commit b6378f0

Browse files
committed
doc: stream.unshift does not reset reading state
Per nodejs/node-v0.x-archive#14604, Document that performing an `unshift` during a read can have unexpected results. Following the `unshift` with a `push('')` resets the reading state appropriately. Also indicate that doing an `unshift` during a read is not optimal and should be avoided. Reviewed-By: James M Snell <[email protected]> PR-URL: nodejs/node-v0.x-archive#25591
1 parent 5ad05af commit b6378f0

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

doc/api/stream.markdown

+24-13
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ function parseHeader(stream, callback) {
466466
}
467467
}
468468
```
469+
Note that, unlike `stream.push(chunk)`, `stream.unshift(chunk)` will not
470+
end the reading process by resetting the internal reading state of the
471+
stream. This can cause unexpected results if `unshift` is called during a
472+
read (i.e. from within a `_read` implementation on a custom stream). Following
473+
the call to `unshift` with an immediate `stream.push('')` will reset the
474+
reading state appropriately, however it is best to simply avoid calling
475+
`unshift` while in the process of performing a read.
469476

470477
#### readable.wrap(stream)
471478

@@ -905,6 +912,10 @@ SimpleProtocol.prototype._read = function(n) {
905912
// back into the read queue so that our consumer will see it.
906913
var b = chunk.slice(split);
907914
this.unshift(b);
915+
// calling unshift by itself does not reset the reading state
916+
// of the stream; since we're inside _read, doing an additional
917+
// push('') will reset the state appropriately.
918+
this.push('');
908919

909920
// and let them know that we are done parsing the header.
910921
this.emit('header', this.header);
@@ -946,19 +957,19 @@ initialized.
946957

947958
Note: **Implement this method, but do NOT call it directly.**
948959

949-
This method is prefixed with an underscore because it is internal to the
950-
class that defines it and should only be called by the internal Readable
951-
class methods. All Readable stream implementations must provide a _read
960+
This method is prefixed with an underscore because it is internal to the
961+
class that defines it and should only be called by the internal Readable
962+
class methods. All Readable stream implementations must provide a _read
952963
method to fetch data from the underlying resource.
953964

954-
When _read is called, if data is available from the resource, `_read` should
955-
start pushing that data into the read queue by calling `this.push(dataChunk)`.
956-
`_read` should continue reading from the resource and pushing data until push
957-
returns false, at which point it should stop reading from the resource. Only
958-
when _read is called again after it has stopped should it start reading
965+
When _read is called, if data is available from the resource, `_read` should
966+
start pushing that data into the read queue by calling `this.push(dataChunk)`.
967+
`_read` should continue reading from the resource and pushing data until push
968+
returns false, at which point it should stop reading from the resource. Only
969+
when _read is called again after it has stopped should it start reading
959970
more data from the resource and pushing that data onto the queue.
960971

961-
Note: once the `_read()` method is called, it will not be called again until
972+
Note: once the `_read()` method is called, it will not be called again until
962973
the `push` method is called.
963974

964975
The `size` argument is advisory. Implementations where a "read" is a
@@ -978,12 +989,12 @@ becomes available. There is no need, for example to "wait" until
978989
Note: **This method should be called by Readable implementors, NOT
979990
by consumers of Readable streams.**
980991

981-
If a value other than null is passed, The `push()` method adds a chunk of data
982-
into the queue for subsequent stream processors to consume. If `null` is
983-
passed, it signals the end of the stream (EOF), after which no more data
992+
If a value other than null is passed, The `push()` method adds a chunk of data
993+
into the queue for subsequent stream processors to consume. If `null` is
994+
passed, it signals the end of the stream (EOF), after which no more data
984995
can be written.
985996

986-
The data added with `push` can be pulled out by calling the `read()` method
997+
The data added with `push` can be pulled out by calling the `read()` method
987998
when the `'readable'`event fires.
988999

9891000
This API is designed to be as flexible as possible. For example,

0 commit comments

Comments
 (0)