var d=Object.defineProperty;var u=(h,e,t)=>e in h?d(h,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[e]=t;var n=(h,e,t)=>u(h,typeof e!="symbol"?e+"":e,t);const l="00000000-0000-0000-0000-000000000001";class g{constructor(){n(this,"ws",null);n(this,"inputSpeed",0);n(this,"outputSpeed",0);n(this,"summSpeed",0);n(this,"iteration",0);n(this,"mode","input");n(this,"startTime",0);n(this,"serverAddress","");n(this,"accessToken","");n(this,"useSecure",!1);n(this,"onProgress",null);n(this,"onDone",null);n(this,"connectWatchdog",null);n(this,"failed",!1)}setParams(e,t,s=!0){this.serverAddress=e,this.accessToken=t,this.useSecure=s}setCallbacks(e,t){this.onProgress=e,this.onDone=t}async start(){return new Promise((e,t)=>{try{this.inputSpeed=0,this.outputSpeed=0,this.summSpeed=0,this.iteration=0,this.mode="input",this.failed=!1;const s=new WebSocket(this.serverAddress);s.binaryType="arraybuffer",this.ws=s,this.startConnectWatchdog(t),s.onopen=()=>{var o;if(this.ws!==s){try{s.close()}catch{}return}this.stopConnectWatchdog(),console.log("[SpeedTester] Connected"),(o=this.onProgress)==null||o.call(this,{type:"connected"}),this.sendLogon(s)},s.onmessage=o=>{this.ws===s&&(o.data instanceof ArrayBuffer?this.onBinary(o.data):this.onMessage(o.data))},s.onclose=()=>{var r;if(this.ws===s&&(this.ws=null),this.stopConnectWatchdog(),this.failed)return;console.log(`[SpeedTester] Done: in=${this.inputSpeed} kbps, out=${this.outputSpeed} kbps`);const o={inputSpeed:this.inputSpeed,outputSpeed:this.outputSpeed};(r=this.onDone)==null||r.call(this,o),e(o)},s.onerror=o=>{var r;this.stopConnectWatchdog(),this.failed=!0,console.error("[SpeedTester] Error:",o),(r=this.onProgress)==null||r.call(this,{type:"error",message:"Connection error"}),t(o)}}catch(s){t(s)}})}stop(){this.stopConnectWatchdog(),this.ws&&(this.ws.close(),this.ws=null)}startConnectWatchdog(e){this.stopConnectWatchdog(),this.connectWatchdog=setTimeout(()=>{var t;if(!(!this.ws||this.ws.readyState!==WebSocket.CONNECTING)){this.failed=!0,console.warn("[SpeedTester] CONNECTING timeout");try{this.ws.close()}catch{}(t=this.onProgress)==null||t.call(this,{type:"error",message:"Connection timeout"}),e(new Error("SpeedTester connect timeout"))}},7e3)}stopConnectWatchdog(){this.connectWatchdog&&(clearTimeout(this.connectWatchdog),this.connectWatchdog=null)}sendLogon(e){if(this.ws!==e||e.readyState!==WebSocket.OPEN)return;const t={connect_request:{channel_type:2,access_token:this.accessToken}};e.send(JSON.stringify(t))}requestInputBlob(){if(!this.ws)return;this.startTime=performance.now();const e={load_blobs:[l]};this.ws.send(JSON.stringify(e))}sendOutputBlob(){if(!this.ws)return;const e=20,t=new ArrayBuffer(e+102400),s=new DataView(t),o=new Uint8Array(t);s.setUint8(0,2),s.setUint8(1,0),s.setUint16(2,1,!1);const r=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];for(let i=0;i<16;i++)s.setUint8(4+i,r[i]);for(let i=e;i<t.byteLength;i++)o[i]=Math.floor(Math.random()*127)+32;this.startTime=performance.now(),this.ws.send(t)}onMessage(e){var t,s;try{const o=JSON.parse(e);if(o.connect_response){console.log("[SpeedTester] Logged in"),this.requestInputBlob();return}if(o.delivery_blobs){if(this.mode==="output"){const r=(performance.now()-this.startTime)/1e3,c=102400*8/r/1e3;this.summSpeed+=c,this.iteration++,this.outputSpeed=Math.round(this.summSpeed/this.iteration);const a=this.iteration*(100/5);(t=this.onProgress)==null||t.call(this,{type:"out_speed_testing",speed:this.outputSpeed,progress:a}),this.iteration<5?this.sendOutputBlob():(console.log(`[SpeedTester] Output speed: ${this.outputSpeed} kbps`),(s=this.ws)==null||s.send(JSON.stringify({disconnect:{}})),setTimeout(()=>{var p;return(p=this.ws)==null?void 0:p.close()},100))}return}}catch{}}onBinary(e){var i,c;if(this.mode!=="input")return;const t=(performance.now()-this.startTime)/1e3,o=e.byteLength*8/t/1e3;this.summSpeed+=o,this.iteration++,this.inputSpeed=Math.round(this.summSpeed/this.iteration);const r=this.iteration*(100/5);(i=this.onProgress)==null||i.call(this,{type:"in_speed_testing",speed:this.inputSpeed,progress:r}),this.iteration<5?this.requestInputBlob():(console.log(`[SpeedTester] Input speed: ${this.inputSpeed} kbps`),this.mode="output",this.summSpeed=0,this.iteration=0,(c=this.onProgress)==null||c.call(this,{type:"out_speed_testing",speed:0,progress:0}),this.sendOutputBlob())}}export{g as SpeedTester};
