summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-10-31 20:49:32 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-10-31 20:49:32 +1100
commit0d57ac8c369011e890c35ff6728d26c22d83f27d (patch)
treee84de086ef59d4e5334ba831554530068e9e5278
parentDisable webrtc preload plugin (diff)
downloadserver-0d57ac8c369011e890c35ff6728d26c22d83f27d.tar.xz
Rewrite gateway message decoding
-rw-r--r--src/gateway/events/Message.ts26
1 files changed, 15 insertions, 11 deletions
diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
index 204f0025..b112fba0 100644
--- a/src/gateway/events/Message.ts
+++ b/src/gateway/events/Message.ts
@@ -16,20 +16,24 @@ 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" && buffer instanceof Buffer && buffer[0] !== 123) {	// bad check for "{"
+	if (
+		(buffer instanceof Buffer && buffer[0] === 123) ||
+		(typeof buffer === "string")
+	) {
+		data = bigIntJson.parse(buffer.toString());
+	}
+	else if (this.encoding === "json" && buffer instanceof Buffer) {
 		if (this.inflate) {
-			try {
-				buffer = this.inflate.process(buffer) as any;
-			} catch {
-				buffer = buffer.toString() as any;
-			}
+			try { buffer = this.inflate.process(buffer) as any; }
+			catch { buffer = buffer.toString() as any; }
 		}
 		data = bigIntJson.parse(buffer as string);
-	} else if (typeof buffer == "string" || (buffer instanceof Buffer && buffer[0] == 123)) {
-		data = bigIntJson.parse(buffer as string);
-	} else return;
+	}
+	else if (this.encoding === "etf" && buffer instanceof Buffer) {
+		try { data = erlpack.unpack(buffer); }
+		catch {	return this.close(CLOSECODES.Decode_error);	}
+	}
+	else return this.close(CLOSECODES.Decode_error);
 
 	check.call(this, PayloadSchema, data);