@@ -164,6 +164,34 @@ readable.on('readable', function() { |
164 | 164 | Once the internal buffer is drained, a `readable` event will fire |
165 | 165 | again when more data is available. |
166 | 166 |
|
| 167 | +The `readable` event is not emitted in the "flowing" mode with the |
| 168 | +sole exception of the last one, on end-of-stream. |
| 169 | + |
| 170 | +The 'readable' event indicates that the stream has new information: |
| 171 | +either new data is available or the end of the stream has been reached. |
| 172 | +In the former case, `.read()` will return that data. In the latter case, |
| 173 | +`.read()` will return null. For instance, in the following example, `foo.txt` |
| 174 | +is an empty file: |
| 175 | + |
| 176 | +```javascript |
| 177 | +var fs = require('fs'); |
| 178 | +var rr = fs.createReadStream('foo.txt'); |
| 179 | +rr.on('readable', function() { |
| 180 | + console.log('readable:', rr.read()); |
| 181 | +}); |
| 182 | +rr.on('end', function() { |
| 183 | + console.log('end'); |
| 184 | +}); |
| 185 | +``` |
| 186 | + |
| 187 | +The output of running this script is: |
| 188 | + |
| 189 | +``` |
| 190 | +bash-3.2$ node test.js |
| 191 | +readable: null |
| 192 | +end |
| 193 | +``` |
| 194 | + |
167 | 195 | #### Event: 'data' |
168 | 196 |
|
169 | 197 | * `chunk` {Buffer | String} The chunk of data. |
@@ -221,7 +249,9 @@ returns it. If there is no data available, then it will return |
221 | 249 | `null`. |
222 | 250 |
|
223 | 251 | If you pass in a `size` argument, then it will return that many |
224 | | -bytes. If `size` bytes are not available, then it will return `null`. |
| 252 | +bytes. If `size` bytes are not available, then it will return `null`, |
| 253 | +unless we've ended, in which case it will return the data remaining |
| 254 | +in the buffer. |
225 | 255 |
|
226 | 256 | If you do not specify a `size` argument, then it will return all the |
227 | 257 | data in the internal buffer. |
@@ -243,6 +273,9 @@ readable.on('readable', function() { |
243 | 273 | If this method returns a data chunk, then it will also trigger the |
244 | 274 | emission of a [`'data'` event][]. |
245 | 275 |
|
| 276 | +Note that calling `readable.read([size])` after the `end` event has been |
| 277 | +triggered will return `null`. No runtime error will be raised. |
| 278 | + |
246 | 279 | #### readable.setEncoding(encoding) |
247 | 280 |
|
248 | 281 | * `encoding` {String} The encoding to use. |
@@ -414,6 +447,9 @@ parser, which needs to "un-consume" some data that it has |
414 | 447 | optimistically pulled out of the source, so that the stream can be |
415 | 448 | passed on to some other party. |
416 | 449 |
|
| 450 | +Note that `stream.unshift(chunk)` cannot be called after the `end` event |
| 451 | +has been triggered; a runtime error will be raised. |
| 452 | + |
417 | 453 | If you find that you must often call `stream.unshift(chunk)` in your |
418 | 454 | programs, consider implementing a [Transform][] stream instead. (See API |
419 | 455 | for Stream Implementors, below.) |
@@ -452,6 +488,13 @@ function parseHeader(stream, callback) { |
452 | 488 | } |
453 | 489 | } |
454 | 490 | ``` |
| 491 | +Note that, unlike `stream.push(chunk)`, `stream.unshift(chunk)` will not |
| 492 | +end the reading process by resetting the internal reading state of the |
| 493 | +stream. This can cause unexpected results if `unshift` is called during a |
| 494 | +read (i.e. from within a `_read` implementation on a custom stream). Following |
| 495 | +the call to `unshift` with an immediate `stream.push('')` will reset the |
| 496 | +reading state appropriately, however it is best to simply avoid calling |
| 497 | +`unshift` while in the process of performing a read. |
455 | 498 |
|
456 | 499 | #### readable.wrap(stream) |
457 | 500 |
|
@@ -883,6 +926,10 @@ SimpleProtocol.prototype._read = function(n) { |
883 | 926 | // back into the read queue so that our consumer will see it. |
884 | 927 | var b = chunk.slice(split); |
885 | 928 | this.unshift(b); |
| 929 | + // calling unshift by itself does not reset the reading state |
| 930 | + // of the stream; since we're inside _read, doing an additional |
| 931 | + // push('') will reset the state appropriately. |
| 932 | + this.push(''); |
886 | 933 |
|
887 | 934 | // and let them know that we are done parsing the header. |
888 | 935 | this.emit('header', this.header); |
@@ -922,24 +969,22 @@ initialized. |
922 | 969 |
|
923 | 970 | * `size` {Number} Number of bytes to read asynchronously |
924 | 971 |
|
925 | | -Note: **Implement this function, but do NOT call it directly.** |
| 972 | +Note: **Implement this method, but do NOT call it directly.** |
926 | 973 |
|
927 | | -This function should NOT be called directly. It should be implemented |
928 | | -by child classes, and only called by the internal Readable class |
929 | | -methods. |
| 974 | +This method is prefixed with an underscore because it is internal to the |
| 975 | +class that defines it and should only be called by the internal Readable |
| 976 | +class methods. All Readable stream implementations must provide a _read |
| 977 | +method to fetch data from the underlying resource. |
930 | 978 |
|
931 | | -All Readable stream implementations must provide a `_read` method to |
932 | | -fetch data from the underlying resource. |
933 | | - |
934 | | -This method is prefixed with an underscore because it is internal to |
935 | | -the class that defines it, and should not be called directly by user |
936 | | -programs. However, you **are** expected to override this method in |
937 | | -your own extension classes. |
| 979 | +When _read is called, if data is available from the resource, `_read` should |
| 980 | +start pushing that data into the read queue by calling `this.push(dataChunk)`. |
| 981 | +`_read` should continue reading from the resource and pushing data until push |
| 982 | +returns false, at which point it should stop reading from the resource. Only |
| 983 | +when _read is called again after it has stopped should it start reading |
| 984 | +more data from the resource and pushing that data onto the queue. |
938 | 985 |
|
939 | | -When data is available, put it into the read queue by calling |
940 | | -`readable.push(chunk)`. If `push` returns false, then you should stop |
941 | | -reading. When `_read` is called again, you should start pushing more |
942 | | -data. |
| 986 | +Note: once the `_read()` method is called, it will not be called again until |
| 987 | +the `push` method is called. |
943 | 988 |
|
944 | 989 | The `size` argument is advisory. Implementations where a "read" is a |
945 | 990 | single call that returns data can use this to know how much data to |
@@ -955,19 +1000,16 @@ becomes available. There is no need, for example to "wait" until |
955 | 1000 | Buffer encoding, such as `'utf8'` or `'ascii'` |
956 | 1001 | * return {Boolean} Whether or not more pushes should be performed |
957 | 1002 |
|
958 | | -Note: **This function should be called by Readable implementors, NOT |
| 1003 | +Note: **This method should be called by Readable implementors, NOT |
959 | 1004 | by consumers of Readable streams.** |
960 | 1005 |
|
961 | | -The `_read()` function will not be called again until at least one |
962 | | -`push(chunk)` call is made. |
963 | | - |
964 | | -The `Readable` class works by putting data into a read queue to be |
965 | | -pulled out later by calling the `read()` method when the `'readable'` |
966 | | -event fires. |
| 1006 | +If a value other than null is passed, The `push()` method adds a chunk of data |
| 1007 | +into the queue for subsequent stream processors to consume. If `null` is |
| 1008 | +passed, it signals the end of the stream (EOF), after which no more data |
| 1009 | +can be written. |
967 | 1010 |
|
968 | | -The `push()` method will explicitly insert some data into the read |
969 | | -queue. If it is called with `null` then it will signal the end of the |
970 | | -data (EOF). |
| 1011 | +The data added with `push` can be pulled out by calling the `read()` method |
| 1012 | +when the `'readable'`event fires. |
971 | 1013 |
|
972 | 1014 | This API is designed to be as flexible as possible. For example, |
973 | 1015 | you may be wrapping a lower-level source which has some sort of |
@@ -1315,7 +1357,7 @@ for examples and testing, but there are occasionally use cases where |
1315 | 1357 | it can come in handy as a building block for novel sorts of streams. |
1316 | 1358 |
|
1317 | 1359 |
|
1318 | | -## Simplified Constructor API |
| 1360 | +## Simplified Constructor API |
1319 | 1361 |
|
1320 | 1362 | <!--type=misc--> |
1321 | 1363 |
|
|