| 876 | } |
| 877 | |
| 878 | function maybeReadMore_(stream, state) { |
| 879 | // Attempt to read more data if we should. |
| 880 | // |
| 881 | // The conditions for reading more data are (one of): |
| 882 | // - Not enough data buffered (state.length < state.highWaterMark). The loop |
| 883 | // is responsible for filling the buffer with enough data if such data |
| 884 | // is available. If highWaterMark is 0 and we are not in the flowing mode |
| 885 | // we should _not_ attempt to buffer any extra data. We'll get more data |
| 886 | // when the stream consumer calls read() instead. |
| 887 | // - No data in the buffer, and the stream is in flowing mode. In this mode |
| 888 | // the loop below is responsible for ensuring read() is called. Failing to |
| 889 | // call read here would abort the flow and there's no other mechanism for |
| 890 | // continuing the flow if the stream consumer has just subscribed to the |
| 891 | // 'data' event. |
| 892 | // |
| 893 | // In addition to the above conditions to keep reading data, the following |
| 894 | // conditions prevent the data from being read: |
| 895 | // - The stream has ended (state.ended). |
| 896 | // - There is already a pending 'read' operation (state.reading). This is a |
| 897 | // case where the stream has called the implementation defined _read() |
| 898 | // method, but they are processing the call asynchronously and have _not_ |
| 899 | // called push() with new data. In this case we skip performing more |
| 900 | // read()s. The execution ends in this method again after the _read() ends |
| 901 | // up calling push() with more data. |
| 902 | while ((state[kState] & (kReading | kEnded)) === 0 && |
| 903 | (state.length < state.highWaterMark || |
| 904 | ((state[kState] & kFlowing) !== 0 && state.length === 0))) { |
| 905 | const len = state.length; |
| 906 | debug('maybeReadMore read 0'); |
| 907 | stream.read(0); |
| 908 | if (len === state.length) |
| 909 | // Didn't get any data, stop spinning. |
| 910 | break; |
| 911 | } |
| 912 | state[kState] &= ~kReadingMore; |
| 913 | } |
| 914 | |
| 915 | // Abstract method. to be overridden in specific implementation classes. |
| 916 | // call cb(er, data) where data is <= n in length. |