Better error handling + general cleanup #182

Merged
punctuations merged 11 commits from master into master 2021-12-07 16:06:33 -03:00
10 changed files with 39 additions and 39 deletions
Showing only changes of commit db7b640ae4 - Show all commits

View file

@ -13,14 +13,14 @@ module.exports = async (app, req, res, level) => {
const raw_data = level.data; const raw_data = level.data;
const response_data = analyze_level(level, raw_data); const response_data = analyze_level(level, raw_data);
return res.send(response_data); return res.status(200).send(response_data);
} else { } else {
zlib.unzip(levelString, (err, buffer) => { zlib.unzip(levelString, (err, buffer) => {
if (err) { return res.send("-2"); } if (err) { return res.status(500).send("-2"); }
const raw_data = buffer.toString(); const raw_data = buffer.toString();
const response_data = analyze_level(level, raw_data); const response_data = analyze_level(level, raw_data);
return res.send(response_data); return res.status(200).send(response_data);
}); });
} }
} }

View file

@ -1,6 +1,6 @@
module.exports = async (app, req, res) => { module.exports = async (app, req, res) => {
if (req.offline) return res.send("-1") if (req.offline) return res.status(500).send("-1")
let count = +req.query.count || 10 let count = +req.query.count || 10
if (count > 1000) count = 1000 if (count > 1000) count = 1000
@ -20,14 +20,14 @@ module.exports = async (app, req, res) => {
req.gdRequest(path, req.gdParams(params), function(err, resp, body) { req.gdRequest(path, req.gdParams(params), function(err, resp, body) {
if (err) return res.send("-1") if (err) return res.status(500).send("-1")
comments = body.split('|') comments = body.split('|')
comments = comments.map(x => x.split(':')) comments = comments.map(x => x.split(':'))
comments = comments.map(x => x.map(x => app.parseResponse(x, "~"))) comments = comments.map(x => x.map(x => app.parseResponse(x, "~")))
if (req.query.type == "profile") comments.filter(x => x[0][2]) if (req.query.type == "profile") comments.filter(x => x[0][2])
else comments = comments.filter(x => x[0] && x[0][2]) else comments = comments.filter(x => x[0] && x[0][2])
if (!comments.length) return res.send("-1") if (!comments.length) return res.status(204).send("-1")
let pages = body.split('#')[1].split(":") let pages = body.split('#')[1].split(":")
let lastPage = +Math.ceil(+pages[0] / +pages[2]); let lastPage = +Math.ceil(+pages[0] / +pages[2]);
@ -79,7 +79,7 @@ module.exports = async (app, req, res) => {
}) })
return res.send(commentArray) return res.status(200).send(commentArray)
}) })
} }

View file

@ -6,7 +6,7 @@ module.exports = async (app, req, res, api, ID, analyze) => {
function rejectLevel() { function rejectLevel() {
if (!api) return res.redirect('search/' + req.params.id) if (!api) return res.redirect('search/' + req.params.id)
else return res.send("-1") else return res.status(500).send("-1")
} }
if (req.offline) { if (req.offline) {
@ -22,7 +22,7 @@ module.exports = async (app, req, res, api, ID, analyze) => {
req.gdRequest('downloadGJLevel22', { levelID }, function (err, resp, body) { req.gdRequest('downloadGJLevel22', { levelID }, function (err, resp, body) {
if (err) { if (err) {
if (analyze && api && req.server.downloadsDisabled) return res.send("-3") if (analyze && api && req.server.downloadsDisabled) return res.status(403).send("-3")
else if (!api && levelID < 0) return res.redirect(`/?daily=${levelID * -1}`) else if (!api && levelID < 0) return res.redirect(`/?daily=${levelID * -1}`)
else return rejectLevel() else return rejectLevel()
} }
@ -69,7 +69,7 @@ module.exports = async (app, req, res, api, ID, analyze) => {
if (analyze) return app.run.analyze(app, req, res, level) if (analyze) return app.run.analyze(app, req, res, level)
function sendLevel() { function sendLevel() {
if (api) return res.send(level) if (api) return res.status(200).send(level)
else return fs.readFile('./html/level.html', 'utf8', function (err, data) { else return fs.readFile('./html/level.html', 'utf8', function (err, data) {
let html = data; let html = data;
@ -78,7 +78,7 @@ module.exports = async (app, req, res, api, ID, analyze) => {
let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g") let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g")
html = html.replace(regex, app.clean(level[x])) html = html.replace(regex, app.clean(level[x]))
}) })
return res.send(html) return res.status(200).send(html)
}) })
} }

View file

@ -3,19 +3,19 @@ let gauntletNames = ["Fire", "Ice", "Poison", "Shadow", "Lava", "Bonus", "Chaos"
module.exports = async (app, req, res) => { module.exports = async (app, req, res) => {
if (req.offline) return res.send("-1") if (req.offline) return res.status(500).send("-1")
let cached = cache[req.id] let cached = cache[req.id]
if (app.config.cacheGauntlets && cached && cached.data && cached.indexed + 2000000 > Date.now()) return res.send(cached.data) // half hour cache if (app.config.cacheGauntlets && cached && cached.data && cached.indexed + 2000000 > Date.now()) return res.status(200).send(cached.data) // half hour cache
req.gdRequest('getGJGauntlets21', {}, function (err, resp, body) { req.gdRequest('getGJGauntlets21', {}, function (err, resp, body) {
if (err) return res.send("-1") if (err) return res.status(500).send("-1")
let gauntlets = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[3]) let gauntlets = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[3])
let gauntletList = gauntlets.map(x => ({ id: +x[1], name: gauntletNames[+x[1] - 1] || "Unknown", levels: x[3].split(",") })) let gauntletList = gauntlets.map(x => ({ id: +x[1], name: gauntletNames[+x[1] - 1] || "Unknown", levels: x[3].split(",") }))
if (app.config.cacheGauntlets) cache[req.id] = {data: gauntletList, indexed: Date.now()} if (app.config.cacheGauntlets) cache[req.id] = {data: gauntletList, indexed: Date.now()}
res.send(gauntletList) res.status(200).send(gauntletList)
}) })

View file

@ -86,7 +86,7 @@ module.exports = async (app, req, res) => {
let iconCode = `${req.query.form == "cursed" ? "cursed" : form}${topless ? "top" : ""}-${iconID}-${col1}-${col2}-${colG || "x"}-${colW || "x"}-${outline ? 1 : 0}` let iconCode = `${req.query.form == "cursed" ? "cursed" : form}${topless ? "top" : ""}-${iconID}-${col1}-${col2}-${colG || "x"}-${colW || "x"}-${outline ? 1 : 0}`
if (!sizeParam && (!isSpecial || drawLegs) && cache[iconCode]) return res.end(cache[iconCode].value) if (!sizeParam && (!isSpecial || drawLegs) && cache[iconCode]) return res.status(200).end(cache[iconCode].value)
let useExtra = false let useExtra = false
let originalOffset = icons[icon].spriteOffset; let originalOffset = icons[icon].spriteOffset;
@ -301,7 +301,7 @@ module.exports = async (app, req, res) => {
cache[iconCode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[iconCode]}, 10000000) } // 3 hour cache cache[iconCode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[iconCode]}, 10000000) } // 3 hour cache
if (usercode) cache[usercode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[usercode]}, 300000) } // 5 min cache for player icons if (usercode) cache[usercode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[usercode]}, 300000) } // 5 min cache for player icons
} }
return res.end(buffer, 'base64') return res.status(200).end(buffer, 'base64')
}) })
} }
@ -363,7 +363,7 @@ module.exports = async (app, req, res) => {
else if (app.config.cachePlayerIcons && !Object.keys(req.query).filter(x => !["form", "forceGD"].includes(x)).length) { else if (app.config.cachePlayerIcons && !Object.keys(req.query).filter(x => !["form", "forceGD"].includes(x)).length) {
userCode = `${req.id}u-${username.toLowerCase()}-${forms[req.query.form] ? req.query.form : 'cube'}` userCode = `${req.id}u-${username.toLowerCase()}-${forms[req.query.form] ? req.query.form : 'cube'}`
if (cache[userCode]) return res.end(cache[userCode].value) if (cache[userCode]) return res.status(200).end(cache[userCode].value)
} }
let accountMode = !req.query.hasOwnProperty("player") && Number(req.params.id) let accountMode = !req.query.hasOwnProperty("player") && Number(req.params.id)

View file

@ -6,7 +6,7 @@ module.exports = async (app, req, res, api, analyze) => {
function rejectLevel() { function rejectLevel() {
if (!api) return res.redirect('search/' + req.params.id) if (!api) return res.redirect('search/' + req.params.id)
else return res.send("-1") else return res.status(500).send("-1")
} }
if (req.offline) return rejectLevel() if (req.offline) return rejectLevel()
@ -37,7 +37,7 @@ module.exports = async (app, req, res, api, analyze) => {
function sendLevel() { function sendLevel() {
if (api) return res.send(level) if (api) return res.status(200).send(level)
else return fs.readFile('./html/level.html', 'utf8', function (err, data) { else return fs.readFile('./html/level.html', 'utf8', function (err, data) {
let html = data; let html = data;
@ -50,7 +50,7 @@ module.exports = async (app, req, res, api, analyze) => {
}) })
if (req.server.downloadsDisabled) html = html.replace('id="additional" class="', 'id="additional" class="downloadDisabled ') if (req.server.downloadsDisabled) html = html.replace('id="additional" class="', 'id="additional" class="downloadDisabled ')
.replace('analyzeBtn"', 'analyzeBtn" style="filter: opacity(30%)"') .replace('analyzeBtn"', 'analyzeBtn" style="filter: opacity(30%)"')
return res.send(html) return res.status(200).send(html)
}) })
} }

View file

@ -3,17 +3,17 @@ let cache = {}
module.exports = async (app, req, res) => { module.exports = async (app, req, res) => {
if (req.offline) return res.send("-1") if (req.offline) return res.status(500).send("-1")
let cached = cache[req.id] let cached = cache[req.id]
if (app.config.cacheMapPacks && cached && cached.data && cached.indexed + 5000000 > Date.now()) return res.send(cached.data) // 1.5 hour cache if (app.config.cacheMapPacks && cached && cached.data && cached.indexed + 5000000 > Date.now()) return res.status(200).send(cached.data) // 1.5 hour cache
let params = { count: 250, page: 0 } let params = { count: 250, page: 0 }
let packs = [] let packs = []
function mapPackLoop() { function mapPackLoop() {
req.gdRequest('getGJMapPacks21', params, function (err, resp, body) { req.gdRequest('getGJMapPacks21', params, function (err, resp, body) {
if (err) return res.send("-1") if (err) return res.status(500).send("-1")
let newPacks = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[2]) let newPacks = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[2])
packs = packs.concat(newPacks) packs = packs.concat(newPacks)
@ -36,7 +36,7 @@ module.exports = async (app, req, res) => {
})) }))
if (app.config.cacheMapPacks) cache[req.id] = {data: mappacks, indexed: Date.now()} if (app.config.cacheMapPacks) cache[req.id] = {data: mappacks, indexed: Date.now()}
return res.send(mappacks) return res.status(200).send(mappacks)
}) })
} }
mapPackLoop() mapPackLoop()

View file

@ -4,7 +4,7 @@ module.exports = async (app, req, res, api, getLevels) => {
if (req.offline) { if (req.offline) {
if (!api) return res.redirect('/search/' + req.params.id) if (!api) return res.redirect('/search/' + req.params.id)
else return res.send("-1") else return res.status(500).send("-1")
} }
let username = getLevels || req.params.id let username = getLevels || req.params.id
@ -42,7 +42,7 @@ module.exports = async (app, req, res, api, getLevels) => {
if (err2 || dumbGDPSError) { if (err2 || dumbGDPSError) {
if (!api) return res.redirect('/search/' + req.params.id) if (!api) return res.redirect('/search/' + req.params.id)
else return res.send("-1") else return res.status(500).send("-1")
} }
if (!foundID) app.userCache(req.id, account[16], account[2], account[1]) if (!foundID) app.userCache(req.id, account[16], account[2], account[1])
@ -78,7 +78,7 @@ module.exports = async (app, req, res, api, getLevels) => {
glow: account[28] == "1", glow: account[28] == "1",
} }
if (api) return res.send(userData) if (api) return res.status(200).send(userData)
else fs.readFile('./html/profile.html', 'utf8', function(err, data) { else fs.readFile('./html/profile.html', 'utf8', function(err, data) {
let html = data; let html = data;
@ -87,7 +87,7 @@ module.exports = async (app, req, res, api, getLevels) => {
let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g") let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g")
html = html.replace(regex, app.clean(userData[x])) html = html.replace(regex, app.clean(userData[x]))
}) })
return res.send(html) return res.status(200).send(html)
}) })
}) })

View file

@ -5,17 +5,17 @@ let demonList = {}
module.exports = async (app, req, res) => { module.exports = async (app, req, res) => {
if (req.offline) return res.send(req.query.hasOwnProperty("err") ? "err" : "-1") if (req.offline) return res.status(500).send(req.query.hasOwnProperty("err") ? "err" : "-1")
let demonMode = req.query.hasOwnProperty("demonlist") || req.query.hasOwnProperty("demonList") || req.query.type == "demonlist" || req.query.type == "demonList" let demonMode = req.query.hasOwnProperty("demonlist") || req.query.hasOwnProperty("demonList") || req.query.type == "demonlist" || req.query.type == "demonList"
if (demonMode) { if (demonMode) {
if (!req.server.demonList) return res.send('-1') if (!req.server.demonList) return res.status(400).send('-1')
let dList = demonList[req.id] let dList = demonList[req.id]
if (!dList || !dList.list.length || dList.lastUpdated + 600000 < Date.now()) { // 10 minute cache if (!dList || !dList.list.length || dList.lastUpdated + 600000 < Date.now()) { // 10 minute cache
return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100', function (err1, resp1, list1) { return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100', function (err1, resp1, list1) {
if (err1) return res.send("-1") if (err1) return res.status(500).send("-1")
else return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100&after=100', function (err2, resp2, list2) { else return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100&after=100', function (err2, resp2, list2) {
if (err2) return res.send("-1") if (err2) return res.status(500).send("-1")
demonList[req.id] = {list: JSON.parse(list1).concat(JSON.parse(list2)).map(x => String(x.level_id)), lastUpdated: Date.now()} demonList[req.id] = {list: JSON.parse(list1).concat(JSON.parse(list2)).map(x => String(x.level_id)), lastUpdated: Date.now()}
return app.run.search(app, req, res) return app.run.search(app, req, res)
}) })
@ -85,7 +85,7 @@ module.exports = async (app, req, res) => {
filters.str = demonMode ? demonList[req.id].list : filters.str.split(",") filters.str = demonMode ? demonList[req.id].list : filters.str.split(",")
listSize = filters.str.length listSize = filters.str.length
filters.str = filters.str.slice(filters.page*amount, filters.page*amount + amount) filters.str = filters.str.slice(filters.page*amount, filters.page*amount + amount)
if (!filters.str.length) return res.send("-1") if (!filters.str.length) return res.status(400).send("-1")
filters.str = filters.str.map(x => String(Number(x) + (+req.query.l || 0))).join() filters.str = filters.str.map(x => String(Number(x) + (+req.query.l || 0))).join()
filters.page = 0 filters.page = 0
} }
@ -95,7 +95,7 @@ module.exports = async (app, req, res) => {
req.gdRequest('getGJLevels21', req.gdParams(filters), function(err, resp, body) { req.gdRequest('getGJLevels21', req.gdParams(filters), function(err, resp, body) {
if (err) return res.send("-1") if (err) return res.status(500).send("-1")
let splitBody = body.split('#') let splitBody = body.split('#')
let preRes = splitBody[0].split('|') let preRes = splitBody[0].split('|')
let authorList = {} let authorList = {}
@ -154,7 +154,7 @@ module.exports = async (app, req, res) => {
}) })
if (filters.type == 10) parsedLevels = parsedLevels.slice((+filters.page) * amount, (+filters.page + 1) * amount) if (filters.type == 10) parsedLevels = parsedLevels.slice((+filters.page) * amount, (+filters.page + 1) * amount)
return res.send(parsedLevels) return res.status(200).send(parsedLevels)
}) })
} }

View file

@ -4,15 +4,15 @@ module.exports = async (app, req, res) => {
// temporary solution until song api is re-enabled // temporary solution until song api is re-enabled
if (req.offline) return res.send('-1') if (req.offline) return res.status(500).send('-1')
let songID = req.params.song let songID = req.params.song
req.gdRequest('getGJSongInfo', {songID: songID}, function(err, resp, body) { req.gdRequest('getGJSongInfo', {songID: songID}, function(err, resp, body) {
if (err) return res.send('-1') if (err) return res.status(400).send('-1')
else if (body < 0) return res.send(false) else if (body < 0) return res.send(false)
request.get('https://www.newgrounds.com/audio/listen/' + songID, function(err2, resp2, song) { request.get('https://www.newgrounds.com/audio/listen/' + songID, function(err2, resp2, song) {
console.log(resp2.statusCode) console.log(resp2.statusCode)
return res.send(resp2.statusCode == 200) return res.status(200).send(resp2.statusCode == 200)
}) })
}) })
} }