| 306 | // event close - optional, indicates upstream close |
| 307 | // event error - duh |
| 308 | read(size) { |
| 309 | let buffers; |
| 310 | // read min(buffer, size || infinity) |
| 311 | if (size) { |
| 312 | buffers = []; |
| 313 | while (size && this.buffers.length && !this.buffers[0].eod) { |
| 314 | const first = this.buffers[0]; |
| 315 | const buffer = first.read(size); |
| 316 | size -= buffer.length; |
| 317 | buffers.push(buffer); |
| 318 | if (first.eod && first.full) { |
| 319 | this.buffers.shift(); |
| 320 | } |
| 321 | } |
| 322 | return Buffer.concat(buffers); |
| 323 | } |
| 324 | |
| 325 | buffers = this.buffers.map(buf => buf.toBuffer()).filter(Boolean); |
| 326 | this.buffers = []; |
| 327 | return Buffer.concat(buffers); |
| 328 | }, |
| 329 | setEncoding(encoding) { |
| 330 | // causes stream.read or stream.on('data) to return strings of encoding instead of Buffer objects |
| 331 | this.encoding = encoding; |