Make all handlers async

This commit is contained in:
Hri7566 2024-07-31 20:08:01 -04:00
parent f314cfd17e
commit 232922ce25
18 changed files with 21 additions and 262 deletions

1
public

@ -1 +0,0 @@
Subproject commit ccc02f653a3eebeb2d64f894b978cc78a5c62c42

4
src/util/types.d.ts vendored
View File

@ -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> = { declare type ServerEventListener<EventID extends keyof ServerEvents> = {
id: EventID; id: EventID;
callback: (msg: ServerEvents[EventID], socket: Socket) => void; callback: ServerEventCallback<EventID>;
}; };
declare type Vector2<T = number> = { declare type Vector2<T = number> = {

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const plus_ls: ServerEventListener<"+ls"> = { export const plus_ls: ServerEventListener<"+ls"> = {
id: "+ls", id: "+ls",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Give us the latest news on literally everything // Give us the latest news on literally everything
// that's happening in the server. In fact, I want // that's happening in the server. In fact, I want
// to know when someone clicks a button instantly, // to know when someone clicks a button instantly,

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const minus_ls: ServerEventListener<"-ls"> = { export const minus_ls: ServerEventListener<"-ls"> = {
id: "-ls", id: "-ls",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Stop giving us the latest server forecast // Stop giving us the latest server forecast
if (socket.rateLimits) { if (socket.rateLimits) {
if (!socket.rateLimits.normal["-ls"].attempt()) return; if (!socket.rateLimits.normal["-ls"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const a: ServerEventListener<"a"> = { export const a: ServerEventListener<"a"> = {
id: "a", id: "a",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Chat message // Chat message
const flags = socket.getUserFlags(); const flags = socket.getUserFlags();
if (!flags) return; if (!flags) return;

View File

@ -1,10 +1,11 @@
import { Logger } from "../../../../util/Logger";
import env from "../../../../util/env"; import env from "../../../../util/env";
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "../../../../util/types";
export const admin_message: ServerEventListener<"admin message"> = { export const admin_message: ServerEventListener<"admin message"> = {
id: "admin message", id: "admin message",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Administrator control message // Administrator control message (Brandonism)
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.normal["admin message"].attempt()) return; if (!socket.rateLimits.normal["admin message"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const bye: ServerEventListener<"bye"> = { export const bye: ServerEventListener<"bye"> = {
id: "bye", id: "bye",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Leave server // Leave server
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.normal.bye.attempt()) return; if (!socket.rateLimits.normal.bye.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const ch: ServerEventListener<"ch"> = { export const ch: ServerEventListener<"ch"> = {
id: "ch", id: "ch",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Switch channel // Switch channel
if (!socket.rateLimits?.normal.ch.attempt()) return; if (!socket.rateLimits?.normal.ch.attempt()) return;
if (typeof msg._id !== "string") return; if (typeof msg._id !== "string") return;

View File

@ -5,7 +5,7 @@ const logger = new Logger("chown");
export const chown: ServerEventListener<"chown"> = { export const chown: ServerEventListener<"chown"> = {
id: "chown", id: "chown",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Change channel ownership // Change channel ownership
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.normal["chown"].attempt()) return; if (!socket.rateLimits.normal["chown"].attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const chset: ServerEventListener<"chset"> = { export const chset: ServerEventListener<"chset"> = {
id: "chset", id: "chset",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Change channel settings // Change channel settings
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.chains.chset.attempt()) return; if (!socket.rateLimits.chains.chset.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const devices: ServerEventListener<"devices"> = { export const devices: ServerEventListener<"devices"> = {
id: "devices", id: "devices",
callback: (msg, socket) => { callback: async (msg, socket) => {
// List of MIDI Devices (or software ports, because nobody can tell the difference) // List of MIDI Devices (or software ports, because nobody can tell the difference)
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.normal.devices.attempt()) return; if (!socket.rateLimits.normal.devices.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const kickban: ServerEventListener<"kickban"> = { export const kickban: ServerEventListener<"kickban"> = {
id: "kickban", id: "kickban",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Kickbanning someone from channel // Kickbanning someone from channel
if (typeof msg._id !== "string") return; if (typeof msg._id !== "string") return;
if (typeof msg.ms !== "number") return; if (typeof msg.ms !== "number") return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const m: ServerEventListener<"m"> = { export const m: ServerEventListener<"m"> = {
id: "m", id: "m",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Cursor movement // Cursor movement
if (socket.rateLimits) { if (socket.rateLimits) {
if (!socket.rateLimits.normal.m.attempt()) return; if (!socket.rateLimits.normal.m.attempt()) return;

View File

@ -3,7 +3,7 @@ import { config } from "../../../usersConfig";
export const n: ServerEventListener<"n"> = { export const n: ServerEventListener<"n"> = {
id: "n", id: "n",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Piano note // Piano note
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.chains.n.attempt()) return; if (!socket.rateLimits.chains.n.attempt()) return;

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const t: ServerEventListener<"t"> = { export const t: ServerEventListener<"t"> = {
id: "t", id: "t",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Ping // Ping
if (socket.rateLimits) if (socket.rateLimits)

View File

@ -2,12 +2,12 @@ import { ServerEventListener } from "../../../../util/types";
export const unban: ServerEventListener<"unban"> = { export const unban: ServerEventListener<"unban"> = {
id: "unban", id: "unban",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Kickbanning someone from channel // Kickbanning someone from channel
if (typeof msg._id !== "string") return; if (typeof msg._id !== "string") return;
if (socket.rateLimits) if (socket.rateLimits)
if (!socket.rateLimits.normal.unban.attempt()) return; if (!socket.rateLimits.normal.kickban.attempt()) return;
socket.unban(msg._id); socket.unban(msg._id);
} }

View File

@ -2,7 +2,7 @@ import { ServerEventListener } from "../../../../util/types";
export const userset: ServerEventListener<"userset"> = { export const userset: ServerEventListener<"userset"> = {
id: "userset", id: "userset",
callback: (msg, socket) => { callback: async (msg, socket) => {
// Change username/color // Change username/color
if (!socket.rateLimits?.chains.userset.attempt()) return; if (!socket.rateLimits?.chains.userset.attempt()) return;
// You can disable color in the config because // You can disable color in the config because

243
top
View File

@ -1,243 +0,0 @@
diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts
index e33ddc6..b6072be 100755
--- a/src/channel/Channel.ts
+++ b/src/channel/Channel.ts
@@ -17,7 +17,7 @@ import Crown from "./Crown";
import { ChannelList } from "./ChannelList";
import { config } from "./config";
import { config as usersConfig } from "../ws/usersConfig";
import { saveChatHistory, [-getChatHistory-]{+getChatHistory, deleteChatHistory+} } from "../data/history";
import { mixin, darken } from "../util/helpers";
import { User } from "@prisma/client";
import { heapStats } from "bun:jsc";
@@ -42,7 +42,18 @@ export class Channel extends EventEmitter {
public chatHistory = new Array<ClientEvents["a"]>();
private async loadChatHistory() {
{+try {+}
this.chatHistory = await getChatHistory(this.getID());
{+this.sendArray([{+}
{+ m: "c",+}
{+ c: this.chatHistory+}
{+ }]);+}
{+ } catch (err) { }+}
{+ }+}
{+ private async deleteChatHistory() {+}
{+ await deleteChatHistory(this.getID());+}
}
public logger: Logger;
@@ -97,10 +108,8 @@ export class Channel extends EventEmitter {
// ...and, possibly, an owner, too
if (creator) {
[- // if (this.crown.canBeSetBy(creator)) {-]
const part = creator.getParticipant();
if (part) [-this.giveCrown(part);-]
[- // }-]{+this.giveCrown(part, true, false);+}
}
} else {
this.settings = config.lobbySettings;
@@ -129,14 +138,13 @@ export class Channel extends EventEmitter {
this.loadChatHistory();
this.logger.info("Loaded chat history");
this.on("update", [-()-]{+(self, uuid)+} => {[-//this.logger.debug("-------- UPDATE START --------");-]
// Send updated info
for (const socket of socketsBySocketID.values()) {
for (const p of this.ppl) {
[-//if (socket.getParticipantID() == p.id) {-]{+const socketUUID = socket.getUUID();+}
if [-(p.uuids.includes(socket.getUUID()))-]{+(p.uuids.includes(socketUUID) && socketUUID !== uuid)+} {[-//this.logger.debug("sending to", socket.getUUID())-]
socket.sendChannelUpdate(
this.getInfo(),
this.getParticipantList()
@@ -276,6 +284,7 @@ export class Channel extends EventEmitter {
);
}
{+this.logger.debug("Update from user data update handler");+}
this.emit("update", this);
} catch (err) {
this.logger.error(err);
@@ -328,6 +337,7 @@ export class Channel extends EventEmitter {
// probably causes jank, but people can just reload their page or whatever
// not sure what to do about the URL situation
this._id = _id;
{+this.logger.debug("Update from setID");+}
this.emit("update", this);
}
@@ -407,6 +417,7 @@ export class Channel extends EventEmitter {
}
*/
{+this.logger.debug("Update from changeSettings");+}
this.emit("update", this);
}
@@ -486,7 +497,7 @@ export class Channel extends EventEmitter {
p.uuids.push(socket.getUUID())
}
[-socket.sendChannelUpdate(this.getInfo(),-]{+//socket.sendChannelUpdate(this.getInfo(),+} this.getParticipantList());
} else {
// Add them to the channel
hasChangedChannel = true;
@@ -534,15 +545,13 @@ export class Channel extends EventEmitter {
if (p) {
// Give the crown back
this.giveCrown(p, [-true);-]{+true, false);+}
}
}
}
}
}
[- // Send our state data back-]
[- // TODO Does this go above?-]
socket.sendArray([
// {
// m: "ch",
@@ -578,7 +587,9 @@ export class Channel extends EventEmitter {
part.id
);
// Broadcast a channel update so everyone subscribed to the channel list can see [-us-]{+the new user count+}
{+ //this.emit("update", this, socket.getUUID());+}
{+ this.logger.debug("Update from join");+}
this.emit("update", this);
//this.logger.debug("Settings:", this.settings);
@@ -625,6 +636,7 @@ export class Channel extends EventEmitter {
}
]);
{+this.logger.debug("Update from leave");+}
this.emit("update", this);
} else {
for (const p of this.ppl) {
@@ -789,6 +801,7 @@ export class Channel extends EventEmitter {
}
ChannelList.remove(this);
{+this.deleteChatHistory();+}
this.logger.info("Destroyed");
}
@@ -819,7 +832,7 @@ export class Channel extends EventEmitter {
* @param part Participant to give crown to
* @param force Whether or not to force-create a crown (useful for lobbies)
*/
public giveCrown(part: Participant, force = [-false)-]{+false, update = true)+} {
if (force) {
if (!this.crown) this.crown = new Crown();
}
@@ -828,7 +841,11 @@ export class Channel extends EventEmitter {
this.crown.userId = part._id;
this.crown.participantId = part.id;
this.crown.time = Date.now();
{+if (update) {+}
{+ this.logger.debug("Update from giveCrown");+}
this.emit("update", this);
{+}+}
}
}
@@ -865,6 +882,7 @@ export class Channel extends EventEmitter {
delete this.crown.participantId;
{+this.logger.debug("Update from dropCrown");+}
this.emit("update", this);
}
}
@@ -951,6 +969,7 @@ export class Channel extends EventEmitter {
}
if (shouldUpdate) {
{+this.logger.debug("Update from kickban");+}
this.emit("update", this);
if (typeof banner !== "undefined") {
@@ -1149,7 +1168,7 @@ export class Channel extends EventEmitter {
public printMemoryInChat() {
const mem = heapStats();
[-this.sendChatAdmin(`Size:-]{+this.sendChatAdmin(`Used:+} ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / [-Capacity:-]{+Allocated:+} ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
}
}
diff --git a/src/data/history.ts b/src/data/history.ts
index b769e27..0b68962 100755
--- a/src/data/history.ts
+++ b/src/data/history.ts
@@ -27,3 +27,7 @@ export async function getChatHistory(_id: string) {
return [];
}
}
{+export async function deleteChatHistory(_id: string) {+}
{+ await prisma.chatHistory.delete({ where: { id: _id } });+}
{+}+}
diff --git a/src/util/token.ts b/src/util/token.ts
index 54d22af..4e7bf6f 100644
--- a/src/util/token.ts
+++ b/src/util/token.ts
@@ -50,7 +50,7 @@ export async function createToken(userID: string, gateway: Gateway) {
let token = "";
if (config.tokenAuth == "uuid") {
token = {+userID + "." ++} crypto.randomUUID();
} else if (config.tokenAuth == "jwt") {
token = generateJWT(userID, gateway);
}
diff --git a/src/ws/events/user/handlers/hi.ts b/src/ws/events/user/handlers/hi.ts
index 91067b1..964c2cb 100755
--- a/src/ws/events/user/handlers/hi.ts
+++ b/src/ws/events/user/handlers/hi.ts
@@ -32,28 +32,30 @@ export const hi: ServerEventListener<"hi"> = {
let token: string | undefined;
let generatedToken = false;
if {+(config.tokenAuth !== "none") {+}
{+ if+} (typeof msg.token !== "string") {
// Get a saved token
token = await getToken(socket.getUserID());[-if (typeof token !== "string") {-]
[- // Generate a new one-]
[- token = await createToken(socket.getUserID(), socket.gateway);-]
if (typeof token !== "string") {
{+// Generate a new one+}
{+ token = await createToken(socket.getUserID(), socket.gateway);+}
{+ if (typeof token !== "string") {+}
logger.warn(`Unable to generate token for user ${socket.getUserID()}`);
} else {
generatedToken = true;
{+}+}
{+ }+}
{+ } else {+}
{+ // Validate the token+}
{+ const valid = await validateToken(socket.getUserID(), msg.token);+}
{+ if (!valid) {+}
{+ socket.ban(60000, "Invalid token");+}
{+ return;+}
}
[- }-]
[- } else {-]
[- // Validate the token-]
[- const valid = await validateToken(socket.getUserID(), msg.token);-]
[- if (!valid) {-]
[- socket.ban(60000, "Invalid token");-]
[- return;-]
[- }-]
token = msg.token;
{+}+}
}
let part = socket.getParticipant();