Files
discord-decomp/modules/gateway/native/fast_connect.tsx
T
2026-11-01 12:12:55 +02:00

188 lines
7.0 KiB
TypeScript

// Metro module 15 (f16).
import "../../../utils/checkEnv";
import { AppState, NativeModules } from "react-native";
import { VERSION_TO_FORCE_RESYNCING_ALL_DATA } from "../../app_database/modules/KvCacheVersionConstants";
import Logger from "../../debug/Logger";
import * as _PlatformUtils from "../../../utils/PlatformUtils";
import Module502 from "../../../_bundle/Module502";
import { getSuperProperties } from "../../../_bundle/Module503";
import { describeConnectionReasons } from "../RequestGatewaySocket";
import { getClientCapabilities } from "../GatewayCapabilities";
import { isCacheEnabled } from "../../cache/isCacheEnabled.native";
import Module12710 from "../../../_bundle/Module12710";
import getCachedUseAltGateway_native from "../getCachedUseAltGateway.native";
import createWebSocket_android from "../createWebSocket.android";
import Module10 from "../../../_bundle/Module10";
import TTITracker from "../../tti_analytics/TTITracker";
let c36_7, c36_8;
export function createFastConnectSocket() {
if (window.WebSocket != null) {
const r12 = _PlatformUtils.isAndroid() ? Module12710.getConstants().supportsZstd : NativeModules.DCDCompressionManager.supportsZstd;
const r16 = r12 ? "zstd-stream" : "zlib-stream";
let GATEWAY_ALT_ENDPOINT = window.GLOBAL_ENV.GATEWAY_ALT_ENDPOINT;
if (!(getCachedUseAltGateway_native() && GATEWAY_ALT_ENDPOINT != null)) {
GATEWAY_ALT_ENDPOINT = window.GLOBAL_ENV.GATEWAY_ENDPOINT;
}
let c12108_1;
const gateway = `${GATEWAY_ALT_ENDPOINT}/?encoding=json&v=${window.GLOBAL_ENV.API_VERSION}&compress=${r16}`;
c36_7.log("[FAST CONNECT] " + gateway);
let c12108_0 = Date.now();
const webSocket_android = createWebSocket_android(gateway);
const r60 = parseFloat(webSocket_android._socketId);
if (isNaN(r60)) {
c36_7.log("[FAST CONNECT] Unable to create socketId from NaN value ", webSocket_android._socketId);
} else {
const isAndroid = _PlatformUtils.isAndroid();
if (r12) {
if (isAndroid) {
Module12710.enableZstdStreamSupport(r60);
} else {
NativeModules.DCDCompressionManager.enableZstdStreamSupport(r60, 0);
}
} else if (isAndroid) {
Module12710.enableZlibStreamSupport(r60);
} else {
NativeModules.DCDCompressionManager.enableZlibStreamSupport(r60);
}
const state = {
open: false,
gateway,
identify: false,
messages: [],
clientState: null,
userId: null
};
c12108_1 = state;
webSocket_android.onopen = () => {
Module10.mark("🌐", "Fastconnect socket opened");
const r9 = Date.now() - c12108_0;
c36_7.log(`connected and identified in ${r9}ms didIdentify:${c12108_1.identify}`);
c12108_1.open = true;
};
const onerror = () => {
Module10.mark("🌐", "Fastconnect socket close");
window._ws = null;
};
webSocket_android.onerror = onerror;
webSocket_android.onclose = onerror;
webSocket_android.onmessage = (a0) => {
Module10.mark("🌐", "Fastconnect socket message");
c12108_1.messages.push(a0);
};
window._ws = {
ws: webSocket_android,
state
};
Module10.mark("🌐", "Fastconnect socket created");
}
} else {
c36_7.log("Skipping fast_connect because `window.WebSocket` does not exist.");
}
}
c36_7 = new Logger("FAST CONNECT");
c36_7.info("initial app state (import time)", AppState.currentState);
c36_8 = null;
createFastConnectSocket();
export function closeFastConnectSocket() {
if (window._ws != null) {
window._ws.ws.close();
window._ws = null;
c36_8 = null;
}
}
export function getLastFastConnectIdentifyUserId() {
return c36_8;
}
export function identifyWebSocket() {
if (window._ws != null) {
TTITracker.beginFastConnect.measure(() => {
let installation_id, useChannelObfuscation2;
const token = TTITracker.loadFastConnectNativeModule.measure(() => {
let r10;
if (_PlatformUtils.isAndroid()) {
let token = Module502.getConstants().token;
if (token == null) {
token = null;
}
r10 = token;
} else {
r10 = NativeModules.DCDFastConnectManager.token;
}
return r10;
});
if (token != null && token !== "") {
const _ws = window._ws;
const { ws, state } = _ws;
const _socketId = ws._socketId;
let r15 = _PlatformUtils.isAndroid() ? Module502.getConstants().userId : NativeModules.DCDFastConnectManager.userId;
c36_8 = r15 == null ? null : r15;
state.userId = r15 == null ? null : r15;
if (r15 == null) {
r15 = null;
}
c36_7.info("prepareIdentify: app state: ", AppState.currentState);
if (_PlatformUtils.isAndroid()) {
let analyticsInstallation = Module502.getConstants().analyticsInstallation;
if (analyticsInstallation == null) {
analyticsInstallation = null;
}
installation_id = analyticsInstallation;
} else {
installation_id = NativeModules.DCDFastConnectManager.analyticsInstallation;
}
const properties = {
...getSuperProperties(),
client_app_state: AppState.currentState,
is_fast_connect: true,
gateway_connect_reasons: describeConnectionReasons(),
...installation_id != null ? {
installation_id
} : {}
};
if (_PlatformUtils.isAndroid()) {
let useChannelObfuscation = Module502.getConstants().useChannelObfuscation;
if (useChannelObfuscation == null) {
useChannelObfuscation = false;
}
useChannelObfuscation2 = useChannelObfuscation;
} else {
useChannelObfuscation2 = NativeModules.DCDFastConnectManager.useChannelObfuscation;
if (useChannelObfuscation2 == null) {
useChannelObfuscation2 = false;
}
}
const r62 = {
token,
properties,
capabilities: getClientCapabilities({
useChannelObfuscation: useChannelObfuscation2
}),
client_state: {
guild_versions: {}
}
};
const r100 = JSON.stringify({
op: 2,
d: r62
});
if (_PlatformUtils.isAndroid()) {
let r116 = r15 == null ? void 0 : r15.toString();
if (r116 == null) {
r116 = null;
}
Module502.prepareIdentify(r116, r100, parseFloat(_socketId), isCacheEnabled() ? VERSION_TO_FORCE_RESYNCING_ALL_DATA : void 0);
} else {
NativeModules.DCDFastConnectManager.prepareIdentify(r15, r100, _socketId, isCacheEnabled() ? VERSION_TO_FORCE_RESYNCING_ALL_DATA : null);
}
const client_state = r62.client_state;
state.identify = true;
state.clientState = client_state;
} else {
c36_7.log("Skipping fast_connect because we could not find a token to connect with.");
}
});
} else {
c36_7.log("Skipping identifyWebSocket because socket is null");
}
}