react / wstein / node_modules / browserify / node_modules / readable-stream / lib / _stream_duplex.js
80538 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021// a duplex stream is just a stream that is both readable and writable.22// Since JS doesn't have multiple prototypal inheritance, this class23// prototypally inherits from Readable, and then parasitically from24// Writable.2526module.exports = Duplex;2728/*<replacement>*/29var objectKeys = Object.keys || function (obj) {30var keys = [];31for (var key in obj) keys.push(key);32return keys;33}34/*</replacement>*/353637/*<replacement>*/38var util = require('core-util-is');39util.inherits = require('inherits');40/*</replacement>*/4142var Readable = require('./_stream_readable');43var Writable = require('./_stream_writable');4445util.inherits(Duplex, Readable);4647forEach(objectKeys(Writable.prototype), function(method) {48if (!Duplex.prototype[method])49Duplex.prototype[method] = Writable.prototype[method];50});5152function Duplex(options) {53if (!(this instanceof Duplex))54return new Duplex(options);5556Readable.call(this, options);57Writable.call(this, options);5859if (options && options.readable === false)60this.readable = false;6162if (options && options.writable === false)63this.writable = false;6465this.allowHalfOpen = true;66if (options && options.allowHalfOpen === false)67this.allowHalfOpen = false;6869this.once('end', onend);70}7172// the no-half-open enforcer73function onend() {74// if we allow half-open state, or if the writable side ended,75// then we're ok.76if (this.allowHalfOpen || this._writableState.ended)77return;7879// no more data can be written.80// But allow more writes to happen in this tick.81process.nextTick(this.end.bind(this));82}8384function forEach (xs, f) {85for (var i = 0, l = xs.length; i < l; i++) {86f(xs[i], i);87}88}899091