"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/concat-stream/index.js" (8 Mar 2017, 3580 Bytes) of package /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 Writable = require('readable-stream').Writable
    2 var inherits = require('inherits')
    3 
    4 if (typeof Uint8Array === 'undefined') {
    5   var U8 = require('typedarray').Uint8Array
    6 } else {
    7   var U8 = Uint8Array
    8 }
    9 
   10 function ConcatStream(opts, cb) {
   11   if (!(this instanceof ConcatStream)) return new ConcatStream(opts, cb)
   12 
   13   if (typeof opts === 'function') {
   14     cb = opts
   15     opts = {}
   16   }
   17   if (!opts) opts = {}
   18 
   19   var encoding = opts.encoding
   20   var shouldInferEncoding = false
   21 
   22   if (!encoding) {
   23     shouldInferEncoding = true
   24   } else {
   25     encoding =  String(encoding).toLowerCase()
   26     if (encoding === 'u8' || encoding === 'uint8') {
   27       encoding = 'uint8array'
   28     }
   29   }
   30 
   31   Writable.call(this, { objectMode: true })
   32 
   33   this.encoding = encoding
   34   this.shouldInferEncoding = shouldInferEncoding
   35 
   36   if (cb) this.on('finish', function () { cb(this.getBody()) })
   37   this.body = []
   38 }
   39 
   40 module.exports = ConcatStream
   41 inherits(ConcatStream, Writable)
   42 
   43 ConcatStream.prototype._write = function(chunk, enc, next) {
   44   this.body.push(chunk)
   45   next()
   46 }
   47 
   48 ConcatStream.prototype.inferEncoding = function (buff) {
   49   var firstBuffer = buff === undefined ? this.body[0] : buff;
   50   if (Buffer.isBuffer(firstBuffer)) return 'buffer'
   51   if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array'
   52   if (Array.isArray(firstBuffer)) return 'array'
   53   if (typeof firstBuffer === 'string') return 'string'
   54   if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object'
   55   return 'buffer'
   56 }
   57 
   58 ConcatStream.prototype.getBody = function () {
   59   if (!this.encoding && this.body.length === 0) return []
   60   if (this.shouldInferEncoding) this.encoding = this.inferEncoding()
   61   if (this.encoding === 'array') return arrayConcat(this.body)
   62   if (this.encoding === 'string') return stringConcat(this.body)
   63   if (this.encoding === 'buffer') return bufferConcat(this.body)
   64   if (this.encoding === 'uint8array') return u8Concat(this.body)
   65   return this.body
   66 }
   67 
   68 var isArray = Array.isArray || function (arr) {
   69   return Object.prototype.toString.call(arr) == '[object Array]'
   70 }
   71 
   72 function isArrayish (arr) {
   73   return /Array\]$/.test(Object.prototype.toString.call(arr))
   74 }
   75 
   76 function stringConcat (parts) {
   77   var strings = []
   78   var needsToString = false
   79   for (var i = 0; i < parts.length; i++) {
   80     var p = parts[i]
   81     if (typeof p === 'string') {
   82       strings.push(p)
   83     } else if (Buffer.isBuffer(p)) {
   84       strings.push(p)
   85     } else {
   86       strings.push(Buffer(p))
   87     }
   88   }
   89   if (Buffer.isBuffer(parts[0])) {
   90     strings = Buffer.concat(strings)
   91     strings = strings.toString('utf8')
   92   } else {
   93     strings = strings.join('')
   94   }
   95   return strings
   96 }
   97 
   98 function bufferConcat (parts) {
   99   var bufs = []
  100   for (var i = 0; i < parts.length; i++) {
  101     var p = parts[i]
  102     if (Buffer.isBuffer(p)) {
  103       bufs.push(p)
  104     } else if (typeof p === 'string' || isArrayish(p)
  105     || (p && typeof p.subarray === 'function')) {
  106       bufs.push(Buffer(p))
  107     } else bufs.push(Buffer(String(p)))
  108   }
  109   return Buffer.concat(bufs)
  110 }
  111 
  112 function arrayConcat (parts) {
  113   var res = []
  114   for (var i = 0; i < parts.length; i++) {
  115     res.push.apply(res, parts[i])
  116   }
  117   return res
  118 }
  119 
  120 function u8Concat (parts) {
  121   var len = 0
  122   for (var i = 0; i < parts.length; i++) {
  123     if (typeof parts[i] === 'string') {
  124       parts[i] = Buffer(parts[i])
  125     }
  126     len += parts[i].length
  127   }
  128   var u8 = new U8(len)
  129   for (var i = 0, offset = 0; i < parts.length; i++) {
  130     var part = parts[i]
  131     for (var j = 0; j < part.length; j++) {
  132       u8[offset++] = part[j]
  133     }
  134   }
  135   return u8
  136 }