aboutsummaryrefslogtreecommitdiff
path: root/src/requests.js
diff options
context:
space:
mode:
authorRémy Raes <contact@remyraes.com>2022-01-07 22:35:59 +0100
committerGitHub <noreply@github.com>2022-01-07 22:35:59 +0100
commit81024fb393af7a2ec6b96c7ae40f415b01718a40 (patch)
treeab57487ca7488f845c80ae06a1d41220398b0210 /src/requests.js
parent89d8d1986b62d5686160f28359383ccd0c67f77d (diff)
downloadViper-81024fb393af7a2ec6b96c7ae40f415b01718a40.tar.gz
Viper-81024fb393af7a2ec6b96c7ae40f415b01718a40.zip
fix: Rate limit (#27)
* [chore] adding electron-fetch dependency * [feat] adding requests module skeleton * [feat] not doing any http request to get latest ns version if data is fresh * [fix] data is considered fresh if less than 5 minutes old * [feat] adding requests.getLatestNsVersionLink implementation * [refactor] fully using requests module (with associated cache) * [docs] adding documentation to requests module * [refactor] adding a key to get nsLatestRelease cache results * [chore] removing request deprecated dependency * [feat] using follow-redirects instead of electron-fetch * [chore] removing electron-fetch dependency * fixed formatting Single quotes to double, proper textwidth for comments, and proper comment style, the usual... Nothing special here... * fixed wrong location for requests.json We don't want to clutter people's config folder, hence we should use the cache folder instead... Co-authored-by: 0neGal <mail@0negal.com>
Diffstat (limited to 'src/requests.js')
-rw-r--r--src/requests.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/requests.js b/src/requests.js
new file mode 100644
index 0000000..06c5577
--- /dev/null
+++ b/src/requests.js
@@ -0,0 +1,77 @@
+const { app } = require("electron");
+const path = require("path");
+const fs = require("fs");
+const { https } = require("follow-redirects");
+
+
+// all requests results are stored in this file
+const cachePath = path.join(app.getPath("cache"), "requests.json");
+const NORTHSTAR_LATEST_RELEASE_KEY = "nsLatestRelease";
+
+
+function _saveCache(data) {
+ fs.writeFileSync(cachePath, JSON.stringify(data));
+}
+
+function _getRequestsCache() {
+ if (fs.existsSync(cachePath)) {
+ return JSON.parse(fs.readFileSync(cachePath, "utf8"));
+ } else {
+ fs.writeFileSync(cachePath, "{}");
+ return {};
+ }
+}
+
+// Returns latest Northstar version available from GitHub releases. If
+// there's no cache result for this request, or if cache exists but is
+// old, refreshes cache with new data.
+async function getLatestNsVersion() {
+ return new Promise((resolve, reject) => {
+ let cache = _getRequestsCache();
+
+ if (cache[NORTHSTAR_LATEST_RELEASE_KEY] && (Date.now() - cache[NORTHSTAR_LATEST_RELEASE_KEY]["time"]) < 5 * 60 * 1000) {
+ console.log(`returning ${NORTHSTAR_LATEST_RELEASE_KEY} data from cache`);
+ resolve( cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"]["tag_name"] );
+ } else {
+ https.get({
+ host: "api.github.com",
+ port: 443,
+ path: "/repos/R2Northstar/Northstar/releases/latest",
+ method: "GET",
+ headers: { "User-Agent": "viper" }
+ },
+
+ response => {
+ response.setEncoding("utf8");
+ let responseData = "";
+
+ response.on("data", data => {
+ responseData += data;
+ });
+
+ response.on("end", _ => {
+ cache[NORTHSTAR_LATEST_RELEASE_KEY] = {
+ "time": Date.now(),
+ "body": JSON.parse(responseData)
+ };
+ _saveCache(cache);
+ resolve( cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"]["tag_name"] );
+ });
+ });
+ }
+ });
+}
+
+// Returns the download link to latest Northstar version. Should always
+// be called after getLatestNsVersion, as it refreshes cache data (if
+// needed).
+function getLatestNsVersionLink() {
+ const cache = _getRequestsCache();
+ return cache[NORTHSTAR_LATEST_RELEASE_KEY]["body"].assets[0].browser_download_url;
+}
+
+
+module.exports = {
+ getLatestNsVersion,
+ getLatestNsVersionLink
+};