_stream_duplex.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 pna = require('process-nextick-args');
  28. /*</replacement>*/
  29. /*<replacement>*/
  30. var objectKeys = Object.keys || function (obj) {
  31. var keys = [];
  32. for (var key in obj) {
  33. keys.push(key);
  34. }return keys;
  35. };
  36. /*</replacement>*/
  37. module.exports = Duplex;
  38. /*<replacement>*/
  39. var util = require('core-util-is');
  40. util.inherits = require('inherits');
  41. /*</replacement>*/
  42. var Readable = require('./_stream_readable');
  43. var Writable = require('./_stream_writable');
  44. util.inherits(Duplex, Readable);
  45. {
  46. // avoid scope creep, the keys array can then be collected
  47. var keys = objectKeys(Writable.prototype);
  48. for (var v = 0; v < keys.length; v++) {
  49. var method = keys[v];
  50. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  51. }
  52. }
  53. function Duplex(options) {
  54. if (!(this instanceof Duplex)) return new Duplex(options);
  55. Readable.call(this, options);
  56. Writable.call(this, options);
  57. if (options && options.readable === false) this.readable = false;
  58. if (options && options.writable === false) this.writable = false;
  59. this.allowHalfOpen = true;
  60. if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
  61. this.once('end', onend);
  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 () {
  69. return this._writableState.highWaterMark;
  70. }
  71. });
  72. // the no-half-open enforcer
  73. function onend() {
  74. // if we allow half-open state, or if the writable side ended,
  75. // then we're ok.
  76. if (this.allowHalfOpen || this._writableState.ended) return;
  77. // no more data can be written.
  78. // But allow more writes to happen in this tick.
  79. pna.nextTick(onEndNT, this);
  80. }
  81. function onEndNT(self) {
  82. self.end();
  83. }
  84. Object.defineProperty(Duplex.prototype, 'destroyed', {
  85. get: function () {
  86. if (this._readableState === undefined || this._writableState === undefined) {
  87. return false;
  88. }
  89. return this._readableState.destroyed && this._writableState.destroyed;
  90. },
  91. set: function (value) {
  92. // we ignore the value if the stream
  93. // has not been initialized yet
  94. if (this._readableState === undefined || this._writableState === undefined) {
  95. return;
  96. }
  97. // backward compatibility, the user is explicitly
  98. // managing destroyed
  99. this._readableState.destroyed = value;
  100. this._writableState.destroyed = value;
  101. }
  102. });
  103. Duplex.prototype._destroy = function (err, cb) {
  104. this.push(null);
  105. this.end();
  106. pna.nextTick(cb, err);
  107. };