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