aboutsummaryrefslogtreecommitdiff
path: root/src/extras/requests.js
diff options
context:
space:
mode:
authorDelta <31860825+AA-Delta@users.noreply.github.com>2022-01-25 18:32:40 -0500
committerGitHub <noreply@github.com>2022-01-25 18:32:40 -0500
commit1c17a26bddd819f2fc0f1b2164cae02d441181a3 (patch)
treeb41587176950fcd5f0ff1a661e7f255fd531d54e /src/extras/requests.js
parentdd1a4d50f590b127f0fd3404b063bc42cf29112b (diff)
parent6469020960576b9e4f62c0c308eb5a055a696daa (diff)
downloadViper-1c17a26bddd819f2fc0f1b2164cae02d441181a3.tar.gz
Viper-1c17a26bddd819f2fc0f1b2164cae02d441181a3.zip
Merge pull request #5 from AA-Delta/main
Update for translation
Diffstat (limited to 'src/extras/requests.js')
-rw-r--r--src/extras/requests.js153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/extras/requests.js b/src/extras/requests.js
new file mode 100644
index 0000000..16b1330
--- /dev/null
+++ b/src/extras/requests.js
@@ -0,0 +1,153 @@
+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";
+const NORTHSTAR_RELEASE_NOTES_KEY = "nsReleaseNotes";
+const VIPER_RELEASE_NOTES_KEY = "vpReleaseNotes";
+
+
+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) {
+ 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;
+}
+
+// Returns and caches the Northstar release notes
+async function getNsReleaseNotes() {
+ return new Promise(resolve => {
+ let cache = _getRequestsCache();
+
+ if (cache[NORTHSTAR_RELEASE_NOTES_KEY] && (Date.now() - cache[NORTHSTAR_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) {
+ resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] );
+ } else {
+ https.get({
+ host: "api.github.com",
+ port: 443,
+ path: "/repos/R2Northstar/Northstar/releases",
+ method: "GET",
+ headers: { "User-Agent": "viper" }
+ },
+
+ response => {
+ response.setEncoding("utf8");
+ let responseData = "";
+
+ response.on("data", data => {
+ responseData += data;
+ });
+
+ response.on("end", _ => {
+ cache[NORTHSTAR_RELEASE_NOTES_KEY] = {
+ "time": Date.now(),
+ "body": JSON.parse(responseData)
+ };
+ _saveCache(cache);
+ resolve( cache[NORTHSTAR_RELEASE_NOTES_KEY]["body"] );
+ });
+ });
+ }
+ });
+}
+
+// Returns and caches the Viper release notes
+async function getVpReleaseNotes() {
+ return new Promise(resolve => {
+ let cache = _getRequestsCache();
+
+ if (cache[VIPER_RELEASE_NOTES_KEY] && (Date.now() - cache[VIPER_RELEASE_NOTES_KEY]["time"]) < 5 * 60 * 1000) {
+ resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] );
+ } else {
+ https.get({
+ host: "api.github.com",
+ port: 443,
+ path: "/repos/0negal/viper/releases",
+ method: "GET",
+ headers: { "User-Agent": "viper" }
+ },
+
+ response => {
+ response.setEncoding("utf8");
+ let responseData = "";
+
+ response.on("data", data => {
+ responseData += data;
+ });
+
+ response.on("end", _ => {
+ cache[VIPER_RELEASE_NOTES_KEY] = {
+ "time": Date.now(),
+ "body": JSON.parse(responseData)
+ };
+ _saveCache(cache);
+ resolve( cache[VIPER_RELEASE_NOTES_KEY]["body"] );
+ });
+ });
+ }
+ });
+}
+
+module.exports = {
+ getLatestNsVersion,
+ getLatestNsVersionLink,
+ getNsReleaseNotes,
+ getVpReleaseNotes
+};