346 lines
11 KiB
TypeScript
346 lines
11 KiB
TypeScript
// Metro module 10624 (f10625).
|
|
import EmbeddedActivitiesStore from "../../activities/EmbeddedActivitiesStore";
|
|
import { RPC_EMBEDDED_APP_SCOPE } from "../Constants";
|
|
import { AnalyticEvents, ComponentActions, RPCCloseCodes, RPCErrors } from "../../../Constants";
|
|
import Durations from "../../../utils/Durations";
|
|
import RPCOpcodes from "../RPCOpcodes";
|
|
import { EventEmitter } from "../../../_bundle/EventEmitter_652";
|
|
import { ComponentDispatch } from "../../../utils/ComponentDispatchUtils";
|
|
import AnalyticsUtils from "../../../utils/AnalyticsUtils";
|
|
import { getEmbeddedActivityLocationChannelId, getEmbeddedActivityLocationGuildId } from "../../activities/utils/embeddedActivityLocationUtils";
|
|
import RPCError from "../RPCError";
|
|
import createRpcJoiSchemaObject from "../helpers/createRpcJoiSchemaObject";
|
|
const timeout_ms = 10 * Durations.Millis.SECOND;
|
|
let c7129_12 = new Map();
|
|
let c7129_13 = new Set();
|
|
const postClose = (a0, a1, a2) => {
|
|
a2([RPCOpcodes.CLOSE, a1], a0);
|
|
};
|
|
class PostMessageTransport extends EventEmitter {
|
|
constructor(a0, a1, a2, a3) {
|
|
let c18157_0, c18157_1;
|
|
let c18158_0, c18158_1;
|
|
super();
|
|
const _this = this;
|
|
this.disconnectSocket = function disconnectSocket(a0, a1) {
|
|
let r5 = arguments[2];
|
|
if (r5 === void 0) {
|
|
r5 = false;
|
|
}
|
|
_this.emit("disconnect", a0, r5 ? void 0 : a1);
|
|
const close = a0.close;
|
|
const code = a1.code;
|
|
let message = a1.message;
|
|
if (message == null) {
|
|
message = "Unknown";
|
|
}
|
|
close.call(a0, code, message);
|
|
let r27 = Array.from(c7129_12.entries()).find((item) => {
|
|
const [, r13] = item;
|
|
return r13 === a0;
|
|
});
|
|
if (r27 == null) {
|
|
r27 = [null, null];
|
|
}
|
|
const [r30] = r27;
|
|
if (r30 != null) {
|
|
c7129_12.delete(r30);
|
|
}
|
|
};
|
|
this.handleIFrameMount = (a0) => {
|
|
c7129_13.add(a0.id);
|
|
_this.handshakeFailureTimeoutId = setTimeout(() => {
|
|
Array.from(EmbeddedActivitiesStore.getSelfEmbeddedActivities().entries()).forEach((item) => {
|
|
const [application_id, r8] = item;
|
|
AnalyticsUtils.track(AnalyticEvents.ACTIVITY_HANDSHAKE_TIMED_OUT, {
|
|
application_id,
|
|
channel_id: getEmbeddedActivityLocationChannelId(r8.location),
|
|
guild_id: getEmbeddedActivityLocationGuildId(r8.location),
|
|
timeout_ms
|
|
});
|
|
});
|
|
}, timeout_ms);
|
|
};
|
|
this.handleIFrameUnmount = ({ id }) => {
|
|
c7129_13.delete(id);
|
|
let r21 = Array.from(c7129_12.entries()).find((item) => {
|
|
const [, r13] = item;
|
|
return r13.frameId === id;
|
|
});
|
|
if (r21 == null) {
|
|
r21 = [null, null];
|
|
}
|
|
const [r24, r9] = r21;
|
|
if (r9 != null && r24 != null) {
|
|
_this.disconnectSocket(r9, {
|
|
code: RPCCloseCodes.CLOSE_NORMAL,
|
|
message: "iFrame gone"
|
|
}, true);
|
|
c7129_12.delete(r24);
|
|
}
|
|
};
|
|
this.handleMessage = (a0, a1, a2) => {
|
|
const r5 = c7129_12.get(a1);
|
|
try {
|
|
_this.routeEvent(r5, a1, a0, a2);
|
|
} catch (r17) {
|
|
if (r17 instanceof RPCError && r17.errorCode === RPCErrors.INVALID_PAYLOAD) {
|
|
throw r17;
|
|
}
|
|
if (r5 != null) {
|
|
_this.disconnectSocket(r5, {
|
|
code: r17.code,
|
|
message: r17.message
|
|
}, true);
|
|
} else {
|
|
postClose(a1, {
|
|
code: r17.code,
|
|
message: r17.message
|
|
}, a2);
|
|
}
|
|
}
|
|
};
|
|
this.handleFrame = (a0, a1, a2) => {
|
|
let r14;
|
|
if (a0 !== a1.origin) {
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.INVALID_ORIGIN
|
|
}, "Origin has changed");
|
|
}
|
|
try {
|
|
r14 = typeof a2 === "string" ? JSON.parse(a2) : a2;
|
|
} catch ($e) {
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, "Payload not recognized encoding");
|
|
}
|
|
const onFrameHandled = _this.onFrameHandled;
|
|
if (onFrameHandled != null) {
|
|
onFrameHandled.call(_this, r14, _this.logger, a1);
|
|
}
|
|
_this.emit("request", a1, r14);
|
|
};
|
|
c18157_0 = void 0;
|
|
c18157_1 = this;
|
|
c18157_0 = async (c30245_0, c30245_1, c30245_2) => {
|
|
let encoding, c30245_6;
|
|
let c30245_7 = 0;
|
|
let c31537_0, c31537_1, c31537_2, c31537_3, c31537_4, c31537_5, c31537_6, c31537_7, c31537_8, c31537_9;
|
|
try {
|
|
c31537_0 = c30245_0;
|
|
c31537_1 = c30245_1;
|
|
c31537_2 = c30245_2;
|
|
c31537_3 = void 0;
|
|
c31537_4 = void 0;
|
|
c31537_5 = void 0;
|
|
c31537_6 = void 0;
|
|
if (c18157_1.handshakeFailureTimeoutId != null) {
|
|
clearTimeout(c18157_1.handshakeFailureTimeoutId);
|
|
}
|
|
c31537_3 = (await import("../../../_bundle/Module10628")).default;
|
|
c30245_7 = 1;
|
|
c31537_3.assert(c31537_1, createRpcJoiSchemaObject(c31537_3).required().keys({
|
|
v: c31537_3.number().min(1).max(1).required(),
|
|
encoding: c31537_3.string().equal("json").optional(),
|
|
client_id: c31537_3.string().required(),
|
|
frame_id: c31537_3.string().required(),
|
|
sdk_version: c31537_3.string().optional()
|
|
}));
|
|
c30245_7 = 0;
|
|
c31537_5 = c31537_1;
|
|
c31537_6 = c31537_5.frame_id;
|
|
if (!c7129_13.has(c31537_6)) {
|
|
c18157_1.logger.error(`Unrecognized frame ID ${c31537_6}`);
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, `Unrecognized frame ID ${c31537_6}`);
|
|
}
|
|
if (c31537_5.sdk_version != null) {
|
|
AnalyticsUtils.track(AnalyticEvents.ACTIVITY_HANDSHAKE, {
|
|
application_id: c31537_5.client_id,
|
|
sdk_version: c31537_5.sdk_version
|
|
});
|
|
}
|
|
c30245_7 = 2;
|
|
const r181 = {
|
|
origin: c31537_0,
|
|
postMessageToRPCClient: c31537_2,
|
|
frameId: c31537_6,
|
|
version: Number(c31537_5.v),
|
|
logger: c18157_1.logger,
|
|
postClose,
|
|
encoding: null
|
|
};
|
|
const encoding2 = c31537_5.encoding;
|
|
encoding = encoding2;
|
|
if (encoding2 == null) {
|
|
encoding = "json";
|
|
}
|
|
r181.encoding = encoding;
|
|
c31537_4 = c18157_1.createPostMessageProxySocket(r181);
|
|
c30245_7 = 0;
|
|
c18157_1.logger.info(`Socket Opened: ${c31537_4.id}`);
|
|
c30245_7 = 3;
|
|
await c18157_1.validateSocketClient(c31537_4, c31537_0, c31537_5.client_id);
|
|
if (!c7129_13.has(c31537_6)) {
|
|
c18157_1.logger.error(`Frame ID ${c31537_6} no longer exists`);
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, `Unrecognized frame ID ${c31537_6}`);
|
|
}
|
|
c7129_12.set(c31537_0, c31537_4);
|
|
c7129_13.delete(c31537_6);
|
|
c31537_4.authorization.scopes.push(RPC_EMBEDDED_APP_SCOPE);
|
|
c18157_1.emit("connect", c31537_4);
|
|
c18157_1.logger.info(`Socket Validated: ${c31537_4.id}`);
|
|
c30245_7 = 0;
|
|
} catch ($e) {
|
|
const r46 = $e;
|
|
let r7 = r46;
|
|
c30245_6 = r46;
|
|
let r47 = c30245_7;
|
|
if (r47 !== 0) {
|
|
while (true) {
|
|
if (r47 === 1) {
|
|
try {
|
|
const r367 = c30245_6;
|
|
c30245_7 = 0;
|
|
c31537_7 = r367;
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, c31537_7.message);
|
|
} catch (r364) {
|
|
r7 = r364;
|
|
c30245_6 = r364;
|
|
r47 = c30245_7;
|
|
if (r47 === 0) {
|
|
break;
|
|
}
|
|
}
|
|
} else if (r47 === 2) {
|
|
try {
|
|
const r351 = c30245_6;
|
|
c30245_7 = 0;
|
|
c31537_8 = r351;
|
|
c18157_1.logger.error(`Error opening window socket ${c31537_8}`);
|
|
throw c31537_8;
|
|
} catch (r348) {
|
|
r7 = r348;
|
|
c30245_6 = r348;
|
|
r47 = c30245_7;
|
|
if (r47 === 0) {
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
try {
|
|
const r332 = c30245_6;
|
|
c30245_7 = 0;
|
|
c31537_9 = r332;
|
|
c18157_1.logger.info(`Socket Closed: ${c31537_4.id}, ${c31537_9.message}`);
|
|
throw c31537_9;
|
|
} catch (r329) {
|
|
r7 = r329;
|
|
c30245_6 = r329;
|
|
r47 = c30245_7;
|
|
if (r47 === 0) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
throw r7;
|
|
}
|
|
};
|
|
this.handleHandshake = function() {
|
|
return c18157_0.apply(this, arguments);
|
|
};
|
|
c18158_0 = void 0;
|
|
c18158_1 = this;
|
|
c18158_0 = async (c30246_0, c30246_1) => {
|
|
let c30246_4;
|
|
let c30246_5 = 0;
|
|
let c31538_0, c31538_1, c31538_2, c31538_3;
|
|
try {
|
|
c31538_0 = c30246_0;
|
|
c31538_1 = c30246_1;
|
|
c31538_2 = void 0;
|
|
c31538_2 = (await import("../../../_bundle/Module10628")).default;
|
|
c30246_5 = 1;
|
|
c31538_2.assert(c31538_1, createRpcJoiSchemaObject(c31538_2).required().keys({
|
|
code: c31538_2.number().valid(Object.values(RPCCloseCodes)).required(),
|
|
message: c31538_2.string().optional()
|
|
}));
|
|
c30246_5 = 0;
|
|
c18158_1.disconnectSocket(c31538_0, c31538_1);
|
|
} catch ($e) {
|
|
const r34 = $e;
|
|
let r7 = r34;
|
|
c30246_4 = r34;
|
|
if (c30246_5 !== 0) {
|
|
while (true) {
|
|
try {
|
|
const r105 = c30246_4;
|
|
c30246_5 = 0;
|
|
c31538_3 = r105;
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, c31538_3.message);
|
|
} catch (r101) {
|
|
r7 = r101;
|
|
c30246_4 = r101;
|
|
if (c30246_5 === 0) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
throw r7;
|
|
}
|
|
};
|
|
this.handleClose = function() {
|
|
return c18158_0.apply(this, arguments);
|
|
};
|
|
ComponentDispatch.subscribe(ComponentActions.IFRAME_MOUNT, this.handleIFrameMount);
|
|
ComponentDispatch.subscribe(ComponentActions.IFRAME_UNMOUNT, this.handleIFrameUnmount);
|
|
this.validateSocketClient = a0;
|
|
this.logger = a1;
|
|
this.createPostMessageProxySocket = a2;
|
|
this.onFrameHandled = a3;
|
|
}
|
|
routeEvent(a0, a1, a2, a3) {
|
|
if (Array.isArray(a2)) {
|
|
const [r11, r8] = a2;
|
|
if (RPCOpcodes.HANDSHAKE === r11) {
|
|
if (a0 != null) {
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, "Already connected");
|
|
}
|
|
return this.handleHandshake(a1, r8, a3);
|
|
}
|
|
if (RPCOpcodes.FRAME === r11) {
|
|
if (a0 == null) {
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, "Not connected");
|
|
}
|
|
return this.handleFrame(a1, a0, r8);
|
|
}
|
|
if (RPCOpcodes.CLOSE === r11) {
|
|
if (a0 == null) {
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, "Not connected");
|
|
}
|
|
return this.handleClose(a0, r8);
|
|
}
|
|
throw new RPCError({
|
|
closeCode: RPCCloseCodes.CLOSE_UNSUPPORTED
|
|
}, "Invalid opcode");
|
|
}
|
|
}
|
|
}
|
|
export default PostMessageTransport;
|