line-stream.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _stream = require('./stream');
  7. var _stream2 = _interopRequireDefault(_stream);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  11. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
  12. * @file m3u8/line-stream.js
  13. */
  14. /**
  15. * A stream that buffers string input and generates a `data` event for each
  16. * line.
  17. *
  18. * @class LineStream
  19. * @extends Stream
  20. */
  21. var LineStream = function (_Stream) {
  22. _inherits(LineStream, _Stream);
  23. function LineStream() {
  24. _classCallCheck(this, LineStream);
  25. var _this = _possibleConstructorReturn(this, (LineStream.__proto__ || Object.getPrototypeOf(LineStream)).call(this));
  26. _this.buffer = '';
  27. return _this;
  28. }
  29. /**
  30. * Add new data to be parsed.
  31. *
  32. * @param {String} data the text to process
  33. */
  34. _createClass(LineStream, [{
  35. key: 'push',
  36. value: function push(data) {
  37. var nextNewline = void 0;
  38. this.buffer += data;
  39. nextNewline = this.buffer.indexOf('\n');
  40. for (; nextNewline > -1; nextNewline = this.buffer.indexOf('\n')) {
  41. this.trigger('data', this.buffer.substring(0, nextNewline));
  42. this.buffer = this.buffer.substring(nextNewline + 1);
  43. }
  44. }
  45. }]);
  46. return LineStream;
  47. }(_stream2['default']);
  48. exports['default'] = LineStream;