@nainudhin/video-meeting-sdk 1.10.0 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/meet-sdk.mjs CHANGED
@@ -6051,7 +6051,7 @@ class Yr {
6051
6051
  mount(e) {
6052
6052
  if (typeof e == "string" ? this.container = document.querySelector(e) : e instanceof Element && (this.container = e), !this.container)
6053
6053
  throw new Error(`Container not found: ${e}`);
6054
- this.render();
6054
+ this._ownershipToken = Symbol("meet-sdk-ownership"), this.container.__meetSdkOwner = this._ownershipToken, this.render();
6055
6055
  }
6056
6056
  render() {
6057
6057
  const e = this.recording === "manual" ? '<button id="toggle-record" style="padding: 10px 20px; background-color: #7a3a3a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">● Record</button>' : "", t = this.recording !== "off" ? '<button id="toggle-pause" style="display:none; padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">⏸ Pause</button>' : "", n = this.screenShare ? '<button id="toggle-screenshare" style="padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">Share Screen</button>' : "", s = this.recording !== "off" ? '<span id="recording-indicator" style="display:none; margin-left: 12px; color: #ff5252; font-size: 13px; font-weight: 600;"><span id="rec-dot" style="display:inline-block; width:8px; height:8px; background:#ff5252; border-radius:50%; margin-right:6px; animation: recPulse 1.2s ease-in-out infinite;"></span>REC <span id="rec-elapsed">00:00</span></span>' : "", i = this.recording !== "off" ? '<span id="queue-indicator" style="display:none; margin-left: 12px; color: #fbbf24; font-size: 12px;">⏳ <span id="queue-count">0</span> uploading</span>' : "";
@@ -6362,7 +6362,7 @@ class Yr {
6362
6362
  t && (t.textContent = e ? "▶ Resume" : "⏸ Pause", t.style.backgroundColor = e ? "#7a5a3a" : "#3a5a7a");
6363
6363
  }
6364
6364
  destroy() {
6365
- this.container && (this.container.innerHTML = ""), this.videoElements = {}, this.chatMessages = [];
6365
+ this.container && this.container.__meetSdkOwner === this._ownershipToken && (this.container.innerHTML = "", this.container.__meetSdkOwner = null), this.videoElements = {}, this.chatMessages = [];
6366
6366
  }
6367
6367
  }
6368
6368
  class Gr {
@@ -38,7 +38,7 @@ a=extmap-allow-mixed`)!==-1){const i=s.sdp.split(`
38
38
  isIOS:${this.isIOS}
39
39
  isWebRTCSupported:${this.isWebRTCSupported()}
40
40
  isBrowserSupported:${this.isBrowserSupported()}
41
- isUnifiedPlanSupported:${this.isUnifiedPlanSupported()}`}constructor(){this.isIOS=typeof navigator<"u"?["iPad","iPhone","iPod"].includes(navigator.platform):!1,this.supportedBrowsers=["firefox","chrome","safari"],this.minFirefoxVersion=59,this.minChromeVersion=72,this.minSafariVersion=605}},An=r=>!r||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.test(r),Nt=()=>Math.random().toString(36).slice(2),zt={iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:["turn:eu-0.turn.peerjs.com:3478","turn:us-0.turn.peerjs.com:3478"],username:"peerjs",credential:"peerjsp"}],sdpSemantics:"unified-plan"};class In extends Ut{noop(){}blobToArrayBuffer(e,t){const n=new FileReader;return n.onload=function(s){s.target&&t(s.target.result)},n.readAsArrayBuffer(e),n}binaryStringToArrayBuffer(e){const t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t.buffer}isSecure(){return location.protocol==="https:"}constructor(...e){super(...e),this.CLOUD_HOST="0.peerjs.com",this.CLOUD_PORT=443,this.chunkedBrowsers={Chrome:1,chrome:1},this.defaultConfig=zt,this.browser=F.getBrowser(),this.browserVersion=F.getVersion(),this.pack=Qe,this.unpack=Ge,this.supports=function(){const t={browser:F.isBrowserSupported(),webRTC:F.isWebRTCSupported(),audioVideo:!1,data:!1,binaryBlob:!1,reliable:!1};if(!t.webRTC)return t;let n;try{n=new RTCPeerConnection(zt),t.audioVideo=!0;let s;try{s=n.createDataChannel("_PEERJSTEST",{ordered:!0}),t.data=!0,t.reliable=!!s.ordered;try{s.binaryType="blob",t.binaryBlob=!F.isIOS}catch{}}catch{}finally{s&&s.close()}}catch{}finally{n&&n.close()}return t}(),this.validateId=An,this.randomToken=Nt}}const k=new In,On="PeerJS: ";class Dn{get logLevel(){return this._logLevel}set logLevel(e){this._logLevel=e}log(...e){this._logLevel>=3&&this._print(3,...e)}warn(...e){this._logLevel>=2&&this._print(2,...e)}error(...e){this._logLevel>=1&&this._print(1,...e)}setLogFunction(e){this._print=e}_print(e,...t){const n=[On,...t];for(const s in n)n[s]instanceof Error&&(n[s]="("+n[s].name+") "+n[s].message);e>=3?console.log(...n):e>=2?console.warn("WARNING",...n):e>=1&&console.error("ERROR",...n)}constructor(){this._logLevel=0}}var f=new Dn,ke={},Ln=Object.prototype.hasOwnProperty,C="~";function j(){}Object.create&&(j.prototype=Object.create(null),new j().__proto__||(C=!1));function $n(r,e,t){this.fn=r,this.context=e,this.once=t||!1}function Ft(r,e,t,n,s){if(typeof t!="function")throw new TypeError("The listener must be a function");var i=new $n(t,n||r,s),o=C?C+e:e;return r._events[o]?r._events[o].fn?r._events[o]=[r._events[o],i]:r._events[o].push(i):(r._events[o]=i,r._eventsCount++),r}function ee(r,e){--r._eventsCount===0?r._events=new j:delete r._events[e]}function v(){this._events=new j,this._eventsCount=0}v.prototype.eventNames=function(){var e=[],t,n;if(this._eventsCount===0)return e;for(n in t=this._events)Ln.call(t,n)&&e.push(C?n.slice(1):n);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(t)):e},v.prototype.listeners=function(e){var t=C?C+e:e,n=this._events[t];if(!n)return[];if(n.fn)return[n.fn];for(var s=0,i=n.length,o=new Array(i);s<i;s++)o[s]=n[s].fn;return o},v.prototype.listenerCount=function(e){var t=C?C+e:e,n=this._events[t];return n?n.fn?1:n.length:0},v.prototype.emit=function(e,t,n,s,i,o){var a=C?C+e:e;if(!this._events[a])return!1;var c=this._events[a],h=arguments.length,l,d;if(c.fn){switch(c.once&&this.removeListener(e,c.fn,void 0,!0),h){case 1:return c.fn.call(c.context),!0;case 2:return c.fn.call(c.context,t),!0;case 3:return c.fn.call(c.context,t,n),!0;case 4:return c.fn.call(c.context,t,n,s),!0;case 5:return c.fn.call(c.context,t,n,s,i),!0;case 6:return c.fn.call(c.context,t,n,s,i,o),!0}for(d=1,l=new Array(h-1);d<h;d++)l[d-1]=arguments[d];c.fn.apply(c.context,l)}else{var u=c.length,p;for(d=0;d<u;d++)switch(c[d].once&&this.removeListener(e,c[d].fn,void 0,!0),h){case 1:c[d].fn.call(c[d].context);break;case 2:c[d].fn.call(c[d].context,t);break;case 3:c[d].fn.call(c[d].context,t,n);break;case 4:c[d].fn.call(c[d].context,t,n,s);break;default:if(!l)for(p=1,l=new Array(h-1);p<h;p++)l[p-1]=arguments[p];c[d].fn.apply(c[d].context,l)}}return!0},v.prototype.on=function(e,t,n){return Ft(this,e,t,n,!1)},v.prototype.once=function(e,t,n){return Ft(this,e,t,n,!0)},v.prototype.removeListener=function(e,t,n,s){var i=C?C+e:e;if(!this._events[i])return this;if(!t)return ee(this,i),this;var o=this._events[i];if(o.fn)o.fn===t&&(!s||o.once)&&(!n||o.context===n)&&ee(this,i);else{for(var a=0,c=[],h=o.length;a<h;a++)(o[a].fn!==t||s&&!o[a].once||n&&o[a].context!==n)&&c.push(o[a]);c.length?this._events[i]=c.length===1?c[0]:c:ee(this,i)}return this},v.prototype.removeAllListeners=function(e){var t;return e?(t=C?C+e:e,this._events[t]&&ee(this,t)):(this._events=new j,this._eventsCount=0),this},v.prototype.off=v.prototype.removeListener,v.prototype.addListener=v.prototype.on,v.prefixed=C,v.EventEmitter=v,ke=v;var L={};D(L,"ConnectionType",()=>M),D(L,"PeerErrorType",()=>y),D(L,"BaseConnectionErrorType",()=>xe),D(L,"DataConnectionErrorType",()=>Te),D(L,"SerializationType",()=>te),D(L,"SocketEventType",()=>A),D(L,"ServerMessageType",()=>S);var M=function(r){return r.Data="data",r.Media="media",r}({}),y=function(r){return r.BrowserIncompatible="browser-incompatible",r.Disconnected="disconnected",r.InvalidID="invalid-id",r.InvalidKey="invalid-key",r.Network="network",r.PeerUnavailable="peer-unavailable",r.SslUnavailable="ssl-unavailable",r.ServerError="server-error",r.SocketError="socket-error",r.SocketClosed="socket-closed",r.UnavailableID="unavailable-id",r.WebRTC="webrtc",r}({}),xe=function(r){return r.NegotiationFailed="negotiation-failed",r.ConnectionClosed="connection-closed",r}({}),Te=function(r){return r.NotOpenYet="not-open-yet",r.MessageToBig="message-too-big",r}({}),te=function(r){return r.Binary="binary",r.BinaryUTF8="binary-utf8",r.JSON="json",r.None="raw",r}({}),A=function(r){return r.Message="message",r.Disconnected="disconnected",r.Error="error",r.Close="close",r}({}),S=function(r){return r.Heartbeat="HEARTBEAT",r.Candidate="CANDIDATE",r.Offer="OFFER",r.Answer="ANSWER",r.Open="OPEN",r.Error="ERROR",r.IdTaken="ID-TAKEN",r.InvalidKey="INVALID-KEY",r.Leave="LEAVE",r.Expire="EXPIRE",r}({});const jt="1.5.5";class Bn extends ke.EventEmitter{constructor(e,t,n,s,i,o=5e3){super(),this.pingInterval=o,this._disconnected=!0,this._messagesQueue=[];const a=e?"wss://":"ws://";this._baseUrl=a+t+":"+n+s+"peerjs?key="+i}start(e,t){this._id=e;const n=`${this._baseUrl}&id=${e}&token=${t}`;this._socket||!this._disconnected||(this._socket=new WebSocket(n+"&version="+jt),this._disconnected=!1,this._socket.onmessage=s=>{let i;try{i=JSON.parse(s.data),f.log("Server message received:",i)}catch{f.log("Invalid server message",s.data);return}this.emit(A.Message,i)},this._socket.onclose=s=>{this._disconnected||(f.log("Socket closed.",s),this._cleanup(),this._disconnected=!0,this.emit(A.Disconnected))},this._socket.onopen=()=>{this._disconnected||(this._sendQueuedMessages(),f.log("Socket open"),this._scheduleHeartbeat())})}_scheduleHeartbeat(){this._wsPingTimer=setTimeout(()=>{this._sendHeartbeat()},this.pingInterval)}_sendHeartbeat(){if(!this._wsOpen()){f.log("Cannot send heartbeat, because socket closed");return}const e=JSON.stringify({type:S.Heartbeat});this._socket.send(e),this._scheduleHeartbeat()}_wsOpen(){return!!this._socket&&this._socket.readyState===1}_sendQueuedMessages(){const e=[...this._messagesQueue];this._messagesQueue=[];for(const t of e)this.send(t)}send(e){if(this._disconnected)return;if(!this._id){this._messagesQueue.push(e);return}if(!e.type){this.emit(A.Error,"Invalid message");return}if(!this._wsOpen())return;const t=JSON.stringify(e);this._socket.send(t)}close(){this._disconnected||(this._cleanup(),this._disconnected=!0)}_cleanup(){this._socket&&(this._socket.onopen=this._socket.onmessage=this._socket.onclose=null,this._socket.close(),this._socket=void 0),clearTimeout(this._wsPingTimer)}}class qt{constructor(e){this.connection=e}startConnection(e){const t=this._startPeerConnection();if(this.connection.peerConnection=t,this.connection.type===M.Media&&e._stream&&this._addTracksToConnection(e._stream,t),e.originator){const n=this.connection,s={ordered:!!e.reliable},i=t.createDataChannel(n.label,s);n._initializeDataChannel(i),this._makeOffer()}else this.handleSDP("OFFER",e.sdp)}_startPeerConnection(){f.log("Creating RTCPeerConnection.");const e=new RTCPeerConnection(this.connection.provider.options.config);return this._setupListeners(e),e}_setupListeners(e){const t=this.connection.peer,n=this.connection.connectionId,s=this.connection.type,i=this.connection.provider;f.log("Listening for ICE candidates."),e.onicecandidate=o=>{!o.candidate||!o.candidate.candidate||(f.log(`Received ICE candidates for ${t}:`,o.candidate),i.socket.send({type:S.Candidate,payload:{candidate:o.candidate,type:s,connectionId:n},dst:t}))},e.oniceconnectionstatechange=()=>{switch(e.iceConnectionState){case"failed":f.log("iceConnectionState is failed, closing connections to "+t),this.connection.emitError(xe.NegotiationFailed,"Negotiation of connection to "+t+" failed."),this.connection.close();break;case"closed":f.log("iceConnectionState is closed, closing connections to "+t),this.connection.emitError(xe.ConnectionClosed,"Connection to "+t+" closed."),this.connection.close();break;case"disconnected":f.log("iceConnectionState changed to disconnected on the connection with "+t);break;case"completed":e.onicecandidate=()=>{};break}this.connection.emit("iceStateChanged",e.iceConnectionState)},f.log("Listening for data channel"),e.ondatachannel=o=>{f.log("Received data channel");const a=o.channel;i.getConnection(t,n)._initializeDataChannel(a)},f.log("Listening for remote stream"),e.ontrack=o=>{f.log("Received remote stream");const a=o.streams[0],c=i.getConnection(t,n);if(c.type===M.Media){const h=c;this._addStreamToMediaConnection(a,h)}}}cleanup(){f.log("Cleaning up PeerConnection to "+this.connection.peer);const e=this.connection.peerConnection;if(!e)return;this.connection.peerConnection=null,e.onicecandidate=e.oniceconnectionstatechange=e.ondatachannel=e.ontrack=()=>{};const t=e.signalingState!=="closed";let n=!1;const s=this.connection.dataChannel;s&&(n=!!s.readyState&&s.readyState!=="closed"),(t||n)&&e.close()}async _makeOffer(){const e=this.connection.peerConnection,t=this.connection.provider;try{const n=await e.createOffer(this.connection.options.constraints);f.log("Created offer."),this.connection.options.sdpTransform&&typeof this.connection.options.sdpTransform=="function"&&(n.sdp=this.connection.options.sdpTransform(n.sdp)||n.sdp);try{await e.setLocalDescription(n),f.log("Set localDescription:",n,`for:${this.connection.peer}`);let s={sdp:n,type:this.connection.type,connectionId:this.connection.connectionId,metadata:this.connection.metadata};if(this.connection.type===M.Data){const i=this.connection;s={...s,label:i.label,reliable:i.reliable,serialization:i.serialization}}t.socket.send({type:S.Offer,payload:s,dst:this.connection.peer})}catch(s){s!="OperationError: Failed to set local offer sdp: Called in wrong state: kHaveRemoteOffer"&&(t.emitError(y.WebRTC,s),f.log("Failed to setLocalDescription, ",s))}}catch(n){t.emitError(y.WebRTC,n),f.log("Failed to createOffer, ",n)}}async _makeAnswer(){const e=this.connection.peerConnection,t=this.connection.provider;try{const n=await e.createAnswer();f.log("Created answer."),this.connection.options.sdpTransform&&typeof this.connection.options.sdpTransform=="function"&&(n.sdp=this.connection.options.sdpTransform(n.sdp)||n.sdp);try{await e.setLocalDescription(n),f.log("Set localDescription:",n,`for:${this.connection.peer}`),t.socket.send({type:S.Answer,payload:{sdp:n,type:this.connection.type,connectionId:this.connection.connectionId},dst:this.connection.peer})}catch(s){t.emitError(y.WebRTC,s),f.log("Failed to setLocalDescription, ",s)}}catch(n){t.emitError(y.WebRTC,n),f.log("Failed to create answer, ",n)}}async handleSDP(e,t){t=new RTCSessionDescription(t);const n=this.connection.peerConnection,s=this.connection.provider;f.log("Setting remote description",t);const i=this;try{await n.setRemoteDescription(t),f.log(`Set remoteDescription:${e} for:${this.connection.peer}`),e==="OFFER"&&await i._makeAnswer()}catch(o){s.emitError(y.WebRTC,o),f.log("Failed to setRemoteDescription, ",o)}}async handleCandidate(e){f.log("handleCandidate:",e);try{await this.connection.peerConnection.addIceCandidate(e),f.log(`Added ICE candidate for:${this.connection.peer}`)}catch(t){this.connection.provider.emitError(y.WebRTC,t),f.log("Failed to handleCandidate, ",t)}}_addTracksToConnection(e,t){if(f.log(`add tracks from stream ${e.id} to peer connection`),!t.addTrack)return f.error("Your browser does't support RTCPeerConnection#addTrack. Ignored.");e.getTracks().forEach(n=>{t.addTrack(n,e)})}_addStreamToMediaConnection(e,t){f.log(`add stream ${e.id} to media connection ${t.connectionId}`),t.addStream(e)}}class Ht extends ke.EventEmitter{emitError(e,t){f.error("Error:",t),this.emit("error",new Un(`${e}`,t))}}class Un extends Error{constructor(e,t){typeof t=="string"?super(t):(super(),Object.assign(this,t)),this.type=e}}class Vt extends Ht{get open(){return this._open}constructor(e,t,n){super(),this.peer=e,this.provider=t,this.options=n,this._open=!1,this.metadata=n.metadata}}const K=class K extends Vt{get type(){return M.Media}get localStream(){return this._localStream}get remoteStream(){return this._remoteStream}constructor(e,t,n){super(e,t,n),this._localStream=this.options._stream,this.connectionId=this.options.connectionId||K.ID_PREFIX+k.randomToken(),this._negotiator=new qt(this),this._localStream&&this._negotiator.startConnection({_stream:this._localStream,originator:!0})}_initializeDataChannel(e){this.dataChannel=e,this.dataChannel.onopen=()=>{f.log(`DC#${this.connectionId} dc connection success`),this.emit("willCloseOnRemote")},this.dataChannel.onclose=()=>{f.log(`DC#${this.connectionId} dc closed for:`,this.peer),this.close()}}addStream(e){f.log("Receiving stream",e),this._remoteStream=e,super.emit("stream",e)}handleMessage(e){const t=e.type,n=e.payload;switch(e.type){case S.Answer:this._negotiator.handleSDP(t,n.sdp),this._open=!0;break;case S.Candidate:this._negotiator.handleCandidate(n.candidate);break;default:f.warn(`Unrecognized message type:${t} from peer:${this.peer}`);break}}answer(e,t={}){if(this._localStream){f.warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");return}this._localStream=e,t&&t.sdpTransform&&(this.options.sdpTransform=t.sdpTransform),this._negotiator.startConnection({...this.options._payload,_stream:e});const n=this.provider._getMessages(this.connectionId);for(const s of n)this.handleMessage(s);this._open=!0}close(){this._negotiator&&(this._negotiator.cleanup(),this._negotiator=null),this._localStream=null,this._remoteStream=null,this.provider&&(this.provider._removeConnection(this),this.provider=null),this.options&&this.options._stream&&(this.options._stream=null),this.open&&(this._open=!1,super.emit("close"))}};Ve=new WeakMap,W(K,Ve,K.ID_PREFIX="mc_");let ne=K;class Nn{constructor(e){this._options=e}_buildRequest(e){const t=this._options.secure?"https":"http",{host:n,port:s,path:i,key:o}=this._options,a=new URL(`${t}://${n}:${s}${i}${o}/${e}`);return a.searchParams.set("ts",`${Date.now()}${Math.random()}`),a.searchParams.set("version",jt),fetch(a.href,{referrerPolicy:this._options.referrerPolicy})}async retrieveId(){try{const e=await this._buildRequest("id");if(e.status!==200)throw new Error(`Error. Status:${e.status}`);return e.text()}catch(e){f.error("Error retrieving ID",e);let t="";throw this._options.path==="/"&&this._options.host!==k.CLOUD_HOST&&(t=" If you passed in a `path` to your self-hosted PeerServer, you'll also need to pass in that same path when creating a new Peer."),new Error("Could not get an ID from the server."+t)}}async listAllPeers(){try{const e=await this._buildRequest("peers");if(e.status!==200){if(e.status===401){let t="";throw this._options.host===k.CLOUD_HOST?t="It looks like you're using the cloud server. You can email team@peerjs.com to enable peer listing for your API key.":t="You need to enable `allow_discovery` on your self-hosted PeerServer to use this feature.",new Error("It doesn't look like you have permission to list peers IDs. "+t)}throw new Error(`Error. Status:${e.status}`)}return e.json()}catch(e){throw f.error("Error retrieving list peers",e),new Error("Could not get list peers from the server."+e)}}}const B=class B extends Vt{get type(){return M.Data}constructor(e,t,n){super(e,t,n),this.connectionId=this.options.connectionId||B.ID_PREFIX+Nt(),this.label=this.options.label||this.connectionId,this.reliable=!!this.options.reliable,this._negotiator=new qt(this),this._negotiator.startConnection(this.options._payload||{originator:!0,reliable:this.reliable})}_initializeDataChannel(e){this.dataChannel=e,this.dataChannel.onopen=()=>{f.log(`DC#${this.connectionId} dc connection success`),this._open=!0,this.emit("open")},this.dataChannel.onmessage=t=>{f.log(`DC#${this.connectionId} dc onmessage:`,t.data)},this.dataChannel.onclose=()=>{f.log(`DC#${this.connectionId} dc closed for:`,this.peer),this.close()}}close(e){if(e!=null&&e.flush){this.send({__peerData:{type:"close"}});return}this._negotiator&&(this._negotiator.cleanup(),this._negotiator=null),this.provider&&(this.provider._removeConnection(this),this.provider=null),this.dataChannel&&(this.dataChannel.onopen=null,this.dataChannel.onmessage=null,this.dataChannel.onclose=null,this.dataChannel=null),this.open&&(this._open=!1,super.emit("close"))}send(e,t=!1){if(!this.open){this.emitError(Te.NotOpenYet,"Connection is not open. You should listen for the `open` event before sending messages.");return}return this._send(e,t)}async handleMessage(e){const t=e.payload;switch(e.type){case S.Answer:await this._negotiator.handleSDP(e.type,t.sdp);break;case S.Candidate:await this._negotiator.handleCandidate(t.candidate);break;default:f.warn("Unrecognized message type:",e.type,"from peer:",this.peer);break}}};Ke=new WeakMap,Je=new WeakMap,W(B,Ke,B.ID_PREFIX="dc_"),W(B,Je,B.MAX_BUFFERED_AMOUNT=8388608);let re=B;class Re extends re{get bufferSize(){return this._bufferSize}_initializeDataChannel(e){super._initializeDataChannel(e),this.dataChannel.binaryType="arraybuffer",this.dataChannel.addEventListener("message",t=>this._handleDataMessage(t))}_bufferedSend(e){(this._buffering||!this._trySend(e))&&(this._buffer.push(e),this._bufferSize=this._buffer.length)}_trySend(e){if(!this.open)return!1;if(this.dataChannel.bufferedAmount>re.MAX_BUFFERED_AMOUNT)return this._buffering=!0,setTimeout(()=>{this._buffering=!1,this._tryBuffer()},50),!1;try{this.dataChannel.send(e)}catch(t){return f.error(`DC#:${this.connectionId} Error when sending:`,t),this._buffering=!0,this.close(),!1}return!0}_tryBuffer(){if(!this.open||this._buffer.length===0)return;const e=this._buffer[0];this._trySend(e)&&(this._buffer.shift(),this._bufferSize=this._buffer.length,this._tryBuffer())}close(e){if(e!=null&&e.flush){this.send({__peerData:{type:"close"}});return}this._buffer=[],this._bufferSize=0,super.close()}constructor(...e){super(...e),this._buffer=[],this._bufferSize=0,this._buffering=!1}}class Pe extends Re{close(e){super.close(e),this._chunkedData={}}constructor(e,t,n){super(e,t,n),this.chunker=new Ut,this.serialization=te.Binary,this._chunkedData={}}_handleDataMessage({data:e}){const t=Ge(e),n=t.__peerData;if(n){if(n.type==="close"){this.close();return}this._handleChunk(t);return}this.emit("data",t)}_handleChunk(e){const t=e.__peerData,n=this._chunkedData[t]||{data:[],count:0,total:e.total};if(n.data[e.n]=new Uint8Array(e.data),n.count++,this._chunkedData[t]=n,n.total===n.count){delete this._chunkedData[t];const s=Mn(n.data);this._handleDataMessage({data:s})}}_send(e,t){const n=Qe(e);if(n instanceof Promise)return this._send_blob(n);if(!t&&n.byteLength>this.chunker.chunkedMTU){this._sendChunks(n);return}this._bufferedSend(n)}async _send_blob(e){const t=await e;if(t.byteLength>this.chunker.chunkedMTU){this._sendChunks(t);return}this._bufferedSend(t)}_sendChunks(e){const t=this.chunker.chunk(e);f.log(`DC#${this.connectionId} Try to send ${t.length} chunks...`);for(const n of t)this.send(n,!0)}}class zn extends Re{_handleDataMessage({data:e}){super.emit("data",e)}_send(e,t){this._bufferedSend(e)}constructor(...e){super(...e),this.serialization=te.None}}class Fn extends Re{_handleDataMessage({data:e}){const t=this.parse(this.decoder.decode(e)),n=t.__peerData;if(n&&n.type==="close"){this.close();return}this.emit("data",t)}_send(e,t){const n=this.encoder.encode(this.stringify(e));if(n.byteLength>=k.chunkedMTU){this.emitError(Te.MessageToBig,"Message too big for JSON channel");return}this._bufferedSend(n)}constructor(...e){super(...e),this.serialization=te.JSON,this.encoder=new TextEncoder,this.decoder=new TextDecoder,this.stringify=JSON.stringify,this.parse=JSON.parse}}const J=class J extends Ht{get id(){return this._id}get options(){return this._options}get open(){return this._open}get socket(){return this._socket}get connections(){const e=Object.create(null);for(const[t,n]of this._connections)e[t]=n;return e}get destroyed(){return this._destroyed}get disconnected(){return this._disconnected}constructor(e,t){super(),this._serializers={raw:zn,json:Fn,binary:Pe,"binary-utf8":Pe,default:Pe},this._id=null,this._lastServerId=null,this._destroyed=!1,this._disconnected=!1,this._open=!1,this._connections=new Map,this._lostMessages=new Map;let n;if(e&&e.constructor==Object?t=e:e&&(n=e.toString()),t={debug:0,host:k.CLOUD_HOST,port:k.CLOUD_PORT,path:"/",key:J.DEFAULT_KEY,token:k.randomToken(),config:k.defaultConfig,referrerPolicy:"strict-origin-when-cross-origin",serializers:{},...t},this._options=t,this._serializers={...this._serializers,...this.options.serializers},this._options.host==="/"&&(this._options.host=window.location.hostname),this._options.path&&(this._options.path[0]!=="/"&&(this._options.path="/"+this._options.path),this._options.path[this._options.path.length-1]!=="/"&&(this._options.path+="/")),this._options.secure===void 0&&this._options.host!==k.CLOUD_HOST?this._options.secure=k.isSecure():this._options.host==k.CLOUD_HOST&&(this._options.secure=!0),this._options.logFunction&&f.setLogFunction(this._options.logFunction),f.logLevel=this._options.debug||0,this._api=new Nn(t),this._socket=this._createServerConnection(),!k.supports.audioVideo&&!k.supports.data){this._delayedAbort(y.BrowserIncompatible,"The current browser does not support WebRTC");return}if(n&&!k.validateId(n)){this._delayedAbort(y.InvalidID,`ID "${n}" is invalid`);return}n?this._initialize(n):this._api.retrieveId().then(s=>this._initialize(s)).catch(s=>this._abort(y.ServerError,s))}_createServerConnection(){const e=new Bn(this._options.secure,this._options.host,this._options.port,this._options.path,this._options.key,this._options.pingInterval);return e.on(A.Message,t=>{this._handleMessage(t)}),e.on(A.Error,t=>{this._abort(y.SocketError,t)}),e.on(A.Disconnected,()=>{this.disconnected||(this.emitError(y.Network,"Lost connection to server."),this.disconnect())}),e.on(A.Close,()=>{this.disconnected||this._abort(y.SocketClosed,"Underlying socket is already closed.")}),e}_initialize(e){this._id=e,this.socket.start(e,this._options.token)}_handleMessage(e){const t=e.type,n=e.payload,s=e.src;switch(t){case S.Open:this._lastServerId=this.id,this._open=!0,this.emit("open",this.id);break;case S.Error:this._abort(y.ServerError,n.msg);break;case S.IdTaken:this._abort(y.UnavailableID,`ID "${this.id}" is taken`);break;case S.InvalidKey:this._abort(y.InvalidKey,`API KEY "${this._options.key}" is invalid`);break;case S.Leave:f.log(`Received leave message from ${s}`),this._cleanupPeer(s),this._connections.delete(s);break;case S.Expire:this.emitError(y.PeerUnavailable,`Could not connect to peer ${s}`);break;case S.Offer:{const i=n.connectionId;let o=this.getConnection(s,i);if(o&&(o.close(),f.warn(`Offer received for existing Connection ID:${i}`)),n.type===M.Media){const c=new ne(s,this,{connectionId:i,_payload:n,metadata:n.metadata});o=c,this._addConnection(s,o),this.emit("call",c)}else if(n.type===M.Data){const c=new this._serializers[n.serialization](s,this,{connectionId:i,_payload:n,metadata:n.metadata,label:n.label,serialization:n.serialization,reliable:n.reliable});o=c,this._addConnection(s,o),this.emit("connection",c)}else{f.warn(`Received malformed connection type:${n.type}`);return}const a=this._getMessages(i);for(const c of a)o.handleMessage(c);break}default:{if(!n){f.warn(`You received a malformed message from ${s} of type ${t}`);return}const i=n.connectionId,o=this.getConnection(s,i);o&&o.peerConnection?o.handleMessage(e):i?this._storeMessage(i,e):f.warn("You received an unrecognized message:",e);break}}}_storeMessage(e,t){this._lostMessages.has(e)||this._lostMessages.set(e,[]),this._lostMessages.get(e).push(t)}_getMessages(e){const t=this._lostMessages.get(e);return t?(this._lostMessages.delete(e),t):[]}connect(e,t={}){if(t={serialization:"default",...t},this.disconnected){f.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect, or call reconnect on this peer if you believe its ID to still be available."),this.emitError(y.Disconnected,"Cannot connect to new Peer after disconnecting from server.");return}const n=new this._serializers[t.serialization](e,this,t);return this._addConnection(e,n),n}call(e,t,n={}){if(this.disconnected){f.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emitError(y.Disconnected,"Cannot connect to new Peer after disconnecting from server.");return}if(!t){f.error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");return}const s=new ne(e,this,{...n,_stream:t});return this._addConnection(e,s),s}_addConnection(e,t){f.log(`add connection ${t.type}:${t.connectionId} to peerId:${e}`),this._connections.has(e)||this._connections.set(e,[]),this._connections.get(e).push(t)}_removeConnection(e){const t=this._connections.get(e.peer);if(t){const n=t.indexOf(e);n!==-1&&t.splice(n,1)}this._lostMessages.delete(e.connectionId)}getConnection(e,t){const n=this._connections.get(e);if(!n)return null;for(const s of n)if(s.connectionId===t)return s;return null}_delayedAbort(e,t){setTimeout(()=>{this._abort(e,t)},0)}_abort(e,t){f.error("Aborting!"),this.emitError(e,t),this._lastServerId?this.disconnect():this.destroy()}destroy(){this.destroyed||(f.log(`Destroy peer with ID:${this.id}`),this.disconnect(),this._cleanup(),this._destroyed=!0,this.emit("close"))}_cleanup(){for(const e of this._connections.keys())this._cleanupPeer(e),this._connections.delete(e);this.socket.removeAllListeners()}_cleanupPeer(e){const t=this._connections.get(e);if(t)for(const n of t)n.close()}disconnect(){if(this.disconnected)return;const e=this.id;f.log(`Disconnect peer with ID:${e}`),this._disconnected=!0,this._open=!1,this.socket.close(),this._lastServerId=e,this._id=null,this.emit("disconnected",e)}reconnect(){if(this.disconnected&&!this.destroyed)f.log(`Attempting reconnection to server with ID ${this._lastServerId}`),this._disconnected=!1,this._initialize(this._lastServerId);else{if(this.destroyed)throw new Error("This peer cannot reconnect to the server. It has already been destroyed.");if(!this.disconnected&&!this.open)f.error("In a hurry? We're still trying to make the initial connection!");else throw new Error(`Peer ${this.id} cannot reconnect because it is not disconnected from the server!`)}}listAllPeers(e=t=>{}){this._api.listAllPeers().then(t=>e(t)).catch(t=>this._abort(y.ServerError,t))}};We=new WeakMap,W(J,We,J.DEFAULT_KEY="peerjs");let Ee=J;var jn=Ee;class qn{constructor(e){this.config=e,this.peer=null,this.localStream=null,this.connections={},this.callbacks={onStream:null,onPeerLeft:null,onError:null,onScreenShareEnded:null}}buildIceServers(){const e=[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}],t=Array.isArray(this.config.iceServers)?this.config.iceServers:[];return[...e,...t]}async initialize(e,t=0){return new Promise((n,s)=>{const i=t===0?e:`${e}-r${Math.random().toString(36).slice(2,6)}`;this.peer=new jn(i,{host:this.config.peerServer.host,port:this.config.peerServer.port,path:this.config.peerServer.path||"/peerjs",secure:this.config.peerServer.secure??!1,config:{iceServers:this.buildIceServers()}});let o=!1;this.peer.on("open",a=>{o=!0,console.log("Peer initialized:",a),this.actualPeerId=a,n(a)}),this.peer.on("error",async a=>{if(!o&&a&&a.type==="unavailable-id"&&t<1){console.warn(`Peer id "${i}" taken — retrying with a suffix`);try{this.peer.destroy()}catch{}this.peer=null;try{const c=await this.initialize(e,t+1);o=!0,n(c)}catch(c){s(c)}return}console.error("Peer error:",a),this.callbacks.onError&&this.callbacks.onError(a),o||s(a)}),this.peer.on("call",a=>{this.handleIncomingCall(a)}),this.peer.on("disconnected",()=>{this._disposed||(console.warn("PeerJS signalling disconnected — attempting reconnect"),setTimeout(()=>{try{this.peer&&!this.peer.destroyed&&this.peer.reconnect()}catch(a){console.warn("Peer reconnect threw:",a&&a.message)}},1e3))})})}async getLocalStream(){if(this.localStream)return this.localStream;try{return this.localStream=await navigator.mediaDevices.getUserMedia({video:!0,audio:!0}),this.localStream}catch(e){throw console.error("Failed to get local stream:",e),this.callbacks.onError&&this.callbacks.onError(e),e}}async handleIncomingCall(e){if(this.connections[e.peer]){console.log("Duplicate incoming call from",e.peer,"— closing");try{e.close()}catch{}return}this.connections[e.peer]={call:e,stream:null};const t=await this.getLocalStream();e.answer(t),e.on("stream",n=>{console.log("Received stream from:",e.peer),this.connections[e.peer]={call:e,stream:n},this.callbacks.onStream&&this.callbacks.onStream(e.peer,n,!1)}),e.on("close",()=>{console.log("Call closed:",e.peer),delete this.connections[e.peer],this.callbacks.onPeerLeft&&this.callbacks.onPeerLeft(e.peer)})}async callPeer(e){if(this.connections[e]){console.log("Already connected to:",e);return}if(this.peer&&this.peer.id&&this.peer.id>e){console.log(`Waiting for ${e} to call us (our id ${this.peer.id} > theirs)`);return}this.connections[e]={call:null,stream:null};const t=await this.getLocalStream();if(this.connections[e]&&this.connections[e].call)return;const n=this.peer.call(e,t);this.connections[e]={call:n,stream:null},n.on("stream",s=>{console.log("Received stream from:",e),this.connections[e]={call:n,stream:s},this.callbacks.onStream&&this.callbacks.onStream(e,s,!1)}),n.on("close",()=>{console.log("Call closed:",e),delete this.connections[e],this.callbacks.onPeerLeft&&this.callbacks.onPeerLeft(e)}),n.on("error",s=>{console.error("Call error:",s),delete this.connections[e]})}toggleVideo(){if(this.localStream){const e=this.localStream.getVideoTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}toggleAudio(){if(this.localStream){const e=this.localStream.getAudioTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}replaceVideoTrackOnAllConnections(e){for(const{call:t}of Object.values(this.connections)){const n=t&&t.peerConnection;if(!n)continue;const s=n.getSenders().find(i=>i.track&&i.track.kind==="video");s&&s.replaceTrack(e).catch(i=>console.warn("replaceTrack failed:",i&&i.message))}}async startScreenShare(){if(!navigator.mediaDevices||!navigator.mediaDevices.getDisplayMedia)throw new Error("Screen sharing is not supported in this browser");const t=(await navigator.mediaDevices.getDisplayMedia({video:!0,audio:!1})).getVideoTracks()[0];if(!t)throw new Error("Could not obtain display video track");return this.cameraVideoTrack=this.localStream.getVideoTracks()[0]||null,this.replaceVideoTrackOnAllConnections(t),this.cameraVideoTrack&&this.localStream.removeTrack(this.cameraVideoTrack),this.localStream.addTrack(t),this.screenTrack=t,t.addEventListener("ended",async()=>{const n=!!this.screenTrack;if(await this.stopScreenShare().catch(()=>{}),n&&this.callbacks.onScreenShareEnded)try{this.callbacks.onScreenShareEnded()}catch(s){console.warn(s)}}),t}async stopScreenShare(){if(!this.screenTrack)return!1;try{this.screenTrack.stop()}catch{}let e=this.cameraVideoTrack;if(!e||e.readyState==="ended")try{e=(await navigator.mediaDevices.getUserMedia({video:!0,audio:!1})).getVideoTracks()[0]}catch(t){console.warn("Could not restore camera after screen share:",t&&t.message),e=null}if(e){this.replaceVideoTrackOnAllConnections(e);try{this.localStream.removeTrack(this.screenTrack)}catch{}this.localStream.addTrack(e),this.cameraVideoTrack=e}return this.screenTrack=null,!0}isScreenSharing(){return!!this.screenTrack}on(e,t){this.callbacks.hasOwnProperty("on"+e.charAt(0).toUpperCase()+e.slice(1))&&(this.callbacks["on"+e.charAt(0).toUpperCase()+e.slice(1)]=t)}disconnect(){this._disposed=!0,Object.values(this.connections).forEach(({call:e})=>{e.close()}),this.connections={},this.localStream&&(this.localStream.getTracks().forEach(e=>e.stop()),this.localStream=null),this.peer&&(this.peer.destroy(),this.peer=null)}}const E=Object.create(null);E.open="0",E.close="1",E.ping="2",E.pong="3",E.message="4",E.upgrade="5",E.noop="6";const se=Object.create(null);Object.keys(E).forEach(r=>{se[E[r]]=r});const we={type:"error",data:"parser error"},Kt=typeof Blob=="function"||typeof Blob<"u"&&Object.prototype.toString.call(Blob)==="[object BlobConstructor]",Jt=typeof ArrayBuffer=="function",Wt=r=>typeof ArrayBuffer.isView=="function"?ArrayBuffer.isView(r):r&&r.buffer instanceof ArrayBuffer,Me=({type:r,data:e},t,n)=>Kt&&e instanceof Blob?t?n(e):Yt(e,n):Jt&&(e instanceof ArrayBuffer||Wt(e))?t?n(e):Yt(new Blob([e]),n):n(E[r]+(e||"")),Yt=(r,e)=>{const t=new FileReader;return t.onload=function(){const n=t.result.split(",")[1];e("b"+(n||""))},t.readAsDataURL(r)};function Gt(r){return r instanceof Uint8Array?r:r instanceof ArrayBuffer?new Uint8Array(r):new Uint8Array(r.buffer,r.byteOffset,r.byteLength)}let Ae;function Hn(r,e){if(Kt&&r.data instanceof Blob)return r.data.arrayBuffer().then(Gt).then(e);if(Jt&&(r.data instanceof ArrayBuffer||Wt(r.data)))return e(Gt(r.data));Me(r,!1,t=>{Ae||(Ae=new TextEncoder),e(Ae.encode(t))})}const Qt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",q=typeof Uint8Array>"u"?[]:new Uint8Array(256);for(let r=0;r<Qt.length;r++)q[Qt.charCodeAt(r)]=r;const Vn=r=>{let e=r.length*.75,t=r.length,n,s=0,i,o,a,c;r[r.length-1]==="="&&(e--,r[r.length-2]==="="&&e--);const h=new ArrayBuffer(e),l=new Uint8Array(h);for(n=0;n<t;n+=4)i=q[r.charCodeAt(n)],o=q[r.charCodeAt(n+1)],a=q[r.charCodeAt(n+2)],c=q[r.charCodeAt(n+3)],l[s++]=i<<2|o>>4,l[s++]=(o&15)<<4|a>>2,l[s++]=(a&3)<<6|c&63;return h},Kn=typeof ArrayBuffer=="function",Ie=(r,e)=>{if(typeof r!="string")return{type:"message",data:Xt(r,e)};const t=r.charAt(0);return t==="b"?{type:"message",data:Jn(r.substring(1),e)}:se[t]?r.length>1?{type:se[t],data:r.substring(1)}:{type:se[t]}:we},Jn=(r,e)=>{if(Kn){const t=Vn(r);return Xt(t,e)}else return{base64:!0,data:r}},Xt=(r,e)=>{switch(e){case"blob":return r instanceof Blob?r:new Blob([r]);case"arraybuffer":default:return r instanceof ArrayBuffer?r:r.buffer}},Zt="",Wn=(r,e)=>{const t=r.length,n=new Array(t);let s=0;r.forEach((i,o)=>{Me(i,!1,a=>{n[o]=a,++s===t&&e(n.join(Zt))})})},Yn=(r,e)=>{const t=r.split(Zt),n=[];for(let s=0;s<t.length;s++){const i=Ie(t[s],e);if(n.push(i),i.type==="error")break}return n};function Gn(){return new TransformStream({transform(r,e){Hn(r,t=>{const n=t.length;let s;if(n<126)s=new Uint8Array(1),new DataView(s.buffer).setUint8(0,n);else if(n<65536){s=new Uint8Array(3);const i=new DataView(s.buffer);i.setUint8(0,126),i.setUint16(1,n)}else{s=new Uint8Array(9);const i=new DataView(s.buffer);i.setUint8(0,127),i.setBigUint64(1,BigInt(n))}r.data&&typeof r.data!="string"&&(s[0]|=128),e.enqueue(s),e.enqueue(t)})}})}let Oe;function ie(r){return r.reduce((e,t)=>e+t.length,0)}function oe(r,e){if(r[0].length===e)return r.shift();const t=new Uint8Array(e);let n=0;for(let s=0;s<e;s++)t[s]=r[0][n++],n===r[0].length&&(r.shift(),n=0);return r.length&&n<r[0].length&&(r[0]=r[0].slice(n)),t}function Qn(r,e){Oe||(Oe=new TextDecoder);const t=[];let n=0,s=-1,i=!1;return new TransformStream({transform(o,a){for(t.push(o);;){if(n===0){if(ie(t)<1)break;const c=oe(t,1);i=(c[0]&128)===128,s=c[0]&127,s<126?n=3:s===126?n=1:n=2}else if(n===1){if(ie(t)<2)break;const c=oe(t,2);s=new DataView(c.buffer,c.byteOffset,c.length).getUint16(0),n=3}else if(n===2){if(ie(t)<8)break;const c=oe(t,8),h=new DataView(c.buffer,c.byteOffset,c.length),l=h.getUint32(0);if(l>Math.pow(2,21)-1){a.enqueue(we);break}s=l*Math.pow(2,32)+h.getUint32(4),n=3}else{if(ie(t)<s)break;const c=oe(t,s);a.enqueue(Ie(i?c:Oe.decode(c),e)),n=0}if(s===0||s>r){a.enqueue(we);break}}}})}const en=4;function b(r){if(r)return Xn(r)}function Xn(r){for(var e in b.prototype)r[e]=b.prototype[e];return r}b.prototype.on=b.prototype.addEventListener=function(r,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+r]=this._callbacks["$"+r]||[]).push(e),this},b.prototype.once=function(r,e){function t(){this.off(r,t),e.apply(this,arguments)}return t.fn=e,this.on(r,t),this},b.prototype.off=b.prototype.removeListener=b.prototype.removeAllListeners=b.prototype.removeEventListener=function(r,e){if(this._callbacks=this._callbacks||{},arguments.length==0)return this._callbacks={},this;var t=this._callbacks["$"+r];if(!t)return this;if(arguments.length==1)return delete this._callbacks["$"+r],this;for(var n,s=0;s<t.length;s++)if(n=t[s],n===e||n.fn===e){t.splice(s,1);break}return t.length===0&&delete this._callbacks["$"+r],this},b.prototype.emit=function(r){this._callbacks=this._callbacks||{};for(var e=new Array(arguments.length-1),t=this._callbacks["$"+r],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(t){t=t.slice(0);for(var n=0,s=t.length;n<s;++n)t[n].apply(this,e)}return this},b.prototype.emitReserved=b.prototype.emit,b.prototype.listeners=function(r){return this._callbacks=this._callbacks||{},this._callbacks["$"+r]||[]},b.prototype.hasListeners=function(r){return!!this.listeners(r).length};const ae=typeof Promise=="function"&&typeof Promise.resolve=="function"?e=>Promise.resolve().then(e):(e,t)=>t(e,0),T=typeof self<"u"?self:typeof window<"u"?window:Function("return this")(),Zn="arraybuffer";function ss(){}function tn(r,...e){return e.reduce((t,n)=>(r.hasOwnProperty(n)&&(t[n]=r[n]),t),{})}const er=T.setTimeout,tr=T.clearTimeout;function ce(r,e){e.useNativeTimers?(r.setTimeoutFn=er.bind(T),r.clearTimeoutFn=tr.bind(T)):(r.setTimeoutFn=T.setTimeout.bind(T),r.clearTimeoutFn=T.clearTimeout.bind(T))}const nr=1.33;function rr(r){return typeof r=="string"?sr(r):Math.ceil((r.byteLength||r.size)*nr)}function sr(r){let e=0,t=0;for(let n=0,s=r.length;n<s;n++)e=r.charCodeAt(n),e<128?t+=1:e<2048?t+=2:e<55296||e>=57344?t+=3:(n++,t+=4);return t}function nn(){return Date.now().toString(36).substring(3)+Math.random().toString(36).substring(2,5)}function ir(r){let e="";for(let t in r)r.hasOwnProperty(t)&&(e.length&&(e+="&"),e+=encodeURIComponent(t)+"="+encodeURIComponent(r[t]));return e}function or(r){let e={},t=r.split("&");for(let n=0,s=t.length;n<s;n++){let i=t[n].split("=");e[decodeURIComponent(i[0])]=decodeURIComponent(i[1])}return e}class ar extends Error{constructor(e,t,n){super(e),this.description=t,this.context=n,this.type="TransportError"}}class De extends b{constructor(e){super(),this.writable=!1,ce(this,e),this.opts=e,this.query=e.query,this.socket=e.socket,this.supportsBinary=!e.forceBase64}onError(e,t,n){return super.emitReserved("error",new ar(e,t,n)),this}open(){return this.readyState="opening",this.doOpen(),this}close(){return(this.readyState==="opening"||this.readyState==="open")&&(this.doClose(),this.onClose()),this}send(e){this.readyState==="open"&&this.write(e)}onOpen(){this.readyState="open",this.writable=!0,super.emitReserved("open")}onData(e){const t=Ie(e,this.socket.binaryType);this.onPacket(t)}onPacket(e){super.emitReserved("packet",e)}onClose(e){this.readyState="closed",super.emitReserved("close",e)}pause(e){}createUri(e,t={}){return e+"://"+this._hostname()+this._port()+this.opts.path+this._query(t)}_hostname(){const e=this.opts.hostname;return e.indexOf(":")===-1?e:"["+e+"]"}_port(){return this.opts.port&&(this.opts.secure&&Number(this.opts.port)!==443||!this.opts.secure&&Number(this.opts.port)!==80)?":"+this.opts.port:""}_query(e){const t=ir(e);return t.length?"?"+t:""}}class cr extends De{constructor(){super(...arguments),this._polling=!1}get name(){return"polling"}doOpen(){this._poll()}pause(e){this.readyState="pausing";const t=()=>{this.readyState="paused",e()};if(this._polling||!this.writable){let n=0;this._polling&&(n++,this.once("pollComplete",function(){--n||t()})),this.writable||(n++,this.once("drain",function(){--n||t()}))}else t()}_poll(){this._polling=!0,this.doPoll(),this.emitReserved("poll")}onData(e){const t=n=>{if(this.readyState==="opening"&&n.type==="open"&&this.onOpen(),n.type==="close")return this.onClose({description:"transport closed by the server"}),!1;this.onPacket(n)};Yn(e,this.socket.binaryType).forEach(t),this.readyState!=="closed"&&(this._polling=!1,this.emitReserved("pollComplete"),this.readyState==="open"&&this._poll())}doClose(){const e=()=>{this.write([{type:"close"}])};this.readyState==="open"?e():this.once("open",e)}write(e){this.writable=!1,Wn(e,t=>{this.doWrite(t,()=>{this.writable=!0,this.emitReserved("drain")})})}uri(){const e=this.opts.secure?"https":"http",t=this.query||{};return this.opts.timestampRequests!==!1&&(t[this.opts.timestampParam]=nn()),!this.supportsBinary&&!t.sid&&(t.b64=1),this.createUri(e,t)}}let rn=!1;try{rn=typeof XMLHttpRequest<"u"&&"withCredentials"in new XMLHttpRequest}catch{}const hr=rn;function lr(){}class dr extends cr{constructor(e){if(super(e),typeof location<"u"){const t=location.protocol==="https:";let n=location.port;n||(n=t?"443":"80"),this.xd=typeof location<"u"&&e.hostname!==location.hostname||n!==e.port}}doWrite(e,t){const n=this.request({method:"POST",data:e});n.on("success",t),n.on("error",(s,i)=>{this.onError("xhr post error",s,i)})}doPoll(){const e=this.request();e.on("data",this.onData.bind(this)),e.on("error",(t,n)=>{this.onError("xhr poll error",t,n)}),this.pollXhr=e}}class w extends b{constructor(e,t,n){super(),this.createRequest=e,ce(this,n),this._opts=n,this._method=n.method||"GET",this._uri=t,this._data=n.data!==void 0?n.data:null,this._create()}_create(){var e;const t=tn(this._opts,"agent","pfx","key","passphrase","cert","ca","ciphers","rejectUnauthorized","autoUnref");t.xdomain=!!this._opts.xd;const n=this._xhr=this.createRequest(t);try{n.open(this._method,this._uri,!0);try{if(this._opts.extraHeaders){n.setDisableHeaderCheck&&n.setDisableHeaderCheck(!0);for(let s in this._opts.extraHeaders)this._opts.extraHeaders.hasOwnProperty(s)&&n.setRequestHeader(s,this._opts.extraHeaders[s])}}catch{}if(this._method==="POST")try{n.setRequestHeader("Content-type","text/plain;charset=UTF-8")}catch{}try{n.setRequestHeader("Accept","*/*")}catch{}(e=this._opts.cookieJar)===null||e===void 0||e.addCookies(n),"withCredentials"in n&&(n.withCredentials=this._opts.withCredentials),this._opts.requestTimeout&&(n.timeout=this._opts.requestTimeout),n.onreadystatechange=()=>{var s;n.readyState===3&&((s=this._opts.cookieJar)===null||s===void 0||s.parseCookies(n.getResponseHeader("set-cookie"))),n.readyState===4&&(n.status===200||n.status===1223?this._onLoad():this.setTimeoutFn(()=>{this._onError(typeof n.status=="number"?n.status:0)},0))},n.send(this._data)}catch(s){this.setTimeoutFn(()=>{this._onError(s)},0);return}typeof document<"u"&&(this._index=w.requestsCount++,w.requests[this._index]=this)}_onError(e){this.emitReserved("error",e,this._xhr),this._cleanup(!0)}_cleanup(e){if(!(typeof this._xhr>"u"||this._xhr===null)){if(this._xhr.onreadystatechange=lr,e)try{this._xhr.abort()}catch{}typeof document<"u"&&delete w.requests[this._index],this._xhr=null}}_onLoad(){const e=this._xhr.responseText;e!==null&&(this.emitReserved("data",e),this.emitReserved("success"),this._cleanup())}abort(){this._cleanup()}}if(w.requestsCount=0,w.requests={},typeof document<"u"){if(typeof attachEvent=="function")attachEvent("onunload",sn);else if(typeof addEventListener=="function"){const r="onpagehide"in T?"pagehide":"unload";addEventListener(r,sn,!1)}}function sn(){for(let r in w.requests)w.requests.hasOwnProperty(r)&&w.requests[r].abort()}const ur=function(){const r=on({xdomain:!1});return r&&r.responseType!==null}();class fr extends dr{constructor(e){super(e);const t=e&&e.forceBase64;this.supportsBinary=ur&&!t}request(e={}){return Object.assign(e,{xd:this.xd},this.opts),new w(on,this.uri(),e)}}function on(r){const e=r.xdomain;try{if(typeof XMLHttpRequest<"u"&&(!e||hr))return new XMLHttpRequest}catch{}if(!e)try{return new T[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP")}catch{}}const an=typeof navigator<"u"&&typeof navigator.product=="string"&&navigator.product.toLowerCase()==="reactnative";class pr extends De{get name(){return"websocket"}doOpen(){const e=this.uri(),t=this.opts.protocols,n=an?{}:tn(this.opts,"agent","perMessageDeflate","pfx","key","passphrase","cert","ca","ciphers","rejectUnauthorized","localAddress","protocolVersion","origin","maxPayload","family","checkServerIdentity");this.opts.extraHeaders&&(n.headers=this.opts.extraHeaders);try{this.ws=this.createSocket(e,t,n)}catch(s){return this.emitReserved("error",s)}this.ws.binaryType=this.socket.binaryType,this.addEventListeners()}addEventListeners(){this.ws.onopen=()=>{this.opts.autoUnref&&this.ws._socket.unref(),this.onOpen()},this.ws.onclose=e=>this.onClose({description:"websocket connection closed",context:e}),this.ws.onmessage=e=>this.onData(e.data),this.ws.onerror=e=>this.onError("websocket error",e)}write(e){this.writable=!1;for(let t=0;t<e.length;t++){const n=e[t],s=t===e.length-1;Me(n,this.supportsBinary,i=>{try{this.doWrite(n,i)}catch{}s&&ae(()=>{this.writable=!0,this.emitReserved("drain")},this.setTimeoutFn)})}}doClose(){typeof this.ws<"u"&&(this.ws.onerror=()=>{},this.ws.close(),this.ws=null)}uri(){const e=this.opts.secure?"wss":"ws",t=this.query||{};return this.opts.timestampRequests&&(t[this.opts.timestampParam]=nn()),this.supportsBinary||(t.b64=1),this.createUri(e,t)}}const Le=T.WebSocket||T.MozWebSocket;class gr extends pr{createSocket(e,t,n){return an?new Le(e,t,n):t?new Le(e,t):new Le(e)}doWrite(e,t){this.ws.send(t)}}class mr extends De{get name(){return"webtransport"}doOpen(){try{this._transport=new WebTransport(this.createUri("https"),this.opts.transportOptions[this.name])}catch(e){return this.emitReserved("error",e)}this._transport.closed.then(()=>{this.onClose()}).catch(e=>{this.onError("webtransport error",e)}),this._transport.ready.then(()=>{this._transport.createBidirectionalStream().then(e=>{const t=Qn(Number.MAX_SAFE_INTEGER,this.socket.binaryType),n=e.readable.pipeThrough(t).getReader(),s=Gn();s.readable.pipeTo(e.writable),this._writer=s.writable.getWriter();const i=()=>{n.read().then(({done:a,value:c})=>{a||(this.onPacket(c),i())}).catch(a=>{})};i();const o={type:"open"};this.query.sid&&(o.data=`{"sid":"${this.query.sid}"}`),this._writer.write(o).then(()=>this.onOpen())})})}write(e){this.writable=!1;for(let t=0;t<e.length;t++){const n=e[t],s=t===e.length-1;this._writer.write(n).then(()=>{s&&ae(()=>{this.writable=!0,this.emitReserved("drain")},this.setTimeoutFn)})}}doClose(){var e;(e=this._transport)===null||e===void 0||e.close()}}const yr={websocket:gr,webtransport:mr,polling:fr},br=/^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,_r=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"];function $e(r){if(r.length>8e3)throw"URI too long";const e=r,t=r.indexOf("["),n=r.indexOf("]");t!=-1&&n!=-1&&(r=r.substring(0,t)+r.substring(t,n).replace(/:/g,";")+r.substring(n,r.length));let s=br.exec(r||""),i={},o=14;for(;o--;)i[_r[o]]=s[o]||"";return t!=-1&&n!=-1&&(i.source=e,i.host=i.host.substring(1,i.host.length-1).replace(/;/g,":"),i.authority=i.authority.replace("[","").replace("]","").replace(/;/g,":"),i.ipv6uri=!0),i.pathNames=vr(i,i.path),i.queryKey=Sr(i,i.query),i}function vr(r,e){const t=/\/{2,9}/g,n=e.replace(t,"/").split("/");return(e.slice(0,1)=="/"||e.length===0)&&n.splice(0,1),e.slice(-1)=="/"&&n.splice(n.length-1,1),n}function Sr(r,e){const t={};return e.replace(/(?:^|&)([^&=]*)=?([^&]*)/g,function(n,s,i){s&&(t[s]=i)}),t}const Be=typeof addEventListener=="function"&&typeof removeEventListener=="function",he=[];Be&&addEventListener("offline",()=>{he.forEach(r=>r())},!1);class I extends b{constructor(e,t){if(super(),this.binaryType=Zn,this.writeBuffer=[],this._prevBufferLen=0,this._pingInterval=-1,this._pingTimeout=-1,this._maxPayload=-1,this._pingTimeoutTime=1/0,e&&typeof e=="object"&&(t=e,e=null),e){const n=$e(e);t.hostname=n.host,t.secure=n.protocol==="https"||n.protocol==="wss",t.port=n.port,n.query&&(t.query=n.query)}else t.host&&(t.hostname=$e(t.host).host);ce(this,t),this.secure=t.secure!=null?t.secure:typeof location<"u"&&location.protocol==="https:",t.hostname&&!t.port&&(t.port=this.secure?"443":"80"),this.hostname=t.hostname||(typeof location<"u"?location.hostname:"localhost"),this.port=t.port||(typeof location<"u"&&location.port?location.port:this.secure?"443":"80"),this.transports=[],this._transportsByName={},t.transports.forEach(n=>{const s=n.prototype.name;this.transports.push(s),this._transportsByName[s]=n}),this.opts=Object.assign({path:"/engine.io",agent:!1,withCredentials:!1,upgrade:!0,timestampParam:"t",rememberUpgrade:!1,addTrailingSlash:!0,rejectUnauthorized:!0,perMessageDeflate:{threshold:1024},transportOptions:{},closeOnBeforeunload:!1},t),this.opts.path=this.opts.path.replace(/\/$/,"")+(this.opts.addTrailingSlash?"/":""),typeof this.opts.query=="string"&&(this.opts.query=or(this.opts.query)),Be&&(this.opts.closeOnBeforeunload&&(this._beforeunloadEventListener=()=>{this.transport&&(this.transport.removeAllListeners(),this.transport.close())},addEventListener("beforeunload",this._beforeunloadEventListener,!1)),this.hostname!=="localhost"&&(this._offlineEventListener=()=>{this._onClose("transport close",{description:"network connection lost"})},he.push(this._offlineEventListener))),this.opts.withCredentials&&(this._cookieJar=void 0),this._open()}createTransport(e){const t=Object.assign({},this.opts.query);t.EIO=en,t.transport=e,this.id&&(t.sid=this.id);const n=Object.assign({},this.opts,{query:t,socket:this,hostname:this.hostname,secure:this.secure,port:this.port},this.opts.transportOptions[e]);return new this._transportsByName[e](n)}_open(){if(this.transports.length===0){this.setTimeoutFn(()=>{this.emitReserved("error","No transports available")},0);return}const e=this.opts.rememberUpgrade&&I.priorWebsocketSuccess&&this.transports.indexOf("websocket")!==-1?"websocket":this.transports[0];this.readyState="opening";const t=this.createTransport(e);t.open(),this.setTransport(t)}setTransport(e){this.transport&&this.transport.removeAllListeners(),this.transport=e,e.on("drain",this._onDrain.bind(this)).on("packet",this._onPacket.bind(this)).on("error",this._onError.bind(this)).on("close",t=>this._onClose("transport close",t))}onOpen(){this.readyState="open",I.priorWebsocketSuccess=this.transport.name==="websocket",this.emitReserved("open"),this.flush()}_onPacket(e){if(this.readyState==="opening"||this.readyState==="open"||this.readyState==="closing")switch(this.emitReserved("packet",e),this.emitReserved("heartbeat"),e.type){case"open":this.onHandshake(JSON.parse(e.data));break;case"ping":this._sendPacket("pong"),this.emitReserved("ping"),this.emitReserved("pong"),this._resetPingTimeout();break;case"error":const t=new Error("server error");t.code=e.data,this._onError(t);break;case"message":this.emitReserved("data",e.data),this.emitReserved("message",e.data);break}}onHandshake(e){this.emitReserved("handshake",e),this.id=e.sid,this.transport.query.sid=e.sid,this._pingInterval=e.pingInterval,this._pingTimeout=e.pingTimeout,this._maxPayload=e.maxPayload,this.onOpen(),this.readyState!=="closed"&&this._resetPingTimeout()}_resetPingTimeout(){this.clearTimeoutFn(this._pingTimeoutTimer);const e=this._pingInterval+this._pingTimeout;this._pingTimeoutTime=Date.now()+e,this._pingTimeoutTimer=this.setTimeoutFn(()=>{this._onClose("ping timeout")},e),this.opts.autoUnref&&this._pingTimeoutTimer.unref()}_onDrain(){this.writeBuffer.splice(0,this._prevBufferLen),this._prevBufferLen=0,this.writeBuffer.length===0?this.emitReserved("drain"):this.flush()}flush(){if(this.readyState!=="closed"&&this.transport.writable&&!this.upgrading&&this.writeBuffer.length){const e=this._getWritablePackets();this.transport.send(e),this._prevBufferLen=e.length,this.emitReserved("flush")}}_getWritablePackets(){if(!(this._maxPayload&&this.transport.name==="polling"&&this.writeBuffer.length>1))return this.writeBuffer;let t=1;for(let n=0;n<this.writeBuffer.length;n++){const s=this.writeBuffer[n].data;if(s&&(t+=rr(s)),n>0&&t>this._maxPayload)return this.writeBuffer.slice(0,n);t+=2}return this.writeBuffer}_hasPingExpired(){if(!this._pingTimeoutTime)return!0;const e=Date.now()>this._pingTimeoutTime;return e&&(this._pingTimeoutTime=0,ae(()=>{this._onClose("ping timeout")},this.setTimeoutFn)),e}write(e,t,n){return this._sendPacket("message",e,t,n),this}send(e,t,n){return this._sendPacket("message",e,t,n),this}_sendPacket(e,t,n,s){if(typeof t=="function"&&(s=t,t=void 0),typeof n=="function"&&(s=n,n=null),this.readyState==="closing"||this.readyState==="closed")return;n=n||{},n.compress=n.compress!==!1;const i={type:e,data:t,options:n};this.emitReserved("packetCreate",i),this.writeBuffer.push(i),s&&this.once("flush",s),this.flush()}close(){const e=()=>{this._onClose("forced close"),this.transport.close()},t=()=>{this.off("upgrade",t),this.off("upgradeError",t),e()},n=()=>{this.once("upgrade",t),this.once("upgradeError",t)};return(this.readyState==="opening"||this.readyState==="open")&&(this.readyState="closing",this.writeBuffer.length?this.once("drain",()=>{this.upgrading?n():e()}):this.upgrading?n():e()),this}_onError(e){if(I.priorWebsocketSuccess=!1,this.opts.tryAllTransports&&this.transports.length>1&&this.readyState==="opening")return this.transports.shift(),this._open();this.emitReserved("error",e),this._onClose("transport error",e)}_onClose(e,t){if(this.readyState==="opening"||this.readyState==="open"||this.readyState==="closing"){if(this.clearTimeoutFn(this._pingTimeoutTimer),this.transport.removeAllListeners("close"),this.transport.close(),this.transport.removeAllListeners(),Be&&(this._beforeunloadEventListener&&removeEventListener("beforeunload",this._beforeunloadEventListener,!1),this._offlineEventListener)){const n=he.indexOf(this._offlineEventListener);n!==-1&&he.splice(n,1)}this.readyState="closed",this.id=null,this.emitReserved("close",e,t),this.writeBuffer=[],this._prevBufferLen=0}}}I.protocol=en;class Cr extends I{constructor(){super(...arguments),this._upgrades=[]}onOpen(){if(super.onOpen(),this.readyState==="open"&&this.opts.upgrade)for(let e=0;e<this._upgrades.length;e++)this._probe(this._upgrades[e])}_probe(e){let t=this.createTransport(e),n=!1;I.priorWebsocketSuccess=!1;const s=()=>{n||(t.send([{type:"ping",data:"probe"}]),t.once("packet",d=>{if(!n)if(d.type==="pong"&&d.data==="probe"){if(this.upgrading=!0,this.emitReserved("upgrading",t),!t)return;I.priorWebsocketSuccess=t.name==="websocket",this.transport.pause(()=>{n||this.readyState!=="closed"&&(l(),this.setTransport(t),t.send([{type:"upgrade"}]),this.emitReserved("upgrade",t),t=null,this.upgrading=!1,this.flush())})}else{const u=new Error("probe error");u.transport=t.name,this.emitReserved("upgradeError",u)}}))};function i(){n||(n=!0,l(),t.close(),t=null)}const o=d=>{const u=new Error("probe error: "+d);u.transport=t.name,i(),this.emitReserved("upgradeError",u)};function a(){o("transport closed")}function c(){o("socket closed")}function h(d){t&&d.name!==t.name&&i()}const l=()=>{t.removeListener("open",s),t.removeListener("error",o),t.removeListener("close",a),this.off("close",c),this.off("upgrading",h)};t.once("open",s),t.once("error",o),t.once("close",a),this.once("close",c),this.once("upgrading",h),this._upgrades.indexOf("webtransport")!==-1&&e!=="webtransport"?this.setTimeoutFn(()=>{n||t.open()},200):t.open()}onHandshake(e){this._upgrades=this._filterUpgrades(e.upgrades),super.onHandshake(e)}_filterUpgrades(e){const t=[];for(let n=0;n<e.length;n++)~this.transports.indexOf(e[n])&&t.push(e[n]);return t}}let kr=class extends Cr{constructor(e,t={}){const n=typeof e=="object",s=n?{...e}:{...t};(!s.transports||s.transports&&typeof s.transports[0]=="string")&&(s.transports=(s.transports||["polling","websocket","webtransport"]).map(i=>yr[i]).filter(i=>!!i)),super(n?s:e,s)}};function xr(r,e="",t){let n=r;t=t||typeof location<"u"&&location,r==null&&(r=t.protocol+"//"+t.host),typeof r=="string"&&(r.charAt(0)==="/"&&(r.charAt(1)==="/"?r=t.protocol+r:r=t.host+r),/^(https?|wss?):\/\//.test(r)||(typeof t<"u"?r=t.protocol+"//"+r:r="https://"+r),n=$e(r)),n.port||(/^(http|ws)$/.test(n.protocol)?n.port="80":/^(http|ws)s$/.test(n.protocol)&&(n.port="443")),n.path=n.path||"/";const i=n.host.indexOf(":")!==-1?"["+n.host+"]":n.host;return n.id=n.protocol+"://"+i+":"+n.port+e,n.href=n.protocol+"://"+i+(t&&t.port===n.port?"":":"+n.port),n}const Tr=typeof ArrayBuffer=="function",Rr=r=>typeof ArrayBuffer.isView=="function"?ArrayBuffer.isView(r):r.buffer instanceof ArrayBuffer,cn=Object.prototype.toString,Pr=typeof Blob=="function"||typeof Blob<"u"&&cn.call(Blob)==="[object BlobConstructor]",Er=typeof File=="function"||typeof File<"u"&&cn.call(File)==="[object FileConstructor]";function Ue(r){return Tr&&(r instanceof ArrayBuffer||Rr(r))||Pr&&r instanceof Blob||Er&&r instanceof File}function le(r,e){if(!r||typeof r!="object")return!1;if(Array.isArray(r)){for(let t=0,n=r.length;t<n;t++)if(le(r[t]))return!0;return!1}if(Ue(r))return!0;if(r.toJSON&&typeof r.toJSON=="function"&&arguments.length===1)return le(r.toJSON(),!0);for(const t in r)if(Object.prototype.hasOwnProperty.call(r,t)&&le(r[t]))return!0;return!1}function wr(r){const e=[],t=r.data,n=r;return n.data=Ne(t,e),n.attachments=e.length,{packet:n,buffers:e}}function Ne(r,e){if(!r)return r;if(Ue(r)){const t={_placeholder:!0,num:e.length};return e.push(r),t}else if(Array.isArray(r)){const t=new Array(r.length);for(let n=0;n<r.length;n++)t[n]=Ne(r[n],e);return t}else if(typeof r=="object"&&!(r instanceof Date)){const t={};for(const n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=Ne(r[n],e));return t}return r}function Mr(r,e){return r.data=ze(r.data,e),delete r.attachments,r}function ze(r,e){if(!r)return r;if(r&&r._placeholder===!0){if(typeof r.num=="number"&&r.num>=0&&r.num<e.length)return e[r.num];throw new Error("illegal attachments")}else if(Array.isArray(r))for(let t=0;t<r.length;t++)r[t]=ze(r[t],e);else if(typeof r=="object")for(const t in r)Object.prototype.hasOwnProperty.call(r,t)&&(r[t]=ze(r[t],e));return r}const Ar=["connect","connect_error","disconnect","disconnecting","newListener","removeListener"];var g;(function(r){r[r.CONNECT=0]="CONNECT",r[r.DISCONNECT=1]="DISCONNECT",r[r.EVENT=2]="EVENT",r[r.ACK=3]="ACK",r[r.CONNECT_ERROR=4]="CONNECT_ERROR",r[r.BINARY_EVENT=5]="BINARY_EVENT",r[r.BINARY_ACK=6]="BINARY_ACK"})(g||(g={}));class Ir{constructor(e){this.replacer=e}encode(e){return(e.type===g.EVENT||e.type===g.ACK)&&le(e)?this.encodeAsBinary({type:e.type===g.EVENT?g.BINARY_EVENT:g.BINARY_ACK,nsp:e.nsp,data:e.data,id:e.id}):[this.encodeAsString(e)]}encodeAsString(e){let t=""+e.type;return(e.type===g.BINARY_EVENT||e.type===g.BINARY_ACK)&&(t+=e.attachments+"-"),e.nsp&&e.nsp!=="/"&&(t+=e.nsp+","),e.id!=null&&(t+=e.id),e.data!=null&&(t+=JSON.stringify(e.data,this.replacer)),t}encodeAsBinary(e){const t=wr(e),n=this.encodeAsString(t.packet),s=t.buffers;return s.unshift(n),s}}class Fe extends b{constructor(e){super(),this.opts=Object.assign({reviver:void 0,maxAttachments:10},typeof e=="function"?{reviver:e}:e)}add(e){let t;if(typeof e=="string"){if(this.reconstructor)throw new Error("got plaintext data when reconstructing a packet");t=this.decodeString(e);const n=t.type===g.BINARY_EVENT;n||t.type===g.BINARY_ACK?(t.type=n?g.EVENT:g.ACK,this.reconstructor=new Or(t),t.attachments===0&&super.emitReserved("decoded",t)):super.emitReserved("decoded",t)}else if(Ue(e)||e.base64)if(this.reconstructor)t=this.reconstructor.takeBinaryData(e),t&&(this.reconstructor=null,super.emitReserved("decoded",t));else throw new Error("got binary data when not reconstructing a packet");else throw new Error("Unknown type: "+e)}decodeString(e){let t=0;const n={type:Number(e.charAt(0))};if(g[n.type]===void 0)throw new Error("unknown packet type "+n.type);if(n.type===g.BINARY_EVENT||n.type===g.BINARY_ACK){const i=t+1;for(;e.charAt(++t)!=="-"&&t!=e.length;);const o=e.substring(i,t);if(o!=Number(o)||e.charAt(t)!=="-")throw new Error("Illegal attachments");const a=Number(o);if(!Dr(a)||a<0)throw new Error("Illegal attachments");if(a>this.opts.maxAttachments)throw new Error("too many attachments");n.attachments=a}if(e.charAt(t+1)==="/"){const i=t+1;for(;++t&&!(e.charAt(t)===","||t===e.length););n.nsp=e.substring(i,t)}else n.nsp="/";const s=e.charAt(t+1);if(s!==""&&Number(s)==s){const i=t+1;for(;++t;){const o=e.charAt(t);if(o==null||Number(o)!=o){--t;break}if(t===e.length)break}n.id=Number(e.substring(i,t+1))}if(e.charAt(++t)){const i=this.tryParse(e.substr(t));if(Fe.isPayloadValid(n.type,i))n.data=i;else throw new Error("invalid payload")}return n}tryParse(e){try{return JSON.parse(e,this.opts.reviver)}catch{return!1}}static isPayloadValid(e,t){switch(e){case g.CONNECT:return hn(t);case g.DISCONNECT:return t===void 0;case g.CONNECT_ERROR:return typeof t=="string"||hn(t);case g.EVENT:case g.BINARY_EVENT:return Array.isArray(t)&&(typeof t[0]=="number"||typeof t[0]=="string"&&Ar.indexOf(t[0])===-1);case g.ACK:case g.BINARY_ACK:return Array.isArray(t)}}destroy(){this.reconstructor&&(this.reconstructor.finishedReconstruction(),this.reconstructor=null)}}class Or{constructor(e){this.packet=e,this.buffers=[],this.reconPack=e}takeBinaryData(e){if(this.buffers.push(e),this.buffers.length===this.reconPack.attachments){const t=Mr(this.reconPack,this.buffers);return this.finishedReconstruction(),t}return null}finishedReconstruction(){this.reconPack=null,this.buffers=[]}}const Dr=Number.isInteger||function(r){return typeof r=="number"&&isFinite(r)&&Math.floor(r)===r};function hn(r){return Object.prototype.toString.call(r)==="[object Object]"}const Lr=Object.freeze(Object.defineProperty({__proto__:null,Decoder:Fe,Encoder:Ir,get PacketType(){return g}},Symbol.toStringTag,{value:"Module"}));function R(r,e,t){return r.on(e,t),function(){r.off(e,t)}}const $r=Object.freeze({connect:1,connect_error:1,disconnect:1,disconnecting:1,newListener:1,removeListener:1});class ln extends b{constructor(e,t,n){super(),this.connected=!1,this.recovered=!1,this.receiveBuffer=[],this.sendBuffer=[],this._queue=[],this._queueSeq=0,this.ids=0,this.acks={},this.flags={},this.io=e,this.nsp=t,n&&n.auth&&(this.auth=n.auth),this._opts=Object.assign({},n),this.io._autoConnect&&this.open()}get disconnected(){return!this.connected}subEvents(){if(this.subs)return;const e=this.io;this.subs=[R(e,"open",this.onopen.bind(this)),R(e,"packet",this.onpacket.bind(this)),R(e,"error",this.onerror.bind(this)),R(e,"close",this.onclose.bind(this))]}get active(){return!!this.subs}connect(){return this.connected?this:(this.subEvents(),this.io._reconnecting||this.io.open(),this.io._readyState==="open"&&this.onopen(),this)}open(){return this.connect()}send(...e){return e.unshift("message"),this.emit.apply(this,e),this}emit(e,...t){var n,s,i;if($r.hasOwnProperty(e))throw new Error('"'+e.toString()+'" is a reserved event name');if(t.unshift(e),this._opts.retries&&!this.flags.fromQueue&&!this.flags.volatile)return this._addToQueue(t),this;const o={type:g.EVENT,data:t};if(o.options={},o.options.compress=this.flags.compress!==!1,typeof t[t.length-1]=="function"){const l=this.ids++,d=t.pop();this._registerAckCallback(l,d),o.id=l}const a=(s=(n=this.io.engine)===null||n===void 0?void 0:n.transport)===null||s===void 0?void 0:s.writable,c=this.connected&&!(!((i=this.io.engine)===null||i===void 0)&&i._hasPingExpired());return this.flags.volatile&&!a||(c?(this.notifyOutgoingListeners(o),this.packet(o)):this.sendBuffer.push(o)),this.flags={},this}_registerAckCallback(e,t){var n;const s=(n=this.flags.timeout)!==null&&n!==void 0?n:this._opts.ackTimeout;if(s===void 0){this.acks[e]=t;return}const i=this.io.setTimeoutFn(()=>{delete this.acks[e];for(let a=0;a<this.sendBuffer.length;a++)this.sendBuffer[a].id===e&&this.sendBuffer.splice(a,1);t.call(this,new Error("operation has timed out"))},s),o=(...a)=>{this.io.clearTimeoutFn(i),t.apply(this,a)};o.withError=!0,this.acks[e]=o}emitWithAck(e,...t){return new Promise((n,s)=>{const i=(o,a)=>o?s(o):n(a);i.withError=!0,t.push(i),this.emit(e,...t)})}_addToQueue(e){let t;typeof e[e.length-1]=="function"&&(t=e.pop());const n={id:this._queueSeq++,tryCount:0,pending:!1,args:e,flags:Object.assign({fromQueue:!0},this.flags)};e.push((s,...i)=>(this._queue[0],s!==null?n.tryCount>this._opts.retries&&(this._queue.shift(),t&&t(s)):(this._queue.shift(),t&&t(null,...i)),n.pending=!1,this._drainQueue())),this._queue.push(n),this._drainQueue()}_drainQueue(e=!1){if(!this.connected||this._queue.length===0)return;const t=this._queue[0];t.pending&&!e||(t.pending=!0,t.tryCount++,this.flags=t.flags,this.emit.apply(this,t.args))}packet(e){e.nsp=this.nsp,this.io._packet(e)}onopen(){typeof this.auth=="function"?this.auth(e=>{this._sendConnectPacket(e)}):this._sendConnectPacket(this.auth)}_sendConnectPacket(e){this.packet({type:g.CONNECT,data:this._pid?Object.assign({pid:this._pid,offset:this._lastOffset},e):e})}onerror(e){this.connected||this.emitReserved("connect_error",e)}onclose(e,t){this.connected=!1,delete this.id,this.emitReserved("disconnect",e,t),this._clearAcks()}_clearAcks(){Object.keys(this.acks).forEach(e=>{if(!this.sendBuffer.some(n=>String(n.id)===e)){const n=this.acks[e];delete this.acks[e],n.withError&&n.call(this,new Error("socket has been disconnected"))}})}onpacket(e){if(e.nsp===this.nsp)switch(e.type){case g.CONNECT:e.data&&e.data.sid?this.onconnect(e.data.sid,e.data.pid):this.emitReserved("connect_error",new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)"));break;case g.EVENT:case g.BINARY_EVENT:this.onevent(e);break;case g.ACK:case g.BINARY_ACK:this.onack(e);break;case g.DISCONNECT:this.ondisconnect();break;case g.CONNECT_ERROR:this.destroy();const n=new Error(e.data.message);n.data=e.data.data,this.emitReserved("connect_error",n);break}}onevent(e){const t=e.data||[];e.id!=null&&t.push(this.ack(e.id)),this.connected?this.emitEvent(t):this.receiveBuffer.push(Object.freeze(t))}emitEvent(e){if(this._anyListeners&&this._anyListeners.length){const t=this._anyListeners.slice();for(const n of t)n.apply(this,e)}super.emit.apply(this,e),this._pid&&e.length&&typeof e[e.length-1]=="string"&&(this._lastOffset=e[e.length-1])}ack(e){const t=this;let n=!1;return function(...s){n||(n=!0,t.packet({type:g.ACK,id:e,data:s}))}}onack(e){const t=this.acks[e.id];typeof t=="function"&&(delete this.acks[e.id],t.withError&&e.data.unshift(null),t.apply(this,e.data))}onconnect(e,t){this.id=e,this.recovered=t&&this._pid===t,this._pid=t,this.connected=!0,this.emitBuffered(),this._drainQueue(!0),this.emitReserved("connect")}emitBuffered(){this.receiveBuffer.forEach(e=>this.emitEvent(e)),this.receiveBuffer=[],this.sendBuffer.forEach(e=>{this.notifyOutgoingListeners(e),this.packet(e)}),this.sendBuffer=[]}ondisconnect(){this.destroy(),this.onclose("io server disconnect")}destroy(){this.subs&&(this.subs.forEach(e=>e()),this.subs=void 0),this.io._destroy(this)}disconnect(){return this.connected&&this.packet({type:g.DISCONNECT}),this.destroy(),this.connected&&this.onclose("io client disconnect"),this}close(){return this.disconnect()}compress(e){return this.flags.compress=e,this}get volatile(){return this.flags.volatile=!0,this}timeout(e){return this.flags.timeout=e,this}onAny(e){return this._anyListeners=this._anyListeners||[],this._anyListeners.push(e),this}prependAny(e){return this._anyListeners=this._anyListeners||[],this._anyListeners.unshift(e),this}offAny(e){if(!this._anyListeners)return this;if(e){const t=this._anyListeners;for(let n=0;n<t.length;n++)if(e===t[n])return t.splice(n,1),this}else this._anyListeners=[];return this}listenersAny(){return this._anyListeners||[]}onAnyOutgoing(e){return this._anyOutgoingListeners=this._anyOutgoingListeners||[],this._anyOutgoingListeners.push(e),this}prependAnyOutgoing(e){return this._anyOutgoingListeners=this._anyOutgoingListeners||[],this._anyOutgoingListeners.unshift(e),this}offAnyOutgoing(e){if(!this._anyOutgoingListeners)return this;if(e){const t=this._anyOutgoingListeners;for(let n=0;n<t.length;n++)if(e===t[n])return t.splice(n,1),this}else this._anyOutgoingListeners=[];return this}listenersAnyOutgoing(){return this._anyOutgoingListeners||[]}notifyOutgoingListeners(e){if(this._anyOutgoingListeners&&this._anyOutgoingListeners.length){const t=this._anyOutgoingListeners.slice();for(const n of t)n.apply(this,e.data)}}}function N(r){r=r||{},this.ms=r.min||100,this.max=r.max||1e4,this.factor=r.factor||2,this.jitter=r.jitter>0&&r.jitter<=1?r.jitter:0,this.attempts=0}N.prototype.duration=function(){var r=this.ms*Math.pow(this.factor,this.attempts++);if(this.jitter){var e=Math.random(),t=Math.floor(e*this.jitter*r);r=Math.floor(e*10)&1?r+t:r-t}return Math.min(r,this.max)|0},N.prototype.reset=function(){this.attempts=0},N.prototype.setMin=function(r){this.ms=r},N.prototype.setMax=function(r){this.max=r},N.prototype.setJitter=function(r){this.jitter=r};class je extends b{constructor(e,t){var n;super(),this.nsps={},this.subs=[],e&&typeof e=="object"&&(t=e,e=void 0),t=t||{},t.path=t.path||"/socket.io",this.opts=t,ce(this,t),this.reconnection(t.reconnection!==!1),this.reconnectionAttempts(t.reconnectionAttempts||1/0),this.reconnectionDelay(t.reconnectionDelay||1e3),this.reconnectionDelayMax(t.reconnectionDelayMax||5e3),this.randomizationFactor((n=t.randomizationFactor)!==null&&n!==void 0?n:.5),this.backoff=new N({min:this.reconnectionDelay(),max:this.reconnectionDelayMax(),jitter:this.randomizationFactor()}),this.timeout(t.timeout==null?2e4:t.timeout),this._readyState="closed",this.uri=e;const s=t.parser||Lr;this.encoder=new s.Encoder,this.decoder=new s.Decoder,this._autoConnect=t.autoConnect!==!1,this._autoConnect&&this.open()}reconnection(e){return arguments.length?(this._reconnection=!!e,e||(this.skipReconnect=!0),this):this._reconnection}reconnectionAttempts(e){return e===void 0?this._reconnectionAttempts:(this._reconnectionAttempts=e,this)}reconnectionDelay(e){var t;return e===void 0?this._reconnectionDelay:(this._reconnectionDelay=e,(t=this.backoff)===null||t===void 0||t.setMin(e),this)}randomizationFactor(e){var t;return e===void 0?this._randomizationFactor:(this._randomizationFactor=e,(t=this.backoff)===null||t===void 0||t.setJitter(e),this)}reconnectionDelayMax(e){var t;return e===void 0?this._reconnectionDelayMax:(this._reconnectionDelayMax=e,(t=this.backoff)===null||t===void 0||t.setMax(e),this)}timeout(e){return arguments.length?(this._timeout=e,this):this._timeout}maybeReconnectOnOpen(){!this._reconnecting&&this._reconnection&&this.backoff.attempts===0&&this.reconnect()}open(e){if(~this._readyState.indexOf("open"))return this;this.engine=new kr(this.uri,this.opts);const t=this.engine,n=this;this._readyState="opening",this.skipReconnect=!1;const s=R(t,"open",function(){n.onopen(),e&&e()}),i=a=>{this.cleanup(),this._readyState="closed",this.emitReserved("error",a),e?e(a):this.maybeReconnectOnOpen()},o=R(t,"error",i);if(this._timeout!==!1){const a=this._timeout,c=this.setTimeoutFn(()=>{s(),i(new Error("timeout")),t.close()},a);this.opts.autoUnref&&c.unref(),this.subs.push(()=>{this.clearTimeoutFn(c)})}return this.subs.push(s),this.subs.push(o),this}connect(e){return this.open(e)}onopen(){this.cleanup(),this._readyState="open",this.emitReserved("open");const e=this.engine;this.subs.push(R(e,"ping",this.onping.bind(this)),R(e,"data",this.ondata.bind(this)),R(e,"error",this.onerror.bind(this)),R(e,"close",this.onclose.bind(this)),R(this.decoder,"decoded",this.ondecoded.bind(this)))}onping(){this.emitReserved("ping")}ondata(e){try{this.decoder.add(e)}catch(t){this.onclose("parse error",t)}}ondecoded(e){ae(()=>{this.emitReserved("packet",e)},this.setTimeoutFn)}onerror(e){this.emitReserved("error",e)}socket(e,t){let n=this.nsps[e];return n?this._autoConnect&&!n.active&&n.connect():(n=new ln(this,e,t),this.nsps[e]=n),n}_destroy(e){const t=Object.keys(this.nsps);for(const n of t)if(this.nsps[n].active)return;this._close()}_packet(e){const t=this.encoder.encode(e);for(let n=0;n<t.length;n++)this.engine.write(t[n],e.options)}cleanup(){this.subs.forEach(e=>e()),this.subs.length=0,this.decoder.destroy()}_close(){this.skipReconnect=!0,this._reconnecting=!1,this.onclose("forced close")}disconnect(){return this._close()}onclose(e,t){var n;this.cleanup(),(n=this.engine)===null||n===void 0||n.close(),this.backoff.reset(),this._readyState="closed",this.emitReserved("close",e,t),this._reconnection&&!this.skipReconnect&&this.reconnect()}reconnect(){if(this._reconnecting||this.skipReconnect)return this;const e=this;if(this.backoff.attempts>=this._reconnectionAttempts)this.backoff.reset(),this.emitReserved("reconnect_failed"),this._reconnecting=!1;else{const t=this.backoff.duration();this._reconnecting=!0;const n=this.setTimeoutFn(()=>{e.skipReconnect||(this.emitReserved("reconnect_attempt",e.backoff.attempts),!e.skipReconnect&&e.open(s=>{s?(e._reconnecting=!1,e.reconnect(),this.emitReserved("reconnect_error",s)):e.onreconnect()}))},t);this.opts.autoUnref&&n.unref(),this.subs.push(()=>{this.clearTimeoutFn(n)})}}onreconnect(){const e=this.backoff.attempts;this._reconnecting=!1,this.backoff.reset(),this.emitReserved("reconnect",e)}}const H={};function de(r,e){typeof r=="object"&&(e=r,r=void 0),e=e||{};const t=xr(r,e.path||"/socket.io"),n=t.source,s=t.id,i=t.path,o=H[s]&&i in H[s].nsps,a=e.forceNew||e["force new connection"]||e.multiplex===!1||o;let c;return a?c=new je(n,e):(H[s]||(H[s]=new je(n,e)),c=H[s]),t.query&&!e.query&&(e.query=t.queryKey),c.socket(t.path,e)}Object.assign(de,{Manager:je,Socket:ln,io:de,connect:de});class Br{constructor(e){this.config=e,this.socket=null,this.callbacks={onMessage:null,onExistingPeers:null,onYouJoined:null,onUserJoined:null,onUserLeft:null,onPeerMediaStateChange:null,onError:null},this.rawListeners=new Map}connect(e){let t=this.config.socketUrl,n="/socket.io/";try{const i=new URL(this.config.socketUrl);t=`${i.protocol}//${i.host}`;const o=i.pathname.replace(/\/+$/,"");o&&(n=`${o}/socket.io/`)}catch{}this.socket=de(t,{path:n,auth:{workspace:e},transports:["websocket","polling"]}),this.socket.on("connect",()=>{console.log("Socket connected")}),this.socket.on("disconnect",()=>{console.log("Socket disconnected")}),this.socket.on("new-message",i=>{this.callbacks.onMessage&&this.callbacks.onMessage(i)}),this.socket.on("existing-peers",i=>{this.callbacks.onExistingPeers&&this.callbacks.onExistingPeers(i)}),this.socket.on("you-joined",i=>{this.callbacks.onYouJoined&&this.callbacks.onYouJoined(i)}),this.socket.on("user-joined",i=>{this.callbacks.onUserJoined&&this.callbacks.onUserJoined(i)}),this.socket.on("user-left",i=>{this.callbacks.onUserLeft&&this.callbacks.onUserLeft(i)}),this.socket.on("peer-media-state-change",i=>{this.callbacks.onPeerMediaStateChange&&this.callbacks.onPeerMediaStateChange(i)});const s=["code-changed","code-language-changed","code-executing","code-execution-result","code-state-sync","code-permission-changed","code-sandbox-toggled","whiteboard-drawing","whiteboard-cleared","whiteboard-image-uploaded","whiteboard-state-sync","whiteboard-permission-changed","whiteboard-toggled"];for(const i of s)this.socket.on(i,o=>{const a=this.rawListeners&&this.rawListeners.get(i);a&&a.forEach(c=>{try{c(o)}catch(h){console.warn(h)}})});this.socket.on("error",i=>{console.error("Socket error:",i),this.callbacks.onError&&this.callbacks.onError(i)})}joinRoom(e,t,n,s){this.socket&&this.socket.emit("join-room",{roomId:e,userId:t,userName:n,peerId:s})}emitMediaState(e,t,n,s){this.socket&&this.socket.emit("media-state-change",{roomId:e,userId:t,type:n,enabled:s})}emit(e,t){this.socket&&this.socket.emit(e,t)}onRaw(e,t){return this.rawListeners.has(e)||this.rawListeners.set(e,new Set),this.rawListeners.get(e).add(t),()=>{var n;return(n=this.rawListeners.get(e))==null?void 0:n.delete(t)}}sendMessage(e,t,n,s,i){if(this.socket){const o={roomId:e,userId:t,userName:n,message:s||""};Array.isArray(i)&&i.length&&(o.attachmentIds=i),this.socket.emit("send-message",o)}}leaveRoom(){this.socket&&this.socket.emit("leave-room")}on(e,t){this.callbacks.hasOwnProperty("on"+e.charAt(0).toUpperCase()+e.slice(1))&&(this.callbacks["on"+e.charAt(0).toUpperCase()+e.slice(1)]=t)}disconnect(){this.socket&&(this.socket.close(),this.socket=null)}}class Ur{constructor(){this.localStream=null,this.remoteStreams={}}async getLocalStream(){if(this.localStream)return this.localStream;try{return this.localStream=await navigator.mediaDevices.getUserMedia({video:!0,audio:!0}),this.localStream}catch(e){throw console.error("Failed to get media devices:",e),e}}addRemoteStream(e,t){this.remoteStreams[e]=t}removeRemoteStream(e){delete this.remoteStreams[e]}toggleVideo(){if(this.localStream){const e=this.localStream.getVideoTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}toggleAudio(){if(this.localStream){const e=this.localStream.getAudioTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}stopLocalStream(){this.localStream&&(this.localStream.getTracks().forEach(e=>e.stop()),this.localStream=null)}cleanup(){this.stopLocalStream(),this.remoteStreams={}}}class qe{constructor(){this.recorder=null,this.stream=null,this.state="idle",this.startedAt=null,this.seq=0,this.pendingUploads=[],this.callbacks={onStateChange:null,onChunk:null}}static isSupported(){return typeof window<"u"&&typeof window.MediaRecorder<"u"}pickMimeType(){const e=["video/webm;codecs=vp9,opus","video/webm;codecs=vp8,opus","video/webm","video/mp4"];for(const t of e)if(window.MediaRecorder.isTypeSupported(t))return t;return""}setState(e){this.state=e,this.callbacks.onStateChange&&this.callbacks.onStateChange(e)}on(e,t){e==="stateChange"?this.callbacks.onStateChange=t:e==="chunk"&&(this.callbacks.onChunk=t)}start(e,t={}){if(!qe.isSupported())throw new Error("MediaRecorder API not supported in this browser");if(this.state==="recording")return;if(!e)throw new Error("stream is required to start recording");const n=t.timesliceMs||15e3,s=Number.isFinite(t.videoBitsPerSecond)?t.videoBitsPerSecond:1e6,i=Number.isFinite(t.audioBitsPerSecond)?t.audioBitsPerSecond:64e3;t.onChunk&&(this.callbacks.onChunk=t.onChunk),this.stream=e,this.seq=typeof t.startSeq=="number"?t.startSeq:0,this.pendingUploads=[];const o=this.pickMimeType(),a={videoBitsPerSecond:s,audioBitsPerSecond:i};o&&(a.mimeType=o),this.recorder=new window.MediaRecorder(e,a),this.mimeType=this.recorder.mimeType||o||"video/webm",this.recorder.ondataavailable=c=>{if(!c.data||c.data.size===0)return;const h=this.seq++,l=c.data,d=this.callbacks.onChunk;if(!d)return;const u=Promise.resolve().then(()=>d(l,h)).catch(p=>{console.warn(`Recording chunk seq=${h} upload failed:`,p&&p.message)});this.pendingUploads.push(u)},this.recorder.onerror=c=>{console.error("MediaRecorder error:",c)},this.startedAt=new Date,this.recorder.start(n),this.setState("recording")}async stop(){if(this.state!=="recording"||!this.recorder)return null;await new Promise(i=>{this.recorder.onstop=i,this.recorder.stop()});const t=(await Promise.allSettled(this.pendingUploads)).filter(i=>i.status==="rejected").length,n=new Date,s={startedAt:this.startedAt,endedAt:n,mimeType:this.mimeType,totalChunks:this.seq,droppedChunks:t};return this.recorder=null,this.stream=null,this.startedAt=null,this.pendingUploads=[],this.setState("stopped"),s}isRecording(){return this.state==="recording"||this.state==="paused"}isPaused(){return this.state==="paused"}pause(){if(!this.recorder||this.state!=="recording")return!1;try{return this.recorder.pause(),this.setState("paused"),!0}catch{return!1}}resume(){if(!this.recorder||this.state!=="paused")return!1;try{return this.recorder.resume(),this.setState("recording"),!0}catch{return!1}}}class He{constructor(e={}){this.width=e.width||1280,this.height=e.height||720,this.fps=e.fps||30,this.canvas=null,this.ctx=null,this.videos=new Map,this.audioContext=null,this.audioDestination=null,this.audioSources=new Map,this.combinedStream=null,this.animationHandle=null,this.running=!1}static isSupported(){return typeof window<"u"&&typeof HTMLCanvasElement<"u"&&typeof HTMLCanvasElement.prototype.captureStream=="function"&&typeof(window.AudioContext||window.webkitAudioContext)<"u"}async start(){if(this.running)return this.combinedStream;if(!He.isSupported())throw new Error("Composite recording requires canvas.captureStream + AudioContext");this.canvas=document.createElement("canvas"),this.canvas.width=this.width,this.canvas.height=this.height,this.canvas.style.cssText="position:absolute;left:-9999px;top:-9999px;",document.body.appendChild(this.canvas),this.ctx=this.canvas.getContext("2d");const e=window.AudioContext||window.webkitAudioContext;if(this.audioContext=new e,this.audioContext.state==="suspended")try{await this.audioContext.resume()}catch{}this.audioDestination=this.audioContext.createMediaStreamDestination();const t=this.canvas.captureStream(this.fps);this.combinedStream=new MediaStream;for(const n of t.getVideoTracks())this.combinedStream.addTrack(n);for(const n of this.audioDestination.stream.getAudioTracks())this.combinedStream.addTrack(n);return this.running=!0,this.tick(),this.combinedStream}tick(){this.running&&(this.draw(),this.animationHandle=requestAnimationFrame(()=>this.tick()))}draw(){if(!this.ctx)return;this.ctx.fillStyle="#0d0d0d",this.ctx.fillRect(0,0,this.width,this.height);const e=Array.from(this.videos.values());if(e.length===0)return;const{cols:t,rows:n}=jr(e.length),s=this.width/t,i=this.height/n;e.forEach((o,a)=>{const c=a%t,h=Math.floor(a/t),l=c*s,d=h*i;this.ctx.fillStyle="#1a1a1a",this.ctx.fillRect(l,d,s,i);const u=o.videoEl.videoWidth||0,p=o.videoEl.videoHeight||0,m=o.videoTrack,_=m&&m.enabled&&m.readyState==="live";if(u>0&&p>0&&_!==!1){const mn=Math.min(s/u,i/p),yn=u*mn,bn=p*mn,ts=l+(s-yn)/2,ns=d+(i-bn)/2;try{this.ctx.drawImage(o.videoEl,ts,ns,yn,bn)}catch{}}else Fr(this.ctx,o.label||"Peer",l,d,s,i);const un=o.label||"Peer";this.ctx.font="bold 16px system-ui, sans-serif";const Xr=this.ctx.measureText(un),fn=10,Zr=6,es=Xr.width+fn*2,Ye=24,pn=l+12,gn=d+i-Ye-12;this.ctx.fillStyle="rgba(0,0,0,0.65)",this.ctx.fillRect(pn,gn,es,Ye),this.ctx.fillStyle="#fff",this.ctx.fillText(un,pn+fn,gn+Ye-Zr-2)})}addStream(e,t,n={}){if(!this.running)return;this.videos.has(e)&&this.removeStream(e);const s=document.createElement("video");s.srcObject=t,s.autoplay=!0,s.muted=!0,s.playsInline=!0,s.style.cssText="position:absolute;left:-9999px;top:-9999px;",document.body.appendChild(s),s.play().catch(()=>{});const i=t.getVideoTracks()[0]||null;this.videos.set(e,{videoEl:s,videoTrack:i,label:n.label||(n.isLocal?"You":e)});const o=t.getAudioTracks();if(o.length&&this.audioContext)try{const a=new MediaStream([o[0]]),c=this.audioContext.createMediaStreamSource(a),h=this.audioContext.createGain();h.gain.value=1,c.connect(h).connect(this.audioDestination),this.audioSources.set(e,{source:c,gain:h})}catch(a){console.warn("Compositor audio source failed for",e,a&&a.message)}}removeStream(e){const t=this.videos.get(e);t&&(t.videoEl.pause(),t.videoEl.srcObject=null,t.videoEl.remove(),this.videos.delete(e));const n=this.audioSources.get(e);if(n){try{n.source.disconnect()}catch{}try{n.gain.disconnect()}catch{}this.audioSources.delete(e)}}getStream(){return this.combinedStream}stop(){if(this.running){this.running=!1,this.animationHandle&&cancelAnimationFrame(this.animationHandle),this.animationHandle=null;for(const e of Array.from(this.videos.keys()))this.removeStream(e);this.combinedStream&&(this.combinedStream.getTracks().forEach(e=>{try{e.stop()}catch{}}),this.combinedStream=null),this.audioContext&&(this.audioContext.close().catch(()=>{}),this.audioContext=null),this.canvas&&(this.canvas.remove(),this.canvas=null,this.ctx=null)}}}function Nr(r){const e=String(r).trim().split(/\s+/).filter(Boolean);return e.length===0?"?":e.length===1?e[0].slice(0,2).toUpperCase():(e[0][0]+e[e.length-1][0]).toUpperCase()}function zr(r){let e=0;const t=String(r);for(let s=0;s<t.length;s++)e=e*31+t.charCodeAt(s)>>>0;return`hsl(${e%360}, 45%, 40%)`}function Fr(r,e,t,n,s,i){const o=t+s/2,a=n+i/2,c=Math.min(s,i)*.22;r.fillStyle=zr(e),r.beginPath(),r.arc(o,a,c,0,Math.PI*2),r.fill(),r.fillStyle="#fff",r.font=`600 ${Math.floor(c)}px system-ui, sans-serif`,r.textAlign="center",r.textBaseline="middle",r.fillText(Nr(e),o,a+1),r.textAlign="start",r.textBaseline="alphabetic"}function jr(r){if(r<=1)return{cols:1,rows:1};if(r===2)return{cols:2,rows:1};if(r<=4)return{cols:2,rows:2};if(r<=6)return{cols:3,rows:2};if(r<=9)return{cols:3,rows:3};const e=4;return{cols:e,rows:Math.ceil(r/e)}}function qr(r,e){return r.includes("pdf")||/\.pdf$/i.test(e)?"📕":r.includes("word")||/\.docx?$/i.test(e)?"📘":r.includes("sheet")||r.includes("excel")||/\.(xlsx?|csv)$/i.test(e)?"📗":r.startsWith("text/")?"📄":"📎"}class Hr{constructor(e={}){this.container=null,this.videoElements={},this.chatMessages=[],this.recording=e.recording||"off",this.screenShare=e.screenShare!==!1,this.enabledPanels=new Set(Array.isArray(e.panels)&&e.panels.length?e.panels:["chat"]),this.enabledPanels.add("chat"),this.activePanel="chat",this.panelSlots={}}mount(e){if(typeof e=="string"?this.container=document.querySelector(e):e instanceof Element&&(this.container=e),!this.container)throw new Error(`Container not found: ${e}`);this.render()}render(){const e=this.recording==="manual"?'<button id="toggle-record" style="padding: 10px 20px; background-color: #7a3a3a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">● Record</button>':"",t=this.recording!=="off"?'<button id="toggle-pause" style="display:none; padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">⏸ Pause</button>':"",n=this.screenShare?'<button id="toggle-screenshare" style="padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">Share Screen</button>':"",s=this.recording!=="off"?'<span id="recording-indicator" style="display:none; margin-left: 12px; color: #ff5252; font-size: 13px; font-weight: 600;"><span id="rec-dot" style="display:inline-block; width:8px; height:8px; background:#ff5252; border-radius:50%; margin-right:6px; animation: recPulse 1.2s ease-in-out infinite;"></span>REC <span id="rec-elapsed">00:00</span></span>':"",i=this.recording!=="off"?'<span id="queue-indicator" style="display:none; margin-left: 12px; color: #fbbf24; font-size: 12px;">⏳ <span id="queue-count">0</span> uploading</span>':"";this._panelWiredOnRender=!0,this.container.innerHTML=`
41
+ isUnifiedPlanSupported:${this.isUnifiedPlanSupported()}`}constructor(){this.isIOS=typeof navigator<"u"?["iPad","iPhone","iPod"].includes(navigator.platform):!1,this.supportedBrowsers=["firefox","chrome","safari"],this.minFirefoxVersion=59,this.minChromeVersion=72,this.minSafariVersion=605}},An=r=>!r||/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.test(r),Nt=()=>Math.random().toString(36).slice(2),zt={iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:["turn:eu-0.turn.peerjs.com:3478","turn:us-0.turn.peerjs.com:3478"],username:"peerjs",credential:"peerjsp"}],sdpSemantics:"unified-plan"};class In extends Ut{noop(){}blobToArrayBuffer(e,t){const n=new FileReader;return n.onload=function(s){s.target&&t(s.target.result)},n.readAsArrayBuffer(e),n}binaryStringToArrayBuffer(e){const t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t.buffer}isSecure(){return location.protocol==="https:"}constructor(...e){super(...e),this.CLOUD_HOST="0.peerjs.com",this.CLOUD_PORT=443,this.chunkedBrowsers={Chrome:1,chrome:1},this.defaultConfig=zt,this.browser=F.getBrowser(),this.browserVersion=F.getVersion(),this.pack=Qe,this.unpack=Ge,this.supports=function(){const t={browser:F.isBrowserSupported(),webRTC:F.isWebRTCSupported(),audioVideo:!1,data:!1,binaryBlob:!1,reliable:!1};if(!t.webRTC)return t;let n;try{n=new RTCPeerConnection(zt),t.audioVideo=!0;let s;try{s=n.createDataChannel("_PEERJSTEST",{ordered:!0}),t.data=!0,t.reliable=!!s.ordered;try{s.binaryType="blob",t.binaryBlob=!F.isIOS}catch{}}catch{}finally{s&&s.close()}}catch{}finally{n&&n.close()}return t}(),this.validateId=An,this.randomToken=Nt}}const k=new In,On="PeerJS: ";class Dn{get logLevel(){return this._logLevel}set logLevel(e){this._logLevel=e}log(...e){this._logLevel>=3&&this._print(3,...e)}warn(...e){this._logLevel>=2&&this._print(2,...e)}error(...e){this._logLevel>=1&&this._print(1,...e)}setLogFunction(e){this._print=e}_print(e,...t){const n=[On,...t];for(const s in n)n[s]instanceof Error&&(n[s]="("+n[s].name+") "+n[s].message);e>=3?console.log(...n):e>=2?console.warn("WARNING",...n):e>=1&&console.error("ERROR",...n)}constructor(){this._logLevel=0}}var f=new Dn,ke={},Ln=Object.prototype.hasOwnProperty,C="~";function j(){}Object.create&&(j.prototype=Object.create(null),new j().__proto__||(C=!1));function $n(r,e,t){this.fn=r,this.context=e,this.once=t||!1}function Ft(r,e,t,n,s){if(typeof t!="function")throw new TypeError("The listener must be a function");var i=new $n(t,n||r,s),o=C?C+e:e;return r._events[o]?r._events[o].fn?r._events[o]=[r._events[o],i]:r._events[o].push(i):(r._events[o]=i,r._eventsCount++),r}function ee(r,e){--r._eventsCount===0?r._events=new j:delete r._events[e]}function v(){this._events=new j,this._eventsCount=0}v.prototype.eventNames=function(){var e=[],t,n;if(this._eventsCount===0)return e;for(n in t=this._events)Ln.call(t,n)&&e.push(C?n.slice(1):n);return Object.getOwnPropertySymbols?e.concat(Object.getOwnPropertySymbols(t)):e},v.prototype.listeners=function(e){var t=C?C+e:e,n=this._events[t];if(!n)return[];if(n.fn)return[n.fn];for(var s=0,i=n.length,o=new Array(i);s<i;s++)o[s]=n[s].fn;return o},v.prototype.listenerCount=function(e){var t=C?C+e:e,n=this._events[t];return n?n.fn?1:n.length:0},v.prototype.emit=function(e,t,n,s,i,o){var a=C?C+e:e;if(!this._events[a])return!1;var c=this._events[a],h=arguments.length,l,d;if(c.fn){switch(c.once&&this.removeListener(e,c.fn,void 0,!0),h){case 1:return c.fn.call(c.context),!0;case 2:return c.fn.call(c.context,t),!0;case 3:return c.fn.call(c.context,t,n),!0;case 4:return c.fn.call(c.context,t,n,s),!0;case 5:return c.fn.call(c.context,t,n,s,i),!0;case 6:return c.fn.call(c.context,t,n,s,i,o),!0}for(d=1,l=new Array(h-1);d<h;d++)l[d-1]=arguments[d];c.fn.apply(c.context,l)}else{var u=c.length,p;for(d=0;d<u;d++)switch(c[d].once&&this.removeListener(e,c[d].fn,void 0,!0),h){case 1:c[d].fn.call(c[d].context);break;case 2:c[d].fn.call(c[d].context,t);break;case 3:c[d].fn.call(c[d].context,t,n);break;case 4:c[d].fn.call(c[d].context,t,n,s);break;default:if(!l)for(p=1,l=new Array(h-1);p<h;p++)l[p-1]=arguments[p];c[d].fn.apply(c[d].context,l)}}return!0},v.prototype.on=function(e,t,n){return Ft(this,e,t,n,!1)},v.prototype.once=function(e,t,n){return Ft(this,e,t,n,!0)},v.prototype.removeListener=function(e,t,n,s){var i=C?C+e:e;if(!this._events[i])return this;if(!t)return ee(this,i),this;var o=this._events[i];if(o.fn)o.fn===t&&(!s||o.once)&&(!n||o.context===n)&&ee(this,i);else{for(var a=0,c=[],h=o.length;a<h;a++)(o[a].fn!==t||s&&!o[a].once||n&&o[a].context!==n)&&c.push(o[a]);c.length?this._events[i]=c.length===1?c[0]:c:ee(this,i)}return this},v.prototype.removeAllListeners=function(e){var t;return e?(t=C?C+e:e,this._events[t]&&ee(this,t)):(this._events=new j,this._eventsCount=0),this},v.prototype.off=v.prototype.removeListener,v.prototype.addListener=v.prototype.on,v.prefixed=C,v.EventEmitter=v,ke=v;var L={};D(L,"ConnectionType",()=>M),D(L,"PeerErrorType",()=>y),D(L,"BaseConnectionErrorType",()=>xe),D(L,"DataConnectionErrorType",()=>Te),D(L,"SerializationType",()=>te),D(L,"SocketEventType",()=>A),D(L,"ServerMessageType",()=>S);var M=function(r){return r.Data="data",r.Media="media",r}({}),y=function(r){return r.BrowserIncompatible="browser-incompatible",r.Disconnected="disconnected",r.InvalidID="invalid-id",r.InvalidKey="invalid-key",r.Network="network",r.PeerUnavailable="peer-unavailable",r.SslUnavailable="ssl-unavailable",r.ServerError="server-error",r.SocketError="socket-error",r.SocketClosed="socket-closed",r.UnavailableID="unavailable-id",r.WebRTC="webrtc",r}({}),xe=function(r){return r.NegotiationFailed="negotiation-failed",r.ConnectionClosed="connection-closed",r}({}),Te=function(r){return r.NotOpenYet="not-open-yet",r.MessageToBig="message-too-big",r}({}),te=function(r){return r.Binary="binary",r.BinaryUTF8="binary-utf8",r.JSON="json",r.None="raw",r}({}),A=function(r){return r.Message="message",r.Disconnected="disconnected",r.Error="error",r.Close="close",r}({}),S=function(r){return r.Heartbeat="HEARTBEAT",r.Candidate="CANDIDATE",r.Offer="OFFER",r.Answer="ANSWER",r.Open="OPEN",r.Error="ERROR",r.IdTaken="ID-TAKEN",r.InvalidKey="INVALID-KEY",r.Leave="LEAVE",r.Expire="EXPIRE",r}({});const jt="1.5.5";class Bn extends ke.EventEmitter{constructor(e,t,n,s,i,o=5e3){super(),this.pingInterval=o,this._disconnected=!0,this._messagesQueue=[];const a=e?"wss://":"ws://";this._baseUrl=a+t+":"+n+s+"peerjs?key="+i}start(e,t){this._id=e;const n=`${this._baseUrl}&id=${e}&token=${t}`;this._socket||!this._disconnected||(this._socket=new WebSocket(n+"&version="+jt),this._disconnected=!1,this._socket.onmessage=s=>{let i;try{i=JSON.parse(s.data),f.log("Server message received:",i)}catch{f.log("Invalid server message",s.data);return}this.emit(A.Message,i)},this._socket.onclose=s=>{this._disconnected||(f.log("Socket closed.",s),this._cleanup(),this._disconnected=!0,this.emit(A.Disconnected))},this._socket.onopen=()=>{this._disconnected||(this._sendQueuedMessages(),f.log("Socket open"),this._scheduleHeartbeat())})}_scheduleHeartbeat(){this._wsPingTimer=setTimeout(()=>{this._sendHeartbeat()},this.pingInterval)}_sendHeartbeat(){if(!this._wsOpen()){f.log("Cannot send heartbeat, because socket closed");return}const e=JSON.stringify({type:S.Heartbeat});this._socket.send(e),this._scheduleHeartbeat()}_wsOpen(){return!!this._socket&&this._socket.readyState===1}_sendQueuedMessages(){const e=[...this._messagesQueue];this._messagesQueue=[];for(const t of e)this.send(t)}send(e){if(this._disconnected)return;if(!this._id){this._messagesQueue.push(e);return}if(!e.type){this.emit(A.Error,"Invalid message");return}if(!this._wsOpen())return;const t=JSON.stringify(e);this._socket.send(t)}close(){this._disconnected||(this._cleanup(),this._disconnected=!0)}_cleanup(){this._socket&&(this._socket.onopen=this._socket.onmessage=this._socket.onclose=null,this._socket.close(),this._socket=void 0),clearTimeout(this._wsPingTimer)}}class qt{constructor(e){this.connection=e}startConnection(e){const t=this._startPeerConnection();if(this.connection.peerConnection=t,this.connection.type===M.Media&&e._stream&&this._addTracksToConnection(e._stream,t),e.originator){const n=this.connection,s={ordered:!!e.reliable},i=t.createDataChannel(n.label,s);n._initializeDataChannel(i),this._makeOffer()}else this.handleSDP("OFFER",e.sdp)}_startPeerConnection(){f.log("Creating RTCPeerConnection.");const e=new RTCPeerConnection(this.connection.provider.options.config);return this._setupListeners(e),e}_setupListeners(e){const t=this.connection.peer,n=this.connection.connectionId,s=this.connection.type,i=this.connection.provider;f.log("Listening for ICE candidates."),e.onicecandidate=o=>{!o.candidate||!o.candidate.candidate||(f.log(`Received ICE candidates for ${t}:`,o.candidate),i.socket.send({type:S.Candidate,payload:{candidate:o.candidate,type:s,connectionId:n},dst:t}))},e.oniceconnectionstatechange=()=>{switch(e.iceConnectionState){case"failed":f.log("iceConnectionState is failed, closing connections to "+t),this.connection.emitError(xe.NegotiationFailed,"Negotiation of connection to "+t+" failed."),this.connection.close();break;case"closed":f.log("iceConnectionState is closed, closing connections to "+t),this.connection.emitError(xe.ConnectionClosed,"Connection to "+t+" closed."),this.connection.close();break;case"disconnected":f.log("iceConnectionState changed to disconnected on the connection with "+t);break;case"completed":e.onicecandidate=()=>{};break}this.connection.emit("iceStateChanged",e.iceConnectionState)},f.log("Listening for data channel"),e.ondatachannel=o=>{f.log("Received data channel");const a=o.channel;i.getConnection(t,n)._initializeDataChannel(a)},f.log("Listening for remote stream"),e.ontrack=o=>{f.log("Received remote stream");const a=o.streams[0],c=i.getConnection(t,n);if(c.type===M.Media){const h=c;this._addStreamToMediaConnection(a,h)}}}cleanup(){f.log("Cleaning up PeerConnection to "+this.connection.peer);const e=this.connection.peerConnection;if(!e)return;this.connection.peerConnection=null,e.onicecandidate=e.oniceconnectionstatechange=e.ondatachannel=e.ontrack=()=>{};const t=e.signalingState!=="closed";let n=!1;const s=this.connection.dataChannel;s&&(n=!!s.readyState&&s.readyState!=="closed"),(t||n)&&e.close()}async _makeOffer(){const e=this.connection.peerConnection,t=this.connection.provider;try{const n=await e.createOffer(this.connection.options.constraints);f.log("Created offer."),this.connection.options.sdpTransform&&typeof this.connection.options.sdpTransform=="function"&&(n.sdp=this.connection.options.sdpTransform(n.sdp)||n.sdp);try{await e.setLocalDescription(n),f.log("Set localDescription:",n,`for:${this.connection.peer}`);let s={sdp:n,type:this.connection.type,connectionId:this.connection.connectionId,metadata:this.connection.metadata};if(this.connection.type===M.Data){const i=this.connection;s={...s,label:i.label,reliable:i.reliable,serialization:i.serialization}}t.socket.send({type:S.Offer,payload:s,dst:this.connection.peer})}catch(s){s!="OperationError: Failed to set local offer sdp: Called in wrong state: kHaveRemoteOffer"&&(t.emitError(y.WebRTC,s),f.log("Failed to setLocalDescription, ",s))}}catch(n){t.emitError(y.WebRTC,n),f.log("Failed to createOffer, ",n)}}async _makeAnswer(){const e=this.connection.peerConnection,t=this.connection.provider;try{const n=await e.createAnswer();f.log("Created answer."),this.connection.options.sdpTransform&&typeof this.connection.options.sdpTransform=="function"&&(n.sdp=this.connection.options.sdpTransform(n.sdp)||n.sdp);try{await e.setLocalDescription(n),f.log("Set localDescription:",n,`for:${this.connection.peer}`),t.socket.send({type:S.Answer,payload:{sdp:n,type:this.connection.type,connectionId:this.connection.connectionId},dst:this.connection.peer})}catch(s){t.emitError(y.WebRTC,s),f.log("Failed to setLocalDescription, ",s)}}catch(n){t.emitError(y.WebRTC,n),f.log("Failed to create answer, ",n)}}async handleSDP(e,t){t=new RTCSessionDescription(t);const n=this.connection.peerConnection,s=this.connection.provider;f.log("Setting remote description",t);const i=this;try{await n.setRemoteDescription(t),f.log(`Set remoteDescription:${e} for:${this.connection.peer}`),e==="OFFER"&&await i._makeAnswer()}catch(o){s.emitError(y.WebRTC,o),f.log("Failed to setRemoteDescription, ",o)}}async handleCandidate(e){f.log("handleCandidate:",e);try{await this.connection.peerConnection.addIceCandidate(e),f.log(`Added ICE candidate for:${this.connection.peer}`)}catch(t){this.connection.provider.emitError(y.WebRTC,t),f.log("Failed to handleCandidate, ",t)}}_addTracksToConnection(e,t){if(f.log(`add tracks from stream ${e.id} to peer connection`),!t.addTrack)return f.error("Your browser does't support RTCPeerConnection#addTrack. Ignored.");e.getTracks().forEach(n=>{t.addTrack(n,e)})}_addStreamToMediaConnection(e,t){f.log(`add stream ${e.id} to media connection ${t.connectionId}`),t.addStream(e)}}class Ht extends ke.EventEmitter{emitError(e,t){f.error("Error:",t),this.emit("error",new Un(`${e}`,t))}}class Un extends Error{constructor(e,t){typeof t=="string"?super(t):(super(),Object.assign(this,t)),this.type=e}}class Vt extends Ht{get open(){return this._open}constructor(e,t,n){super(),this.peer=e,this.provider=t,this.options=n,this._open=!1,this.metadata=n.metadata}}const K=class K extends Vt{get type(){return M.Media}get localStream(){return this._localStream}get remoteStream(){return this._remoteStream}constructor(e,t,n){super(e,t,n),this._localStream=this.options._stream,this.connectionId=this.options.connectionId||K.ID_PREFIX+k.randomToken(),this._negotiator=new qt(this),this._localStream&&this._negotiator.startConnection({_stream:this._localStream,originator:!0})}_initializeDataChannel(e){this.dataChannel=e,this.dataChannel.onopen=()=>{f.log(`DC#${this.connectionId} dc connection success`),this.emit("willCloseOnRemote")},this.dataChannel.onclose=()=>{f.log(`DC#${this.connectionId} dc closed for:`,this.peer),this.close()}}addStream(e){f.log("Receiving stream",e),this._remoteStream=e,super.emit("stream",e)}handleMessage(e){const t=e.type,n=e.payload;switch(e.type){case S.Answer:this._negotiator.handleSDP(t,n.sdp),this._open=!0;break;case S.Candidate:this._negotiator.handleCandidate(n.candidate);break;default:f.warn(`Unrecognized message type:${t} from peer:${this.peer}`);break}}answer(e,t={}){if(this._localStream){f.warn("Local stream already exists on this MediaConnection. Are you answering a call twice?");return}this._localStream=e,t&&t.sdpTransform&&(this.options.sdpTransform=t.sdpTransform),this._negotiator.startConnection({...this.options._payload,_stream:e});const n=this.provider._getMessages(this.connectionId);for(const s of n)this.handleMessage(s);this._open=!0}close(){this._negotiator&&(this._negotiator.cleanup(),this._negotiator=null),this._localStream=null,this._remoteStream=null,this.provider&&(this.provider._removeConnection(this),this.provider=null),this.options&&this.options._stream&&(this.options._stream=null),this.open&&(this._open=!1,super.emit("close"))}};Ve=new WeakMap,W(K,Ve,K.ID_PREFIX="mc_");let ne=K;class Nn{constructor(e){this._options=e}_buildRequest(e){const t=this._options.secure?"https":"http",{host:n,port:s,path:i,key:o}=this._options,a=new URL(`${t}://${n}:${s}${i}${o}/${e}`);return a.searchParams.set("ts",`${Date.now()}${Math.random()}`),a.searchParams.set("version",jt),fetch(a.href,{referrerPolicy:this._options.referrerPolicy})}async retrieveId(){try{const e=await this._buildRequest("id");if(e.status!==200)throw new Error(`Error. Status:${e.status}`);return e.text()}catch(e){f.error("Error retrieving ID",e);let t="";throw this._options.path==="/"&&this._options.host!==k.CLOUD_HOST&&(t=" If you passed in a `path` to your self-hosted PeerServer, you'll also need to pass in that same path when creating a new Peer."),new Error("Could not get an ID from the server."+t)}}async listAllPeers(){try{const e=await this._buildRequest("peers");if(e.status!==200){if(e.status===401){let t="";throw this._options.host===k.CLOUD_HOST?t="It looks like you're using the cloud server. You can email team@peerjs.com to enable peer listing for your API key.":t="You need to enable `allow_discovery` on your self-hosted PeerServer to use this feature.",new Error("It doesn't look like you have permission to list peers IDs. "+t)}throw new Error(`Error. Status:${e.status}`)}return e.json()}catch(e){throw f.error("Error retrieving list peers",e),new Error("Could not get list peers from the server."+e)}}}const B=class B extends Vt{get type(){return M.Data}constructor(e,t,n){super(e,t,n),this.connectionId=this.options.connectionId||B.ID_PREFIX+Nt(),this.label=this.options.label||this.connectionId,this.reliable=!!this.options.reliable,this._negotiator=new qt(this),this._negotiator.startConnection(this.options._payload||{originator:!0,reliable:this.reliable})}_initializeDataChannel(e){this.dataChannel=e,this.dataChannel.onopen=()=>{f.log(`DC#${this.connectionId} dc connection success`),this._open=!0,this.emit("open")},this.dataChannel.onmessage=t=>{f.log(`DC#${this.connectionId} dc onmessage:`,t.data)},this.dataChannel.onclose=()=>{f.log(`DC#${this.connectionId} dc closed for:`,this.peer),this.close()}}close(e){if(e!=null&&e.flush){this.send({__peerData:{type:"close"}});return}this._negotiator&&(this._negotiator.cleanup(),this._negotiator=null),this.provider&&(this.provider._removeConnection(this),this.provider=null),this.dataChannel&&(this.dataChannel.onopen=null,this.dataChannel.onmessage=null,this.dataChannel.onclose=null,this.dataChannel=null),this.open&&(this._open=!1,super.emit("close"))}send(e,t=!1){if(!this.open){this.emitError(Te.NotOpenYet,"Connection is not open. You should listen for the `open` event before sending messages.");return}return this._send(e,t)}async handleMessage(e){const t=e.payload;switch(e.type){case S.Answer:await this._negotiator.handleSDP(e.type,t.sdp);break;case S.Candidate:await this._negotiator.handleCandidate(t.candidate);break;default:f.warn("Unrecognized message type:",e.type,"from peer:",this.peer);break}}};Ke=new WeakMap,Je=new WeakMap,W(B,Ke,B.ID_PREFIX="dc_"),W(B,Je,B.MAX_BUFFERED_AMOUNT=8388608);let re=B;class Re extends re{get bufferSize(){return this._bufferSize}_initializeDataChannel(e){super._initializeDataChannel(e),this.dataChannel.binaryType="arraybuffer",this.dataChannel.addEventListener("message",t=>this._handleDataMessage(t))}_bufferedSend(e){(this._buffering||!this._trySend(e))&&(this._buffer.push(e),this._bufferSize=this._buffer.length)}_trySend(e){if(!this.open)return!1;if(this.dataChannel.bufferedAmount>re.MAX_BUFFERED_AMOUNT)return this._buffering=!0,setTimeout(()=>{this._buffering=!1,this._tryBuffer()},50),!1;try{this.dataChannel.send(e)}catch(t){return f.error(`DC#:${this.connectionId} Error when sending:`,t),this._buffering=!0,this.close(),!1}return!0}_tryBuffer(){if(!this.open||this._buffer.length===0)return;const e=this._buffer[0];this._trySend(e)&&(this._buffer.shift(),this._bufferSize=this._buffer.length,this._tryBuffer())}close(e){if(e!=null&&e.flush){this.send({__peerData:{type:"close"}});return}this._buffer=[],this._bufferSize=0,super.close()}constructor(...e){super(...e),this._buffer=[],this._bufferSize=0,this._buffering=!1}}class Pe extends Re{close(e){super.close(e),this._chunkedData={}}constructor(e,t,n){super(e,t,n),this.chunker=new Ut,this.serialization=te.Binary,this._chunkedData={}}_handleDataMessage({data:e}){const t=Ge(e),n=t.__peerData;if(n){if(n.type==="close"){this.close();return}this._handleChunk(t);return}this.emit("data",t)}_handleChunk(e){const t=e.__peerData,n=this._chunkedData[t]||{data:[],count:0,total:e.total};if(n.data[e.n]=new Uint8Array(e.data),n.count++,this._chunkedData[t]=n,n.total===n.count){delete this._chunkedData[t];const s=Mn(n.data);this._handleDataMessage({data:s})}}_send(e,t){const n=Qe(e);if(n instanceof Promise)return this._send_blob(n);if(!t&&n.byteLength>this.chunker.chunkedMTU){this._sendChunks(n);return}this._bufferedSend(n)}async _send_blob(e){const t=await e;if(t.byteLength>this.chunker.chunkedMTU){this._sendChunks(t);return}this._bufferedSend(t)}_sendChunks(e){const t=this.chunker.chunk(e);f.log(`DC#${this.connectionId} Try to send ${t.length} chunks...`);for(const n of t)this.send(n,!0)}}class zn extends Re{_handleDataMessage({data:e}){super.emit("data",e)}_send(e,t){this._bufferedSend(e)}constructor(...e){super(...e),this.serialization=te.None}}class Fn extends Re{_handleDataMessage({data:e}){const t=this.parse(this.decoder.decode(e)),n=t.__peerData;if(n&&n.type==="close"){this.close();return}this.emit("data",t)}_send(e,t){const n=this.encoder.encode(this.stringify(e));if(n.byteLength>=k.chunkedMTU){this.emitError(Te.MessageToBig,"Message too big for JSON channel");return}this._bufferedSend(n)}constructor(...e){super(...e),this.serialization=te.JSON,this.encoder=new TextEncoder,this.decoder=new TextDecoder,this.stringify=JSON.stringify,this.parse=JSON.parse}}const J=class J extends Ht{get id(){return this._id}get options(){return this._options}get open(){return this._open}get socket(){return this._socket}get connections(){const e=Object.create(null);for(const[t,n]of this._connections)e[t]=n;return e}get destroyed(){return this._destroyed}get disconnected(){return this._disconnected}constructor(e,t){super(),this._serializers={raw:zn,json:Fn,binary:Pe,"binary-utf8":Pe,default:Pe},this._id=null,this._lastServerId=null,this._destroyed=!1,this._disconnected=!1,this._open=!1,this._connections=new Map,this._lostMessages=new Map;let n;if(e&&e.constructor==Object?t=e:e&&(n=e.toString()),t={debug:0,host:k.CLOUD_HOST,port:k.CLOUD_PORT,path:"/",key:J.DEFAULT_KEY,token:k.randomToken(),config:k.defaultConfig,referrerPolicy:"strict-origin-when-cross-origin",serializers:{},...t},this._options=t,this._serializers={...this._serializers,...this.options.serializers},this._options.host==="/"&&(this._options.host=window.location.hostname),this._options.path&&(this._options.path[0]!=="/"&&(this._options.path="/"+this._options.path),this._options.path[this._options.path.length-1]!=="/"&&(this._options.path+="/")),this._options.secure===void 0&&this._options.host!==k.CLOUD_HOST?this._options.secure=k.isSecure():this._options.host==k.CLOUD_HOST&&(this._options.secure=!0),this._options.logFunction&&f.setLogFunction(this._options.logFunction),f.logLevel=this._options.debug||0,this._api=new Nn(t),this._socket=this._createServerConnection(),!k.supports.audioVideo&&!k.supports.data){this._delayedAbort(y.BrowserIncompatible,"The current browser does not support WebRTC");return}if(n&&!k.validateId(n)){this._delayedAbort(y.InvalidID,`ID "${n}" is invalid`);return}n?this._initialize(n):this._api.retrieveId().then(s=>this._initialize(s)).catch(s=>this._abort(y.ServerError,s))}_createServerConnection(){const e=new Bn(this._options.secure,this._options.host,this._options.port,this._options.path,this._options.key,this._options.pingInterval);return e.on(A.Message,t=>{this._handleMessage(t)}),e.on(A.Error,t=>{this._abort(y.SocketError,t)}),e.on(A.Disconnected,()=>{this.disconnected||(this.emitError(y.Network,"Lost connection to server."),this.disconnect())}),e.on(A.Close,()=>{this.disconnected||this._abort(y.SocketClosed,"Underlying socket is already closed.")}),e}_initialize(e){this._id=e,this.socket.start(e,this._options.token)}_handleMessage(e){const t=e.type,n=e.payload,s=e.src;switch(t){case S.Open:this._lastServerId=this.id,this._open=!0,this.emit("open",this.id);break;case S.Error:this._abort(y.ServerError,n.msg);break;case S.IdTaken:this._abort(y.UnavailableID,`ID "${this.id}" is taken`);break;case S.InvalidKey:this._abort(y.InvalidKey,`API KEY "${this._options.key}" is invalid`);break;case S.Leave:f.log(`Received leave message from ${s}`),this._cleanupPeer(s),this._connections.delete(s);break;case S.Expire:this.emitError(y.PeerUnavailable,`Could not connect to peer ${s}`);break;case S.Offer:{const i=n.connectionId;let o=this.getConnection(s,i);if(o&&(o.close(),f.warn(`Offer received for existing Connection ID:${i}`)),n.type===M.Media){const c=new ne(s,this,{connectionId:i,_payload:n,metadata:n.metadata});o=c,this._addConnection(s,o),this.emit("call",c)}else if(n.type===M.Data){const c=new this._serializers[n.serialization](s,this,{connectionId:i,_payload:n,metadata:n.metadata,label:n.label,serialization:n.serialization,reliable:n.reliable});o=c,this._addConnection(s,o),this.emit("connection",c)}else{f.warn(`Received malformed connection type:${n.type}`);return}const a=this._getMessages(i);for(const c of a)o.handleMessage(c);break}default:{if(!n){f.warn(`You received a malformed message from ${s} of type ${t}`);return}const i=n.connectionId,o=this.getConnection(s,i);o&&o.peerConnection?o.handleMessage(e):i?this._storeMessage(i,e):f.warn("You received an unrecognized message:",e);break}}}_storeMessage(e,t){this._lostMessages.has(e)||this._lostMessages.set(e,[]),this._lostMessages.get(e).push(t)}_getMessages(e){const t=this._lostMessages.get(e);return t?(this._lostMessages.delete(e),t):[]}connect(e,t={}){if(t={serialization:"default",...t},this.disconnected){f.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect, or call reconnect on this peer if you believe its ID to still be available."),this.emitError(y.Disconnected,"Cannot connect to new Peer after disconnecting from server.");return}const n=new this._serializers[t.serialization](e,this,t);return this._addConnection(e,n),n}call(e,t,n={}){if(this.disconnected){f.warn("You cannot connect to a new Peer because you called .disconnect() on this Peer and ended your connection with the server. You can create a new Peer to reconnect."),this.emitError(y.Disconnected,"Cannot connect to new Peer after disconnecting from server.");return}if(!t){f.error("To call a peer, you must provide a stream from your browser's `getUserMedia`.");return}const s=new ne(e,this,{...n,_stream:t});return this._addConnection(e,s),s}_addConnection(e,t){f.log(`add connection ${t.type}:${t.connectionId} to peerId:${e}`),this._connections.has(e)||this._connections.set(e,[]),this._connections.get(e).push(t)}_removeConnection(e){const t=this._connections.get(e.peer);if(t){const n=t.indexOf(e);n!==-1&&t.splice(n,1)}this._lostMessages.delete(e.connectionId)}getConnection(e,t){const n=this._connections.get(e);if(!n)return null;for(const s of n)if(s.connectionId===t)return s;return null}_delayedAbort(e,t){setTimeout(()=>{this._abort(e,t)},0)}_abort(e,t){f.error("Aborting!"),this.emitError(e,t),this._lastServerId?this.disconnect():this.destroy()}destroy(){this.destroyed||(f.log(`Destroy peer with ID:${this.id}`),this.disconnect(),this._cleanup(),this._destroyed=!0,this.emit("close"))}_cleanup(){for(const e of this._connections.keys())this._cleanupPeer(e),this._connections.delete(e);this.socket.removeAllListeners()}_cleanupPeer(e){const t=this._connections.get(e);if(t)for(const n of t)n.close()}disconnect(){if(this.disconnected)return;const e=this.id;f.log(`Disconnect peer with ID:${e}`),this._disconnected=!0,this._open=!1,this.socket.close(),this._lastServerId=e,this._id=null,this.emit("disconnected",e)}reconnect(){if(this.disconnected&&!this.destroyed)f.log(`Attempting reconnection to server with ID ${this._lastServerId}`),this._disconnected=!1,this._initialize(this._lastServerId);else{if(this.destroyed)throw new Error("This peer cannot reconnect to the server. It has already been destroyed.");if(!this.disconnected&&!this.open)f.error("In a hurry? We're still trying to make the initial connection!");else throw new Error(`Peer ${this.id} cannot reconnect because it is not disconnected from the server!`)}}listAllPeers(e=t=>{}){this._api.listAllPeers().then(t=>e(t)).catch(t=>this._abort(y.ServerError,t))}};We=new WeakMap,W(J,We,J.DEFAULT_KEY="peerjs");let Ee=J;var jn=Ee;class qn{constructor(e){this.config=e,this.peer=null,this.localStream=null,this.connections={},this.callbacks={onStream:null,onPeerLeft:null,onError:null,onScreenShareEnded:null}}buildIceServers(){const e=[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"}],t=Array.isArray(this.config.iceServers)?this.config.iceServers:[];return[...e,...t]}async initialize(e,t=0){return new Promise((n,s)=>{const i=t===0?e:`${e}-r${Math.random().toString(36).slice(2,6)}`;this.peer=new jn(i,{host:this.config.peerServer.host,port:this.config.peerServer.port,path:this.config.peerServer.path||"/peerjs",secure:this.config.peerServer.secure??!1,config:{iceServers:this.buildIceServers()}});let o=!1;this.peer.on("open",a=>{o=!0,console.log("Peer initialized:",a),this.actualPeerId=a,n(a)}),this.peer.on("error",async a=>{if(!o&&a&&a.type==="unavailable-id"&&t<1){console.warn(`Peer id "${i}" taken — retrying with a suffix`);try{this.peer.destroy()}catch{}this.peer=null;try{const c=await this.initialize(e,t+1);o=!0,n(c)}catch(c){s(c)}return}console.error("Peer error:",a),this.callbacks.onError&&this.callbacks.onError(a),o||s(a)}),this.peer.on("call",a=>{this.handleIncomingCall(a)}),this.peer.on("disconnected",()=>{this._disposed||(console.warn("PeerJS signalling disconnected — attempting reconnect"),setTimeout(()=>{try{this.peer&&!this.peer.destroyed&&this.peer.reconnect()}catch(a){console.warn("Peer reconnect threw:",a&&a.message)}},1e3))})})}async getLocalStream(){if(this.localStream)return this.localStream;try{return this.localStream=await navigator.mediaDevices.getUserMedia({video:!0,audio:!0}),this.localStream}catch(e){throw console.error("Failed to get local stream:",e),this.callbacks.onError&&this.callbacks.onError(e),e}}async handleIncomingCall(e){if(this.connections[e.peer]){console.log("Duplicate incoming call from",e.peer,"— closing");try{e.close()}catch{}return}this.connections[e.peer]={call:e,stream:null};const t=await this.getLocalStream();e.answer(t),e.on("stream",n=>{console.log("Received stream from:",e.peer),this.connections[e.peer]={call:e,stream:n},this.callbacks.onStream&&this.callbacks.onStream(e.peer,n,!1)}),e.on("close",()=>{console.log("Call closed:",e.peer),delete this.connections[e.peer],this.callbacks.onPeerLeft&&this.callbacks.onPeerLeft(e.peer)})}async callPeer(e){if(this.connections[e]){console.log("Already connected to:",e);return}if(this.peer&&this.peer.id&&this.peer.id>e){console.log(`Waiting for ${e} to call us (our id ${this.peer.id} > theirs)`);return}this.connections[e]={call:null,stream:null};const t=await this.getLocalStream();if(this.connections[e]&&this.connections[e].call)return;const n=this.peer.call(e,t);this.connections[e]={call:n,stream:null},n.on("stream",s=>{console.log("Received stream from:",e),this.connections[e]={call:n,stream:s},this.callbacks.onStream&&this.callbacks.onStream(e,s,!1)}),n.on("close",()=>{console.log("Call closed:",e),delete this.connections[e],this.callbacks.onPeerLeft&&this.callbacks.onPeerLeft(e)}),n.on("error",s=>{console.error("Call error:",s),delete this.connections[e]})}toggleVideo(){if(this.localStream){const e=this.localStream.getVideoTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}toggleAudio(){if(this.localStream){const e=this.localStream.getAudioTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}replaceVideoTrackOnAllConnections(e){for(const{call:t}of Object.values(this.connections)){const n=t&&t.peerConnection;if(!n)continue;const s=n.getSenders().find(i=>i.track&&i.track.kind==="video");s&&s.replaceTrack(e).catch(i=>console.warn("replaceTrack failed:",i&&i.message))}}async startScreenShare(){if(!navigator.mediaDevices||!navigator.mediaDevices.getDisplayMedia)throw new Error("Screen sharing is not supported in this browser");const t=(await navigator.mediaDevices.getDisplayMedia({video:!0,audio:!1})).getVideoTracks()[0];if(!t)throw new Error("Could not obtain display video track");return this.cameraVideoTrack=this.localStream.getVideoTracks()[0]||null,this.replaceVideoTrackOnAllConnections(t),this.cameraVideoTrack&&this.localStream.removeTrack(this.cameraVideoTrack),this.localStream.addTrack(t),this.screenTrack=t,t.addEventListener("ended",async()=>{const n=!!this.screenTrack;if(await this.stopScreenShare().catch(()=>{}),n&&this.callbacks.onScreenShareEnded)try{this.callbacks.onScreenShareEnded()}catch(s){console.warn(s)}}),t}async stopScreenShare(){if(!this.screenTrack)return!1;try{this.screenTrack.stop()}catch{}let e=this.cameraVideoTrack;if(!e||e.readyState==="ended")try{e=(await navigator.mediaDevices.getUserMedia({video:!0,audio:!1})).getVideoTracks()[0]}catch(t){console.warn("Could not restore camera after screen share:",t&&t.message),e=null}if(e){this.replaceVideoTrackOnAllConnections(e);try{this.localStream.removeTrack(this.screenTrack)}catch{}this.localStream.addTrack(e),this.cameraVideoTrack=e}return this.screenTrack=null,!0}isScreenSharing(){return!!this.screenTrack}on(e,t){this.callbacks.hasOwnProperty("on"+e.charAt(0).toUpperCase()+e.slice(1))&&(this.callbacks["on"+e.charAt(0).toUpperCase()+e.slice(1)]=t)}disconnect(){this._disposed=!0,Object.values(this.connections).forEach(({call:e})=>{e.close()}),this.connections={},this.localStream&&(this.localStream.getTracks().forEach(e=>e.stop()),this.localStream=null),this.peer&&(this.peer.destroy(),this.peer=null)}}const E=Object.create(null);E.open="0",E.close="1",E.ping="2",E.pong="3",E.message="4",E.upgrade="5",E.noop="6";const se=Object.create(null);Object.keys(E).forEach(r=>{se[E[r]]=r});const we={type:"error",data:"parser error"},Kt=typeof Blob=="function"||typeof Blob<"u"&&Object.prototype.toString.call(Blob)==="[object BlobConstructor]",Jt=typeof ArrayBuffer=="function",Wt=r=>typeof ArrayBuffer.isView=="function"?ArrayBuffer.isView(r):r&&r.buffer instanceof ArrayBuffer,Me=({type:r,data:e},t,n)=>Kt&&e instanceof Blob?t?n(e):Yt(e,n):Jt&&(e instanceof ArrayBuffer||Wt(e))?t?n(e):Yt(new Blob([e]),n):n(E[r]+(e||"")),Yt=(r,e)=>{const t=new FileReader;return t.onload=function(){const n=t.result.split(",")[1];e("b"+(n||""))},t.readAsDataURL(r)};function Gt(r){return r instanceof Uint8Array?r:r instanceof ArrayBuffer?new Uint8Array(r):new Uint8Array(r.buffer,r.byteOffset,r.byteLength)}let Ae;function Hn(r,e){if(Kt&&r.data instanceof Blob)return r.data.arrayBuffer().then(Gt).then(e);if(Jt&&(r.data instanceof ArrayBuffer||Wt(r.data)))return e(Gt(r.data));Me(r,!1,t=>{Ae||(Ae=new TextEncoder),e(Ae.encode(t))})}const Qt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",q=typeof Uint8Array>"u"?[]:new Uint8Array(256);for(let r=0;r<Qt.length;r++)q[Qt.charCodeAt(r)]=r;const Vn=r=>{let e=r.length*.75,t=r.length,n,s=0,i,o,a,c;r[r.length-1]==="="&&(e--,r[r.length-2]==="="&&e--);const h=new ArrayBuffer(e),l=new Uint8Array(h);for(n=0;n<t;n+=4)i=q[r.charCodeAt(n)],o=q[r.charCodeAt(n+1)],a=q[r.charCodeAt(n+2)],c=q[r.charCodeAt(n+3)],l[s++]=i<<2|o>>4,l[s++]=(o&15)<<4|a>>2,l[s++]=(a&3)<<6|c&63;return h},Kn=typeof ArrayBuffer=="function",Ie=(r,e)=>{if(typeof r!="string")return{type:"message",data:Xt(r,e)};const t=r.charAt(0);return t==="b"?{type:"message",data:Jn(r.substring(1),e)}:se[t]?r.length>1?{type:se[t],data:r.substring(1)}:{type:se[t]}:we},Jn=(r,e)=>{if(Kn){const t=Vn(r);return Xt(t,e)}else return{base64:!0,data:r}},Xt=(r,e)=>{switch(e){case"blob":return r instanceof Blob?r:new Blob([r]);case"arraybuffer":default:return r instanceof ArrayBuffer?r:r.buffer}},Zt="",Wn=(r,e)=>{const t=r.length,n=new Array(t);let s=0;r.forEach((i,o)=>{Me(i,!1,a=>{n[o]=a,++s===t&&e(n.join(Zt))})})},Yn=(r,e)=>{const t=r.split(Zt),n=[];for(let s=0;s<t.length;s++){const i=Ie(t[s],e);if(n.push(i),i.type==="error")break}return n};function Gn(){return new TransformStream({transform(r,e){Hn(r,t=>{const n=t.length;let s;if(n<126)s=new Uint8Array(1),new DataView(s.buffer).setUint8(0,n);else if(n<65536){s=new Uint8Array(3);const i=new DataView(s.buffer);i.setUint8(0,126),i.setUint16(1,n)}else{s=new Uint8Array(9);const i=new DataView(s.buffer);i.setUint8(0,127),i.setBigUint64(1,BigInt(n))}r.data&&typeof r.data!="string"&&(s[0]|=128),e.enqueue(s),e.enqueue(t)})}})}let Oe;function ie(r){return r.reduce((e,t)=>e+t.length,0)}function oe(r,e){if(r[0].length===e)return r.shift();const t=new Uint8Array(e);let n=0;for(let s=0;s<e;s++)t[s]=r[0][n++],n===r[0].length&&(r.shift(),n=0);return r.length&&n<r[0].length&&(r[0]=r[0].slice(n)),t}function Qn(r,e){Oe||(Oe=new TextDecoder);const t=[];let n=0,s=-1,i=!1;return new TransformStream({transform(o,a){for(t.push(o);;){if(n===0){if(ie(t)<1)break;const c=oe(t,1);i=(c[0]&128)===128,s=c[0]&127,s<126?n=3:s===126?n=1:n=2}else if(n===1){if(ie(t)<2)break;const c=oe(t,2);s=new DataView(c.buffer,c.byteOffset,c.length).getUint16(0),n=3}else if(n===2){if(ie(t)<8)break;const c=oe(t,8),h=new DataView(c.buffer,c.byteOffset,c.length),l=h.getUint32(0);if(l>Math.pow(2,21)-1){a.enqueue(we);break}s=l*Math.pow(2,32)+h.getUint32(4),n=3}else{if(ie(t)<s)break;const c=oe(t,s);a.enqueue(Ie(i?c:Oe.decode(c),e)),n=0}if(s===0||s>r){a.enqueue(we);break}}}})}const en=4;function b(r){if(r)return Xn(r)}function Xn(r){for(var e in b.prototype)r[e]=b.prototype[e];return r}b.prototype.on=b.prototype.addEventListener=function(r,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+r]=this._callbacks["$"+r]||[]).push(e),this},b.prototype.once=function(r,e){function t(){this.off(r,t),e.apply(this,arguments)}return t.fn=e,this.on(r,t),this},b.prototype.off=b.prototype.removeListener=b.prototype.removeAllListeners=b.prototype.removeEventListener=function(r,e){if(this._callbacks=this._callbacks||{},arguments.length==0)return this._callbacks={},this;var t=this._callbacks["$"+r];if(!t)return this;if(arguments.length==1)return delete this._callbacks["$"+r],this;for(var n,s=0;s<t.length;s++)if(n=t[s],n===e||n.fn===e){t.splice(s,1);break}return t.length===0&&delete this._callbacks["$"+r],this},b.prototype.emit=function(r){this._callbacks=this._callbacks||{};for(var e=new Array(arguments.length-1),t=this._callbacks["$"+r],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(t){t=t.slice(0);for(var n=0,s=t.length;n<s;++n)t[n].apply(this,e)}return this},b.prototype.emitReserved=b.prototype.emit,b.prototype.listeners=function(r){return this._callbacks=this._callbacks||{},this._callbacks["$"+r]||[]},b.prototype.hasListeners=function(r){return!!this.listeners(r).length};const ae=typeof Promise=="function"&&typeof Promise.resolve=="function"?e=>Promise.resolve().then(e):(e,t)=>t(e,0),T=typeof self<"u"?self:typeof window<"u"?window:Function("return this")(),Zn="arraybuffer";function ss(){}function tn(r,...e){return e.reduce((t,n)=>(r.hasOwnProperty(n)&&(t[n]=r[n]),t),{})}const er=T.setTimeout,tr=T.clearTimeout;function ce(r,e){e.useNativeTimers?(r.setTimeoutFn=er.bind(T),r.clearTimeoutFn=tr.bind(T)):(r.setTimeoutFn=T.setTimeout.bind(T),r.clearTimeoutFn=T.clearTimeout.bind(T))}const nr=1.33;function rr(r){return typeof r=="string"?sr(r):Math.ceil((r.byteLength||r.size)*nr)}function sr(r){let e=0,t=0;for(let n=0,s=r.length;n<s;n++)e=r.charCodeAt(n),e<128?t+=1:e<2048?t+=2:e<55296||e>=57344?t+=3:(n++,t+=4);return t}function nn(){return Date.now().toString(36).substring(3)+Math.random().toString(36).substring(2,5)}function ir(r){let e="";for(let t in r)r.hasOwnProperty(t)&&(e.length&&(e+="&"),e+=encodeURIComponent(t)+"="+encodeURIComponent(r[t]));return e}function or(r){let e={},t=r.split("&");for(let n=0,s=t.length;n<s;n++){let i=t[n].split("=");e[decodeURIComponent(i[0])]=decodeURIComponent(i[1])}return e}class ar extends Error{constructor(e,t,n){super(e),this.description=t,this.context=n,this.type="TransportError"}}class De extends b{constructor(e){super(),this.writable=!1,ce(this,e),this.opts=e,this.query=e.query,this.socket=e.socket,this.supportsBinary=!e.forceBase64}onError(e,t,n){return super.emitReserved("error",new ar(e,t,n)),this}open(){return this.readyState="opening",this.doOpen(),this}close(){return(this.readyState==="opening"||this.readyState==="open")&&(this.doClose(),this.onClose()),this}send(e){this.readyState==="open"&&this.write(e)}onOpen(){this.readyState="open",this.writable=!0,super.emitReserved("open")}onData(e){const t=Ie(e,this.socket.binaryType);this.onPacket(t)}onPacket(e){super.emitReserved("packet",e)}onClose(e){this.readyState="closed",super.emitReserved("close",e)}pause(e){}createUri(e,t={}){return e+"://"+this._hostname()+this._port()+this.opts.path+this._query(t)}_hostname(){const e=this.opts.hostname;return e.indexOf(":")===-1?e:"["+e+"]"}_port(){return this.opts.port&&(this.opts.secure&&Number(this.opts.port)!==443||!this.opts.secure&&Number(this.opts.port)!==80)?":"+this.opts.port:""}_query(e){const t=ir(e);return t.length?"?"+t:""}}class cr extends De{constructor(){super(...arguments),this._polling=!1}get name(){return"polling"}doOpen(){this._poll()}pause(e){this.readyState="pausing";const t=()=>{this.readyState="paused",e()};if(this._polling||!this.writable){let n=0;this._polling&&(n++,this.once("pollComplete",function(){--n||t()})),this.writable||(n++,this.once("drain",function(){--n||t()}))}else t()}_poll(){this._polling=!0,this.doPoll(),this.emitReserved("poll")}onData(e){const t=n=>{if(this.readyState==="opening"&&n.type==="open"&&this.onOpen(),n.type==="close")return this.onClose({description:"transport closed by the server"}),!1;this.onPacket(n)};Yn(e,this.socket.binaryType).forEach(t),this.readyState!=="closed"&&(this._polling=!1,this.emitReserved("pollComplete"),this.readyState==="open"&&this._poll())}doClose(){const e=()=>{this.write([{type:"close"}])};this.readyState==="open"?e():this.once("open",e)}write(e){this.writable=!1,Wn(e,t=>{this.doWrite(t,()=>{this.writable=!0,this.emitReserved("drain")})})}uri(){const e=this.opts.secure?"https":"http",t=this.query||{};return this.opts.timestampRequests!==!1&&(t[this.opts.timestampParam]=nn()),!this.supportsBinary&&!t.sid&&(t.b64=1),this.createUri(e,t)}}let rn=!1;try{rn=typeof XMLHttpRequest<"u"&&"withCredentials"in new XMLHttpRequest}catch{}const hr=rn;function lr(){}class dr extends cr{constructor(e){if(super(e),typeof location<"u"){const t=location.protocol==="https:";let n=location.port;n||(n=t?"443":"80"),this.xd=typeof location<"u"&&e.hostname!==location.hostname||n!==e.port}}doWrite(e,t){const n=this.request({method:"POST",data:e});n.on("success",t),n.on("error",(s,i)=>{this.onError("xhr post error",s,i)})}doPoll(){const e=this.request();e.on("data",this.onData.bind(this)),e.on("error",(t,n)=>{this.onError("xhr poll error",t,n)}),this.pollXhr=e}}class w extends b{constructor(e,t,n){super(),this.createRequest=e,ce(this,n),this._opts=n,this._method=n.method||"GET",this._uri=t,this._data=n.data!==void 0?n.data:null,this._create()}_create(){var e;const t=tn(this._opts,"agent","pfx","key","passphrase","cert","ca","ciphers","rejectUnauthorized","autoUnref");t.xdomain=!!this._opts.xd;const n=this._xhr=this.createRequest(t);try{n.open(this._method,this._uri,!0);try{if(this._opts.extraHeaders){n.setDisableHeaderCheck&&n.setDisableHeaderCheck(!0);for(let s in this._opts.extraHeaders)this._opts.extraHeaders.hasOwnProperty(s)&&n.setRequestHeader(s,this._opts.extraHeaders[s])}}catch{}if(this._method==="POST")try{n.setRequestHeader("Content-type","text/plain;charset=UTF-8")}catch{}try{n.setRequestHeader("Accept","*/*")}catch{}(e=this._opts.cookieJar)===null||e===void 0||e.addCookies(n),"withCredentials"in n&&(n.withCredentials=this._opts.withCredentials),this._opts.requestTimeout&&(n.timeout=this._opts.requestTimeout),n.onreadystatechange=()=>{var s;n.readyState===3&&((s=this._opts.cookieJar)===null||s===void 0||s.parseCookies(n.getResponseHeader("set-cookie"))),n.readyState===4&&(n.status===200||n.status===1223?this._onLoad():this.setTimeoutFn(()=>{this._onError(typeof n.status=="number"?n.status:0)},0))},n.send(this._data)}catch(s){this.setTimeoutFn(()=>{this._onError(s)},0);return}typeof document<"u"&&(this._index=w.requestsCount++,w.requests[this._index]=this)}_onError(e){this.emitReserved("error",e,this._xhr),this._cleanup(!0)}_cleanup(e){if(!(typeof this._xhr>"u"||this._xhr===null)){if(this._xhr.onreadystatechange=lr,e)try{this._xhr.abort()}catch{}typeof document<"u"&&delete w.requests[this._index],this._xhr=null}}_onLoad(){const e=this._xhr.responseText;e!==null&&(this.emitReserved("data",e),this.emitReserved("success"),this._cleanup())}abort(){this._cleanup()}}if(w.requestsCount=0,w.requests={},typeof document<"u"){if(typeof attachEvent=="function")attachEvent("onunload",sn);else if(typeof addEventListener=="function"){const r="onpagehide"in T?"pagehide":"unload";addEventListener(r,sn,!1)}}function sn(){for(let r in w.requests)w.requests.hasOwnProperty(r)&&w.requests[r].abort()}const ur=function(){const r=on({xdomain:!1});return r&&r.responseType!==null}();class fr extends dr{constructor(e){super(e);const t=e&&e.forceBase64;this.supportsBinary=ur&&!t}request(e={}){return Object.assign(e,{xd:this.xd},this.opts),new w(on,this.uri(),e)}}function on(r){const e=r.xdomain;try{if(typeof XMLHttpRequest<"u"&&(!e||hr))return new XMLHttpRequest}catch{}if(!e)try{return new T[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP")}catch{}}const an=typeof navigator<"u"&&typeof navigator.product=="string"&&navigator.product.toLowerCase()==="reactnative";class pr extends De{get name(){return"websocket"}doOpen(){const e=this.uri(),t=this.opts.protocols,n=an?{}:tn(this.opts,"agent","perMessageDeflate","pfx","key","passphrase","cert","ca","ciphers","rejectUnauthorized","localAddress","protocolVersion","origin","maxPayload","family","checkServerIdentity");this.opts.extraHeaders&&(n.headers=this.opts.extraHeaders);try{this.ws=this.createSocket(e,t,n)}catch(s){return this.emitReserved("error",s)}this.ws.binaryType=this.socket.binaryType,this.addEventListeners()}addEventListeners(){this.ws.onopen=()=>{this.opts.autoUnref&&this.ws._socket.unref(),this.onOpen()},this.ws.onclose=e=>this.onClose({description:"websocket connection closed",context:e}),this.ws.onmessage=e=>this.onData(e.data),this.ws.onerror=e=>this.onError("websocket error",e)}write(e){this.writable=!1;for(let t=0;t<e.length;t++){const n=e[t],s=t===e.length-1;Me(n,this.supportsBinary,i=>{try{this.doWrite(n,i)}catch{}s&&ae(()=>{this.writable=!0,this.emitReserved("drain")},this.setTimeoutFn)})}}doClose(){typeof this.ws<"u"&&(this.ws.onerror=()=>{},this.ws.close(),this.ws=null)}uri(){const e=this.opts.secure?"wss":"ws",t=this.query||{};return this.opts.timestampRequests&&(t[this.opts.timestampParam]=nn()),this.supportsBinary||(t.b64=1),this.createUri(e,t)}}const Le=T.WebSocket||T.MozWebSocket;class gr extends pr{createSocket(e,t,n){return an?new Le(e,t,n):t?new Le(e,t):new Le(e)}doWrite(e,t){this.ws.send(t)}}class mr extends De{get name(){return"webtransport"}doOpen(){try{this._transport=new WebTransport(this.createUri("https"),this.opts.transportOptions[this.name])}catch(e){return this.emitReserved("error",e)}this._transport.closed.then(()=>{this.onClose()}).catch(e=>{this.onError("webtransport error",e)}),this._transport.ready.then(()=>{this._transport.createBidirectionalStream().then(e=>{const t=Qn(Number.MAX_SAFE_INTEGER,this.socket.binaryType),n=e.readable.pipeThrough(t).getReader(),s=Gn();s.readable.pipeTo(e.writable),this._writer=s.writable.getWriter();const i=()=>{n.read().then(({done:a,value:c})=>{a||(this.onPacket(c),i())}).catch(a=>{})};i();const o={type:"open"};this.query.sid&&(o.data=`{"sid":"${this.query.sid}"}`),this._writer.write(o).then(()=>this.onOpen())})})}write(e){this.writable=!1;for(let t=0;t<e.length;t++){const n=e[t],s=t===e.length-1;this._writer.write(n).then(()=>{s&&ae(()=>{this.writable=!0,this.emitReserved("drain")},this.setTimeoutFn)})}}doClose(){var e;(e=this._transport)===null||e===void 0||e.close()}}const yr={websocket:gr,webtransport:mr,polling:fr},br=/^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,_r=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"];function $e(r){if(r.length>8e3)throw"URI too long";const e=r,t=r.indexOf("["),n=r.indexOf("]");t!=-1&&n!=-1&&(r=r.substring(0,t)+r.substring(t,n).replace(/:/g,";")+r.substring(n,r.length));let s=br.exec(r||""),i={},o=14;for(;o--;)i[_r[o]]=s[o]||"";return t!=-1&&n!=-1&&(i.source=e,i.host=i.host.substring(1,i.host.length-1).replace(/;/g,":"),i.authority=i.authority.replace("[","").replace("]","").replace(/;/g,":"),i.ipv6uri=!0),i.pathNames=vr(i,i.path),i.queryKey=Sr(i,i.query),i}function vr(r,e){const t=/\/{2,9}/g,n=e.replace(t,"/").split("/");return(e.slice(0,1)=="/"||e.length===0)&&n.splice(0,1),e.slice(-1)=="/"&&n.splice(n.length-1,1),n}function Sr(r,e){const t={};return e.replace(/(?:^|&)([^&=]*)=?([^&]*)/g,function(n,s,i){s&&(t[s]=i)}),t}const Be=typeof addEventListener=="function"&&typeof removeEventListener=="function",he=[];Be&&addEventListener("offline",()=>{he.forEach(r=>r())},!1);class I extends b{constructor(e,t){if(super(),this.binaryType=Zn,this.writeBuffer=[],this._prevBufferLen=0,this._pingInterval=-1,this._pingTimeout=-1,this._maxPayload=-1,this._pingTimeoutTime=1/0,e&&typeof e=="object"&&(t=e,e=null),e){const n=$e(e);t.hostname=n.host,t.secure=n.protocol==="https"||n.protocol==="wss",t.port=n.port,n.query&&(t.query=n.query)}else t.host&&(t.hostname=$e(t.host).host);ce(this,t),this.secure=t.secure!=null?t.secure:typeof location<"u"&&location.protocol==="https:",t.hostname&&!t.port&&(t.port=this.secure?"443":"80"),this.hostname=t.hostname||(typeof location<"u"?location.hostname:"localhost"),this.port=t.port||(typeof location<"u"&&location.port?location.port:this.secure?"443":"80"),this.transports=[],this._transportsByName={},t.transports.forEach(n=>{const s=n.prototype.name;this.transports.push(s),this._transportsByName[s]=n}),this.opts=Object.assign({path:"/engine.io",agent:!1,withCredentials:!1,upgrade:!0,timestampParam:"t",rememberUpgrade:!1,addTrailingSlash:!0,rejectUnauthorized:!0,perMessageDeflate:{threshold:1024},transportOptions:{},closeOnBeforeunload:!1},t),this.opts.path=this.opts.path.replace(/\/$/,"")+(this.opts.addTrailingSlash?"/":""),typeof this.opts.query=="string"&&(this.opts.query=or(this.opts.query)),Be&&(this.opts.closeOnBeforeunload&&(this._beforeunloadEventListener=()=>{this.transport&&(this.transport.removeAllListeners(),this.transport.close())},addEventListener("beforeunload",this._beforeunloadEventListener,!1)),this.hostname!=="localhost"&&(this._offlineEventListener=()=>{this._onClose("transport close",{description:"network connection lost"})},he.push(this._offlineEventListener))),this.opts.withCredentials&&(this._cookieJar=void 0),this._open()}createTransport(e){const t=Object.assign({},this.opts.query);t.EIO=en,t.transport=e,this.id&&(t.sid=this.id);const n=Object.assign({},this.opts,{query:t,socket:this,hostname:this.hostname,secure:this.secure,port:this.port},this.opts.transportOptions[e]);return new this._transportsByName[e](n)}_open(){if(this.transports.length===0){this.setTimeoutFn(()=>{this.emitReserved("error","No transports available")},0);return}const e=this.opts.rememberUpgrade&&I.priorWebsocketSuccess&&this.transports.indexOf("websocket")!==-1?"websocket":this.transports[0];this.readyState="opening";const t=this.createTransport(e);t.open(),this.setTransport(t)}setTransport(e){this.transport&&this.transport.removeAllListeners(),this.transport=e,e.on("drain",this._onDrain.bind(this)).on("packet",this._onPacket.bind(this)).on("error",this._onError.bind(this)).on("close",t=>this._onClose("transport close",t))}onOpen(){this.readyState="open",I.priorWebsocketSuccess=this.transport.name==="websocket",this.emitReserved("open"),this.flush()}_onPacket(e){if(this.readyState==="opening"||this.readyState==="open"||this.readyState==="closing")switch(this.emitReserved("packet",e),this.emitReserved("heartbeat"),e.type){case"open":this.onHandshake(JSON.parse(e.data));break;case"ping":this._sendPacket("pong"),this.emitReserved("ping"),this.emitReserved("pong"),this._resetPingTimeout();break;case"error":const t=new Error("server error");t.code=e.data,this._onError(t);break;case"message":this.emitReserved("data",e.data),this.emitReserved("message",e.data);break}}onHandshake(e){this.emitReserved("handshake",e),this.id=e.sid,this.transport.query.sid=e.sid,this._pingInterval=e.pingInterval,this._pingTimeout=e.pingTimeout,this._maxPayload=e.maxPayload,this.onOpen(),this.readyState!=="closed"&&this._resetPingTimeout()}_resetPingTimeout(){this.clearTimeoutFn(this._pingTimeoutTimer);const e=this._pingInterval+this._pingTimeout;this._pingTimeoutTime=Date.now()+e,this._pingTimeoutTimer=this.setTimeoutFn(()=>{this._onClose("ping timeout")},e),this.opts.autoUnref&&this._pingTimeoutTimer.unref()}_onDrain(){this.writeBuffer.splice(0,this._prevBufferLen),this._prevBufferLen=0,this.writeBuffer.length===0?this.emitReserved("drain"):this.flush()}flush(){if(this.readyState!=="closed"&&this.transport.writable&&!this.upgrading&&this.writeBuffer.length){const e=this._getWritablePackets();this.transport.send(e),this._prevBufferLen=e.length,this.emitReserved("flush")}}_getWritablePackets(){if(!(this._maxPayload&&this.transport.name==="polling"&&this.writeBuffer.length>1))return this.writeBuffer;let t=1;for(let n=0;n<this.writeBuffer.length;n++){const s=this.writeBuffer[n].data;if(s&&(t+=rr(s)),n>0&&t>this._maxPayload)return this.writeBuffer.slice(0,n);t+=2}return this.writeBuffer}_hasPingExpired(){if(!this._pingTimeoutTime)return!0;const e=Date.now()>this._pingTimeoutTime;return e&&(this._pingTimeoutTime=0,ae(()=>{this._onClose("ping timeout")},this.setTimeoutFn)),e}write(e,t,n){return this._sendPacket("message",e,t,n),this}send(e,t,n){return this._sendPacket("message",e,t,n),this}_sendPacket(e,t,n,s){if(typeof t=="function"&&(s=t,t=void 0),typeof n=="function"&&(s=n,n=null),this.readyState==="closing"||this.readyState==="closed")return;n=n||{},n.compress=n.compress!==!1;const i={type:e,data:t,options:n};this.emitReserved("packetCreate",i),this.writeBuffer.push(i),s&&this.once("flush",s),this.flush()}close(){const e=()=>{this._onClose("forced close"),this.transport.close()},t=()=>{this.off("upgrade",t),this.off("upgradeError",t),e()},n=()=>{this.once("upgrade",t),this.once("upgradeError",t)};return(this.readyState==="opening"||this.readyState==="open")&&(this.readyState="closing",this.writeBuffer.length?this.once("drain",()=>{this.upgrading?n():e()}):this.upgrading?n():e()),this}_onError(e){if(I.priorWebsocketSuccess=!1,this.opts.tryAllTransports&&this.transports.length>1&&this.readyState==="opening")return this.transports.shift(),this._open();this.emitReserved("error",e),this._onClose("transport error",e)}_onClose(e,t){if(this.readyState==="opening"||this.readyState==="open"||this.readyState==="closing"){if(this.clearTimeoutFn(this._pingTimeoutTimer),this.transport.removeAllListeners("close"),this.transport.close(),this.transport.removeAllListeners(),Be&&(this._beforeunloadEventListener&&removeEventListener("beforeunload",this._beforeunloadEventListener,!1),this._offlineEventListener)){const n=he.indexOf(this._offlineEventListener);n!==-1&&he.splice(n,1)}this.readyState="closed",this.id=null,this.emitReserved("close",e,t),this.writeBuffer=[],this._prevBufferLen=0}}}I.protocol=en;class Cr extends I{constructor(){super(...arguments),this._upgrades=[]}onOpen(){if(super.onOpen(),this.readyState==="open"&&this.opts.upgrade)for(let e=0;e<this._upgrades.length;e++)this._probe(this._upgrades[e])}_probe(e){let t=this.createTransport(e),n=!1;I.priorWebsocketSuccess=!1;const s=()=>{n||(t.send([{type:"ping",data:"probe"}]),t.once("packet",d=>{if(!n)if(d.type==="pong"&&d.data==="probe"){if(this.upgrading=!0,this.emitReserved("upgrading",t),!t)return;I.priorWebsocketSuccess=t.name==="websocket",this.transport.pause(()=>{n||this.readyState!=="closed"&&(l(),this.setTransport(t),t.send([{type:"upgrade"}]),this.emitReserved("upgrade",t),t=null,this.upgrading=!1,this.flush())})}else{const u=new Error("probe error");u.transport=t.name,this.emitReserved("upgradeError",u)}}))};function i(){n||(n=!0,l(),t.close(),t=null)}const o=d=>{const u=new Error("probe error: "+d);u.transport=t.name,i(),this.emitReserved("upgradeError",u)};function a(){o("transport closed")}function c(){o("socket closed")}function h(d){t&&d.name!==t.name&&i()}const l=()=>{t.removeListener("open",s),t.removeListener("error",o),t.removeListener("close",a),this.off("close",c),this.off("upgrading",h)};t.once("open",s),t.once("error",o),t.once("close",a),this.once("close",c),this.once("upgrading",h),this._upgrades.indexOf("webtransport")!==-1&&e!=="webtransport"?this.setTimeoutFn(()=>{n||t.open()},200):t.open()}onHandshake(e){this._upgrades=this._filterUpgrades(e.upgrades),super.onHandshake(e)}_filterUpgrades(e){const t=[];for(let n=0;n<e.length;n++)~this.transports.indexOf(e[n])&&t.push(e[n]);return t}}let kr=class extends Cr{constructor(e,t={}){const n=typeof e=="object",s=n?{...e}:{...t};(!s.transports||s.transports&&typeof s.transports[0]=="string")&&(s.transports=(s.transports||["polling","websocket","webtransport"]).map(i=>yr[i]).filter(i=>!!i)),super(n?s:e,s)}};function xr(r,e="",t){let n=r;t=t||typeof location<"u"&&location,r==null&&(r=t.protocol+"//"+t.host),typeof r=="string"&&(r.charAt(0)==="/"&&(r.charAt(1)==="/"?r=t.protocol+r:r=t.host+r),/^(https?|wss?):\/\//.test(r)||(typeof t<"u"?r=t.protocol+"//"+r:r="https://"+r),n=$e(r)),n.port||(/^(http|ws)$/.test(n.protocol)?n.port="80":/^(http|ws)s$/.test(n.protocol)&&(n.port="443")),n.path=n.path||"/";const i=n.host.indexOf(":")!==-1?"["+n.host+"]":n.host;return n.id=n.protocol+"://"+i+":"+n.port+e,n.href=n.protocol+"://"+i+(t&&t.port===n.port?"":":"+n.port),n}const Tr=typeof ArrayBuffer=="function",Rr=r=>typeof ArrayBuffer.isView=="function"?ArrayBuffer.isView(r):r.buffer instanceof ArrayBuffer,cn=Object.prototype.toString,Pr=typeof Blob=="function"||typeof Blob<"u"&&cn.call(Blob)==="[object BlobConstructor]",Er=typeof File=="function"||typeof File<"u"&&cn.call(File)==="[object FileConstructor]";function Ue(r){return Tr&&(r instanceof ArrayBuffer||Rr(r))||Pr&&r instanceof Blob||Er&&r instanceof File}function le(r,e){if(!r||typeof r!="object")return!1;if(Array.isArray(r)){for(let t=0,n=r.length;t<n;t++)if(le(r[t]))return!0;return!1}if(Ue(r))return!0;if(r.toJSON&&typeof r.toJSON=="function"&&arguments.length===1)return le(r.toJSON(),!0);for(const t in r)if(Object.prototype.hasOwnProperty.call(r,t)&&le(r[t]))return!0;return!1}function wr(r){const e=[],t=r.data,n=r;return n.data=Ne(t,e),n.attachments=e.length,{packet:n,buffers:e}}function Ne(r,e){if(!r)return r;if(Ue(r)){const t={_placeholder:!0,num:e.length};return e.push(r),t}else if(Array.isArray(r)){const t=new Array(r.length);for(let n=0;n<r.length;n++)t[n]=Ne(r[n],e);return t}else if(typeof r=="object"&&!(r instanceof Date)){const t={};for(const n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=Ne(r[n],e));return t}return r}function Mr(r,e){return r.data=ze(r.data,e),delete r.attachments,r}function ze(r,e){if(!r)return r;if(r&&r._placeholder===!0){if(typeof r.num=="number"&&r.num>=0&&r.num<e.length)return e[r.num];throw new Error("illegal attachments")}else if(Array.isArray(r))for(let t=0;t<r.length;t++)r[t]=ze(r[t],e);else if(typeof r=="object")for(const t in r)Object.prototype.hasOwnProperty.call(r,t)&&(r[t]=ze(r[t],e));return r}const Ar=["connect","connect_error","disconnect","disconnecting","newListener","removeListener"];var g;(function(r){r[r.CONNECT=0]="CONNECT",r[r.DISCONNECT=1]="DISCONNECT",r[r.EVENT=2]="EVENT",r[r.ACK=3]="ACK",r[r.CONNECT_ERROR=4]="CONNECT_ERROR",r[r.BINARY_EVENT=5]="BINARY_EVENT",r[r.BINARY_ACK=6]="BINARY_ACK"})(g||(g={}));class Ir{constructor(e){this.replacer=e}encode(e){return(e.type===g.EVENT||e.type===g.ACK)&&le(e)?this.encodeAsBinary({type:e.type===g.EVENT?g.BINARY_EVENT:g.BINARY_ACK,nsp:e.nsp,data:e.data,id:e.id}):[this.encodeAsString(e)]}encodeAsString(e){let t=""+e.type;return(e.type===g.BINARY_EVENT||e.type===g.BINARY_ACK)&&(t+=e.attachments+"-"),e.nsp&&e.nsp!=="/"&&(t+=e.nsp+","),e.id!=null&&(t+=e.id),e.data!=null&&(t+=JSON.stringify(e.data,this.replacer)),t}encodeAsBinary(e){const t=wr(e),n=this.encodeAsString(t.packet),s=t.buffers;return s.unshift(n),s}}class Fe extends b{constructor(e){super(),this.opts=Object.assign({reviver:void 0,maxAttachments:10},typeof e=="function"?{reviver:e}:e)}add(e){let t;if(typeof e=="string"){if(this.reconstructor)throw new Error("got plaintext data when reconstructing a packet");t=this.decodeString(e);const n=t.type===g.BINARY_EVENT;n||t.type===g.BINARY_ACK?(t.type=n?g.EVENT:g.ACK,this.reconstructor=new Or(t),t.attachments===0&&super.emitReserved("decoded",t)):super.emitReserved("decoded",t)}else if(Ue(e)||e.base64)if(this.reconstructor)t=this.reconstructor.takeBinaryData(e),t&&(this.reconstructor=null,super.emitReserved("decoded",t));else throw new Error("got binary data when not reconstructing a packet");else throw new Error("Unknown type: "+e)}decodeString(e){let t=0;const n={type:Number(e.charAt(0))};if(g[n.type]===void 0)throw new Error("unknown packet type "+n.type);if(n.type===g.BINARY_EVENT||n.type===g.BINARY_ACK){const i=t+1;for(;e.charAt(++t)!=="-"&&t!=e.length;);const o=e.substring(i,t);if(o!=Number(o)||e.charAt(t)!=="-")throw new Error("Illegal attachments");const a=Number(o);if(!Dr(a)||a<0)throw new Error("Illegal attachments");if(a>this.opts.maxAttachments)throw new Error("too many attachments");n.attachments=a}if(e.charAt(t+1)==="/"){const i=t+1;for(;++t&&!(e.charAt(t)===","||t===e.length););n.nsp=e.substring(i,t)}else n.nsp="/";const s=e.charAt(t+1);if(s!==""&&Number(s)==s){const i=t+1;for(;++t;){const o=e.charAt(t);if(o==null||Number(o)!=o){--t;break}if(t===e.length)break}n.id=Number(e.substring(i,t+1))}if(e.charAt(++t)){const i=this.tryParse(e.substr(t));if(Fe.isPayloadValid(n.type,i))n.data=i;else throw new Error("invalid payload")}return n}tryParse(e){try{return JSON.parse(e,this.opts.reviver)}catch{return!1}}static isPayloadValid(e,t){switch(e){case g.CONNECT:return hn(t);case g.DISCONNECT:return t===void 0;case g.CONNECT_ERROR:return typeof t=="string"||hn(t);case g.EVENT:case g.BINARY_EVENT:return Array.isArray(t)&&(typeof t[0]=="number"||typeof t[0]=="string"&&Ar.indexOf(t[0])===-1);case g.ACK:case g.BINARY_ACK:return Array.isArray(t)}}destroy(){this.reconstructor&&(this.reconstructor.finishedReconstruction(),this.reconstructor=null)}}class Or{constructor(e){this.packet=e,this.buffers=[],this.reconPack=e}takeBinaryData(e){if(this.buffers.push(e),this.buffers.length===this.reconPack.attachments){const t=Mr(this.reconPack,this.buffers);return this.finishedReconstruction(),t}return null}finishedReconstruction(){this.reconPack=null,this.buffers=[]}}const Dr=Number.isInteger||function(r){return typeof r=="number"&&isFinite(r)&&Math.floor(r)===r};function hn(r){return Object.prototype.toString.call(r)==="[object Object]"}const Lr=Object.freeze(Object.defineProperty({__proto__:null,Decoder:Fe,Encoder:Ir,get PacketType(){return g}},Symbol.toStringTag,{value:"Module"}));function R(r,e,t){return r.on(e,t),function(){r.off(e,t)}}const $r=Object.freeze({connect:1,connect_error:1,disconnect:1,disconnecting:1,newListener:1,removeListener:1});class ln extends b{constructor(e,t,n){super(),this.connected=!1,this.recovered=!1,this.receiveBuffer=[],this.sendBuffer=[],this._queue=[],this._queueSeq=0,this.ids=0,this.acks={},this.flags={},this.io=e,this.nsp=t,n&&n.auth&&(this.auth=n.auth),this._opts=Object.assign({},n),this.io._autoConnect&&this.open()}get disconnected(){return!this.connected}subEvents(){if(this.subs)return;const e=this.io;this.subs=[R(e,"open",this.onopen.bind(this)),R(e,"packet",this.onpacket.bind(this)),R(e,"error",this.onerror.bind(this)),R(e,"close",this.onclose.bind(this))]}get active(){return!!this.subs}connect(){return this.connected?this:(this.subEvents(),this.io._reconnecting||this.io.open(),this.io._readyState==="open"&&this.onopen(),this)}open(){return this.connect()}send(...e){return e.unshift("message"),this.emit.apply(this,e),this}emit(e,...t){var n,s,i;if($r.hasOwnProperty(e))throw new Error('"'+e.toString()+'" is a reserved event name');if(t.unshift(e),this._opts.retries&&!this.flags.fromQueue&&!this.flags.volatile)return this._addToQueue(t),this;const o={type:g.EVENT,data:t};if(o.options={},o.options.compress=this.flags.compress!==!1,typeof t[t.length-1]=="function"){const l=this.ids++,d=t.pop();this._registerAckCallback(l,d),o.id=l}const a=(s=(n=this.io.engine)===null||n===void 0?void 0:n.transport)===null||s===void 0?void 0:s.writable,c=this.connected&&!(!((i=this.io.engine)===null||i===void 0)&&i._hasPingExpired());return this.flags.volatile&&!a||(c?(this.notifyOutgoingListeners(o),this.packet(o)):this.sendBuffer.push(o)),this.flags={},this}_registerAckCallback(e,t){var n;const s=(n=this.flags.timeout)!==null&&n!==void 0?n:this._opts.ackTimeout;if(s===void 0){this.acks[e]=t;return}const i=this.io.setTimeoutFn(()=>{delete this.acks[e];for(let a=0;a<this.sendBuffer.length;a++)this.sendBuffer[a].id===e&&this.sendBuffer.splice(a,1);t.call(this,new Error("operation has timed out"))},s),o=(...a)=>{this.io.clearTimeoutFn(i),t.apply(this,a)};o.withError=!0,this.acks[e]=o}emitWithAck(e,...t){return new Promise((n,s)=>{const i=(o,a)=>o?s(o):n(a);i.withError=!0,t.push(i),this.emit(e,...t)})}_addToQueue(e){let t;typeof e[e.length-1]=="function"&&(t=e.pop());const n={id:this._queueSeq++,tryCount:0,pending:!1,args:e,flags:Object.assign({fromQueue:!0},this.flags)};e.push((s,...i)=>(this._queue[0],s!==null?n.tryCount>this._opts.retries&&(this._queue.shift(),t&&t(s)):(this._queue.shift(),t&&t(null,...i)),n.pending=!1,this._drainQueue())),this._queue.push(n),this._drainQueue()}_drainQueue(e=!1){if(!this.connected||this._queue.length===0)return;const t=this._queue[0];t.pending&&!e||(t.pending=!0,t.tryCount++,this.flags=t.flags,this.emit.apply(this,t.args))}packet(e){e.nsp=this.nsp,this.io._packet(e)}onopen(){typeof this.auth=="function"?this.auth(e=>{this._sendConnectPacket(e)}):this._sendConnectPacket(this.auth)}_sendConnectPacket(e){this.packet({type:g.CONNECT,data:this._pid?Object.assign({pid:this._pid,offset:this._lastOffset},e):e})}onerror(e){this.connected||this.emitReserved("connect_error",e)}onclose(e,t){this.connected=!1,delete this.id,this.emitReserved("disconnect",e,t),this._clearAcks()}_clearAcks(){Object.keys(this.acks).forEach(e=>{if(!this.sendBuffer.some(n=>String(n.id)===e)){const n=this.acks[e];delete this.acks[e],n.withError&&n.call(this,new Error("socket has been disconnected"))}})}onpacket(e){if(e.nsp===this.nsp)switch(e.type){case g.CONNECT:e.data&&e.data.sid?this.onconnect(e.data.sid,e.data.pid):this.emitReserved("connect_error",new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)"));break;case g.EVENT:case g.BINARY_EVENT:this.onevent(e);break;case g.ACK:case g.BINARY_ACK:this.onack(e);break;case g.DISCONNECT:this.ondisconnect();break;case g.CONNECT_ERROR:this.destroy();const n=new Error(e.data.message);n.data=e.data.data,this.emitReserved("connect_error",n);break}}onevent(e){const t=e.data||[];e.id!=null&&t.push(this.ack(e.id)),this.connected?this.emitEvent(t):this.receiveBuffer.push(Object.freeze(t))}emitEvent(e){if(this._anyListeners&&this._anyListeners.length){const t=this._anyListeners.slice();for(const n of t)n.apply(this,e)}super.emit.apply(this,e),this._pid&&e.length&&typeof e[e.length-1]=="string"&&(this._lastOffset=e[e.length-1])}ack(e){const t=this;let n=!1;return function(...s){n||(n=!0,t.packet({type:g.ACK,id:e,data:s}))}}onack(e){const t=this.acks[e.id];typeof t=="function"&&(delete this.acks[e.id],t.withError&&e.data.unshift(null),t.apply(this,e.data))}onconnect(e,t){this.id=e,this.recovered=t&&this._pid===t,this._pid=t,this.connected=!0,this.emitBuffered(),this._drainQueue(!0),this.emitReserved("connect")}emitBuffered(){this.receiveBuffer.forEach(e=>this.emitEvent(e)),this.receiveBuffer=[],this.sendBuffer.forEach(e=>{this.notifyOutgoingListeners(e),this.packet(e)}),this.sendBuffer=[]}ondisconnect(){this.destroy(),this.onclose("io server disconnect")}destroy(){this.subs&&(this.subs.forEach(e=>e()),this.subs=void 0),this.io._destroy(this)}disconnect(){return this.connected&&this.packet({type:g.DISCONNECT}),this.destroy(),this.connected&&this.onclose("io client disconnect"),this}close(){return this.disconnect()}compress(e){return this.flags.compress=e,this}get volatile(){return this.flags.volatile=!0,this}timeout(e){return this.flags.timeout=e,this}onAny(e){return this._anyListeners=this._anyListeners||[],this._anyListeners.push(e),this}prependAny(e){return this._anyListeners=this._anyListeners||[],this._anyListeners.unshift(e),this}offAny(e){if(!this._anyListeners)return this;if(e){const t=this._anyListeners;for(let n=0;n<t.length;n++)if(e===t[n])return t.splice(n,1),this}else this._anyListeners=[];return this}listenersAny(){return this._anyListeners||[]}onAnyOutgoing(e){return this._anyOutgoingListeners=this._anyOutgoingListeners||[],this._anyOutgoingListeners.push(e),this}prependAnyOutgoing(e){return this._anyOutgoingListeners=this._anyOutgoingListeners||[],this._anyOutgoingListeners.unshift(e),this}offAnyOutgoing(e){if(!this._anyOutgoingListeners)return this;if(e){const t=this._anyOutgoingListeners;for(let n=0;n<t.length;n++)if(e===t[n])return t.splice(n,1),this}else this._anyOutgoingListeners=[];return this}listenersAnyOutgoing(){return this._anyOutgoingListeners||[]}notifyOutgoingListeners(e){if(this._anyOutgoingListeners&&this._anyOutgoingListeners.length){const t=this._anyOutgoingListeners.slice();for(const n of t)n.apply(this,e.data)}}}function N(r){r=r||{},this.ms=r.min||100,this.max=r.max||1e4,this.factor=r.factor||2,this.jitter=r.jitter>0&&r.jitter<=1?r.jitter:0,this.attempts=0}N.prototype.duration=function(){var r=this.ms*Math.pow(this.factor,this.attempts++);if(this.jitter){var e=Math.random(),t=Math.floor(e*this.jitter*r);r=Math.floor(e*10)&1?r+t:r-t}return Math.min(r,this.max)|0},N.prototype.reset=function(){this.attempts=0},N.prototype.setMin=function(r){this.ms=r},N.prototype.setMax=function(r){this.max=r},N.prototype.setJitter=function(r){this.jitter=r};class je extends b{constructor(e,t){var n;super(),this.nsps={},this.subs=[],e&&typeof e=="object"&&(t=e,e=void 0),t=t||{},t.path=t.path||"/socket.io",this.opts=t,ce(this,t),this.reconnection(t.reconnection!==!1),this.reconnectionAttempts(t.reconnectionAttempts||1/0),this.reconnectionDelay(t.reconnectionDelay||1e3),this.reconnectionDelayMax(t.reconnectionDelayMax||5e3),this.randomizationFactor((n=t.randomizationFactor)!==null&&n!==void 0?n:.5),this.backoff=new N({min:this.reconnectionDelay(),max:this.reconnectionDelayMax(),jitter:this.randomizationFactor()}),this.timeout(t.timeout==null?2e4:t.timeout),this._readyState="closed",this.uri=e;const s=t.parser||Lr;this.encoder=new s.Encoder,this.decoder=new s.Decoder,this._autoConnect=t.autoConnect!==!1,this._autoConnect&&this.open()}reconnection(e){return arguments.length?(this._reconnection=!!e,e||(this.skipReconnect=!0),this):this._reconnection}reconnectionAttempts(e){return e===void 0?this._reconnectionAttempts:(this._reconnectionAttempts=e,this)}reconnectionDelay(e){var t;return e===void 0?this._reconnectionDelay:(this._reconnectionDelay=e,(t=this.backoff)===null||t===void 0||t.setMin(e),this)}randomizationFactor(e){var t;return e===void 0?this._randomizationFactor:(this._randomizationFactor=e,(t=this.backoff)===null||t===void 0||t.setJitter(e),this)}reconnectionDelayMax(e){var t;return e===void 0?this._reconnectionDelayMax:(this._reconnectionDelayMax=e,(t=this.backoff)===null||t===void 0||t.setMax(e),this)}timeout(e){return arguments.length?(this._timeout=e,this):this._timeout}maybeReconnectOnOpen(){!this._reconnecting&&this._reconnection&&this.backoff.attempts===0&&this.reconnect()}open(e){if(~this._readyState.indexOf("open"))return this;this.engine=new kr(this.uri,this.opts);const t=this.engine,n=this;this._readyState="opening",this.skipReconnect=!1;const s=R(t,"open",function(){n.onopen(),e&&e()}),i=a=>{this.cleanup(),this._readyState="closed",this.emitReserved("error",a),e?e(a):this.maybeReconnectOnOpen()},o=R(t,"error",i);if(this._timeout!==!1){const a=this._timeout,c=this.setTimeoutFn(()=>{s(),i(new Error("timeout")),t.close()},a);this.opts.autoUnref&&c.unref(),this.subs.push(()=>{this.clearTimeoutFn(c)})}return this.subs.push(s),this.subs.push(o),this}connect(e){return this.open(e)}onopen(){this.cleanup(),this._readyState="open",this.emitReserved("open");const e=this.engine;this.subs.push(R(e,"ping",this.onping.bind(this)),R(e,"data",this.ondata.bind(this)),R(e,"error",this.onerror.bind(this)),R(e,"close",this.onclose.bind(this)),R(this.decoder,"decoded",this.ondecoded.bind(this)))}onping(){this.emitReserved("ping")}ondata(e){try{this.decoder.add(e)}catch(t){this.onclose("parse error",t)}}ondecoded(e){ae(()=>{this.emitReserved("packet",e)},this.setTimeoutFn)}onerror(e){this.emitReserved("error",e)}socket(e,t){let n=this.nsps[e];return n?this._autoConnect&&!n.active&&n.connect():(n=new ln(this,e,t),this.nsps[e]=n),n}_destroy(e){const t=Object.keys(this.nsps);for(const n of t)if(this.nsps[n].active)return;this._close()}_packet(e){const t=this.encoder.encode(e);for(let n=0;n<t.length;n++)this.engine.write(t[n],e.options)}cleanup(){this.subs.forEach(e=>e()),this.subs.length=0,this.decoder.destroy()}_close(){this.skipReconnect=!0,this._reconnecting=!1,this.onclose("forced close")}disconnect(){return this._close()}onclose(e,t){var n;this.cleanup(),(n=this.engine)===null||n===void 0||n.close(),this.backoff.reset(),this._readyState="closed",this.emitReserved("close",e,t),this._reconnection&&!this.skipReconnect&&this.reconnect()}reconnect(){if(this._reconnecting||this.skipReconnect)return this;const e=this;if(this.backoff.attempts>=this._reconnectionAttempts)this.backoff.reset(),this.emitReserved("reconnect_failed"),this._reconnecting=!1;else{const t=this.backoff.duration();this._reconnecting=!0;const n=this.setTimeoutFn(()=>{e.skipReconnect||(this.emitReserved("reconnect_attempt",e.backoff.attempts),!e.skipReconnect&&e.open(s=>{s?(e._reconnecting=!1,e.reconnect(),this.emitReserved("reconnect_error",s)):e.onreconnect()}))},t);this.opts.autoUnref&&n.unref(),this.subs.push(()=>{this.clearTimeoutFn(n)})}}onreconnect(){const e=this.backoff.attempts;this._reconnecting=!1,this.backoff.reset(),this.emitReserved("reconnect",e)}}const H={};function de(r,e){typeof r=="object"&&(e=r,r=void 0),e=e||{};const t=xr(r,e.path||"/socket.io"),n=t.source,s=t.id,i=t.path,o=H[s]&&i in H[s].nsps,a=e.forceNew||e["force new connection"]||e.multiplex===!1||o;let c;return a?c=new je(n,e):(H[s]||(H[s]=new je(n,e)),c=H[s]),t.query&&!e.query&&(e.query=t.queryKey),c.socket(t.path,e)}Object.assign(de,{Manager:je,Socket:ln,io:de,connect:de});class Br{constructor(e){this.config=e,this.socket=null,this.callbacks={onMessage:null,onExistingPeers:null,onYouJoined:null,onUserJoined:null,onUserLeft:null,onPeerMediaStateChange:null,onError:null},this.rawListeners=new Map}connect(e){let t=this.config.socketUrl,n="/socket.io/";try{const i=new URL(this.config.socketUrl);t=`${i.protocol}//${i.host}`;const o=i.pathname.replace(/\/+$/,"");o&&(n=`${o}/socket.io/`)}catch{}this.socket=de(t,{path:n,auth:{workspace:e},transports:["websocket","polling"]}),this.socket.on("connect",()=>{console.log("Socket connected")}),this.socket.on("disconnect",()=>{console.log("Socket disconnected")}),this.socket.on("new-message",i=>{this.callbacks.onMessage&&this.callbacks.onMessage(i)}),this.socket.on("existing-peers",i=>{this.callbacks.onExistingPeers&&this.callbacks.onExistingPeers(i)}),this.socket.on("you-joined",i=>{this.callbacks.onYouJoined&&this.callbacks.onYouJoined(i)}),this.socket.on("user-joined",i=>{this.callbacks.onUserJoined&&this.callbacks.onUserJoined(i)}),this.socket.on("user-left",i=>{this.callbacks.onUserLeft&&this.callbacks.onUserLeft(i)}),this.socket.on("peer-media-state-change",i=>{this.callbacks.onPeerMediaStateChange&&this.callbacks.onPeerMediaStateChange(i)});const s=["code-changed","code-language-changed","code-executing","code-execution-result","code-state-sync","code-permission-changed","code-sandbox-toggled","whiteboard-drawing","whiteboard-cleared","whiteboard-image-uploaded","whiteboard-state-sync","whiteboard-permission-changed","whiteboard-toggled"];for(const i of s)this.socket.on(i,o=>{const a=this.rawListeners&&this.rawListeners.get(i);a&&a.forEach(c=>{try{c(o)}catch(h){console.warn(h)}})});this.socket.on("error",i=>{console.error("Socket error:",i),this.callbacks.onError&&this.callbacks.onError(i)})}joinRoom(e,t,n,s){this.socket&&this.socket.emit("join-room",{roomId:e,userId:t,userName:n,peerId:s})}emitMediaState(e,t,n,s){this.socket&&this.socket.emit("media-state-change",{roomId:e,userId:t,type:n,enabled:s})}emit(e,t){this.socket&&this.socket.emit(e,t)}onRaw(e,t){return this.rawListeners.has(e)||this.rawListeners.set(e,new Set),this.rawListeners.get(e).add(t),()=>{var n;return(n=this.rawListeners.get(e))==null?void 0:n.delete(t)}}sendMessage(e,t,n,s,i){if(this.socket){const o={roomId:e,userId:t,userName:n,message:s||""};Array.isArray(i)&&i.length&&(o.attachmentIds=i),this.socket.emit("send-message",o)}}leaveRoom(){this.socket&&this.socket.emit("leave-room")}on(e,t){this.callbacks.hasOwnProperty("on"+e.charAt(0).toUpperCase()+e.slice(1))&&(this.callbacks["on"+e.charAt(0).toUpperCase()+e.slice(1)]=t)}disconnect(){this.socket&&(this.socket.close(),this.socket=null)}}class Ur{constructor(){this.localStream=null,this.remoteStreams={}}async getLocalStream(){if(this.localStream)return this.localStream;try{return this.localStream=await navigator.mediaDevices.getUserMedia({video:!0,audio:!0}),this.localStream}catch(e){throw console.error("Failed to get media devices:",e),e}}addRemoteStream(e,t){this.remoteStreams[e]=t}removeRemoteStream(e){delete this.remoteStreams[e]}toggleVideo(){if(this.localStream){const e=this.localStream.getVideoTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}toggleAudio(){if(this.localStream){const e=this.localStream.getAudioTracks()[0];if(e)return e.enabled=!e.enabled,e.enabled}return!1}stopLocalStream(){this.localStream&&(this.localStream.getTracks().forEach(e=>e.stop()),this.localStream=null)}cleanup(){this.stopLocalStream(),this.remoteStreams={}}}class qe{constructor(){this.recorder=null,this.stream=null,this.state="idle",this.startedAt=null,this.seq=0,this.pendingUploads=[],this.callbacks={onStateChange:null,onChunk:null}}static isSupported(){return typeof window<"u"&&typeof window.MediaRecorder<"u"}pickMimeType(){const e=["video/webm;codecs=vp9,opus","video/webm;codecs=vp8,opus","video/webm","video/mp4"];for(const t of e)if(window.MediaRecorder.isTypeSupported(t))return t;return""}setState(e){this.state=e,this.callbacks.onStateChange&&this.callbacks.onStateChange(e)}on(e,t){e==="stateChange"?this.callbacks.onStateChange=t:e==="chunk"&&(this.callbacks.onChunk=t)}start(e,t={}){if(!qe.isSupported())throw new Error("MediaRecorder API not supported in this browser");if(this.state==="recording")return;if(!e)throw new Error("stream is required to start recording");const n=t.timesliceMs||15e3,s=Number.isFinite(t.videoBitsPerSecond)?t.videoBitsPerSecond:1e6,i=Number.isFinite(t.audioBitsPerSecond)?t.audioBitsPerSecond:64e3;t.onChunk&&(this.callbacks.onChunk=t.onChunk),this.stream=e,this.seq=typeof t.startSeq=="number"?t.startSeq:0,this.pendingUploads=[];const o=this.pickMimeType(),a={videoBitsPerSecond:s,audioBitsPerSecond:i};o&&(a.mimeType=o),this.recorder=new window.MediaRecorder(e,a),this.mimeType=this.recorder.mimeType||o||"video/webm",this.recorder.ondataavailable=c=>{if(!c.data||c.data.size===0)return;const h=this.seq++,l=c.data,d=this.callbacks.onChunk;if(!d)return;const u=Promise.resolve().then(()=>d(l,h)).catch(p=>{console.warn(`Recording chunk seq=${h} upload failed:`,p&&p.message)});this.pendingUploads.push(u)},this.recorder.onerror=c=>{console.error("MediaRecorder error:",c)},this.startedAt=new Date,this.recorder.start(n),this.setState("recording")}async stop(){if(this.state!=="recording"||!this.recorder)return null;await new Promise(i=>{this.recorder.onstop=i,this.recorder.stop()});const t=(await Promise.allSettled(this.pendingUploads)).filter(i=>i.status==="rejected").length,n=new Date,s={startedAt:this.startedAt,endedAt:n,mimeType:this.mimeType,totalChunks:this.seq,droppedChunks:t};return this.recorder=null,this.stream=null,this.startedAt=null,this.pendingUploads=[],this.setState("stopped"),s}isRecording(){return this.state==="recording"||this.state==="paused"}isPaused(){return this.state==="paused"}pause(){if(!this.recorder||this.state!=="recording")return!1;try{return this.recorder.pause(),this.setState("paused"),!0}catch{return!1}}resume(){if(!this.recorder||this.state!=="paused")return!1;try{return this.recorder.resume(),this.setState("recording"),!0}catch{return!1}}}class He{constructor(e={}){this.width=e.width||1280,this.height=e.height||720,this.fps=e.fps||30,this.canvas=null,this.ctx=null,this.videos=new Map,this.audioContext=null,this.audioDestination=null,this.audioSources=new Map,this.combinedStream=null,this.animationHandle=null,this.running=!1}static isSupported(){return typeof window<"u"&&typeof HTMLCanvasElement<"u"&&typeof HTMLCanvasElement.prototype.captureStream=="function"&&typeof(window.AudioContext||window.webkitAudioContext)<"u"}async start(){if(this.running)return this.combinedStream;if(!He.isSupported())throw new Error("Composite recording requires canvas.captureStream + AudioContext");this.canvas=document.createElement("canvas"),this.canvas.width=this.width,this.canvas.height=this.height,this.canvas.style.cssText="position:absolute;left:-9999px;top:-9999px;",document.body.appendChild(this.canvas),this.ctx=this.canvas.getContext("2d");const e=window.AudioContext||window.webkitAudioContext;if(this.audioContext=new e,this.audioContext.state==="suspended")try{await this.audioContext.resume()}catch{}this.audioDestination=this.audioContext.createMediaStreamDestination();const t=this.canvas.captureStream(this.fps);this.combinedStream=new MediaStream;for(const n of t.getVideoTracks())this.combinedStream.addTrack(n);for(const n of this.audioDestination.stream.getAudioTracks())this.combinedStream.addTrack(n);return this.running=!0,this.tick(),this.combinedStream}tick(){this.running&&(this.draw(),this.animationHandle=requestAnimationFrame(()=>this.tick()))}draw(){if(!this.ctx)return;this.ctx.fillStyle="#0d0d0d",this.ctx.fillRect(0,0,this.width,this.height);const e=Array.from(this.videos.values());if(e.length===0)return;const{cols:t,rows:n}=jr(e.length),s=this.width/t,i=this.height/n;e.forEach((o,a)=>{const c=a%t,h=Math.floor(a/t),l=c*s,d=h*i;this.ctx.fillStyle="#1a1a1a",this.ctx.fillRect(l,d,s,i);const u=o.videoEl.videoWidth||0,p=o.videoEl.videoHeight||0,m=o.videoTrack,_=m&&m.enabled&&m.readyState==="live";if(u>0&&p>0&&_!==!1){const mn=Math.min(s/u,i/p),yn=u*mn,bn=p*mn,ts=l+(s-yn)/2,ns=d+(i-bn)/2;try{this.ctx.drawImage(o.videoEl,ts,ns,yn,bn)}catch{}}else Fr(this.ctx,o.label||"Peer",l,d,s,i);const un=o.label||"Peer";this.ctx.font="bold 16px system-ui, sans-serif";const Xr=this.ctx.measureText(un),fn=10,Zr=6,es=Xr.width+fn*2,Ye=24,pn=l+12,gn=d+i-Ye-12;this.ctx.fillStyle="rgba(0,0,0,0.65)",this.ctx.fillRect(pn,gn,es,Ye),this.ctx.fillStyle="#fff",this.ctx.fillText(un,pn+fn,gn+Ye-Zr-2)})}addStream(e,t,n={}){if(!this.running)return;this.videos.has(e)&&this.removeStream(e);const s=document.createElement("video");s.srcObject=t,s.autoplay=!0,s.muted=!0,s.playsInline=!0,s.style.cssText="position:absolute;left:-9999px;top:-9999px;",document.body.appendChild(s),s.play().catch(()=>{});const i=t.getVideoTracks()[0]||null;this.videos.set(e,{videoEl:s,videoTrack:i,label:n.label||(n.isLocal?"You":e)});const o=t.getAudioTracks();if(o.length&&this.audioContext)try{const a=new MediaStream([o[0]]),c=this.audioContext.createMediaStreamSource(a),h=this.audioContext.createGain();h.gain.value=1,c.connect(h).connect(this.audioDestination),this.audioSources.set(e,{source:c,gain:h})}catch(a){console.warn("Compositor audio source failed for",e,a&&a.message)}}removeStream(e){const t=this.videos.get(e);t&&(t.videoEl.pause(),t.videoEl.srcObject=null,t.videoEl.remove(),this.videos.delete(e));const n=this.audioSources.get(e);if(n){try{n.source.disconnect()}catch{}try{n.gain.disconnect()}catch{}this.audioSources.delete(e)}}getStream(){return this.combinedStream}stop(){if(this.running){this.running=!1,this.animationHandle&&cancelAnimationFrame(this.animationHandle),this.animationHandle=null;for(const e of Array.from(this.videos.keys()))this.removeStream(e);this.combinedStream&&(this.combinedStream.getTracks().forEach(e=>{try{e.stop()}catch{}}),this.combinedStream=null),this.audioContext&&(this.audioContext.close().catch(()=>{}),this.audioContext=null),this.canvas&&(this.canvas.remove(),this.canvas=null,this.ctx=null)}}}function Nr(r){const e=String(r).trim().split(/\s+/).filter(Boolean);return e.length===0?"?":e.length===1?e[0].slice(0,2).toUpperCase():(e[0][0]+e[e.length-1][0]).toUpperCase()}function zr(r){let e=0;const t=String(r);for(let s=0;s<t.length;s++)e=e*31+t.charCodeAt(s)>>>0;return`hsl(${e%360}, 45%, 40%)`}function Fr(r,e,t,n,s,i){const o=t+s/2,a=n+i/2,c=Math.min(s,i)*.22;r.fillStyle=zr(e),r.beginPath(),r.arc(o,a,c,0,Math.PI*2),r.fill(),r.fillStyle="#fff",r.font=`600 ${Math.floor(c)}px system-ui, sans-serif`,r.textAlign="center",r.textBaseline="middle",r.fillText(Nr(e),o,a+1),r.textAlign="start",r.textBaseline="alphabetic"}function jr(r){if(r<=1)return{cols:1,rows:1};if(r===2)return{cols:2,rows:1};if(r<=4)return{cols:2,rows:2};if(r<=6)return{cols:3,rows:2};if(r<=9)return{cols:3,rows:3};const e=4;return{cols:e,rows:Math.ceil(r/e)}}function qr(r,e){return r.includes("pdf")||/\.pdf$/i.test(e)?"📕":r.includes("word")||/\.docx?$/i.test(e)?"📘":r.includes("sheet")||r.includes("excel")||/\.(xlsx?|csv)$/i.test(e)?"📗":r.startsWith("text/")?"📄":"📎"}class Hr{constructor(e={}){this.container=null,this.videoElements={},this.chatMessages=[],this.recording=e.recording||"off",this.screenShare=e.screenShare!==!1,this.enabledPanels=new Set(Array.isArray(e.panels)&&e.panels.length?e.panels:["chat"]),this.enabledPanels.add("chat"),this.activePanel="chat",this.panelSlots={}}mount(e){if(typeof e=="string"?this.container=document.querySelector(e):e instanceof Element&&(this.container=e),!this.container)throw new Error(`Container not found: ${e}`);this._ownershipToken=Symbol("meet-sdk-ownership"),this.container.__meetSdkOwner=this._ownershipToken,this.render()}render(){const e=this.recording==="manual"?'<button id="toggle-record" style="padding: 10px 20px; background-color: #7a3a3a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">● Record</button>':"",t=this.recording!=="off"?'<button id="toggle-pause" style="display:none; padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">⏸ Pause</button>':"",n=this.screenShare?'<button id="toggle-screenshare" style="padding: 10px 20px; background-color: #3a5a7a; border: none; border-radius: 6px; color: white; cursor: pointer; font-size: 14px;">Share Screen</button>':"",s=this.recording!=="off"?'<span id="recording-indicator" style="display:none; margin-left: 12px; color: #ff5252; font-size: 13px; font-weight: 600;"><span id="rec-dot" style="display:inline-block; width:8px; height:8px; background:#ff5252; border-radius:50%; margin-right:6px; animation: recPulse 1.2s ease-in-out infinite;"></span>REC <span id="rec-elapsed">00:00</span></span>':"",i=this.recording!=="off"?'<span id="queue-indicator" style="display:none; margin-left: 12px; color: #fbbf24; font-size: 12px;">⏳ <span id="queue-count">0</span> uploading</span>':"";this._panelWiredOnRender=!0,this.container.innerHTML=`
42
42
  <style>
43
43
  @keyframes recPulse { 0%,100%{opacity:1} 50%{opacity:0.3} }
44
44
  </style>
@@ -130,7 +130,7 @@ a=extmap-allow-mixed`)!==-1){const i=s.sdp.split(`
130
130
  ${l}
131
131
  ${h}${o.isSharing?" · presenting":""}
132
132
  ${d}
133
- </div>`}).join(""),i&&this._onParticipantPermToggle&&n.querySelectorAll("button[data-perm]").forEach(o=>{o.addEventListener("click",()=>{const a=e[parseInt(o.getAttribute("data-idx"),10)],c=o.getAttribute("data-perm");this._onParticipantPermToggle(a,c)})})}onParticipantPermToggle(e){this._onParticipantPermToggle=e}setPauseButtonVisible(e){const t=this.container&&this.container.querySelector("#toggle-pause");t&&(t.style.display=e?"inline-block":"none")}setPauseButtonState(e){const t=this.container&&this.container.querySelector("#toggle-pause");t&&(t.textContent=e?"▶ Resume":"⏸ Pause",t.style.backgroundColor=e?"#7a5a3a":"#3a5a7a")}destroy(){this.container&&(this.container.innerHTML=""),this.videoElements={},this.chatMessages=[]}}class Vr{constructor(e){this.container=e.container,this.emit=e.emit,this.canRun=e.canRun||(()=>!0),this.canEdit=e.canEdit||(()=>!0),this.userId=e.userId,this.state={code:"",language:"javascript",running:!1,output:null},this._broadcastTimer=null,this.render()}render(){this.container.innerHTML=`
133
+ </div>`}).join(""),i&&this._onParticipantPermToggle&&n.querySelectorAll("button[data-perm]").forEach(o=>{o.addEventListener("click",()=>{const a=e[parseInt(o.getAttribute("data-idx"),10)],c=o.getAttribute("data-perm");this._onParticipantPermToggle(a,c)})})}onParticipantPermToggle(e){this._onParticipantPermToggle=e}setPauseButtonVisible(e){const t=this.container&&this.container.querySelector("#toggle-pause");t&&(t.style.display=e?"inline-block":"none")}setPauseButtonState(e){const t=this.container&&this.container.querySelector("#toggle-pause");t&&(t.textContent=e?"▶ Resume":"⏸ Pause",t.style.backgroundColor=e?"#7a5a3a":"#3a5a7a")}destroy(){this.container&&this.container.__meetSdkOwner===this._ownershipToken&&(this.container.innerHTML="",this.container.__meetSdkOwner=null),this.videoElements={},this.chatMessages=[]}}class Vr{constructor(e){this.container=e.container,this.emit=e.emit,this.canRun=e.canRun||(()=>!0),this.canEdit=e.canEdit||(()=>!0),this.userId=e.userId,this.state={code:"",language:"javascript",running:!1,output:null},this._broadcastTimer=null,this.render()}render(){this.container.innerHTML=`
134
134
  <style>
135
135
  .cs-topbar { display:flex; gap:8px; padding:8px 10px; border-bottom:1px solid #333; align-items:center; }
136
136
  .cs-select, .cs-runbtn { font-size:13px; padding:6px 10px; border-radius:4px; border:1px solid #333; background:#262626; color:#eee; cursor:pointer; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nainudhin/video-meeting-sdk",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "description": "Embeddable multi-tenant video meeting SDK (PeerJS + Socket.IO)",
5
5
  "type": "module",
6
6
  "main": "./dist/meet-sdk.umd.js",