Make all handlers async
This commit is contained in:
parent
f314cfd17e
commit
232922ce25
1
public
1
public
|
@ -1 +0,0 @@
|
|||
Subproject commit ccc02f653a3eebeb2d64f894b978cc78a5c62c42
|
|
@ -393,9 +393,11 @@ declare interface ClientEvents {
|
|||
};
|
||||
}
|
||||
|
||||
declare type ServerEventCallback<EventID extends keyof ServerEvents> = (msg: ServerEvents[EventID], socket: Socket) => Promise<void>;
|
||||
|
||||
declare type ServerEventListener<EventID extends keyof ServerEvents> = {
|
||||
id: EventID;
|
||||
callback: (msg: ServerEvents[EventID], socket: Socket) => void;
|
||||
callback: ServerEventCallback<EventID>;
|
||||
};
|
||||
|
||||
declare type Vector2<T = number> = {
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const plus_ls: ServerEventListener<"+ls"> = {
|
||||
id: "+ls",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Give us the latest news on literally everything
|
||||
// that's happening in the server. In fact, I want
|
||||
// to know when someone clicks a button instantly,
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const minus_ls: ServerEventListener<"-ls"> = {
|
||||
id: "-ls",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Stop giving us the latest server forecast
|
||||
if (socket.rateLimits) {
|
||||
if (!socket.rateLimits.normal["-ls"].attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const a: ServerEventListener<"a"> = {
|
||||
id: "a",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Chat message
|
||||
const flags = socket.getUserFlags();
|
||||
if (!flags) return;
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { Logger } from "../../../../util/Logger";
|
||||
import env from "../../../../util/env";
|
||||
import { ServerEventListener } from "../../../../util/types";
|
||||
|
||||
export const admin_message: ServerEventListener<"admin message"> = {
|
||||
id: "admin message",
|
||||
callback: (msg, socket) => {
|
||||
// Administrator control message
|
||||
callback: async (msg, socket) => {
|
||||
// Administrator control message (Brandonism)
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.normal["admin message"].attempt()) return;
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const bye: ServerEventListener<"bye"> = {
|
||||
id: "bye",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Leave server
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.normal.bye.attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const ch: ServerEventListener<"ch"> = {
|
||||
id: "ch",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Switch channel
|
||||
if (!socket.rateLimits?.normal.ch.attempt()) return;
|
||||
if (typeof msg._id !== "string") return;
|
||||
|
|
|
@ -5,7 +5,7 @@ const logger = new Logger("chown");
|
|||
|
||||
export const chown: ServerEventListener<"chown"> = {
|
||||
id: "chown",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Change channel ownership
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.normal["chown"].attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const chset: ServerEventListener<"chset"> = {
|
||||
id: "chset",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Change channel settings
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.chains.chset.attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const devices: ServerEventListener<"devices"> = {
|
||||
id: "devices",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// List of MIDI Devices (or software ports, because nobody can tell the difference)
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.normal.devices.attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const kickban: ServerEventListener<"kickban"> = {
|
||||
id: "kickban",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Kickbanning someone from channel
|
||||
if (typeof msg._id !== "string") return;
|
||||
if (typeof msg.ms !== "number") return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const m: ServerEventListener<"m"> = {
|
||||
id: "m",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Cursor movement
|
||||
if (socket.rateLimits) {
|
||||
if (!socket.rateLimits.normal.m.attempt()) return;
|
||||
|
|
|
@ -3,7 +3,7 @@ import { config } from "../../../usersConfig";
|
|||
|
||||
export const n: ServerEventListener<"n"> = {
|
||||
id: "n",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Piano note
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.chains.n.attempt()) return;
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const t: ServerEventListener<"t"> = {
|
||||
id: "t",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Ping
|
||||
|
||||
if (socket.rateLimits)
|
||||
|
|
|
@ -2,12 +2,12 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const unban: ServerEventListener<"unban"> = {
|
||||
id: "unban",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Kickbanning someone from channel
|
||||
if (typeof msg._id !== "string") return;
|
||||
|
||||
if (socket.rateLimits)
|
||||
if (!socket.rateLimits.normal.unban.attempt()) return;
|
||||
if (!socket.rateLimits.normal.kickban.attempt()) return;
|
||||
|
||||
socket.unban(msg._id);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
|
|||
|
||||
export const userset: ServerEventListener<"userset"> = {
|
||||
id: "userset",
|
||||
callback: (msg, socket) => {
|
||||
callback: async (msg, socket) => {
|
||||
// Change username/color
|
||||
if (!socket.rateLimits?.chains.userset.attempt()) return;
|
||||
// You can disable color in the config because
|
||||
|
|
243
top
243
top
|
@ -1,243 +0,0 @@
|
|||
[1mdiff --git a/src/channel/Channel.ts b/src/channel/Channel.ts[m
|
||||
[1mindex e33ddc6..b6072be 100755[m
|
||||
[1m--- a/src/channel/Channel.ts[m
|
||||
[1m+++ b/src/channel/Channel.ts[m
|
||||
[36m@@ -17,7 +17,7 @@[m [mimport Crown from "./Crown";[m
|
||||
import { ChannelList } from "./ChannelList";[m
|
||||
import { config } from "./config";[m
|
||||
import { config as usersConfig } from "../ws/usersConfig";[m
|
||||
import { saveChatHistory, [31m[-getChatHistory-][m[32m{+getChatHistory, deleteChatHistory+}[m } from "../data/history";
|
||||
import { mixin, darken } from "../util/helpers";[m
|
||||
import { User } from "@prisma/client";[m
|
||||
import { heapStats } from "bun:jsc";[m
|
||||
[36m@@ -42,7 +42,18 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
public chatHistory = new Array<ClientEvents["a"]>();[m
|
||||
|
||||
private async loadChatHistory() {[m
|
||||
[32m{+try {+}[m
|
||||
this.chatHistory = await getChatHistory(this.getID());
|
||||
|
||||
[32m{+this.sendArray([{+}[m
|
||||
[32m{+ m: "c",+}[m
|
||||
[32m{+ c: this.chatHistory+}[m
|
||||
[32m{+ }]);+}[m
|
||||
[32m{+ } catch (err) { }+}[m
|
||||
[32m{+ }+}[m
|
||||
|
||||
[32m{+ private async deleteChatHistory() {+}[m
|
||||
[32m{+ await deleteChatHistory(this.getID());+}[m
|
||||
}[m
|
||||
|
||||
public logger: Logger;[m
|
||||
[36m@@ -97,10 +108,8 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
// ...and, possibly, an owner, too[m
|
||||
if (creator) {[m
|
||||
[31m[- // if (this.crown.canBeSetBy(creator)) {-][m
|
||||
const part = creator.getParticipant();[m
|
||||
if (part) [31m[-this.giveCrown(part);-][m
|
||||
[31m[- // }-][m[32m{+this.giveCrown(part, true, false);+}[m
|
||||
}[m
|
||||
} else {[m
|
||||
this.settings = config.lobbySettings;[m
|
||||
[36m@@ -129,14 +138,13 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
this.loadChatHistory();[m
|
||||
this.logger.info("Loaded chat history");[m
|
||||
|
||||
this.on("update", [31m[-()-][m[32m{+(self, uuid)+}[m => {[31m[-//this.logger.debug("-------- UPDATE START --------");-][m
|
||||
// Send updated info[m
|
||||
for (const socket of socketsBySocketID.values()) {[m
|
||||
for (const p of this.ppl) {[m
|
||||
[31m[-//if (socket.getParticipantID() == p.id) {-][m[32m{+const socketUUID = socket.getUUID();+}[m
|
||||
|
||||
if [31m[-(p.uuids.includes(socket.getUUID()))-][m[32m{+(p.uuids.includes(socketUUID) && socketUUID !== uuid)+}[m {[31m[-//this.logger.debug("sending to", socket.getUUID())-][m
|
||||
socket.sendChannelUpdate([m
|
||||
this.getInfo(),[m
|
||||
this.getParticipantList()[m
|
||||
[36m@@ -276,6 +284,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
);[m
|
||||
}[m
|
||||
|
||||
[32m{+this.logger.debug("Update from user data update handler");+}[m
|
||||
this.emit("update", this);[m
|
||||
} catch (err) {[m
|
||||
this.logger.error(err);[m
|
||||
[36m@@ -328,6 +337,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
// probably causes jank, but people can just reload their page or whatever[m
|
||||
// not sure what to do about the URL situation[m
|
||||
this._id = _id;[m
|
||||
[32m{+this.logger.debug("Update from setID");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
|
||||
[36m@@ -407,6 +417,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
*/[m
|
||||
|
||||
[32m{+this.logger.debug("Update from changeSettings");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
|
||||
[36m@@ -486,7 +497,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
p.uuids.push(socket.getUUID())[m
|
||||
}[m
|
||||
|
||||
[31m[-socket.sendChannelUpdate(this.getInfo(),-][m[32m{+//socket.sendChannelUpdate(this.getInfo(),+}[m this.getParticipantList());
|
||||
} else {[m
|
||||
// Add them to the channel[m
|
||||
hasChangedChannel = true;[m
|
||||
[36m@@ -534,15 +545,13 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
if (p) {[m
|
||||
// Give the crown back[m
|
||||
this.giveCrown(p, [31m[-true);-][m[32m{+true, false);+}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[31m[- // Send our state data back-][m
|
||||
[31m[- // TODO Does this go above?-][m
|
||||
socket.sendArray([[m
|
||||
// {[m
|
||||
// m: "ch",[m
|
||||
[36m@@ -578,7 +587,9 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
part.id[m
|
||||
);[m
|
||||
|
||||
// Broadcast a channel update so everyone subscribed to the channel list can see [31m[-us-][m[32m{+the new user count+}[m
|
||||
[32m{+ //this.emit("update", this, socket.getUUID());+}[m
|
||||
[32m{+ this.logger.debug("Update from join");+}[m
|
||||
this.emit("update", this);[m
|
||||
|
||||
//this.logger.debug("Settings:", this.settings);[m
|
||||
[36m@@ -625,6 +636,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
]);[m
|
||||
|
||||
[32m{+this.logger.debug("Update from leave");+}[m
|
||||
this.emit("update", this);[m
|
||||
} else {[m
|
||||
for (const p of this.ppl) {[m
|
||||
[36m@@ -789,6 +801,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
|
||||
ChannelList.remove(this);[m
|
||||
[32m{+this.deleteChatHistory();+}[m
|
||||
this.logger.info("Destroyed");[m
|
||||
}[m
|
||||
|
||||
[36m@@ -819,7 +832,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
* @param part Participant to give crown to[m
|
||||
* @param force Whether or not to force-create a crown (useful for lobbies)[m
|
||||
*/[m
|
||||
public giveCrown(part: Participant, force = [31m[-false)-][m[32m{+false, update = true)+}[m {
|
||||
if (force) {[m
|
||||
if (!this.crown) this.crown = new Crown();[m
|
||||
}[m
|
||||
[36m@@ -828,7 +841,11 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
this.crown.userId = part._id;[m
|
||||
this.crown.participantId = part.id;[m
|
||||
this.crown.time = Date.now();[m
|
||||
|
||||
[32m{+if (update) {+}[m
|
||||
[32m{+ this.logger.debug("Update from giveCrown");+}[m
|
||||
this.emit("update", this);
|
||||
[32m{+}+}[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[36m@@ -865,6 +882,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
delete this.crown.participantId;[m
|
||||
|
||||
[32m{+this.logger.debug("Update from dropCrown");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
}[m
|
||||
[36m@@ -951,6 +969,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
|
||||
if (shouldUpdate) {[m
|
||||
[32m{+this.logger.debug("Update from kickban");+}[m
|
||||
this.emit("update", this);[m
|
||||
|
||||
if (typeof banner !== "undefined") {[m
|
||||
[36m@@ -1149,7 +1168,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
public printMemoryInChat() {[m
|
||||
const mem = heapStats();[m
|
||||
[31m[-this.sendChatAdmin(`Size:-][m[32m{+this.sendChatAdmin(`Used:+}[m ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / [31m[-Capacity:-][m[32m{+Allocated:+}[m ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[1mdiff --git a/src/data/history.ts b/src/data/history.ts[m
|
||||
[1mindex b769e27..0b68962 100755[m
|
||||
[1m--- a/src/data/history.ts[m
|
||||
[1m+++ b/src/data/history.ts[m
|
||||
[36m@@ -27,3 +27,7 @@[m [mexport async function getChatHistory(_id: string) {[m
|
||||
return [];[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[32m{+export async function deleteChatHistory(_id: string) {+}[m
|
||||
[32m{+ await prisma.chatHistory.delete({ where: { id: _id } });+}[m
|
||||
[32m{+}+}[m
|
||||
[1mdiff --git a/src/util/token.ts b/src/util/token.ts[m
|
||||
[1mindex 54d22af..4e7bf6f 100644[m
|
||||
[1m--- a/src/util/token.ts[m
|
||||
[1m+++ b/src/util/token.ts[m
|
||||
[36m@@ -50,7 +50,7 @@[m [mexport async function createToken(userID: string, gateway: Gateway) {[m
|
||||
let token = "";[m
|
||||
|
||||
if (config.tokenAuth == "uuid") {[m
|
||||
token = [32m{+userID + "." ++}[m crypto.randomUUID();
|
||||
} else if (config.tokenAuth == "jwt") {[m
|
||||
token = generateJWT(userID, gateway);[m
|
||||
}[m
|
||||
[1mdiff --git a/src/ws/events/user/handlers/hi.ts b/src/ws/events/user/handlers/hi.ts[m
|
||||
[1mindex 91067b1..964c2cb 100755[m
|
||||
[1m--- a/src/ws/events/user/handlers/hi.ts[m
|
||||
[1m+++ b/src/ws/events/user/handlers/hi.ts[m
|
||||
[36m@@ -32,28 +32,30 @@[m [mexport const hi: ServerEventListener<"hi"> = {[m
|
||||
let token: string | undefined;[m
|
||||
let generatedToken = false;[m
|
||||
|
||||
if [32m{+(config.tokenAuth !== "none") {+}[m
|
||||
[32m{+ if+}[m (typeof msg.token !== "string") {
|
||||
// Get a saved token
|
||||
token = await getToken(socket.getUserID());[31m[-if (typeof token !== "string") {-][m
|
||||
[31m[- // Generate a new one-][m
|
||||
[31m[- token = await createToken(socket.getUserID(), socket.gateway);-][m
|
||||
if (typeof token !== "string") {[m
|
||||
[32m{+// Generate a new one+}[m
|
||||
[32m{+ token = await createToken(socket.getUserID(), socket.gateway);+}[m
|
||||
|
||||
[32m{+ if (typeof token !== "string") {+}[m
|
||||
logger.warn(`Unable to generate token for user ${socket.getUserID()}`);
|
||||
} else {
|
||||
generatedToken = true;
|
||||
[32m{+}+}[m
|
||||
[32m{+ }+}[m
|
||||
[32m{+ } else {+}[m
|
||||
[32m{+ // Validate the token+}[m
|
||||
[32m{+ const valid = await validateToken(socket.getUserID(), msg.token);+}[m
|
||||
[32m{+ if (!valid) {+}[m
|
||||
[32m{+ socket.ban(60000, "Invalid token");+}[m
|
||||
[32m{+ return;+}[m
|
||||
}[m
|
||||
[31m[- }-][m
|
||||
[31m[- } else {-][m
|
||||
[31m[- // Validate the token-][m
|
||||
[31m[- const valid = await validateToken(socket.getUserID(), msg.token);-][m
|
||||
[31m[- if (!valid) {-][m
|
||||
[31m[- socket.ban(60000, "Invalid token");-][m
|
||||
[31m[- return;-][m
|
||||
[31m[- }-][m
|
||||
|
||||
token = msg.token;
|
||||
[32m{+}+}[m
|
||||
}[m
|
||||
|
||||
let part = socket.getParticipant();[m
|
Loading…
Reference in New Issue