node-rtc-connection 2.0.4 → 2.0.6
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/index.cjs +1 -0
- package/index.mjs +1 -0
- package/package.json +10 -47
- package/{dist/types → types}/stun/stun-client.d.ts +2 -0
- package/dist/index.cjs +0 -32
- package/dist/index.mjs +0 -43
- package/src/crypto/der.ts +0 -205
- package/src/crypto/x509.ts +0 -146
- package/src/datachannel/RTCDataChannel.ts +0 -388
- package/src/dtls/RTCCertificate.ts +0 -396
- package/src/dtls/cipher.ts +0 -198
- package/src/dtls/connection.ts +0 -974
- package/src/dtls/prf.ts +0 -62
- package/src/dtls/protocol.ts +0 -204
- package/src/foundation/ByteBufferQueue.ts +0 -237
- package/src/foundation/RTCError.ts +0 -276
- package/src/ice/RTCIceCandidate.ts +0 -349
- package/src/ice/ice-agent.ts +0 -609
- package/src/ice/stun-message.ts +0 -260
- package/src/index.ts +0 -72
- package/src/peerconnection/RTCPeerConnection.ts +0 -430
- package/src/sctp/association.ts +0 -523
- package/src/sctp/chunks.ts +0 -350
- package/src/sctp/crc32c.ts +0 -57
- package/src/sctp/datachannel-manager.ts +0 -187
- package/src/sctp/dcep.ts +0 -94
- package/src/sdp/RTCSessionDescription.ts +0 -115
- package/src/sdp/sdp-utils.ts +0 -229
- package/src/stun/stun-client.ts +0 -936
- package/src/transport-stack.ts +0 -165
- /package/{dist/types → types}/crypto/der.d.ts +0 -0
- /package/{dist/types → types}/crypto/x509.d.ts +0 -0
- /package/{dist/types → types}/datachannel/RTCDataChannel.d.ts +0 -0
- /package/{dist/types → types}/dtls/RTCCertificate.d.ts +0 -0
- /package/{dist/types → types}/dtls/cipher.d.ts +0 -0
- /package/{dist/types → types}/dtls/connection.d.ts +0 -0
- /package/{dist/types → types}/dtls/prf.d.ts +0 -0
- /package/{dist/types → types}/dtls/protocol.d.ts +0 -0
- /package/{dist/types → types}/foundation/ByteBufferQueue.d.ts +0 -0
- /package/{dist/types → types}/foundation/RTCError.d.ts +0 -0
- /package/{dist/types → types}/ice/RTCIceCandidate.d.ts +0 -0
- /package/{dist/types → types}/ice/ice-agent.d.ts +0 -0
- /package/{dist/types → types}/ice/stun-message.d.ts +0 -0
- /package/{dist/types → types}/index.d.ts +0 -0
- /package/{dist/types → types}/peerconnection/RTCPeerConnection.d.ts +0 -0
- /package/{dist/types → types}/sctp/association.d.ts +0 -0
- /package/{dist/types → types}/sctp/chunks.d.ts +0 -0
- /package/{dist/types → types}/sctp/crc32c.d.ts +0 -0
- /package/{dist/types → types}/sctp/datachannel-manager.d.ts +0 -0
- /package/{dist/types → types}/sctp/dcep.d.ts +0 -0
- /package/{dist/types → types}/sdp/RTCSessionDescription.d.ts +0 -0
- /package/{dist/types → types}/sdp/sdp-utils.d.ts +0 -0
- /package/{dist/types → types}/transport-stack.d.ts +0 -0
package/dist/index.cjs
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";var t=require("crypto"),e=require("events"),s=require("dgram"),i=require("os");function r(t){var e=Object.create(null);return t&&Object.keys(t).forEach(function(s){if("default"!==s){var i=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,i.get?i:{enumerable:!0,get:function(){return t[s]}})}}),e.default=t,Object.freeze(e)}var n,a,o,h,c,f,l,d,u,p,E,m,I,w,g=r(t),T=r(s),B=r(i);function y(t,e,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(t):i?i.value:e.get(t)}function C(t,e,s,i,r){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?r.call(t,s):r?r.value=s:e.set(t,s),s}"function"==typeof SuppressedError&&SuppressedError;a=new WeakMap,o=new WeakMap,h=new WeakMap,n=new WeakSet,c=function(){if("production"!==process.env.NODE_ENV){let t=0;for(const e of y(this,o,"f")){if(0===e.length)throw new Error("Invariant violation: empty buffer in queue");t+=e.length}const e=t-y(this,h,"f");if(y(this,a,"f")!==e)throw new Error(`Invariant violation: size=${y(this,a,"f")}, expected=${e}`);if(0===y(this,o,"f").length){if(0!==y(this,h,"f"))throw new Error("Invariant violation: offset non-zero with empty queue")}else if(y(this,h,"f")>=y(this,o,"f")[0].length)throw new Error("Invariant violation: offset >= front buffer size")}};const A=Object.freeze({NONE:"none",DATA_CHANNEL_FAILURE:"data-channel-failure",DTLS_FAILURE:"dtls-failure",FINGERPRINT_FAILURE:"fingerprint-failure",SCTP_FAILURE:"sctp-failure",SDP_SYNTAX_ERROR:"sdp-syntax-error",HARDWARE_ENCODER_NOT_AVAILABLE:"hardware-encoder-not-available",HARDWARE_ENCODER_ERROR:"hardware-encoder-error",INVALID_STATE:"invalid-state",INVALID_MODIFICATION:"invalid-modification",INVALID_ACCESS_ERROR:"invalid-access-error",OPERATION_ERROR:"operation-error"});class R extends Error{constructor(t={},e=""){super(e),f.add(this),l.set(this,void 0),d.set(this,void 0),u.set(this,void 0),p.set(this,void 0),E.set(this,void 0),m.set(this,void 0),this.name="RTCError",Error.captureStackTrace&&Error.captureStackTrace(this,R);const s=t.errorDetail||A.NONE;if("string"!=typeof s)throw new TypeError("errorDetail must be a string");C(this,l,s,"f"),C(this,d,y(this,f,"m",I).call(this,t.sdpLineNumber,"sdpLineNumber"),"f"),C(this,u,y(this,f,"m",I).call(this,t.httpRequestStatusCode,"httpRequestStatusCode"),"f"),C(this,p,y(this,f,"m",I).call(this,t.sctpCauseCode,"sctpCauseCode"),"f"),C(this,E,y(this,f,"m",w).call(this,t.receivedAlert,"receivedAlert"),"f"),C(this,m,y(this,f,"m",w).call(this,t.sentAlert,"sentAlert"),"f")}get errorDetail(){return y(this,l,"f")}get sdpLineNumber(){return y(this,d,"f")}get httpRequestStatusCode(){return y(this,u,"f")}get sctpCauseCode(){return y(this,p,"f")}get receivedAlert(){return y(this,E,"f")}get sentAlert(){return y(this,m,"f")}toJSON(){const t={name:this.name,message:this.message,errorDetail:y(this,l,"f")};return null!==y(this,d,"f")&&(t.sdpLineNumber=y(this,d,"f")),null!==y(this,u,"f")&&(t.httpRequestStatusCode=y(this,u,"f")),null!==y(this,p,"f")&&(t.sctpCauseCode=y(this,p,"f")),null!==y(this,E,"f")&&(t.receivedAlert=y(this,E,"f")),null!==y(this,m,"f")&&(t.sentAlert=y(this,m,"f")),t}static fromNative(t){const e={errorDetail:t.error_detail||A.NONE};return void 0!==t.sctp_cause_code&&(e.sctpCauseCode=t.sctp_cause_code),new R(e,t.message||"Unknown error")}}
|
|
2
|
-
/**
|
|
3
|
-
* @file RTCIceCandidate - ICE candidate representation.
|
|
4
|
-
*
|
|
5
|
-
* Implements the W3C RTCIceCandidate interface
|
|
6
|
-
* (https://www.w3.org/TR/webrtc/#rtcicecandidate-interface).
|
|
7
|
-
*
|
|
8
|
-
* Represents an ICE (Interactive Connectivity Establishment) candidate that
|
|
9
|
-
* describes a potential way to establish a connection with a peer.
|
|
10
|
-
*
|
|
11
|
-
* @license MIT
|
|
12
|
-
* @author nmhung1210
|
|
13
|
-
*/
|
|
14
|
-
var S,N,v,U,O,D,_;l=new WeakMap,d=new WeakMap,u=new WeakMap,p=new WeakMap,E=new WeakMap,m=new WeakMap,f=new WeakSet,I=function(t,e){if(null==t)return null;const s=Number(t);if(!Number.isInteger(s))throw new TypeError(`${e} must be an integer`);return s},w=function(t,e){if(null==t)return null;const s=Number(t);if(!Number.isInteger(s)||s<0)throw new TypeError(`${e} must be an unsigned integer`);return s},R.DetailType=A;class L{constructor(t={}){if(S.add(this),N.set(this,void 0),v.set(this,void 0),U.set(this,void 0),O.set(this,void 0),D.set(this,void 0),null===t.sdpMid&&null===t.sdpMLineIndex)throw new TypeError("sdpMid and sdpMLineIndex are both null");C(this,N,t.candidate||"","f"),C(this,v,void 0!==t.sdpMid?t.sdpMid:null,"f"),C(this,U,void 0!==t.sdpMLineIndex?t.sdpMLineIndex:null,"f"),C(this,O,t.usernameFragment||null,"f"),C(this,D,y(this,S,"m",_).call(this,y(this,N,"f")),"f")}get candidate(){return y(this,N,"f")}get sdpMid(){return y(this,v,"f")}get sdpMLineIndex(){return y(this,U,"f")}get usernameFragment(){return y(this,O,"f")}get foundation(){return y(this,D,"f").foundation}get component(){return y(this,D,"f").component}get priority(){return y(this,D,"f").priority}get address(){return y(this,D,"f").address}get protocol(){return y(this,D,"f").protocol}get port(){return y(this,D,"f").port}get type(){return y(this,D,"f").type}get tcpType(){return y(this,D,"f").tcpType}get relatedAddress(){return y(this,D,"f").relatedAddress}get relatedPort(){return y(this,D,"f").relatedPort}toJSON(){const t={candidate:y(this,N,"f"),sdpMid:y(this,v,"f"),sdpMLineIndex:y(this,U,"f")};return y(this,O,"f")&&(t.usernameFragment=y(this,O,"f")),t}static fromString(t,e=null,s=0){return new L({candidate:t,sdpMid:e,sdpMLineIndex:s})}static isValid(t){if(!t||"string"!=typeof t)return!1;if(!t.startsWith("candidate:"))return!1;return t.substring(10).trim().split(/\s+/).length>=8}}N=new WeakMap,v=new WeakMap,U=new WeakMap,O=new WeakMap,D=new WeakMap,S=new WeakSet,_=function(t){const e={foundation:null,component:null,protocol:null,priority:null,address:null,port:null,type:null,tcpType:null,relatedAddress:null,relatedPort:null};if(!t||!t.startsWith("candidate:"))return e;const s=t.substring(10).trim().split(/\s+/);if(s.length<8)return e;e.foundation=s[0],e.component=s[1],e.protocol=s[2].toLowerCase(),e.priority=parseInt(s[3],10),e.address=s[4],e.port=parseInt(s[5],10),"typ"===s[6]&&(e.type=s[7]);for(let t=8;t<s.length;t+=2){const i=s[t],r=s[t+1];"raddr"===i?e.relatedAddress=r:"rport"===i?e.relatedPort=parseInt(r,10):"tcptype"===i&&(e.tcpType=r)}return e};const k=Object.freeze({BOOLEAN:1,INTEGER:2,BIT_STRING:3,OCTET_STRING:4,NULL:5,OID:6,UTF8_STRING:12,PRINTABLE_STRING:19,IA5_STRING:22,UTC_TIME:23,GENERALIZED_TIME:24,SEQUENCE:48,SET:49});function M(t){if(t<128)return Buffer.from([t]);const e=[];let s=t;for(;s>0;)e.unshift(255&s),s>>>=8;return Buffer.from([128|e.length,...e])}function b(t,e){return Buffer.concat([Buffer.from([t]),M(e.length),e])}function P(t){let e=0;for(;e<t.length-1&&0===t[e];)e++;let s=t.slice(e);return 128&s[0]&&(s=Buffer.concat([Buffer.from([0]),s])),b(k.INTEGER,s)}function W(t){const e=[];let s=t;for(;s>0;)e.unshift(255&s),s=Math.floor(s/256);return 128&e[0]&&e.unshift(0),b(k.INTEGER,Buffer.from(e))}function H(t){const e=t.split(".").map(Number);if(e.length<2)throw new Error(`Invalid OID: ${t}`);const s=[40*e[0]+e[1]];for(let t=2;t<e.length;t++){let i=e[t];const r=[127&i];for(i=Math.floor(i/128);i>0;)r.unshift(127&i|128),i=Math.floor(i/128);s.push(...r)}return b(k.OID,Buffer.from(s))}function x(t){return b(k.SEQUENCE,Buffer.concat(t))}function F(t){const e=t.getUTCFullYear(),s=(t,e=2)=>String(t).padStart(e,"0"),i=s(t.getUTCMonth()+1),r=s(t.getUTCDate()),n=s(t.getUTCHours()),a=s(t.getUTCMinutes()),o=s(t.getUTCSeconds());if(e<2050){const t=s(e%100);return b(k.UTC_TIME,Buffer.from(`${t}${i}${r}${n}${a}${o}Z`,"ascii"))}return b(k.GENERALIZED_TIME,Buffer.from(`${e}${i}${r}${n}${a}${o}Z`,"ascii"))}const K=Object.freeze({ecPublicKey:"1.2.840.10045.2.1",prime256v1:"1.2.840.10045.3.1.7",ecdsaWithSHA256:"1.2.840.10045.4.3.2",commonName:"2.5.4.3"});function G(t){const e=x([H(K.commonName),(s=t,b(k.UTF8_STRING,Buffer.from(s,"utf8")))]);var s;var i;return x([(i=[e],b(k.SET,Buffer.concat(i)))])}function $(){return x([H(K.ecdsaWithSHA256)])}function j(t,e="sha-256"){const s=e.replace("-","").toLowerCase();return g.createHash(s).update(t).digest("hex").toUpperCase().match(/.{2}/g).join(":")}var V,z,Y,q,X,Q,Z,J,tt,et,st,it,rt,nt,at,ot,ht,ct,ft,lt,dt,ut,pt;function Et(t={}){const{name:e,days:s=30}=t,{certDer:i,privateKey:r,publicKey:n,notAfter:a}=function(t={}){const e=t.commonName||`WebRTC-${g.randomBytes(8).toString("hex")}`,s=t.days||30,{publicKey:i,privateKey:r}=g.generateKeyPairSync("ec",{namedCurve:"prime256v1"}),n=i.export({type:"spki",format:"der"}),a=t.notBefore||new Date(Date.now()-864e5),o=new Date(a.getTime()+24*s*60*60*1e3),h=g.randomBytes(20);h[0]&=127,0===h[0]&&(h[0]=1);const c=G(e),f=x([(l=0,d=W(2),b(160|l,d)),P(h),$(),c,x([F(a),F(o)]),c,n]);var l,d;const u=g.sign("sha256",f,r);var p;return{certDer:x([f,$(),(p=u,b(k.BIT_STRING,Buffer.concat([Buffer.from([0]),p])))]),privateKey:r,publicKey:i,notBefore:a,notAfter:o}}({commonName:e,days:s});return{certDer:i,privateKey:r,publicKey:n,expires:a.getTime(),hash:"sha256"}}function mt(t,e="sha-256"){return j(t,e)}class It{constructor(t){V.add(this),z.set(this,void 0),Y.set(this,void 0),q.set(this,void 0),X.set(this,void 0),Q.set(this,void 0),C(this,z,t.certDer||null,"f"),C(this,Y,t.privateKey,"f"),C(this,q,t.publicKey,"f"),C(this,X,t.expires,"f"),C(this,Q,null,"f")}getCertificateDer(){return y(this,z,"f")}get expires(){return y(this,X,"f")}getFingerprints(){if(!y(this,z,"f"))throw new Error("Certificate has no DER encoding; cannot compute fingerprint");if(!y(this,Q,"f")){const t=y(this,z,"f");C(this,Q,["sha-256","sha-384","sha-512"].map(e=>({algorithm:e,value:mt(t,e)})),"f")}return y(this,Q,"f").map(t=>({...t}))}getPrivateKeyObject(){return y(this,V,"m",Z).call(this,y(this,Y,"f"),"private")}getPrivateKey(){return y(this,V,"m",Z).call(this,y(this,Y,"f"),"private").export({type:"pkcs8",format:"pem"})}getPublicKey(){return y(this,V,"m",Z).call(this,y(this,q,"f"),"public").export({type:"spki",format:"pem"})}toPEM(){const t=y(this,z,"f")?`-----BEGIN CERTIFICATE-----\n${y(this,z,"f").toString("base64").match(/.{1,64}/g).join("\n")}\n-----END CERTIFICATE-----\n`:this.getPublicKey();return{pemPrivateKey:this.getPrivateKey(),pemCertificate:t}}isExpired(){return Date.now()>y(this,X,"f")}static async generateCertificate(t={}){return new Promise((e,s)=>{try{let i;if(t.expires)i=t.expires;else{const e=t.days||30;i=Date.now()+24*e*60*60*1e3}setImmediate(()=>{try{const s=Et({name:t.name||"webrtc",days:Math.ceil((i-Date.now())/864e5),hash:t.hash||"sha256"});s.expires=i;const r=new It(s);e(r)}catch(t){s(t)}})}catch(t){s(t)}})}static fromPEM(t,e,s){if("string"!=typeof t||0===t.length)throw new TypeError("pemPrivateKey must be a non-empty string");if("string"!=typeof e||0===e.length)throw new TypeError("pemCertificate must be a non-empty string");const i=s||Date.now()+2592e6;let r=null,n=e;const a=e.match(/-----BEGIN CERTIFICATE-----([\s\S]+?)-----END CERTIFICATE-----/);return a&&(r=Buffer.from(a[1].replace(/\s/g,""),"base64"),n=g.createPublicKey(g.createPrivateKey(t))),new It({certDer:r,privateKey:t,publicKey:n,expires:i,hash:"sha256"})}static isSupportedKeyParams(t){if(!t||"object"!=typeof t)return!1;if("RSA"===t.type){const e=t.rsaModulusLength||2048;return e>=1024&&e<=4096}if("ECDSA"===t.type){const e=t.namedCurve;return["P-256","P-384","P-521"].includes(e)}return!1}}z=new WeakMap,Y=new WeakMap,q=new WeakMap,X=new WeakMap,Q=new WeakMap,V=new WeakSet,Z=function(t,e){return t&&"object"==typeof t&&t.type?t:"private"===e?g.createPrivateKey(t):g.createPublicKey(t)};const wt=Object.freeze({CONNECTING:"connecting",OPEN:"open",CLOSING:"closing",CLOSED:"closed"}),gt=Object.freeze({SEND:Symbol("rtcdatachannel:send"),RECEIVE:Symbol("rtcdatachannel:receive"),OPEN:Symbol("rtcdatachannel:open"),SET_ID:Symbol("rtcdatachannel:setId")});class Tt extends e.EventEmitter{constructor(t,e={}){if(super(),J.add(this),tt.set(this,void 0),et.set(this,void 0),st.set(this,void 0),it.set(this,void 0),rt.set(this,void 0),nt.set(this,void 0),at.set(this,void 0),ot.set(this,void 0),ht.set(this,void 0),ct.set(this,void 0),ft.set(this,void 0),lt.set(this,void 0),"string"!=typeof t)throw new TypeError("label must be a string");C(this,tt,t,"f"),C(this,et,void 0===e.ordered||e.ordered,"f"),C(this,st,e.maxPacketLifeTime||null,"f"),C(this,it,e.maxRetransmits||null,"f"),C(this,rt,e.protocol||"","f"),C(this,nt,e.negotiated||!1,"f"),C(this,at,void 0!==e.id?e.id:null,"f"),C(this,ot,wt.CONNECTING,"f"),C(this,ht,0,"f"),C(this,ct,0,"f"),C(this,ft,"arraybuffer","f"),C(this,lt,!1,"f"),this.on(gt.SET_ID,t=>{C(this,at,t,"f")}),this.on(gt.OPEN,()=>{C(this,lt,!0,"f"),y(this,J,"m",ut).call(this,wt.OPEN)}),this.on(gt.RECEIVE,(t,e)=>{y(this,J,"m",pt).call(this,t,e)})}get label(){return y(this,tt,"f")}get ordered(){return y(this,et,"f")}get maxPacketLifeTime(){return y(this,st,"f")}get maxRetransmits(){return y(this,it,"f")}get protocol(){return y(this,rt,"f")}get negotiated(){return y(this,nt,"f")}get id(){return y(this,at,"f")}get readyState(){return y(this,ot,"f")}get bufferedAmount(){return y(this,ht,"f")}get bufferedAmountLowThreshold(){return y(this,ct,"f")}set bufferedAmountLowThreshold(t){C(this,ct,t,"f")}get binaryType(){return y(this,ft,"f")}set binaryType(t){if("arraybuffer"!==t&&"blob"!==t)throw new TypeError('binaryType must be "arraybuffer" or "blob"');C(this,ft,t,"f")}get reliable(){return y(this,et,"f")&&null===y(this,st,"f")&&null===y(this,it,"f")}send(t){if(y(this,ot,"f")!==wt.OPEN)throw new Error('RTCDataChannel.readyState is not "open"');let e,s,i=0;if("string"==typeof t)e=Buffer.from(t,"utf8"),i=e.length,s=!1;else if(t instanceof ArrayBuffer)e=Buffer.from(t),i=t.byteLength,s=!0;else if(ArrayBuffer.isView(t))e=Buffer.from(t.buffer,t.byteOffset,t.byteLength),i=t.byteLength,s=!0;else{if(!Buffer.isBuffer(t))throw t&&"function"==typeof t.arrayBuffer?new Error("Blob sending not yet implemented"):new TypeError("Invalid data type");e=t,i=t.length,s=!0}if(!y(this,lt,"f"))throw new Error("Data channel not connected to a transport");C(this,ht,y(this,ht,"f")+i,"f");try{this.emit(gt.SEND,e,s),C(this,ht,Math.max(0,y(this,ht,"f")-i),"f"),y(this,J,"m",dt).call(this)}catch(t){throw C(this,ht,Math.max(0,y(this,ht,"f")-i),"f"),this.emit("error",t),t}}close(){y(this,ot,"f")!==wt.CLOSING&&y(this,ot,"f")!==wt.CLOSED&&(y(this,J,"m",ut).call(this,wt.CLOSING),setImmediate(()=>{y(this,ot,"f")===wt.CLOSING&&y(this,J,"m",ut).call(this,wt.CLOSED)}))}}var Bt,yt;tt=new WeakMap,et=new WeakMap,st=new WeakMap,it=new WeakMap,rt=new WeakMap,nt=new WeakMap,at=new WeakMap,ot=new WeakMap,ht=new WeakMap,ct=new WeakMap,ft=new WeakMap,lt=new WeakMap,J=new WeakSet,dt=function(){y(this,ht,"f")<=y(this,ct,"f")&&this.emit("bufferedamountlow")},ut=function(t){y(this,ot,"f")!==t&&(C(this,ot,t,"f"),t===wt.OPEN?this.emit("open"):t===wt.CLOSING?this.emit("closing"):t===wt.CLOSED&&this.emit("close"))},pt=function(t,e){let s;s=e?"arraybuffer"===y(this,ft,"f")?t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength):t:t.toString("utf8"),this.emit("message",{data:s})};const Ct=Object.freeze({OFFER:"offer",PRANSWER:"pranswer",ANSWER:"answer",ROLLBACK:"rollback"});class At{constructor(t={}){if(Bt.set(this,void 0),yt.set(this,void 0),C(this,Bt,t.type||null,"f"),C(this,yt,t.sdp||null,"f"),y(this,Bt,"f")&&!Object.values(Ct).includes(y(this,Bt,"f")))throw new TypeError(`Invalid SDP type: ${y(this,Bt,"f")}`)}get type(){return y(this,Bt,"f")}set type(t){if(t&&!Object.values(Ct).includes(t))throw new TypeError(`Invalid SDP type: ${t}`);C(this,Bt,t,"f")}get sdp(){return y(this,yt,"f")}set sdp(t){C(this,yt,t,"f")}toJSON(){return{type:y(this,Bt,"f"),sdp:y(this,yt,"f")}}}function Rt(t){const{iceUfrag:e,icePwd:s,fingerprint:i,setup:r="actpass",candidates:n=[],sctpPort:a=5e3,maxMessageSize:o=262144}=t,h=[];h.push("v=0");const c=g.randomBytes(4).readUInt32BE(0);h.push(`o=- ${c} 2 IN IP4 127.0.0.1`),h.push("s=-"),h.push("t=0 0"),h.push("a=group:BUNDLE 0"),h.push("a=msid-semantic: WMS"),h.push("m=application 9 UDP/DTLS/SCTP webrtc-datachannel"),h.push("c=IN IP4 0.0.0.0"),h.push("a=ice-ufrag:"+e),h.push("a=ice-pwd:"+s),h.push("a=ice-options:trickle"),i&&h.push(`a=fingerprint:${i.algorithm} ${i.value}`),h.push(`a=setup:${r}`),h.push("a=mid:0"),h.push("a=sctp-port:"+a),h.push("a=max-message-size:"+o);for(const t of n){const e=t.sdp||t.candidate;e&&h.push("a="+(e.startsWith("candidate:")?e:"candidate:"+e))}return h.join("\r\n")+"\r\n"}function St(t){const e=(t.startsWith("candidate:")?t.slice(10):t).split(/\s+/);return e.length<8?null:{candidate:t.startsWith("candidate:")?t:"candidate:"+t,foundation:e[0],component:parseInt(e[1],10),protocol:e[2].toLowerCase(),priority:parseInt(e[3],10)>>>0,address:e[4],port:parseInt(e[5],10),type:e[7]}}var Nt,vt,Ut;Bt=new WeakMap,yt=new WeakMap;const Ot=554869826,Dt=Object.freeze({BINDING_REQUEST:1,BINDING_SUCCESS:257,BINDING_ERROR:273}),_t=Object.freeze({MAPPED_ADDRESS:1,USERNAME:6,MESSAGE_INTEGRITY:8,ERROR_CODE:9,XOR_MAPPED_ADDRESS:32,PRIORITY:36,USE_CANDIDATE:37,FINGERPRINT:32808,ICE_CONTROLLED:32809,ICE_CONTROLLING:32810});function Lt(t){return t+3&-4}const kt=(()=>{const t=new Uint32Array(256);for(let e=0;e<256;e++){let s=e;for(let t=0;t<8;t++)s=1&s?3988292384^s>>>1:s>>>1;t[e]=s>>>0}return t})();class Mt{constructor(t,e){Nt.add(this),this.type=t,this.transactionId=e||g.randomBytes(12),this.attrs=[]}addAttr(t,e){return this.attrs.push({type:t,value:e}),this}addUsername(t){return this.addAttr(_t.USERNAME,Buffer.from(t,"utf8"))}addPriority(t){const e=Buffer.alloc(4);return e.writeUInt32BE(t>>>0,0),this.addAttr(_t.PRIORITY,e)}addIceControlling(t){return this.addAttr(_t.ICE_CONTROLLING,t)}addIceControlled(t){return this.addAttr(_t.ICE_CONTROLLED,t)}addUseCandidate(){return this.addAttr(_t.USE_CANDIDATE,Buffer.alloc(0))}addXorMappedAddress(t,e){return this.addAttr(_t.XOR_MAPPED_ADDRESS,function(t,e){const s=Buffer.alloc(8);s.writeUInt8(0,0),s.writeUInt8(1,1),s.writeUInt16BE(8466^e,2);const i=t.split(".").map(Number),r=(i[0]<<24|i[1]<<16|i[2]<<8|i[3])>>>0;return s.writeUInt32BE((r^Ot)>>>0,4),s}(t,e,this.transactionId))}build(t){let e=y(this,Nt,"m",vt).call(this);if(t){const s=e.length+24,i=y(this,Nt,"m",Ut).call(this,s),r=g.createHmac("sha1",Buffer.from(t,"utf8")).update(Buffer.concat([i,e])).digest(),n=Buffer.alloc(4);n.writeUInt16BE(_t.MESSAGE_INTEGRITY,0),n.writeUInt16BE(20,2),e=Buffer.concat([e,n,r])}const s=e.length+8,i=y(this,Nt,"m",Ut).call(this,s),r=(1398035790^function(t){let e=4294967295;for(let s=0;s<t.length;s++)e=kt[255&(e^t[s])]^e>>>8;return(4294967295^e)>>>0}(Buffer.concat([i,e])))>>>0,n=Buffer.alloc(8);return n.writeUInt16BE(_t.FINGERPRINT,0),n.writeUInt16BE(4,2),n.writeUInt32BE(r,4),e=Buffer.concat([e,n]),Buffer.concat([y(this,Nt,"m",Ut).call(this,e.length),e])}}var bt,Pt,Wt,Ht,xt,Ft,Kt,Gt,$t,jt,Vt,zt,Yt,qt,Xt,Qt,Zt,Jt,te,ee,se,ie,re,ne;Nt=new WeakSet,vt=function(){const t=[];for(const e of this.attrs){const s=Buffer.alloc(4);s.writeUInt16BE(e.type,0),s.writeUInt16BE(e.value.length,2);const i=Buffer.alloc(Lt(e.value.length));e.value.copy(i,0),t.push(s,i)}return Buffer.concat(t)},Ut=function(t){const e=Buffer.alloc(20);return e.writeUInt16BE(this.type,0),e.writeUInt16BE(t,2),e.writeUInt32BE(Ot,4),this.transactionId.copy(e,8),e};const ae=1,oe=257,he=3,ce=259,fe=4,le=260,de=22,ue=23,pe=8,Ee=264,me=265,Ie=1,we=6,ge=8,Te=9,Be=20,ye=21,Ce=32,Ae=13,Re=18,Se=19,Ne=22,ve=25,Ue=554869826;class Oe extends e.EventEmitter{constructor(t){super(),bt.add(this),Pt.set(this,void 0),Wt.set(this,void 0),Ht.set(this,void 0),xt.set(this,void 0),Ft.set(this,void 0),Kt.set(this,void 0),Gt.set(this,void 0),$t.set(this,void 0),C(this,Pt,t.server,"f"),C(this,Wt,t.port,"f"),C(this,Ht,t.username,"f"),C(this,xt,t.credential,"f"),C(this,Ft,null,"f"),C(this,Kt,new Map,"f"),C(this,Gt,null,"f"),C(this,$t,null,"f")}async connect(){if(!y(this,Ft,"f"))return new Promise((t,e)=>{const s=T.createSocket("udp4");C(this,Ft,s,"f"),s.on("message",(t,e)=>{y(this,bt,"m",ee).call(this,t,e)}),s.on("error",t=>{console.error("STUN socket error:",t),e(t)}),s.bind(()=>{t()})})}async getReflexiveAddress(){await this.connect();const t=g.randomBytes(12),e=y(this,bt,"m",zt).call(this,t);return new Promise((s,i)=>{const r=setTimeout(()=>{y(this,Kt,"f").delete(t.toString("hex")),i(new Error("STUN request timeout"))},5e3);y(this,Kt,"f").set(t.toString("hex"),{resolve:t=>{clearTimeout(r),s(t)},reject:t=>{clearTimeout(r),i(t)}}),y(this,Ft,"f").send(e,y(this,Wt,"f"),y(this,Pt,"f"),e=>{e&&(clearTimeout(r),y(this,Kt,"f").delete(t.toString("hex")),i(e))})})}async allocateRelay(t=600){if(!y(this,Ht,"f")||!y(this,xt,"f"))throw new Error("TURN requires username and credential");await this.connect();let e=g.randomBytes(12),s=y(this,bt,"m",Yt).call(this,e,t);try{return await y(this,bt,"m",Vt).call(this,s,e,"allocate")}catch(i){if(i instanceof Error&&i.message.includes("401")&&y(this,Gt,"f")&&y(this,$t,"f"))return e=g.randomBytes(12),s=y(this,bt,"m",Yt).call(this,e,t,!0),await y(this,bt,"m",Vt).call(this,s,e,"allocate");throw i}}async refreshAllocation(t=600){if(!y(this,Ht,"f")||!y(this,xt,"f"))throw new Error("TURN requires username and credential");return y(this,bt,"m",jt).call(this,"refresh",()=>{const e=g.randomBytes(12);return{transactionId:e,request:y(this,bt,"m",Zt).call(this,e,t)}})}async createPermission(t){if(!y(this,Ht,"f")||!y(this,xt,"f"))throw new Error("TURN requires username and credential");await y(this,bt,"m",jt).call(this,"createPermission",()=>{const e=g.randomBytes(12);return{transactionId:e,request:y(this,bt,"m",qt).call(this,e,t)}})}async sendIndication(t,e,s){if(!y(this,Ht,"f")||!y(this,xt,"f"))throw new Error("TURN requires username and credential");const i=g.randomBytes(12),r=y(this,bt,"m",Xt).call(this,i,t,e,s);return new Promise((t,e)=>{y(this,Ft,"f").send(r,y(this,Wt,"f"),y(this,Pt,"f"),s=>{s?e(s):t()})})}close(){y(this,Ft,"f")&&(y(this,Ft,"f").close(),C(this,Ft,null,"f")),y(this,Kt,"f").clear()}}var De,_e,Le,ke,Me,be,Pe,We,He,xe,Fe,Ke,Ge,$e,je,Ve,ze,Ye,qe,Xe,Qe,Ze,Je,ts,es,ss,is,rs,ns,as,os,hs;Pt=new WeakMap,Wt=new WeakMap,Ht=new WeakMap,xt=new WeakMap,Ft=new WeakMap,Kt=new WeakMap,Gt=new WeakMap,$t=new WeakMap,bt=new WeakSet,jt=async function(t,e){const s=e();try{return await y(this,bt,"m",Vt).call(this,s.request,s.transactionId,t)}catch(s){if(s instanceof Error&&s.message.includes("401")&&y(this,Gt,"f")&&y(this,$t,"f")){const s=e();return y(this,bt,"m",Vt).call(this,s.request,s.transactionId,t)}throw s}},Vt=function(t,e,s){return new Promise((i,r)=>{const n=setTimeout(()=>{y(this,Kt,"f").delete(e.toString("hex")),r(new Error(`${s} request timeout`))},5e3);y(this,Kt,"f").set(e.toString("hex"),{type:s,resolve:t=>{clearTimeout(n),i(t)},reject:t=>{clearTimeout(n),r(t)}}),y(this,Ft,"f").send(t,y(this,Wt,"f"),y(this,Pt,"f"),t=>{t&&(clearTimeout(n),y(this,Kt,"f").delete(e.toString("hex")),r(t))})})},zt=function(t){const e=Buffer.alloc(20);return e.writeUInt16BE(ae,0),e.writeUInt16BE(0,2),e.writeUInt32BE(Ue,4),t.copy(e,8),e},Yt=function(t,e,s=!1){const i=[],r=Buffer.alloc(8);r.writeUInt16BE(ve,0),r.writeUInt16BE(4,2),r.writeUInt8(17,4),i.push(r);const n=Buffer.alloc(8);if(n.writeUInt16BE(Ae,0),n.writeUInt16BE(4,2),n.writeUInt32BE(e,4),i.push(n),s&&y(this,Gt,"f")&&y(this,$t,"f")){const t=y(this,bt,"m",te).call(this,we,y(this,Ht,"f"));i.push(t);const e=y(this,bt,"m",te).call(this,Be,y(this,Gt,"f"));i.push(e);const s=y(this,bt,"m",te).call(this,ye,y(this,$t,"f"));i.push(s)}return y(this,bt,"m",Jt).call(this,he,t,i,s)},qt=function(t,e){const s=[],i=y(this,bt,"m",Qt).call(this,e,0,t);return s.push(i),y(this,Gt,"f")&&y(this,$t,"f")&&(s.push(y(this,bt,"m",te).call(this,we,y(this,Ht,"f"))),s.push(y(this,bt,"m",te).call(this,Be,y(this,Gt,"f"))),s.push(y(this,bt,"m",te).call(this,ye,y(this,$t,"f")))),y(this,bt,"m",Jt).call(this,pe,t,s,!0)},Xt=function(t,e,s,i){const r=[],n=y(this,bt,"m",Qt).call(this,e,s,t);r.push(n);const a=Buffer.alloc(4+i.length+(4-i.length%4)%4);return a.writeUInt16BE(Se,0),a.writeUInt16BE(i.length,2),i.copy(a,4),r.push(a),y(this,bt,"m",Jt).call(this,de,t,r,!1)},Qt=function(t,e,s){const i=Buffer.alloc(12);i.writeUInt16BE(Re,0),i.writeUInt16BE(8,2),i.writeUInt8(0,4),i.writeUInt8(1,5);const r=8466^e;i.writeUInt16BE(r,6);const n=t.split(".").map(Number),a=(n[0]<<24|n[1]<<16|n[2]<<8|n[3])^Ue;return i.writeUInt32BE(a>>>0,8),i},Zt=function(t,e){const s=[],i=Buffer.alloc(8);i.writeUInt16BE(Ae,0),i.writeUInt16BE(4,2),i.writeUInt32BE(e,4),s.push(i);const r=y(this,bt,"m",te).call(this,we,y(this,Ht,"f"));if(s.push(r),y(this,Gt,"f")){const t=y(this,bt,"m",te).call(this,Be,y(this,Gt,"f"));s.push(t)}if(y(this,$t,"f")){const t=y(this,bt,"m",te).call(this,ye,y(this,$t,"f"));s.push(t)}return y(this,bt,"m",Jt).call(this,fe,t,s,!0)},Jt=function(t,e,s,i=!1){let r=Buffer.concat(s);if(i&&y(this,xt,"f")){const s=Buffer.alloc(20);s.writeUInt16BE(t,0),s.writeUInt16BE(r.length+24,2),s.writeUInt32BE(Ue,4),e.copy(s,8);const i=Buffer.concat([s,r]);let n=y(this,xt,"f");if(y(this,Ht,"f")&&y(this,Gt,"f")){const t=`${y(this,Ht,"f")}:${y(this,Gt,"f")}:${y(this,xt,"f")}`;n=g.createHash("md5").update(t).digest()}const a=g.createHmac("sha1",n);a.update(i);const o=a.digest(),h=Buffer.alloc(4+o.length);h.writeUInt16BE(ge,0),h.writeUInt16BE(o.length,2),o.copy(h,4),r=Buffer.concat([r,h])}const n=Buffer.alloc(20);return n.writeUInt16BE(t,0),n.writeUInt16BE(r.length,2),n.writeUInt32BE(Ue,4),e.copy(n,8),Buffer.concat([n,r])},te=function(t,e){const s=Buffer.from(e,"utf8"),i=s.length,r=(4-i%4)%4,n=Buffer.alloc(4+i+r);return n.writeUInt16BE(t,0),n.writeUInt16BE(i,2),s.copy(n,4),n},ee=function(t,e){if(t.length<20)return;const s=t.readUInt16BE(0),i=t.readUInt16BE(2),r=t.readUInt32BE(4),n=t.slice(8,20);if(r!==Ue)return;if(s===ue){const e=y(this,bt,"m",se).call(this,t.slice(20,20+i),n);if(e.xorPeerAddress&&e.data){const t={address:e.xorPeerAddress.address,port:e.xorPeerAddress.port,family:e.xorPeerAddress.family||"IPv4"};this.emit("data",e.data,t)}return}const a=n.toString("hex"),o=y(this,Kt,"f").get(a);if(!o)return;const h=y(this,bt,"m",se).call(this,t.slice(20,20+i),n);if(s===oe)h.xorMappedAddress?o.resolve({address:h.xorMappedAddress.address,port:h.xorMappedAddress.port,family:h.xorMappedAddress.family}):h.mappedAddress?o.resolve({address:h.mappedAddress.address,port:h.mappedAddress.port,family:h.mappedAddress.family}):o.reject(new Error("No mapped address in STUN response")),y(this,Kt,"f").delete(a);else if(s===ce)h.xorRelayedAddress?o.resolve({relayedAddress:h.xorRelayedAddress.address,relayedPort:h.xorRelayedAddress.port,lifetime:h.lifetime||600,type:"relay"}):o.reject(new Error("No relayed address in ALLOCATE response")),y(this,Kt,"f").delete(a);else if(s===le)o.resolve({lifetime:h.lifetime||600}),y(this,Kt,"f").delete(a);else if(s===Ee||s===me)o.resolve({ok:!0}),y(this,Kt,"f").delete(a);else if(!(272&~s)){h.realm&&C(this,Gt,h.realm,"f"),h.nonce&&C(this,$t,h.nonce,"f");const t=h.errorCode||"Unknown error";o.reject(new Error(`STUN error: ${t}`)),y(this,Kt,"f").delete(a)}},se=function(t,e){const s={};let i=0;for(;i<t.length&&!(i+4>t.length);){const r=t.readUInt16BE(i),n=t.readUInt16BE(i+2);if(i+=4,i+n>t.length)break;const a=t.slice(i,i+n);switch(r){case Ce:s.xorMappedAddress=y(this,bt,"m",ie).call(this,a,e);break;case Ne:s.xorRelayedAddress=y(this,bt,"m",ie).call(this,a,e);break;case Re:s.xorPeerAddress=y(this,bt,"m",ie).call(this,a,e);break;case Se:s.data=a;break;case Ie:s.mappedAddress=y(this,bt,"m",re).call(this,a);break;case Ae:s.lifetime=a.readUInt32BE(0);break;case Te:s.errorCode=y(this,bt,"m",ne).call(this,a);break;case Be:s.realm=a.toString("utf8"),C(this,Gt,s.realm,"f");break;case ye:s.nonce=a.toString("utf8"),C(this,$t,s.nonce,"f")}i+=n;i+=(4-n%4)%4}return s},ie=function(t,e){const s=t.readUInt8(1),i=8466^t.readUInt16BE(2);if(1===s){const e=t.readUInt32BE(4)^Ue;return{family:"IPv4",port:i,address:[e>>24&255,e>>16&255,e>>8&255,255&e].join(".")}}return null},re=function(t){const e=t.readUInt8(1),s=t.readUInt16BE(2);if(1===e){const e=t.slice(4,8);return{family:"IPv4",port:s,address:Array.from(e).join(".")}}return null},ne=function(t){return`${100*(7&t.readUInt8(2))+t.readUInt8(3)} ${t.slice(4).toString("utf8")}`};const cs={host:126,srflx:100,prflx:110,relay:0};function fs(t){const e=t.match(/^(stuns?|turns?):\/?\/?([^:?]+):?(\d+)?(?:\?(.+))?$/);if(!e)return null;const s=e[1],i=e[2],r={};if(e[4])for(const t of e[4].split("&")){if(!t)continue;const e=t.indexOf("=");-1===e?r[t]=!0:r[t.slice(0,e)]=t.slice(e+1)}const n="turns"===s||"stuns"===s?5349:3478;return{scheme:s,protocol:s,host:i,port:parseInt(e[3]||String(n),10),transport:"string"==typeof r.transport?r.transport:"udp",params:r}}class ls{constructor(t){De.set(this,void 0),this.kind="host",C(this,De,t,"f"),this.onMessage=null,t.on("message",(t,e)=>{this.onMessage&&this.onMessage(t,{address:e.address,port:e.port})})}send(t,e,s){y(this,De,"f").send(t,s,e)}close(){try{y(this,De,"f").close()}catch(t){}}}De=new WeakMap;class ds{constructor(t){_e.set(this,void 0),Le.set(this,void 0),this.kind="relay",C(this,_e,t,"f"),this.onMessage=null,C(this,Le,new Set,"f"),t.on("data",(t,e)=>{this.onMessage&&this.onMessage(t,{address:e.address,port:e.port})})}send(t,e,s){const i=`${e}:${s}`;y(this,Le,"f").has(i)?y(this,_e,"f").sendIndication(e,s,t).catch(()=>{}):(y(this,Le,"f").add(i),y(this,_e,"f").createPermission(e).then(()=>y(this,_e,"f").sendIndication(e,s,t)).catch(()=>{}))}close(){try{y(this,_e,"f").close()}catch(t){}}}_e=new WeakMap,Le=new WeakMap;class us extends e.EventEmitter{constructor(t){super(),ke.add(this),Me.set(this,void 0),be.set(this,void 0),Pe.set(this,void 0),We.set(this,void 0),He.set(this,void 0),xe.set(this,void 0),Fe.set(this,void 0),Ke.set(this,void 0),Ge.set(this,void 0),$e.set(this,void 0),je.set(this,void 0),this.role=t.role,this.localUfrag=t.localUfrag,this.localPwd=t.localPwd,this.remoteUfrag=null,this.remotePwd=null,C(this,Me,g.randomBytes(8),"f"),C(this,be,[],"f"),C(this,Pe,[],"f"),C(this,We,[],"f"),C(this,He,[],"f"),C(this,xe,null,"f"),C(this,Fe,!1,"f"),C(this,Ke,null,"f"),C(this,Ge,null,"f"),C(this,$e,!1,"f"),C(this,je,new Map,"f")}async gather(t={}){const e=t.iceServers||[],s="relay"===t.iceTransportPolicy,i=await y(this,ke,"m",Ve).call(this);for(const t of e){const e=Array.isArray(t.urls)?t.urls:[t.urls];for(const r of e){const e=fs(r);if(e&&"udp"===e.transport)try{"stun"!==e.scheme||s?"turn"!==e.scheme&&"turns"!==e.scheme||await y(this,ke,"m",qe).call(this,e,t):await y(this,ke,"m",Ye).call(this,e,i[0])}catch(t){this.emit("gathererror",{url:r,error:t instanceof Error?t.message:String(t)})}}}s&&C(this,Pe,y(this,Pe,"f").filter(t=>"relay"===t.type),"f"),this.emit("gatheringcomplete")}getLocalCandidates(){return y(this,Pe,"f").slice()}setRemoteCredentials(t,e){this.remoteUfrag=t,this.remotePwd=e}addRemoteCandidate(t){t&&t.address&&t.port&&("string"==typeof t.address&&t.address.endsWith(".local")||(y(this,We,"f").push(t),y(this,ke,"m",Qe).call(this),!y(this,Ke,"f")&&this.remotePwd&&y(this,ke,"m",Ze).call(this)))}start(){this.remotePwd&&y(this,We,"f").length>0&&y(this,ke,"m",Ze).call(this)}send(t){if(!y(this,xe,"f"))throw new Error("ICE not connected");y(this,xe,"f").local.transport.send(t,y(this,xe,"f").remote.address,y(this,xe,"f").remote.port)}getSelectedPair(){return y(this,xe,"f")}getSelectedCandidateType(){return y(this,xe,"f")?y(this,xe,"f").local.type:null}close(){if(!y(this,Fe,"f")){C(this,Fe,!0,"f"),y(this,ke,"m",Je).call(this);for(const t of y(this,be,"f"))try{t.close()}catch(t){}C(this,be,[],"f"),this.emit("closed")}}}Me=new WeakMap,be=new WeakMap,Pe=new WeakMap,We=new WeakMap,He=new WeakMap,xe=new WeakMap,Fe=new WeakMap,Ke=new WeakMap,Ge=new WeakMap,$e=new WeakMap,je=new WeakMap,ke=new WeakSet,Ve=async function(){const t=B.networkInterfaces(),e=[];for(const s of Object.values(t))if(s)for(const t of s)"IPv4"!==t.family||t.internal||e.push(t.address);0===e.length&&e.push("127.0.0.1");const s=[];for(const t of e)s.push(await y(this,ke,"m",ze).call(this,t));return s},ze=function(t){return new Promise((e,s)=>{const i=T.createSocket("udp4");i.on("error",t=>this.emit("error",t)),i.bind(0,t,()=>{const{port:s}=i.address(),r=new ls(i);r.onMessage=(t,e)=>y(this,ke,"m",ss).call(this,r,t,e),y(this,be,"f").push(r);const n=y(this,ke,"m",Xe).call(this,"host",t,s,r);e({socket:i,address:t,port:s,transport:r,candidate:n})})})},Ye=async function(t,e){if(!e)return;const s=new Oe({server:t.host,port:t.port});try{const t=await s.getReflexiveAddress();y(this,ke,"m",Xe).call(this,"srflx",t.address,t.port,e.transport,{relatedAddress:e.address,relatedPort:e.port})}finally{s.close()}},qe=async function(t,e){if(!e.username||!e.credential)throw new Error("TURN server requires username and credential");const s=new Oe({server:t.host,port:t.port,username:e.username,credential:e.credential,transport:t.transport}),i=await s.allocateRelay(600),r=new ds(s);r.onMessage=(t,e)=>y(this,ke,"m",ss).call(this,r,t,e),y(this,be,"f").push(r),y(this,ke,"m",Xe).call(this,"relay",i.relayedAddress,i.relayedPort,r,{relatedAddress:t.host,relatedPort:t.port})},Xe=function(t,e,s,i,r={}){const n=g.createHash("md5").update(`${t}:${e}:${i.kind}`).digest("hex").slice(0,8),a=function(t,e=65535,s=1){return(cs[t]<<24)+(e<<8)+(256-s)>>>0}(t);let o=`candidate:${n} 1 udp ${a} ${e} ${s} typ ${t}`;r.relatedAddress&&(o+=` raddr ${r.relatedAddress} rport ${r.relatedPort}`);const h={foundation:n,component:1,protocol:"udp",priority:a,address:e,port:s,type:t,transport:i,sdp:o};return y(this,Pe,"f").push(h),this.emit("candidate",h),h},Qe=function(){for(const t of y(this,Pe,"f"))for(const e of y(this,We,"f")){const s=`${t.type}:${t.address}:${t.port}->${e.address}:${e.port}`;y(this,He,"f").find(t=>t.key===s)||y(this,He,"f").push({key:s,local:t,remote:e,state:"frozen",nominated:!1})}},Ze=function(){y(this,Ke,"f")||y(this,Fe,"f")||(C(this,Ke,setInterval(()=>y(this,ke,"m",ts).call(this),50),"f"),y(this,Ke,"f").unref&&y(this,Ke,"f").unref(),C(this,Ge,setTimeout(()=>{y(this,$e,"f")||this.emit("failed"),y(this,ke,"m",Je).call(this)},1e4),"f"),y(this,Ge,"f").unref&&y(this,Ge,"f").unref(),y(this,ke,"m",ts).call(this))},Je=function(){y(this,Ke,"f")&&(clearInterval(y(this,Ke,"f")),C(this,Ke,null,"f")),y(this,Ge,"f")&&(clearTimeout(y(this,Ge,"f")),C(this,Ge,null,"f"))},ts=function(){if(!y(this,Fe,"f"))for(const t of y(this,He,"f"))"succeeded"!==t.state&&y(this,ke,"m",es).call(this,t)},es=function(t){const e=g.randomBytes(12),s=`${this.remoteUfrag}:${this.localUfrag}`,i=new Mt(Dt.BINDING_REQUEST,e).addUsername(s).addPriority(t.local.priority);"controlling"===this.role?(i.addIceControlling(y(this,Me,"f")),i.addUseCandidate()):i.addIceControlled(y(this,Me,"f"));const r=i.build(this.remotePwd??void 0);y(this,je,"f").set(e.toString("hex"),t),t.state="in-progress",t.local.transport.send(r,t.remote.address,t.remote.port)},ss=function(t,e,s){if(0===e.length)return;e[0]<=3?y(this,ke,"m",is).call(this,t,e,s):this.emit("data",e,{transport:t,address:s.address,port:s.port})},is=function(t,e,s){const i=function(t){if(t.length<20)return null;if(t.readUInt32BE(4)!==Ot)return null;const e=t.readUInt16BE(0),s=t.readUInt16BE(2);if(20+s>t.length)return null;const i=t.slice(8,20),r=new Map;let n=20;const a=20+s;for(;n+4<=a;){const e=t.readUInt16BE(n),s=t.readUInt16BE(n+2);if(n+=4,n+s>a)break;r.set(e,t.slice(n,n+s)),n+=Lt(s)}return{type:e,transactionId:i,attrs:r,raw:t}}(e);i&&(i.type===Dt.BINDING_REQUEST?y(this,ke,"m",rs).call(this,t,i,s):i.type===Dt.BINDING_SUCCESS&&y(this,ke,"m",ns).call(this,t,i,s))},rs=function(t,e,s){if(this.localPwd&&!function(t,e){let s=20;const i=20+t.readUInt16BE(2);let r=-1;for(;s+4<=i;){const e=t.readUInt16BE(s),i=t.readUInt16BE(s+2);if(e===_t.MESSAGE_INTEGRITY){r=s;break}s+=4+Lt(i)}if(r<0)return!1;const n=t.slice(r+4,r+4+20),a=r+24-20,o=Buffer.from(t.slice(0,20));o.writeUInt16BE(a,2);const h=g.createHmac("sha1",Buffer.from(e,"utf8")).update(Buffer.concat([o,t.slice(20,r)])).digest();return n.length===h.length&&g.timingSafeEqual(n,h)}(e.raw,this.localPwd))return;const i=new Mt(Dt.BINDING_SUCCESS,e.transactionId).addXorMappedAddress(s.address,s.port).build(this.localPwd);t.send(i,s.address,s.port);y(this,We,"f").find(t=>t.address===s.address&&t.port===s.port)||this.addRemoteCandidate({address:s.address,port:s.port,type:"prflx",priority:0});const r=e.attrs.has(_t.USE_CANDIDATE),n=y(this,ke,"m",as).call(this,t,s);r&&"controlled"===this.role&&y(this,ke,"m",hs).call(this,n||y(this,ke,"m",os).call(this,t,s))},ns=function(t,e,s){const i=y(this,je,"f").get(e.transactionId.toString("hex"));i&&(y(this,je,"f").delete(e.transactionId.toString("hex")),i.state="succeeded","controlling"===this.role&&y(this,ke,"m",hs).call(this,i))},as=function(t,e){return y(this,He,"f").find(s=>s.remote.address===e.address&&s.remote.port===e.port&&s.local.transport===t)},os=function(t,e){return{local:{transport:t},remote:{address:e.address,port:e.port}}},hs=function(t){!y(this,xe,"f")&&t&&(C(this,xe,t,"f"),C(this,$e,!0,"f"),y(this,ke,"m",Je).call(this),this.emit("selected",{transport:t.local.transport,candidateType:t.local.type,remoteAddress:t.remote.address,remotePort:t.remote.port}),this.emit("connected"))};const ps=65277,Es=Object.freeze({CHANGE_CIPHER_SPEC:20,ALERT:21,HANDSHAKE:22,APPLICATION_DATA:23}),ms=Object.freeze({HELLO_REQUEST:0,CLIENT_HELLO:1,SERVER_HELLO:2,HELLO_VERIFY_REQUEST:3,CERTIFICATE:11,SERVER_KEY_EXCHANGE:12,CERTIFICATE_REQUEST:13,SERVER_HELLO_DONE:14,CERTIFICATE_VERIFY:15,CLIENT_KEY_EXCHANGE:16,FINISHED:20}),Is=Object.freeze({WARNING:1,FATAL:2}),ws=Object.freeze({CLOSE_NOTIFY:0,HANDSHAKE_FAILURE:40,BAD_CERTIFICATE:42,DECRYPT_ERROR:51,INTERNAL_ERROR:80}),gs=49195,Ts=Object.freeze({secp256r1:23}),Bs=Object.freeze({uncompressed:0}),ys=Object.freeze({sha256:4,sha384:5,sha512:6}),Cs=Object.freeze({rsa:1,ecdsa:3}),As=Object.freeze({ecdsa_sign:64,rsa_sign:1}),Rs=Object.freeze({SUPPORTED_GROUPS:10,EC_POINT_FORMATS:11,SIGNATURE_ALGORITHMS:13,EXTENDED_MASTER_SECRET:23,RENEGOTIATION_INFO:65281}),Ss=Object.freeze({CLIENT:"client finished",SERVER:"server finished"});function Ns(t){return Buffer.from([t>>16&255,t>>8&255,255&t])}function vs(t,e){return t[e]<<16|t[e+1]<<8|t[e+2]}function Us(t){return Buffer.concat([Buffer.from([t.length]),t])}function Os(t){const e=Buffer.alloc(2);return e.writeUInt16BE(t.length,0),Buffer.concat([e,t])}function Ds(t){return Buffer.concat([Ns(t.length),t])}function _s(t,e,s){const i=Buffer.alloc(12);return i.writeUInt8(t,0),Ns(s.length).copy(i,1),i.writeUInt16BE(e,4),Ns(0).copy(i,6),Ns(s.length).copy(i,9),Buffer.concat([i,s])}function Ls(t,e,s,i){return function(t,e,s,i){const r=[];let n=0,a=s;for(;n<i;){a=g.createHmac(t,e).update(a).digest();const i=g.createHmac(t,e).update(Buffer.concat([a,s])).digest();r.push(i),n+=i.length}return Buffer.concat(r).slice(0,i)}("sha256",t,Buffer.concat([Buffer.from(e,"ascii"),s]),i)}var ks,Ms;function bs(t,e,s){return Ls(t,"master secret",Buffer.concat([e,s]),48)}function Ps(t,e){return Ls(t,"extended master secret",e,48)}function Ws(t,e,s,i,r){const n=Buffer.alloc(13);return n.writeUInt16BE(t,0),n.writeUIntBE(e,2,6),n.writeUInt8(s,8),n.writeUInt16BE(i,9),n.writeUInt16BE(r,11),n}class Hs{constructor(t,e){ks.set(this,void 0),Ms.set(this,void 0),C(this,ks,t,"f"),C(this,Ms,e,"f")}encrypt(t,e,s,i,r){const n=Buffer.alloc(8);n.writeUInt16BE(t,0),n.writeUIntBE(e,2,6);const a=Buffer.concat([y(this,Ms,"f"),n]),o=Ws(t,e,s,i,r.length),h=g.createCipheriv("aes-128-gcm",y(this,ks,"f"),a);h.setAAD(o);const c=Buffer.concat([h.update(r),h.final()]),f=h.getAuthTag();return Buffer.concat([n,c,f])}decrypt(t,e,s,i,r){const n=r.slice(0,8),a=r.slice(r.length-16),o=r.slice(8,r.length-16),h=Buffer.concat([y(this,Ms,"f"),n]),c=Ws(t,e,s,i,o.length),f=g.createDecipheriv("aes-128-gcm",y(this,ks,"f"),h);return f.setAAD(c),f.setAuthTag(a),Buffer.concat([f.update(o),f.final()])}}var xs,Fs,Ks,Gs,$s,js,Vs,zs,Ys,qs,Xs,Qs,Zs,Js,ti,ei,si,ii,ri,ni,ai,oi,hi,ci,fi,li,di,ui,pi,Ei,mi,Ii,wi,gi,Ti,Bi,yi,Ci,Ai,Ri,Si,Ni,vi,Ui,Oi,Di,_i,Li,ki,Mi,bi,Pi,Wi,Hi,xi,Fi,Ki,Gi,$i,ji,Vi,zi,Yi,qi,Xi;ks=new WeakMap,Ms=new WeakMap;const Qi=Object.freeze({CLIENT:"client",SERVER:"server"}),Zi=Object.freeze({NEW:"new",HANDSHAKING:"handshaking",CONNECTED:"connected",CLOSED:"closed",FAILED:"failed"});class Ji extends e.EventEmitter{constructor(t){super(),xs.add(this),Fs.set(this,void 0),Ks.set(this,void 0),Gs.set(this,void 0),$s.set(this,void 0),js.set(this,void 0),Vs.set(this,void 0),zs.set(this,void 0),Ys.set(this,void 0),qs.set(this,void 0),Xs.set(this,void 0),Qs.set(this,void 0),Zs.set(this,void 0),Js.set(this,void 0),ti.set(this,void 0),ei.set(this,void 0),si.set(this,void 0),ii.set(this,void 0),ri.set(this,void 0),ni.set(this,void 0),ai.set(this,void 0),oi.set(this,void 0),hi.set(this,void 0),ci.set(this,void 0),fi.set(this,void 0),li.set(this,void 0),di.set(this,void 0),ui.set(this,void 0),this.role=t.role,C(this,Fs,t.certDer,"f"),C(this,Ks,t.privateKey,"f"),C(this,Gs,t.verifyFingerprint||null,"f"),C(this,$s,t.output,"f"),this.state=Zi.NEW,C(this,js,0,"f"),C(this,Vs,0,"f"),C(this,zs,0,"f"),C(this,Ys,null,"f"),C(this,qs,null,"f"),C(this,Xs,!1,"f"),C(this,Qs,null,"f"),C(this,Zs,null,"f"),C(this,Js,Buffer.alloc(0),"f"),C(this,ti,null,"f"),C(this,ei,null,"f"),C(this,si,null,"f"),C(this,ii,null,"f"),C(this,ri,!1,"f"),C(this,ni,[],"f"),C(this,ai,new Map,"f"),C(this,oi,0,"f"),C(this,hi,[],"f"),C(this,ci,null,"f"),C(this,fi,0,"f"),C(this,li,!1,"f")}start(){this.state===Zi.NEW&&(this.state=Zi.HANDSHAKING,this.role===Qi.CLIENT&&(C(this,Qs,y(this,xs,"m",pi).call(this),"f"),y(this,xs,"m",vi).call(this)))}handlePacket(t){if(this.state===Zi.CLOSED||this.state===Zi.FAILED)return;let e;try{e=function(t){const e=[];let s=0;for(;s+13<=t.length;){const i=t.readUInt8(s),r=t.readUInt16BE(s+1),n=t.readUInt16BE(s+3),a=t.readUIntBE(s+5,6),o=t.readUInt16BE(s+11),h=s+13;if(h+o>t.length)break;e.push({type:i,version:r,epoch:n,seq:a,fragment:t.slice(h,h+o)}),s=h+o}return e}(t)}catch(t){return}for(const t of e)try{y(this,xs,"m",Bi).call(this,t)}catch(t){return void y(this,xs,"m",Xi).call(this,t instanceof Error?t:new Error(String(t)))}}send(t){if(this.state!==Zi.CONNECTED)throw new Error("DTLS connection not established");y(this,xs,"m",wi).call(this,Es.APPLICATION_DATA,t)}close(){if(this.state!==Zi.CLOSED&&this.state!==Zi.FAILED){try{y(this,Xs,"f")&&y(this,xs,"m",wi).call(this,Es.ALERT,Buffer.from([Is.WARNING,ws.CLOSE_NOTIFY]))}catch(t){}y(this,xs,"m",Ti).call(this),this.state=Zi.CLOSED,this.emit("close")}}getRemoteCertificate(){return y(this,si,"f")}}Fs=new WeakMap,Ks=new WeakMap,Gs=new WeakMap,$s=new WeakMap,js=new WeakMap,Vs=new WeakMap,zs=new WeakMap,Ys=new WeakMap,qs=new WeakMap,Xs=new WeakMap,Qs=new WeakMap,Zs=new WeakMap,Js=new WeakMap,ti=new WeakMap,ei=new WeakMap,si=new WeakMap,ii=new WeakMap,ri=new WeakMap,ni=new WeakMap,ai=new WeakMap,oi=new WeakMap,hi=new WeakMap,ci=new WeakMap,fi=new WeakMap,li=new WeakMap,di=new WeakMap,ui=new WeakMap,xs=new WeakSet,pi=function(){const t=g.randomBytes(32);return t.writeUInt32BE(Math.floor(Date.now()/1e3),0),t},Ei=function(t){var e,s;const i=[];for(const r of t){const t=(C(this,zs,(s=y(this,zs,"f"),e=s++,s),"f"),e),n=_s(r.type,t,r.body);y(this,ni,"f").push(n);const a=r.body.length;let o=0;do{const e=r.body.slice(o,o+1200),s=Buffer.alloc(12);s.writeUInt8(r.type,0),Ns(a).copy(s,1),s.writeUInt16BE(t,4),Ns(o).copy(s,6),Ns(e.length).copy(s,9);const n=Buffer.concat([s,e]);i.push({type:Es.HANDSHAKE,payload:n}),o+=e.length}while(o<a)}C(this,hi,i,"f"),C(this,fi,0,"f"),y(this,xs,"m",mi).call(this),y(this,xs,"m",gi).call(this)},mi=function(){for(const t of y(this,hi,"f"))y(this,xs,"m",wi).call(this,t.type,t.payload)},Ii=function(){y(this,xs,"m",wi).call(this,Es.CHANGE_CIPHER_SPEC,Buffer.from([1])),C(this,js,1,"f"),C(this,Vs,0,"f"),C(this,Xs,!0,"f")},wi=function(t,e){var s,i;let r=e;const n=(C(this,Vs,(i=y(this,Vs,"f"),s=i++,i),"f"),s);y(this,Xs,"f")&&y(this,Ys,"f")&&(r=y(this,Ys,"f").encrypt(y(this,js,"f"),n,t,ps,e));const a=function(t,e,s,i,r=65277){const n=Buffer.alloc(13);return n.writeUInt8(t,0),n.writeUInt16BE(r,1),n.writeUInt16BE(e,3),n.writeUIntBE(s,5,6),n.writeUInt16BE(i.length,11),Buffer.concat([n,i])}(t,y(this,js,"f"),n,r,ps);y(this,$s,"f").call(this,a)},gi=function t(){y(this,xs,"m",Ti).call(this),C(this,ci,setTimeout(()=>{var e;y(this,li,"f")||this.state!==Zi.HANDSHAKING||(y(this,fi,"f")>=10?y(this,xs,"m",Xi).call(this,new Error("DTLS handshake timed out")):(C(this,fi,(e=y(this,fi,"f"),++e),"f"),y(this,xs,"m",mi).call(this),y(this,xs,"m",t).call(this)))},1e3*Math.pow(2,y(this,fi,"f"))),"f"),y(this,ci,"f").unref&&y(this,ci,"f").unref()},Ti=function(){y(this,ci,"f")&&(clearTimeout(y(this,ci,"f")),C(this,ci,null,"f"))},Bi=function(t){let e=t.fragment;if(t.epoch>=1){if(!y(this,qs,"f"))return;e=y(this,qs,"f").decrypt(t.epoch,t.seq,t.type,t.version,t.fragment)}switch(t.type){case Es.HANDSHAKE:y(this,xs,"m",Ci).call(this,e);break;case Es.CHANGE_CIPHER_SPEC:break;case Es.APPLICATION_DATA:this.state===Zi.CONNECTED&&this.emit("data",e);break;case Es.ALERT:y(this,xs,"m",yi).call(this,e)}},yi=function(t){if(t.length<2)return;const e=t[0],s=t[1];s===ws.CLOSE_NOTIFY?this.close():e===Is.FATAL&&y(this,xs,"m",Xi).call(this,new Error(`DTLS fatal alert: ${s}`))},Ci=function(t){var e;const s=function(t){const e=t.readUInt8(0),s=vs(t,1),i=t.readUInt16BE(4),r=vs(t,6),n=vs(t,9);return{msgType:e,length:s,messageSeq:i,fragmentOffset:r,fragmentLength:n,body:t.slice(12,12+n)}}(t);let i=y(this,ai,"f").get(s.messageSeq);for(i||(i={type:s.msgType,length:s.length,data:Buffer.alloc(s.length),received:0,ranges:[]},y(this,ai,"f").set(s.messageSeq,i)),s.body.copy(i.data,s.fragmentOffset),i.received=Math.max(i.received,s.fragmentOffset+s.fragmentLength);;){const t=y(this,ai,"f").get(y(this,oi,"f"));if(!t||t.received<t.length)break;y(this,ai,"f").delete(y(this,oi,"f")),C(this,oi,(e=y(this,oi,"f"),++e),"f"),y(this,xs,"m",Ri).call(this,t.type,t.data)}},Ai=function(t,e){const s=y(this,oi,"f")-1;y(this,ni,"f").push(_s(t,s,e))},Ri=function(t,e){this.role===Qi.CLIENT?y(this,xs,"m",_i).call(this,t,e):y(this,xs,"m",Mi).call(this,t,e)},Si=function(){const t=g.createHash("sha256");for(const e of y(this,ni,"f"))t.update(e);return t.digest()},Ni=function(){return Buffer.concat(y(this,ni,"f"))},vi=function(){var t,e,s,i;const r=y(this,xs,"m",Ui).call(this);if(0===y(this,Js,"f").length){const s=(C(this,zs,(e=y(this,zs,"f"),t=e++,e),"f"),t),i=_s(ms.CLIENT_HELLO,s,r);C(this,hi,[{type:Es.HANDSHAKE,payload:i}],"f"),y(this,xs,"m",mi).call(this),y(this,xs,"m",gi).call(this)}else{const t=(C(this,zs,(i=y(this,zs,"f"),s=i++,i),"f"),s),e=_s(ms.CLIENT_HELLO,t,r);y(this,ni,"f").push(e),C(this,hi,[{type:Es.HANDSHAKE,payload:e}],"f"),C(this,fi,0,"f"),y(this,xs,"m",mi).call(this),y(this,xs,"m",gi).call(this)}},Ui=function(){const t=[];t.push(Buffer.from([254,253])),t.push(y(this,Qs,"f")),t.push(Us(Buffer.alloc(0))),t.push(Us(y(this,Js,"f")));const e=Buffer.alloc(2);return e.writeUInt16BE(gs,0),t.push(Os(e)),t.push(Us(Buffer.from([0]))),t.push(Os(y(this,xs,"m",Oi).call(this))),Buffer.concat(t)},Oi=function(){const t=[],e=Buffer.alloc(2);e.writeUInt16BE(Ts.secp256r1,0),t.push(y(this,xs,"m",Di).call(this,Rs.SUPPORTED_GROUPS,Os(e))),t.push(y(this,xs,"m",Di).call(this,Rs.EC_POINT_FORMATS,Us(Buffer.from([Bs.uncompressed]))));const s=Buffer.from([ys.sha256,Cs.ecdsa]);return t.push(y(this,xs,"m",Di).call(this,Rs.SIGNATURE_ALGORITHMS,Os(s))),t.push(y(this,xs,"m",Di).call(this,Rs.EXTENDED_MASTER_SECRET,Buffer.alloc(0))),Buffer.concat(t)},Di=function(t,e){const s=Buffer.alloc(4);return s.writeUInt16BE(t,0),s.writeUInt16BE(e.length,2),Buffer.concat([s,e])},_i=function(t,e){switch(t){case ms.HELLO_VERIFY_REQUEST:{const t=e.readUInt8(2);C(this,Js,e.slice(3,3+t),"f"),y(this,xs,"m",Ti).call(this),y(this,xs,"m",vi).call(this);break}case ms.SERVER_HELLO:y(this,xs,"m",Ai).call(this,t,e),y(this,xs,"m",Li).call(this,e);break;case ms.CERTIFICATE:y(this,xs,"m",Ai).call(this,t,e),C(this,si,y(this,xs,"m",Gi).call(this,e),"f");break;case ms.SERVER_KEY_EXCHANGE:y(this,xs,"m",Ai).call(this,t,e),y(this,xs,"m",$i).call(this,e);break;case ms.CERTIFICATE_REQUEST:y(this,xs,"m",Ai).call(this,t,e);break;case ms.SERVER_HELLO_DONE:y(this,xs,"m",Ai).call(this,t,e),y(this,xs,"m",Ti).call(this),y(this,xs,"m",ki).call(this);break;case ms.FINISHED:y(this,xs,"m",Yi).call(this,e,Ss.SERVER),y(this,xs,"m",Ai).call(this,t,e),y(this,xs,"m",qi).call(this)}},Li=function(t){let e=2;C(this,Zs,t.slice(e,e+32),"f"),e+=32;e+=1+t.readUInt8(e);const s=t.readUInt16BE(e);if(e+=2,s!==gs)throw new Error(`Server chose unsupported cipher suite 0x${s.toString(16)}`);if(e+=1,e+2<=t.length){const s=t.readUInt16BE(e);e+=2;const i=e+s;for(;e+4<=i;){const s=t.readUInt16BE(e),i=t.readUInt16BE(e+2);e+=4,s===Rs.EXTENDED_MASTER_SECRET&&C(this,ri,!0,"f"),e+=i}}},ki=function(){C(this,ti,g.createECDH("prime256v1"),"f"),y(this,ti,"f").generateKeys();const t=y(this,ti,"f").getPublicKey(),e=y(this,ti,"f").computeSecret(y(this,ii,"f")),s=y(this,xs,"m",Ki).call(this),i=Us(t),r=y(this,zs,"f"),n=r+1,a=_s(ms.CERTIFICATE,r,s),o=_s(ms.CLIENT_KEY_EXCHANGE,n,i);if(y(this,ri,"f")){const t=g.createHash("sha256");for(const e of y(this,ni,"f"))t.update(e);t.update(a),t.update(o);const s=t.digest();C(this,ei,Ps(e,s),"f")}else C(this,ei,bs(e,y(this,Qs,"f"),y(this,Zs,"f")),"f");y(this,xs,"m",Vi).call(this);const h=Buffer.concat([...y(this,ni,"f"),a,o]),c=g.sign("sha256",h,{key:y(this,Ks,"f"),dsaEncoding:"der"}),f=Buffer.concat([Buffer.from([ys.sha256,Cs.ecdsa]),Os(c)]);y(this,xs,"m",Ei).call(this,[{type:ms.CERTIFICATE,body:s},{type:ms.CLIENT_KEY_EXCHANGE,body:i},{type:ms.CERTIFICATE_VERIFY,body:f}]),y(this,xs,"m",Ii).call(this),y(this,xs,"m",zi).call(this,Ss.CLIENT)},Mi=function(t,e){switch(t){case ms.CLIENT_HELLO:y(this,xs,"m",bi).call(this,e);break;case ms.CERTIFICATE:y(this,xs,"m",Ai).call(this,t,e),C(this,si,y(this,xs,"m",Gi).call(this,e),"f");break;case ms.CLIENT_KEY_EXCHANGE:{y(this,xs,"m",Ai).call(this,t,e);const s=e.readUInt8(0);C(this,ii,e.slice(1,1+s),"f");const i=y(this,ti,"f").computeSecret(y(this,ii,"f"));y(this,ri,"f")?C(this,ei,Ps(i,y(this,xs,"m",Si).call(this)),"f"):C(this,ei,bs(i,y(this,Qs,"f"),y(this,Zs,"f")),"f"),y(this,xs,"m",Vi).call(this);break}case ms.CERTIFICATE_VERIFY:y(this,xs,"m",Fi).call(this,e),y(this,xs,"m",Ai).call(this,t,e);break;case ms.FINISHED:y(this,xs,"m",Yi).call(this,e,Ss.CLIENT),y(this,xs,"m",Ai).call(this,t,e),y(this,xs,"m",Ii).call(this),y(this,xs,"m",zi).call(this,Ss.SERVER),y(this,xs,"m",qi).call(this)}},bi=function(t){let e=2;const s=t.slice(e,e+32);e+=32;e+=1+t.readUInt8(e);const i=t.readUInt8(e),r=t.slice(e+1,e+1+i);e+=1+i;const n=t.readUInt16BE(e),a=t.slice(e+2,e+2+n);e+=2+n;e+=1+t.readUInt8(e);let o=!1,h=!1;for(let t=0;t+1<a.length;t+=2)255===a.readUInt16BE(t)&&(h=!0);if(e+2<=t.length){const s=t.readUInt16BE(e);e+=2;const i=e+s;for(;e+4<=i;){const s=t.readUInt16BE(e),i=t.readUInt16BE(e+2);e+=4,s===Rs.EXTENDED_MASTER_SECRET&&(o=!0),s===Rs.RENEGOTIATION_INFO&&(h=!0),e+=i}}if(C(this,ui,h,"f"),0===r.length)return C(this,Qs,s,"f"),y(this,xs,"m",Wi).call(this,y(this,xs,"m",Pi).call(this,s)),y(this,ai,"f").clear(),void C(this,oi,1,"f");const c=y(this,xs,"m",Pi).call(this,s);if(!r.equals(c))return y(this,xs,"m",Wi).call(this,c),y(this,ai,"f").clear(),void C(this,oi,1,"f");C(this,Qs,s,"f"),C(this,ri,o,"f"),y(this,xs,"m",Ai).call(this,ms.CLIENT_HELLO,t),y(this,xs,"m",Hi).call(this)},Pi=function(t){return y(this,di,"f")||C(this,di,g.randomBytes(32),"f"),g.createHmac("sha256",y(this,di,"f")).update(t).digest().slice(0,20)},Wi=function(t){const e=Buffer.concat([Buffer.from([254,255]),Us(t)]),s=_s(ms.HELLO_VERIFY_REQUEST,0,e);y(this,xs,"m",wi).call(this,Es.HANDSHAKE,s),C(this,zs,1,"f")},Hi=function(){C(this,Zs,y(this,xs,"m",pi).call(this),"f"),C(this,ti,g.createECDH("prime256v1"),"f"),y(this,ti,"f").generateKeys();const t=y(this,ti,"f").getPublicKey(),e=y(this,xs,"m",xi).call(this),s=y(this,xs,"m",Ki).call(this),i=Buffer.concat([Buffer.from([3]),(()=>{const t=Buffer.alloc(2);return t.writeUInt16BE(Ts.secp256r1,0),t})(),Us(t)]),r=Buffer.concat([y(this,Qs,"f"),y(this,Zs,"f"),i]),n=g.sign("sha256",r,{key:y(this,Ks,"f"),dsaEncoding:"der"}),a=Buffer.concat([i,Buffer.from([ys.sha256,Cs.ecdsa]),Os(n)]),o=Us(Buffer.from([As.ecdsa_sign,As.rsa_sign])),h=Os(Buffer.from([ys.sha256,Cs.ecdsa])),c=Os(Buffer.alloc(0)),f=Buffer.concat([o,h,c]),l=Buffer.alloc(0);y(this,xs,"m",Ei).call(this,[{type:ms.SERVER_HELLO,body:e},{type:ms.CERTIFICATE,body:s},{type:ms.SERVER_KEY_EXCHANGE,body:a},{type:ms.CERTIFICATE_REQUEST,body:f},{type:ms.SERVER_HELLO_DONE,body:l}])},xi=function(){const t=[];t.push(Buffer.from([254,253])),t.push(y(this,Zs,"f")),t.push(Us(Buffer.alloc(0)));const e=Buffer.alloc(2);e.writeUInt16BE(gs,0),t.push(e),t.push(Buffer.from([0]));const s=[];return y(this,ri,"f")&&s.push(y(this,xs,"m",Di).call(this,Rs.EXTENDED_MASTER_SECRET,Buffer.alloc(0))),s.push(y(this,xs,"m",Di).call(this,Rs.EC_POINT_FORMATS,Us(Buffer.from([Bs.uncompressed])))),y(this,ui,"f")&&s.push(y(this,xs,"m",Di).call(this,Rs.RENEGOTIATION_INFO,Us(Buffer.alloc(0)))),t.push(Os(Buffer.concat(s))),Buffer.concat(t)},Fi=function(t){const e=t.readUInt16BE(2),s=t.slice(4,4+e),i=y(this,xs,"m",Ni).call(this),r=y(this,xs,"m",ji).call(this,y(this,si,"f"));if(!g.verify("sha256",i,{key:r,dsaEncoding:"der"},s))throw new Error("Client CertificateVerify signature invalid")},Ki=function(){const t=Ds(y(this,Fs,"f"));return Ds(t)},Gi=function(t){let e=3;if(e+3>3+vs(t,0))return null;const s=vs(t,e);e+=3;const i=t.slice(e,e+s);if(y(this,Gs,"f")){const t={algorithm:"sha-256",value:j(i,"sha-256")};if(!y(this,Gs,"f").call(this,t,i))throw new Error("Remote certificate fingerprint mismatch")}return i},$i=function(t){let e=0;const s=t.readUInt8(e);e+=1;const i=t.readUInt16BE(e);if(e+=2,3!==s||i!==Ts.secp256r1)throw new Error("Unsupported ECDHE curve from server");const r=t.readUInt8(e);e+=1;const n=t.slice(e,e+r);e+=r,C(this,ii,n,"f");const a=t.slice(0,e);e+=2;const o=t.readUInt16BE(e);e+=2;const h=t.slice(e,e+o),c=Buffer.concat([y(this,Qs,"f"),y(this,Zs,"f"),a]),f=y(this,xs,"m",ji).call(this,y(this,si,"f"));if(!g.verify("sha256",c,{key:f,dsaEncoding:"der"},h))throw new Error("ServerKeyExchange signature invalid")},ji=function(t){return new g.X509Certificate(t).publicKey},Vi=function(){const{clientKey:t,serverKey:e,clientIV:s,serverIV:i}=function(t,e,s){const i=Ls(t,"key expansion",Buffer.concat([s,e]),40);let r=0;return{clientKey:i.slice(r,r+=16),serverKey:i.slice(r,r+=16),clientIV:i.slice(r,r+=4),serverIV:i.slice(r,r+=4)}}(y(this,ei,"f"),y(this,Qs,"f"),y(this,Zs,"f"));this.role===Qi.CLIENT?(C(this,Ys,new Hs(t,s),"f"),C(this,qs,new Hs(e,i),"f")):(C(this,Ys,new Hs(e,i),"f"),C(this,qs,new Hs(t,s),"f"))},zi=function(t){var e,s;const i=Ls(y(this,ei,"f"),t,y(this,xs,"m",Si).call(this),12),r=(C(this,zs,(s=y(this,zs,"f"),e=s++,s),"f"),e),n=_s(ms.FINISHED,r,i);y(this,ni,"f").push(n),y(this,xs,"m",wi).call(this,Es.HANDSHAKE,n)},Yi=function(t,e){const s=Ls(y(this,ei,"f"),e,y(this,xs,"m",Si).call(this),12);if(!g.timingSafeEqual(t,s))throw new Error("Peer Finished verify_data mismatch")},qi=function(){y(this,li,"f")||(C(this,li,!0,"f"),y(this,xs,"m",Ti).call(this),this.state=Zi.CONNECTED,this.emit("connect"))},Xi=function(t){this.state!==Zi.FAILED&&this.state!==Zi.CLOSED&&(y(this,xs,"m",Ti).call(this),this.state=Zi.FAILED,this.emit("error",t))};const tr=Object.freeze({DATA:0,INIT:1,INIT_ACK:2,SACK:3,HEARTBEAT:4,HEARTBEAT_ACK:5,ABORT:6,SHUTDOWN:7,SHUTDOWN_ACK:8,ERROR:9,COOKIE_ECHO:10,COOKIE_ACK:11,SHUTDOWN_COMPLETE:14,FORWARD_TSN:192}),er=Object.freeze({HEARTBEAT_INFO:1,STATE_COOKIE:7,UNRECOGNIZED_PARAM:8,COOKIE_PRESERVATIVE:9,SUPPORTED_ADDR_TYPES:11,FORWARD_TSN_SUPPORTED:49152,SUPPORTED_EXTENSIONS:32776}),sr=Object.freeze({DCEP:50,STRING:51,BINARY:53,STRING_EMPTY:56,BINARY_EMPTY:57,STRING_PARTIAL:54,BINARY_PARTIAL:52});function ir(t){return t+3&-4}function rr(t,e,s){const i=4+s.length,r=Buffer.alloc(ir(i));return r.writeUInt8(t,0),r.writeUInt8(e,1),r.writeUInt16BE(i,2),s.copy(r,4),r}function nr(t,e){const s=4+e.length,i=Buffer.alloc(ir(s));return i.writeUInt16BE(t,0),i.writeUInt16BE(s,2),e.copy(i,4),i}function ar(t){const e=[];let s=0;for(;s+4<=t.length;){const i=t.readUInt16BE(s),r=t.readUInt16BE(s+2);if(r<4||s+r>t.length)break;e.push({type:i,length:r,value:t.slice(s+4,s+r)}),s+=ir(r)}return e}function or({initiateTag:t,a_rwnd:e,outStreams:s,inStreams:i,initialTSN:r}){const n=Buffer.alloc(16);return n.writeUInt32BE(t>>>0,0),n.writeUInt32BE(e>>>0,4),n.writeUInt16BE(s,8),n.writeUInt16BE(i,10),n.writeUInt32BE(r>>>0,12),n}function hr(t){return{initiateTag:t.readUInt32BE(0),a_rwnd:t.readUInt32BE(4),outStreams:t.readUInt16BE(8),inStreams:t.readUInt16BE(10),initialTSN:t.readUInt32BE(12),params:ar(t.slice(16))}}function cr({tsn:t,streamId:e,streamSeq:s,ppid:i,userData:r,unordered:n=!1,beginning:a=!0,ending:o=!0}){const h=Buffer.alloc(12);h.writeUInt32BE(t>>>0,0),h.writeUInt16BE(e,4),h.writeUInt16BE(s,6),h.writeUInt32BE(i>>>0,8);let c=0;return o&&(c|=1),a&&(c|=2),n&&(c|=4),{flags:c,body:Buffer.concat([h,r])}}const fr=(()=>{const t=new Uint32Array(256);for(let e=0;e<256;e++){let s=e;for(let t=0;t<8;t++)s=1&s?2197175160^s>>>1:s>>>1;t[e]=s>>>0}return t})();function lr(t){let e=4294967295;for(let s=0;s<t.length;s++)e=fr[255&(e^t[s])]^e>>>8;return(4294967295^e)>>>0}var dr,ur,pr,Er,mr,Ir,wr,gr,Tr,Br,yr,Cr,Ar,Rr,Sr,Nr,vr,Ur,Or,Dr,_r,Lr,kr,Mr,br,Pr,Wr,Hr,xr,Fr,Kr,Gr,$r;const jr=1048576,Vr=Object.freeze({CLOSED:"closed",COOKIE_WAIT:"cookie-wait",COOKIE_ECHOED:"cookie-echoed",ESTABLISHED:"established"});function zr(t,e){return(t-e&4294967295)>2147483648}function Yr(t,e){return t===e||zr(t,e)}class qr extends e.EventEmitter{constructor(t={}){super(),dr.add(this),ur.set(this,void 0),pr.set(this,void 0),Er.set(this,void 0),mr.set(this,void 0),Ir.set(this,void 0),wr.set(this,void 0),gr.set(this,void 0),Tr.set(this,void 0),Br.set(this,void 0),yr.set(this,void 0),Cr.set(this,void 0),Ar.set(this,void 0),this.isClient=!!t.isClient,this.state=Vr.CLOSED,C(this,ur,5e3,"f"),C(this,pr,5e3,"f"),C(this,Er,g.randomBytes(4).readUInt32BE(0)>>>0||1,"f"),C(this,mr,0,"f"),C(this,Ir,g.randomBytes(4).readUInt32BE(0)>>>0,"f"),C(this,wr,new Map,"f"),C(this,gr,new Map,"f"),C(this,Tr,null,"f"),C(this,Br,new Map,"f"),C(this,yr,new Map,"f"),C(this,Cr,null,"f")}start(){this.state===Vr.CLOSED&&this.isClient&&(y(this,dr,"m",vr).call(this),this.state=Vr.COOKIE_WAIT)}receivePacket(t){if(t.length<12)return;if(!function(t){const e=t.readUInt32LE(8);t.writeUInt32LE(0,8);const s=lr(t);return t.writeUInt32LE(e,8),s===e}(t))return;const e=function(t){return{srcPort:t.readUInt16BE(0),dstPort:t.readUInt16BE(2),verificationTag:t.readUInt32BE(4),checksum:t.readUInt32LE(8)}}(t),s=function(t){const e=[];let s=12;for(;s+4<=t.length;){const i=t.readUInt8(s),r=t.readUInt8(s+1),n=t.readUInt16BE(s+2);if(n<4||s+n>t.length)break;const a=t.slice(s+4,s+n);e.push({type:i,flags:r,length:n,body:a}),s+=ir(n)}return e}(t);for(const t of s)y(this,dr,"m",Sr).call(this,t,e)}sendData(t,e,s,i={}){if(this.state!==Vr.ESTABLISHED)throw new Error("SCTP association not established");const r=!!i.unordered;let n=0;r||(n=y(this,wr,"f").get(t)||0,y(this,wr,"f").set(t,n+1&65535));const a=s.length;let o=0;const h=[];do{const i=s.slice(o,o+1200),c=0===o,f=o+i.length>=a,l=y(this,Ir,"f");C(this,Ir,y(this,Ir,"f")+1>>>0,"f");const{flags:d,body:u}=cr({tsn:l,streamId:t,streamSeq:n,ppid:e,userData:i,unordered:r,beginning:c,ending:f}),p=rr(tr.DATA,d,u);y(this,gr,"f").set(l,{chunk:p}),h.push(p),o+=i.length}while(o<a);for(const t of h)y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[t])}shutdown(){if(this.state!==Vr.ESTABLISHED)return void y(this,dr,"m",$r).call(this);const t=rr(tr.SHUTDOWN,0,(()=>{const t=Buffer.alloc(4);return t.writeUInt32BE(y(this,Tr,"f")>>>0,0),t})());y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[t]),y(this,dr,"m",$r).call(this)}}ur=new WeakMap,pr=new WeakMap,Er=new WeakMap,mr=new WeakMap,Ir=new WeakMap,wr=new WeakMap,gr=new WeakMap,Tr=new WeakMap,Br=new WeakMap,yr=new WeakMap,Cr=new WeakMap,Ar=new WeakMap,dr=new WeakSet,Rr=function(t,e){const s=function(t,e,s){const i=Buffer.alloc(12);return i.writeUInt16BE(t,0),i.writeUInt16BE(e,2),i.writeUInt32BE(s>>>0,4),i.writeUInt32BE(0,8),i}(y(this,ur,"f"),y(this,pr,"f"),t),i=Buffer.concat([s,...e]);!function(t){t.writeUInt32LE(0,8);const e=lr(t);t.writeUInt32LE(e,8)}(i),this.emit("output",i)},Sr=function(t,e){switch(t.type){case tr.INIT:y(this,dr,"m",Dr).call(this,t);break;case tr.INIT_ACK:y(this,dr,"m",_r).call(this,t);break;case tr.COOKIE_ECHO:y(this,dr,"m",Lr).call(this,t);break;case tr.COOKIE_ACK:y(this,dr,"m",kr).call(this);break;case tr.DATA:y(this,dr,"m",br).call(this,t);break;case tr.SACK:y(this,dr,"m",xr).call(this,t);break;case tr.HEARTBEAT:y(this,dr,"m",Fr).call(this,t);break;case tr.ABORT:y(this,dr,"m",Gr).call(this,"peer sent ABORT");break;case tr.SHUTDOWN:y(this,dr,"m",Kr).call(this)}},Nr=function(){return[nr(er.FORWARD_TSN_SUPPORTED,Buffer.alloc(0))]},vr=function(){const t=or({initiateTag:y(this,Er,"f"),a_rwnd:jr,outStreams:65535,inStreams:65535,initialTSN:y(this,Ir,"f")}),e=rr(tr.INIT,0,Buffer.concat([t,...y(this,dr,"m",Nr).call(this)]));y(this,dr,"m",Rr).call(this,0,[e]),y(this,dr,"m",Ur).call(this,[e])},Ur=function(t){y(this,dr,"m",Or).call(this);let e=500,s=0;const i=()=>{this.state!==Vr.ESTABLISHED&&this.state!==Vr.CLOSED&&(s>=8?y(this,dr,"m",Gr).call(this,"SCTP setup timed out"):(s++,y(this,dr,"m",Rr).call(this,this.state===Vr.COOKIE_ECHOED?y(this,mr,"f"):0,t),e=Math.min(2*e,5e3),C(this,Cr,setTimeout(i,e),"f"),y(this,Cr,"f").unref&&y(this,Cr,"f").unref()))};C(this,Cr,setTimeout(i,e),"f"),y(this,Cr,"f").unref&&y(this,Cr,"f").unref()},Or=function(){y(this,Cr,"f")&&(clearTimeout(y(this,Cr,"f")),C(this,Cr,null,"f"))},Dr=function(t){const e=hr(t.body);C(this,mr,e.initiateTag,"f"),C(this,Tr,e.initialTSN-1>>>0,"f"),y(this,Ar,"f")||C(this,Ar,g.randomBytes(32),"f");const s=Buffer.alloc(16);s.writeUInt32BE(y(this,Er,"f"),0),s.writeUInt32BE(y(this,mr,"f"),4),s.writeUInt32BE(y(this,Ir,"f"),8),s.writeUInt32BE(e.initialTSN,12);const i=g.createHmac("sha256",y(this,Ar,"f")).update(s).digest(),r=Buffer.concat([s,i]),n=or({initiateTag:y(this,Er,"f"),a_rwnd:jr,outStreams:65535,inStreams:65535,initialTSN:y(this,Ir,"f")}),a=Buffer.concat([nr(er.STATE_COOKIE,r),...y(this,dr,"m",Nr).call(this)]),o=rr(tr.INIT_ACK,0,Buffer.concat([n,a]));y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[o])},_r=function(t){if(this.state!==Vr.COOKIE_WAIT)return;y(this,dr,"m",Or).call(this);const e=hr(t.body);C(this,mr,e.initiateTag,"f"),C(this,Tr,e.initialTSN-1>>>0,"f");const s=e.params.find(t=>t.type===er.STATE_COOKIE);if(!s)return void y(this,dr,"m",Gr).call(this,"INIT_ACK missing state cookie");const i=rr(tr.COOKIE_ECHO,0,s.value);this.state=Vr.COOKIE_ECHOED,y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[i]),y(this,dr,"m",Ur).call(this,[i])},Lr=function(t){const e=t.body;if(e.length>=48&&y(this,Ar,"f")){const t=e.slice(0,16),s=e.slice(16,48),i=g.createHmac("sha256",y(this,Ar,"f")).update(t).digest();if(!g.timingSafeEqual(s,i))return}const s=rr(tr.COOKIE_ACK,0,Buffer.alloc(0));y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[s]),y(this,dr,"m",Mr).call(this)},kr=function(){this.state===Vr.COOKIE_ECHOED&&(y(this,dr,"m",Or).call(this),y(this,dr,"m",Mr).call(this))},Mr=function(){this.state!==Vr.ESTABLISHED&&(this.state=Vr.ESTABLISHED,this.emit("established"))},br=function(t){const e=(s=t.flags,i=t.body,{unordered:!!(4&s),beginning:!!(2&s),ending:!!(1&s),tsn:i.readUInt32BE(0),streamId:i.readUInt16BE(4),streamSeq:i.readUInt16BE(6),ppid:i.readUInt32BE(8),userData:i.slice(12)});var s,i;y(this,dr,"m",Pr).call(this,e),y(this,dr,"m",Hr).call(this)},Pr=function(t){const e=y(this,Tr,"f")+1>>>0;if(!zr(t.tsn,e))if(t.tsn===e){C(this,Tr,t.tsn,"f"),y(this,dr,"m",Wr).call(this,t);let e=y(this,Tr,"f")+1>>>0;for(;y(this,Br,"f").has(e);){const t=y(this,Br,"f").get(e);y(this,Br,"f").delete(e),C(this,Tr,e,"f"),y(this,dr,"m",Wr).call(this,t),e=y(this,Tr,"f")+1>>>0}}else y(this,Br,"f").has(t.tsn)||y(this,Br,"f").set(t.tsn,t)},Wr=function(t){const e=`${t.streamId}:${t.unordered?"u":"o"}`;if(t.beginning&&t.ending)return void this.emit("message",{streamId:t.streamId,ppid:t.ppid,data:t.userData});let s=y(this,yr,"f").get(e);if(t.beginning)s={ppid:t.ppid,parts:[t.userData]},y(this,yr,"f").set(e,s);else{if(!s)return;s.parts.push(t.userData)}t.ending&&s&&(y(this,yr,"f").delete(e),this.emit("message",{streamId:t.streamId,ppid:s.ppid,data:Buffer.concat(s.parts)}))},Hr=function(){const t=[];if(y(this,Br,"f").size>0){const e=[...y(this,Br,"f").keys()].sort((t,e)=>zr(t,e)?-1:1),s=y(this,Tr,"f")+1>>>0;let i=null,r=null;for(const n of e)null!==i?n!==r+1>>>0?(t.push([1+(i-s&65535),1+(r-s&65535)]),i=n,r=n):r=n:(i=n,r=n);null!==i&&t.push([1+(i-s&65535),1+(r-s&65535)])}const e=function({cumulativeTSNAck:t,a_rwnd:e,gapBlocks:s=[],dupTSNs:i=[]}){const r=Buffer.alloc(12+4*s.length+4*i.length);r.writeUInt32BE(t>>>0,0),r.writeUInt32BE(e>>>0,4),r.writeUInt16BE(s.length,8),r.writeUInt16BE(i.length,10);let n=12;for(const[t,e]of s)r.writeUInt16BE(t,n),r.writeUInt16BE(e,n+2),n+=4;for(const t of i)r.writeUInt32BE(t>>>0,n),n+=4;return r}({cumulativeTSNAck:y(this,Tr,"f")>>>0,a_rwnd:jr,gapBlocks:t}),s=rr(tr.SACK,0,e);y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[s])},xr=function(t){const e=function(t){const e=t.readUInt32BE(0),s=t.readUInt32BE(4),i=t.readUInt16BE(8),r=t.readUInt16BE(10),n=[];let a=12;for(let e=0;e<i;e++)n.push([t.readUInt16BE(a),t.readUInt16BE(a+2)]),a+=4;const o=[];for(let e=0;e<r;e++)o.push(t.readUInt32BE(a)),a+=4;return{cumulativeTSNAck:e,a_rwnd:s,gapBlocks:n,dupTSNs:o}}(t.body);for(const t of[...y(this,gr,"f").keys()])Yr(t,e.cumulativeTSNAck)&&y(this,gr,"f").delete(t);const s=e.cumulativeTSNAck+1>>>0;for(const[t,i]of e.gapBlocks)for(let e=t;e<=i;e++)y(this,gr,"f").delete(s+e-1>>>0)},Fr=function(t){const e=rr(tr.HEARTBEAT_ACK,0,t.body);y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[e])},Kr=function(){const t=rr(tr.SHUTDOWN_ACK,0,Buffer.alloc(0));y(this,dr,"m",Rr).call(this,y(this,mr,"f"),[t]),y(this,dr,"m",$r).call(this)},Gr=function(t){y(this,dr,"m",Or).call(this),this.state!==Vr.CLOSED&&(this.state=Vr.CLOSED,this.emit("error",new Error(t||"SCTP abort")),this.emit("close"))},$r=function(){y(this,dr,"m",Or).call(this),this.state!==Vr.CLOSED&&(this.state=Vr.CLOSED,this.emit("close"))};const Xr=Object.freeze({DATA_CHANNEL_ACK:2,DATA_CHANNEL_OPEN:3}),Qr=Object.freeze({RELIABLE:0,RELIABLE_UNORDERED:128,PARTIAL_RELIABLE_REXMIT:1,PARTIAL_RELIABLE_REXMIT_UNORDERED:129,PARTIAL_RELIABLE_TIMED:2,PARTIAL_RELIABLE_TIMED_UNORDERED:130});var Zr,Jr,tn,en,sn,rn,nn,an,on,hn,cn,fn,ln,dn,un,pn,En,mn,In,wn,gn,Tn,Bn,yn,Cn,An,Rn,Sn,Nn,vn,Un,On,Dn,_n,Ln,kn,Mn,bn,Pn,Wn;class Hn extends e.EventEmitter{constructor(t,e){super(),Zr.add(this),Jr.set(this,void 0),tn.set(this,void 0),en.set(this,void 0),C(this,Jr,t,"f"),C(this,tn,new Map,"f"),C(this,en,e?0:1,"f"),y(this,Jr,"f").on("message",t=>y(this,Zr,"m",on).call(this,t))}openChannel(t,e={}){let s=t.id;if(null==s&&(s=y(this,Zr,"m",sn).call(this),t.emit(gt.SET_ID,s)),y(this,tn,"f").set(s,{channel:t,acked:!1}),y(this,Zr,"m",an).call(this,t,s,e),t.negotiated)y(this,tn,"f").get(s).acked=!0,t.emit(gt.OPEN);else{const i=function({channelType:t,priority:e=0,reliabilityParameter:s=0,label:i="",protocol:r=""}){const n=Buffer.from(i,"utf8"),a=Buffer.from(r,"utf8"),o=Buffer.alloc(12+n.length+a.length);return o.writeUInt8(Xr.DATA_CHANNEL_OPEN,0),o.writeUInt8(t,1),o.writeUInt16BE(e,2),o.writeUInt32BE(s>>>0,4),o.writeUInt16BE(n.length,8),o.writeUInt16BE(a.length,10),n.copy(o,12),a.copy(o,12+n.length),o}({channelType:y(this,Zr,"m",rn).call(this,e),priority:0,reliabilityParameter:y(this,Zr,"m",nn).call(this,e),label:t.label,protocol:e.protocol||t.protocol||""});y(this,Jr,"f").sendData(s,sr.DCEP,i)}}acceptChannel(t,e){t.emit(gt.SET_ID,e.streamId),y(this,tn,"f").set(e.streamId,{channel:t,acked:!0}),y(this,Zr,"m",an).call(this,t,e.streamId,{ordered:e.ordered}),t.emit(gt.OPEN)}}Jr=new WeakMap,tn=new WeakMap,en=new WeakMap,Zr=new WeakSet,sn=function(){let t=y(this,en,"f");for(;y(this,tn,"f").has(t);)t+=2;return C(this,en,t+2,"f"),t},rn=function(t){const e=!1===t.ordered;return null!=t.maxRetransmits?e?Qr.PARTIAL_RELIABLE_REXMIT_UNORDERED:Qr.PARTIAL_RELIABLE_REXMIT:null!=t.maxPacketLifeTime?e?Qr.PARTIAL_RELIABLE_TIMED_UNORDERED:Qr.PARTIAL_RELIABLE_TIMED:e?Qr.RELIABLE_UNORDERED:Qr.RELIABLE},nn=function(t){return null!=t.maxRetransmits?t.maxRetransmits>>>0:null!=t.maxPacketLifeTime?t.maxPacketLifeTime>>>0:0},an=function(t,e,s){const i=!1===s.ordered;t.on(gt.SEND,(t,s)=>{let r;r=s?0===t.length?sr.BINARY_EMPTY:sr.BINARY:0===t.length?sr.STRING_EMPTY:sr.STRING;const n=0===t.length?Buffer.from([0]):t;y(this,Jr,"f").sendData(e,r,n,{unordered:i})})},on=function(t){if(t.ppid===sr.DCEP)return void y(this,Zr,"m",hn).call(this,t);const e=y(this,tn,"f").get(t.streamId);if(!e)return;const s=t.ppid===sr.BINARY||t.ppid===sr.BINARY_EMPTY||t.ppid===sr.BINARY_PARTIAL,i=t.ppid===sr.STRING_EMPTY||t.ppid===sr.BINARY_EMPTY?Buffer.alloc(0):t.data;e.channel.emit(gt.RECEIVE,i,s)},hn=function(t){const e=(s=t.data).length>0?s.readUInt8(0):-1;var s;if(e===Xr.DATA_CHANNEL_OPEN){const e=function(t){const e=t.readUInt8(1),s=t.readUInt16BE(2),i=t.readUInt32BE(4),r=t.readUInt16BE(8),n=t.readUInt16BE(10);return{channelType:e,priority:s,reliabilityParameter:i,label:t.slice(12,12+r).toString("utf8"),protocol:t.slice(12+r,12+r+n).toString("utf8"),unordered:!!(128&e)}}(t.data);y(this,Jr,"f").sendData(t.streamId,sr.DCEP,Buffer.from([Xr.DATA_CHANNEL_ACK])),this.emit("open-request",{streamId:t.streamId,label:e.label,protocol:e.protocol,ordered:!e.unordered,channelType:e.channelType,reliabilityParameter:e.reliabilityParameter})}else if(e===Xr.DATA_CHANNEL_ACK){const e=y(this,tn,"f").get(t.streamId);e&&!e.acked&&(e.acked=!0,e.channel.emit(gt.OPEN))}};class xn extends e.EventEmitter{constructor(t){super(),cn.add(this),fn.set(this,void 0),ln.set(this,void 0),C(this,fn,t,"f"),this.ice=new us({role:t.iceRole,localUfrag:t.localUfrag,localPwd:t.localPwd}),this.dtls=null,this.sctp=null,this.dcm=null,C(this,ln,!1,"f"),this.ice.on("candidate",t=>this.emit("candidate",t)),this.ice.on("error",t=>this.emit("error",t)),this.ice.on("failed",()=>this.emit("error",new Error("ICE failed"))),this.ice.on("data",t=>{this.dtls&&this.dtls.handlePacket(t)}),this.ice.on("connected",()=>{this.emit("iceconnected"),y(this,cn,"m",dn).call(this)})}async gather(t={}){await this.ice.gather(t)}getLocalCandidates(){return this.ice.getLocalCandidates()}setRemote(t,e){this.ice.setRemoteCredentials(t,e),this.ice.start()}addRemoteCandidate(t){this.ice.addRemoteCandidate(t)}openChannel(t,e){if(!this.dcm)throw new Error("SCTP not ready");this.dcm.openChannel(t,e)}acceptChannel(t,e){if(!this.dcm)throw new Error("SCTP not ready");this.dcm.acceptChannel(t,e)}isReady(){return!!this.sctp&&"established"===this.sctp.state}close(){if(this.sctp)try{this.sctp.shutdown()}catch{}if(this.dtls)try{this.dtls.close()}catch{}if(this.ice)try{this.ice.close()}catch{}}}fn=new WeakMap,ln=new WeakMap,cn=new WeakSet,dn=function(){y(this,ln,"f")||(C(this,ln,!0,"f"),this.dtls=new Ji({role:"client"===y(this,fn,"f").dtlsRole?Qi.CLIENT:Qi.SERVER,certDer:y(this,fn,"f").certDer,privateKey:y(this,fn,"f").privateKey,verifyFingerprint:y(this,fn,"f").verifyFingerprint,output:t=>{try{this.ice.send(t)}catch(t){this.emit("error",t)}}}),this.dtls.on("connect",()=>{this.emit("dtlsconnected"),y(this,cn,"m",un).call(this)}),this.dtls.on("data",t=>{this.sctp&&this.sctp.receivePacket(t)}),this.dtls.on("error",t=>this.emit("error",t)),this.dtls.on("close",()=>this.emit("close")),this.dtls.start())},un=function(){const t="client"===y(this,fn,"f").dtlsRole,e=new qr({isClient:t});this.sctp=e,e.on("output",t=>{try{this.dtls&&this.dtls.send(t)}catch(t){this.emit("error",t)}}),e.on("error",t=>this.emit("error",t)),e.on("close",()=>this.emit("close")),this.dcm=new Hn(e,t),this.dcm.on("open-request",t=>this.emit("datachannel-request",t)),e.on("established",()=>{this.emit("sctpconnected"),this.emit("ready")}),e.start()};const Fn=Object.freeze({STABLE:"stable",HAVE_LOCAL_OFFER:"have-local-offer",HAVE_REMOTE_OFFER:"have-remote-offer",HAVE_LOCAL_PRANSWER:"have-local-pranswer",HAVE_REMOTE_PRANSWER:"have-remote-pranswer",CLOSED:"closed"}),Kn=Object.freeze({NEW:"new",GATHERING:"gathering",COMPLETE:"complete"}),Gn=Object.freeze({NEW:"new",CONNECTING:"connecting",CONNECTED:"connected",DISCONNECTED:"disconnected",FAILED:"failed",CLOSED:"closed"});class $n extends e.EventEmitter{constructor(t={}){super(),pn.add(this),En.set(this,void 0),mn.set(this,void 0),In.set(this,void 0),wn.set(this,void 0),gn.set(this,void 0),Tn.set(this,void 0),Bn.set(this,void 0),yn.set(this,void 0),Cn.set(this,void 0),An.set(this,void 0),Rn.set(this,void 0),Sn.set(this,void 0),Nn.set(this,void 0),vn.set(this,void 0),Un.set(this,void 0),On.set(this,void 0),Dn.set(this,void 0),C(this,En,t,"f"),C(this,mn,Fn.STABLE,"f"),C(this,In,Kn.NEW,"f"),C(this,wn,Gn.NEW,"f"),C(this,gn,null,"f"),C(this,Tn,null,"f"),C(this,Bn,null,"f"),C(this,yn,{usernameFragment:g.randomBytes(3).toString("base64").replace(/[^a-zA-Z0-9]/g,"").slice(0,4).padEnd(4,"x"),password:g.randomBytes(18).toString("base64").replace(/[^a-zA-Z0-9]/g,"").slice(0,24).padEnd(24,"x")},"f"),C(this,Cn,null,"f"),C(this,An,[],"f"),C(this,Rn,null,"f"),C(this,Sn,null,"f"),C(this,Nn,!1,"f"),C(this,vn,!1,"f"),C(this,Un,[],"f"),C(this,On,new Set,"f"),C(this,Dn,[],"f")}createDataChannel(t,e={}){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");const s=new Tt(t,e);y(this,On,"f").add(s);const i={ordered:!1!==e.ordered,maxRetransmits:e.maxRetransmits,maxPacketLifeTime:e.maxPacketLifeTime,protocol:e.protocol||"",negotiated:e.negotiated||!1};return y(this,Sn,"f")&&y(this,Sn,"f").isReady()?y(this,Sn,"f").openChannel(s,i):y(this,Un,"f").push({channel:s,init:i}),setImmediate(()=>{y(this,vn,"f")||this.emit("negotiationneeded")}),s}async createOffer(){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");await y(this,pn,"m",_n).call(this),C(this,Nn,!0,"f");const t=y(this,pn,"m",Mn).call(this),e=(s={iceUfrag:y(this,yn,"f").usernameFragment,icePwd:y(this,yn,"f").password,fingerprint:t,setup:"actpass",candidates:[]},Rt({...s,setup:s.setup||"actpass"}));var s;return new At({type:Ct.OFFER,sdp:e})}async createAnswer(){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");if(!y(this,Tn,"f")||"offer"!==y(this,Tn,"f").type)throw new Error("Cannot create answer without remote offer");await y(this,pn,"m",_n).call(this);const t=y(this,pn,"m",Mn).call(this),e=(s={iceUfrag:y(this,yn,"f").usernameFragment,icePwd:y(this,yn,"f").password,fingerprint:t,setup:"active",candidates:[]},Rt({...s,setup:s.setup||"active"}));var s;return new At({type:Ct.ANSWER,sdp:e})}async setLocalDescription(t){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");const e=t||(y(this,mn,"f")===Fn.HAVE_REMOTE_OFFER?await this.createAnswer():await this.createOffer());await y(this,pn,"m",_n).call(this),C(this,gn,new At({type:e.type??void 0,sdp:e.sdp??void 0}),"f"),"offer"===e.type?C(this,mn,Fn.HAVE_LOCAL_OFFER,"f"):"answer"===e.type&&C(this,mn,Fn.STABLE,"f"),y(this,pn,"m",bn).call(this,e,!0),C(this,In,Kn.GATHERING,"f"),this.emit("icegatheringstatechange"),y(this,Sn,"f")&&(await y(this,Sn,"f").gather({iceServers:y(this,En,"f").iceServers||[],iceTransportPolicy:y(this,En,"f").iceTransportPolicy||"all"}),C(this,In,Kn.COMPLETE,"f"),this.emit("icegatheringstatechange"),this.emit("icecandidate",{candidate:null}),y(this,pn,"m",Pn).call(this)),this.emit("signalingstatechange")}async setRemoteDescription(t){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");if(!t||!t.sdp)throw new Error("Invalid session description");await y(this,pn,"m",_n).call(this),C(this,Tn,new At(t),"f"),"offer"===t.type?C(this,mn,Fn.HAVE_REMOTE_OFFER,"f"):"answer"===t.type&&C(this,mn,Fn.STABLE,"f"),C(this,Cn,function(t){const e={usernameFragment:null,password:null};for(const s of t.split(/\r?\n/))s.startsWith("a=ice-ufrag:")?e.usernameFragment=s.slice(12).trim():s.startsWith("a=ice-pwd:")&&(e.password=s.slice(10).trim());return e}(t.sdp),"f");const e=function(t){const e={role:"auto",fingerprints:[]};for(const s of t.split(/\r?\n/))if(s.startsWith("a=setup:")){const t=s.slice(8).trim();e.role="active"===t?"client":"passive"===t?"server":"actpass",e.setup=t}else if(s.startsWith("a=fingerprint:")){const t=s.slice(14).trim().split(/\s+/);2===t.length&&e.fingerprints.push({algorithm:t[0].toLowerCase(),value:t[1].toUpperCase()})}return e}(t.sdp);if(C(this,An,e.fingerprints,"f"),C(this,Rn,e.setup??null,"f"),y(this,pn,"m",bn).call(this,t,!1),y(this,Sn,"f")&&y(this,Cn,"f").usernameFragment){y(this,Sn,"f").setRemote(y(this,Cn,"f").usernameFragment,y(this,Cn,"f").password??"");for(const e of function(t){const e=[];for(const s of t.split(/\r?\n/)){if(!s.startsWith("a=candidate:"))continue;const t=St(s.slice(2));t&&e.push(t)}return e}(t.sdp))y(this,Sn,"f").addRemoteCandidate(e)}y(this,pn,"m",Pn).call(this),this.emit("signalingstatechange")}async addIceCandidate(t){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");if(!t||"string"!=typeof t&&""===t.candidate)return;const e=St("string"==typeof t?t:t.candidate||"");e&&y(this,Sn,"f")&&y(this,Sn,"f").addRemoteCandidate(e)}getConfiguration(){return{...y(this,En,"f")}}setConfiguration(t){if(y(this,vn,"f"))throw new Error("RTCPeerConnection is closed");C(this,En,{...t},"f")}close(){if(!y(this,vn,"f")){C(this,vn,!0,"f"),C(this,mn,Fn.CLOSED,"f");for(const t of y(this,On,"f"))try{t.close()}catch(t){}if(y(this,Sn,"f"))try{y(this,Sn,"f").close()}catch(t){}y(this,pn,"m",Wn).call(this,Gn.CLOSED),this.emit("signalingstatechange")}}get signalingState(){return y(this,mn,"f")}get iceGatheringState(){return y(this,In,"f")}get iceConnectionState(){return y(this,wn,"f")===Gn.CONNECTED?"connected":y(this,wn,"f")===Gn.CONNECTING?"checking":y(this,wn,"f")===Gn.FAILED?"failed":"new"}get connectionState(){return y(this,wn,"f")}get localDescription(){return y(this,gn,"f")}get remoteDescription(){return y(this,Tn,"f")}get currentLocalDescription(){return y(this,gn,"f")}get currentRemoteDescription(){return y(this,Tn,"f")}get pendingLocalDescription(){return y(this,mn,"f")===Fn.STABLE?null:y(this,gn,"f")}get pendingRemoteDescription(){return y(this,mn,"f")===Fn.STABLE?null:y(this,Tn,"f")}get canTrickleIceCandidates(){return!0}get sctp(){return y(this,Sn,"f")?y(this,Sn,"f").sctp:null}}En=new WeakMap,mn=new WeakMap,In=new WeakMap,wn=new WeakMap,gn=new WeakMap,Tn=new WeakMap,Bn=new WeakMap,yn=new WeakMap,Cn=new WeakMap,An=new WeakMap,Rn=new WeakMap,Sn=new WeakMap,Nn=new WeakMap,vn=new WeakMap,Un=new WeakMap,On=new WeakMap,Dn=new WeakMap,pn=new WeakSet,_n=async function(){return y(this,Bn,"f")||(y(this,En,"f").certificates&&y(this,En,"f").certificates[0]?C(this,Bn,y(this,En,"f").certificates[0],"f"):C(this,Bn,await It.generateCertificate(),"f")),y(this,Bn,"f")},Ln=function(t,e){if(y(this,Sn,"f"))return y(this,Sn,"f");const s=y(this,Bn,"f");if(!s)throw new Error("Certificate not initialized");const i=s.getCertificateDer();if(!i)throw new Error("Certificate has no DER encoding");const r=new xn({iceRole:t,dtlsRole:e,localUfrag:y(this,yn,"f").usernameFragment,localPwd:y(this,yn,"f").password,certDer:i,privateKey:s.getPrivateKeyObject(),verifyFingerprint:t=>y(this,pn,"m",kn).call(this,t)});return r.on("candidate",t=>{const e={candidate:t.sdp,sdpMid:"0",sdpMLineIndex:0,usernameFragment:y(this,yn,"f").usernameFragment};y(this,Dn,"f").push(e),this.emit("icecandidate",{candidate:e})}),r.on("iceconnected",()=>y(this,pn,"m",Wn).call(this,Gn.CONNECTING)),r.on("sctpconnected",()=>y(this,pn,"m",Wn).call(this,Gn.CONNECTED)),r.on("error",t=>{this.emit("error",t),y(this,pn,"m",Wn).call(this,Gn.FAILED)}),r.on("close",()=>y(this,pn,"m",Wn).call(this,Gn.DISCONNECTED)),r.on("datachannel-request",t=>{const e=new Tt(t.label,{ordered:t.ordered,protocol:t.protocol,id:t.streamId});r.acceptChannel(e,t),y(this,On,"f").add(e),this.emit("datachannel",{channel:e})}),r.on("ready",()=>{for(const{channel:t,init:e}of y(this,Un,"f"))r.openChannel(t,e);C(this,Un,[],"f")}),C(this,Sn,r,"f"),r},kn=function(t){return 0===y(this,An,"f").length||y(this,An,"f").some(e=>e.algorithm===t.algorithm&&e.value.toUpperCase()===t.value.toUpperCase())},Mn=function(){const t=y(this,Bn,"f");if(!t)throw new Error("Certificate not initialized");const e=t.getFingerprints();return e.find(t=>"sha-256"===t.algorithm)||e[0]},bn=function(t,e){if(y(this,Sn,"f"))return;const s=y(this,Nn,"f")?"controlling":"controlled";let i;i=y(this,Nn,"f")?"active"===y(this,Rn,"f")?"server":"passive"===y(this,Rn,"f")?"client":"server":"client",y(this,pn,"m",Ln).call(this,s,i)},Pn=function(){y(this,Sn,"f")&&y(this,Cn,"f")&&y(this,Cn,"f").usernameFragment&&y(this,Sn,"f").setRemote(y(this,Cn,"f").usernameFragment,y(this,Cn,"f").password??"")},Wn=function(t){y(this,wn,"f")!==t&&(C(this,wn,t,"f"),this.emit("connectionstatechange"),this.emit("iceconnectionstatechange"))};
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview node-rtc-connection - WebRTC DataChannel implementation for Node.js
|
|
17
|
-
*
|
|
18
|
-
* A from-scratch, pure-Node.js implementation of WebRTC peer connections and
|
|
19
|
-
* data channels (no native dependencies). Interoperates with browsers.
|
|
20
|
-
*
|
|
21
|
-
* This implementation focuses on DataChannel functionality without media streams.
|
|
22
|
-
* Features:
|
|
23
|
-
* - ICE (Interactive Connectivity Establishment), RFC 8445
|
|
24
|
-
* - STUN/TURN support for NAT traversal
|
|
25
|
-
* - DTLS 1.2 encryption (RFC 6347)
|
|
26
|
-
* - SCTP over DTLS + DCEP for data channels (RFC 8831 / 8832)
|
|
27
|
-
* - W3C-compatible RTCPeerConnection / RTCDataChannel API
|
|
28
|
-
*
|
|
29
|
-
* @license MIT
|
|
30
|
-
* @author nmhung1210
|
|
31
|
-
*/
|
|
32
|
-
const jn="2.0.4";exports.ByteBufferQueue=class{constructor(){n.add(this),a.set(this,void 0),o.set(this,void 0),h.set(this,void 0),C(this,a,0,"f"),C(this,o,[],"f"),C(this,h,0,"f")}get size(){return y(this,a,"f")}get empty(){return 0===y(this,a,"f")}readInto(t){if(!Buffer.isBuffer(t))throw new TypeError("bufferOut must be a Buffer");let e=0,s=0;for(;s<t.length&&y(this,o,"f").length>0;){const i=y(this,o,"f")[0],r=i.length-y(this,h,"f"),n=t.length-s,a=Math.min(r,n);i.copy(t,s,y(this,h,"f"),y(this,h,"f")+a),e+=a,s+=a,a<r?C(this,h,y(this,h,"f")+a,"f"):(y(this,o,"f").shift(),C(this,h,0,"f"))}return C(this,a,y(this,a,"f")-e,"f"),y(this,n,"m",c).call(this),e}append(t){if(!Buffer.isBuffer(t))throw new TypeError("buffer must be a Buffer");0!==t.length&&(C(this,a,y(this,a,"f")+t.length,"f"),y(this,o,"f").push(t),y(this,n,"m",c).call(this))}clear(){C(this,o,[],"f"),C(this,h,0,"f"),C(this,a,0,"f"),y(this,n,"m",c).call(this)}read(t){if(t>y(this,a,"f"))throw new RangeError(`Cannot read ${t} bytes, only ${y(this,a,"f")} available`);if(0===t)return Buffer.allocUnsafe(0);const e=Buffer.allocUnsafe(t),s=this.readInto(e);if(s!==t)throw new Error(`Internal error: read ${s} bytes, expected ${t}`);return e}peek(t=y(this,a,"f")){const e=Math.min(t,y(this,a,"f"));if(0===e)return Buffer.allocUnsafe(0);const s=Buffer.allocUnsafe(e);let i=0,r=0,n=y(this,h,"f");for(;i<e&&r<y(this,o,"f").length;){const t=y(this,o,"f")[r],a=t.length-n,h=Math.min(a,e-i);t.copy(s,i,n,n+h),i+=h,r++,n=0}return s}},exports.RTCCertificate=It,exports.RTCDataChannel=Tt,exports.RTCDataChannelState=wt,exports.RTCError=R,exports.RTCIceCandidate=L,exports.RTCIceGatheringState=Kn,exports.RTCPeerConnection=$n,exports.RTCPeerConnectionState=Gn,exports.RTCSdpType=Ct,exports.RTCSessionDescription=At,exports.RTCSignalingState=Fn,exports.version=jn;
|
package/dist/index.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import*as t from"crypto";import{EventEmitter as e}from"events";import*as s from"dgram";import*as i from"os";function r(t,e,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(t):i?i.value:e.get(t)}function n(t,e,s,i,r){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?r.call(t,s):r?r.value=s:e.set(t,s),s}
|
|
2
|
-
/**
|
|
3
|
-
* @file ByteBufferQueue - Efficient byte buffer with O(1) append and O(n) read.
|
|
4
|
-
*
|
|
5
|
-
* This class provides efficient management of byte buffers with O(1) append operations
|
|
6
|
-
* and O(n) read operations. Clients can append entire buffers then copy data out across
|
|
7
|
-
* buffer boundaries.
|
|
8
|
-
*
|
|
9
|
-
* @license MIT
|
|
10
|
-
* @author nmhung1210
|
|
11
|
-
*/
|
|
12
|
-
var a,o,h,c,f,l,d,u,p,E,m,I,w,g;"function"==typeof SuppressedError&&SuppressedError;class T{constructor(){a.add(this),o.set(this,void 0),h.set(this,void 0),c.set(this,void 0),n(this,o,0,"f"),n(this,h,[],"f"),n(this,c,0,"f")}get size(){return r(this,o,"f")}get empty(){return 0===r(this,o,"f")}readInto(t){if(!Buffer.isBuffer(t))throw new TypeError("bufferOut must be a Buffer");let e=0,s=0;for(;s<t.length&&r(this,h,"f").length>0;){const i=r(this,h,"f")[0],a=i.length-r(this,c,"f"),o=t.length-s,f=Math.min(a,o);i.copy(t,s,r(this,c,"f"),r(this,c,"f")+f),e+=f,s+=f,f<a?n(this,c,r(this,c,"f")+f,"f"):(r(this,h,"f").shift(),n(this,c,0,"f"))}return n(this,o,r(this,o,"f")-e,"f"),r(this,a,"m",f).call(this),e}append(t){if(!Buffer.isBuffer(t))throw new TypeError("buffer must be a Buffer");0!==t.length&&(n(this,o,r(this,o,"f")+t.length,"f"),r(this,h,"f").push(t),r(this,a,"m",f).call(this))}clear(){n(this,h,[],"f"),n(this,c,0,"f"),n(this,o,0,"f"),r(this,a,"m",f).call(this)}read(t){if(t>r(this,o,"f"))throw new RangeError(`Cannot read ${t} bytes, only ${r(this,o,"f")} available`);if(0===t)return Buffer.allocUnsafe(0);const e=Buffer.allocUnsafe(t),s=this.readInto(e);if(s!==t)throw new Error(`Internal error: read ${s} bytes, expected ${t}`);return e}peek(t=r(this,o,"f")){const e=Math.min(t,r(this,o,"f"));if(0===e)return Buffer.allocUnsafe(0);const s=Buffer.allocUnsafe(e);let i=0,n=0,a=r(this,c,"f");for(;i<e&&n<r(this,h,"f").length;){const t=r(this,h,"f")[n],o=t.length-a,c=Math.min(o,e-i);t.copy(s,i,a,a+c),i+=c,n++,a=0}return s}}o=new WeakMap,h=new WeakMap,c=new WeakMap,a=new WeakSet,f=function(){if("production"!==process.env.NODE_ENV){let t=0;for(const e of r(this,h,"f")){if(0===e.length)throw new Error("Invariant violation: empty buffer in queue");t+=e.length}const e=t-r(this,c,"f");if(r(this,o,"f")!==e)throw new Error(`Invariant violation: size=${r(this,o,"f")}, expected=${e}`);if(0===r(this,h,"f").length){if(0!==r(this,c,"f"))throw new Error("Invariant violation: offset non-zero with empty queue")}else if(r(this,c,"f")>=r(this,h,"f")[0].length)throw new Error("Invariant violation: offset >= front buffer size")}};const B=Object.freeze({NONE:"none",DATA_CHANNEL_FAILURE:"data-channel-failure",DTLS_FAILURE:"dtls-failure",FINGERPRINT_FAILURE:"fingerprint-failure",SCTP_FAILURE:"sctp-failure",SDP_SYNTAX_ERROR:"sdp-syntax-error",HARDWARE_ENCODER_NOT_AVAILABLE:"hardware-encoder-not-available",HARDWARE_ENCODER_ERROR:"hardware-encoder-error",INVALID_STATE:"invalid-state",INVALID_MODIFICATION:"invalid-modification",INVALID_ACCESS_ERROR:"invalid-access-error",OPERATION_ERROR:"operation-error"});class y extends Error{constructor(t={},e=""){super(e),l.add(this),d.set(this,void 0),u.set(this,void 0),p.set(this,void 0),E.set(this,void 0),m.set(this,void 0),I.set(this,void 0),this.name="RTCError",Error.captureStackTrace&&Error.captureStackTrace(this,y);const s=t.errorDetail||B.NONE;if("string"!=typeof s)throw new TypeError("errorDetail must be a string");n(this,d,s,"f"),n(this,u,r(this,l,"m",w).call(this,t.sdpLineNumber,"sdpLineNumber"),"f"),n(this,p,r(this,l,"m",w).call(this,t.httpRequestStatusCode,"httpRequestStatusCode"),"f"),n(this,E,r(this,l,"m",w).call(this,t.sctpCauseCode,"sctpCauseCode"),"f"),n(this,m,r(this,l,"m",g).call(this,t.receivedAlert,"receivedAlert"),"f"),n(this,I,r(this,l,"m",g).call(this,t.sentAlert,"sentAlert"),"f")}get errorDetail(){return r(this,d,"f")}get sdpLineNumber(){return r(this,u,"f")}get httpRequestStatusCode(){return r(this,p,"f")}get sctpCauseCode(){return r(this,E,"f")}get receivedAlert(){return r(this,m,"f")}get sentAlert(){return r(this,I,"f")}toJSON(){const t={name:this.name,message:this.message,errorDetail:r(this,d,"f")};return null!==r(this,u,"f")&&(t.sdpLineNumber=r(this,u,"f")),null!==r(this,p,"f")&&(t.httpRequestStatusCode=r(this,p,"f")),null!==r(this,E,"f")&&(t.sctpCauseCode=r(this,E,"f")),null!==r(this,m,"f")&&(t.receivedAlert=r(this,m,"f")),null!==r(this,I,"f")&&(t.sentAlert=r(this,I,"f")),t}static fromNative(t){const e={errorDetail:t.error_detail||B.NONE};return void 0!==t.sctp_cause_code&&(e.sctpCauseCode=t.sctp_cause_code),new y(e,t.message||"Unknown error")}}
|
|
13
|
-
/**
|
|
14
|
-
* @file RTCIceCandidate - ICE candidate representation.
|
|
15
|
-
*
|
|
16
|
-
* Implements the W3C RTCIceCandidate interface
|
|
17
|
-
* (https://www.w3.org/TR/webrtc/#rtcicecandidate-interface).
|
|
18
|
-
*
|
|
19
|
-
* Represents an ICE (Interactive Connectivity Establishment) candidate that
|
|
20
|
-
* describes a potential way to establish a connection with a peer.
|
|
21
|
-
*
|
|
22
|
-
* @license MIT
|
|
23
|
-
* @author nmhung1210
|
|
24
|
-
*/
|
|
25
|
-
var A,C,R,S,N,v,U;d=new WeakMap,u=new WeakMap,p=new WeakMap,E=new WeakMap,m=new WeakMap,I=new WeakMap,l=new WeakSet,w=function(t,e){if(null==t)return null;const s=Number(t);if(!Number.isInteger(s))throw new TypeError(`${e} must be an integer`);return s},g=function(t,e){if(null==t)return null;const s=Number(t);if(!Number.isInteger(s)||s<0)throw new TypeError(`${e} must be an unsigned integer`);return s},y.DetailType=B;class O{constructor(t={}){if(A.add(this),C.set(this,void 0),R.set(this,void 0),S.set(this,void 0),N.set(this,void 0),v.set(this,void 0),null===t.sdpMid&&null===t.sdpMLineIndex)throw new TypeError("sdpMid and sdpMLineIndex are both null");n(this,C,t.candidate||"","f"),n(this,R,void 0!==t.sdpMid?t.sdpMid:null,"f"),n(this,S,void 0!==t.sdpMLineIndex?t.sdpMLineIndex:null,"f"),n(this,N,t.usernameFragment||null,"f"),n(this,v,r(this,A,"m",U).call(this,r(this,C,"f")),"f")}get candidate(){return r(this,C,"f")}get sdpMid(){return r(this,R,"f")}get sdpMLineIndex(){return r(this,S,"f")}get usernameFragment(){return r(this,N,"f")}get foundation(){return r(this,v,"f").foundation}get component(){return r(this,v,"f").component}get priority(){return r(this,v,"f").priority}get address(){return r(this,v,"f").address}get protocol(){return r(this,v,"f").protocol}get port(){return r(this,v,"f").port}get type(){return r(this,v,"f").type}get tcpType(){return r(this,v,"f").tcpType}get relatedAddress(){return r(this,v,"f").relatedAddress}get relatedPort(){return r(this,v,"f").relatedPort}toJSON(){const t={candidate:r(this,C,"f"),sdpMid:r(this,R,"f"),sdpMLineIndex:r(this,S,"f")};return r(this,N,"f")&&(t.usernameFragment=r(this,N,"f")),t}static fromString(t,e=null,s=0){return new O({candidate:t,sdpMid:e,sdpMLineIndex:s})}static isValid(t){if(!t||"string"!=typeof t)return!1;if(!t.startsWith("candidate:"))return!1;return t.substring(10).trim().split(/\s+/).length>=8}}C=new WeakMap,R=new WeakMap,S=new WeakMap,N=new WeakMap,v=new WeakMap,A=new WeakSet,U=function(t){const e={foundation:null,component:null,protocol:null,priority:null,address:null,port:null,type:null,tcpType:null,relatedAddress:null,relatedPort:null};if(!t||!t.startsWith("candidate:"))return e;const s=t.substring(10).trim().split(/\s+/);if(s.length<8)return e;e.foundation=s[0],e.component=s[1],e.protocol=s[2].toLowerCase(),e.priority=parseInt(s[3],10),e.address=s[4],e.port=parseInt(s[5],10),"typ"===s[6]&&(e.type=s[7]);for(let t=8;t<s.length;t+=2){const i=s[t],r=s[t+1];"raddr"===i?e.relatedAddress=r:"rport"===i?e.relatedPort=parseInt(r,10):"tcptype"===i&&(e.tcpType=r)}return e};const _=Object.freeze({BOOLEAN:1,INTEGER:2,BIT_STRING:3,OCTET_STRING:4,NULL:5,OID:6,UTF8_STRING:12,PRINTABLE_STRING:19,IA5_STRING:22,UTC_TIME:23,GENERALIZED_TIME:24,SEQUENCE:48,SET:49});function D(t){if(t<128)return Buffer.from([t]);const e=[];let s=t;for(;s>0;)e.unshift(255&s),s>>>=8;return Buffer.from([128|e.length,...e])}function L(t,e){return Buffer.concat([Buffer.from([t]),D(e.length),e])}function k(t){let e=0;for(;e<t.length-1&&0===t[e];)e++;let s=t.slice(e);return 128&s[0]&&(s=Buffer.concat([Buffer.from([0]),s])),L(_.INTEGER,s)}function M(t){const e=[];let s=t;for(;s>0;)e.unshift(255&s),s=Math.floor(s/256);return 128&e[0]&&e.unshift(0),L(_.INTEGER,Buffer.from(e))}function b(t){const e=t.split(".").map(Number);if(e.length<2)throw new Error(`Invalid OID: ${t}`);const s=[40*e[0]+e[1]];for(let t=2;t<e.length;t++){let i=e[t];const r=[127&i];for(i=Math.floor(i/128);i>0;)r.unshift(127&i|128),i=Math.floor(i/128);s.push(...r)}return L(_.OID,Buffer.from(s))}function P(t){return L(_.SEQUENCE,Buffer.concat(t))}function W(t){const e=t.getUTCFullYear(),s=(t,e=2)=>String(t).padStart(e,"0"),i=s(t.getUTCMonth()+1),r=s(t.getUTCDate()),n=s(t.getUTCHours()),a=s(t.getUTCMinutes()),o=s(t.getUTCSeconds());if(e<2050){const t=s(e%100);return L(_.UTC_TIME,Buffer.from(`${t}${i}${r}${n}${a}${o}Z`,"ascii"))}return L(_.GENERALIZED_TIME,Buffer.from(`${e}${i}${r}${n}${a}${o}Z`,"ascii"))}const H=Object.freeze({ecPublicKey:"1.2.840.10045.2.1",prime256v1:"1.2.840.10045.3.1.7",ecdsaWithSHA256:"1.2.840.10045.4.3.2",commonName:"2.5.4.3"});function F(t){const e=P([b(H.commonName),(s=t,L(_.UTF8_STRING,Buffer.from(s,"utf8")))]);var s;var i;return P([(i=[e],L(_.SET,Buffer.concat(i)))])}function K(){return P([b(H.ecdsaWithSHA256)])}function x(e,s="sha-256"){const i=s.replace("-","").toLowerCase();return t.createHash(i).update(e).digest("hex").toUpperCase().match(/.{2}/g).join(":")}var G,$,j,V,z,Y,q,X,Q,Z,J,tt,et,st,it,rt,nt,at,ot,ht,ct,ft,lt;function dt(e={}){const{name:s,days:i=30}=e,{certDer:r,privateKey:n,publicKey:a,notAfter:o}=function(e={}){const s=e.commonName||`WebRTC-${t.randomBytes(8).toString("hex")}`,i=e.days||30,{publicKey:r,privateKey:n}=t.generateKeyPairSync("ec",{namedCurve:"prime256v1"}),a=r.export({type:"spki",format:"der"}),o=e.notBefore||new Date(Date.now()-864e5),h=new Date(o.getTime()+24*i*60*60*1e3),c=t.randomBytes(20);c[0]&=127,0===c[0]&&(c[0]=1);const f=F(s),l=P([(d=0,u=M(2),L(160|d,u)),k(c),K(),f,P([W(o),W(h)]),f,a]);var d,u;const p=t.sign("sha256",l,n);var E;return{certDer:P([l,K(),(E=p,L(_.BIT_STRING,Buffer.concat([Buffer.from([0]),E])))]),privateKey:n,publicKey:r,notBefore:o,notAfter:h}}({commonName:s,days:i});return{certDer:r,privateKey:n,publicKey:a,expires:o.getTime(),hash:"sha256"}}function ut(t,e="sha-256"){return x(t,e)}class pt{constructor(t){G.add(this),$.set(this,void 0),j.set(this,void 0),V.set(this,void 0),z.set(this,void 0),Y.set(this,void 0),n(this,$,t.certDer||null,"f"),n(this,j,t.privateKey,"f"),n(this,V,t.publicKey,"f"),n(this,z,t.expires,"f"),n(this,Y,null,"f")}getCertificateDer(){return r(this,$,"f")}get expires(){return r(this,z,"f")}getFingerprints(){if(!r(this,$,"f"))throw new Error("Certificate has no DER encoding; cannot compute fingerprint");if(!r(this,Y,"f")){const t=r(this,$,"f");n(this,Y,["sha-256","sha-384","sha-512"].map(e=>({algorithm:e,value:ut(t,e)})),"f")}return r(this,Y,"f").map(t=>({...t}))}getPrivateKeyObject(){return r(this,G,"m",q).call(this,r(this,j,"f"),"private")}getPrivateKey(){return r(this,G,"m",q).call(this,r(this,j,"f"),"private").export({type:"pkcs8",format:"pem"})}getPublicKey(){return r(this,G,"m",q).call(this,r(this,V,"f"),"public").export({type:"spki",format:"pem"})}toPEM(){const t=r(this,$,"f")?`-----BEGIN CERTIFICATE-----\n${r(this,$,"f").toString("base64").match(/.{1,64}/g).join("\n")}\n-----END CERTIFICATE-----\n`:this.getPublicKey();return{pemPrivateKey:this.getPrivateKey(),pemCertificate:t}}isExpired(){return Date.now()>r(this,z,"f")}static async generateCertificate(t={}){return new Promise((e,s)=>{try{let i;if(t.expires)i=t.expires;else{const e=t.days||30;i=Date.now()+24*e*60*60*1e3}setImmediate(()=>{try{const s=dt({name:t.name||"webrtc",days:Math.ceil((i-Date.now())/864e5),hash:t.hash||"sha256"});s.expires=i;const r=new pt(s);e(r)}catch(t){s(t)}})}catch(t){s(t)}})}static fromPEM(e,s,i){if("string"!=typeof e||0===e.length)throw new TypeError("pemPrivateKey must be a non-empty string");if("string"!=typeof s||0===s.length)throw new TypeError("pemCertificate must be a non-empty string");const r=i||Date.now()+2592e6;let n=null,a=s;const o=s.match(/-----BEGIN CERTIFICATE-----([\s\S]+?)-----END CERTIFICATE-----/);return o&&(n=Buffer.from(o[1].replace(/\s/g,""),"base64"),a=t.createPublicKey(t.createPrivateKey(e))),new pt({certDer:n,privateKey:e,publicKey:a,expires:r,hash:"sha256"})}static isSupportedKeyParams(t){if(!t||"object"!=typeof t)return!1;if("RSA"===t.type){const e=t.rsaModulusLength||2048;return e>=1024&&e<=4096}if("ECDSA"===t.type){const e=t.namedCurve;return["P-256","P-384","P-521"].includes(e)}return!1}}$=new WeakMap,j=new WeakMap,V=new WeakMap,z=new WeakMap,Y=new WeakMap,G=new WeakSet,q=function(e,s){return e&&"object"==typeof e&&e.type?e:"private"===s?t.createPrivateKey(e):t.createPublicKey(e)};const Et=Object.freeze({CONNECTING:"connecting",OPEN:"open",CLOSING:"closing",CLOSED:"closed"}),mt=Object.freeze({SEND:Symbol("rtcdatachannel:send"),RECEIVE:Symbol("rtcdatachannel:receive"),OPEN:Symbol("rtcdatachannel:open"),SET_ID:Symbol("rtcdatachannel:setId")});class It extends e{constructor(t,e={}){if(super(),X.add(this),Q.set(this,void 0),Z.set(this,void 0),J.set(this,void 0),tt.set(this,void 0),et.set(this,void 0),st.set(this,void 0),it.set(this,void 0),rt.set(this,void 0),nt.set(this,void 0),at.set(this,void 0),ot.set(this,void 0),ht.set(this,void 0),"string"!=typeof t)throw new TypeError("label must be a string");n(this,Q,t,"f"),n(this,Z,void 0===e.ordered||e.ordered,"f"),n(this,J,e.maxPacketLifeTime||null,"f"),n(this,tt,e.maxRetransmits||null,"f"),n(this,et,e.protocol||"","f"),n(this,st,e.negotiated||!1,"f"),n(this,it,void 0!==e.id?e.id:null,"f"),n(this,rt,Et.CONNECTING,"f"),n(this,nt,0,"f"),n(this,at,0,"f"),n(this,ot,"arraybuffer","f"),n(this,ht,!1,"f"),this.on(mt.SET_ID,t=>{n(this,it,t,"f")}),this.on(mt.OPEN,()=>{n(this,ht,!0,"f"),r(this,X,"m",ft).call(this,Et.OPEN)}),this.on(mt.RECEIVE,(t,e)=>{r(this,X,"m",lt).call(this,t,e)})}get label(){return r(this,Q,"f")}get ordered(){return r(this,Z,"f")}get maxPacketLifeTime(){return r(this,J,"f")}get maxRetransmits(){return r(this,tt,"f")}get protocol(){return r(this,et,"f")}get negotiated(){return r(this,st,"f")}get id(){return r(this,it,"f")}get readyState(){return r(this,rt,"f")}get bufferedAmount(){return r(this,nt,"f")}get bufferedAmountLowThreshold(){return r(this,at,"f")}set bufferedAmountLowThreshold(t){n(this,at,t,"f")}get binaryType(){return r(this,ot,"f")}set binaryType(t){if("arraybuffer"!==t&&"blob"!==t)throw new TypeError('binaryType must be "arraybuffer" or "blob"');n(this,ot,t,"f")}get reliable(){return r(this,Z,"f")&&null===r(this,J,"f")&&null===r(this,tt,"f")}send(t){if(r(this,rt,"f")!==Et.OPEN)throw new Error('RTCDataChannel.readyState is not "open"');let e,s,i=0;if("string"==typeof t)e=Buffer.from(t,"utf8"),i=e.length,s=!1;else if(t instanceof ArrayBuffer)e=Buffer.from(t),i=t.byteLength,s=!0;else if(ArrayBuffer.isView(t))e=Buffer.from(t.buffer,t.byteOffset,t.byteLength),i=t.byteLength,s=!0;else{if(!Buffer.isBuffer(t))throw t&&"function"==typeof t.arrayBuffer?new Error("Blob sending not yet implemented"):new TypeError("Invalid data type");e=t,i=t.length,s=!0}if(!r(this,ht,"f"))throw new Error("Data channel not connected to a transport");n(this,nt,r(this,nt,"f")+i,"f");try{this.emit(mt.SEND,e,s),n(this,nt,Math.max(0,r(this,nt,"f")-i),"f"),r(this,X,"m",ct).call(this)}catch(t){throw n(this,nt,Math.max(0,r(this,nt,"f")-i),"f"),this.emit("error",t),t}}close(){r(this,rt,"f")!==Et.CLOSING&&r(this,rt,"f")!==Et.CLOSED&&(r(this,X,"m",ft).call(this,Et.CLOSING),setImmediate(()=>{r(this,rt,"f")===Et.CLOSING&&r(this,X,"m",ft).call(this,Et.CLOSED)}))}}var wt,gt;Q=new WeakMap,Z=new WeakMap,J=new WeakMap,tt=new WeakMap,et=new WeakMap,st=new WeakMap,it=new WeakMap,rt=new WeakMap,nt=new WeakMap,at=new WeakMap,ot=new WeakMap,ht=new WeakMap,X=new WeakSet,ct=function(){r(this,nt,"f")<=r(this,at,"f")&&this.emit("bufferedamountlow")},ft=function(t){r(this,rt,"f")!==t&&(n(this,rt,t,"f"),t===Et.OPEN?this.emit("open"):t===Et.CLOSING?this.emit("closing"):t===Et.CLOSED&&this.emit("close"))},lt=function(t,e){let s;s=e?"arraybuffer"===r(this,ot,"f")?t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength):t:t.toString("utf8"),this.emit("message",{data:s})};const Tt=Object.freeze({OFFER:"offer",PRANSWER:"pranswer",ANSWER:"answer",ROLLBACK:"rollback"});class Bt{constructor(t={}){if(wt.set(this,void 0),gt.set(this,void 0),n(this,wt,t.type||null,"f"),n(this,gt,t.sdp||null,"f"),r(this,wt,"f")&&!Object.values(Tt).includes(r(this,wt,"f")))throw new TypeError(`Invalid SDP type: ${r(this,wt,"f")}`)}get type(){return r(this,wt,"f")}set type(t){if(t&&!Object.values(Tt).includes(t))throw new TypeError(`Invalid SDP type: ${t}`);n(this,wt,t,"f")}get sdp(){return r(this,gt,"f")}set sdp(t){n(this,gt,t,"f")}toJSON(){return{type:r(this,wt,"f"),sdp:r(this,gt,"f")}}}function yt(e){const{iceUfrag:s,icePwd:i,fingerprint:r,setup:n="actpass",candidates:a=[],sctpPort:o=5e3,maxMessageSize:h=262144}=e,c=[];c.push("v=0");const f=t.randomBytes(4).readUInt32BE(0);c.push(`o=- ${f} 2 IN IP4 127.0.0.1`),c.push("s=-"),c.push("t=0 0"),c.push("a=group:BUNDLE 0"),c.push("a=msid-semantic: WMS"),c.push("m=application 9 UDP/DTLS/SCTP webrtc-datachannel"),c.push("c=IN IP4 0.0.0.0"),c.push("a=ice-ufrag:"+s),c.push("a=ice-pwd:"+i),c.push("a=ice-options:trickle"),r&&c.push(`a=fingerprint:${r.algorithm} ${r.value}`),c.push(`a=setup:${n}`),c.push("a=mid:0"),c.push("a=sctp-port:"+o),c.push("a=max-message-size:"+h);for(const t of a){const e=t.sdp||t.candidate;e&&c.push("a="+(e.startsWith("candidate:")?e:"candidate:"+e))}return c.join("\r\n")+"\r\n"}function At(t){const e=(t.startsWith("candidate:")?t.slice(10):t).split(/\s+/);return e.length<8?null:{candidate:t.startsWith("candidate:")?t:"candidate:"+t,foundation:e[0],component:parseInt(e[1],10),protocol:e[2].toLowerCase(),priority:parseInt(e[3],10)>>>0,address:e[4],port:parseInt(e[5],10),type:e[7]}}var Ct,Rt,St;wt=new WeakMap,gt=new WeakMap;const Nt=554869826,vt=Object.freeze({BINDING_REQUEST:1,BINDING_SUCCESS:257,BINDING_ERROR:273}),Ut=Object.freeze({MAPPED_ADDRESS:1,USERNAME:6,MESSAGE_INTEGRITY:8,ERROR_CODE:9,XOR_MAPPED_ADDRESS:32,PRIORITY:36,USE_CANDIDATE:37,FINGERPRINT:32808,ICE_CONTROLLED:32809,ICE_CONTROLLING:32810});function Ot(t){return t+3&-4}const _t=(()=>{const t=new Uint32Array(256);for(let e=0;e<256;e++){let s=e;for(let t=0;t<8;t++)s=1&s?3988292384^s>>>1:s>>>1;t[e]=s>>>0}return t})();class Dt{constructor(e,s){Ct.add(this),this.type=e,this.transactionId=s||t.randomBytes(12),this.attrs=[]}addAttr(t,e){return this.attrs.push({type:t,value:e}),this}addUsername(t){return this.addAttr(Ut.USERNAME,Buffer.from(t,"utf8"))}addPriority(t){const e=Buffer.alloc(4);return e.writeUInt32BE(t>>>0,0),this.addAttr(Ut.PRIORITY,e)}addIceControlling(t){return this.addAttr(Ut.ICE_CONTROLLING,t)}addIceControlled(t){return this.addAttr(Ut.ICE_CONTROLLED,t)}addUseCandidate(){return this.addAttr(Ut.USE_CANDIDATE,Buffer.alloc(0))}addXorMappedAddress(t,e){return this.addAttr(Ut.XOR_MAPPED_ADDRESS,function(t,e){const s=Buffer.alloc(8);s.writeUInt8(0,0),s.writeUInt8(1,1),s.writeUInt16BE(8466^e,2);const i=t.split(".").map(Number),r=(i[0]<<24|i[1]<<16|i[2]<<8|i[3])>>>0;return s.writeUInt32BE((r^Nt)>>>0,4),s}(t,e,this.transactionId))}build(e){let s=r(this,Ct,"m",Rt).call(this);if(e){const i=s.length+24,n=r(this,Ct,"m",St).call(this,i),a=t.createHmac("sha1",Buffer.from(e,"utf8")).update(Buffer.concat([n,s])).digest(),o=Buffer.alloc(4);o.writeUInt16BE(Ut.MESSAGE_INTEGRITY,0),o.writeUInt16BE(20,2),s=Buffer.concat([s,o,a])}const i=s.length+8,n=r(this,Ct,"m",St).call(this,i),a=(1398035790^function(t){let e=4294967295;for(let s=0;s<t.length;s++)e=_t[255&(e^t[s])]^e>>>8;return(4294967295^e)>>>0}(Buffer.concat([n,s])))>>>0,o=Buffer.alloc(8);return o.writeUInt16BE(Ut.FINGERPRINT,0),o.writeUInt16BE(4,2),o.writeUInt32BE(a,4),s=Buffer.concat([s,o]),Buffer.concat([r(this,Ct,"m",St).call(this,s.length),s])}}var Lt,kt,Mt,bt,Pt,Wt,Ht,Ft,Kt,xt,Gt,$t,jt,Vt,zt,Yt,qt,Xt,Qt,Zt,Jt,te,ee,se;Ct=new WeakSet,Rt=function(){const t=[];for(const e of this.attrs){const s=Buffer.alloc(4);s.writeUInt16BE(e.type,0),s.writeUInt16BE(e.value.length,2);const i=Buffer.alloc(Ot(e.value.length));e.value.copy(i,0),t.push(s,i)}return Buffer.concat(t)},St=function(t){const e=Buffer.alloc(20);return e.writeUInt16BE(this.type,0),e.writeUInt16BE(t,2),e.writeUInt32BE(Nt,4),this.transactionId.copy(e,8),e};const ie=1,re=257,ne=3,ae=259,oe=4,he=260,ce=22,fe=23,le=8,de=264,ue=265,pe=1,Ee=6,me=8,Ie=9,we=20,ge=21,Te=32,Be=13,ye=18,Ae=19,Ce=22,Re=25,Se=554869826;class Ne extends e{constructor(t){super(),Lt.add(this),kt.set(this,void 0),Mt.set(this,void 0),bt.set(this,void 0),Pt.set(this,void 0),Wt.set(this,void 0),Ht.set(this,void 0),Ft.set(this,void 0),Kt.set(this,void 0),n(this,kt,t.server,"f"),n(this,Mt,t.port,"f"),n(this,bt,t.username,"f"),n(this,Pt,t.credential,"f"),n(this,Wt,null,"f"),n(this,Ht,new Map,"f"),n(this,Ft,null,"f"),n(this,Kt,null,"f")}async connect(){if(!r(this,Wt,"f"))return new Promise((t,e)=>{const i=s.createSocket("udp4");n(this,Wt,i,"f"),i.on("message",(t,e)=>{r(this,Lt,"m",Zt).call(this,t,e)}),i.on("error",t=>{console.error("STUN socket error:",t),e(t)}),i.bind(()=>{t()})})}async getReflexiveAddress(){await this.connect();const e=t.randomBytes(12),s=r(this,Lt,"m",$t).call(this,e);return new Promise((t,i)=>{const n=setTimeout(()=>{r(this,Ht,"f").delete(e.toString("hex")),i(new Error("STUN request timeout"))},5e3);r(this,Ht,"f").set(e.toString("hex"),{resolve:e=>{clearTimeout(n),t(e)},reject:t=>{clearTimeout(n),i(t)}}),r(this,Wt,"f").send(s,r(this,Mt,"f"),r(this,kt,"f"),t=>{t&&(clearTimeout(n),r(this,Ht,"f").delete(e.toString("hex")),i(t))})})}async allocateRelay(e=600){if(!r(this,bt,"f")||!r(this,Pt,"f"))throw new Error("TURN requires username and credential");await this.connect();let s=t.randomBytes(12),i=r(this,Lt,"m",jt).call(this,s,e);try{return await r(this,Lt,"m",Gt).call(this,i,s,"allocate")}catch(n){if(n instanceof Error&&n.message.includes("401")&&r(this,Ft,"f")&&r(this,Kt,"f"))return s=t.randomBytes(12),i=r(this,Lt,"m",jt).call(this,s,e,!0),await r(this,Lt,"m",Gt).call(this,i,s,"allocate");throw n}}async refreshAllocation(e=600){if(!r(this,bt,"f")||!r(this,Pt,"f"))throw new Error("TURN requires username and credential");return r(this,Lt,"m",xt).call(this,"refresh",()=>{const s=t.randomBytes(12);return{transactionId:s,request:r(this,Lt,"m",qt).call(this,s,e)}})}async createPermission(e){if(!r(this,bt,"f")||!r(this,Pt,"f"))throw new Error("TURN requires username and credential");await r(this,Lt,"m",xt).call(this,"createPermission",()=>{const s=t.randomBytes(12);return{transactionId:s,request:r(this,Lt,"m",Vt).call(this,s,e)}})}async sendIndication(e,s,i){if(!r(this,bt,"f")||!r(this,Pt,"f"))throw new Error("TURN requires username and credential");const n=t.randomBytes(12),a=r(this,Lt,"m",zt).call(this,n,e,s,i);return new Promise((t,e)=>{r(this,Wt,"f").send(a,r(this,Mt,"f"),r(this,kt,"f"),s=>{s?e(s):t()})})}close(){r(this,Wt,"f")&&(r(this,Wt,"f").close(),n(this,Wt,null,"f")),r(this,Ht,"f").clear()}}var ve,Ue,Oe,_e,De,Le,ke,Me,be,Pe,We,He,Fe,Ke,xe,Ge,$e,je,Ve,ze,Ye,qe,Xe,Qe,Ze,Je,ts,es,ss,is,rs,ns;kt=new WeakMap,Mt=new WeakMap,bt=new WeakMap,Pt=new WeakMap,Wt=new WeakMap,Ht=new WeakMap,Ft=new WeakMap,Kt=new WeakMap,Lt=new WeakSet,xt=async function(t,e){const s=e();try{return await r(this,Lt,"m",Gt).call(this,s.request,s.transactionId,t)}catch(s){if(s instanceof Error&&s.message.includes("401")&&r(this,Ft,"f")&&r(this,Kt,"f")){const s=e();return r(this,Lt,"m",Gt).call(this,s.request,s.transactionId,t)}throw s}},Gt=function(t,e,s){return new Promise((i,n)=>{const a=setTimeout(()=>{r(this,Ht,"f").delete(e.toString("hex")),n(new Error(`${s} request timeout`))},5e3);r(this,Ht,"f").set(e.toString("hex"),{type:s,resolve:t=>{clearTimeout(a),i(t)},reject:t=>{clearTimeout(a),n(t)}}),r(this,Wt,"f").send(t,r(this,Mt,"f"),r(this,kt,"f"),t=>{t&&(clearTimeout(a),r(this,Ht,"f").delete(e.toString("hex")),n(t))})})},$t=function(t){const e=Buffer.alloc(20);return e.writeUInt16BE(ie,0),e.writeUInt16BE(0,2),e.writeUInt32BE(Se,4),t.copy(e,8),e},jt=function(t,e,s=!1){const i=[],n=Buffer.alloc(8);n.writeUInt16BE(Re,0),n.writeUInt16BE(4,2),n.writeUInt8(17,4),i.push(n);const a=Buffer.alloc(8);if(a.writeUInt16BE(Be,0),a.writeUInt16BE(4,2),a.writeUInt32BE(e,4),i.push(a),s&&r(this,Ft,"f")&&r(this,Kt,"f")){const t=r(this,Lt,"m",Qt).call(this,Ee,r(this,bt,"f"));i.push(t);const e=r(this,Lt,"m",Qt).call(this,we,r(this,Ft,"f"));i.push(e);const s=r(this,Lt,"m",Qt).call(this,ge,r(this,Kt,"f"));i.push(s)}return r(this,Lt,"m",Xt).call(this,ne,t,i,s)},Vt=function(t,e){const s=[],i=r(this,Lt,"m",Yt).call(this,e,0,t);return s.push(i),r(this,Ft,"f")&&r(this,Kt,"f")&&(s.push(r(this,Lt,"m",Qt).call(this,Ee,r(this,bt,"f"))),s.push(r(this,Lt,"m",Qt).call(this,we,r(this,Ft,"f"))),s.push(r(this,Lt,"m",Qt).call(this,ge,r(this,Kt,"f")))),r(this,Lt,"m",Xt).call(this,le,t,s,!0)},zt=function(t,e,s,i){const n=[],a=r(this,Lt,"m",Yt).call(this,e,s,t);n.push(a);const o=Buffer.alloc(4+i.length+(4-i.length%4)%4);return o.writeUInt16BE(Ae,0),o.writeUInt16BE(i.length,2),i.copy(o,4),n.push(o),r(this,Lt,"m",Xt).call(this,ce,t,n,!1)},Yt=function(t,e,s){const i=Buffer.alloc(12);i.writeUInt16BE(ye,0),i.writeUInt16BE(8,2),i.writeUInt8(0,4),i.writeUInt8(1,5);const r=8466^e;i.writeUInt16BE(r,6);const n=t.split(".").map(Number),a=(n[0]<<24|n[1]<<16|n[2]<<8|n[3])^Se;return i.writeUInt32BE(a>>>0,8),i},qt=function(t,e){const s=[],i=Buffer.alloc(8);i.writeUInt16BE(Be,0),i.writeUInt16BE(4,2),i.writeUInt32BE(e,4),s.push(i);const n=r(this,Lt,"m",Qt).call(this,Ee,r(this,bt,"f"));if(s.push(n),r(this,Ft,"f")){const t=r(this,Lt,"m",Qt).call(this,we,r(this,Ft,"f"));s.push(t)}if(r(this,Kt,"f")){const t=r(this,Lt,"m",Qt).call(this,ge,r(this,Kt,"f"));s.push(t)}return r(this,Lt,"m",Xt).call(this,oe,t,s,!0)},Xt=function(e,s,i,n=!1){let a=Buffer.concat(i);if(n&&r(this,Pt,"f")){const i=Buffer.alloc(20);i.writeUInt16BE(e,0),i.writeUInt16BE(a.length+24,2),i.writeUInt32BE(Se,4),s.copy(i,8);const n=Buffer.concat([i,a]);let o=r(this,Pt,"f");if(r(this,bt,"f")&&r(this,Ft,"f")){const e=`${r(this,bt,"f")}:${r(this,Ft,"f")}:${r(this,Pt,"f")}`;o=t.createHash("md5").update(e).digest()}const h=t.createHmac("sha1",o);h.update(n);const c=h.digest(),f=Buffer.alloc(4+c.length);f.writeUInt16BE(me,0),f.writeUInt16BE(c.length,2),c.copy(f,4),a=Buffer.concat([a,f])}const o=Buffer.alloc(20);return o.writeUInt16BE(e,0),o.writeUInt16BE(a.length,2),o.writeUInt32BE(Se,4),s.copy(o,8),Buffer.concat([o,a])},Qt=function(t,e){const s=Buffer.from(e,"utf8"),i=s.length,r=(4-i%4)%4,n=Buffer.alloc(4+i+r);return n.writeUInt16BE(t,0),n.writeUInt16BE(i,2),s.copy(n,4),n},Zt=function(t,e){if(t.length<20)return;const s=t.readUInt16BE(0),i=t.readUInt16BE(2),a=t.readUInt32BE(4),o=t.slice(8,20);if(a!==Se)return;if(s===fe){const e=r(this,Lt,"m",Jt).call(this,t.slice(20,20+i),o);if(e.xorPeerAddress&&e.data){const t={address:e.xorPeerAddress.address,port:e.xorPeerAddress.port,family:e.xorPeerAddress.family||"IPv4"};this.emit("data",e.data,t)}return}const h=o.toString("hex"),c=r(this,Ht,"f").get(h);if(!c)return;const f=r(this,Lt,"m",Jt).call(this,t.slice(20,20+i),o);if(s===re)f.xorMappedAddress?c.resolve({address:f.xorMappedAddress.address,port:f.xorMappedAddress.port,family:f.xorMappedAddress.family}):f.mappedAddress?c.resolve({address:f.mappedAddress.address,port:f.mappedAddress.port,family:f.mappedAddress.family}):c.reject(new Error("No mapped address in STUN response")),r(this,Ht,"f").delete(h);else if(s===ae)f.xorRelayedAddress?c.resolve({relayedAddress:f.xorRelayedAddress.address,relayedPort:f.xorRelayedAddress.port,lifetime:f.lifetime||600,type:"relay"}):c.reject(new Error("No relayed address in ALLOCATE response")),r(this,Ht,"f").delete(h);else if(s===he)c.resolve({lifetime:f.lifetime||600}),r(this,Ht,"f").delete(h);else if(s===de||s===ue)c.resolve({ok:!0}),r(this,Ht,"f").delete(h);else if(!(272&~s)){f.realm&&n(this,Ft,f.realm,"f"),f.nonce&&n(this,Kt,f.nonce,"f");const t=f.errorCode||"Unknown error";c.reject(new Error(`STUN error: ${t}`)),r(this,Ht,"f").delete(h)}},Jt=function(t,e){const s={};let i=0;for(;i<t.length&&!(i+4>t.length);){const a=t.readUInt16BE(i),o=t.readUInt16BE(i+2);if(i+=4,i+o>t.length)break;const h=t.slice(i,i+o);switch(a){case Te:s.xorMappedAddress=r(this,Lt,"m",te).call(this,h,e);break;case Ce:s.xorRelayedAddress=r(this,Lt,"m",te).call(this,h,e);break;case ye:s.xorPeerAddress=r(this,Lt,"m",te).call(this,h,e);break;case Ae:s.data=h;break;case pe:s.mappedAddress=r(this,Lt,"m",ee).call(this,h);break;case Be:s.lifetime=h.readUInt32BE(0);break;case Ie:s.errorCode=r(this,Lt,"m",se).call(this,h);break;case we:s.realm=h.toString("utf8"),n(this,Ft,s.realm,"f");break;case ge:s.nonce=h.toString("utf8"),n(this,Kt,s.nonce,"f")}i+=o;i+=(4-o%4)%4}return s},te=function(t,e){const s=t.readUInt8(1),i=8466^t.readUInt16BE(2);if(1===s){const e=t.readUInt32BE(4)^Se;return{family:"IPv4",port:i,address:[e>>24&255,e>>16&255,e>>8&255,255&e].join(".")}}return null},ee=function(t){const e=t.readUInt8(1),s=t.readUInt16BE(2);if(1===e){const e=t.slice(4,8);return{family:"IPv4",port:s,address:Array.from(e).join(".")}}return null},se=function(t){return`${100*(7&t.readUInt8(2))+t.readUInt8(3)} ${t.slice(4).toString("utf8")}`};const as={host:126,srflx:100,prflx:110,relay:0};function os(t){const e=t.match(/^(stuns?|turns?):\/?\/?([^:?]+):?(\d+)?(?:\?(.+))?$/);if(!e)return null;const s=e[1],i=e[2],r={};if(e[4])for(const t of e[4].split("&")){if(!t)continue;const e=t.indexOf("=");-1===e?r[t]=!0:r[t.slice(0,e)]=t.slice(e+1)}const n="turns"===s||"stuns"===s?5349:3478;return{scheme:s,protocol:s,host:i,port:parseInt(e[3]||String(n),10),transport:"string"==typeof r.transport?r.transport:"udp",params:r}}class hs{constructor(t){ve.set(this,void 0),this.kind="host",n(this,ve,t,"f"),this.onMessage=null,t.on("message",(t,e)=>{this.onMessage&&this.onMessage(t,{address:e.address,port:e.port})})}send(t,e,s){r(this,ve,"f").send(t,s,e)}close(){try{r(this,ve,"f").close()}catch(t){}}}ve=new WeakMap;class cs{constructor(t){Ue.set(this,void 0),Oe.set(this,void 0),this.kind="relay",n(this,Ue,t,"f"),this.onMessage=null,n(this,Oe,new Set,"f"),t.on("data",(t,e)=>{this.onMessage&&this.onMessage(t,{address:e.address,port:e.port})})}send(t,e,s){const i=`${e}:${s}`;r(this,Oe,"f").has(i)?r(this,Ue,"f").sendIndication(e,s,t).catch(()=>{}):(r(this,Oe,"f").add(i),r(this,Ue,"f").createPermission(e).then(()=>r(this,Ue,"f").sendIndication(e,s,t)).catch(()=>{}))}close(){try{r(this,Ue,"f").close()}catch(t){}}}Ue=new WeakMap,Oe=new WeakMap;class fs extends e{constructor(e){super(),_e.add(this),De.set(this,void 0),Le.set(this,void 0),ke.set(this,void 0),Me.set(this,void 0),be.set(this,void 0),Pe.set(this,void 0),We.set(this,void 0),He.set(this,void 0),Fe.set(this,void 0),Ke.set(this,void 0),xe.set(this,void 0),this.role=e.role,this.localUfrag=e.localUfrag,this.localPwd=e.localPwd,this.remoteUfrag=null,this.remotePwd=null,n(this,De,t.randomBytes(8),"f"),n(this,Le,[],"f"),n(this,ke,[],"f"),n(this,Me,[],"f"),n(this,be,[],"f"),n(this,Pe,null,"f"),n(this,We,!1,"f"),n(this,He,null,"f"),n(this,Fe,null,"f"),n(this,Ke,!1,"f"),n(this,xe,new Map,"f")}async gather(t={}){const e=t.iceServers||[],s="relay"===t.iceTransportPolicy,i=await r(this,_e,"m",Ge).call(this);for(const t of e){const e=Array.isArray(t.urls)?t.urls:[t.urls];for(const n of e){const e=os(n);if(e&&"udp"===e.transport)try{"stun"!==e.scheme||s?"turn"!==e.scheme&&"turns"!==e.scheme||await r(this,_e,"m",Ve).call(this,e,t):await r(this,_e,"m",je).call(this,e,i[0])}catch(t){this.emit("gathererror",{url:n,error:t instanceof Error?t.message:String(t)})}}}s&&n(this,ke,r(this,ke,"f").filter(t=>"relay"===t.type),"f"),this.emit("gatheringcomplete")}getLocalCandidates(){return r(this,ke,"f").slice()}setRemoteCredentials(t,e){this.remoteUfrag=t,this.remotePwd=e}addRemoteCandidate(t){t&&t.address&&t.port&&("string"==typeof t.address&&t.address.endsWith(".local")||(r(this,Me,"f").push(t),r(this,_e,"m",Ye).call(this),!r(this,He,"f")&&this.remotePwd&&r(this,_e,"m",qe).call(this)))}start(){this.remotePwd&&r(this,Me,"f").length>0&&r(this,_e,"m",qe).call(this)}send(t){if(!r(this,Pe,"f"))throw new Error("ICE not connected");r(this,Pe,"f").local.transport.send(t,r(this,Pe,"f").remote.address,r(this,Pe,"f").remote.port)}getSelectedPair(){return r(this,Pe,"f")}getSelectedCandidateType(){return r(this,Pe,"f")?r(this,Pe,"f").local.type:null}close(){if(!r(this,We,"f")){n(this,We,!0,"f"),r(this,_e,"m",Xe).call(this);for(const t of r(this,Le,"f"))try{t.close()}catch(t){}n(this,Le,[],"f"),this.emit("closed")}}}De=new WeakMap,Le=new WeakMap,ke=new WeakMap,Me=new WeakMap,be=new WeakMap,Pe=new WeakMap,We=new WeakMap,He=new WeakMap,Fe=new WeakMap,Ke=new WeakMap,xe=new WeakMap,_e=new WeakSet,Ge=async function(){const t=i.networkInterfaces(),e=[];for(const s of Object.values(t))if(s)for(const t of s)"IPv4"!==t.family||t.internal||e.push(t.address);0===e.length&&e.push("127.0.0.1");const s=[];for(const t of e)s.push(await r(this,_e,"m",$e).call(this,t));return s},$e=function(t){return new Promise((e,i)=>{const n=s.createSocket("udp4");n.on("error",t=>this.emit("error",t)),n.bind(0,t,()=>{const{port:s}=n.address(),i=new hs(n);i.onMessage=(t,e)=>r(this,_e,"m",Je).call(this,i,t,e),r(this,Le,"f").push(i);const a=r(this,_e,"m",ze).call(this,"host",t,s,i);e({socket:n,address:t,port:s,transport:i,candidate:a})})})},je=async function(t,e){if(!e)return;const s=new Ne({server:t.host,port:t.port});try{const t=await s.getReflexiveAddress();r(this,_e,"m",ze).call(this,"srflx",t.address,t.port,e.transport,{relatedAddress:e.address,relatedPort:e.port})}finally{s.close()}},Ve=async function(t,e){if(!e.username||!e.credential)throw new Error("TURN server requires username and credential");const s=new Ne({server:t.host,port:t.port,username:e.username,credential:e.credential,transport:t.transport}),i=await s.allocateRelay(600),n=new cs(s);n.onMessage=(t,e)=>r(this,_e,"m",Je).call(this,n,t,e),r(this,Le,"f").push(n),r(this,_e,"m",ze).call(this,"relay",i.relayedAddress,i.relayedPort,n,{relatedAddress:t.host,relatedPort:t.port})},ze=function(e,s,i,n,a={}){const o=t.createHash("md5").update(`${e}:${s}:${n.kind}`).digest("hex").slice(0,8),h=function(t,e=65535,s=1){return(as[t]<<24)+(e<<8)+(256-s)>>>0}(e);let c=`candidate:${o} 1 udp ${h} ${s} ${i} typ ${e}`;a.relatedAddress&&(c+=` raddr ${a.relatedAddress} rport ${a.relatedPort}`);const f={foundation:o,component:1,protocol:"udp",priority:h,address:s,port:i,type:e,transport:n,sdp:c};return r(this,ke,"f").push(f),this.emit("candidate",f),f},Ye=function(){for(const t of r(this,ke,"f"))for(const e of r(this,Me,"f")){const s=`${t.type}:${t.address}:${t.port}->${e.address}:${e.port}`;r(this,be,"f").find(t=>t.key===s)||r(this,be,"f").push({key:s,local:t,remote:e,state:"frozen",nominated:!1})}},qe=function(){r(this,He,"f")||r(this,We,"f")||(n(this,He,setInterval(()=>r(this,_e,"m",Qe).call(this),50),"f"),r(this,He,"f").unref&&r(this,He,"f").unref(),n(this,Fe,setTimeout(()=>{r(this,Ke,"f")||this.emit("failed"),r(this,_e,"m",Xe).call(this)},1e4),"f"),r(this,Fe,"f").unref&&r(this,Fe,"f").unref(),r(this,_e,"m",Qe).call(this))},Xe=function(){r(this,He,"f")&&(clearInterval(r(this,He,"f")),n(this,He,null,"f")),r(this,Fe,"f")&&(clearTimeout(r(this,Fe,"f")),n(this,Fe,null,"f"))},Qe=function(){if(!r(this,We,"f"))for(const t of r(this,be,"f"))"succeeded"!==t.state&&r(this,_e,"m",Ze).call(this,t)},Ze=function(e){const s=t.randomBytes(12),i=`${this.remoteUfrag}:${this.localUfrag}`,n=new Dt(vt.BINDING_REQUEST,s).addUsername(i).addPriority(e.local.priority);"controlling"===this.role?(n.addIceControlling(r(this,De,"f")),n.addUseCandidate()):n.addIceControlled(r(this,De,"f"));const a=n.build(this.remotePwd??void 0);r(this,xe,"f").set(s.toString("hex"),e),e.state="in-progress",e.local.transport.send(a,e.remote.address,e.remote.port)},Je=function(t,e,s){if(0===e.length)return;e[0]<=3?r(this,_e,"m",ts).call(this,t,e,s):this.emit("data",e,{transport:t,address:s.address,port:s.port})},ts=function(t,e,s){const i=function(t){if(t.length<20)return null;if(t.readUInt32BE(4)!==Nt)return null;const e=t.readUInt16BE(0),s=t.readUInt16BE(2);if(20+s>t.length)return null;const i=t.slice(8,20),r=new Map;let n=20;const a=20+s;for(;n+4<=a;){const e=t.readUInt16BE(n),s=t.readUInt16BE(n+2);if(n+=4,n+s>a)break;r.set(e,t.slice(n,n+s)),n+=Ot(s)}return{type:e,transactionId:i,attrs:r,raw:t}}(e);i&&(i.type===vt.BINDING_REQUEST?r(this,_e,"m",es).call(this,t,i,s):i.type===vt.BINDING_SUCCESS&&r(this,_e,"m",ss).call(this,t,i,s))},es=function(e,s,i){if(this.localPwd&&!function(e,s){let i=20;const r=20+e.readUInt16BE(2);let n=-1;for(;i+4<=r;){const t=e.readUInt16BE(i),s=e.readUInt16BE(i+2);if(t===Ut.MESSAGE_INTEGRITY){n=i;break}i+=4+Ot(s)}if(n<0)return!1;const a=e.slice(n+4,n+4+20),o=n+24-20,h=Buffer.from(e.slice(0,20));h.writeUInt16BE(o,2);const c=t.createHmac("sha1",Buffer.from(s,"utf8")).update(Buffer.concat([h,e.slice(20,n)])).digest();return a.length===c.length&&t.timingSafeEqual(a,c)}(s.raw,this.localPwd))return;const n=new Dt(vt.BINDING_SUCCESS,s.transactionId).addXorMappedAddress(i.address,i.port).build(this.localPwd);e.send(n,i.address,i.port);r(this,Me,"f").find(t=>t.address===i.address&&t.port===i.port)||this.addRemoteCandidate({address:i.address,port:i.port,type:"prflx",priority:0});const a=s.attrs.has(Ut.USE_CANDIDATE),o=r(this,_e,"m",is).call(this,e,i);a&&"controlled"===this.role&&r(this,_e,"m",ns).call(this,o||r(this,_e,"m",rs).call(this,e,i))},ss=function(t,e,s){const i=r(this,xe,"f").get(e.transactionId.toString("hex"));i&&(r(this,xe,"f").delete(e.transactionId.toString("hex")),i.state="succeeded","controlling"===this.role&&r(this,_e,"m",ns).call(this,i))},is=function(t,e){return r(this,be,"f").find(s=>s.remote.address===e.address&&s.remote.port===e.port&&s.local.transport===t)},rs=function(t,e){return{local:{transport:t},remote:{address:e.address,port:e.port}}},ns=function(t){!r(this,Pe,"f")&&t&&(n(this,Pe,t,"f"),n(this,Ke,!0,"f"),r(this,_e,"m",Xe).call(this),this.emit("selected",{transport:t.local.transport,candidateType:t.local.type,remoteAddress:t.remote.address,remotePort:t.remote.port}),this.emit("connected"))};const ls=65277,ds=Object.freeze({CHANGE_CIPHER_SPEC:20,ALERT:21,HANDSHAKE:22,APPLICATION_DATA:23}),us=Object.freeze({HELLO_REQUEST:0,CLIENT_HELLO:1,SERVER_HELLO:2,HELLO_VERIFY_REQUEST:3,CERTIFICATE:11,SERVER_KEY_EXCHANGE:12,CERTIFICATE_REQUEST:13,SERVER_HELLO_DONE:14,CERTIFICATE_VERIFY:15,CLIENT_KEY_EXCHANGE:16,FINISHED:20}),ps=Object.freeze({WARNING:1,FATAL:2}),Es=Object.freeze({CLOSE_NOTIFY:0,HANDSHAKE_FAILURE:40,BAD_CERTIFICATE:42,DECRYPT_ERROR:51,INTERNAL_ERROR:80}),ms=49195,Is=Object.freeze({secp256r1:23}),ws=Object.freeze({uncompressed:0}),gs=Object.freeze({sha256:4,sha384:5,sha512:6}),Ts=Object.freeze({rsa:1,ecdsa:3}),Bs=Object.freeze({ecdsa_sign:64,rsa_sign:1}),ys=Object.freeze({SUPPORTED_GROUPS:10,EC_POINT_FORMATS:11,SIGNATURE_ALGORITHMS:13,EXTENDED_MASTER_SECRET:23,RENEGOTIATION_INFO:65281}),As=Object.freeze({CLIENT:"client finished",SERVER:"server finished"});function Cs(t){return Buffer.from([t>>16&255,t>>8&255,255&t])}function Rs(t,e){return t[e]<<16|t[e+1]<<8|t[e+2]}function Ss(t){return Buffer.concat([Buffer.from([t.length]),t])}function Ns(t){const e=Buffer.alloc(2);return e.writeUInt16BE(t.length,0),Buffer.concat([e,t])}function vs(t){return Buffer.concat([Cs(t.length),t])}function Us(t,e,s){const i=Buffer.alloc(12);return i.writeUInt8(t,0),Cs(s.length).copy(i,1),i.writeUInt16BE(e,4),Cs(0).copy(i,6),Cs(s.length).copy(i,9),Buffer.concat([i,s])}function Os(e,s,i,r){return function(e,s,i,r){const n=[];let a=0,o=i;for(;a<r;){o=t.createHmac(e,s).update(o).digest();const r=t.createHmac(e,s).update(Buffer.concat([o,i])).digest();n.push(r),a+=r.length}return Buffer.concat(n).slice(0,r)}("sha256",e,Buffer.concat([Buffer.from(s,"ascii"),i]),r)}var _s,Ds;function Ls(t,e,s){return Os(t,"master secret",Buffer.concat([e,s]),48)}function ks(t,e){return Os(t,"extended master secret",e,48)}function Ms(t,e,s,i,r){const n=Buffer.alloc(13);return n.writeUInt16BE(t,0),n.writeUIntBE(e,2,6),n.writeUInt8(s,8),n.writeUInt16BE(i,9),n.writeUInt16BE(r,11),n}class bs{constructor(t,e){_s.set(this,void 0),Ds.set(this,void 0),n(this,_s,t,"f"),n(this,Ds,e,"f")}encrypt(e,s,i,n,a){const o=Buffer.alloc(8);o.writeUInt16BE(e,0),o.writeUIntBE(s,2,6);const h=Buffer.concat([r(this,Ds,"f"),o]),c=Ms(e,s,i,n,a.length),f=t.createCipheriv("aes-128-gcm",r(this,_s,"f"),h);f.setAAD(c);const l=Buffer.concat([f.update(a),f.final()]),d=f.getAuthTag();return Buffer.concat([o,l,d])}decrypt(e,s,i,n,a){const o=a.slice(0,8),h=a.slice(a.length-16),c=a.slice(8,a.length-16),f=Buffer.concat([r(this,Ds,"f"),o]),l=Ms(e,s,i,n,c.length),d=t.createDecipheriv("aes-128-gcm",r(this,_s,"f"),f);return d.setAAD(l),d.setAuthTag(h),Buffer.concat([d.update(c),d.final()])}}var Ps,Ws,Hs,Fs,Ks,xs,Gs,$s,js,Vs,zs,Ys,qs,Xs,Qs,Zs,Js,ti,ei,si,ii,ri,ni,ai,oi,hi,ci,fi,li,di,ui,pi,Ei,mi,Ii,wi,gi,Ti,Bi,yi,Ai,Ci,Ri,Si,Ni,vi,Ui,Oi,_i,Di,Li,ki,Mi,bi,Pi,Wi,Hi,Fi,Ki,xi,Gi,$i,ji,Vi,zi;_s=new WeakMap,Ds=new WeakMap;const Yi=Object.freeze({CLIENT:"client",SERVER:"server"}),qi=Object.freeze({NEW:"new",HANDSHAKING:"handshaking",CONNECTED:"connected",CLOSED:"closed",FAILED:"failed"});class Xi extends e{constructor(t){super(),Ps.add(this),Ws.set(this,void 0),Hs.set(this,void 0),Fs.set(this,void 0),Ks.set(this,void 0),xs.set(this,void 0),Gs.set(this,void 0),$s.set(this,void 0),js.set(this,void 0),Vs.set(this,void 0),zs.set(this,void 0),Ys.set(this,void 0),qs.set(this,void 0),Xs.set(this,void 0),Qs.set(this,void 0),Zs.set(this,void 0),Js.set(this,void 0),ti.set(this,void 0),ei.set(this,void 0),si.set(this,void 0),ii.set(this,void 0),ri.set(this,void 0),ni.set(this,void 0),ai.set(this,void 0),oi.set(this,void 0),hi.set(this,void 0),ci.set(this,void 0),fi.set(this,void 0),this.role=t.role,n(this,Ws,t.certDer,"f"),n(this,Hs,t.privateKey,"f"),n(this,Fs,t.verifyFingerprint||null,"f"),n(this,Ks,t.output,"f"),this.state=qi.NEW,n(this,xs,0,"f"),n(this,Gs,0,"f"),n(this,$s,0,"f"),n(this,js,null,"f"),n(this,Vs,null,"f"),n(this,zs,!1,"f"),n(this,Ys,null,"f"),n(this,qs,null,"f"),n(this,Xs,Buffer.alloc(0),"f"),n(this,Qs,null,"f"),n(this,Zs,null,"f"),n(this,Js,null,"f"),n(this,ti,null,"f"),n(this,ei,!1,"f"),n(this,si,[],"f"),n(this,ii,new Map,"f"),n(this,ri,0,"f"),n(this,ni,[],"f"),n(this,ai,null,"f"),n(this,oi,0,"f"),n(this,hi,!1,"f")}start(){this.state===qi.NEW&&(this.state=qi.HANDSHAKING,this.role===Yi.CLIENT&&(n(this,Ys,r(this,Ps,"m",li).call(this),"f"),r(this,Ps,"m",Ri).call(this)))}handlePacket(t){if(this.state===qi.CLOSED||this.state===qi.FAILED)return;let e;try{e=function(t){const e=[];let s=0;for(;s+13<=t.length;){const i=t.readUInt8(s),r=t.readUInt16BE(s+1),n=t.readUInt16BE(s+3),a=t.readUIntBE(s+5,6),o=t.readUInt16BE(s+11),h=s+13;if(h+o>t.length)break;e.push({type:i,version:r,epoch:n,seq:a,fragment:t.slice(h,h+o)}),s=h+o}return e}(t)}catch(t){return}for(const t of e)try{r(this,Ps,"m",wi).call(this,t)}catch(t){return void r(this,Ps,"m",zi).call(this,t instanceof Error?t:new Error(String(t)))}}send(t){if(this.state!==qi.CONNECTED)throw new Error("DTLS connection not established");r(this,Ps,"m",Ei).call(this,ds.APPLICATION_DATA,t)}close(){if(this.state!==qi.CLOSED&&this.state!==qi.FAILED){try{r(this,zs,"f")&&r(this,Ps,"m",Ei).call(this,ds.ALERT,Buffer.from([ps.WARNING,Es.CLOSE_NOTIFY]))}catch(t){}r(this,Ps,"m",Ii).call(this),this.state=qi.CLOSED,this.emit("close")}}getRemoteCertificate(){return r(this,Js,"f")}}Ws=new WeakMap,Hs=new WeakMap,Fs=new WeakMap,Ks=new WeakMap,xs=new WeakMap,Gs=new WeakMap,$s=new WeakMap,js=new WeakMap,Vs=new WeakMap,zs=new WeakMap,Ys=new WeakMap,qs=new WeakMap,Xs=new WeakMap,Qs=new WeakMap,Zs=new WeakMap,Js=new WeakMap,ti=new WeakMap,ei=new WeakMap,si=new WeakMap,ii=new WeakMap,ri=new WeakMap,ni=new WeakMap,ai=new WeakMap,oi=new WeakMap,hi=new WeakMap,ci=new WeakMap,fi=new WeakMap,Ps=new WeakSet,li=function(){const e=t.randomBytes(32);return e.writeUInt32BE(Math.floor(Date.now()/1e3),0),e},di=function(t){var e,s;const i=[];for(const a of t){const t=(n(this,$s,(s=r(this,$s,"f"),e=s++,s),"f"),e),o=Us(a.type,t,a.body);r(this,si,"f").push(o);const h=a.body.length;let c=0;do{const e=a.body.slice(c,c+1200),s=Buffer.alloc(12);s.writeUInt8(a.type,0),Cs(h).copy(s,1),s.writeUInt16BE(t,4),Cs(c).copy(s,6),Cs(e.length).copy(s,9);const r=Buffer.concat([s,e]);i.push({type:ds.HANDSHAKE,payload:r}),c+=e.length}while(c<h)}n(this,ni,i,"f"),n(this,oi,0,"f"),r(this,Ps,"m",ui).call(this),r(this,Ps,"m",mi).call(this)},ui=function(){for(const t of r(this,ni,"f"))r(this,Ps,"m",Ei).call(this,t.type,t.payload)},pi=function(){r(this,Ps,"m",Ei).call(this,ds.CHANGE_CIPHER_SPEC,Buffer.from([1])),n(this,xs,1,"f"),n(this,Gs,0,"f"),n(this,zs,!0,"f")},Ei=function(t,e){var s,i;let a=e;const o=(n(this,Gs,(i=r(this,Gs,"f"),s=i++,i),"f"),s);r(this,zs,"f")&&r(this,js,"f")&&(a=r(this,js,"f").encrypt(r(this,xs,"f"),o,t,ls,e));const h=function(t,e,s,i,r=65277){const n=Buffer.alloc(13);return n.writeUInt8(t,0),n.writeUInt16BE(r,1),n.writeUInt16BE(e,3),n.writeUIntBE(s,5,6),n.writeUInt16BE(i.length,11),Buffer.concat([n,i])}(t,r(this,xs,"f"),o,a,ls);r(this,Ks,"f").call(this,h)},mi=function t(){r(this,Ps,"m",Ii).call(this),n(this,ai,setTimeout(()=>{var e;r(this,hi,"f")||this.state!==qi.HANDSHAKING||(r(this,oi,"f")>=10?r(this,Ps,"m",zi).call(this,new Error("DTLS handshake timed out")):(n(this,oi,(e=r(this,oi,"f"),++e),"f"),r(this,Ps,"m",ui).call(this),r(this,Ps,"m",t).call(this)))},1e3*Math.pow(2,r(this,oi,"f"))),"f"),r(this,ai,"f").unref&&r(this,ai,"f").unref()},Ii=function(){r(this,ai,"f")&&(clearTimeout(r(this,ai,"f")),n(this,ai,null,"f"))},wi=function(t){let e=t.fragment;if(t.epoch>=1){if(!r(this,Vs,"f"))return;e=r(this,Vs,"f").decrypt(t.epoch,t.seq,t.type,t.version,t.fragment)}switch(t.type){case ds.HANDSHAKE:r(this,Ps,"m",Ti).call(this,e);break;case ds.CHANGE_CIPHER_SPEC:break;case ds.APPLICATION_DATA:this.state===qi.CONNECTED&&this.emit("data",e);break;case ds.ALERT:r(this,Ps,"m",gi).call(this,e)}},gi=function(t){if(t.length<2)return;const e=t[0],s=t[1];s===Es.CLOSE_NOTIFY?this.close():e===ps.FATAL&&r(this,Ps,"m",zi).call(this,new Error(`DTLS fatal alert: ${s}`))},Ti=function(t){var e;const s=function(t){const e=t.readUInt8(0),s=Rs(t,1),i=t.readUInt16BE(4),r=Rs(t,6),n=Rs(t,9);return{msgType:e,length:s,messageSeq:i,fragmentOffset:r,fragmentLength:n,body:t.slice(12,12+n)}}(t);let i=r(this,ii,"f").get(s.messageSeq);for(i||(i={type:s.msgType,length:s.length,data:Buffer.alloc(s.length),received:0,ranges:[]},r(this,ii,"f").set(s.messageSeq,i)),s.body.copy(i.data,s.fragmentOffset),i.received=Math.max(i.received,s.fragmentOffset+s.fragmentLength);;){const t=r(this,ii,"f").get(r(this,ri,"f"));if(!t||t.received<t.length)break;r(this,ii,"f").delete(r(this,ri,"f")),n(this,ri,(e=r(this,ri,"f"),++e),"f"),r(this,Ps,"m",yi).call(this,t.type,t.data)}},Bi=function(t,e){const s=r(this,ri,"f")-1;r(this,si,"f").push(Us(t,s,e))},yi=function(t,e){this.role===Yi.CLIENT?r(this,Ps,"m",Ui).call(this,t,e):r(this,Ps,"m",Di).call(this,t,e)},Ai=function(){const e=t.createHash("sha256");for(const t of r(this,si,"f"))e.update(t);return e.digest()},Ci=function(){return Buffer.concat(r(this,si,"f"))},Ri=function(){var t,e,s,i;const a=r(this,Ps,"m",Si).call(this);if(0===r(this,Xs,"f").length){const s=(n(this,$s,(e=r(this,$s,"f"),t=e++,e),"f"),t),i=Us(us.CLIENT_HELLO,s,a);n(this,ni,[{type:ds.HANDSHAKE,payload:i}],"f"),r(this,Ps,"m",ui).call(this),r(this,Ps,"m",mi).call(this)}else{const t=(n(this,$s,(i=r(this,$s,"f"),s=i++,i),"f"),s),e=Us(us.CLIENT_HELLO,t,a);r(this,si,"f").push(e),n(this,ni,[{type:ds.HANDSHAKE,payload:e}],"f"),n(this,oi,0,"f"),r(this,Ps,"m",ui).call(this),r(this,Ps,"m",mi).call(this)}},Si=function(){const t=[];t.push(Buffer.from([254,253])),t.push(r(this,Ys,"f")),t.push(Ss(Buffer.alloc(0))),t.push(Ss(r(this,Xs,"f")));const e=Buffer.alloc(2);return e.writeUInt16BE(ms,0),t.push(Ns(e)),t.push(Ss(Buffer.from([0]))),t.push(Ns(r(this,Ps,"m",Ni).call(this))),Buffer.concat(t)},Ni=function(){const t=[],e=Buffer.alloc(2);e.writeUInt16BE(Is.secp256r1,0),t.push(r(this,Ps,"m",vi).call(this,ys.SUPPORTED_GROUPS,Ns(e))),t.push(r(this,Ps,"m",vi).call(this,ys.EC_POINT_FORMATS,Ss(Buffer.from([ws.uncompressed]))));const s=Buffer.from([gs.sha256,Ts.ecdsa]);return t.push(r(this,Ps,"m",vi).call(this,ys.SIGNATURE_ALGORITHMS,Ns(s))),t.push(r(this,Ps,"m",vi).call(this,ys.EXTENDED_MASTER_SECRET,Buffer.alloc(0))),Buffer.concat(t)},vi=function(t,e){const s=Buffer.alloc(4);return s.writeUInt16BE(t,0),s.writeUInt16BE(e.length,2),Buffer.concat([s,e])},Ui=function(t,e){switch(t){case us.HELLO_VERIFY_REQUEST:{const t=e.readUInt8(2);n(this,Xs,e.slice(3,3+t),"f"),r(this,Ps,"m",Ii).call(this),r(this,Ps,"m",Ri).call(this);break}case us.SERVER_HELLO:r(this,Ps,"m",Bi).call(this,t,e),r(this,Ps,"m",Oi).call(this,e);break;case us.CERTIFICATE:r(this,Ps,"m",Bi).call(this,t,e),n(this,Js,r(this,Ps,"m",Fi).call(this,e),"f");break;case us.SERVER_KEY_EXCHANGE:r(this,Ps,"m",Bi).call(this,t,e),r(this,Ps,"m",Ki).call(this,e);break;case us.CERTIFICATE_REQUEST:r(this,Ps,"m",Bi).call(this,t,e);break;case us.SERVER_HELLO_DONE:r(this,Ps,"m",Bi).call(this,t,e),r(this,Ps,"m",Ii).call(this),r(this,Ps,"m",_i).call(this);break;case us.FINISHED:r(this,Ps,"m",ji).call(this,e,As.SERVER),r(this,Ps,"m",Bi).call(this,t,e),r(this,Ps,"m",Vi).call(this)}},Oi=function(t){let e=2;n(this,qs,t.slice(e,e+32),"f"),e+=32;e+=1+t.readUInt8(e);const s=t.readUInt16BE(e);if(e+=2,s!==ms)throw new Error(`Server chose unsupported cipher suite 0x${s.toString(16)}`);if(e+=1,e+2<=t.length){const s=t.readUInt16BE(e);e+=2;const i=e+s;for(;e+4<=i;){const s=t.readUInt16BE(e),i=t.readUInt16BE(e+2);e+=4,s===ys.EXTENDED_MASTER_SECRET&&n(this,ei,!0,"f"),e+=i}}},_i=function(){n(this,Qs,t.createECDH("prime256v1"),"f"),r(this,Qs,"f").generateKeys();const e=r(this,Qs,"f").getPublicKey(),s=r(this,Qs,"f").computeSecret(r(this,ti,"f")),i=r(this,Ps,"m",Hi).call(this),a=Ss(e),o=r(this,$s,"f"),h=o+1,c=Us(us.CERTIFICATE,o,i),f=Us(us.CLIENT_KEY_EXCHANGE,h,a);if(r(this,ei,"f")){const e=t.createHash("sha256");for(const t of r(this,si,"f"))e.update(t);e.update(c),e.update(f);const i=e.digest();n(this,Zs,ks(s,i),"f")}else n(this,Zs,Ls(s,r(this,Ys,"f"),r(this,qs,"f")),"f");r(this,Ps,"m",Gi).call(this);const l=Buffer.concat([...r(this,si,"f"),c,f]),d=t.sign("sha256",l,{key:r(this,Hs,"f"),dsaEncoding:"der"}),u=Buffer.concat([Buffer.from([gs.sha256,Ts.ecdsa]),Ns(d)]);r(this,Ps,"m",di).call(this,[{type:us.CERTIFICATE,body:i},{type:us.CLIENT_KEY_EXCHANGE,body:a},{type:us.CERTIFICATE_VERIFY,body:u}]),r(this,Ps,"m",pi).call(this),r(this,Ps,"m",$i).call(this,As.CLIENT)},Di=function(t,e){switch(t){case us.CLIENT_HELLO:r(this,Ps,"m",Li).call(this,e);break;case us.CERTIFICATE:r(this,Ps,"m",Bi).call(this,t,e),n(this,Js,r(this,Ps,"m",Fi).call(this,e),"f");break;case us.CLIENT_KEY_EXCHANGE:{r(this,Ps,"m",Bi).call(this,t,e);const s=e.readUInt8(0);n(this,ti,e.slice(1,1+s),"f");const i=r(this,Qs,"f").computeSecret(r(this,ti,"f"));r(this,ei,"f")?n(this,Zs,ks(i,r(this,Ps,"m",Ai).call(this)),"f"):n(this,Zs,Ls(i,r(this,Ys,"f"),r(this,qs,"f")),"f"),r(this,Ps,"m",Gi).call(this);break}case us.CERTIFICATE_VERIFY:r(this,Ps,"m",Wi).call(this,e),r(this,Ps,"m",Bi).call(this,t,e);break;case us.FINISHED:r(this,Ps,"m",ji).call(this,e,As.CLIENT),r(this,Ps,"m",Bi).call(this,t,e),r(this,Ps,"m",pi).call(this),r(this,Ps,"m",$i).call(this,As.SERVER),r(this,Ps,"m",Vi).call(this)}},Li=function(t){let e=2;const s=t.slice(e,e+32);e+=32;e+=1+t.readUInt8(e);const i=t.readUInt8(e),a=t.slice(e+1,e+1+i);e+=1+i;const o=t.readUInt16BE(e),h=t.slice(e+2,e+2+o);e+=2+o;e+=1+t.readUInt8(e);let c=!1,f=!1;for(let t=0;t+1<h.length;t+=2)255===h.readUInt16BE(t)&&(f=!0);if(e+2<=t.length){const s=t.readUInt16BE(e);e+=2;const i=e+s;for(;e+4<=i;){const s=t.readUInt16BE(e),i=t.readUInt16BE(e+2);e+=4,s===ys.EXTENDED_MASTER_SECRET&&(c=!0),s===ys.RENEGOTIATION_INFO&&(f=!0),e+=i}}if(n(this,fi,f,"f"),0===a.length)return n(this,Ys,s,"f"),r(this,Ps,"m",Mi).call(this,r(this,Ps,"m",ki).call(this,s)),r(this,ii,"f").clear(),void n(this,ri,1,"f");const l=r(this,Ps,"m",ki).call(this,s);if(!a.equals(l))return r(this,Ps,"m",Mi).call(this,l),r(this,ii,"f").clear(),void n(this,ri,1,"f");n(this,Ys,s,"f"),n(this,ei,c,"f"),r(this,Ps,"m",Bi).call(this,us.CLIENT_HELLO,t),r(this,Ps,"m",bi).call(this)},ki=function(e){return r(this,ci,"f")||n(this,ci,t.randomBytes(32),"f"),t.createHmac("sha256",r(this,ci,"f")).update(e).digest().slice(0,20)},Mi=function(t){const e=Buffer.concat([Buffer.from([254,255]),Ss(t)]),s=Us(us.HELLO_VERIFY_REQUEST,0,e);r(this,Ps,"m",Ei).call(this,ds.HANDSHAKE,s),n(this,$s,1,"f")},bi=function(){n(this,qs,r(this,Ps,"m",li).call(this),"f"),n(this,Qs,t.createECDH("prime256v1"),"f"),r(this,Qs,"f").generateKeys();const e=r(this,Qs,"f").getPublicKey(),s=r(this,Ps,"m",Pi).call(this),i=r(this,Ps,"m",Hi).call(this),a=Buffer.concat([Buffer.from([3]),(()=>{const t=Buffer.alloc(2);return t.writeUInt16BE(Is.secp256r1,0),t})(),Ss(e)]),o=Buffer.concat([r(this,Ys,"f"),r(this,qs,"f"),a]),h=t.sign("sha256",o,{key:r(this,Hs,"f"),dsaEncoding:"der"}),c=Buffer.concat([a,Buffer.from([gs.sha256,Ts.ecdsa]),Ns(h)]),f=Ss(Buffer.from([Bs.ecdsa_sign,Bs.rsa_sign])),l=Ns(Buffer.from([gs.sha256,Ts.ecdsa])),d=Ns(Buffer.alloc(0)),u=Buffer.concat([f,l,d]),p=Buffer.alloc(0);r(this,Ps,"m",di).call(this,[{type:us.SERVER_HELLO,body:s},{type:us.CERTIFICATE,body:i},{type:us.SERVER_KEY_EXCHANGE,body:c},{type:us.CERTIFICATE_REQUEST,body:u},{type:us.SERVER_HELLO_DONE,body:p}])},Pi=function(){const t=[];t.push(Buffer.from([254,253])),t.push(r(this,qs,"f")),t.push(Ss(Buffer.alloc(0)));const e=Buffer.alloc(2);e.writeUInt16BE(ms,0),t.push(e),t.push(Buffer.from([0]));const s=[];return r(this,ei,"f")&&s.push(r(this,Ps,"m",vi).call(this,ys.EXTENDED_MASTER_SECRET,Buffer.alloc(0))),s.push(r(this,Ps,"m",vi).call(this,ys.EC_POINT_FORMATS,Ss(Buffer.from([ws.uncompressed])))),r(this,fi,"f")&&s.push(r(this,Ps,"m",vi).call(this,ys.RENEGOTIATION_INFO,Ss(Buffer.alloc(0)))),t.push(Ns(Buffer.concat(s))),Buffer.concat(t)},Wi=function(e){const s=e.readUInt16BE(2),i=e.slice(4,4+s),n=r(this,Ps,"m",Ci).call(this),a=r(this,Ps,"m",xi).call(this,r(this,Js,"f"));if(!t.verify("sha256",n,{key:a,dsaEncoding:"der"},i))throw new Error("Client CertificateVerify signature invalid")},Hi=function(){const t=vs(r(this,Ws,"f"));return vs(t)},Fi=function(t){let e=3;if(e+3>3+Rs(t,0))return null;const s=Rs(t,e);e+=3;const i=t.slice(e,e+s);if(r(this,Fs,"f")){const t={algorithm:"sha-256",value:x(i,"sha-256")};if(!r(this,Fs,"f").call(this,t,i))throw new Error("Remote certificate fingerprint mismatch")}return i},Ki=function(e){let s=0;const i=e.readUInt8(s);s+=1;const a=e.readUInt16BE(s);if(s+=2,3!==i||a!==Is.secp256r1)throw new Error("Unsupported ECDHE curve from server");const o=e.readUInt8(s);s+=1;const h=e.slice(s,s+o);s+=o,n(this,ti,h,"f");const c=e.slice(0,s);s+=2;const f=e.readUInt16BE(s);s+=2;const l=e.slice(s,s+f),d=Buffer.concat([r(this,Ys,"f"),r(this,qs,"f"),c]),u=r(this,Ps,"m",xi).call(this,r(this,Js,"f"));if(!t.verify("sha256",d,{key:u,dsaEncoding:"der"},l))throw new Error("ServerKeyExchange signature invalid")},xi=function(e){return new t.X509Certificate(e).publicKey},Gi=function(){const{clientKey:t,serverKey:e,clientIV:s,serverIV:i}=function(t,e,s){const i=Os(t,"key expansion",Buffer.concat([s,e]),40);let r=0;return{clientKey:i.slice(r,r+=16),serverKey:i.slice(r,r+=16),clientIV:i.slice(r,r+=4),serverIV:i.slice(r,r+=4)}}(r(this,Zs,"f"),r(this,Ys,"f"),r(this,qs,"f"));this.role===Yi.CLIENT?(n(this,js,new bs(t,s),"f"),n(this,Vs,new bs(e,i),"f")):(n(this,js,new bs(e,i),"f"),n(this,Vs,new bs(t,s),"f"))},$i=function(t){var e,s;const i=Os(r(this,Zs,"f"),t,r(this,Ps,"m",Ai).call(this),12),a=(n(this,$s,(s=r(this,$s,"f"),e=s++,s),"f"),e),o=Us(us.FINISHED,a,i);r(this,si,"f").push(o),r(this,Ps,"m",Ei).call(this,ds.HANDSHAKE,o)},ji=function(e,s){const i=Os(r(this,Zs,"f"),s,r(this,Ps,"m",Ai).call(this),12);if(!t.timingSafeEqual(e,i))throw new Error("Peer Finished verify_data mismatch")},Vi=function(){r(this,hi,"f")||(n(this,hi,!0,"f"),r(this,Ps,"m",Ii).call(this),this.state=qi.CONNECTED,this.emit("connect"))},zi=function(t){this.state!==qi.FAILED&&this.state!==qi.CLOSED&&(r(this,Ps,"m",Ii).call(this),this.state=qi.FAILED,this.emit("error",t))};const Qi=Object.freeze({DATA:0,INIT:1,INIT_ACK:2,SACK:3,HEARTBEAT:4,HEARTBEAT_ACK:5,ABORT:6,SHUTDOWN:7,SHUTDOWN_ACK:8,ERROR:9,COOKIE_ECHO:10,COOKIE_ACK:11,SHUTDOWN_COMPLETE:14,FORWARD_TSN:192}),Zi=Object.freeze({HEARTBEAT_INFO:1,STATE_COOKIE:7,UNRECOGNIZED_PARAM:8,COOKIE_PRESERVATIVE:9,SUPPORTED_ADDR_TYPES:11,FORWARD_TSN_SUPPORTED:49152,SUPPORTED_EXTENSIONS:32776}),Ji=Object.freeze({DCEP:50,STRING:51,BINARY:53,STRING_EMPTY:56,BINARY_EMPTY:57,STRING_PARTIAL:54,BINARY_PARTIAL:52});function tr(t){return t+3&-4}function er(t,e,s){const i=4+s.length,r=Buffer.alloc(tr(i));return r.writeUInt8(t,0),r.writeUInt8(e,1),r.writeUInt16BE(i,2),s.copy(r,4),r}function sr(t,e){const s=4+e.length,i=Buffer.alloc(tr(s));return i.writeUInt16BE(t,0),i.writeUInt16BE(s,2),e.copy(i,4),i}function ir(t){const e=[];let s=0;for(;s+4<=t.length;){const i=t.readUInt16BE(s),r=t.readUInt16BE(s+2);if(r<4||s+r>t.length)break;e.push({type:i,length:r,value:t.slice(s+4,s+r)}),s+=tr(r)}return e}function rr({initiateTag:t,a_rwnd:e,outStreams:s,inStreams:i,initialTSN:r}){const n=Buffer.alloc(16);return n.writeUInt32BE(t>>>0,0),n.writeUInt32BE(e>>>0,4),n.writeUInt16BE(s,8),n.writeUInt16BE(i,10),n.writeUInt32BE(r>>>0,12),n}function nr(t){return{initiateTag:t.readUInt32BE(0),a_rwnd:t.readUInt32BE(4),outStreams:t.readUInt16BE(8),inStreams:t.readUInt16BE(10),initialTSN:t.readUInt32BE(12),params:ir(t.slice(16))}}function ar({tsn:t,streamId:e,streamSeq:s,ppid:i,userData:r,unordered:n=!1,beginning:a=!0,ending:o=!0}){const h=Buffer.alloc(12);h.writeUInt32BE(t>>>0,0),h.writeUInt16BE(e,4),h.writeUInt16BE(s,6),h.writeUInt32BE(i>>>0,8);let c=0;return o&&(c|=1),a&&(c|=2),n&&(c|=4),{flags:c,body:Buffer.concat([h,r])}}const or=(()=>{const t=new Uint32Array(256);for(let e=0;e<256;e++){let s=e;for(let t=0;t<8;t++)s=1&s?2197175160^s>>>1:s>>>1;t[e]=s>>>0}return t})();function hr(t){let e=4294967295;for(let s=0;s<t.length;s++)e=or[255&(e^t[s])]^e>>>8;return(4294967295^e)>>>0}var cr,fr,lr,dr,ur,pr,Er,mr,Ir,wr,gr,Tr,Br,yr,Ar,Cr,Rr,Sr,Nr,vr,Ur,Or,_r,Dr,Lr,kr,Mr,br,Pr,Wr,Hr,Fr,Kr;const xr=1048576,Gr=Object.freeze({CLOSED:"closed",COOKIE_WAIT:"cookie-wait",COOKIE_ECHOED:"cookie-echoed",ESTABLISHED:"established"});function $r(t,e){return(t-e&4294967295)>2147483648}function jr(t,e){return t===e||$r(t,e)}class Vr extends e{constructor(e={}){super(),cr.add(this),fr.set(this,void 0),lr.set(this,void 0),dr.set(this,void 0),ur.set(this,void 0),pr.set(this,void 0),Er.set(this,void 0),mr.set(this,void 0),Ir.set(this,void 0),wr.set(this,void 0),gr.set(this,void 0),Tr.set(this,void 0),Br.set(this,void 0),this.isClient=!!e.isClient,this.state=Gr.CLOSED,n(this,fr,5e3,"f"),n(this,lr,5e3,"f"),n(this,dr,t.randomBytes(4).readUInt32BE(0)>>>0||1,"f"),n(this,ur,0,"f"),n(this,pr,t.randomBytes(4).readUInt32BE(0)>>>0,"f"),n(this,Er,new Map,"f"),n(this,mr,new Map,"f"),n(this,Ir,null,"f"),n(this,wr,new Map,"f"),n(this,gr,new Map,"f"),n(this,Tr,null,"f")}start(){this.state===Gr.CLOSED&&this.isClient&&(r(this,cr,"m",Rr).call(this),this.state=Gr.COOKIE_WAIT)}receivePacket(t){if(t.length<12)return;if(!function(t){const e=t.readUInt32LE(8);t.writeUInt32LE(0,8);const s=hr(t);return t.writeUInt32LE(e,8),s===e}(t))return;const e=function(t){return{srcPort:t.readUInt16BE(0),dstPort:t.readUInt16BE(2),verificationTag:t.readUInt32BE(4),checksum:t.readUInt32LE(8)}}(t),s=function(t){const e=[];let s=12;for(;s+4<=t.length;){const i=t.readUInt8(s),r=t.readUInt8(s+1),n=t.readUInt16BE(s+2);if(n<4||s+n>t.length)break;const a=t.slice(s+4,s+n);e.push({type:i,flags:r,length:n,body:a}),s+=tr(n)}return e}(t);for(const t of s)r(this,cr,"m",Ar).call(this,t,e)}sendData(t,e,s,i={}){if(this.state!==Gr.ESTABLISHED)throw new Error("SCTP association not established");const a=!!i.unordered;let o=0;a||(o=r(this,Er,"f").get(t)||0,r(this,Er,"f").set(t,o+1&65535));const h=s.length;let c=0;const f=[];do{const i=s.slice(c,c+1200),l=0===c,d=c+i.length>=h,u=r(this,pr,"f");n(this,pr,r(this,pr,"f")+1>>>0,"f");const{flags:p,body:E}=ar({tsn:u,streamId:t,streamSeq:o,ppid:e,userData:i,unordered:a,beginning:l,ending:d}),m=er(Qi.DATA,p,E);r(this,mr,"f").set(u,{chunk:m}),f.push(m),c+=i.length}while(c<h);for(const t of f)r(this,cr,"m",yr).call(this,r(this,ur,"f"),[t])}shutdown(){if(this.state!==Gr.ESTABLISHED)return void r(this,cr,"m",Kr).call(this);const t=er(Qi.SHUTDOWN,0,(()=>{const t=Buffer.alloc(4);return t.writeUInt32BE(r(this,Ir,"f")>>>0,0),t})());r(this,cr,"m",yr).call(this,r(this,ur,"f"),[t]),r(this,cr,"m",Kr).call(this)}}fr=new WeakMap,lr=new WeakMap,dr=new WeakMap,ur=new WeakMap,pr=new WeakMap,Er=new WeakMap,mr=new WeakMap,Ir=new WeakMap,wr=new WeakMap,gr=new WeakMap,Tr=new WeakMap,Br=new WeakMap,cr=new WeakSet,yr=function(t,e){const s=function(t,e,s){const i=Buffer.alloc(12);return i.writeUInt16BE(t,0),i.writeUInt16BE(e,2),i.writeUInt32BE(s>>>0,4),i.writeUInt32BE(0,8),i}(r(this,fr,"f"),r(this,lr,"f"),t),i=Buffer.concat([s,...e]);!function(t){t.writeUInt32LE(0,8);const e=hr(t);t.writeUInt32LE(e,8)}(i),this.emit("output",i)},Ar=function(t,e){switch(t.type){case Qi.INIT:r(this,cr,"m",vr).call(this,t);break;case Qi.INIT_ACK:r(this,cr,"m",Ur).call(this,t);break;case Qi.COOKIE_ECHO:r(this,cr,"m",Or).call(this,t);break;case Qi.COOKIE_ACK:r(this,cr,"m",_r).call(this);break;case Qi.DATA:r(this,cr,"m",Lr).call(this,t);break;case Qi.SACK:r(this,cr,"m",Pr).call(this,t);break;case Qi.HEARTBEAT:r(this,cr,"m",Wr).call(this,t);break;case Qi.ABORT:r(this,cr,"m",Fr).call(this,"peer sent ABORT");break;case Qi.SHUTDOWN:r(this,cr,"m",Hr).call(this)}},Cr=function(){return[sr(Zi.FORWARD_TSN_SUPPORTED,Buffer.alloc(0))]},Rr=function(){const t=rr({initiateTag:r(this,dr,"f"),a_rwnd:xr,outStreams:65535,inStreams:65535,initialTSN:r(this,pr,"f")}),e=er(Qi.INIT,0,Buffer.concat([t,...r(this,cr,"m",Cr).call(this)]));r(this,cr,"m",yr).call(this,0,[e]),r(this,cr,"m",Sr).call(this,[e])},Sr=function(t){r(this,cr,"m",Nr).call(this);let e=500,s=0;const i=()=>{this.state!==Gr.ESTABLISHED&&this.state!==Gr.CLOSED&&(s>=8?r(this,cr,"m",Fr).call(this,"SCTP setup timed out"):(s++,r(this,cr,"m",yr).call(this,this.state===Gr.COOKIE_ECHOED?r(this,ur,"f"):0,t),e=Math.min(2*e,5e3),n(this,Tr,setTimeout(i,e),"f"),r(this,Tr,"f").unref&&r(this,Tr,"f").unref()))};n(this,Tr,setTimeout(i,e),"f"),r(this,Tr,"f").unref&&r(this,Tr,"f").unref()},Nr=function(){r(this,Tr,"f")&&(clearTimeout(r(this,Tr,"f")),n(this,Tr,null,"f"))},vr=function(e){const s=nr(e.body);n(this,ur,s.initiateTag,"f"),n(this,Ir,s.initialTSN-1>>>0,"f"),r(this,Br,"f")||n(this,Br,t.randomBytes(32),"f");const i=Buffer.alloc(16);i.writeUInt32BE(r(this,dr,"f"),0),i.writeUInt32BE(r(this,ur,"f"),4),i.writeUInt32BE(r(this,pr,"f"),8),i.writeUInt32BE(s.initialTSN,12);const a=t.createHmac("sha256",r(this,Br,"f")).update(i).digest(),o=Buffer.concat([i,a]),h=rr({initiateTag:r(this,dr,"f"),a_rwnd:xr,outStreams:65535,inStreams:65535,initialTSN:r(this,pr,"f")}),c=Buffer.concat([sr(Zi.STATE_COOKIE,o),...r(this,cr,"m",Cr).call(this)]),f=er(Qi.INIT_ACK,0,Buffer.concat([h,c]));r(this,cr,"m",yr).call(this,r(this,ur,"f"),[f])},Ur=function(t){if(this.state!==Gr.COOKIE_WAIT)return;r(this,cr,"m",Nr).call(this);const e=nr(t.body);n(this,ur,e.initiateTag,"f"),n(this,Ir,e.initialTSN-1>>>0,"f");const s=e.params.find(t=>t.type===Zi.STATE_COOKIE);if(!s)return void r(this,cr,"m",Fr).call(this,"INIT_ACK missing state cookie");const i=er(Qi.COOKIE_ECHO,0,s.value);this.state=Gr.COOKIE_ECHOED,r(this,cr,"m",yr).call(this,r(this,ur,"f"),[i]),r(this,cr,"m",Sr).call(this,[i])},Or=function(e){const s=e.body;if(s.length>=48&&r(this,Br,"f")){const e=s.slice(0,16),i=s.slice(16,48),n=t.createHmac("sha256",r(this,Br,"f")).update(e).digest();if(!t.timingSafeEqual(i,n))return}const i=er(Qi.COOKIE_ACK,0,Buffer.alloc(0));r(this,cr,"m",yr).call(this,r(this,ur,"f"),[i]),r(this,cr,"m",Dr).call(this)},_r=function(){this.state===Gr.COOKIE_ECHOED&&(r(this,cr,"m",Nr).call(this),r(this,cr,"m",Dr).call(this))},Dr=function(){this.state!==Gr.ESTABLISHED&&(this.state=Gr.ESTABLISHED,this.emit("established"))},Lr=function(t){const e=(s=t.flags,i=t.body,{unordered:!!(4&s),beginning:!!(2&s),ending:!!(1&s),tsn:i.readUInt32BE(0),streamId:i.readUInt16BE(4),streamSeq:i.readUInt16BE(6),ppid:i.readUInt32BE(8),userData:i.slice(12)});var s,i;r(this,cr,"m",kr).call(this,e),r(this,cr,"m",br).call(this)},kr=function(t){const e=r(this,Ir,"f")+1>>>0;if(!$r(t.tsn,e))if(t.tsn===e){n(this,Ir,t.tsn,"f"),r(this,cr,"m",Mr).call(this,t);let e=r(this,Ir,"f")+1>>>0;for(;r(this,wr,"f").has(e);){const t=r(this,wr,"f").get(e);r(this,wr,"f").delete(e),n(this,Ir,e,"f"),r(this,cr,"m",Mr).call(this,t),e=r(this,Ir,"f")+1>>>0}}else r(this,wr,"f").has(t.tsn)||r(this,wr,"f").set(t.tsn,t)},Mr=function(t){const e=`${t.streamId}:${t.unordered?"u":"o"}`;if(t.beginning&&t.ending)return void this.emit("message",{streamId:t.streamId,ppid:t.ppid,data:t.userData});let s=r(this,gr,"f").get(e);if(t.beginning)s={ppid:t.ppid,parts:[t.userData]},r(this,gr,"f").set(e,s);else{if(!s)return;s.parts.push(t.userData)}t.ending&&s&&(r(this,gr,"f").delete(e),this.emit("message",{streamId:t.streamId,ppid:s.ppid,data:Buffer.concat(s.parts)}))},br=function(){const t=[];if(r(this,wr,"f").size>0){const e=[...r(this,wr,"f").keys()].sort((t,e)=>$r(t,e)?-1:1),s=r(this,Ir,"f")+1>>>0;let i=null,n=null;for(const r of e)null!==i?r!==n+1>>>0?(t.push([1+(i-s&65535),1+(n-s&65535)]),i=r,n=r):n=r:(i=r,n=r);null!==i&&t.push([1+(i-s&65535),1+(n-s&65535)])}const e=function({cumulativeTSNAck:t,a_rwnd:e,gapBlocks:s=[],dupTSNs:i=[]}){const r=Buffer.alloc(12+4*s.length+4*i.length);r.writeUInt32BE(t>>>0,0),r.writeUInt32BE(e>>>0,4),r.writeUInt16BE(s.length,8),r.writeUInt16BE(i.length,10);let n=12;for(const[t,e]of s)r.writeUInt16BE(t,n),r.writeUInt16BE(e,n+2),n+=4;for(const t of i)r.writeUInt32BE(t>>>0,n),n+=4;return r}({cumulativeTSNAck:r(this,Ir,"f")>>>0,a_rwnd:xr,gapBlocks:t}),s=er(Qi.SACK,0,e);r(this,cr,"m",yr).call(this,r(this,ur,"f"),[s])},Pr=function(t){const e=function(t){const e=t.readUInt32BE(0),s=t.readUInt32BE(4),i=t.readUInt16BE(8),r=t.readUInt16BE(10),n=[];let a=12;for(let e=0;e<i;e++)n.push([t.readUInt16BE(a),t.readUInt16BE(a+2)]),a+=4;const o=[];for(let e=0;e<r;e++)o.push(t.readUInt32BE(a)),a+=4;return{cumulativeTSNAck:e,a_rwnd:s,gapBlocks:n,dupTSNs:o}}(t.body);for(const t of[...r(this,mr,"f").keys()])jr(t,e.cumulativeTSNAck)&&r(this,mr,"f").delete(t);const s=e.cumulativeTSNAck+1>>>0;for(const[t,i]of e.gapBlocks)for(let e=t;e<=i;e++)r(this,mr,"f").delete(s+e-1>>>0)},Wr=function(t){const e=er(Qi.HEARTBEAT_ACK,0,t.body);r(this,cr,"m",yr).call(this,r(this,ur,"f"),[e])},Hr=function(){const t=er(Qi.SHUTDOWN_ACK,0,Buffer.alloc(0));r(this,cr,"m",yr).call(this,r(this,ur,"f"),[t]),r(this,cr,"m",Kr).call(this)},Fr=function(t){r(this,cr,"m",Nr).call(this),this.state!==Gr.CLOSED&&(this.state=Gr.CLOSED,this.emit("error",new Error(t||"SCTP abort")),this.emit("close"))},Kr=function(){r(this,cr,"m",Nr).call(this),this.state!==Gr.CLOSED&&(this.state=Gr.CLOSED,this.emit("close"))};const zr=Object.freeze({DATA_CHANNEL_ACK:2,DATA_CHANNEL_OPEN:3}),Yr=Object.freeze({RELIABLE:0,RELIABLE_UNORDERED:128,PARTIAL_RELIABLE_REXMIT:1,PARTIAL_RELIABLE_REXMIT_UNORDERED:129,PARTIAL_RELIABLE_TIMED:2,PARTIAL_RELIABLE_TIMED_UNORDERED:130});var qr,Xr,Qr,Zr,Jr,tn,en,sn,rn,nn,an,on,hn,cn,fn,ln,dn,un,pn,En,mn,In,wn,gn,Tn,Bn,yn,An,Cn,Rn,Sn,Nn,vn,Un,On,_n,Dn,Ln,kn,Mn;class bn extends e{constructor(t,e){super(),qr.add(this),Xr.set(this,void 0),Qr.set(this,void 0),Zr.set(this,void 0),n(this,Xr,t,"f"),n(this,Qr,new Map,"f"),n(this,Zr,e?0:1,"f"),r(this,Xr,"f").on("message",t=>r(this,qr,"m",rn).call(this,t))}openChannel(t,e={}){let s=t.id;if(null==s&&(s=r(this,qr,"m",Jr).call(this),t.emit(mt.SET_ID,s)),r(this,Qr,"f").set(s,{channel:t,acked:!1}),r(this,qr,"m",sn).call(this,t,s,e),t.negotiated)r(this,Qr,"f").get(s).acked=!0,t.emit(mt.OPEN);else{const i=function({channelType:t,priority:e=0,reliabilityParameter:s=0,label:i="",protocol:r=""}){const n=Buffer.from(i,"utf8"),a=Buffer.from(r,"utf8"),o=Buffer.alloc(12+n.length+a.length);return o.writeUInt8(zr.DATA_CHANNEL_OPEN,0),o.writeUInt8(t,1),o.writeUInt16BE(e,2),o.writeUInt32BE(s>>>0,4),o.writeUInt16BE(n.length,8),o.writeUInt16BE(a.length,10),n.copy(o,12),a.copy(o,12+n.length),o}({channelType:r(this,qr,"m",tn).call(this,e),priority:0,reliabilityParameter:r(this,qr,"m",en).call(this,e),label:t.label,protocol:e.protocol||t.protocol||""});r(this,Xr,"f").sendData(s,Ji.DCEP,i)}}acceptChannel(t,e){t.emit(mt.SET_ID,e.streamId),r(this,Qr,"f").set(e.streamId,{channel:t,acked:!0}),r(this,qr,"m",sn).call(this,t,e.streamId,{ordered:e.ordered}),t.emit(mt.OPEN)}}Xr=new WeakMap,Qr=new WeakMap,Zr=new WeakMap,qr=new WeakSet,Jr=function(){let t=r(this,Zr,"f");for(;r(this,Qr,"f").has(t);)t+=2;return n(this,Zr,t+2,"f"),t},tn=function(t){const e=!1===t.ordered;return null!=t.maxRetransmits?e?Yr.PARTIAL_RELIABLE_REXMIT_UNORDERED:Yr.PARTIAL_RELIABLE_REXMIT:null!=t.maxPacketLifeTime?e?Yr.PARTIAL_RELIABLE_TIMED_UNORDERED:Yr.PARTIAL_RELIABLE_TIMED:e?Yr.RELIABLE_UNORDERED:Yr.RELIABLE},en=function(t){return null!=t.maxRetransmits?t.maxRetransmits>>>0:null!=t.maxPacketLifeTime?t.maxPacketLifeTime>>>0:0},sn=function(t,e,s){const i=!1===s.ordered;t.on(mt.SEND,(t,s)=>{let n;n=s?0===t.length?Ji.BINARY_EMPTY:Ji.BINARY:0===t.length?Ji.STRING_EMPTY:Ji.STRING;const a=0===t.length?Buffer.from([0]):t;r(this,Xr,"f").sendData(e,n,a,{unordered:i})})},rn=function(t){if(t.ppid===Ji.DCEP)return void r(this,qr,"m",nn).call(this,t);const e=r(this,Qr,"f").get(t.streamId);if(!e)return;const s=t.ppid===Ji.BINARY||t.ppid===Ji.BINARY_EMPTY||t.ppid===Ji.BINARY_PARTIAL,i=t.ppid===Ji.STRING_EMPTY||t.ppid===Ji.BINARY_EMPTY?Buffer.alloc(0):t.data;e.channel.emit(mt.RECEIVE,i,s)},nn=function(t){const e=(s=t.data).length>0?s.readUInt8(0):-1;var s;if(e===zr.DATA_CHANNEL_OPEN){const e=function(t){const e=t.readUInt8(1),s=t.readUInt16BE(2),i=t.readUInt32BE(4),r=t.readUInt16BE(8),n=t.readUInt16BE(10);return{channelType:e,priority:s,reliabilityParameter:i,label:t.slice(12,12+r).toString("utf8"),protocol:t.slice(12+r,12+r+n).toString("utf8"),unordered:!!(128&e)}}(t.data);r(this,Xr,"f").sendData(t.streamId,Ji.DCEP,Buffer.from([zr.DATA_CHANNEL_ACK])),this.emit("open-request",{streamId:t.streamId,label:e.label,protocol:e.protocol,ordered:!e.unordered,channelType:e.channelType,reliabilityParameter:e.reliabilityParameter})}else if(e===zr.DATA_CHANNEL_ACK){const e=r(this,Qr,"f").get(t.streamId);e&&!e.acked&&(e.acked=!0,e.channel.emit(mt.OPEN))}};class Pn extends e{constructor(t){super(),an.add(this),on.set(this,void 0),hn.set(this,void 0),n(this,on,t,"f"),this.ice=new fs({role:t.iceRole,localUfrag:t.localUfrag,localPwd:t.localPwd}),this.dtls=null,this.sctp=null,this.dcm=null,n(this,hn,!1,"f"),this.ice.on("candidate",t=>this.emit("candidate",t)),this.ice.on("error",t=>this.emit("error",t)),this.ice.on("failed",()=>this.emit("error",new Error("ICE failed"))),this.ice.on("data",t=>{this.dtls&&this.dtls.handlePacket(t)}),this.ice.on("connected",()=>{this.emit("iceconnected"),r(this,an,"m",cn).call(this)})}async gather(t={}){await this.ice.gather(t)}getLocalCandidates(){return this.ice.getLocalCandidates()}setRemote(t,e){this.ice.setRemoteCredentials(t,e),this.ice.start()}addRemoteCandidate(t){this.ice.addRemoteCandidate(t)}openChannel(t,e){if(!this.dcm)throw new Error("SCTP not ready");this.dcm.openChannel(t,e)}acceptChannel(t,e){if(!this.dcm)throw new Error("SCTP not ready");this.dcm.acceptChannel(t,e)}isReady(){return!!this.sctp&&"established"===this.sctp.state}close(){if(this.sctp)try{this.sctp.shutdown()}catch{}if(this.dtls)try{this.dtls.close()}catch{}if(this.ice)try{this.ice.close()}catch{}}}on=new WeakMap,hn=new WeakMap,an=new WeakSet,cn=function(){r(this,hn,"f")||(n(this,hn,!0,"f"),this.dtls=new Xi({role:"client"===r(this,on,"f").dtlsRole?Yi.CLIENT:Yi.SERVER,certDer:r(this,on,"f").certDer,privateKey:r(this,on,"f").privateKey,verifyFingerprint:r(this,on,"f").verifyFingerprint,output:t=>{try{this.ice.send(t)}catch(t){this.emit("error",t)}}}),this.dtls.on("connect",()=>{this.emit("dtlsconnected"),r(this,an,"m",fn).call(this)}),this.dtls.on("data",t=>{this.sctp&&this.sctp.receivePacket(t)}),this.dtls.on("error",t=>this.emit("error",t)),this.dtls.on("close",()=>this.emit("close")),this.dtls.start())},fn=function(){const t="client"===r(this,on,"f").dtlsRole,e=new Vr({isClient:t});this.sctp=e,e.on("output",t=>{try{this.dtls&&this.dtls.send(t)}catch(t){this.emit("error",t)}}),e.on("error",t=>this.emit("error",t)),e.on("close",()=>this.emit("close")),this.dcm=new bn(e,t),this.dcm.on("open-request",t=>this.emit("datachannel-request",t)),e.on("established",()=>{this.emit("sctpconnected"),this.emit("ready")}),e.start()};const Wn=Object.freeze({STABLE:"stable",HAVE_LOCAL_OFFER:"have-local-offer",HAVE_REMOTE_OFFER:"have-remote-offer",HAVE_LOCAL_PRANSWER:"have-local-pranswer",HAVE_REMOTE_PRANSWER:"have-remote-pranswer",CLOSED:"closed"}),Hn=Object.freeze({NEW:"new",GATHERING:"gathering",COMPLETE:"complete"}),Fn=Object.freeze({NEW:"new",CONNECTING:"connecting",CONNECTED:"connected",DISCONNECTED:"disconnected",FAILED:"failed",CLOSED:"closed"});class Kn extends e{constructor(e={}){super(),ln.add(this),dn.set(this,void 0),un.set(this,void 0),pn.set(this,void 0),En.set(this,void 0),mn.set(this,void 0),In.set(this,void 0),wn.set(this,void 0),gn.set(this,void 0),Tn.set(this,void 0),Bn.set(this,void 0),yn.set(this,void 0),An.set(this,void 0),Cn.set(this,void 0),Rn.set(this,void 0),Sn.set(this,void 0),Nn.set(this,void 0),vn.set(this,void 0),n(this,dn,e,"f"),n(this,un,Wn.STABLE,"f"),n(this,pn,Hn.NEW,"f"),n(this,En,Fn.NEW,"f"),n(this,mn,null,"f"),n(this,In,null,"f"),n(this,wn,null,"f"),n(this,gn,{usernameFragment:t.randomBytes(3).toString("base64").replace(/[^a-zA-Z0-9]/g,"").slice(0,4).padEnd(4,"x"),password:t.randomBytes(18).toString("base64").replace(/[^a-zA-Z0-9]/g,"").slice(0,24).padEnd(24,"x")},"f"),n(this,Tn,null,"f"),n(this,Bn,[],"f"),n(this,yn,null,"f"),n(this,An,null,"f"),n(this,Cn,!1,"f"),n(this,Rn,!1,"f"),n(this,Sn,[],"f"),n(this,Nn,new Set,"f"),n(this,vn,[],"f")}createDataChannel(t,e={}){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");const s=new It(t,e);r(this,Nn,"f").add(s);const i={ordered:!1!==e.ordered,maxRetransmits:e.maxRetransmits,maxPacketLifeTime:e.maxPacketLifeTime,protocol:e.protocol||"",negotiated:e.negotiated||!1};return r(this,An,"f")&&r(this,An,"f").isReady()?r(this,An,"f").openChannel(s,i):r(this,Sn,"f").push({channel:s,init:i}),setImmediate(()=>{r(this,Rn,"f")||this.emit("negotiationneeded")}),s}async createOffer(){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");await r(this,ln,"m",Un).call(this),n(this,Cn,!0,"f");const t=r(this,ln,"m",Dn).call(this),e=(s={iceUfrag:r(this,gn,"f").usernameFragment,icePwd:r(this,gn,"f").password,fingerprint:t,setup:"actpass",candidates:[]},yt({...s,setup:s.setup||"actpass"}));var s;return new Bt({type:Tt.OFFER,sdp:e})}async createAnswer(){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");if(!r(this,In,"f")||"offer"!==r(this,In,"f").type)throw new Error("Cannot create answer without remote offer");await r(this,ln,"m",Un).call(this);const t=r(this,ln,"m",Dn).call(this),e=(s={iceUfrag:r(this,gn,"f").usernameFragment,icePwd:r(this,gn,"f").password,fingerprint:t,setup:"active",candidates:[]},yt({...s,setup:s.setup||"active"}));var s;return new Bt({type:Tt.ANSWER,sdp:e})}async setLocalDescription(t){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");const e=t||(r(this,un,"f")===Wn.HAVE_REMOTE_OFFER?await this.createAnswer():await this.createOffer());await r(this,ln,"m",Un).call(this),n(this,mn,new Bt({type:e.type??void 0,sdp:e.sdp??void 0}),"f"),"offer"===e.type?n(this,un,Wn.HAVE_LOCAL_OFFER,"f"):"answer"===e.type&&n(this,un,Wn.STABLE,"f"),r(this,ln,"m",Ln).call(this,e,!0),n(this,pn,Hn.GATHERING,"f"),this.emit("icegatheringstatechange"),r(this,An,"f")&&(await r(this,An,"f").gather({iceServers:r(this,dn,"f").iceServers||[],iceTransportPolicy:r(this,dn,"f").iceTransportPolicy||"all"}),n(this,pn,Hn.COMPLETE,"f"),this.emit("icegatheringstatechange"),this.emit("icecandidate",{candidate:null}),r(this,ln,"m",kn).call(this)),this.emit("signalingstatechange")}async setRemoteDescription(t){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");if(!t||!t.sdp)throw new Error("Invalid session description");await r(this,ln,"m",Un).call(this),n(this,In,new Bt(t),"f"),"offer"===t.type?n(this,un,Wn.HAVE_REMOTE_OFFER,"f"):"answer"===t.type&&n(this,un,Wn.STABLE,"f"),n(this,Tn,function(t){const e={usernameFragment:null,password:null};for(const s of t.split(/\r?\n/))s.startsWith("a=ice-ufrag:")?e.usernameFragment=s.slice(12).trim():s.startsWith("a=ice-pwd:")&&(e.password=s.slice(10).trim());return e}(t.sdp),"f");const e=function(t){const e={role:"auto",fingerprints:[]};for(const s of t.split(/\r?\n/))if(s.startsWith("a=setup:")){const t=s.slice(8).trim();e.role="active"===t?"client":"passive"===t?"server":"actpass",e.setup=t}else if(s.startsWith("a=fingerprint:")){const t=s.slice(14).trim().split(/\s+/);2===t.length&&e.fingerprints.push({algorithm:t[0].toLowerCase(),value:t[1].toUpperCase()})}return e}(t.sdp);if(n(this,Bn,e.fingerprints,"f"),n(this,yn,e.setup??null,"f"),r(this,ln,"m",Ln).call(this,t,!1),r(this,An,"f")&&r(this,Tn,"f").usernameFragment){r(this,An,"f").setRemote(r(this,Tn,"f").usernameFragment,r(this,Tn,"f").password??"");for(const e of function(t){const e=[];for(const s of t.split(/\r?\n/)){if(!s.startsWith("a=candidate:"))continue;const t=At(s.slice(2));t&&e.push(t)}return e}(t.sdp))r(this,An,"f").addRemoteCandidate(e)}r(this,ln,"m",kn).call(this),this.emit("signalingstatechange")}async addIceCandidate(t){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");if(!t||"string"!=typeof t&&""===t.candidate)return;const e=At("string"==typeof t?t:t.candidate||"");e&&r(this,An,"f")&&r(this,An,"f").addRemoteCandidate(e)}getConfiguration(){return{...r(this,dn,"f")}}setConfiguration(t){if(r(this,Rn,"f"))throw new Error("RTCPeerConnection is closed");n(this,dn,{...t},"f")}close(){if(!r(this,Rn,"f")){n(this,Rn,!0,"f"),n(this,un,Wn.CLOSED,"f");for(const t of r(this,Nn,"f"))try{t.close()}catch(t){}if(r(this,An,"f"))try{r(this,An,"f").close()}catch(t){}r(this,ln,"m",Mn).call(this,Fn.CLOSED),this.emit("signalingstatechange")}}get signalingState(){return r(this,un,"f")}get iceGatheringState(){return r(this,pn,"f")}get iceConnectionState(){return r(this,En,"f")===Fn.CONNECTED?"connected":r(this,En,"f")===Fn.CONNECTING?"checking":r(this,En,"f")===Fn.FAILED?"failed":"new"}get connectionState(){return r(this,En,"f")}get localDescription(){return r(this,mn,"f")}get remoteDescription(){return r(this,In,"f")}get currentLocalDescription(){return r(this,mn,"f")}get currentRemoteDescription(){return r(this,In,"f")}get pendingLocalDescription(){return r(this,un,"f")===Wn.STABLE?null:r(this,mn,"f")}get pendingRemoteDescription(){return r(this,un,"f")===Wn.STABLE?null:r(this,In,"f")}get canTrickleIceCandidates(){return!0}get sctp(){return r(this,An,"f")?r(this,An,"f").sctp:null}}dn=new WeakMap,un=new WeakMap,pn=new WeakMap,En=new WeakMap,mn=new WeakMap,In=new WeakMap,wn=new WeakMap,gn=new WeakMap,Tn=new WeakMap,Bn=new WeakMap,yn=new WeakMap,An=new WeakMap,Cn=new WeakMap,Rn=new WeakMap,Sn=new WeakMap,Nn=new WeakMap,vn=new WeakMap,ln=new WeakSet,Un=async function(){return r(this,wn,"f")||(r(this,dn,"f").certificates&&r(this,dn,"f").certificates[0]?n(this,wn,r(this,dn,"f").certificates[0],"f"):n(this,wn,await pt.generateCertificate(),"f")),r(this,wn,"f")},On=function(t,e){if(r(this,An,"f"))return r(this,An,"f");const s=r(this,wn,"f");if(!s)throw new Error("Certificate not initialized");const i=s.getCertificateDer();if(!i)throw new Error("Certificate has no DER encoding");const a=new Pn({iceRole:t,dtlsRole:e,localUfrag:r(this,gn,"f").usernameFragment,localPwd:r(this,gn,"f").password,certDer:i,privateKey:s.getPrivateKeyObject(),verifyFingerprint:t=>r(this,ln,"m",_n).call(this,t)});return a.on("candidate",t=>{const e={candidate:t.sdp,sdpMid:"0",sdpMLineIndex:0,usernameFragment:r(this,gn,"f").usernameFragment};r(this,vn,"f").push(e),this.emit("icecandidate",{candidate:e})}),a.on("iceconnected",()=>r(this,ln,"m",Mn).call(this,Fn.CONNECTING)),a.on("sctpconnected",()=>r(this,ln,"m",Mn).call(this,Fn.CONNECTED)),a.on("error",t=>{this.emit("error",t),r(this,ln,"m",Mn).call(this,Fn.FAILED)}),a.on("close",()=>r(this,ln,"m",Mn).call(this,Fn.DISCONNECTED)),a.on("datachannel-request",t=>{const e=new It(t.label,{ordered:t.ordered,protocol:t.protocol,id:t.streamId});a.acceptChannel(e,t),r(this,Nn,"f").add(e),this.emit("datachannel",{channel:e})}),a.on("ready",()=>{for(const{channel:t,init:e}of r(this,Sn,"f"))a.openChannel(t,e);n(this,Sn,[],"f")}),n(this,An,a,"f"),a},_n=function(t){return 0===r(this,Bn,"f").length||r(this,Bn,"f").some(e=>e.algorithm===t.algorithm&&e.value.toUpperCase()===t.value.toUpperCase())},Dn=function(){const t=r(this,wn,"f");if(!t)throw new Error("Certificate not initialized");const e=t.getFingerprints();return e.find(t=>"sha-256"===t.algorithm)||e[0]},Ln=function(t,e){if(r(this,An,"f"))return;const s=r(this,Cn,"f")?"controlling":"controlled";let i;i=r(this,Cn,"f")?"active"===r(this,yn,"f")?"server":"passive"===r(this,yn,"f")?"client":"server":"client",r(this,ln,"m",On).call(this,s,i)},kn=function(){r(this,An,"f")&&r(this,Tn,"f")&&r(this,Tn,"f").usernameFragment&&r(this,An,"f").setRemote(r(this,Tn,"f").usernameFragment,r(this,Tn,"f").password??"")},Mn=function(t){r(this,En,"f")!==t&&(n(this,En,t,"f"),this.emit("connectionstatechange"),this.emit("iceconnectionstatechange"))};
|
|
26
|
-
/**
|
|
27
|
-
* @fileoverview node-rtc-connection - WebRTC DataChannel implementation for Node.js
|
|
28
|
-
*
|
|
29
|
-
* A from-scratch, pure-Node.js implementation of WebRTC peer connections and
|
|
30
|
-
* data channels (no native dependencies). Interoperates with browsers.
|
|
31
|
-
*
|
|
32
|
-
* This implementation focuses on DataChannel functionality without media streams.
|
|
33
|
-
* Features:
|
|
34
|
-
* - ICE (Interactive Connectivity Establishment), RFC 8445
|
|
35
|
-
* - STUN/TURN support for NAT traversal
|
|
36
|
-
* - DTLS 1.2 encryption (RFC 6347)
|
|
37
|
-
* - SCTP over DTLS + DCEP for data channels (RFC 8831 / 8832)
|
|
38
|
-
* - W3C-compatible RTCPeerConnection / RTCDataChannel API
|
|
39
|
-
*
|
|
40
|
-
* @license MIT
|
|
41
|
-
* @author nmhung1210
|
|
42
|
-
*/
|
|
43
|
-
const xn="2.0.4";export{T as ByteBufferQueue,pt as RTCCertificate,It as RTCDataChannel,Et as RTCDataChannelState,y as RTCError,O as RTCIceCandidate,Hn as RTCIceGatheringState,Kn as RTCPeerConnection,Fn as RTCPeerConnectionState,Tt as RTCSdpType,Bt as RTCSessionDescription,Wn as RTCSignalingState,xn as version};
|