"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.readStream = readStream;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _awaitAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/awaitAsyncGenerator"));
var _wrapAsyncGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapAsyncGenerator"));
var _utils = require("./utils");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function readStream(_x) {
  return _readStream.apply(this, arguments);
}
function _readStream() {
  _readStream = (0, _wrapAsyncGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(response) {
    var reader, doneStreaming, chunkBuffer, _yield$_awaitAsyncGen, value, done, processedChunks, _iterator, _step, chunk, jsonChunk, error, _t, _t2, _t3;
    return _regenerator.default.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          reader = (0, _utils.getBufferReader)(response);
          doneStreaming = false;
          chunkBuffer = '';
        case 1:
          _context.prev = 1;
          _context.next = 2;
          return (0, _awaitAsyncGenerator2.default)(reader.read());
        case 2:
          _yield$_awaitAsyncGen = _context.sent;
          value = _yield$_awaitAsyncGen.value;
          done = _yield$_awaitAsyncGen.done;
          doneStreaming = done;
          if (!value) {
            _context.next = 12;
            break;
          }
          processedChunks = "".concat(chunkBuffer).concat(value).split('\n'); // assumes that all JSON chunks are separated by a new-line;
          chunkBuffer = '';
          _iterator = _createForOfIteratorHelper(processedChunks);
          _context.prev = 3;
          _iterator.s();
        case 4:
          if ((_step = _iterator.n()).done) {
            _context.next = 9;
            break;
          }
          chunk = _step.value;
          _context.prev = 5;
          jsonChunk = JSON.parse(chunk);
          if (jsonChunk.type === 'ERROR') {
            reader.cancel();
          }
          _context.next = 6;
          return jsonChunk;
        case 6:
          _context.next = 8;
          break;
        case 7:
          _context.prev = 7;
          _t = _context["catch"](5);
          // the chunk may be incomplete, so we'll save it for the next iteration.
          chunkBuffer = chunk;
        case 8:
          _context.next = 4;
          break;
        case 9:
          _context.next = 11;
          break;
        case 10:
          _context.prev = 10;
          _t2 = _context["catch"](3);
          _iterator.e(_t2);
        case 11:
          _context.prev = 11;
          _iterator.f();
          return _context.finish(11);
        case 12:
          _context.next = 15;
          break;
        case 13:
          _context.prev = 13;
          _t3 = _context["catch"](1);
          if (!(_t3 instanceof DOMException && _t3.name === 'AbortError')) {
            _context.next = 14;
            break;
          }
          doneStreaming = true;
          _context.next = 15;
          break;
        case 14:
          reader.cancel();
          throw _t3;
        case 15:
          if (!doneStreaming) {
            _context.next = 1;
            break;
          }
        case 16:
          if (!chunkBuffer) {
            _context.next = 17;
            break;
          }
          error = new Error("Not all the data was processed: ".concat(chunkBuffer));
          throw error;
        case 17:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 13], [3, 10, 11, 12], [5, 7]]);
  }));
  return _readStream.apply(this, arguments);
}