ZSTD on gateway

This commit is contained in:
Rory&
2025-10-17 11:58:53 +02:00
parent eef3fee049
commit 37594a5bec
8 changed files with 42 additions and 27 deletions
+16 -12
View File
@@ -43,22 +43,20 @@ const recurseJsonReplace = (json: any) => {
return json;
};
export function Send(socket: WebSocket, data: Payload) {
export async function Send(socket: WebSocket, data: Payload) {
if (process.env.WS_VERBOSE)
console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
if (process.env.WS_DUMP) {
const id = socket.session_id || "unknown";
(async () => {
await fs.mkdir(path.join("dump", id), {
recursive: true,
});
await fs.writeFile(
path.join("dump", id, `${Date.now()}.out.json`),
JSON.stringify(data, null, 2),
);
})();
await fs.mkdir(path.join("dump", id), {
recursive: true,
});
await fs.writeFile(
path.join("dump", id, `${Date.now()}.out.json`),
JSON.stringify(data, null, 2),
);
}
let buffer: Buffer | string;
@@ -71,9 +69,15 @@ export function Send(socket: WebSocket, data: Payload) {
else if (socket.encoding === "json")
buffer = JSON.stringify(data, JSONReplacer);
else return;
// TODO: compression
if (socket.deflate) {
buffer = socket.deflate.process(buffer) as Buffer;
if (socket.compress === "zlib-stream") {
buffer = socket.deflate!.process(buffer) as Buffer;
} else if (socket.compress === "zstd-stream") {
if (typeof(buffer) === "string")
buffer = Buffer.from(buffer as string);
buffer = await socket.zstdEncoder!.encode(buffer as Buffer) as Buffer;
}
return new Promise((res, rej) => {
+6 -1
View File
@@ -20,19 +20,24 @@ import { Intents, ListenEventOpts, Permissions } from "@spacebar/util";
import WS from "ws";
import { Deflate, Inflate } from "fast-zlib";
import { Capabilities } from "./Capabilities";
import { ZstdCompress } from "zlib";
import { ZstdDecompress } from "node:zlib";
import { Decoder, Encoder } from "@toondepauw/node-zstd";
export interface WebSocket extends WS {
version: number;
user_id: string;
session_id: string;
encoding: "etf" | "json";
compress?: "zlib-stream";
compress?: "zlib-stream" | "zstd-stream";
ipAddress?: string;
userAgent?: string; // for cdn request signing
shard_count?: bigint;
shard_id?: bigint;
deflate?: Deflate;
inflate?: Inflate;
zstdEncoder?: Encoder;
zstdDecoder?: Decoder;
heartbeatTimeout: NodeJS.Timeout;
readyTimeout: NodeJS.Timeout;
intents: Intents;