[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const DuplexStream = require('readable-stream').Duplex
|
---|
| 4 | const inherits = require('inherits')
|
---|
| 5 | const BufferList = require('./BufferList')
|
---|
| 6 |
|
---|
| 7 | function BufferListStream (callback) {
|
---|
| 8 | if (!(this instanceof BufferListStream)) {
|
---|
| 9 | return new BufferListStream(callback)
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | if (typeof callback === 'function') {
|
---|
| 13 | this._callback = callback
|
---|
| 14 |
|
---|
| 15 | const piper = function piper (err) {
|
---|
| 16 | if (this._callback) {
|
---|
| 17 | this._callback(err)
|
---|
| 18 | this._callback = null
|
---|
| 19 | }
|
---|
| 20 | }.bind(this)
|
---|
| 21 |
|
---|
| 22 | this.on('pipe', function onPipe (src) {
|
---|
| 23 | src.on('error', piper)
|
---|
| 24 | })
|
---|
| 25 | this.on('unpipe', function onUnpipe (src) {
|
---|
| 26 | src.removeListener('error', piper)
|
---|
| 27 | })
|
---|
| 28 |
|
---|
| 29 | callback = null
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | BufferList._init.call(this, callback)
|
---|
| 33 | DuplexStream.call(this)
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | inherits(BufferListStream, DuplexStream)
|
---|
| 37 | Object.assign(BufferListStream.prototype, BufferList.prototype)
|
---|
| 38 |
|
---|
| 39 | BufferListStream.prototype._new = function _new (callback) {
|
---|
| 40 | return new BufferListStream(callback)
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | BufferListStream.prototype._write = function _write (buf, encoding, callback) {
|
---|
| 44 | this._appendBuffer(buf)
|
---|
| 45 |
|
---|
| 46 | if (typeof callback === 'function') {
|
---|
| 47 | callback()
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | BufferListStream.prototype._read = function _read (size) {
|
---|
| 52 | if (!this.length) {
|
---|
| 53 | return this.push(null)
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | size = Math.min(size, this.length)
|
---|
| 57 | this.push(this.slice(0, size))
|
---|
| 58 | this.consume(size)
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | BufferListStream.prototype.end = function end (chunk) {
|
---|
| 62 | DuplexStream.prototype.end.call(this, chunk)
|
---|
| 63 |
|
---|
| 64 | if (this._callback) {
|
---|
| 65 | this._callback(null, this.slice())
|
---|
| 66 | this._callback = null
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | BufferListStream.prototype._destroy = function _destroy (err, cb) {
|
---|
| 71 | this._bufs.length = 0
|
---|
| 72 | this.length = 0
|
---|
| 73 | cb(err)
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | BufferListStream.prototype._isBufferList = function _isBufferList (b) {
|
---|
| 77 | return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | BufferListStream.isBufferList = BufferList.isBufferList
|
---|
| 81 |
|
---|
| 82 | module.exports = BufferListStream
|
---|
| 83 | module.exports.BufferListStream = BufferListStream
|
---|
| 84 | module.exports.BufferList = BufferList
|
---|