AK-21/Graphite-Industrial-Intelligence
0
1'use strict'2 3const DuplexStream = require('readable-stream').Duplex4const inherits = require('inherits')5const BufferList = require('./BufferList')6 7function BufferListStream (callback) {8 if (!(this instanceof BufferListStream)) {9 return new BufferListStream(callback)10 }11 12 if (typeof callback === 'function') {13 this._callback = callback14 15 const piper = function piper (err) {16 if (this._callback) {17 this._callback(err)18 this._callback = null19 }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 = null30 }31 32 BufferList._init.call(this, callback)33 DuplexStream.call(this)34}35 36inherits(BufferListStream, DuplexStream)37Object.assign(BufferListStream.prototype, BufferList.prototype)38 39BufferListStream.prototype._new = function _new (callback) {40 return new BufferListStream(callback)41}42 43BufferListStream.prototype._write = function _write (buf, encoding, callback) {44 this._appendBuffer(buf)45 46 if (typeof callback === 'function') {47 callback()48 }49}50 51BufferListStream.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 61BufferListStream.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 = null67 }68}69 70BufferListStream.prototype._destroy = function _destroy (err, cb) {71 this._bufs.length = 072 this.length = 073 cb(err)74}75 76BufferListStream.prototype._isBufferList = function _isBufferList (b) {77 return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)78}79 80BufferListStream.isBufferList = BufferList.isBufferList81 82module.exports = BufferListStream83module.exports.BufferListStream = BufferListStream84module.exports.BufferList = BufferList85 