1 // a duplex stream is just a stream that is both readable and writable.
2 // Since JS doesn't have multiple prototypal inheritance, this class
3 // prototypally inherits from Readable, and then parasitically from
8 module.exports = Duplex;
10 const util = require('util');
11 const Readable = require('_stream_readable');
12 const Writable = require('_stream_writable');
14 util.inherits(Duplex, Readable);
16 var keys = Object.keys(Writable.prototype);
17 for (var v = 0; v < keys.length; v++) {
19 if (!Duplex.prototype[method])
20 Duplex.prototype[method] = Writable.prototype[method];
23 function Duplex(options) {
24 if (!(this instanceof Duplex))
25 return new Duplex(options);
27 Readable.call(this, options);
28 Writable.call(this, options);
30 if (options && options.readable === false)
31 this.readable = false;
33 if (options && options.writable === false)
34 this.writable = false;
36 this.allowHalfOpen = true;
37 if (options && options.allowHalfOpen === false)
38 this.allowHalfOpen = false;
40 this.once('end', onend);
43 // the no-half-open enforcer
45 // if we allow half-open state, or if the writable side ended,
47 if (this.allowHalfOpen || this._writableState.ended)
50 // no more data can be written.
51 // But allow more writes to happen in this tick.
52 process.nextTick(onEndNT, this);
55 function onEndNT(self) {