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.

141 lines
2.9 KiB

7 years ago
  1. /*
  2. * buffered-stream.js: A simple(r) Stream which is partially buffered into memory.
  3. *
  4. * (C) 2010, Mikeal Rogers
  5. *
  6. * Adapted for Flatiron
  7. * (C) 2011, Charlie Robbins & the Contributors
  8. * MIT LICENSE
  9. *
  10. */
  11. var events = require('events'),
  12. fs = require('fs'),
  13. stream = require('stream'),
  14. util = require('util');
  15. //
  16. // ### function BufferedStream (limit)
  17. // #### @limit {number} **Optional** Size of the buffer to limit
  18. // Constructor function for the BufferedStream object responsible for
  19. // maintaining a stream interface which can also persist to memory
  20. // temporarily.
  21. //
  22. var BufferedStream = module.exports = function (limit) {
  23. events.EventEmitter.call(this);
  24. if (typeof limit === 'undefined') {
  25. limit = Infinity;
  26. }
  27. this.limit = limit;
  28. this.size = 0;
  29. this.chunks = [];
  30. this.writable = true;
  31. this.readable = true;
  32. this._buffer = true;
  33. };
  34. util.inherits(BufferedStream, stream.Stream);
  35. Object.defineProperty(BufferedStream.prototype, 'buffer', {
  36. get: function () {
  37. return this._buffer;
  38. },
  39. set: function (value) {
  40. if (!value && this.chunks) {
  41. var self = this;
  42. this.chunks.forEach(function (c) { self.emit('data', c) });
  43. if (this.ended) this.emit('end');
  44. this.size = 0;
  45. delete this.chunks;
  46. }
  47. this._buffer = value;
  48. }
  49. });
  50. BufferedStream.prototype.pipe = function () {
  51. var self = this,
  52. dest;
  53. if (self.resume) {
  54. self.resume();
  55. }
  56. dest = stream.Stream.prototype.pipe.apply(self, arguments);
  57. //
  58. // just incase you are piping to two streams, do not emit data twice.
  59. // note: you can pipe twice, but you need to pipe both streams in the same tick.
  60. // (this is normal for streams)
  61. //
  62. if (this.piped) {
  63. return dest;
  64. }
  65. process.nextTick(function () {
  66. if (self.chunks) {
  67. self.chunks.forEach(function (c) { self.emit('data', c) });
  68. self.size = 0;
  69. delete self.chunks;
  70. }
  71. if (!self.readable) {
  72. if (self.ended) {
  73. self.emit('end');
  74. }
  75. else if (self.closed) {
  76. self.emit('close');
  77. }
  78. }
  79. });
  80. this.piped = true;
  81. return dest;
  82. };
  83. BufferedStream.prototype.write = function (chunk) {
  84. if (!this.chunks || this.piped) {
  85. this.emit('data', chunk);
  86. return;
  87. }
  88. this.chunks.push(chunk);
  89. this.size += chunk.length;
  90. if (this.limit < this.size) {
  91. this.pause();
  92. }
  93. };
  94. BufferedStream.prototype.end = function () {
  95. this.readable = false;
  96. this.ended = true;
  97. this.emit('end');
  98. };
  99. BufferedStream.prototype.destroy = function () {
  100. this.readable = false;
  101. this.writable = false;
  102. delete this.chunks;
  103. };
  104. BufferedStream.prototype.close = function () {
  105. this.readable = false;
  106. this.closed = true;
  107. };
  108. if (!stream.Stream.prototype.pause) {
  109. BufferedStream.prototype.pause = function () {
  110. this.emit('pause');
  111. };
  112. }
  113. if (!stream.Stream.prototype.resume) {
  114. BufferedStream.prototype.resume = function () {
  115. this.emit('resume');
  116. };
  117. }