var t=function(s,e){if(!(this instanceof t))return new t(s,e);this.url=s,e=e||{},this.headers=e.headers||{},this.payload=void 0!==e.payload?e.payload:"",this.method=e.method||(this.payload?"POST":"GET"),this.withCredentials=!!e.withCredentials,this.debug=!!e.debug,this.FIELD_SEPARATOR=":",this.listeners={},this.xhr=null,this.readyState=t.INITIALIZING,this.progress=0,this.chunk="",this.lastEventId="",this.addEventListener=function(t,s){void 0===this.listeners[t]&&(this.listeners[t]=[]),-1===this.listeners[t].indexOf(s)&&this.listeners[t].push(s)},this.removeEventListener=function(t,s){if(void 0===this.listeners[t])return;const e=[];this.listeners[t].forEach((function(t){t!==s&&e.push(t)})),0===e.length?delete this.listeners[t]:this.listeners[t]=e},this.dispatchEvent=function(t){if(!t)return!0;this.debug,t.source=this;const s="on"+t.type;return(!this.hasOwnProperty(s)||(this[s].call(this,t),!t.defaultPrevented))&&(!this.listeners[t.type]||this.listeners[t.type].every((function(s){return s(t),!t.defaultPrevented})))},this._markClosed=function(){this.xhr=null,this.progress=0,this.chunk="",this._setReadyState(t.CLOSED)},this._setReadyState=function(t){const s=new CustomEvent("readystatechange");s.readyState=t,this.readyState=t,this.dispatchEvent(s)},this._onStreamFailure=function(t){const s=new CustomEvent("error");s.responseCode=t.currentTarget.status,s.data=t.currentTarget.response,this.dispatchEvent(s),this._markClosed()},this._onStreamAbort=function(){this.dispatchEvent(new CustomEvent("abort")),this._markClosed()},this._onStreamProgress=function(t){if(!this.xhr)return;if(this.xhr.status<200||this.xhr.status>=300)return void this._onStreamFailure(t);const s=this.xhr.responseText.substring(this.progress);this.progress+=s.length;const e=(this.chunk+s).split(/(\r\n\r\n|\r\r|\n\n)/g),i=e.pop();e.forEach(function(t){t.trim().length>0&&this.dispatchEvent(this._parseEventChunk(t))}.bind(this)),this.chunk=i},this._onStreamLoaded=function(t){this._onStreamProgress(t),this.dispatchEvent(this._parseEventChunk(this.chunk)),this.chunk="",this._markClosed()},this._parseEventChunk=function(t){if(!t||0===t.length)return null;this.debug;const s={id:null,retry:null,data:null,event:null};t.split(/\n|\r\n|\r/).forEach(function(t){const e=t.indexOf(this.FIELD_SEPARATOR);let i,n;if(e>0){const s=" "===t[e+1]?2:1;i=t.substring(0,e),n=t.substring(e+s)}else{if(!(e<0))return;i=t,n=""}i in s&&("data"===i&&null!==s[i]?s.data+="\n"+n:s[i]=n)}.bind(this)),null!==s.id&&(this.lastEventId=s.id);const e=new CustomEvent(s.event||"message");return e.id=s.id,e.data=s.data||"",e.lastEventId=this.lastEventId,e},this._onReadyStateChange=function(){if(this.xhr&&this.xhr.readyState===XMLHttpRequest.HEADERS_RECEIVED){const s={},e=this.xhr.getAllResponseHeaders().trim().split("\r\n");for(const t of e){const[e,...i]=t.split(":"),n=i.join(":").trim();s[e.trim().toLowerCase()]=s[e.trim().toLowerCase()]||[],s[e.trim().toLowerCase()].push(n)}const i=new CustomEvent("open");i.responseCode=this.xhr.status,i.headers=s,this.dispatchEvent(i),this._setReadyState(t.OPEN)}},this.stream=function(){if(!this.xhr){this._setReadyState(t.CONNECTING),this.xhr=new XMLHttpRequest,this.xhr.addEventListener("progress",this._onStreamProgress.bind(this)),this.xhr.addEventListener("load",this._onStreamLoaded.bind(this)),this.xhr.addEventListener("readystatechange",this._onReadyStateChange.bind(this)),this.xhr.addEventListener("error",this._onStreamFailure.bind(this)),this.xhr.addEventListener("abort",this._onStreamAbort.bind(this)),this.xhr.open(this.method,this.url);for(let t in this.headers)this.xhr.setRequestHeader(t,this.headers[t]);this.lastEventId.length>0&&this.xhr.setRequestHeader("Last-Event-ID",this.lastEventId),this.xhr.withCredentials=this.withCredentials,this.xhr.send(this.payload)}},this.close=function(){this.readyState!==t.CLOSED&&this.xhr.abort()},(void 0===e.start||e.start)&&this.stream()};t.INITIALIZING=-1,t.CONNECTING=0,t.OPEN=1,t.CLOSED=2,"undefined"!=typeof exports&&(exports.SSE=t);export{t as SSE};
