index.js 5.8 KB

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