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

273 lines
7.2 KiB
TypeScript

// Metro module 12708 (f12709).
import { NativeModules } from "react-native";
import { createZstdContextWeb, supportsZstd } from "./GatewayZstdUtils.native";
import * as _Module12711 from "../../_bundle/Module12711";
import Logger from "../debug/Logger";
import * as _PlatformUtils from "../../utils/PlatformUtils";
import * as _Module12710 from "../../_bundle/Module12710";
import { ProcessArgs } from "../../utils/ProcessArgs";
let c8757_6, c8757_7, c8757_8, c8757_9;
let c8757_4 = [];
class BaseGatewayCompressionHandler {
_onDataReady = null;
constructor(a0) {
this._gatewayEncoding = a0;
}
static canUse() {
return false;
}
bindWebSocket() {}
feed() {}
dataReady(a0) {
this._onDataReady = a0;
}
}
const r33 = class extends BaseGatewayCompressionHandler {
_decoder = null;
constructor(a0) {
super(a0);
if (this._gatewayEncoding.wantsString()) {
this._decoder = new TextDecoder("utf-8");
} else {
this._decoder = null;
}
this._stream = createZstdContextWeb();
}
static canUse() {
return false;
}
getAlgorithm() {
return "zstd-stream";
}
usesLegacyCompression() {
return false;
}
feed(a0) {
if (this._stream == null) {
throw new Error("Trying to decompress with zstd but did not initialize with it");
}
if (a0 instanceof ArrayBuffer) {
const r17 = this._stream.decompress(a0);
const r19 = this._decoder != null ? this._decoder.decode(r17) : r17;
if (this._onDataReady != null) {
this._onDataReady(r19);
}
return void 0;
}
throw new Error("Expected array buffer, but got " + typeof a0);
}
close() {}
};
c8757_6 = r33;
c8757_4.push(c8757_6);
const r42 = class extends BaseGatewayCompressionHandler {
_pako = _Module12711;
_usesZstd = false;
_zstdDecoder = null;
_zstdStream = null;
_inflate = new this._pako.Inflate({
chunkSize: 65536,
to: this._gatewayEncoding.wantsString() ? "string" : ""
});
constructor(a0) {
super(a0);
this._inflate.onEnd = this.handleFlushEnd.bind(this);
}
static canUse() {
return false;
}
getAlgorithm() {
return "zlib-stream";
}
usesLegacyCompression() {
return false;
}
feed(a0) {
if (this._inflate == null) {
throw new Error("Trying to feed to closed compression adapter");
}
if (this._onDataReady === null) {
throw new Error("Cannot feed unless a data ready callback is registered.");
}
if (a0 instanceof ArrayBuffer) {
const dataView = new DataView(a0);
this._inflate.push(a0, !!(dataView.byteLength >= 4 && dataView.getUint32(dataView.byteLength - 4, false) === 65535) && this._pako.Z_SYNC_FLUSH);
return void 0;
}
throw new Error("Expected array buffer, but got " + typeof a0);
}
close() {
if (this._inflate != null) {
this._inflate.onEnd = null;
this._inflate.chunks = [];
}
this._inflate = null;
}
handleFlushEnd(a0) {
let r34;
const _pako = this._pako;
const _inflate = this._inflate;
if (_inflate != null) {
if (a0 !== _pako.Z_OK) {
throw new Error(`zlib error, ${a0}, ${_inflate.strm.msg}`);
}
const chunks = _inflate.chunks;
const length = chunks.length;
if (this._gatewayEncoding.wantsString()) {
r34 = length > 1 ? chunks.join("") : chunks[0];
} else if (length > 1) {
const r35 = 0 < length;
let r0 = 0;
let r36 = 0;
let r14 = 0;
if (r35) {
do {
r36 = r36 + chunks[r0].length;
r0 = r0 + 1;
r14 = r36;
} while (r0 < length);
}
const uint8Array = new Uint8Array(r14);
const r45 = 0 < length;
let r46 = 0;
let r47 = 0;
r34 = uint8Array;
if (r45) {
do {
const r48 = chunks[r46];
uint8Array.set(r48, r47);
r47 = r47 + r48.length;
r46 = r46 + 1;
r34 = uint8Array;
} while (r46 < length);
}
} else {
r34 = chunks[0];
}
chunks.length = 0;
if (this._onDataReady != null) {
this._onDataReady(r34);
}
} else {
new Logger("GatewayCompressionHandler").error("flush end happened on closed compression adapter");
}
}
};
c8757_7 = r42;
c8757_4.push(c8757_7);
const r53 = class extends BaseGatewayCompressionHandler {
_pako = _Module12711;
static canUse() {
return false;
}
getAlgorithm() {
return null;
}
usesLegacyCompression() {
return true;
}
feed(a0) {
const _pako = this._pako;
const r12 = a0 instanceof ArrayBuffer && this._gatewayEncoding.wantsString() ? _pako.inflate(a0, {
to: "string"
}) : a0;
if (this._onDataReady == null) {
throw new Error("Cannot feed unless a data ready callback is registered.");
}
this._onDataReady(r12);
}
close() {}
};
c8757_8 = r53;
c8757_4.push(c8757_8);
const r63 = class extends BaseGatewayCompressionHandler {
_socketId = null;
constructor(a0) {
super(a0);
}
static canUse() {
return _PlatformUtils.isAndroid() ? _Module12710.default != null : NativeModules.DCDCompressionManager != null;
}
bindWebSocket(a0) {
this.close();
this._socketId = a0._socketId;
const r16 = supportsZstd();
const isAndroid = _PlatformUtils.isAndroid();
if (r16) {
if (isAndroid) {
if (_Module12710.default != null) {
_Module12710.default.enableZstdStreamSupport(this._socketId);
}
} else {
NativeModules.DCDCompressionManager.enableZstdStreamSupport(this._socketId, 0);
}
} else if (isAndroid) {
if (_Module12710.default != null) {
_Module12710.default.enableZlibStreamSupport(this._socketId);
}
} else {
NativeModules.DCDCompressionManager.enableZlibStreamSupport(this._socketId);
}
}
getAlgorithm() {
return supportsZstd() ? "zstd-stream" : "zlib-stream";
}
usesLegacyCompression() {
return false;
}
feed(a0) {
if (this._onDataReady == null) {
throw new Error("Cannot feed unless a data ready callback is registered.");
}
if (a0 !== null) {
this._onDataReady(a0);
}
}
close() {
const _socketId = this._socketId;
this._socketId = null;
if (_socketId !== null) {
if (_PlatformUtils.isAndroid()) {
if (_Module12710.default != null) {
_Module12710.default.disableZlibStreamSupport(_socketId);
}
} else {
NativeModules.DCDCompressionManager.disableZlibStreamSupport(_socketId);
}
}
}
};
c8757_9 = r63;
c8757_4.push(c8757_9);
class NullGatewayCompressionHandler extends BaseGatewayCompressionHandler {
static canUse() {
return true;
}
getAlgorithm() {
return null;
}
usesLegacyCompression() {
return false;
}
feed(a0) {
if (this._onDataReady == null) {
throw new Error("Cannot feed unless a data ready callback is registered.");
}
this._onDataReady(a0);
}
close() {}
}
c8757_4.push(NullGatewayCompressionHandler);
export function getCompressionHandler(a0) {
if (ProcessArgs.isDiscordGatewayPlaintextSet()) {
return new NullGatewayCompressionHandler(a0);
}
for (const item of c8757_4) {
if (!item.canUse()) {
continue;
}
return new item(a0);
}
return new NullGatewayCompressionHandler(a0);
}