Build 576754stable
Module 33006
4,725 characters · in no known chunkDependencies
Source Raw
function(e,t,n){"use strict";let i,r;n.d(t,{A:()=>b}),n(323874),n(14289),n(35956),n(321073);var a=n(143236),s=n(735438),l=n.n(s),o=n(228366),d=n(626584),c=n(111162),u=n(998218),_=n(19575),E=n(636401),A=n(861621),h=n(313731),I=n(546983),f=n(613057),p=n(652215),T=n(264572).Buffer;try{i=_.Ay.requireModule("discord_erlpack")}catch(e){try{i=_.Ay.requireModule("erlpack")}catch(e){}}let m=_.Ay.requireModule("discord_rpc").RPCWebSocket,g=window.GLOBAL_ENV.MARKETING_ENDPOINT,S=new d.A("RPCServer:WSS"),N=[];function C(e){return"function"==typeof e?e():e}function R(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=e>0?void 0:()=>{if(!C(r.listening))return;let e=r.address().port;S.info(`Starting on ${e}`),o.h.dispatch({type:"RPC_SERVER_READY",port:e})};r.listen(p.xEi+e%p.sJq,"127.0.0.1",t)}function O(e,t,n){let i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:200,r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{},a=null!=C(e.headers).origin?{"Access-Control-Allow-Origin":C(e.headers).origin,"Access-Control-Allow-Credentials":"true","Access-Control-Allow-Methods":"POST, GET, PUT, PATCH, DELETE","Access-Control-Allow-Headers":"Content-Type, Authorization"}:{};n=n?JSON.stringify(n):"",i=200===i&&0===n.length?204:i,t.setHeader("Content-Length",T.byteLength(n).toString()),t.setHeader("Content-Type","application/json"),t.writeHead(i,{...r,...a}),t.end(n)}function L(e,t,n,i){let r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0;O(e,t,{code:r,message:i},n)}class D extends h.A{_socket;constructor(e,t,n){if(super("ws",t,n),-1===["etf","json"].indexOf(n))throw new E.A({closeCode:p.YI$.INVALID_ENCODING},`Invalid Encoding: ${n}`);if("etf"===n&&null==i)throw new E.A({closeCode:p.YI$.INVALID_ENCODING},"Erlpack cannot be used on this client");this._socket=e}send(e){(c.default.isLoggingOverlayEvents||e.cmd!==p.e$_.OVERLAY&&e.evt!==p.ZE4.OVERLAY)&&S.info(`Socket Emit: ${this.id}`,(0,A.A)(e)),null!=i&&"etf"===this.encoding?this._socket.send(i.pack(e),{binary:!0}):this._socket.send(JSON.stringify(e))}close(e,t){this._socket.close(e,t)}}class y extends h.A{_sendCallback;_closeCallback;constructor(e,t,n,i){if(super("http",n,i),"json"!==i)throw new E.A({closeCode:p.YI$.INVALID_ENCODING},`Invalid Encoding: ${i}`);this._sendCallback=e,this._closeCallback=t}send(e){(c.default.isLoggingOverlayEvents||e.cmd!==p.e$_.OVERLAY)&&S.info(`Socket Emit: ${this.id}`,e),this._sendCallback(e)}close(e,t){this._closeCallback(t,e)}}class v extends a.EventEmitter{constructor(){super();let e=0;(r=m.http.createServer()).on("error",t=>{S.error(`Error: ${t.message}`),("EADDRINUSE"===t.code||t.message.includes("EADDRINUSE"))&&setTimeout(()=>R(++e),1e3)}),r.on("request",this.handleRequest.bind(this)),R(e);const t={instanceId:r.instanceId??0,server:r};new m.ws.Server(t).on("connection",e=>this.handleConnection(e))}handleRequest(e,t){let[n,i]=C(e.url).split("?"),r=C(e.method);if("/rpc"===n&&"OPTIONS"===r)return void O(e,t,{body:""});let a="POST"===r;if("/rpc"===n&&("GET"===r||a)){let n=new URLSearchParams(i),r=a?C(e.headers)["content-type"].split("/")[1]:"json",s=function(){let{protocol:e,host:i}=u.A.toURLSafe(n.get("callback")??"")??{};e===location.protocol&&i===location.host?t.setHeader("Location",n.get("callback")):t.setHeader("Location",g),t.writeHead(301),t.end()},l=new y(!a?s:O.bind(null,e,t),!a?s:L.bind(null,e,t,400),Number(n.get("v")),r);a?(0,I.j7)(l,C(e.headers).origin,n.get("client_id")).then(()=>{let n="";e.on("data",e=>n+=e),e.on("error",()=>L(e,t,500,"Internal Server Error")),e.on("end",()=>this.handleMessage(l,n))}).catch(e=>{let{code:t,message:n}=e;return l.close(t,n)}):(l.authorization.scopes=[f.kw],this.handleMessage(l,decodeURIComponent(n.get("payload")??"")));return}L(e,t,404,"Not Found")}handleConnection(e){let t,n=new URLSearchParams(C(e.upgradeReq).url.split("?")[1]),i=C(e.upgradeReq).headers.origin??"";try{t=new D(e,Number(n.get("v")),n.get("encoding")??"json")}catch(t){e.close(t.code,t.message);return}S.info(`Socket Opened: ${t.id}`),e.on("error",e=>S.error(`WS Error: ${e.message}`)),e.on("close",(e,n)=>{S.info(`Socket Closed: ${t.id}, code ${e}, message ${n}`),l().remove(N,e=>e===t),this.emit("disconnect",t)}),(0,I.j7)(t,i,n.get("client_id")).then(()=>{N.push(t),e.on("message",e=>this.handleMessage(t,e)),this.emit("connect",t)}).catch(e=>{let{code:n,message:i}=e;return t.close(n,i)})}handleMessage(e,t){let n;try{if(null!=i&&"etf"===e.encoding)n=i.unpack(t);else if("string"==typeof t)n=JSON.parse(t);else throw Error()}catch(t){e.close(p.YI$.CLOSE_UNSUPPORTED,`Payload not ${e.encoding}`);return}(c.default.isLoggingOverlayEvents||n.cmd!==p.e$_.OVERLAY)&&S.info(`Socket Message: ${e.id}`,(0,A.A)(n)),this.emit("request",e,n)}}let b=new v}