summary refs log tree commit diff
path: root/src/gateway/events/Message.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/gateway/events/Message.ts')
-rw-r--r--src/gateway/events/Message.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
new file mode 100644
index 00000000..569f5fc7
--- /dev/null
+++ b/src/gateway/events/Message.ts
@@ -0,0 +1,61 @@
+import { CLOSECODES } from "../util/Constants";
+import { WebSocket, Payload } from "@fosscord/gateway";
+let erlpack: any;
+try {
+	erlpack = require("@yukikaze-bot/erlpack");
+} catch (error) {}
+import OPCodeHandlers from "../opcodes";
+import { check } from "../opcodes/instanceOf";
+
+const PayloadSchema = {
+	op: Number,
+	$d: Object || Number, // or number for heartbeat sequence
+	$s: Number,
+	$t: String,
+};
+
+export async function Message(this: WebSocket, buffer: Buffer) {
+	// TODO: compression
+	let data: Payload;
+
+	if (this.encoding === "etf" && buffer instanceof Buffer)
+		data = erlpack.unpack(buffer);
+	else if (this.encoding === "json")
+		data = JSON.parse(buffer as unknown as string); //TODO: is this even correct?? seems to work for web clients...
+	else if(/--debug|--inspect/.test(process.execArgv.join(' '))) {
+		debugger;
+		return;
+	}
+	else {
+		console.log("Invalid gateway connection! Use a debugger to inspect!");
+		return;
+	}
+
+	if(process.env.WS_VERBOSE)
+		console.log(`[Websocket] Incomming message: ${JSON.stringify(data)}`);
+	if(data.op !== 1)
+		check.call(this, PayloadSchema, data);
+	else { //custom validation for numbers, because heartbeat
+		if(data.s || data.t || (typeof data.d !== "number" && data.d)) {
+			console.log("Invalid heartbeat...");
+			this.close(CLOSECODES.Decode_error);
+		}
+	}
+
+	// @ts-ignore
+	const OPCodeHandler = OPCodeHandlers[data.op];
+	if (!OPCodeHandler) {
+		console.error("[Gateway] Unkown opcode " + data.op);
+		// TODO: if all opcodes are implemented comment this out:
+		// this.close(CLOSECODES.Unknown_opcode);
+		return;
+	}
+
+	try {
+		return await OPCodeHandler.call(this, data);
+	} catch (error) {
+		console.error(error);
+		if (!this.CLOSED && this.CLOSING)
+			return this.close(CLOSECODES.Unknown_error);
+	}
+}