| 1 | // Copyright Joyent, Inc. and other Node contributors.
|
|---|
| 2 | //
|
|---|
| 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:
|
|---|
| 10 | //
|
|---|
| 11 | // The above copyright notice and this permission notice shall be included
|
|---|
| 12 | // in all copies or substantial portions of the Software.
|
|---|
| 13 | //
|
|---|
| 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.
|
|---|
| 21 |
|
|---|
| 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
|
|---|
| 25 | // Writable.
|
|---|
| 26 |
|
|---|
| 27 | 'use strict';
|
|---|
| 28 |
|
|---|
| 29 | /*<replacement>*/
|
|---|
| 30 | var objectKeys = Object.keys || function (obj) {
|
|---|
| 31 | var keys = [];
|
|---|
| 32 | for (var key in obj) keys.push(key);
|
|---|
| 33 | return keys;
|
|---|
| 34 | };
|
|---|
| 35 | /*</replacement>*/
|
|---|
| 36 |
|
|---|
| 37 | module.exports = Duplex;
|
|---|
| 38 | var Readable = require('./_stream_readable');
|
|---|
| 39 | var Writable = require('./_stream_writable');
|
|---|
| 40 | require('inherits')(Duplex, Readable);
|
|---|
| 41 | {
|
|---|
| 42 | // Allow the keys array to be GC'ed.
|
|---|
| 43 | var keys = objectKeys(Writable.prototype);
|
|---|
| 44 | for (var v = 0; v < keys.length; v++) {
|
|---|
| 45 | var method = keys[v];
|
|---|
| 46 | if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | function Duplex(options) {
|
|---|
| 50 | if (!(this instanceof Duplex)) return new Duplex(options);
|
|---|
| 51 | Readable.call(this, options);
|
|---|
| 52 | Writable.call(this, options);
|
|---|
| 53 | this.allowHalfOpen = true;
|
|---|
| 54 | if (options) {
|
|---|
| 55 | if (options.readable === false) this.readable = false;
|
|---|
| 56 | if (options.writable === false) this.writable = false;
|
|---|
| 57 | if (options.allowHalfOpen === false) {
|
|---|
| 58 | this.allowHalfOpen = false;
|
|---|
| 59 | this.once('end', onend);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
|
|---|
| 64 | // making it explicit this property is not enumerable
|
|---|
| 65 | // because otherwise some prototype manipulation in
|
|---|
| 66 | // userland will fail
|
|---|
| 67 | enumerable: false,
|
|---|
| 68 | get: function get() {
|
|---|
| 69 | return this._writableState.highWaterMark;
|
|---|
| 70 | }
|
|---|
| 71 | });
|
|---|
| 72 | Object.defineProperty(Duplex.prototype, 'writableBuffer', {
|
|---|
| 73 | // making it explicit this property is not enumerable
|
|---|
| 74 | // because otherwise some prototype manipulation in
|
|---|
| 75 | // userland will fail
|
|---|
| 76 | enumerable: false,
|
|---|
| 77 | get: function get() {
|
|---|
| 78 | return this._writableState && this._writableState.getBuffer();
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 | Object.defineProperty(Duplex.prototype, 'writableLength', {
|
|---|
| 82 | // making it explicit this property is not enumerable
|
|---|
| 83 | // because otherwise some prototype manipulation in
|
|---|
| 84 | // userland will fail
|
|---|
| 85 | enumerable: false,
|
|---|
| 86 | get: function get() {
|
|---|
| 87 | return this._writableState.length;
|
|---|
| 88 | }
|
|---|
| 89 | });
|
|---|
| 90 |
|
|---|
| 91 | // the no-half-open enforcer
|
|---|
| 92 | function onend() {
|
|---|
| 93 | // If the writable side ended, then we're ok.
|
|---|
| 94 | if (this._writableState.ended) return;
|
|---|
| 95 |
|
|---|
| 96 | // no more data can be written.
|
|---|
| 97 | // But allow more writes to happen in this tick.
|
|---|
| 98 | process.nextTick(onEndNT, this);
|
|---|
| 99 | }
|
|---|
| 100 | function onEndNT(self) {
|
|---|
| 101 | self.end();
|
|---|
| 102 | }
|
|---|
| 103 | Object.defineProperty(Duplex.prototype, 'destroyed', {
|
|---|
| 104 | // making it explicit this property is not enumerable
|
|---|
| 105 | // because otherwise some prototype manipulation in
|
|---|
| 106 | // userland will fail
|
|---|
| 107 | enumerable: false,
|
|---|
| 108 | get: function get() {
|
|---|
| 109 | if (this._readableState === undefined || this._writableState === undefined) {
|
|---|
| 110 | return false;
|
|---|
| 111 | }
|
|---|
| 112 | return this._readableState.destroyed && this._writableState.destroyed;
|
|---|
| 113 | },
|
|---|
| 114 | set: function set(value) {
|
|---|
| 115 | // we ignore the value if the stream
|
|---|
| 116 | // has not been initialized yet
|
|---|
| 117 | if (this._readableState === undefined || this._writableState === undefined) {
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // backward compatibility, the user is explicitly
|
|---|
| 122 | // managing destroyed
|
|---|
| 123 | this._readableState.destroyed = value;
|
|---|
| 124 | this._writableState.destroyed = value;
|
|---|
| 125 | }
|
|---|
| 126 | }); |
|---|