add compression

This commit is contained in:
Skillz
2020-09-21 00:44:40 -04:00
parent 0c1f9529e0
commit db61a954c7
3 changed files with 49 additions and 36 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import type { MessageCreateOptions } from "../types/message.ts"; import type { MessageCreateOptions } from "../types/message.ts";
import { import type {
GetMessagesAfter, GetMessagesAfter,
GetMessagesBefore, GetMessagesBefore,
GetMessagesAround, GetMessagesAround,
+47 -34
View File
@@ -3,6 +3,8 @@ import type { IdentifyPayload } from "./client.ts";
import { import {
connectWebSocket, connectWebSocket,
isWebSocketCloseEvent, isWebSocketCloseEvent,
isWebSocketPingEvent,
isWebSocketPongEvent,
WebSocket, WebSocket,
} from "https://deno.land/std@0.67.0/ws/mod.ts"; } from "https://deno.land/std@0.67.0/ws/mod.ts";
import type { DiscordHeartbeatPayload } from "../types/discord.ts"; import type { DiscordHeartbeatPayload } from "../types/discord.ts";
@@ -11,12 +13,14 @@ import type { BotStatusRequest } from "../utils/utils.ts";
import { handleDiscordPayload } from "./shardingManager.ts"; import { handleDiscordPayload } from "./shardingManager.ts";
import { logRed } from "../utils/logger.ts"; import { logRed } from "../utils/logger.ts";
import { delay } from "https://deno.land/std@0.67.0/async/delay.ts";
import { GatewayOpcode } from "../types/discord.ts"; import { GatewayOpcode } from "../types/discord.ts";
import { import {
eventHandlers, eventHandlers,
botGatewayData, botGatewayData,
} from "./client.ts"; } from "./client.ts";
import { delay } from "https://deno.land/std@0.67.0/async/delay.ts";
import { cache } from "../utils/cache.ts";
import { inflate } from "https://deno.land/x/zlib.es@v1.0.0/mod.ts";
const basicShards = new Map<number, BasicShard>(); const basicShards = new Map<number, BasicShard>();
const heartbeating = new Set<number>(); const heartbeating = new Set<number>();
@@ -66,7 +70,48 @@ export async function createBasicShard(
await resume(basicShard, identifyPayload); await resume(basicShard, identifyPayload);
} }
for await (const message of basicShard.socket) { for await (let message of basicShard.socket) {
if (isWebSocketCloseEvent(message)) {
eventHandlers.debug?.(
{ type: "websocketClose", data: { shardID: basicShard.id, message } },
);
// These error codes should just crash the projects
if ([4004, 4005, 4012, 4013, 4014].includes(message.code)) {
logRed(`Close :( ${JSON.stringify(message)}`);
eventHandlers.debug?.(
{
type: "websocketErrored",
data: { shardID: basicShard.id, message },
},
);
throw new Error(
"Shard.ts: Error occurred that is not resumeable or able to be reconnected.",
);
}
// These error codes can not be resumed but need to reconnect from start
if ([4003, 4007, 4008, 4009].includes(message.code)) {
eventHandlers.debug?.(
{
type: "websocketReconnecting",
data: { shardID: basicShard.id, message },
},
);
createBasicShard(botGatewayData, identifyPayload, false, shardID);
} else {
basicShard.needToResume = true;
resumeConnection(botGatewayData, identifyPayload, basicShard.id);
}
continue;
} else if (isWebSocketPingEvent(message) || isWebSocketPongEvent(message)) {
continue;
}
if (message instanceof Uint8Array) {
message = new TextDecoder().decode(inflate(message as Uint8Array));
}
if (typeof message === "string") { if (typeof message === "string") {
const data = JSON.parse(message); const data = JSON.parse(message);
if (!data.t) eventHandlers.rawGateway?.(data); if (!data.t) eventHandlers.rawGateway?.(data);
@@ -118,38 +163,6 @@ export async function createBasicShard(
handleDiscordPayload(data, basicShard.id); handleDiscordPayload(data, basicShard.id);
break; break;
} }
} else if (isWebSocketCloseEvent(message)) {
eventHandlers.debug?.(
{ type: "websocketClose", data: { shardID: basicShard.id, message } },
);
// These error codes should just crash the projects
if ([4004, 4005, 4012, 4013, 4014].includes(message.code)) {
logRed(`Close :( ${JSON.stringify(message)}`);
eventHandlers.debug?.(
{
type: "websocketErrored",
data: { shardID: basicShard.id, message },
},
);
throw new Error(
"Shard.ts: Error occurred that is not resumeable or able to be reconnected.",
);
}
// These error codes can not be resumed but need to reconnect from start
if ([4003, 4007, 4008, 4009].includes(message.code)) {
eventHandlers.debug?.(
{
type: "websocketReconnecting",
data: { shardID: basicShard.id, message },
},
);
createBasicShard(botGatewayData, identifyPayload, false, shardID);
} else {
basicShard.needToResume = true;
resumeConnection(botGatewayData, identifyPayload, basicShard.id);
}
} }
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@ export let botGatewayData: DiscordBotGatewayData;
export const identifyPayload: IdentifyPayload = { export const identifyPayload: IdentifyPayload = {
token: "", token: "",
compress: false, compress: true,
properties: { properties: {
$os: "linux", $os: "linux",
$browser: "Discordeno", $browser: "Discordeno",