Files
discord-decomp/modules/rpc/RPCServer.tsx
T
2026-11-01 12:12:55 +02:00

328 lines
10 KiB
TypeScript

// Metro module 13581 (f13582).
import { TransportTypes } from "./Constants";
import { RPCErrors, RPCCloseCodes, RPCCommands, RPCEvents, AnalyticEvents } from "../../Constants";
import Module12 from "../../_bundle/Module12";
import transformUser from "./helpers/transformUser";
import RPCError from "./RPCError";
import validateScope from "./helpers/validateScope";
import AnalyticsUtils from "../../utils/AnalyticsUtils";
import Module38 from "../../_bundle/Module38";
import Durations from "../../utils/Durations";
function $mk118910($env) {
return (a0) => {
const r1 = $env;
if (a0 == null) {
(0, r1[0])($env.$parent[0]);
} else {
const r3 = r1[1];
r3(new RPCError({
errorCode: RPCErrors.INVALID_PAYLOAD
}, a0.message));
}
};
}
let env9435 = {};
const c9435_9 = "RPC_STORE_WAIT";
const c9435_10 = [];
class RPCServer {
constructor(a0) {
const r7 = {
__proto__: new.target.prototype,
getCurrentUser: () => null,
onConnect: null,
onDisconnect: null,
events: null,
commands: null,
sockets: null,
subscriptions: null,
isSubscribedListeners: null
};
r7.onConnect = () => {};
r7.onDisconnect = () => {};
r7.events = {};
r7.commands = {};
r7.sockets = new Set();
r7.subscriptions = [];
r7.isSubscribedListeners = new Set();
r7.getJoi = a0;
return r7;
}
registerTransport(a0) {
a0.on("connect", (a0) => this.handleConnect(a0));
a0.on("request", (a0, a1) => this.handleRequest(a0, a1));
a0.on("disconnect", (a0, a1) => this.handleDisconnect(a0, a1));
}
handleConnect(a0) {
this.sockets.add(a0);
this.onConnect(a0);
const r6 = {
v: a0.version,
config: {
cdn_host: window.GLOBAL_ENV.CDN_HOST,
api_endpoint: window.GLOBAL_ENV.API_ENDPOINT,
environment: "production"
}
};
if (a0.transport === TransportTypes.IPC) {
const currentUser = this.getCurrentUser();
if (currentUser == null) {
a0.close(RPCCloseCodes.CLOSE_NORMAL, "User logged out");
return void 0;
}
r6.user = transformUser(currentUser);
}
this.dispatch(a0, null, RPCCommands.DISPATCH, RPCEvents.READY, r6);
}
handleDisconnect(a0, a1) {
a0.abortController.abort("DISCONNECTED");
this.removeSubscriptions(a0);
this.sockets.delete(a0);
this.onDisconnect(a0, a1);
}
handleRequest(a0, a1) {
let env20547 = {
$parent: env9435
};
env20547[3] = this;
env20547[1] = a0;
env20547[2] = a1;
env20547[0] = this;
new Promise((resolve) => {
const r1 = env20547[2];
if (r1.nonce != null && r1.nonce !== "") {
const cmd = r1.cmd;
const r19 = env20547[3].commands[cmd];
if (r19 == null) {
throw new RPCError({
errorCode: RPCErrors.INVALID_COMMAND
}, `Invalid command: ${r1.cmd}`);
}
const r8 = env20547[1];
if (validateScope(r8.authorization.scopes, r19.scope)) {
AnalyticsUtils.track(AnalyticEvents.RPC_COMMAND_SENT, {
command: cmd,
scope: typeof r19.scope === "object" ? JSON.stringify(r19.scope) : r19.scope,
application_id: r8.application.id,
socket_scope: r8.authorization.scopes.toString()
});
resolve(r19);
return void 0;
}
throw new RPCError({
errorCode: RPCErrors.INVALID_PERMISSIONS
}, "Not authenticated or invalid scope");
}
throw new RPCError({
errorCode: RPCErrors.INVALID_PAYLOAD
}, "Payload requires a nonce");
}).then((result) => {
let env27523 = {
$parent: env20547
};
env27523[0] = result;
const r3 = Promise;
let c27524_0 = async (c31693_0, c31693_1) => {
let c31693_2, c31693_3;
L14: {
let env32050;
c31693_3 = void 0;
const r12 = env32050 = {
$parent: env27523
};
c31693_2 = r12;
env32050[0] = c31693_0;
env32050[1] = c31693_1;
env32050[2] = void 0;
if (env27523[0].validation != null) {
const joi = await env20547[0].getJoi();
const r51 = c31693_2;
env32050[2] = joi;
Module38(env27523[0].validation != null, "command.validation must not be null");
env32050[2].validate(env20547[2].args, env27523[0].validation(env32050[2]), {
convert: false
}, $mk118910(r51));
break L14;
}
c31693_0(env27523[0]);
return void 0;
}
};
return new r3(function() {
return c27524_0.apply(this, arguments);
});
}).then((result) => {
const handler = result.handler;
const socket = env20547[1];
const r8 = env20547[2];
let args = r8.args;
if (args == null) {
args = {};
}
return handler.call(result, {
socket,
server: env20547[3],
cmd: r8.cmd,
evt: r8.evt,
nonce: r8.nonce,
args,
isSocketConnected: () => env20547[3].sockets.has(env20547[1]),
signal: socket.abortController.signal
});
}).then((result) => {
const r6 = env20547[3];
const r11 = env20547[1];
const r13 = env20547[2];
return r6.dispatch(r11, r13.nonce, r13.cmd, null, result);
}).catch(({ code, message }) => {
const r4 = env20547[3];
const r11 = env20547[1];
const r13 = env20547[2];
return r4.error(r11, r13.nonce, r13.cmd, code, message);
});
}
setCommandHandler(a0, a1) {
this.commands[a0] = a1;
}
setEventHandler(a0, a1) {
this.events[a0] = a1;
}
dispatch(a0, nonce = null, cmd = RPCCommands.DISPATCH, evt = null, data = null) {
a0.send({
cmd,
data,
evt,
nonce
});
}
error(a0, r8 = null, command = RPCCommands.DISPATCH, code = RPCErrors.UNKNOWN_ERROR, message = "Unknown Error") {
AnalyticsUtils.track(AnalyticEvents.RPC_SERVER_ERROR_CAUGHT, {
command,
code,
message
});
this.dispatch(a0, r8, command, RPCEvents.ERROR, {
code,
message
});
}
listenIsSubscribed(a0) {
this.isSubscribedListeners.add(a0);
return () => {
this.isSubscribedListeners.delete(a0);
};
}
dispatchIsSubscribedUpdate() {
this.isSubscribedListeners.forEach((isSubscribedListener) => isSubscribedListener());
}
isSubscribed(c20549_0, c20549_1) {
return this.subscriptions.find((subscription) => subscription.socket.application.id === c20549_0 && subscription.evt === c20549_1) !== void 0;
}
isChildSubscribed(a0, c20550_1) {
return a0 != null && this.subscriptions.find((subscription) => subscription.socket.application.parentId === a0 && subscription.evt === c20550_1) !== void 0;
}
getSubscription(c20551_0, c20551_1, c20551_2) {
return this.subscriptions.find((subscription) => subscription.socket === c20551_0 && subscription.evt === c20551_1 && Module12.isEqual(subscription.args, c20551_2));
}
addSubscription(a0, a1, a2, update = null) {
const dispatch2 = this.dispatch.bind(this, a0, null, RPCCommands.DISPATCH, a1);
if (this.getSubscription(a0, a1, a2) == null) {
this.subscriptions.push({
update,
dispatch: dispatch2,
prevState: update ? update({
prevState: null,
dispatch: dispatch2
}) : null,
socket: a0,
evt: a1,
args: a2
});
this.dispatchIsSubscribedUpdate();
}
}
removeSubscription(c20552_0, c20552_1, c20552_2) {
Module12.remove(this.subscriptions, (a0) => a0.socket === c20552_0 && a0.evt === c20552_1 && Module12.isEqual(a0.args, c20552_2));
this.dispatchIsSubscribedUpdate();
}
removeSubscriptions(c20553_0) {
Module12.remove(this.subscriptions, (a0) => a0.socket === c20553_0);
this.dispatchIsSubscribedUpdate();
}
dispatchToSubscriptions(c20554_1, c20554_2, c20554_0, a3) {
let r11 = a3 != null && a3 !== "";
if (r11) {
let r16 = !!c9435_10.includes(a3);
if (!r16) {
c9435_10.unshift(a3);
c9435_10.splice(50);
r16 = false;
}
r11 = r16;
}
if (!r11) {
this.subscriptions.forEach((subscription) => {
if (subscription.evt === c20554_1) {
let r0 = typeof c20554_2 === "function" ? !c20554_2(subscription) : typeof c20554_2 === "function";
if (!r0) {
let r20 = typeof c20554_2 === "object";
if (typeof c20554_2 === "object") {
let args = subscription.args;
if (args == null) {
args = {};
}
r20 = !Module12.isEqual(c20554_2, Module12.pick(args, Object.keys(c20554_2)));
}
r0 = r20;
}
if (!r0) {
const r33 = c20554_0;
if (typeof r33 === "function") {
c20554_0 = r33(subscription);
}
this.dispatch(subscription.socket, null, RPCCommands.DISPATCH, subscription.evt, c20554_0);
}
}
});
}
}
updateSubscriptions() {
this.subscriptions.forEach((subscription) => {
if (subscription.update) {
subscription.prevState = subscription.update(subscription);
}
});
}
storeWait(c20555_1, a1, a2) {
let removeSubscription2, uniqueId;
const r11 = a1();
if (!r11 && a2 !== 0) {
uniqueId = Module12.uniqueId();
removeSubscription2 = () => {};
return new Promise((c27525_0, c27525_1) => {
let c27525_2 = setTimeout(() => {
this.removeSubscription(c20555_1, c9435_9, {
uniqueId
});
c27525_1(new Error("timeout"));
}, a2 * Durations.Millis.SECOND);
this.addSubscription(c20555_1, c9435_9, {
uniqueId
}, () => {
const r2 = a1();
if (r2) {
clearTimeout(c27525_2);
c27525_0(r2);
}
});
}).then((result) => {
this.removeSubscription(c20555_1, c9435_9, {
uniqueId
});
return result;
});
}
return Promise.resolve(r11);
}
}
export default RPCServer;