summary refs log tree commit diff
path: root/gateway/src
diff options
context:
space:
mode:
Diffstat (limited to 'gateway/src')
-rw-r--r--gateway/src/events/Close.ts1
-rw-r--r--gateway/src/events/Connection.ts7
-rw-r--r--gateway/src/events/Message.ts19
-rw-r--r--gateway/src/opcodes/Identify.ts9
-rw-r--r--gateway/src/schema/Identify.ts17
-rw-r--r--gateway/src/util/Send.ts4
-rw-r--r--gateway/src/util/WebSocket.ts3
7 files changed, 45 insertions, 15 deletions
diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts
index 5b7c512c..40d9a6f7 100644
--- a/gateway/src/events/Close.ts
+++ b/gateway/src/events/Close.ts
@@ -13,6 +13,7 @@ export async function Close(this: WebSocket, code: number, reason: string) {
 	if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
 	if (this.readyTimeout) clearTimeout(this.readyTimeout);
 	this.deflate?.close();
+	this.inflate?.close();
 	this.removeAllListeners();
 
 	if (this.session_id) {
diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts
index 2d41b7a3..af897ed7 100644
--- a/gateway/src/events/Connection.ts
+++ b/gateway/src/events/Connection.ts
@@ -6,7 +6,7 @@ import { setHeartbeat } from "../util/Heartbeat";
 import { IncomingMessage } from "http";
 import { Close } from "./Close";
 import { Message } from "./Message";
-import { createDeflate } from "zlib";
+import { Deflate, Inflate } from "fast-zlib";
 import { URL } from "url";
 import { Config } from "@fosscord/util";
 var erlpack: any;
@@ -45,7 +45,6 @@ export async function Connection(
 			return socket.close(CLOSECODES.Decode_error);
 		}
 
-		// @ts-ignore
 		socket.version = Number(searchParams.get("version")) || 8;
 		if (socket.version != 8)
 			return socket.close(CLOSECODES.Invalid_API_version);
@@ -55,8 +54,8 @@ export async function Connection(
 		if (socket.compress) {
 			if (socket.compress !== "zlib-stream")
 				return socket.close(CLOSECODES.Decode_error);
-			socket.deflate = createDeflate({ chunkSize: 65535 });
-			socket.deflate.on("data", (chunk) => socket.send(chunk));
+			socket.deflate = new Deflate();
+			socket.inflate = new Inflate();
 		}
 
 		socket.events = {};
diff --git a/gateway/src/events/Message.ts b/gateway/src/events/Message.ts
index b675afcd..83917690 100644
--- a/gateway/src/events/Message.ts
+++ b/gateway/src/events/Message.ts
@@ -3,7 +3,7 @@ import { WebSocket, Payload } from "@fosscord/gateway";
 var erlpack: any;
 try {
 	erlpack = require("@yukikaze-bot/erlpack");
-} catch (error) {}
+} catch (error) { }
 import OPCodeHandlers from "../opcodes";
 import { Tuple } from "lambert-server";
 import { check } from "../opcodes/instanceOf";
@@ -22,8 +22,19 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 
 	if (this.encoding === "etf" && buffer instanceof Buffer)
 		data = erlpack.unpack(buffer);
-	else if (this.encoding === "json" && typeof buffer === "string")
-		data = JSON.parse(buffer);
+	else if (this.encoding === "json" && buffer instanceof Buffer) {
+		if (this.inflate) {
+			try {
+				buffer = this.inflate.process(buffer) as any;
+			} catch {
+				buffer = buffer.toString() as any;
+			}
+		}
+		data = JSON.parse(buffer as string);
+	}
+	else if (typeof buffer == "string") {
+		data = JSON.parse(buffer as string);
+	}
 	else return;
 
 	check.call(this, PayloadSchema, data);
@@ -42,6 +53,6 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 	} catch (error) {
 		console.error(error);
 		// if (!this.CLOSED && this.CLOSING)
-			return this.close(CLOSECODES.Unknown_error);
+		return this.close(CLOSECODES.Unknown_error);
 	}
 }
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 301f714d..041512c5 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -175,7 +175,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 			avatar: related_user.avatar,
 			bot: related_user.bot,
 			bio: related_user.bio,
-			premium_since: user.premium_since
+			premium_since: user.premium_since,
+			accent_color: related_user.accent_color,
 		};
 		users.push(public_related_user);
 	}
@@ -222,10 +223,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		premium: user.premium,
 		premium_type: user.premium_type,
 		public_flags: user.public_flags,
+		premium_usage_flags: user.premium_usage_flags,
+		purchased_flags: user.purchased_flags,
 		username: user.username,
 		verified: user.verified,
 		bot: user.bot,
-		accent_color: user.accent_color || 0,
+		accent_color: user.accent_color,
 		banner: user.banner,
 		bio: user.bio,
 		premium_since: user.premium_since
@@ -274,6 +277,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		users: users.filter((x) => x).unique(),
 		merged_members: merged_members,
 		// shard // TODO: only for user sharding
+		sessions: [], // TODO:
+		presences: [], // TODO:
 	};
 
 	// TODO: send real proper data structure
diff --git a/gateway/src/schema/Identify.ts b/gateway/src/schema/Identify.ts
index 6aa93ce7..6f68b515 100644
--- a/gateway/src/schema/Identify.ts
+++ b/gateway/src/schema/Identify.ts
@@ -42,7 +42,14 @@ export const IdentifySchema = {
 		$read_state_version: Number,
 		$user_guild_settings_version: Number,
 		$user_settings_version: undefined,
-		$useruser_guild_settings_version: Number,
+		$useruser_guild_settings_version: undefined,
+	},
+	$clientState: {
+		$guildHashes: Object,
+		$highestLastMessageId: String || Number,
+		$readStateVersion: Number,
+		$useruserGuildSettingsVersion: undefined,
+		$userGuildSettingsVersion: undefined,
 	},
 	$v: Number,
 	$version: Number,
@@ -76,6 +83,7 @@ export interface IdentifySchema {
 	presence?: ActivitySchema;
 	compress?: boolean;
 	large_threshold?: number;
+	largeThreshold?: number;
 	shard?: [bigint, bigint];
 	guild_subscriptions?: boolean;
 	capabilities?: number;
@@ -87,5 +95,12 @@ export interface IdentifySchema {
 		user_settings_version?: number;
 		useruser_guild_settings_version?: number;
 	};
+	clientState?: {
+		guildHashes?: any;
+		highestLastMessageId?: string | number;
+		readStateVersion?: number;
+		userGuildSettingsVersion?: number;
+		useruserGuildSettingsVersion?: number;
+	};
 	v?: number;
 }
diff --git a/gateway/src/util/Send.ts b/gateway/src/util/Send.ts
index c4202b21..2a4aa5ef 100644
--- a/gateway/src/util/Send.ts
+++ b/gateway/src/util/Send.ts
@@ -14,9 +14,7 @@ export function Send(socket: WebSocket, data: Payload) {
 	else return;
 	// TODO: compression
 	if (socket.deflate) {
-		socket.deflate.write(buffer);
-		socket.deflate.flush();
-		return;
+		buffer = socket.deflate.process(buffer) as Buffer;
 	}
 
 	return new Promise((res, rej) => {
diff --git a/gateway/src/util/WebSocket.ts b/gateway/src/util/WebSocket.ts
index e3313f40..1ca90340 100644
--- a/gateway/src/util/WebSocket.ts
+++ b/gateway/src/util/WebSocket.ts
@@ -1,6 +1,6 @@
 import { Intents, Permissions } from "@fosscord/util";
 import WS from "ws";
-import { Deflate } from "zlib";
+import { Deflate, Inflate } from "fast-zlib";
 
 export interface WebSocket extends WS {
 	version: number;
@@ -11,6 +11,7 @@ export interface WebSocket extends WS {
 	shard_count?: bigint;
 	shard_id?: bigint;
 	deflate?: Deflate;
+	inflate?: Inflate;
 	heartbeatTimeout: NodeJS.Timeout;
 	readyTimeout: NodeJS.Timeout;
 	intents: Intents;