summary refs log tree commit diff
path: root/src/gateway
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-21 11:56:56 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-21 12:01:49 +1100
commiteee98516dd09516f1c1cf891526339420807d50c (patch)
tree8d60f6717eac0416bcb73421ca71c31bdc1fff68 /src/gateway
parentClose #954 (diff)
downloadserver-eee98516dd09516f1c1cf891526339420807d50c.tar.xz
Fix gateway encoding Date objects as {} when using erlpack. Fixes NaN/NaN/NaN timestamps in desktop client
Diffstat (limited to 'src/gateway')
-rw-r--r--src/gateway/util/Send.ts26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts
index 38e5ab4a..e39554a4 100644
--- a/src/gateway/util/Send.ts
+++ b/src/gateway/util/Send.ts
@@ -20,7 +20,7 @@ import { Payload, WebSocket } from "@fosscord/gateway";
 import fs from "fs/promises";
 import path from "path";
 
-import type { ErlpackType } from "@fosscord/util";
+import { ErlpackType, JSONReplacer } from "@fosscord/util";
 let erlpack: ErlpackType | null = null;
 try {
 	erlpack = require("erlpack") as ErlpackType;
@@ -28,6 +28,21 @@ try {
 	// empty
 }
 
+// don't care
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+const recurseJsonReplace = (json: any) => {
+	for (const key in json) {
+		// eslint-disable-next-line no-prototype-builtins
+		if (!json.hasOwnProperty(key)) continue;
+
+		json[key] = JSONReplacer.call(json, key, json[key]);
+
+		if (typeof json[key] == "object" && json[key] !== null)
+			json[key] = recurseJsonReplace(json[key]);
+	}
+	return json;
+};
+
 export function Send(socket: WebSocket, data: Payload) {
 	if (process.env.WS_VERBOSE)
 		console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
@@ -47,9 +62,14 @@ export function Send(socket: WebSocket, data: Payload) {
 	}
 
 	let buffer: Buffer | string;
-	if (socket.encoding === "etf" && erlpack) buffer = erlpack.pack(data);
+	if (socket.encoding === "etf" && erlpack) {
+		// Erlpack doesn't like Date objects, encodes them as {}
+		data = recurseJsonReplace(data);
+		buffer = erlpack.pack(data);
+	}
 	// TODO: encode circular object
-	else if (socket.encoding === "json") buffer = JSON.stringify(data);
+	else if (socket.encoding === "json")
+		buffer = JSON.stringify(data, JSONReplacer);
 	else return;
 	// TODO: compression
 	if (socket.deflate) {