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