From 5b3e218d446ac805685e43c8c95ca524755b6601 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Wed, 24 Jan 2024 12:03:25 +0100 Subject: entirely refactor src/modules/requests.js Its now been split into 2, requests.js and releases.js, the latter simply gets relevant info from GitHub release pages. The prior however gives simple functions for doing `GET` requests, and caching the result, and then transparently it'll use that cache when you request it next time. On top of this, some requests made by the renderer will now also use this, and this in turn ends up making loading the mod browser much faster. As instead of having to request the list of packages from Thunderstore, we can simply load the result of an old request. The current lifetime of the cache is 5 minutes, however this can also easily be adjusted. This also moves the cached requests away from /viper-requests.json, and over to /Viper/cached-requests.json --- src/modules/releases.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/modules/releases.js (limited to 'src/modules/releases.js') diff --git a/src/modules/releases.js b/src/modules/releases.js new file mode 100644 index 0000000..c071970 --- /dev/null +++ b/src/modules/releases.js @@ -0,0 +1,80 @@ +const requests = require("./requests"); + +let releases = { + notes: {}, + latest: {} +} + +// gets and returns the release notes of a GitHub repo +async function github_releases(repo) { + let request = false; + + // attempt to perform the request, while caching it + try { + request = JSON.parse(await requests.get( + "api.github.com", `/repos/${repo}/releases`, + "release-notes-" + repo + )) + }catch(err) { + // request or parsing failed, return `false` + return false; + } + + // request is somehow falsy, return `false` + if (! request) { + return false; + } + + // return the actual request as parsed JSON + return request; +} + +// returns release notes for Viper +releases.notes.viper = async () => { + return await github_releases("0neGal/viper"); +} + +// returns release notes for Northstar +releases.notes.northstar = async () => { + return await github_releases("R2Northstar/Northstar"); +} + +// gets and returns some details of the latest release of a GitHub repo +async function github_latest(repo) { + let request = false; + + // attempt to perform the request, while caching it + try { + request = JSON.parse(await requests.get( + "api.github.com", `/repos/${repo}/releases/latest`, + "latest-release-" + repo + )) + }catch(err) { + // request or parsing failed, return `false` + return false; + } + + // request is somehow falsy, return `false` + if (! request) { + return false; + } + + // return the actual request as parsed JSON + return { + notes: request.body, + version: request.tag_name, + download_link: request.assets[0].browser_download_url + } +} + +// returns latest release for Viper +releases.latest.viper = async () => { + return await github_latest("0neGal/viper"); +} + +// returns latest release for Northstar +releases.latest.northstar = async () => { + return await github_latest("R2Northstar/Northstar"); +} + +module.exports = releases; -- cgit v1.2.3