All files index.js

100% Statements 101/101
98.67% Branches 74/75
100% Functions 19/19
100% Lines 98/98
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176  2x 2x 2x 2x 2x 2x 2x       18x 18x 18x 18x 18x 18x 1x 18x 18x 18x       34x 1x   33x 1x   33x 19x   33x 33x       33x 33x 1x         8x 8x 3x   5x 1x   5x   8x         5x 2x 3x 2x   1x 1x     3x 3x       11x 1x 11x 1x 11x 1x 11x 2x 11x 11x 6x       16x 16x 5x   11x       4x       5x   5x 4x       12x       4x 4x 4x       1x       23x 23x   23x 8x         2x       107x 11x 11x 11x         96x 26x 11x   26x 3x   23x 7x 70x 11x 6x 6x 2x 2x     11x 2x 2x   11x     93x 93x   93x 82x         2x  
'use strict'
const EE = require('events')
const Yallist = require('yallist')
const EOF = Symbol('EOF')
const MAYBE_EMIT_END = Symbol('maybeEmitEnd')
const EMITTED_END = Symbol('emittedEnd')
const READ = Symbol('read')
const SD = require('string_decoder').StringDecoder
 
class MiniPass extends EE {
  constructor (options) {
    super()
    this.flowing = false
    this.pipes = new Yallist()
    this.buffer = new Yallist()
    this.encoding = options && options.encoding || null
    if (this.encoding === 'buffer')
      this.encoding = null
    this.decoder = this.encoding ? new SD(this.encoding) : null
    this[EOF] = false
    this[EMITTED_END] = false
  }
 
  write (chunk, encoding, cb) {
    if (this[EOF])
      throw new Error('write after end')
 
    if (typeof encoding === 'function')
      cb = encoding, encoding = 'utf8'
 
    if (typeof chunk === 'string')
      chunk = new Buffer(chunk, encoding)
 
    try {
      return this.flowing
        ? (this.emit('data', chunk), true)
        : (this.buffer.push(chunk), false)
    } finally {
      this.emit('readable')
      if (cb)
        cb()
    }
  }
 
  read (n) {
    try {
      if (!this.buffer.length || n === 0)
        return null
 
      if (this.buffer.length > 1)
        this.buffer = new Yallist([Buffer.concat(Array.from(this.buffer))])
 
      return this[READ](n, this.buffer.head.value)
    } finally {
      this[MAYBE_EMIT_END]()
    }
  }
 
  [READ] (n, chunk) {
    if (n > chunk.length)
      return null
    else if (n === chunk.length)
      this.buffer.pop()
    else {
      this.buffer.head.value = chunk.slice(n)
      chunk = chunk.slice(0, n)
    }
 
    this.emit('data', chunk)
    return chunk
  }
 
  end (chunk, encoding, cb) {
    if (typeof chunk === 'function')
      cb = chunk, chunk = null
    if (typeof encoding === 'function')
      cb = encoding, encoding = 'utf8'
    if (chunk)
      this.write(chunk, encoding)
    if (cb)
      this.once('end', cb)
    this[EOF] = true
    if (this.flowing)
      this[MAYBE_EMIT_END]()
  }
 
  resume () {
    this.flowing = true
    if (this.buffer.length)
      this.flush()
    else
      this[MAYBE_EMIT_END]()
  }
 
  pause () {
    this.flowing = false
  }
 
  flush () {
    do {} while (this.flushChunk(this.buffer.shift()))
 
    if (!this.buffer.length)
      this.emit('drain')
  }
 
  flushChunk (chunk) {
    return chunk ? (this.emit('data', chunk), this.flowing) : false
  }
 
  pipe (dest) {
    this.pipes.push(dest)
    dest.on('drain', _ => this.resume())
    this.resume()
  }
 
  addEventHandler (ev, fn) {
    return this.on(ev, fn)
  }
 
  on (ev, fn) {
    try {
      return super.on(ev, fn)
    } finally {
      if (ev === 'data' && !this.pipes.length && !this.flowing)
        this.resume()
    }
  }
 
  get emittedEnd () {
    return this[EMITTED_END]
  }
 
  [MAYBE_EMIT_END] () {
    if (!this[EMITTED_END] && this.buffer.length === 0 && this[EOF]) {
      this.emit('end')
      this.emit('finished')
      this.emit('close')
    }
  }
 
  emit (ev, data, ...args) {
    if (ev === 'data') {
      if (this.decoder)
        data = this.decoder.write(data)
 
      if (!data)
        return
 
      if (this.pipes.length)
        this.pipes.forEach(dest => dest.write(data) || this.pause())
    } else if (ev === 'end') {
      if (this.decoder) {
        data = this.decoder.end()
        if (data) {
          this.pipes.forEach(dest => dest.write(data))
          super.emit('data', data)
        }
      }
      this.pipes.forEach(dest => {
        Eif (dest !== process.stdout && dest !== process.stderr)
          dest.end()
      })
      this[EMITTED_END] = true
    }
 
    try {
      return super.emit(ev, data, ...args)
    } finally {
      if (ev !== 'end')
        this[MAYBE_EMIT_END]()
    }
  }
}
 
module.exports = MiniPass