forked from axway-streams/axway-amplify-streams-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamdataio.min.js
26 lines (26 loc) · 13.3 KB
/
streamdataio.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/** streamdataio-js-sdk - Javascript SDK for Streamdata.io
* @version v1.0.5
* rev: 271b24c1f3127fa1e56154dd9d791692076325d6
*/
(function(b){function f(g,e,a){var b=window.location.origin;this.cancelable=this.cancelBubble=this.bubbles=!1;this.data=e||null;this.origin=b||"";this.lastEventId=a||"";this.type=g||"message"}if(!b.EventSource||b.ja){var d=(b.ja||"")+"EventSource",c=function(g,e){if(!g||"string"!=typeof g)throw new SyntaxError("Not enough arguments");this.URL=g;this.Ba(e);var a=this;setTimeout(function(){a.X()},0)};c.prototype={CONNECTING:0,OPEN:1,CLOSED:2,R:{loggingEnabled:!1,loggingPrefix:"eventsource",interval:500,
bufferSizeLimit:1048576,C:3E5,za:3E3,w:{},$:{Accept:"text/event-stream","Cache-Control":"no-cache","X-Requested-With":"XMLHttpRequest"}},Ba:function(g){var e=this.R,a;for(a in e)e.hasOwnProperty(a)&&(this[a]=e[a]);for(a in g)a in e&&g.hasOwnProperty(a)&&(this[a]=g[a]);if("undefined"===typeof console||"undefined"===typeof console.log)this.loggingEnabled=!1},log:function(g){this.loggingEnabled&&console.log("["+this.loggingPrefix+"]:"+g)},X:function(){try{this.readyState!=this.CLOSED&&(this.P(),this.readyState=
this.CONNECTING,this.cursor=0,this.cache="",this.h=new this.u(this),this.Y())}catch(g){this.log("An error occured during polling."),this.dispatchEvent("error",{type:"error",data:g.message})}},s:function(g){var a=this;a.readyState=a.CONNECTING;a.dispatchEvent("error",{type:"error",data:"Reconnecting "});this.v=setTimeout(function(){a.X()},g||0)},P:function(){this.log("evs cleaning up");this.v&&(clearInterval(this.v),this.v=null);this.l&&(clearInterval(this.l),this.l=null);this.h&&(this.h.abort(),this.h=
null)},Y:function(){if(this.C){this.l&&clearInterval(this.l);var g=this;this.l=setTimeout(function(){g.log("Timeout! silentTImeout:"+g.C);g.s()},this.C)}},close:function(){this.readyState=this.CLOSED;this.log("Closing connection.");this.P()},A:function(){var g=this.h;if(g.W()&&!g.U()){this.Y();this.readyState==this.CONNECTING&&(this.readyState=this.OPEN,this.dispatchEvent("open",{type:"open"}));var a=g.T();a.length>this.bufferSizeLimit&&(this.log("buffer length > bufferSizeLimit"),this.s());0==this.cursor&&
0<a.length&&"\ufeff"==a.substring(0,1)&&(this.cursor=1);var h=this.ra(a);h[0]>=this.cursor&&(h=h[1],this.ya(a.substring(this.cursor,h)),this.cursor=h);g.V()&&(this.log("request.isDone(). reopening the connection"),this.s(this.interval))}else this.readyState!==this.CLOSED&&(this.log("this.readyState !== this.CLOSED"),this.s(this.interval))},ya:function(g){g=this.cache+this.sa(g);g=g.split("\n\n");var a,h,b,d,c;for(a=0;a<g.length-1;a++){b="message";d=[];parts=g[a].split("\n");for(h=0;h<parts.length;h++)c=
this.Ca(parts[h]),0==c.indexOf("event")?b=c.replace(/event:?\s*/,""):0==c.indexOf("retry")?(c=parseInt(c.replace(/retry:?\s*/,"")),isNaN(c)||(this.interval=c)):0==c.indexOf("data")?d.push(c.replace(/data:?\s*/,"")):0==c.indexOf("id:")?this.lastEventId=c.replace(/id:?\s*/,""):0==c.indexOf("id")&&(this.lastEventId=null);d.length&&this.dispatchEvent(b,new f(b,d.join("\n"),this.lastEventId))}this.cache=g[g.length-1]},dispatchEvent:function(a,e){var h=this["_"+a+"Handlers"];if(h)for(var b=0;b<h.length;b++)h[b].call(this,
e);this["on"+a]&&this["on"+a].call(this,e)},addEventListener:function(a,e){this["_"+a+"Handlers"]||(this["_"+a+"Handlers"]=[]);this["_"+a+"Handlers"].push(e)},v:null,h:null,lastEventId:null,cache:"",cursor:0,readyState:0,Z:function(a,e){var b=[];if(e){var c,f,d=encodeURIComponent;for(c in e)e.hasOwnProperty(c)&&(f=d(c)+"="+d(e[c]),b.push(f))}return 0<b.length?-1==a.indexOf("?")?a+"?"+b.join("&"):a+"&"+b.join("&"):a},ra:function(a){var e=a.lastIndexOf("\n\n"),b=a.lastIndexOf("\r\r");a=a.lastIndexOf("\r\n\r\n");
return a>Math.max(e,b)?[a,a+4]:[Math.max(e,b),Math.max(e,b)+2]},Ca:function(a){return a.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"")},sa:function(a){return a.replace(/\r\n|\r/g,"\n")}};if(window.XDomainRequest&&window.XMLHttpRequest&&void 0===(new XMLHttpRequest).responseType){c.isPolyfill="IE_8-9";var a=c.prototype.R;a.$=null;a.w.evs_preamble=2056;c.prototype.u=function(a){this.c=request=new XDomainRequest;request.onprogress=function(){request.L=!0;a.A()};request.onload=function(){this.H=!0;a.A()};
request.onerror=function(){this.i=!0;a.readyState=a.CLOSED;a.dispatchEvent("error",{type:"error",data:"XDomainRequest error"})};request.ontimeout=function(){this.i=!0;a.readyState=a.CLOSED;a.dispatchEvent("error",{type:"error",data:"XDomainRequest timed out"})};var e={};if(a.w){var b=a.w,c;for(c in b)b.hasOwnProperty(c)&&(e[c]=b[c]);a.lastEventId&&(e.evs_last_event_id=a.lastEventId)}request.open("GET",a.Z(a.URL,e));request.send()};c.prototype.u.prototype={c:null,L:!1,H:!1,i:!1,W:function(){return this.c.L},
V:function(){return this.c.H},U:function(){return this.c.i},T:function(){var a="";try{a=this.c.responseText||""}catch(b){}return a},abort:function(){this.c&&this.c.abort()}}}else c.isPolyfill="XHR",c.prototype.u=function(a){this.c=request=new XMLHttpRequest;a.h=this;request.onreadystatechange=function(){1<request.readyState&&a.readyState!=a.CLOSED&&(200==request.status||300<=request.status&&400>request.status?a.A():a.s(a.za))};request.onprogress=function(){};request.open("GET",a.Z(a.URL,a.w),!0);
var b=a.$,c;for(c in b)b.hasOwnProperty(c)&&request.setRequestHeader(c,b[c]);a.lastEventId&&request.setRequestHeader("Last-Event-Id",a.lastEventId);request.send()},c.prototype.u.prototype={c:null,i:!1,W:function(){return 2<=this.c.readyState},V:function(){return 4==this.c.readyState},U:function(){return this.i||400<=this.c.status},T:function(){var a="";try{a=this.c.responseText||""}catch(b){}return a},abort:function(){this.c&&this.c.abort()}};b[d]=c}})(this);var Exporter={m:function(b,f,d){f=f.split(".");for(var c=0;c<f.length-1;c++)b=b[f[c]];this.a(b,f[f.length-1],d)},a:function(b,f,d){b[f]=d}};var Logger=function(){function b(b,a){return b.replace(/{(\d+)}/g,function(b,e){var c;if(a[e]&&"object"===typeof a[e]&&a[e]instanceof Error)try{c=a[e].message,a[e].stack&&f.error(a[e].stack)}catch(d){c=a[e]}else if(a[e]&&"object"===typeof a[e])try{a[e].toString!==Object.prototype.toString?c=a[e].toString():c=JSON.stringify(a[e])}catch(d){c=a[e]}else a[e]&&"function"===typeof a[e]?c="function":c=a[e]?a[e]:b;c=""+c;return c.substring(0,Math.min(500,c.length))})}var f=window.console,d=2;return{aa:3,
ba:2,ca:1,ERROR:0,Aa:function(b){d=b},debug:function(){3<=d&&f&&f.log&&f.log(b(arguments[0],Array.prototype.slice.call(arguments,1)))},info:function(){2<=d&&f&&f.info&&f.info(b(arguments[0],Array.prototype.slice.call(arguments,1)))},warn:function(){1<=d&&f&&f.warn&&f.warn(b(arguments[0],Array.prototype.slice.call(arguments,1)))},error:function(){0<=d&&f&&f.error&&f.error(b(arguments[0],Array.prototype.slice.call(arguments,1)))}}}();var Preconditions=function(b){return{b:function(b,d){if("undefined"===typeof b||null===b){if(d)throw Error(d);throw Error("value cannot be null");}return b},m:function(f,d){this.b(f,"functionName cannot be null");this.b(d,"message cannot be null");b.warn("Deprecated: function '{0}' is deprecated because '{1}'.",f,d)},O:function(b,d){if(!b){if(d)throw Error(d);throw Error("expression is not valid");}},na:function(b,d){if(!b){if(d)throw Error(d);throw Error("expression is not valid");}}}}(Logger);function Listeners(b){Preconditions.b(b,"bind cannot be null");this.da=b;this.j=[]}
Listeners.prototype={o:function(){for(var b=this.j.slice(),f=0,d=b.length;f<d;f++)try{var c=b[f];c&&c.apply(this.da,arguments)}catch(a){Logger.error("Unable to forward event: {0}",a)}},add:function(b){Preconditions.b(b,"listener cannot be null");Preconditions.O(-1==this.j.indexOf(b),"listener already exists");this.j.push(b)},remove:function(b){Preconditions.b(b,"listener cannot be null");b=this.j.indexOf(b);Preconditions.O(0<=b,"listener not exists");this.j.splice(b,1)}};function StreamdataEventSource(b,f,d,c){Preconditions.b(b,"url cannot be null.");Preconditions.b(f,"appToken cannot be null.");Preconditions.na(null===c||void 0===c||c.hasOwnProperty("signUrl"),"authStrategy does not implement signUrl() function.");d=d||[];c=void 0===c?null:c;var a=this;a.streamdataConfig={PROTOCOL:"https://",HOST:"streamdata.motwin.net",PORT:""};a.f=null;a.g=!1;a.B=b;a.J=new Listeners(a);a.D=new Listeners(a);a.K=new Listeners(a);a.G=new Listeners(a);a.I=new Listeners(a);a.ka=d;a.F=
{type:"UnknownError",status:1E3,message:"An error occured. Please check your console logs for more details.",source:"server"};a.ea=1048576;a.ma=!1;a.polyfillOptions={bufferSizeLimit:a.ea,loggingEnabled:a.ma};a.open=function(){Preconditions.b(a.B,"url cannot be null");a.close();var b=a.ia(a.B,a.ka);b&&(a.f=a.la()?new EventSource(b,a.polyfillOptions):new EventSource(b),a.f.addEventListener("open",function(b){Logger.debug("SSE Stream Opened to "+a.B+"event: "+JSON.stringify(b));a.g=!0;a.J.o()}),a.f.addEventListener("error",
function(b){0===a.f.readyState&&a.g?Logger.info("SSE server connection lost, retrying ..."):(Logger.debug("Error with SSE at "+b+": closing the stream."),a.f.close(),a.g=!1,a.G.o(a.fa(b)))}),a.f.addEventListener("data",function(b){Logger.debug("Received data:"+b.data);a.D.o(JSON.parse(b.data))}),a.f.addEventListener("patch",function(b){Logger.debug("Received patch:"+b.data);a.K.o(JSON.parse(b.data))}),a.f.addEventListener("monitor",function(b){Logger.debug("Received monitor:"+b.data);a.I.o(JSON.parse(b.data))}));
return this};a.close=function(){a.g&&null!==a.f&&(Logger.info("Closing the SSE Stream."),a.f.close(),a.g=!1);return this};a.ia=function(b,e){Preconditions.b(b,"url cannot be null");e=e||[];var d=document.createElement("a");d.href=b;var d=d.protocol+"//"+b.substring(d.protocol.length+2,b.indexOf(d.hostname))+d.hostname+("0"!=d.port&&""!=d.port&&"80"!=d.port&&"443"!=d.port?":"+d.port:"")+(0==d.pathname.indexOf("/")?"":"/")+d.pathname+d.search,d=null===c?d:c.signUrl(d),f=a.ha(e),k="";0<f.length&&(k=
document.createElement("a"),k.href=d,k=(-1===k.search.indexOf("?")?"?":"&")+f.join("&"));d=a.streamdataConfig.PROTOCOL+a.streamdataConfig.HOST+(a.qa(a.streamdataConfig.PORT)?"":":")+a.streamdataConfig.PORT+"/"+d+k;Logger.debug("converted url :"+d);return d};a.ha=function(b){b=b||[];b=a.ga(b);b.push("X-Sd-Token="+encodeURIComponent(f));return b};a.ga=function(a){a=a||[];return a.map(function(a){return"X-Sd-Header="+encodeURIComponent(a)})};a.fa=function(b){Preconditions.b(b,"event cannot be null");
var c=a.F;try{var d=JSON.parse(b.data);c.type=d.cause;c.message=d.message;c.status=d.status}catch(f){c=a.F}c.source="server";console.log(JSON.stringify(c));return new StreamdataError(c.type,c.message,c.status,c.source,!0)};a.qa=function(a){return!a||0===a.length};a.la=function(){switch(EventSource.isPolyfill){case void 0:return!1;case "XHR":return!0;case "IE_8-9":return!0;default:return!1}}}
StreamdataEventSource.prototype={wa:function(b){Preconditions.b(b,"listener cannot be null");this.J.add(b);return this},ua:function(b){Preconditions.b(b,"listener cannot be null");this.G.add(b);return this},ta:function(b){Preconditions.b(b,"listener cannot be null");this.D.add(b);return this},xa:function(b){Preconditions.b(b,"listener cannot be null");this.K.add(b);return this},va:function(b){Preconditions.b(b,"listener cannot be null");this.I.add(b);return this},pa:function(){return this.g}};function StreamdataError(b,f,d,c,a){this.m=c;this.S=b;this.M=f;this.oa=a||!1;this.N=d;Exporter.a(this,"source",this.m);Exporter.a(this,"type",this.S);Exporter.a(this,"message",this.M);Exporter.a(this,"status",this.N)}StreamdataError.prototype={isFatal:function(){return this.oa},isServer:function(){return"server"==this.m},isClient:function(){return"client"==this.m},getMessage:function(){return this.M},getStatus:function(){return this.N},getType:function(){return this.S}};var streamdataio=new function(){};Exporter.a(streamdataio,"Logger",Logger);Exporter.a(Logger,"DEBUG",Logger.aa);Exporter.a(Logger,"INFO",Logger.ba);Exporter.a(Logger,"WARN",Logger.ca);Exporter.a(Logger,"ERROR",Logger.ERROR);Exporter.a(Logger,"setLevel",Logger.Aa);Exporter.a(Logger,"debug",Logger.debug);Exporter.a(Logger,"info",Logger.info);Exporter.a(Logger,"warn",Logger.warn);Exporter.a(Logger,"error",Logger.error);
Exporter.a(streamdataio,"createEventSource",function(b,f,d,c){Preconditions.b(b,"url cannot be null");d=d||[];c=c||null;0!=b.lastIndexOf("http://",0)&&0!=b.lastIndexOf("https://",0)&&(b="http://"+b,Logger.warn("url has no default protocol defined. Add http:// as a default protocol."));return new StreamdataEventSource(b,f,d,c)});Exporter.a(StreamdataError.prototype,"isFatal",StreamdataError.prototype.isFatal);Exporter.a(StreamdataError.prototype,"isServer",StreamdataError.prototype.isServer);
Exporter.a(StreamdataError.prototype,"isClient",StreamdataError.prototype.isClient);Exporter.a(StreamdataError.prototype,"getMessage",StreamdataError.prototype.getMessage);Exporter.a(StreamdataError.prototype,"getStatus",StreamdataError.prototype.getStatus);Exporter.a(StreamdataError.prototype,"getType",StreamdataError.prototype.getType);Exporter.a(StreamdataEventSource.prototype,"open",StreamdataEventSource.prototype.connect);Exporter.a(StreamdataEventSource.prototype,"onOpen",StreamdataEventSource.prototype.wa);
Exporter.a(StreamdataEventSource.prototype,"onError",StreamdataEventSource.prototype.ua);Exporter.a(StreamdataEventSource.prototype,"onData",StreamdataEventSource.prototype.ta);Exporter.a(StreamdataEventSource.prototype,"onPatch",StreamdataEventSource.prototype.xa);Exporter.a(StreamdataEventSource.prototype,"onMonitor",StreamdataEventSource.prototype.va);Exporter.a(StreamdataEventSource.prototype,"isConnected",StreamdataEventSource.prototype.pa);