/node_modules/minipass/index.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 304 lines · 252 code · 43 blank · 9 comment · 76 complexity · e8c90e209dd3dcbc2d870c78bbc968f1 MD5 · raw file

  1. 'use strict'
  2. const EE = require('events')
  3. const Yallist = require('yallist')
  4. const EOF = Symbol('EOF')
  5. const MAYBE_EMIT_END = Symbol('maybeEmitEnd')
  6. const EMITTED_END = Symbol('emittedEnd')
  7. const CLOSED = Symbol('closed')
  8. const READ = Symbol('read')
  9. const FLUSH = Symbol('flush')
  10. const FLUSHCHUNK = Symbol('flushChunk')
  11. const SD = require('string_decoder').StringDecoder
  12. const ENCODING = Symbol('encoding')
  13. const DECODER = Symbol('decoder')
  14. const FLOWING = Symbol('flowing')
  15. const RESUME = Symbol('resume')
  16. const BUFFERLENGTH = Symbol('bufferLength')
  17. const BUFFERPUSH = Symbol('bufferPush')
  18. const BUFFERSHIFT = Symbol('bufferShift')
  19. const OBJECTMODE = Symbol('objectMode')
  20. // Buffer in node 4.x < 4.5.0 doesn't have working Buffer.from
  21. // or Buffer.alloc, and Buffer in node 10 deprecated the ctor.
  22. // .M, this is fine .\^/M..
  23. let B = Buffer
  24. /* istanbul ignore next */
  25. if (!B.alloc) {
  26. B = require('safe-buffer').Buffer
  27. }
  28. class MiniPass extends EE {
  29. constructor (options) {
  30. super()
  31. this[FLOWING] = false
  32. this.pipes = new Yallist()
  33. this.buffer = new Yallist()
  34. this[OBJECTMODE] = options && options.objectMode || false
  35. if (this[OBJECTMODE])
  36. this[ENCODING] = null
  37. else
  38. this[ENCODING] = options && options.encoding || null
  39. if (this[ENCODING] === 'buffer')
  40. this[ENCODING] = null
  41. this[DECODER] = this[ENCODING] ? new SD(this[ENCODING]) : null
  42. this[EOF] = false
  43. this[EMITTED_END] = false
  44. this[CLOSED] = false
  45. this.writable = true
  46. this.readable = true
  47. this[BUFFERLENGTH] = 0
  48. }
  49. get bufferLength () { return this[BUFFERLENGTH] }
  50. get encoding () { return this[ENCODING] }
  51. set encoding (enc) {
  52. if (this[OBJECTMODE])
  53. throw new Error('cannot set encoding in objectMode')
  54. if (this[ENCODING] && enc !== this[ENCODING] &&
  55. (this[DECODER] && this[DECODER].lastNeed || this[BUFFERLENGTH]))
  56. throw new Error('cannot change encoding')
  57. if (this[ENCODING] !== enc) {
  58. this[DECODER] = enc ? new SD(enc) : null
  59. if (this.buffer.length)
  60. this.buffer = this.buffer.map(chunk => this[DECODER].write(chunk))
  61. }
  62. this[ENCODING] = enc
  63. }
  64. setEncoding (enc) {
  65. this.encoding = enc
  66. }
  67. write (chunk, encoding, cb) {
  68. if (this[EOF])
  69. throw new Error('write after end')
  70. if (typeof encoding === 'function')
  71. cb = encoding, encoding = 'utf8'
  72. if (!encoding)
  73. encoding = 'utf8'
  74. // fast-path writing strings of same encoding to a stream with
  75. // an empty buffer, skipping the buffer/decoder dance
  76. if (typeof chunk === 'string' && !this[OBJECTMODE] &&
  77. // unless it is a string already ready for us to use
  78. !(encoding === this[ENCODING] && !this[DECODER].lastNeed)) {
  79. chunk = B.from(chunk, encoding)
  80. }
  81. if (B.isBuffer(chunk) && this[ENCODING])
  82. chunk = this[DECODER].write(chunk)
  83. try {
  84. return this.flowing
  85. ? (this.emit('data', chunk), this.flowing)
  86. : (this[BUFFERPUSH](chunk), false)
  87. } finally {
  88. this.emit('readable')
  89. if (cb)
  90. cb()
  91. }
  92. }
  93. read (n) {
  94. try {
  95. if (this[BUFFERLENGTH] === 0 || n === 0 || n > this[BUFFERLENGTH])
  96. return null
  97. if (this[OBJECTMODE])
  98. n = null
  99. if (this.buffer.length > 1 && !this[OBJECTMODE]) {
  100. if (this.encoding)
  101. this.buffer = new Yallist([
  102. Array.from(this.buffer).join('')
  103. ])
  104. else
  105. this.buffer = new Yallist([
  106. B.concat(Array.from(this.buffer), this[BUFFERLENGTH])
  107. ])
  108. }
  109. return this[READ](n || null, this.buffer.head.value)
  110. } finally {
  111. this[MAYBE_EMIT_END]()
  112. }
  113. }
  114. [READ] (n, chunk) {
  115. if (n === chunk.length || n === null)
  116. this[BUFFERSHIFT]()
  117. else {
  118. this.buffer.head.value = chunk.slice(n)
  119. chunk = chunk.slice(0, n)
  120. this[BUFFERLENGTH] -= n
  121. }
  122. this.emit('data', chunk)
  123. if (!this.buffer.length && !this[EOF])
  124. this.emit('drain')
  125. return chunk
  126. }
  127. end (chunk, encoding, cb) {
  128. if (typeof chunk === 'function')
  129. cb = chunk, chunk = null
  130. if (typeof encoding === 'function')
  131. cb = encoding, encoding = 'utf8'
  132. if (chunk)
  133. this.write(chunk, encoding)
  134. if (cb)
  135. this.once('end', cb)
  136. this[EOF] = true
  137. this.writable = false
  138. if (this.flowing)
  139. this[MAYBE_EMIT_END]()
  140. }
  141. // don't let the internal resume be overwritten
  142. [RESUME] () {
  143. this[FLOWING] = true
  144. this.emit('resume')
  145. if (this.buffer.length)
  146. this[FLUSH]()
  147. else if (this[EOF])
  148. this[MAYBE_EMIT_END]()
  149. else
  150. this.emit('drain')
  151. }
  152. resume () {
  153. return this[RESUME]()
  154. }
  155. pause () {
  156. this[FLOWING] = false
  157. }
  158. get flowing () {
  159. return this[FLOWING]
  160. }
  161. [BUFFERPUSH] (chunk) {
  162. if (this[OBJECTMODE])
  163. this[BUFFERLENGTH] += 1
  164. else
  165. this[BUFFERLENGTH] += chunk.length
  166. return this.buffer.push(chunk)
  167. }
  168. [BUFFERSHIFT] () {
  169. if (this.buffer.length) {
  170. if (this[OBJECTMODE])
  171. this[BUFFERLENGTH] -= 1
  172. else
  173. this[BUFFERLENGTH] -= this.buffer.head.value.length
  174. }
  175. return this.buffer.shift()
  176. }
  177. [FLUSH] () {
  178. do {} while (this[FLUSHCHUNK](this[BUFFERSHIFT]()))
  179. if (!this.buffer.length && !this[EOF])
  180. this.emit('drain')
  181. }
  182. [FLUSHCHUNK] (chunk) {
  183. return chunk ? (this.emit('data', chunk), this.flowing) : false
  184. }
  185. pipe (dest, opts) {
  186. if (dest === process.stdout || dest === process.stderr)
  187. (opts = opts || {}).end = false
  188. const p = { dest: dest, opts: opts, ondrain: _ => this[RESUME]() }
  189. this.pipes.push(p)
  190. dest.on('drain', p.ondrain)
  191. this[RESUME]()
  192. return dest
  193. }
  194. addListener (ev, fn) {
  195. return this.on(ev, fn)
  196. }
  197. on (ev, fn) {
  198. try {
  199. return super.on(ev, fn)
  200. } finally {
  201. if (ev === 'data' && !this.pipes.length && !this.flowing) {
  202. this[RESUME]()
  203. }
  204. }
  205. }
  206. get emittedEnd () {
  207. return this[EMITTED_END]
  208. }
  209. [MAYBE_EMIT_END] () {
  210. if (!this[EMITTED_END] && this.buffer.length === 0 && this[EOF]) {
  211. this.emit('end')
  212. this.emit('prefinish')
  213. this.emit('finish')
  214. if (this[CLOSED])
  215. this.emit('close')
  216. }
  217. }
  218. emit (ev, data) {
  219. if (ev === 'data') {
  220. if (!data)
  221. return
  222. if (this.pipes.length)
  223. this.pipes.forEach(p => p.dest.write(data) || this.pause())
  224. } else if (ev === 'end') {
  225. if (this[DECODER]) {
  226. data = this[DECODER].end()
  227. if (data) {
  228. this.pipes.forEach(p => p.dest.write(data))
  229. super.emit('data', data)
  230. }
  231. }
  232. this.pipes.forEach(p => {
  233. p.dest.removeListener('drain', p.ondrain)
  234. if (!p.opts || p.opts.end !== false)
  235. p.dest.end()
  236. })
  237. this[EMITTED_END] = true
  238. this.readable = false
  239. } else if (ev === 'close') {
  240. this[CLOSED] = true
  241. // don't emit close before 'end' and 'finish'
  242. if (!this[EMITTED_END])
  243. return
  244. }
  245. const args = new Array(arguments.length)
  246. args[0] = ev
  247. args[1] = data
  248. if (arguments.length > 2) {
  249. for (let i = 2; i < arguments.length; i++) {
  250. args[i] = arguments[i]
  251. }
  252. }
  253. try {
  254. return super.emit.apply(this, args)
  255. } finally {
  256. if (ev !== 'end')
  257. this[MAYBE_EMIT_END]()
  258. }
  259. }
  260. }
  261. module.exports = MiniPass