index.js 3.0 KB

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