summary refs log tree commit diff
path: root/gateway
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 15:33:06 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 15:33:06 +1000
commitdb76d5c8fc840f8a8a689bb14466f5c5ba902bc1 (patch)
tree116d1ace6a26d0205971928c7761c1b7efe095e4 /gateway
parentPrevent lazyrequest when sent with guild_id as number (diff)
downloadserver-db76d5c8fc840f8a8a689bb14466f5c5ba902bc1.tar.xz
LazyRequest on mobile + socket not closed fixes
Diffstat (limited to 'gateway')
-rw-r--r--gateway/package-lock.json78
-rw-r--r--gateway/package.json2
-rw-r--r--gateway/src/events/Message.ts11
-rw-r--r--gateway/src/listener/listener.ts2
-rw-r--r--gateway/src/opcodes/Identify.ts3
-rw-r--r--gateway/src/opcodes/LazyRequest.ts9
6 files changed, 85 insertions, 20 deletions
diff --git a/gateway/package-lock.json b/gateway/package-lock.json
index 38bdab90..e39901de 100644
--- a/gateway/package-lock.json
+++ b/gateway/package-lock.json
@@ -8,11 +8,13 @@
 			"name": "@fosscord/gateway",
 			"version": "1.0.0",
 			"hasInstallScript": true,
-			"license": "GPLV3",
+			"license": "AGPL-3.0-only",
 			"dependencies": {
 				"@fosscord/util": "file:../util",
 				"amqplib": "^0.8.0",
 				"dotenv": "^8.2.0",
+				"fast-zlib": "^2.0.1",
+				"json-bigint": "^1.0.0",
 				"jsonwebtoken": "^8.5.1",
 				"lambert-server": "^1.2.11",
 				"missing-native-js-functions": "^1.2.18",
@@ -23,8 +25,9 @@
 			},
 			"devDependencies": {
 				"@types/amqplib": "^0.8.1",
+				"@types/json-bigint": "^1.0.1",
 				"@types/jsonwebtoken": "^8.5.0",
-				"@types/node": "^14.17.9",
+				"@types/node": "^14.18.24",
 				"@types/node-fetch": "^2.5.12",
 				"@types/ws": "^7.4.0",
 				"@zerollup/ts-transform-paths": "^1.7.18",
@@ -40,7 +43,7 @@
 			"name": "@fosscord/util",
 			"version": "1.0.0",
 			"hasInstallScript": true,
-			"license": "GPLV3",
+			"license": "AGPL-3.0-only",
 			"dependencies": {
 				"amqplib": "^0.8.0",
 				"form-data": "^4.0.0",
@@ -54,7 +57,7 @@
 				"picocolors": "^1.0.0",
 				"proxy-agent": "^5.0.0",
 				"reflect-metadata": "^0.1.13",
-				"typeorm": "^0.2.38",
+				"typeorm": "^0.2.37",
 				"typescript": "^4.4.2",
 				"typescript-json-schema": "^0.50.1"
 			},
@@ -7164,6 +7167,12 @@
 			"integrity": "sha512-HBNx4lhkxN7bx6P0++W8E289foSu8kO8GCk2unhuVggO+cE7rh9DhZUyPhUxNRG9m+5B5BTKxZQ5ZP92x/mx9Q==",
 			"dev": true
 		},
+		"node_modules/@types/json-bigint": {
+			"version": "1.0.1",
+			"resolved": "https://registry.npmjs.org/@types/json-bigint/-/json-bigint-1.0.1.tgz",
+			"integrity": "sha512-zpchZLNsNuzJHi6v64UBoFWAvQlPhch7XAi36FkH6tL1bbbmimIF+cS7vwkzY4u5RaSWMoflQfu+TshMPPw8uw==",
+			"dev": true
+		},
 		"node_modules/@types/jsonwebtoken": {
 			"version": "8.5.5",
 			"integrity": "sha512-OGqtHQ7N5/Ap/TUwO6IgHDuLiAoTmHhGpNvgkCm/F4N6pKzx/RBSfr2OXZSwC6vkfnsEdb6+7DNZVtiXiwdwFw==",
@@ -7173,8 +7182,9 @@
 			}
 		},
 		"node_modules/@types/node": {
-			"version": "14.17.16",
-			"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ==",
+			"version": "14.18.25",
+			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.25.tgz",
+			"integrity": "sha512-9pLfceRSrKIsv/MISN6RoFWTIzka36Uk2Uuf5a8cHyDYhEgl5Hm5dXoe621KULeBjt+cFsY18mILsWWtJeG80w==",
 			"dev": true
 		},
 		"node_modules/@types/node-fetch": {
@@ -7459,6 +7469,14 @@
 				}
 			]
 		},
+		"node_modules/bignumber.js": {
+			"version": "9.1.0",
+			"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.0.tgz",
+			"integrity": "sha512-4LwHK4nfDOraBCtst+wOWIHbu1vhvAPJK8g8nROd4iuc3PSEjWif/qwbkh8jwCJz6yDBvtU4KPynETgrfh7y3A==",
+			"engines": {
+				"node": "*"
+			}
+		},
 		"node_modules/binary-extensions": {
 			"version": "2.2.0",
 			"integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==",
@@ -8012,6 +8030,15 @@
 			"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
 			"integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc="
 		},
+		"node_modules/fast-zlib": {
+			"version": "2.0.1",
+			"resolved": "https://registry.npmjs.org/fast-zlib/-/fast-zlib-2.0.1.tgz",
+			"integrity": "sha512-DCoYgNagM2Bt1VIpXpdGnRx4LzqJeYG0oh6Nf/7cWo6elTXkFGMw9CrRCYYUIapYNrozYMoyDRflx9mgT3Awyw==",
+			"funding": {
+				"type": "patreon",
+				"url": "https://patreon.com/timotejroiko"
+			}
+		},
 		"node_modules/figlet": {
 			"version": "1.5.2",
 			"integrity": "sha512-WOn21V8AhyE1QqVfPIVxe3tupJacq1xGkPTB4iagT6o+P2cAgEOOwIxMftr4+ZCTI6d551ij9j61DFr0nsP2uQ==",
@@ -8560,6 +8587,14 @@
 				"js-yaml": "bin/js-yaml.js"
 			}
 		},
+		"node_modules/json-bigint": {
+			"version": "1.0.0",
+			"resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",
+			"integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",
+			"dependencies": {
+				"bignumber.js": "^9.0.0"
+			}
+		},
 		"node_modules/jsonfile": {
 			"version": "4.0.0",
 			"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
@@ -10226,7 +10261,7 @@
 				"proxy-agent": "^5.0.0",
 				"reflect-metadata": "^0.1.13",
 				"ts-node": "^10.2.1",
-				"typeorm": "^0.2.38",
+				"typeorm": "^0.2.37",
 				"typescript": "^4.4.2",
 				"typescript-json-schema": "^0.50.1"
 			},
@@ -15633,6 +15668,12 @@
 			"integrity": "sha512-HBNx4lhkxN7bx6P0++W8E289foSu8kO8GCk2unhuVggO+cE7rh9DhZUyPhUxNRG9m+5B5BTKxZQ5ZP92x/mx9Q==",
 			"dev": true
 		},
+		"@types/json-bigint": {
+			"version": "1.0.1",
+			"resolved": "https://registry.npmjs.org/@types/json-bigint/-/json-bigint-1.0.1.tgz",
+			"integrity": "sha512-zpchZLNsNuzJHi6v64UBoFWAvQlPhch7XAi36FkH6tL1bbbmimIF+cS7vwkzY4u5RaSWMoflQfu+TshMPPw8uw==",
+			"dev": true
+		},
 		"@types/jsonwebtoken": {
 			"version": "8.5.5",
 			"integrity": "sha512-OGqtHQ7N5/Ap/TUwO6IgHDuLiAoTmHhGpNvgkCm/F4N6pKzx/RBSfr2OXZSwC6vkfnsEdb6+7DNZVtiXiwdwFw==",
@@ -15642,8 +15683,9 @@
 			}
 		},
 		"@types/node": {
-			"version": "14.17.16",
-			"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ==",
+			"version": "14.18.25",
+			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.25.tgz",
+			"integrity": "sha512-9pLfceRSrKIsv/MISN6RoFWTIzka36Uk2Uuf5a8cHyDYhEgl5Hm5dXoe621KULeBjt+cFsY18mILsWWtJeG80w==",
 			"dev": true
 		},
 		"@types/node-fetch": {
@@ -15867,6 +15909,11 @@
 			"version": "1.5.1",
 			"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="
 		},
+		"bignumber.js": {
+			"version": "9.1.0",
+			"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.0.tgz",
+			"integrity": "sha512-4LwHK4nfDOraBCtst+wOWIHbu1vhvAPJK8g8nROd4iuc3PSEjWif/qwbkh8jwCJz6yDBvtU4KPynETgrfh7y3A=="
+		},
 		"binary-extensions": {
 			"version": "2.2.0",
 			"integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==",
@@ -16307,6 +16354,11 @@
 			"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
 			"integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc="
 		},
+		"fast-zlib": {
+			"version": "2.0.1",
+			"resolved": "https://registry.npmjs.org/fast-zlib/-/fast-zlib-2.0.1.tgz",
+			"integrity": "sha512-DCoYgNagM2Bt1VIpXpdGnRx4LzqJeYG0oh6Nf/7cWo6elTXkFGMw9CrRCYYUIapYNrozYMoyDRflx9mgT3Awyw=="
+		},
 		"figlet": {
 			"version": "1.5.2",
 			"integrity": "sha512-WOn21V8AhyE1QqVfPIVxe3tupJacq1xGkPTB4iagT6o+P2cAgEOOwIxMftr4+ZCTI6d551ij9j61DFr0nsP2uQ=="
@@ -16703,6 +16755,14 @@
 				"argparse": "^2.0.1"
 			}
 		},
+		"json-bigint": {
+			"version": "1.0.0",
+			"resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",
+			"integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",
+			"requires": {
+				"bignumber.js": "^9.0.0"
+			}
+		},
 		"jsonfile": {
 			"version": "4.0.0",
 			"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
diff --git a/gateway/package.json b/gateway/package.json
index 205cd9c1..ad500897 100644
--- a/gateway/package.json
+++ b/gateway/package.json
@@ -16,6 +16,7 @@
 	"license": "AGPL-3.0-only",
 	"devDependencies": {
 		"@types/amqplib": "^0.8.1",
+		"@types/json-bigint": "^1.0.1",
 		"@types/jsonwebtoken": "^8.5.0",
 		"@types/node": "^14.18.24",
 		"@types/node-fetch": "^2.5.12",
@@ -30,6 +31,7 @@
 		"amqplib": "^0.8.0",
 		"dotenv": "^8.2.0",
 		"fast-zlib": "^2.0.1",
+		"json-bigint": "^1.0.0",
 		"jsonwebtoken": "^8.5.1",
 		"lambert-server": "^1.2.11",
 		"missing-native-js-functions": "^1.2.18",
diff --git a/gateway/src/events/Message.ts b/gateway/src/events/Message.ts
index e9271da4..1a177e0c 100644
--- a/gateway/src/events/Message.ts
+++ b/gateway/src/events/Message.ts
@@ -8,6 +8,8 @@ import OPCodeHandlers from "../opcodes";
 import { Tuple } from "lambert-server";
 import { check } from "../opcodes/instanceOf";
 import WS from "ws";
+import BigIntJson from "json-bigint";
+const bigIntJson = BigIntJson({ storeAsString: true });
 
 const PayloadSchema = {
 	op: Number,
@@ -30,16 +32,13 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 				buffer = buffer.toString() as any;
 			}
 		}
-		data = JSON.parse(buffer as string);
+		data = bigIntJson.parse(buffer as string);
 	}
 	else if (typeof buffer == "string") {
-		data = JSON.parse(buffer as string);
+		data = bigIntJson.parse(buffer as string)
 	}
 	else return;
 
-	// TODO: find a way to properly convert a funny number to string
-	if (data?.op == 14 && typeof data.d.guild_id == "number") return;
-
 	check.call(this, PayloadSchema, data);
 
 	// @ts-ignore
@@ -54,7 +53,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 	try {
 		return await OPCodeHandler.call(this, data);
 	} catch (error) {
-		console.error(error);
+		console.error(`Error: Op ${data.op}`, error);
 		// if (!this.CLOSED && this.CLOSING)
 		return this.close(CLOSECODES.Unknown_error);
 	}
diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts
index 060de65b..8a90ce0c 100644
--- a/gateway/src/listener/listener.ts
+++ b/gateway/src/listener/listener.ts
@@ -241,7 +241,7 @@ async function consume(this: WebSocket, opts: EventOpts) {
 			break;
 	}
 
-	Send(this, {
+	await Send(this, {
 		op: OPCODES.Dispatch,
 		t: event,
 		d: data,
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 041512c5..57c45d1f 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -137,12 +137,13 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 	guilds = guilds.map((guild) => {
 		if (user.bot) {
 			setTimeout(() => {
-				Send(this, {
+				var promise = Send(this, {
 					op: OPCODES.Dispatch,
 					t: EVENTEnum.GuildCreate,
 					s: this.sequence++,
 					d: guild,
 				});
+				if (promise) promise.catch(console.error);
 			}, 500);
 			return { id: guild.id, unavailable: true };
 		}
diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts
index 0db35286..54b06eb2 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/gateway/src/opcodes/LazyRequest.ts
@@ -39,13 +39,16 @@ async function getMembers(guild_id: string, range: [number, number]) {
 	}
 	catch (e) {
 		console.error(`LazyRequest`, e);
+	}
+
+	if (!members) {
 		return {
 			items: [],
 			groups: [],
 			range: [],
 			members: [],
-		}
-	}		
+		};
+	}
 
 	const groups = [] as any[];
 	const items = [];
@@ -157,7 +160,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 		.flat()
 		.unique();
 
-	return Send(this, {
+	return await Send(this, {
 		op: OPCODES.Dispatch,
 		s: this.sequence++,
 		t: "GUILD_MEMBER_LIST_UPDATE",