Use erlpack instead of @yukikaze-bot/erlpack (#968)
This commit is contained in:
parent
72c0319a9f
commit
da20866967
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -99,20 +99,20 @@
|
||||
"probe-image-size": "^7.2.3",
|
||||
"proxy-agent": "^5.0.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"sqlite3": "^5.1.4",
|
||||
"ts-node": "^10.9.1",
|
||||
"tslib": "^2.4.1",
|
||||
"typeorm": "^0.3.10",
|
||||
"typescript-json-schema": "^0.50.1",
|
||||
"ws": "^8.9.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@yukikaze-bot/erlpack": "^1.0.1"
|
||||
},
|
||||
"_moduleAliases": {
|
||||
"@fosscord/api": "dist/api",
|
||||
"@fosscord/cdn": "dist/cdn",
|
||||
"@fosscord/gateway": "dist/gateway",
|
||||
"@fosscord/util": "dist/util"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"erlpack": "^0.1.4",
|
||||
"sqlite3": "^5.1.4"
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -16,18 +16,18 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
let erlpack: { pack: (data: Payload) => Buffer };
|
||||
try {
|
||||
erlpack = require("@yukikaze-bot/erlpack");
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Missing @yukikaze-bot/erlpack, electron-based desktop clients designed for discord.com will not be able to connect!",
|
||||
);
|
||||
}
|
||||
import { Payload, WebSocket } from "@fosscord/gateway";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
import type { ErlpackType } from "@fosscord/util";
|
||||
let erlpack: ErlpackType | null = null;
|
||||
try {
|
||||
erlpack = require("erlpack") as ErlpackType;
|
||||
} catch (e) {
|
||||
// empty
|
||||
}
|
||||
|
||||
export function Send(socket: WebSocket, data: Payload) {
|
||||
if (process.env.WS_VERBOSE)
|
||||
console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
|
||||
@ -47,7 +47,7 @@ export function Send(socket: WebSocket, data: Payload) {
|
||||
}
|
||||
|
||||
let buffer: Buffer | string;
|
||||
if (socket.encoding === "etf") buffer = erlpack.pack(data);
|
||||
if (socket.encoding === "etf" && erlpack) buffer = erlpack.pack(data);
|
||||
// TODO: encode circular object
|
||||
else if (socket.encoding === "json") buffer = JSON.stringify(data);
|
||||
else return;
|
||||
|
||||
12
src/util/imports/Erlpack.ts
Normal file
12
src/util/imports/Erlpack.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
https://github.com/discord/erlpack/blob/master/js/index.d.ts
|
||||
MIT License
|
||||
Copyright (c) 2017 Discord
|
||||
*/
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// @fc-license-skip
|
||||
|
||||
export type ErlpackType = {
|
||||
pack: (data: any) => Buffer;
|
||||
unpack: <T = any>(data: Buffer) => T;
|
||||
};
|
||||
@ -17,3 +17,4 @@
|
||||
*/
|
||||
|
||||
export * from "./OrmUtils";
|
||||
export * from "./Erlpack";
|
||||
|
||||
@ -23,10 +23,6 @@ import WS from "ws";
|
||||
import { VoiceOPCodes } from "../util";
|
||||
import { onClose } from "./Close";
|
||||
import { onMessage } from "./Message";
|
||||
var erlpack: any;
|
||||
try {
|
||||
erlpack = require("@yukikaze-bot/erlpack");
|
||||
} catch (error) {}
|
||||
|
||||
// TODO: check rate limit
|
||||
// TODO: specify rate limit in config
|
||||
|
||||
Reference in New Issue
Block a user