1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // a duplex stream is just a stream that is both readable and writable.
23 // Since JS doesn't have multiple prototypal inheritance, this class
24 // prototypally inherits from Readable, and then parasitically from
27 module.exports = Duplex;
28 var util = require('util');
29 var Readable = require('_stream_readable');
30 var Writable = require('_stream_writable');
32 util.inherits(Duplex, Readable);
34 Object.keys(Writable.prototype).forEach(function(method) {
35 if (!Duplex.prototype[method])
36 Duplex.prototype[method] = Writable.prototype[method];
39 function Duplex(options) {
40 if (!(this instanceof Duplex))
41 return new Duplex(options);
43 Readable.call(this, options);
44 Writable.call(this, options);
46 if (options && options.readable === false)
47 this.readable = false;
49 if (options && options.writable === false)
50 this.writable = false;
52 this.allowHalfOpen = true;
53 if (options && options.allowHalfOpen === false)
54 this.allowHalfOpen = false;
56 this.once('end', onend);
59 // the no-half-open enforcer
61 // if we allow half-open state, or if the writable side ended,
63 if (this.allowHalfOpen || this._writableState.ended)
66 // no more data can be written.
67 // But allow more writes to happen in this tick.
68 process.nextTick(this.end.bind(this));