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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. module.exports = Stream;
  22. var EE = require('events').EventEmitter;
  23. var inherits = require('inherits');
  24. inherits(Stream, EE);
  25. Stream.Readable = require('readable-stream/readable.js');
  26. Stream.Writable = require('readable-stream/writable.js');
  27. Stream.Duplex = require('readable-stream/duplex.js');
  28. Stream.Transform = require('readable-stream/transform.js');
  29. Stream.PassThrough = require('readable-stream/passthrough.js');
  30. // Backwards-compat with node 0.4.x
  31. Stream.Stream = Stream;
  32. // old-style streams. Note that the pipe method (the only relevant
  33. // part of this class) is overridden in the Readable class.
  34. function Stream() {
  35. EE.call(this);
  36. }
  37. Stream.prototype.pipe = function(dest, options) {
  38. var source = this;
  39. function ondata(chunk) {
  40. if (dest.writable) {
  41. if (false === dest.write(chunk) && source.pause) {
  42. source.pause();
  43. }
  44. }
  45. }
  46. source.on('data', ondata);
  47. function ondrain() {
  48. if (source.readable && source.resume) {
  49. source.resume();
  50. }
  51. }
  52. dest.on('drain', ondrain);
  53. // If the 'end' option is not supplied, dest.end() will be called when
  54. // source gets the 'end' or 'close' events. Only dest.end() once.
  55. if (!dest._isStdio && (!options || options.end !== false)) {
  56. source.on('end', onend);
  57. source.on('close', onclose);
  58. }
  59. var didOnEnd = false;
  60. function onend() {
  61. if (didOnEnd) return;
  62. didOnEnd = true;
  63. dest.end();
  64. }
  65. function onclose() {
  66. if (didOnEnd) return;
  67. didOnEnd = true;
  68. if (typeof dest.destroy === 'function') dest.destroy();
  69. }
  70. // don't leave dangling pipes when there are errors.
  71. function onerror(er) {
  72. cleanup();
  73. if (EE.listenerCount(this, 'error') === 0) {
  74. throw er; // Unhandled stream error in pipe.
  75. }
  76. }
  77. source.on('error', onerror);
  78. dest.on('error', onerror);
  79. // remove all the event listeners that were added.
  80. function cleanup() {
  81. source.removeListener('data', ondata);
  82. dest.removeListener('drain', ondrain);
  83. source.removeListener('end', onend);
  84. source.removeListener('close', onclose);
  85. source.removeListener('error', onerror);
  86. dest.removeListener('error', onerror);
  87. source.removeListener('end', cleanup);
  88. source.removeListener('close', cleanup);
  89. dest.removeListener('close', cleanup);
  90. }
  91. source.on('end', cleanup);
  92. source.on('close', cleanup);
  93. dest.on('close', cleanup);
  94. dest.emit('pipe', source);
  95. // Allow for unix-like usage: A.pipe(B).pipe(C)
  96. return dest;
  97. };