index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var stream = require('readable-stream')
  2. var eos = require('end-of-stream')
  3. var inherits = require('inherits')
  4. var shift = require('stream-shift')
  5. var SIGNAL_FLUSH = new Buffer([0])
  6. var onuncork = function(self, fn) {
  7. if (self._corked) self.once('uncork', fn)
  8. else fn()
  9. }
  10. var destroyer = function(self, end) {
  11. return function(err) {
  12. if (err) self.destroy(err.message === 'premature close' ? null : err)
  13. else if (end && !self._ended) self.end()
  14. }
  15. }
  16. var end = function(ws, fn) {
  17. if (!ws) return fn()
  18. if (ws._writableState && ws._writableState.finished) return fn()
  19. if (ws._writableState) return ws.end(fn)
  20. ws.end()
  21. fn()
  22. }
  23. var toStreams2 = function(rs) {
  24. return new (stream.Readable)({objectMode:true, highWaterMark:16}).wrap(rs)
  25. }
  26. var Duplexify = function(writable, readable, opts) {
  27. if (!(this instanceof Duplexify)) return new Duplexify(writable, readable, opts)
  28. stream.Duplex.call(this, opts)
  29. this._writable = null
  30. this._readable = null
  31. this._readable2 = null
  32. this._forwardDestroy = !opts || opts.destroy !== false
  33. this._forwardEnd = !opts || opts.end !== false
  34. this._corked = 1 // start corked
  35. this._ondrain = null
  36. this._drained = false
  37. this._forwarding = false
  38. this._unwrite = null
  39. this._unread = null
  40. this._ended = false
  41. this.destroyed = false
  42. if (writable) this.setWritable(writable)
  43. if (readable) this.setReadable(readable)
  44. }
  45. inherits(Duplexify, stream.Duplex)
  46. Duplexify.obj = function(writable, readable, opts) {
  47. if (!opts) opts = {}
  48. opts.objectMode = true
  49. opts.highWaterMark = 16
  50. return new Duplexify(writable, readable, opts)
  51. }
  52. Duplexify.prototype.cork = function() {
  53. if (++this._corked === 1) this.emit('cork')
  54. }
  55. Duplexify.prototype.uncork = function() {
  56. if (this._corked && --this._corked === 0) this.emit('uncork')
  57. }
  58. Duplexify.prototype.setWritable = function(writable) {
  59. if (this._unwrite) this._unwrite()
  60. if (this.destroyed) {
  61. if (writable && writable.destroy) writable.destroy()
  62. return
  63. }
  64. if (writable === null || writable === false) {
  65. this.end()
  66. return
  67. }
  68. var self = this
  69. var unend = eos(writable, {writable:true, readable:false}, destroyer(this, this._forwardEnd))
  70. var ondrain = function() {
  71. var ondrain = self._ondrain
  72. self._ondrain = null
  73. if (ondrain) ondrain()
  74. }
  75. var clear = function() {
  76. self._writable.removeListener('drain', ondrain)
  77. unend()
  78. }
  79. if (this._unwrite) process.nextTick(ondrain) // force a drain on stream reset to avoid livelocks
  80. this._writable = writable
  81. this._writable.on('drain', ondrain)
  82. this._unwrite = clear
  83. this.uncork() // always uncork setWritable
  84. }
  85. Duplexify.prototype.setReadable = function(readable) {
  86. if (this._unread) this._unread()
  87. if (this.destroyed) {
  88. if (readable && readable.destroy) readable.destroy()
  89. return
  90. }
  91. if (readable === null || readable === false) {
  92. this.push(null)
  93. this.resume()
  94. return
  95. }
  96. var self = this
  97. var unend = eos(readable, {writable:false, readable:true}, destroyer(this))
  98. var onreadable = function() {
  99. self._forward()
  100. }
  101. var onend = function() {
  102. self.push(null)
  103. }
  104. var clear = function() {
  105. self._readable2.removeListener('readable', onreadable)
  106. self._readable2.removeListener('end', onend)
  107. unend()
  108. }
  109. this._drained = true
  110. this._readable = readable
  111. this._readable2 = readable._readableState ? readable : toStreams2(readable)
  112. this._readable2.on('readable', onreadable)
  113. this._readable2.on('end', onend)
  114. this._unread = clear
  115. this._forward()
  116. }
  117. Duplexify.prototype._read = function() {
  118. this._drained = true
  119. this._forward()
  120. }
  121. Duplexify.prototype._forward = function() {
  122. if (this._forwarding || !this._readable2 || !this._drained) return
  123. this._forwarding = true
  124. var data
  125. while (this._drained && (data = shift(this._readable2)) !== null) {
  126. if (this.destroyed) continue
  127. this._drained = this.push(data)
  128. }
  129. this._forwarding = false
  130. }
  131. Duplexify.prototype.destroy = function(err) {
  132. if (this.destroyed) return
  133. this.destroyed = true
  134. var self = this
  135. process.nextTick(function() {
  136. self._destroy(err)
  137. })
  138. }
  139. Duplexify.prototype._destroy = function(err) {
  140. if (err) {
  141. var ondrain = this._ondrain
  142. this._ondrain = null
  143. if (ondrain) ondrain(err)
  144. else this.emit('error', err)
  145. }
  146. if (this._forwardDestroy) {
  147. if (this._readable && this._readable.destroy) this._readable.destroy()
  148. if (this._writable && this._writable.destroy) this._writable.destroy()
  149. }
  150. this.emit('close')
  151. }
  152. Duplexify.prototype._write = function(data, enc, cb) {
  153. if (this.destroyed) return cb()
  154. if (this._corked) return onuncork(this, this._write.bind(this, data, enc, cb))
  155. if (data === SIGNAL_FLUSH) return this._finish(cb)
  156. if (!this._writable) return cb()
  157. if (this._writable.write(data) === false) this._ondrain = cb
  158. else cb()
  159. }
  160. Duplexify.prototype._finish = function(cb) {
  161. var self = this
  162. this.emit('preend')
  163. onuncork(this, function() {
  164. end(self._forwardEnd && self._writable, function() {
  165. // haxx to not emit prefinish twice
  166. if (self._writableState.prefinished === false) self._writableState.prefinished = true
  167. self.emit('prefinish')
  168. onuncork(self, cb)
  169. })
  170. })
  171. }
  172. Duplexify.prototype.end = function(data, enc, cb) {
  173. if (typeof data === 'function') return this.end(null, null, data)
  174. if (typeof enc === 'function') return this.end(data, null, enc)
  175. this._ended = true
  176. if (data) this.write(data)
  177. if (!this._writableState.ending) this.write(SIGNAL_FLUSH)
  178. return stream.Writable.prototype.end.call(this, cb)
  179. }
  180. module.exports = Duplexify