_stream_duplex.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. module.exports = Duplex;
  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. /*<replacement>*/
  34. var util = require('core-util-is');
  35. util.inherits = require('inherits');
  36. /*</replacement>*/
  37. var Readable = require('./_stream_readable');
  38. var Writable = require('./_stream_writable');
  39. util.inherits(Duplex, Readable);
  40. forEach(objectKeys(Writable.prototype), function(method) {
  41. if (!Duplex.prototype[method])
  42. Duplex.prototype[method] = Writable.prototype[method];
  43. });
  44. function Duplex(options) {
  45. if (!(this instanceof Duplex))
  46. return new Duplex(options);
  47. Readable.call(this, options);
  48. Writable.call(this, options);
  49. if (options && options.readable === false)
  50. this.readable = false;
  51. if (options && options.writable === false)
  52. this.writable = false;
  53. this.allowHalfOpen = true;
  54. if (options && options.allowHalfOpen === false)
  55. this.allowHalfOpen = false;
  56. this.once('end', onend);
  57. }
  58. // the no-half-open enforcer
  59. function onend() {
  60. // if we allow half-open state, or if the writable side ended,
  61. // then we're ok.
  62. if (this.allowHalfOpen || this._writableState.ended)
  63. return;
  64. // no more data can be written.
  65. // But allow more writes to happen in this tick.
  66. process.nextTick(this.end.bind(this));
  67. }
  68. function forEach (xs, f) {
  69. for (var i = 0, l = xs.length; i < l; i++) {
  70. f(xs[i], i);
  71. }
  72. }