_stream_duplex.js 4.3 KB

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