From 07dcc8f825c70bd5ea83fb30e317b6d330cbb10b Mon Sep 17 00:00:00 2001 From: holly Date: Fri, 3 Sep 2021 21:11:40 +0100 Subject: [PATCH] allow AsyncHttpRequest to parse j... son on it's own --- server/loginHandler.js | 3 ++- server/util/AsyncHttpRequest.js | 12 +++++++++--- server/util/RequestType.json | 5 +++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 server/util/RequestType.json diff --git a/server/loginHandler.js b/server/loginHandler.js index d9c2b3d..367e77e 100644 --- a/server/loginHandler.js +++ b/server/loginHandler.js @@ -2,6 +2,7 @@ const osu = require("osu-packet"), User = require("./User.js"), { v4: uuid } = require('uuid'), ahttp = require("./util/AsyncHttpRequest.js"), + RequestType = require("./util/RequestType.json"), getUserByUsername = require("./util/getUserByUsername.js"), getUserByToken = require("./util/getUserByToken.js"), @@ -41,7 +42,7 @@ module.exports = async function(req, res, loginInfo) { userLocation = [0, 0]; } else { // Get user's location using zxq - userLocationData = JSON.parse(await ahttp(`http://ip.zxq.co/${requestIP}`)); + userLocationData = await ahttp(`http://ip.zxq.co/${requestIP}`, RequestType.JSON); userLocation = userLocationData.loc.split(","); } diff --git a/server/util/AsyncHttpRequest.js b/server/util/AsyncHttpRequest.js index d2c9bc4..a2683bd 100644 --- a/server/util/AsyncHttpRequest.js +++ b/server/util/AsyncHttpRequest.js @@ -1,10 +1,16 @@ -const request = require("request"); +const request = require("request"), RequestType = require("./RequestType.json"); -module.exports = async function(url) { +const functionMap = { + 0: (resolve, body) => resolve(body), + 1: (resolve, body) => resolve(JSON.parse(body)), + 2: null +}; + +module.exports = async function(url, reqType = RequestType.Text) { return new Promise((resolve, reject) => { request(url, (err, res, body) => { if (err) reject(err); - else resolve(body); + else functionMap[reqType](resolve, body); }); }); } \ No newline at end of file diff --git a/server/util/RequestType.json b/server/util/RequestType.json new file mode 100644 index 0000000..3b9fcf6 --- /dev/null +++ b/server/util/RequestType.json @@ -0,0 +1,5 @@ +{ + "Text": 0, + "JSON": 1, + "XML": 2 +} \ No newline at end of file