Files
sysPass/js/eventsource.min.js
nuxsmin e76c819273 * [FIX] Fixed issue when user changes its password
* [FIX] Fixed issue when using temporary master password
* [MOD] Updated translations
2017-03-01 03:25:19 +01:00

27 lines
9.8 KiB
JavaScript

/*
eventsource.js
Available under MIT License (MIT)
https://github.com/Yaffle/EventSource/
*/
(function(e){function y(a,c,b,d,f){this._internal=new g(a,c,b,d,f)}function g(a,c,b,d,f){this.onStartCallback=c;this.onProgressCallback=b;this.onFinishCallback=d;this.thisArg=f;this.xhr=a;this.offset=this.charOffset=this.state=0;this.url="";this.withCredentials=!1;this.timeout=0}function m(){this._data={}}function r(){this._listeners=new m}function t(a){h(function(){throw a;},0)}function n(a){this.type=a;this.target=void 0}function A(a,c){n.call(this,a);this.data=c.data;this.lastEventId=c.lastEventId}
function p(a,c){this._listeners=new m;this.onerror=this.onmessage=this.onopen=void 0;this.url="";this.readyState=0;this.withCredentials=!1;this._internal=new k(this,a,c)}function k(a,c,b){this.url=c.toString();this.readyState=0;this.withCredentials=u&&void 0!=b&&!!b.withCredentials;this.es=a;this.initialRetry=v(1E3,0);this.heartbeatTimeout=v(45E3,0);this.lastEventId="";this.retry=this.initialRetry;this.wasActivity=!1;a=new (void 0!=b&&void 0!=b.Transport?b.Transport:B);this.transport=new y(a,this.onStart,
this.onProgress,this.onFinish,this);this.timeout=0;this.currentState=-1;this.dataBuffer=[];this.eventTypeBuffer=this.lastEventIdBuffer="";this.state=4;this.valueStart=this.fieldStart=0;this.es.url=this.url;this.es.readyState=this.readyState;this.es.withCredentials=this.withCredentials;this.onTimeout()}function z(){this.CONNECTING=0;this.OPEN=1;this.CLOSED=2}var h=e.setTimeout,l=e.clearTimeout,q=function(){};y.prototype.open=function(a,c){this._internal.open(a,c)};y.prototype.cancel=function(){this._internal.cancel()};
g.prototype.onStart=function(){if(1===this.state){this.state=2;var a=0,c="",b=void 0;if("contentType"in this.xhr)a=200,c="OK",b=this.xhr.contentType;else try{a=this.xhr.status,c=this.xhr.statusText,b=this.xhr.getResponseHeader("Content-Type")}catch(d){a=0,c="",b=void 0}void 0==b&&(b="");this.onStartCallback.call(this.thisArg,a,c,b)}};g.prototype.onProgress=function(){this.onStart();if(2===this.state||3===this.state){this.state=3;var a="";try{a=this.xhr.responseText}catch(C){}for(var c=this.charOffset,
b=a.length,d=this.offset;d<b;d+=1){var f=a.charCodeAt(d);if(10===f||13===f)this.charOffset=d+1}this.offset=b;a=a.slice(c,this.charOffset);this.onProgressCallback.call(this.thisArg,a)}};g.prototype.onFinish=function(){this.onProgress();3===this.state&&(this.state=4,0!==this.timeout&&(l(this.timeout),this.timeout=0),this.onFinishCallback.call(this.thisArg))};g.prototype.onReadyStateChange=function(){if(void 0!=this.xhr)if(4===this.xhr.readyState)this.onFinish();else if(3===this.xhr.readyState)this.onProgress()};
g.prototype.onTimeout2=function(){this.timeout=0;var a=/^data\:([^,]*?)(base64)?,([\S]*)$/.exec(this.url),c=a[1],a="base64"===a[2]?e.atob(a[3]):decodeURIComponent(a[3]);1===this.state&&(this.state=2,this.onStartCallback.call(this.thisArg,200,"OK",c));if(2===this.state||3===this.state)this.state=3,this.onProgressCallback.call(this.thisArg,a);3===this.state&&(this.state=4,this.onFinishCallback.call(this.thisArg))};g.prototype.onTimeout1=function(){this.timeout=0;this.open(this.url,this.withCredentials)};
g.prototype.onTimeout0=function(){var a=this;this.timeout=h(function(){a.onTimeout0()},500);if(3===this.xhr.readyState)this.onProgress()};g.prototype.handleEvent=function(a){if("load"===a.type)this.onFinish();else if("error"===a.type)this.onFinish();else if("abort"===a.type)this.onFinish();else if("progress"===a.type)this.onProgress();else if("readystatechange"===a.type)this.onReadyStateChange()};g.prototype.open=function(a,c){0!==this.timeout&&(l(this.timeout),this.timeout=0);this.url=a;this.withCredentials=
c;this.state=1;this.offset=this.charOffset=0;var b=this;if(void 0!=/^data\:([^,]*?)(?:;base64)?,[\S]*$/.exec(a))this.timeout=h(function(){b.onTimeout2()},0);else if((!("ontimeout"in this.xhr)||"sendAsBinary"in this.xhr||"mozAnon"in this.xhr)&&void 0!=e.document&&void 0!=e.document.readyState&&"complete"!==e.document.readyState)this.timeout=h(function(){b.onTimeout1()},4);else{this.xhr.onload=function(a){b.handleEvent({type:"load"})};this.xhr.onerror=function(){b.handleEvent({type:"error"})};this.xhr.onabort=
function(){b.handleEvent({type:"abort"})};this.xhr.onprogress=function(){b.handleEvent({type:"progress"})};this.xhr.onreadystatechange=function(){b.handleEvent({type:"readystatechange"})};this.xhr.open("GET",a,!0);this.xhr.withCredentials=c;this.xhr.responseType="text";"setRequestHeader"in this.xhr&&this.xhr.setRequestHeader("Accept","text/event-stream");try{this.xhr.send(void 0)}catch(d){throw d;}"readyState"in this.xhr&&void 0!=e.opera&&(this.timeout=h(function(){b.onTimeout0()},0))}};g.prototype.cancel=
function(){0!==this.state&&4!==this.state&&(this.state=4,this.xhr.onload=q,this.xhr.onerror=q,this.xhr.onabort=q,this.xhr.onprogress=q,this.xhr.onreadystatechange=q,this.xhr.abort(),0!==this.timeout&&(l(this.timeout),this.timeout=0),this.onFinishCallback.call(this.thisArg));this.state=0};m.prototype.get=function(a){return this._data[a+"~"]};m.prototype.set=function(a,c){this._data[a+"~"]=c};m.prototype["delete"]=function(a){delete this._data[a+"~"]};r.prototype.dispatchEvent=function(a){a.target=
this;var c=this._listeners.get(a.type.toString());if(void 0!=c)for(var b=c.length,d,f=0;f<b;f+=1){d=c[f];try{"function"===typeof d.handleEvent?d.handleEvent(a):d.call(this,a)}catch(C){t(C)}}};r.prototype.addEventListener=function(a,c){a=a.toString();var b=this._listeners,d=b.get(a);void 0==d&&(d=[],b.set(a,d));for(b=d.length;0<=b;--b)if(d[b]===c)return;d.push(c)};r.prototype.removeEventListener=function(a,c){a=a.toString();var b=this._listeners,d=b.get(a);if(void 0!=d){for(var f=d.length,e=[],g=0;g<
f;g+=1)d[g]!==c&&e.push(d[g]);if(0===e.length)b["delete"](a);else b.set(a,e)}};A.prototype=n.prototype;var w=e.XMLHttpRequest,D=e.XDomainRequest,u=void 0!=w&&void 0!=(new w).withCredentials,B=u||void 0!=w&&void 0==D?w:D,E=/^text\/event\-stream;?(\s*charset\=utf\-8)?$/i,v=function(a,c){var b=a;b!==b&&(b=c);return 1E3>b?1E3:18E6<b?18E6:b},x=function(a,c,b){try{"function"===typeof c&&c.call(a,b)}catch(d){t(d)}};k.prototype.onStart=function(a,c,b){0===this.currentState&&(void 0==b&&(b=""),200===a&&E.test(b)?
(this.currentState=1,this.wasActivity=!0,this.retry=this.initialRetry,this.readyState=1,this.es.readyState=1,a=new n("open"),this.es.dispatchEvent(a),x(this.es,this.es.onopen,a)):0!==a&&(a=200!==a?"EventSource's response has a status "+a+" "+c.replace(/\s+/g," ")+" that is not 200. Aborting the connection.":"EventSource's response has a Content-Type specifying an unsupported type: "+b.replace(/\s+/g," ")+". Aborting the connection.",t(Error(a)),this.close(),a=new n("error"),this.es.dispatchEvent(a),
x(this.es,this.es.onerror,a)))};k.prototype.onProgress=function(a){if(1===this.currentState){var c=a.length;0!==c&&(this.wasActivity=!0);for(var b=0;b<c;b+=1){var d=a.charCodeAt(b);if(3===this.state&&10===d)this.state=4;else if(3===this.state&&(this.state=4),13===d||10===d){if(4!==this.state){5===this.state&&(this.valueStart=b+1);var f=a.slice(this.fieldStart,this.valueStart-1),e=a.slice(this.valueStart+(this.valueStart<b&&32===a.charCodeAt(this.valueStart)?1:0),b);if("data"===f)this.dataBuffer.push(e);
else if("id"===f)this.lastEventIdBuffer=e;else if("event"===f)this.eventTypeBuffer=e;else if("retry"===f)this.retry=this.initialRetry=v(Number(e),this.initialRetry);else if("heartbeatTimeout"===f&&(this.heartbeatTimeout=v(Number(e),this.heartbeatTimeout),0!==this.timeout)){l(this.timeout);var g=this;this.timeout=h(function(){g.onTimeout()},this.heartbeatTimeout)}}if(4===this.state){if(0!==this.dataBuffer.length&&(this.lastEventId=this.lastEventIdBuffer,""===this.eventTypeBuffer&&(this.eventTypeBuffer=
"message"),f=new A(this.eventTypeBuffer,{data:this.dataBuffer.join("\n"),lastEventId:this.lastEventIdBuffer}),this.es.dispatchEvent(f),"message"===this.eventTypeBuffer&&x(this.es,this.es.onmessage,f),2===this.currentState))break;this.dataBuffer.length=0;this.eventTypeBuffer=""}this.state=13===d?3:4}else 4===this.state&&(this.fieldStart=b,this.state=5),5===this.state?58===d&&(this.valueStart=b+1,this.state=6):6===this.state&&(this.state=7)}}};k.prototype.onFinish=function(){if(1===this.currentState||
0===this.currentState){this.currentState=-1;0!==this.timeout&&(l(this.timeout),this.timeout=0);this.retry>16*this.initialRetry&&(this.retry=16*this.initialRetry);18E6<this.retry&&(this.retry=18E6);var a=this;this.timeout=h(function(){a.onTimeout()},this.retry);this.retry=2*this.retry+1;this.readyState=0;this.es.readyState=0;var c=new n("error");this.es.dispatchEvent(c);x(this.es,this.es.onerror,c)}};k.prototype.onTimeout=function(){this.timeout=0;if(-1!==this.currentState)if(this.wasActivity){this.wasActivity=
!1;var a=this;this.timeout=h(function(){a.onTimeout()},this.heartbeatTimeout)}else t(Error("No activity within "+this.heartbeatTimeout+" milliseconds. Reconnecting.")),this.transport.cancel();else{this.wasActivity=!1;a=this;this.timeout=h(function(){a.onTimeout()},this.heartbeatTimeout);this.currentState=0;this.dataBuffer.length=0;this.eventTypeBuffer="";this.lastEventIdBuffer=this.lastEventId;this.valueStart=this.fieldStart=0;this.state=4;var c=this.url.slice(0,5),c="data:"!==c&&"blob:"!==c?this.url+
((-1===this.url.indexOf("?",0)?"?":"&")+"lastEventId="+encodeURIComponent(this.lastEventId)+"&r="+(Math.random()+1).toString().slice(2)):this.url;try{this.transport.open(c,this.withCredentials)}catch(b){throw this.close(),b;}}};k.prototype.close=function(){this.currentState=2;this.transport.cancel();0!==this.timeout&&(l(this.timeout),this.timeout=0);this.readyState=2;this.es.readyState=2};z.prototype=r.prototype;p.prototype=new z;p.prototype.close=function(){this._internal.close()};z.call(p);u&&(p.prototype.withCredentials=
void 0);void 0==B||void 0!=e.EventSource&&(!u||void 0!=e.EventSource&&"withCredentials"in e.EventSource.prototype)||(e.NativeEventSource=e.EventSource,e.EventSource=p)})("undefined"!==typeof window?window:this);