@@ -466,6 +466,13 @@ function parseHeader(stream, callback) {
466
466
}
467
467
}
468
468
```
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.
469
476
470
477
#### readable.wrap(stream)
471
478
@@ -905,6 +912,10 @@ SimpleProtocol.prototype._read = function(n) {
905
912
// back into the read queue so that our consumer will see it.
906
913
var b = chunk .slice (split);
907
914
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 (' ' );
908
919
909
920
// and let them know that we are done parsing the header.
910
921
this .emit (' header' , this .header );
@@ -946,19 +957,19 @@ initialized.
946
957
947
958
Note: ** Implement this method, but do NOT call it directly.**
948
959
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
952
963
method to fetch data from the underlying resource.
953
964
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
959
970
more data from the resource and pushing that data onto the queue.
960
971
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
962
973
the ` push ` method is called.
963
974
964
975
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
978
989
Note: ** This method should be called by Readable implementors, NOT
979
990
by consumers of Readable streams.**
980
991
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
984
995
can be written.
985
996
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
987
998
when the ` 'readable' ` event fires.
988
999
989
1000
This API is designed to be as flexible as possible. For example,
0 commit comments