🎨 remove long relatives paths -> short module paths

This commit is contained in:
Flam3rboy
2021-09-12 21:09:29 +02:00
parent 7eea37d184
commit 60535f5159
79 changed files with 1633 additions and 261 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import WebSocket from "../util/WebSocket";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import { Message } from "./Message";
import { Session } from "@fosscord/util";
+15 -8
View File
@@ -1,10 +1,10 @@
import WebSocket, { Server } from "../util/WebSocket";
import WebSocket, { Server } from "@fosscord/gateway/util/WebSocket";
import { IncomingMessage } from "http";
import { Close } from "./Close";
import { Message } from "./Message";
import { setHeartbeat } from "../util/setHeartbeat";
import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants";
import { setHeartbeat } from "@fosscord/gateway/util/setHeartbeat";
import { Send } from "@fosscord/gateway/util/Send";
import { CLOSECODES, OPCODES } from "@fosscord/gateway/util/Constants";
import { createDeflate } from "zlib";
import { URL } from "url";
import { Session } from "@fosscord/util";
@@ -17,7 +17,11 @@ try {
// TODO: specify rate limit in config
// TODO: check msg max size
export async function Connection(this: Server, socket: WebSocket, request: IncomingMessage) {
export async function Connection(
this: Server,
socket: WebSocket,
request: IncomingMessage
) {
try {
socket.on("close", Close);
// @ts-ignore
@@ -27,18 +31,21 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom
// @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 -D erlpack'");
if (socket.encoding === "etf" && erlpack)
throw new Error("Erlpack is not installed: 'npm i -D erlpack'");
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);
if (socket.version != 8)
return socket.close(CLOSECODES.Invalid_API_version);
// @ts-ignore
socket.compress = searchParams.get("compress") || "";
if (socket.compress) {
if (socket.compress !== "zlib-stream") return socket.close(CLOSECODES.Decode_error);
if (socket.compress !== "zlib-stream")
return socket.close(CLOSECODES.Decode_error);
socket.deflate = createDeflate({ chunkSize: 65535 });
socket.deflate.on("data", (chunk) => socket.send(chunk));
}
+8 -5
View File
@@ -1,10 +1,10 @@
import WebSocket, { Data } from "../util/WebSocket";
import WebSocket, { Data } from "@fosscord/gateway/util/WebSocket";
var erlpack: any;
try {
erlpack = require("erlpack");
} catch (error) {}
import OPCodeHandlers from "../opcodes";
import { Payload, CLOSECODES, OPCODES } from "../util/Constants";
import { Payload, CLOSECODES, OPCODES } from "@fosscord/gateway/util/Constants";
import { instanceOf, Tuple } from "lambert-server";
import { check } from "../opcodes/instanceOf";
import WS from "ws";
@@ -20,8 +20,10 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
// TODO: compression
var data: Payload;
if (this.encoding === "etf" && buffer instanceof Buffer) data = erlpack.unpack(buffer);
else if (this.encoding === "json" && typeof buffer === "string") data = JSON.parse(buffer);
if (this.encoding === "etf" && buffer instanceof Buffer)
data = erlpack.unpack(buffer);
else if (this.encoding === "json" && typeof buffer === "string")
data = JSON.parse(buffer);
else return;
check.call(this, PayloadSchema, data);
@@ -41,6 +43,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
return await OPCodeHandler.call(this, data);
} catch (error) {
console.error(error);
if (!this.CLOSED && this.CLOSING) return this.close(CLOSECODES.Unknown_error);
if (!this.CLOSED && this.CLOSING)
return this.close(CLOSECODES.Unknown_error);
}
}
+3
View File
@@ -1 +1,4 @@
export * from "./Server";
export * from "./util/";
export * from "./opcodes/";
export * from "./listener/listener";
+5 -5
View File
@@ -9,13 +9,13 @@ import {
ListenEventOpts,
Member,
} from "@fosscord/util";
import { OPCODES } from "../util/Constants";
import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket";
import { OPCODES } from "@fosscord/gateway/util/Constants";
import { Send } from "@fosscord/gateway/util/Send";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import "missing-native-js-functions";
import { Channel as AMQChannel } from "amqplib";
import { In, Like } from "../../../util/node_modules/typeorm";
import { Recipient } from "../../../util/dist/entities/Recipient";
import { In, Like } from "typeorm";
import { Recipient } from "@fosscord/util";
// TODO: close connection on Invalidated Token
// TODO: check intent
+4 -4
View File
@@ -1,7 +1,7 @@
import { CLOSECODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
import { setHeartbeat } from "../util/setHeartbeat";
import WebSocket from "../util/WebSocket";
import { CLOSECODES, Payload } from "@fosscord/gateway/util/Constants";
import { Send } from "@fosscord/gateway/util/Send";
import { setHeartbeat } from "@fosscord/gateway/util/setHeartbeat";
import WebSocket from "@fosscord/gateway/util/WebSocket";
export async function onHeartbeat(this: WebSocket, data: Payload) {
// TODO: validate payload
+5 -5
View File
@@ -1,5 +1,5 @@
import { CLOSECODES, Payload, OPCODES } from "../util/Constants";
import WebSocket from "../util/WebSocket";
import { CLOSECODES, Payload, OPCODES } from "@fosscord/gateway/util/Constants";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import {
Channel,
checkToken,
@@ -17,12 +17,12 @@ import {
} from "@fosscord/util";
import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify";
import { Send } from "../util/Send";
import { Send } from "@fosscord/gateway/util/Send";
// import experiments from "./experiments.json";
const experiments: any = [];
import { check } from "./instanceOf";
import { Recipient } from "../../../util/dist/entities/Recipient";
import { genSessionId } from "../util/SessionUtils";
import { Recipient } from "@fosscord/util";
import { genSessionId } from "@fosscord/gateway/util/SessionUtils";
// TODO: bot sharding
// TODO: check priviliged intents
+3 -3
View File
@@ -6,9 +6,9 @@ import {
Role,
} from "@fosscord/util";
import { LazyRequest } from "../schema/LazyRequest";
import { OPCODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket";
import { OPCODES, Payload } from "@fosscord/gateway/util/Constants";
import { Send } from "@fosscord/gateway/util/Send";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import { check } from "./instanceOf";
import "missing-native-js-functions";
+2 -2
View File
@@ -1,5 +1,5 @@
import { CLOSECODES, Payload } from "../util/Constants";
import WebSocket from "../util/WebSocket";
import { CLOSECODES, Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "@fosscord/gateway/util/WebSocket";
export function onPresenceUpdate(this: WebSocket, data: Payload) {
// return this.close(CLOSECODES.Unknown_error);
+2 -2
View File
@@ -1,6 +1,6 @@
import { CLOSECODES, Payload } from "../util/Constants";
import { CLOSECODES, Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "../util/WebSocket";
import WebSocket from "@fosscord/gateway/util/WebSocket";
export function onRequestGuildMembers(this: WebSocket, data: Payload) {
// return this.close(CLOSECODES.Unknown_error);
+3 -3
View File
@@ -1,7 +1,7 @@
import { CLOSECODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
import { CLOSECODES, Payload } from "@fosscord/gateway/util/Constants";
import { Send } from "@fosscord/gateway/util/Send";
import WebSocket from "../util/WebSocket";
import WebSocket from "@fosscord/gateway/util/WebSocket";
export async function onResume(this: WebSocket, data: Payload) {
console.log("Got Resume -> cancel not implemented");
+33 -13
View File
@@ -1,9 +1,18 @@
import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
import { Payload } from "../util/Constants";
import WebSocket from "../util/WebSocket";
import { Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import { check } from "./instanceOf";
import { Config, emitEvent, Guild, Member, Region, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util";
import { genVoiceToken } from "../util/SessionUtils";
import {
Config,
emitEvent,
Guild,
Member,
Region,
VoiceServerUpdateEvent,
VoiceState,
VoiceStateUpdateEvent,
} from "@fosscord/util";
import { genVoiceToken } from "@fosscord/gateway/util/SessionUtils";
// TODO: check if a voice server is setup
// Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not.
@@ -14,21 +23,27 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
let voiceState: VoiceState;
try {
voiceState = await VoiceState.findOneOrFail({
where: { user_id: this.user_id }
where: { user_id: this.user_id },
});
if (voiceState.session_id !== this.session_id && body.channel_id === null) {
if (
voiceState.session_id !== this.session_id &&
body.channel_id === null
) {
//Should we also check guild_id === null?
//changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored
return;
}
//If a user change voice channel between guild we should send a left event first
if (voiceState.guild_id !== body.guild_id && voiceState.session_id === this.session_id) {
if (
voiceState.guild_id !== body.guild_id &&
voiceState.session_id === this.session_id
) {
await emitEvent({
event: "VOICE_STATE_UPDATE",
data: { ...voiceState, channel_id: null },
guild_id: voiceState.guild_id,
})
});
}
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
@@ -50,10 +65,11 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
voiceState.member = await Member.findOneOrFail({
where: { id: voiceState.user_id, guild_id: voiceState.guild_id },
relations: ["user", "roles"],
})
});
//If the session changed we generate a new token
if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken();
if (voiceState.session_id !== this.session_id)
voiceState.token = genVoiceToken();
voiceState.session_id = this.session_id;
const { id, ...newObj } = voiceState;
@@ -69,13 +85,17 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//If it's null it means that we are leaving the channel and this event is not needed
if (voiceState.channel_id !== null) {
const guild = await Guild.findOne({ id: voiceState.guild_id })
const guild = await Guild.findOne({ id: voiceState.guild_id });
const regions = Config.get().regions;
let guildRegion: Region;
if (guild && guild.region) {
guildRegion = regions.available.filter(r => (r.id === guild.region))[0]
guildRegion = regions.available.filter(
(r) => r.id === guild.region
)[0];
} else {
guildRegion = regions.available.filter(r => (r.id === regions.default))[0]
guildRegion = regions.available.filter(
(r) => r.id === regions.default
)[0];
}
await emitEvent({
+2 -2
View File
@@ -1,5 +1,5 @@
import { Payload } from "../util/Constants";
import WebSocket from "../util/WebSocket";
import { Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "@fosscord/gateway/util/WebSocket";
import { onHeartbeat } from "./Heartbeat";
import { onIdentify } from "./Identify";
import { onLazyRequest } from "./LazyRequest";
+2 -2
View File
@@ -1,6 +1,6 @@
import { instanceOf } from "lambert-server";
import { CLOSECODES } from "../util/Constants";
import WebSocket from "../util/WebSocket";
import { CLOSECODES } from "@fosscord/gateway/util/Constants";
import WebSocket from "@fosscord/gateway/util/WebSocket";
export function check(this: WebSocket, schema: any, data: any) {
try {
+1 -1
View File
@@ -2,7 +2,7 @@ var erlpack: any;
try {
erlpack = require("erlpack");
} catch (error) {}
import { Payload } from "../util/Constants";
import { Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "./WebSocket";
+1 -3
View File
@@ -1,7 +1,6 @@
import { Intents, Permissions } from "@fosscord/util";
import WS, { Server, Data } from "ws";
import WS from "ws";
import { Deflate } from "zlib";
import { Channel } from "amqplib";
interface WebSocket extends WS {
version: number;
@@ -21,4 +20,3 @@ interface WebSocket extends WS {
}
export default WebSocket;
export { Server, Data };
+5
View File
@@ -0,0 +1,5 @@
export * from "./Constants";
export * from "./Send";
export * from "./SessionUtils";
export * from "./setHeartbeat";
export * from "./WebSocket";