Use erlpack instead of @yukikaze-bot/erlpack (#968)

This commit is contained in:
Madeline
2023-02-01 13:00:04 +11:00
committed by GitHub
parent 72c0319a9f
commit da20866967
8 changed files with 80 additions and 49 deletions
+10 -12
View File
@@ -27,12 +27,13 @@ import { Close } from "./Close";
import { Message } from "./Message";
import { Deflate, Inflate } from "fast-zlib";
import { URL } from "url";
import { Config } from "@fosscord/util";
let erlpack: unknown;
import { Config, ErlpackType } from "@fosscord/util";
let erlpack: ErlpackType | null = null;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {
/* empty */
erlpack = require("erlpack") as ErlpackType;
} catch (e) {
// empty
}
// TODO: check rate limit
@@ -84,14 +85,11 @@ export async function Connection(
const { searchParams } = new URL(`http://localhost${request.url}`);
// @ts-ignore
socket.encoding = searchParams.get("encoding") || "json";
if (!["json", "etf"].includes(socket.encoding)) {
if (socket.encoding === "etf" && erlpack) {
throw new Error(
"Erlpack is not installed: 'npm i @yukikaze-bot/erlpack'",
);
}
if (!["json", "etf"].includes(socket.encoding))
return socket.close(CLOSECODES.Decode_error);
}
if (socket.encoding === "etf" && erlpack)
throw new Error("Erlpack is not installed: 'npm i erlpack'");
socket.version = Number(searchParams.get("version")) || 8;
if (socket.version != 8)
+6 -6
View File
@@ -20,18 +20,18 @@ import { WebSocket, Payload, CLOSECODES, OPCODES } from "@fosscord/gateway";
import OPCodeHandlers from "../opcodes";
import { check } from "../opcodes/instanceOf";
import WS from "ws";
import { PayloadSchema } from "@fosscord/util";
import { PayloadSchema, ErlpackType } from "@fosscord/util";
import * as Sentry from "@sentry/node";
import BigIntJson from "json-bigint";
import path from "path";
import fs from "fs/promises";
const bigIntJson = BigIntJson({ storeAsString: true });
let erlpack: { unpack: (buffer: Buffer) => Payload };
let erlpack: ErlpackType | null = null;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {
/* empty */
erlpack = require("erlpack") as ErlpackType;
} catch (e) {
// empty
}
export async function Message(this: WebSocket, buffer: WS.Data) {
@@ -52,7 +52,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
}
}
data = bigIntJson.parse(buffer as string);
} else if (this.encoding === "etf" && buffer instanceof Buffer) {
} else if (this.encoding === "etf" && buffer instanceof Buffer && erlpack) {
try {
data = erlpack.unpack(buffer);
} catch {