index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1662612701763, function(require, module, exports) {
  8. var util = require('util');
  9. var Stream = require('stream').Stream;
  10. var DelayedStream = require('delayed-stream');
  11. module.exports = CombinedStream;
  12. function CombinedStream() {
  13. this.writable = false;
  14. this.readable = true;
  15. this.dataSize = 0;
  16. this.maxDataSize = 2 * 1024 * 1024;
  17. this.pauseStreams = true;
  18. this._released = false;
  19. this._streams = [];
  20. this._currentStream = null;
  21. this._insideLoop = false;
  22. this._pendingNext = false;
  23. }
  24. util.inherits(CombinedStream, Stream);
  25. CombinedStream.create = function(options) {
  26. var combinedStream = new this();
  27. options = options || {};
  28. for (var option in options) {
  29. combinedStream[option] = options[option];
  30. }
  31. return combinedStream;
  32. };
  33. CombinedStream.isStreamLike = function(stream) {
  34. return (typeof stream !== 'function')
  35. && (typeof stream !== 'string')
  36. && (typeof stream !== 'boolean')
  37. && (typeof stream !== 'number')
  38. && (!Buffer.isBuffer(stream));
  39. };
  40. CombinedStream.prototype.append = function(stream) {
  41. var isStreamLike = CombinedStream.isStreamLike(stream);
  42. if (isStreamLike) {
  43. if (!(stream instanceof DelayedStream)) {
  44. var newStream = DelayedStream.create(stream, {
  45. maxDataSize: Infinity,
  46. pauseStream: this.pauseStreams,
  47. });
  48. stream.on('data', this._checkDataSize.bind(this));
  49. stream = newStream;
  50. }
  51. this._handleErrors(stream);
  52. if (this.pauseStreams) {
  53. stream.pause();
  54. }
  55. }
  56. this._streams.push(stream);
  57. return this;
  58. };
  59. CombinedStream.prototype.pipe = function(dest, options) {
  60. Stream.prototype.pipe.call(this, dest, options);
  61. this.resume();
  62. return dest;
  63. };
  64. CombinedStream.prototype._getNext = function() {
  65. this._currentStream = null;
  66. if (this._insideLoop) {
  67. this._pendingNext = true;
  68. return; // defer call
  69. }
  70. this._insideLoop = true;
  71. try {
  72. do {
  73. this._pendingNext = false;
  74. this._realGetNext();
  75. } while (this._pendingNext);
  76. } finally {
  77. this._insideLoop = false;
  78. }
  79. };
  80. CombinedStream.prototype._realGetNext = function() {
  81. var stream = this._streams.shift();
  82. if (typeof stream == 'undefined') {
  83. this.end();
  84. return;
  85. }
  86. if (typeof stream !== 'function') {
  87. this._pipeNext(stream);
  88. return;
  89. }
  90. var getStream = stream;
  91. getStream(function(stream) {
  92. var isStreamLike = CombinedStream.isStreamLike(stream);
  93. if (isStreamLike) {
  94. stream.on('data', this._checkDataSize.bind(this));
  95. this._handleErrors(stream);
  96. }
  97. this._pipeNext(stream);
  98. }.bind(this));
  99. };
  100. CombinedStream.prototype._pipeNext = function(stream) {
  101. this._currentStream = stream;
  102. var isStreamLike = CombinedStream.isStreamLike(stream);
  103. if (isStreamLike) {
  104. stream.on('end', this._getNext.bind(this));
  105. stream.pipe(this, {end: false});
  106. return;
  107. }
  108. var value = stream;
  109. this.write(value);
  110. this._getNext();
  111. };
  112. CombinedStream.prototype._handleErrors = function(stream) {
  113. var self = this;
  114. stream.on('error', function(err) {
  115. self._emitError(err);
  116. });
  117. };
  118. CombinedStream.prototype.write = function(data) {
  119. this.emit('data', data);
  120. };
  121. CombinedStream.prototype.pause = function() {
  122. if (!this.pauseStreams) {
  123. return;
  124. }
  125. if(this.pauseStreams && this._currentStream && typeof(this._currentStream.pause) == 'function') this._currentStream.pause();
  126. this.emit('pause');
  127. };
  128. CombinedStream.prototype.resume = function() {
  129. if (!this._released) {
  130. this._released = true;
  131. this.writable = true;
  132. this._getNext();
  133. }
  134. if(this.pauseStreams && this._currentStream && typeof(this._currentStream.resume) == 'function') this._currentStream.resume();
  135. this.emit('resume');
  136. };
  137. CombinedStream.prototype.end = function() {
  138. this._reset();
  139. this.emit('end');
  140. };
  141. CombinedStream.prototype.destroy = function() {
  142. this._reset();
  143. this.emit('close');
  144. };
  145. CombinedStream.prototype._reset = function() {
  146. this.writable = false;
  147. this._streams = [];
  148. this._currentStream = null;
  149. };
  150. CombinedStream.prototype._checkDataSize = function() {
  151. this._updateDataSize();
  152. if (this.dataSize <= this.maxDataSize) {
  153. return;
  154. }
  155. var message =
  156. 'DelayedStream#maxDataSize of ' + this.maxDataSize + ' bytes exceeded.';
  157. this._emitError(new Error(message));
  158. };
  159. CombinedStream.prototype._updateDataSize = function() {
  160. this.dataSize = 0;
  161. var self = this;
  162. this._streams.forEach(function(stream) {
  163. if (!stream.dataSize) {
  164. return;
  165. }
  166. self.dataSize += stream.dataSize;
  167. });
  168. if (this._currentStream && this._currentStream.dataSize) {
  169. this.dataSize += this._currentStream.dataSize;
  170. }
  171. };
  172. CombinedStream.prototype._emitError = function(err) {
  173. this._reset();
  174. this.emit('error', err);
  175. };
  176. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  177. return __REQUIRE__(1662612701763);
  178. })()
  179. //miniprogram-npm-outsideDeps=["util","stream","delayed-stream"]
  180. //# sourceMappingURL=index.js.map