  function TextDecoder() {
  }

  TextDecoder.prototype.decode = function (octets) {
    var string = "";
    var i = 0;
    while (i < octets.length) {
      var octet = octets[i];
      var bytesNeeded = 0;
      var codePoint = 0;
      if (octet <= 0x7F) {
        bytesNeeded = 0;
        codePoint = octet & 0xFF;
      } else if (octet <= 0xDF) {
        bytesNeeded = 1;
        codePoint = octet & 0x1F;
      } else if (octet <= 0xEF) {
        bytesNeeded = 2;
        codePoint = octet & 0x0F;
      } else if (octet <= 0xF4) {
        bytesNeeded = 3;
        codePoint = octet & 0x07;
      }
      if (octets.length - i - bytesNeeded > 0) {
        var k = 0;
        while (k < bytesNeeded) {
          octet = octets[i + k + 1];
          codePoint = (codePoint << 6) | (octet & 0x3F);
          k += 1;
        }
      } else {
        codePoint = 0xFFFD;
        bytesNeeded = octets.length - i;
      }
      string += String.fromCodePoint(codePoint);
      i += bytesNeeded + 1;
    }
    return string
  };

  function FetchTransport(fetch, onStartCallback, onProgressCallback, onFinishCallback, thisArg) {
    this._internal = new FetchTransportInternal(fetch, onStartCallback, onProgressCallback, onFinishCallback, thisArg);
  }

  FetchTransport.prototype.open = function (url, withCredentials) {
    this._internal.open(url, withCredentials);
  };

  FetchTransport.prototype.cancel = function () {
    this._internal.cancel();
  };

  function FetchTransportInternal(fetch, onStartCallback, onProgressCallback, onFinishCallback, thisArg) {
    this.fetch = fetch;
    this.onStartCallback = onStartCallback;
    this.onProgressCallback = onProgressCallback;
    this.onFinishCallback = onFinishCallback;
    this.thisArg = thisArg;
    this.reader = undefined;
    this.lastRequestId = 0;
    this.octets = [];
    this.textDecoder = new TextDecoder();
  }

  FetchTransportInternal.prototype.read = function () {
    var that = this;
    return new Promise(function (resolve, reject) {
      var onRead = function () {
        if (result.done) {
          resolve(undefined);
        } else {
          var value = result.value;
          var n = 0;
          for (var i = 0; i < value.length; i += 1) {
            var c = value[i];
            if (c === "\n".charCodeAt(0) || c === "\r".charCodeAt(0)) {
              n = i + 1;
            }
          }
          var k = that.octets.length;
          for (var i = 0; i < n; i += 1) {
            that.octets[k + i] = value[i];
          }
          var chunk = "";
          if (n !== 0) {
            chunk = that.textDecoder.decode(that.octets);//String.fromCharCode.apply(undefined, that.octets);
            that.octets.length = 0;
            k = 0;
          }
          for (var i = n; i < value.length; i += 1) {
            that.octets[k + i - n] = value[i];
          }
          that.onProgressCallback.call(that.thisArg, chunk);
          that.reader.read().then(onRead)["catch"](reject);
        }
      };
      that.reader.read().then(onRead);
    });
  };

  FetchTransportInternal.prototype.open = function (url, withCredentials) {
    this.cancel();
    this.octets.length = 0;
    var that = this;
    var requestId = this.lastRequestId;
    this.fetch(url, {
      headers: {
        "Accept": "text/event-stream"
      },
      credentials: withCredentials ? "include" : "same-origin",
      cache: "no-store"
    }).then(function (response) {
      if (lastRequestId === this.lastRequestId) {
        that.reader = response.body.getReader();
        that.onStartCallback.call(that.thisArg, response.status, response.statusText, response.headers.get("Content-Type"));
        if (lastRequestId !== that.lastRequestId) {
          return undefined;
        }
        return that.read();
      }
      return undefined;
    }).then(function (error) {
      that.onFinishCallback.call(that.thisArg);
      if (error != undefined) {
        throw error;
      }
    })["catch"](function (error) {
      return error;
    });
  };

  FetchTransportInternal.prototype.cancel = function () {
    this.lastRequestId += 1;
    if (this.reader != undefined) {
      this.reader.cancel();
      this.reader = undefined;
    }
  };
