(function(){vare,n,t,s,i;(t=classe{constructor(){this.header={sid:0,cid:0,type:0,size:0},this.data=void0}as_raw(){varn,t,s;returns=5+this.header.size,(n=newUint8Array(s))[0]=this.header.type,t=e.bytes_of(this.header.cid),n.set(t,1),t=e.bytes_of(this.header.sid),n.set(t,3),this.data&&n.set(this.data,5),n.buffer}}).decode=function(e){returnnewPromise((function(n,s){vari;returni=newt,!e||e.length<5?s("Invalid message format"):(i.header.type=e[0],i.header.cid=t.int_from(e,1),i.header.sid=t.int_from(e,3),i.header.size=e.length-5,i.data=e.slice(5,5+i.header.size),n(i))}))},t.bytes_of=function(e,n){vart;return4!==n&&(n=2),(t=newUint8Array(n))[0]=255&e,e>>=8,t[1]=255&e,4!==n||(e>>=8,t[2]=255&e,e>>=8,t[3]=255&e),t},t.int_from=function(e,n,t){return4!==t?e[n]|e[n+1]<<8:e[n]|e[n+1]<<8|e[n+2]<<16|e[n+3]<<24},t.OK=0,t.ERROR=1,t.DATA=6,t.CLOSE=5,t.SUBSCRIBE=2,t.UNSUBSCRIBE=3,t.CTRL=7,t.PING=8,s=class{constructor(e){this.channel=e,this.id=void0,this.channel_id=void0,this.onmessage=void0,this.onerror=void0,this.onopen=void0,this.onclose=void0,this.tunnel=void0,this.is_opened=!1}send(e,n){if(this.tunnel){if(this.is_opened)returnthis.tunnel.send(this.genmsg(e,n));this.onerror&&this.onerror("Channel is not opened yet")}elsethis.onerror&&this.onerror("Tunnel is not opened")}genmsg(e,n){vars;return(s=newt).header.sid=this.id,s.header.cid=this.channel_id,s.header.type=e,s.header.size=n?n.length:0,s.data=n,s}close(e){if(this.is_opened=!1,this.tunnel)returnthis.tunnel.unsubscribe(this,e)}},e=class{constructor(e){this.uri=e,this.socket=void0,this.pending={},this.subscribers={},this.onclose=void0}ready(){returnnewPromise((e,n)=>this.uri?void0!==this.socket?e():(console.log("Connect to "+this.uri),this.socket=newWebSocket(this.uri),this.socket.binaryType="arraybuffer",this.socket.onmessage=e=>this.process(e),this.socket.onclose=e=>{varn,t,s,i;for(ninthis.socket=void0,t=this.pending)(i=t[n]).tunnel=void0,i.onclose&&i.onclose();for(nins=this.subscribers)(i=s[n]).tunnel=void0,i.is_opened=!1,i.onclose&&i.onclose();if(this.pending={},this.subscribe={},this.onclose())returnthis.onclose()},this.socket.onerror=e=>{varn,t,s,i,r;for(nint=this.pending)(r=t[n]).onerror&&r.onerror(e.toString());for(nini=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e.toString()));returni},this.socket.onopen=n=>e()):n())}process(e){returnt.decode(newUint8Array(e.data)).then(e=>{varn,s;switch(n=(e,n)=>{vart;if(!(t=this.pending[e.header.sid]))return(t=this.subscribers[e.header.sid])&&t[n]?t[n](e):void0;t[n]&&t[n](e)},e.header.type){caset.OK:if(!(s=this.pending[e.header.sid]))returnn(e,"onmessage");if(deletethis.pending[e.header.sid],s.id=t.int_from(e.data,0),s.channel_id=e.header.cid,this.subscribers[s.id]=s,s.is_opened=!0,s.onopen)returns.onopen();break;caset.DATA:returnn(e,"onmessage");caset.CTRL:returnn(e,"onctrl");caset.ERROR:returnn(e,"onerror");caset.UNSUBSCRIBE:if(!(s=this.subscribers[e.header.sid]))return;returns.close(!0);caset.PING:break;default:returnconsole.error(`Message of type ${e.header.type} is unsupported`,e)}}).catch(e=>{varn,t,s,i;for(nint=this.pending)(i=t[n]).onerror&&i.onerror(e);for(nins=this.subscribers)(i=s[n]).onerror&&i.onerror(e);returnconsole.log(e)})}subscribe(e){returnthis.ready().then(()=>{for(e.tunnel=this,e.id=Math.floor(1e3*Math.random())+1;this.subscribers[e.id]||this.pending[e.id];)e.id=Math.floor(1e3*Math.random())+1;returnthis.pending[e.id]=e,this.send(e.genmsg(t.SUBSCRIBE,(newTextEncoder).encode(e.channel)))}).catch((function(n){if(e.onerror)returne.onerror(n.toString())}))}unsubscribe(e,n){returnthis.ready().then(()=>{if(this.subscribers[e.id])returnn||this.send(e.genmsg(t.UNSUBSCRIBE,void0)),e.onclose&&e.onclose(),deletethis.subscribers[e.id],e.tunnel=void0,e.is_opened=!1}).catch((function(n){if(e.onerror)returne.onerror(n.toString())}))}send(e){returnthis.socket.send(e.as_raw())}close(){if(console.log("Close connection to "+this.uri),this.socket&&this.socket.close(),this.onclose())returnthis.onclose()}},(i=this).Antunnel||(i.Antunnel={tunnel