index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict'
  2. var PassThrough = require('readable-stream').PassThrough
  3. var inherits = require('inherits')
  4. var p = require('process-nextick-args')
  5. function Cloneable (stream, opts) {
  6. if (!(this instanceof Cloneable)) {
  7. return new Cloneable(stream, opts)
  8. }
  9. var objectMode = stream._readableState.objectMode
  10. this._original = stream
  11. this._clonesCount = 1
  12. opts = opts || {}
  13. opts.objectMode = objectMode
  14. PassThrough.call(this, opts)
  15. forwardDestroy(stream, this)
  16. this.on('newListener', onData)
  17. this.once('resume', onResume)
  18. this._hasListener = true
  19. }
  20. inherits(Cloneable, PassThrough)
  21. function onData (event, listener) {
  22. if (event === 'data' || event === 'readable') {
  23. this._hasListener = false
  24. this.removeListener('newListener', onData)
  25. this.removeListener('resume', onResume)
  26. p.nextTick(clonePiped, this)
  27. }
  28. }
  29. function onResume () {
  30. this._hasListener = false
  31. this.removeListener('newListener', onData)
  32. p.nextTick(clonePiped, this)
  33. }
  34. Cloneable.prototype.clone = function () {
  35. if (!this._original) {
  36. throw new Error('already started')
  37. }
  38. this._clonesCount++
  39. // the events added by the clone should not count
  40. // for starting the flow
  41. this.removeListener('newListener', onData)
  42. var clone = new Clone(this)
  43. if (this._hasListener) {
  44. this.on('newListener', onData)
  45. }
  46. return clone
  47. }
  48. Cloneable.prototype._destroy = function (err, cb) {
  49. if (!err) {
  50. this.push(null)
  51. this.end()
  52. this.emit('close')
  53. }
  54. p.nextTick(cb, err)
  55. }
  56. function forwardDestroy (src, dest) {
  57. src.on('error', destroy)
  58. src.on('close', onClose)
  59. function destroy (err) {
  60. dest.destroy(err)
  61. }
  62. function onClose () {
  63. dest.end()
  64. }
  65. }
  66. function clonePiped (that) {
  67. if (--that._clonesCount === 0 && !that._readableState.destroyed) {
  68. that._original.pipe(that)
  69. that._original = undefined
  70. }
  71. }
  72. function Clone (parent, opts) {
  73. if (!(this instanceof Clone)) {
  74. return new Clone(parent, opts)
  75. }
  76. var objectMode = parent._readableState.objectMode
  77. opts = opts || {}
  78. opts.objectMode = objectMode
  79. this.parent = parent
  80. PassThrough.call(this, opts)
  81. forwardDestroy(parent, this)
  82. parent.pipe(this)
  83. // the events added by the clone should not count
  84. // for starting the flow
  85. // so we add the newListener handle after we are done
  86. this.on('newListener', onDataClone)
  87. this.on('resume', onResumeClone)
  88. }
  89. function onDataClone (event, listener) {
  90. // We start the flow once all clones are piped or destroyed
  91. if (event === 'data' || event === 'readable' || event === 'close') {
  92. p.nextTick(clonePiped, this.parent)
  93. this.removeListener('newListener', onDataClone)
  94. }
  95. }
  96. function onResumeClone () {
  97. this.removeListener('newListener', onDataClone)
  98. p.nextTick(clonePiped, this.parent)
  99. }
  100. inherits(Clone, PassThrough)
  101. Clone.prototype.clone = function () {
  102. return this.parent.clone()
  103. }
  104. Cloneable.isCloneable = function (stream) {
  105. return stream instanceof Cloneable || stream instanceof Clone
  106. }
  107. Clone.prototype._destroy = function (err, cb) {
  108. if (!err) {
  109. this.push(null)
  110. this.end()
  111. this.emit('close')
  112. }
  113. p.nextTick(cb, err)
  114. }
  115. module.exports = Cloneable