index.js 5.7 KB

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