stuttered-pipe.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  6. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  7. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  8. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  9. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  10. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  11. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  12. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  13. var events = require('events'); // =============================================================================
  14. // StutteredPipe - Used to slow down streaming so GC can get a look in
  15. var StutteredPipe = /*#__PURE__*/function (_events$EventEmitter) {
  16. _inherits(StutteredPipe, _events$EventEmitter);
  17. var _super = _createSuper(StutteredPipe);
  18. function StutteredPipe(readable, writable, options) {
  19. var _this;
  20. _classCallCheck(this, StutteredPipe);
  21. _this = _super.call(this);
  22. options = options || {};
  23. _this.readable = readable;
  24. _this.writable = writable;
  25. _this.bufSize = options.bufSize || 16384;
  26. _this.autoPause = options.autoPause || false;
  27. _this.paused = false;
  28. _this.eod = false;
  29. _this.scheduled = null;
  30. readable.on('end', function () {
  31. _this.eod = true;
  32. writable.end();
  33. }); // need to have some way to communicate speed of stream
  34. // back from the consumer
  35. readable.on('readable', function () {
  36. if (!_this.paused) {
  37. _this.resume();
  38. }
  39. });
  40. _this._schedule();
  41. return _this;
  42. }
  43. _createClass(StutteredPipe, [{
  44. key: "pause",
  45. value: function pause() {
  46. this.paused = true;
  47. }
  48. }, {
  49. key: "resume",
  50. value: function resume() {
  51. if (!this.eod) {
  52. if (this.scheduled !== null) {
  53. clearImmediate(this.scheduled);
  54. }
  55. this._schedule();
  56. }
  57. }
  58. }, {
  59. key: "_schedule",
  60. value: function _schedule() {
  61. var _this2 = this;
  62. this.scheduled = setImmediate(function () {
  63. _this2.scheduled = null;
  64. if (!_this2.eod && !_this2.paused) {
  65. var data = _this2.readable.read(_this2.bufSize);
  66. if (data && data.length) {
  67. _this2.writable.write(data);
  68. if (!_this2.paused && !_this2.autoPause) {
  69. _this2._schedule();
  70. }
  71. } else if (!_this2.paused) {
  72. _this2._schedule();
  73. }
  74. }
  75. });
  76. }
  77. }]);
  78. return StutteredPipe;
  79. }(events.EventEmitter);
  80. module.exports = StutteredPipe;
  81. //# sourceMappingURL=stuttered-pipe.js.map