react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / bl / bl.js
81145 viewsvar DuplexStream = require('readable-stream/duplex')1, util = require('util')23function BufferList (callback) {4if (!(this instanceof BufferList))5return new BufferList(callback)67this._bufs = []8this.length = 0910if (typeof callback == 'function') {11this._callback = callback1213var piper = function (err) {14if (this._callback) {15this._callback(err)16this._callback = null17}18}.bind(this)1920this.on('pipe', function (src) {21src.on('error', piper)22})23this.on('unpipe', function (src) {24src.removeListener('error', piper)25})26}27else if (Buffer.isBuffer(callback))28this.append(callback)29else if (Array.isArray(callback)) {30callback.forEach(function (b) {31Buffer.isBuffer(b) && this.append(b)32}.bind(this))33}3435DuplexStream.call(this)36}3738util.inherits(BufferList, DuplexStream)3940BufferList.prototype._offset = function (offset) {41var tot = 0, i = 0, _t42for (; i < this._bufs.length; i++) {43_t = tot + this._bufs[i].length44if (offset < _t)45return [ i, offset - tot ]46tot = _t47}48}4950BufferList.prototype.append = function (buf) {51var isBuffer = Buffer.isBuffer(buf) ||52buf instanceof BufferList5354this._bufs.push(isBuffer ? buf : new Buffer(buf))55this.length += buf.length56return this57}5859BufferList.prototype._write = function (buf, encoding, callback) {60this.append(buf)61if (callback)62callback()63}6465BufferList.prototype._read = function (size) {66if (!this.length)67return this.push(null)68size = Math.min(size, this.length)69this.push(this.slice(0, size))70this.consume(size)71}7273BufferList.prototype.end = function (chunk) {74DuplexStream.prototype.end.call(this, chunk)7576if (this._callback) {77this._callback(null, this.slice())78this._callback = null79}80}8182BufferList.prototype.get = function (index) {83return this.slice(index, index + 1)[0]84}8586BufferList.prototype.slice = function (start, end) {87return this.copy(null, 0, start, end)88}8990BufferList.prototype.copy = function (dst, dstStart, srcStart, srcEnd) {91if (typeof srcStart != 'number' || srcStart < 0)92srcStart = 093if (typeof srcEnd != 'number' || srcEnd > this.length)94srcEnd = this.length95if (srcStart >= this.length)96return dst || new Buffer(0)97if (srcEnd <= 0)98return dst || new Buffer(0)99100var copy = !!dst101, off = this._offset(srcStart)102, len = srcEnd - srcStart103, bytes = len104, bufoff = (copy && dstStart) || 0105, start = off[1]106, l107, i108109// copy/slice everything110if (srcStart === 0 && srcEnd == this.length) {111if (!copy) // slice, just return a full concat112return Buffer.concat(this._bufs)113114// copy, need to copy individual buffers115for (i = 0; i < this._bufs.length; i++) {116this._bufs[i].copy(dst, bufoff)117bufoff += this._bufs[i].length118}119120return dst121}122123// easy, cheap case where it's a subset of one of the buffers124if (bytes <= this._bufs[off[0]].length - start) {125return copy126? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)127: this._bufs[off[0]].slice(start, start + bytes)128}129130if (!copy) // a slice, we need something to copy in to131dst = new Buffer(len)132133for (i = off[0]; i < this._bufs.length; i++) {134l = this._bufs[i].length - start135136if (bytes > l) {137this._bufs[i].copy(dst, bufoff, start)138} else {139this._bufs[i].copy(dst, bufoff, start, start + bytes)140break141}142143bufoff += l144bytes -= l145146if (start)147start = 0148}149150return dst151}152153BufferList.prototype.toString = function (encoding, start, end) {154return this.slice(start, end).toString(encoding)155}156157BufferList.prototype.consume = function (bytes) {158while (this._bufs.length) {159if (bytes > this._bufs[0].length) {160bytes -= this._bufs[0].length161this.length -= this._bufs[0].length162this._bufs.shift()163} else {164this._bufs[0] = this._bufs[0].slice(bytes)165this.length -= bytes166break167}168}169return this170}171172BufferList.prototype.duplicate = function () {173var i = 0174, copy = new BufferList()175176for (; i < this._bufs.length; i++)177copy.append(this._bufs[i])178179return copy180}181182BufferList.prototype.destroy = function () {183this._bufs.length = 0;184this.length = 0;185this.push(null);186}187188;(function () {189var methods = {190'readDoubleBE' : 8191, 'readDoubleLE' : 8192, 'readFloatBE' : 4193, 'readFloatLE' : 4194, 'readInt32BE' : 4195, 'readInt32LE' : 4196, 'readUInt32BE' : 4197, 'readUInt32LE' : 4198, 'readInt16BE' : 2199, 'readInt16LE' : 2200, 'readUInt16BE' : 2201, 'readUInt16LE' : 2202, 'readInt8' : 1203, 'readUInt8' : 1204}205206for (var m in methods) {207(function (m) {208BufferList.prototype[m] = function (offset) {209return this.slice(offset, offset + methods[m])[m](0)210}211}(m))212}213}())214215module.exports = BufferList216217218