You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_stream_duplex.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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) {
  30. keys.push(key);
  31. }
  32. return keys;
  33. };
  34. /*</replacement>*/
  35. module.exports = Duplex;
  36. var Readable = require('./_stream_readable');
  37. var Writable = require('./_stream_writable');
  38. require('inherits')(Duplex, Readable);
  39. {
  40. // Allow the keys array to be GC'ed.
  41. var keys = objectKeys(Writable.prototype);
  42. for (var v = 0; v < keys.length; v++) {
  43. var method = keys[v];
  44. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  45. }
  46. }
  47. function Duplex(options) {
  48. if (!(this instanceof Duplex)) return new Duplex(options);
  49. Readable.call(this, options);
  50. Writable.call(this, options);
  51. this.allowHalfOpen = true;
  52. if (options) {
  53. if (options.readable === false) this.readable = false;
  54. if (options.writable === false) this.writable = false;
  55. if (options.allowHalfOpen === false) {
  56. this.allowHalfOpen = false;
  57. this.once('end', onend);
  58. }
  59. }
  60. }
  61. Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
  62. // making it explicit this property is not enumerable
  63. // because otherwise some prototype manipulation in
  64. // userland will fail
  65. enumerable: false,
  66. get: function get() {
  67. return this._writableState.highWaterMark;
  68. }
  69. });
  70. Object.defineProperty(Duplex.prototype, 'writableBuffer', {
  71. // making it explicit this property is not enumerable
  72. // because otherwise some prototype manipulation in
  73. // userland will fail
  74. enumerable: false,
  75. get: function get() {
  76. return this._writableState && this._writableState.getBuffer();
  77. }
  78. });
  79. Object.defineProperty(Duplex.prototype, 'writableLength', {
  80. // making it explicit this property is not enumerable
  81. // because otherwise some prototype manipulation in
  82. // userland will fail
  83. enumerable: false,
  84. get: function get() {
  85. return this._writableState.length;
  86. }
  87. }); // the no-half-open enforcer
  88. function onend() {
  89. // If the writable side ended, then we're ok.
  90. if (this._writableState.ended) return; // 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. } // backward compatibility, the user is explicitly
  114. // managing destroyed
  115. this._readableState.destroyed = value;
  116. this._writableState.destroyed = value;
  117. }
  118. });