"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/bl/bl.js" (7 Feb 2017, 5109 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var DuplexStream = require('readable-stream/duplex')
    2   , util         = require('util')
    3 
    4 function BufferList (callback) {
    5   if (!(this instanceof BufferList))
    6     return new BufferList(callback)
    7 
    8   this._bufs  = []
    9   this.length = 0
   10 
   11   if (typeof callback == 'function') {
   12     this._callback = callback
   13 
   14     var piper = function (err) {
   15       if (this._callback) {
   16         this._callback(err)
   17         this._callback = null
   18       }
   19     }.bind(this)
   20 
   21     this.on('pipe', function (src) {
   22       src.on('error', piper)
   23     })
   24     this.on('unpipe', function (src) {
   25       src.removeListener('error', piper)
   26     })
   27   }
   28   else if (Buffer.isBuffer(callback))
   29     this.append(callback)
   30   else if (Array.isArray(callback)) {
   31     callback.forEach(function (b) {
   32       Buffer.isBuffer(b) && this.append(b)
   33     }.bind(this))
   34   }
   35 
   36   DuplexStream.call(this)
   37 }
   38 
   39 util.inherits(BufferList, DuplexStream)
   40 
   41 BufferList.prototype._offset = function (offset) {
   42   var tot = 0, i = 0, _t
   43   for (; i < this._bufs.length; i++) {
   44     _t = tot + this._bufs[i].length
   45     if (offset < _t)
   46       return [ i, offset - tot ]
   47     tot = _t
   48   }
   49 }
   50 
   51 BufferList.prototype.append = function (buf) {
   52   var isBuffer = Buffer.isBuffer(buf) ||
   53                  buf instanceof BufferList
   54 
   55   // coerce number arguments to strings, since Buffer(number) does
   56   // uninitialized memory allocation
   57   if (typeof buf == 'number')
   58     buf = buf.toString()
   59 
   60   this._bufs.push(isBuffer ? buf : new Buffer(buf))
   61   this.length += buf.length
   62   return this
   63 }
   64 
   65 BufferList.prototype._write = function (buf, encoding, callback) {
   66   this.append(buf)
   67   if (callback)
   68     callback()
   69 }
   70 
   71 BufferList.prototype._read = function (size) {
   72   if (!this.length)
   73     return this.push(null)
   74   size = Math.min(size, this.length)
   75   this.push(this.slice(0, size))
   76   this.consume(size)
   77 }
   78 
   79 BufferList.prototype.end = function (chunk) {
   80   DuplexStream.prototype.end.call(this, chunk)
   81 
   82   if (this._callback) {
   83     this._callback(null, this.slice())
   84     this._callback = null
   85   }
   86 }
   87 
   88 BufferList.prototype.get = function (index) {
   89   return this.slice(index, index + 1)[0]
   90 }
   91 
   92 BufferList.prototype.slice = function (start, end) {
   93   return this.copy(null, 0, start, end)
   94 }
   95 
   96 BufferList.prototype.copy = function (dst, dstStart, srcStart, srcEnd) {
   97   if (typeof srcStart != 'number' || srcStart < 0)
   98     srcStart = 0
   99   if (typeof srcEnd != 'number' || srcEnd > this.length)
  100     srcEnd = this.length
  101   if (srcStart >= this.length)
  102     return dst || new Buffer(0)
  103   if (srcEnd <= 0)
  104     return dst || new Buffer(0)
  105 
  106   var copy   = !!dst
  107     , off    = this._offset(srcStart)
  108     , len    = srcEnd - srcStart
  109     , bytes  = len
  110     , bufoff = (copy && dstStart) || 0
  111     , start  = off[1]
  112     , l
  113     , i
  114 
  115   // copy/slice everything
  116   if (srcStart === 0 && srcEnd == this.length) {
  117     if (!copy) // slice, just return a full concat
  118       return Buffer.concat(this._bufs)
  119 
  120     // copy, need to copy individual buffers
  121     for (i = 0; i < this._bufs.length; i++) {
  122       this._bufs[i].copy(dst, bufoff)
  123       bufoff += this._bufs[i].length
  124     }
  125 
  126     return dst
  127   }
  128 
  129   // easy, cheap case where it's a subset of one of the buffers
  130   if (bytes <= this._bufs[off[0]].length - start) {
  131     return copy
  132       ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
  133       : this._bufs[off[0]].slice(start, start + bytes)
  134   }
  135 
  136   if (!copy) // a slice, we need something to copy in to
  137     dst = new Buffer(len)
  138 
  139   for (i = off[0]; i < this._bufs.length; i++) {
  140     l = this._bufs[i].length - start
  141 
  142     if (bytes > l) {
  143       this._bufs[i].copy(dst, bufoff, start)
  144     } else {
  145       this._bufs[i].copy(dst, bufoff, start, start + bytes)
  146       break
  147     }
  148 
  149     bufoff += l
  150     bytes -= l
  151 
  152     if (start)
  153       start = 0
  154   }
  155 
  156   return dst
  157 }
  158 
  159 BufferList.prototype.toString = function (encoding, start, end) {
  160   return this.slice(start, end).toString(encoding)
  161 }
  162 
  163 BufferList.prototype.consume = function (bytes) {
  164   while (this._bufs.length) {
  165     if (bytes > this._bufs[0].length) {
  166       bytes -= this._bufs[0].length
  167       this.length -= this._bufs[0].length
  168       this._bufs.shift()
  169     } else {
  170       this._bufs[0] = this._bufs[0].slice(bytes)
  171       this.length -= bytes
  172       break
  173     }
  174   }
  175   return this
  176 }
  177 
  178 BufferList.prototype.duplicate = function () {
  179   var i = 0
  180     , copy = new BufferList()
  181 
  182   for (; i < this._bufs.length; i++)
  183     copy.append(this._bufs[i])
  184 
  185   return copy
  186 }
  187 
  188 BufferList.prototype.destroy = function () {
  189   this._bufs.length = 0;
  190   this.length = 0;
  191   this.push(null);
  192 }
  193 
  194 ;(function () {
  195   var methods = {
  196       'readDoubleBE' : 8
  197     , 'readDoubleLE' : 8
  198     , 'readFloatBE'  : 4
  199     , 'readFloatLE'  : 4
  200     , 'readInt32BE'  : 4
  201     , 'readInt32LE'  : 4
  202     , 'readUInt32BE' : 4
  203     , 'readUInt32LE' : 4
  204     , 'readInt16BE'  : 2
  205     , 'readInt16LE'  : 2
  206     , 'readUInt16BE' : 2
  207     , 'readUInt16LE' : 2
  208     , 'readInt8'     : 1
  209     , 'readUInt8'    : 1
  210   }
  211 
  212   for (var m in methods) {
  213     (function (m) {
  214       BufferList.prototype[m] = function (offset) {
  215         return this.slice(offset, offset + methods[m])[m](0)
  216       }
  217     }(m))
  218   }
  219 }())
  220 
  221 module.exports = BufferList