| 164 | } |
| 165 | |
| 166 | function onStreamRead(arrayBuffer) { |
| 167 | const nread = streamBaseState[kReadBytesOrError]; |
| 168 | |
| 169 | const handle = this; |
| 170 | const stream = this[owner_symbol]; |
| 171 | |
| 172 | stream[kUpdateTimer](); |
| 173 | |
| 174 | if (nread > 0 && !stream.destroyed) { |
| 175 | let ret; |
| 176 | let result; |
| 177 | const userBuf = stream[kBuffer]; |
| 178 | if (userBuf) { |
| 179 | result = (stream[kBufferCb](nread, userBuf) !== false); |
| 180 | const bufGen = stream[kBufferGen]; |
| 181 | if (bufGen !== null) { |
| 182 | const nextBuf = bufGen(); |
| 183 | if (isUint8Array(nextBuf)) |
| 184 | stream[kBuffer] = ret = nextBuf; |
| 185 | } |
| 186 | } else { |
| 187 | const offset = streamBaseState[kArrayBufferOffset]; |
| 188 | const buf = new FastBuffer(arrayBuffer, offset, nread); |
| 189 | result = stream.push(buf); |
| 190 | } |
| 191 | if (!result) { |
| 192 | handle.reading = false; |
| 193 | if (!stream.destroyed) { |
| 194 | const err = handle.readStop(); |
| 195 | if (err) |
| 196 | stream.destroy(new ErrnoException(err, 'read')); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | if (nread === 0) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // After seeing EOF, most streams will be closed permanently, |
| 208 | // and will not deliver any more read events after this point. |
| 209 | // (equivalently, it should have called readStop on itself already). |
| 210 | // Some streams may be reset and explicitly started again with a call |
| 211 | // to readStart, such as TTY. |
| 212 | |
| 213 | if (nread !== UV_EOF) { |
| 214 | // CallJSOnreadMethod expects the return value to be a buffer. |
| 215 | // Ref: https://github.com/nodejs/node/pull/34375 |
| 216 | stream.destroy(new ErrnoException(nread, 'read')); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // Defer this until we actually emit end |
| 221 | if (stream._readableState.endEmitted) { |
| 222 | if (stream[kMaybeDestroy]) |
| 223 | stream[kMaybeDestroy](); |