mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-08 01:16:56 +01:00
* [MOD] Added import events to fixed ones * [MOD] Minor code tweaks Signed-off-by: Rubén D <nuxsmin@syspass.org>
27 lines
9.9 KiB
JavaScript
27 lines
9.9 KiB
JavaScript
/*
|
|
|
|
eventsource.js
|
|
Available under MIT License (MIT)
|
|
https://github.com/Yaffle/EventSource/
|
|
*/
|
|
(function(h){function R(){this.codePoint=this.bitsNeeded=0}function y(a){this.withCredentials=!1;this.responseType="";this.status=this.readyState=0;this.responseText=this.statusText="";this.onreadystatechange=this.onprogress=k;this._contentType="";this._xhr=a;this._sendTimeout=0;this._abort=k}function S(a){return a.replace(/[A-Z]/g,function(a){return String.fromCharCode(a.charCodeAt(0)+32)})}function T(a){var c=Object.create(null);a=a.split("\r\n");for(var d=0;d<a.length;d+=1){var b=a[d].split(": "),
|
|
e=b.shift();b=b.join(": ");c[S(e)]=b}this._map=c}function U(){}function V(a){this._headers=a}function W(){}function t(){this._listeners=Object.create(null)}function L(a){w(function(){throw a;},0)}function G(a){this.type=a;this.target=void 0}function X(a,c){G.call(this,a);this.data=c.data;this.lastEventId=c.lastEventId}function O(a,c){G.call(this,a);this.status=c.status;this.statusText=c.statusText;this.headers=c.headers}function n(a,c){t.call(this);this._close=this.withCredentials=this.readyState=
|
|
this.url=this.onerror=this.onmessage=this.onopen=void 0;ca(this,a,c)}function ca(a,c,d){c=String(c);var b=void 0!=d&&!!d.withCredentials,e=M(1E3),f=void 0!=d&&void 0!=d.heartbeatTimeout?P(d.heartbeatTimeout,45E3):M(45E3),r="",g=e,H=!1,l=void 0!=d&&void 0!=d.headers?JSON.parse(JSON.stringify(d.headers)):void 0,A=void 0!=d&&void 0!=d.Transport?d.Transport:void 0!=z&&"withCredentials"in z.prototype||void 0==Y?z:Y,h=!da||void 0!=d&&void 0!=d.Transport?new y(new A):void 0,n=void 0==h?new W:new U,x=void 0,
|
|
p=0,u=-1,k="",q="",B="",t="",m=0,v=0,C=0,F=function(b,d,c,f,l){0===u&&(x=l,200===b&&void 0!=c&&fa.test(c)?(u=1,H=!0,g=e,a.readyState=1,b=new O("open",{status:b,statusText:d,headers:f}),a.dispatchEvent(b),N(a,a.onopen,b)):(200!==b?(d&&(d=d.replace(/\s+/g," ")),c="EventSource's response has a status "+b+" "+d+" that is not 200. Aborting the connection."):c="EventSource's response has a Content-Type specifying an unsupported type: "+(void 0==c?"-":c.replace(/\s+/g," "))+". Aborting the connection.",
|
|
L(Error(c)),D(),b=new O("error",{status:b,statusText:d,headers:f}),a.dispatchEvent(b),N(a,a.onerror,b)))},J=function(b){if(1===u){for(var c=-1,d=0;d<b.length;d+=1){var l=b.charCodeAt(d);if(10===l||13===l)c=d}d=(-1!==c?t:"")+b.slice(0,c+1);t=(-1===c?t:"")+b.slice(c+1);""!==d&&(H=!0);for(b=0;b<d.length;b+=1)if(l=d.charCodeAt(b),-1===m&&10===l)m=0;else if(-1===m&&(m=0),13===l||10===l){if(0!==m){1===m&&(C=b+1);c=d.slice(v,C-1);var A=d.slice(C+(C<b&&32===d.charCodeAt(C)?1:0),b);"data"===c?(k+="\n",k+=
|
|
A):"id"===c?q=A:"event"===c?B=A:"retry"===c?g=e=P(A,e):"heartbeatTimeout"===c&&(f=P(A,f),0!==p&&(E(p),p=w(function(){I()},f)))}if(0===m){if(""!==k&&(r=q,""===B&&(B="message"),c=new X(B,{data:k.slice(1),lastEventId:q}),a.dispatchEvent(c),"message"===B&&N(a,a.onmessage,c),2===u))break;B=k=""}m=13===l?-1:0}else 0===m&&(v=b,m=1),1===m?58===l&&(C=b+1,m=2):2===m&&(m=3)}},K=function(){if(1===u||0===u){u=-1;0!==p&&(E(p),p=0);p=w(function(){I()},g);g=M(Math.min(16*e,2*g));a.readyState=0;var b=new G("error");
|
|
a.dispatchEvent(b);N(a,a.onerror,b)}},D=function(){u=2;void 0!=x&&(x(),x=void 0);0!==p&&(E(p),p=0);a.readyState=2},I=function(){p=0;if(-1!==u)H||void 0==x?(H=!1,p=w(function(){I()},f)):(L(Error("No activity within "+f+" milliseconds. Reconnecting.")),x(),x=void 0);else{H=!1;p=w(function(){I()},f);u=0;B=k="";q=r;t="";m=C=v=0;var a=c;"data:"!==c.slice(0,5)&&"blob:"!==c.slice(0,5)&&""!==r&&(a+=(-1===c.indexOf("?")?"?":"&")+"lastEventId="+encodeURIComponent(r));var d={Accept:"text/event-stream"};if(void 0!=
|
|
l)for(var e in l)Object.prototype.hasOwnProperty.call(l,e)&&(d[e]=l[e]);try{n.open(h,F,J,K,a,b,d)}catch(ea){throw D(),ea;}}};a.url=c;a.readyState=0;a.withCredentials=b;a._close=D;I()}var w=h.setTimeout,E=h.clearTimeout,z=h.XMLHttpRequest,Y=h.XDomainRequest,J=h.EventSource,K=h.document,q=h.Promise,v=h.fetch,Z=h.Response,F=h.TextDecoder,aa=h.TextEncoder,D=h.AbortController;void 0==Object.create&&(Object.create=function(a){function c(){}c.prototype=a;return new c});void 0!=q&&void 0==q.prototype["finally"]&&
|
|
(q.prototype["finally"]=function(a){return this.then(function(c){return q.resolve(a()).then(function(){return c})},function(c){return q.resolve(a()).then(function(){throw c;})})});if(void 0!=v){var ha=v;v=function(a,c){return q.resolve(ha(a,c))}}void 0==D&&(D=function(){this.signal=null;this.abort=function(){}});R.prototype.decode=function(a){function c(a,b,c){if(1===c)return a>=128>>b&&2047>=a<<b;if(2===c)return a>=2048>>b&&55295>=a<<b||a>=57344>>b&&65535>=a<<b;if(3===c)return a>=65536>>b&&1114111>=
|
|
a<<b;throw Error();}function d(a,b){if(6===a)return 15<b>>6?3:31<b?2:1;if(12===a)return 15<b?3:2;if(18===a)return 3;throw Error();}for(var b="",e=this.bitsNeeded,f=this.codePoint,r=0;r<a.length;r+=1){var g=a[r];0!==e&&(128>g||191<g||!c(f<<6|g&63,e-6,d(e,f)))&&(e=0,f=65533,b+=String.fromCharCode(f));0===e?(0<=g&&127>=g?(e=0,f=g):192<=g&&223>=g?(e=6,f=g&31):224<=g&&239>=g?(e=12,f=g&15):240<=g&&247>=g?(e=18,f=g&7):(e=0,f=65533),0===e||c(f,e,d(e,f))||(e=0,f=65533)):(e-=6,f=f<<6|g&63);0===e&&(65535>=f?
|
|
b+=String.fromCharCode(f):(b+=String.fromCharCode(55296+(f-65535-1>>10)),b+=String.fromCharCode(56320+(f-65535-1&1023))))}this.bitsNeeded=e;this.codePoint=f;return b};var Q;if(!(Q=void 0==F||void 0==aa)){a:{try{var ba="test"===(new F).decode((new aa).encode("test"),{stream:!0});break a}catch(a){console.log(a)}ba=!1}Q=!ba}Q&&(F=R);var k=function(){};y.prototype.open=function(a,c){this._abort(!0);var d=this,b=this._xhr,e=1,f=0;this._abort=function(a){0!==d._sendTimeout&&(E(d._sendTimeout),d._sendTimeout=
|
|
0);if(1===e||2===e||3===e)e=4,b.onload=k,b.onerror=k,b.onabort=k,b.onprogress=k,b.onreadystatechange=k,b.abort(),0!==f&&(E(f),f=0),a||(d.readyState=4,d.onreadystatechange());e=0};var r=function(){if(1===e){var a=0,c="",f=void 0;if("contentType"in b)a=200,c="OK",f=b.contentType;else try{a=b.status,c=b.statusText,f=b.getResponseHeader("Content-Type")}catch(x){a=0,c="",f=void 0}0!==a&&(e=2,d.readyState=2,d.status=a,d.statusText=c,d._contentType=f,d.onreadystatechange())}},g=function(){r();if(2===e||
|
|
3===e){e=3;var a="";try{a=b.responseText}catch(ia){}d.readyState=3;d.responseText=a;d.onprogress()}},h=function(){g();if(1===e||2===e||3===e)e=4,0!==f&&(E(f),f=0),d.readyState=4,d.onreadystatechange()},l=function(){f=w(function(){l()},500);3===b.readyState&&g()};b.onload=h;b.onerror=h;b.onabort=h;"sendAsBinary"in z.prototype||"mozAnon"in z.prototype||(b.onprogress=g);b.onreadystatechange=function(){void 0!=b&&(4===b.readyState?h():3===b.readyState?g():2===b.readyState&&r())};"contentType"in b&&(c+=
|
|
(-1===c.indexOf("?")?"?":"&")+"padding=true");b.open(a,c,!0);"readyState"in b&&(f=w(function(){l()},0))};y.prototype.abort=function(){this._abort(!1)};y.prototype.getResponseHeader=function(a){return this._contentType};y.prototype.setRequestHeader=function(a,c){var d=this._xhr;"setRequestHeader"in d&&d.setRequestHeader(a,c)};y.prototype.getAllResponseHeaders=function(){return void 0!=this._xhr.getAllResponseHeaders?this._xhr.getAllResponseHeaders():""};y.prototype.send=function(){if("ontimeout"in
|
|
z.prototype||void 0==K||void 0==K.readyState||"complete"===K.readyState){var a=this._xhr;a.withCredentials=this.withCredentials;a.responseType=this.responseType;try{a.send(void 0)}catch(d){throw d;}}else{var c=this;c._sendTimeout=w(function(){c._sendTimeout=0;c.send()},4)}};T.prototype.get=function(a){return this._map[S(a)]};U.prototype.open=function(a,c,d,b,e,f,h){a.open("GET",e);var g=0;a.onprogress=function(){var b=a.responseText.slice(g);g+=b.length;d(b)};a.onreadystatechange=function(){if(2===
|
|
a.readyState){var d=a.status,e=a.statusText,f=a.getResponseHeader("Content-Type"),g=a.getAllResponseHeaders();c(d,e,f,new T(g),function(){a.abort()})}else 4===a.readyState&&b()};a.withCredentials=f;a.responseType="text";for(var k in h)Object.prototype.hasOwnProperty.call(h,k)&&a.setRequestHeader(k,h[k]);a.send()};V.prototype.get=function(a){return this._headers.get(a)};W.prototype.open=function(a,c,d,b,e,f,h){var g=new D;a=g.signal;var k=new F;v(e,{headers:h,credentials:f?"include":"same-origin",
|
|
signal:a,cache:"no-store"}).then(function(a){var b=a.body.getReader();c(a.status,a.statusText,a.headers.get("Content-Type"),new V(a.headers),function(){g.abort();b.cancel()});return new q(function(a,c){var e=function(){b.read().then(function(b){b.done?a(void 0):(b=k.decode(b.value,{stream:!0}),d(b),e())})["catch"](function(a){c(a)})};e()})})["finally"](function(){b()})};t.prototype.dispatchEvent=function(a){a.target=this;var c=this._listeners[a.type];if(void 0!=c)for(var d=c.length,b=0;b<d;b+=1){var e=
|
|
c[b];try{"function"===typeof e.handleEvent?e.handleEvent(a):e.call(this,a)}catch(f){L(f)}}};t.prototype.addEventListener=function(a,c){a=String(a);var d=this._listeners,b=d[a];void 0==b&&(b=[],d[a]=b);a=!1;for(d=0;d<b.length;d+=1)b[d]===c&&(a=!0);a||b.push(c)};t.prototype.removeEventListener=function(a,c){a=String(a);var d=this._listeners,b=d[a];if(void 0!=b){for(var e=[],f=0;f<b.length;f+=1)b[f]!==c&&e.push(b[f]);0===e.length?delete d[a]:d[a]=e}};X.prototype=Object.create(G.prototype);O.prototype=
|
|
Object.create(G.prototype);var fa=/^text\/event\-stream;?(\s*charset=utf\-8)?$/i,P=function(a,c){a=parseInt(a,10);a!==a&&(a=c);return M(a)},M=function(a){return Math.min(Math.max(a,1E3),18E6)},N=function(a,c,d){try{"function"===typeof c&&c.call(a,d)}catch(b){L(b)}},da=void 0!=v&&void 0!=Z&&"body"in Z.prototype;n.prototype=Object.create(t.prototype);n.prototype.CONNECTING=0;n.prototype.OPEN=1;n.prototype.CLOSED=2;n.prototype.close=function(){this.controller.abort();this._close()};n.CONNECTING=0;n.OPEN=
|
|
1;n.CLOSED=2;n.prototype.withCredentials=void 0;(function(a){"object"===typeof module&&"object"===typeof module.exports?(a=a(exports),void 0!==a&&(module.exports=a)):"function"===typeof define&&define.amd?define(["exports"],a):a(h)})(function(a){a.EventSourcePolyfill=n;a.NativeEventSource=J;void 0==z||void 0!=J&&"withCredentials"in J.prototype||(a.EventSource=n)})})("undefined"!==typeof window?window:this);
|