aboutsummaryrefslogtreecommitdiff
path: root/src/utils.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/utils.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/utils.js')
-rw-r--r--src/utils.js98
1 files changed, 46 insertions, 52 deletions
diff --git a/src/utils.js b/src/utils.js
index d2e6fe6..85db119 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -7,9 +7,9 @@ const events = new Emitter();
const cli = require("./cli");
const lang = require("./lang");
+const requests = require("./requests");
const unzip = require("unzipper");
-const request = require("request");
const exec = require("child_process").spawn;
const { https } = require("follow-redirects");
@@ -69,7 +69,7 @@ function getNSVersion() {
}
}
-function update() {
+async function update() {
for (let i = 0; i < settings.excludes.length; i++) {
let exclude = path.join(settings.gamepath + "/" + settings.excludes[i]);
if (fs.existsSync(exclude)) {
@@ -81,59 +81,53 @@ function update() {
console.log(lang("cli.update.checking"));
var version = getNSVersion();
- request({
- json: true,
- headers: {"User-Agent": "Viper"},
- url: "https://api.github.com/repos/R2Northstar/Northstar/releases/latest",
- }, (error, response, body) => {
- var tag = body["tag_name"];
-
- if (version === tag) {
- ipcMain.emit("ns-updated");
- console.log(lang("cli.update.uptodate"), version);
-
- winLog(lang("gui.update.uptodate"));
- return;
- } else {
- if (version != "unknown") {
- console.log(lang("cli.update.current"), version);
- }; console.log(lang("cli.update.downloading") + ":", tag);
-
- winLog(lang("gui.update.downloading"));
- }
+ const latestAvailableVersion = await requests.getLatestNsVersion();
+
+ if (version === latestAvailableVersion) {
+ ipcMain.emit("ns-updated");
+ console.log(lang("cli.update.uptodate"), version);
+
+ winLog(lang("gui.update.uptodate"));
+ return;
+ } else {
+ if (version != "unknown") {
+ console.log(lang("cli.update.current"), version);
+ }; console.log(lang("cli.update.downloading") + ":", latestAvailableVersion);
+
+ winLog(lang("gui.update.downloading"));
+ }
+
+ https.get(requests.getLatestNsVersionLink(), (res) => {
+ let stream = fs.createWriteStream(settings.zip);
+ res.pipe(stream);
+
+ let received = 0;
+ res.on("data", (chunk) => {
+ received += chunk.length;
+ winLog(lang("gui.update.downloading") + " " + (received / 1024 / 1024).toFixed(1) + "mb");
+ })
- https.get(body.assets[0].browser_download_url, (res) => {
- let stream = fs.createWriteStream(settings.zip);
- res.pipe(stream);
-
- let received = 0;
- res.on("data", (chunk) => {
- received += chunk.length;
- winLog(lang("gui.update.downloading") + " " + (received / 1024 / 1024).toFixed(1) + "mb");
- })
-
- stream.on("finish", () => {
- stream.close();
- winLog(lang("gui.update.extracting"));
- console.log(lang("cli.update.downloaddone"));
- fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath}))
- .on("finish", () => {
- fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), tag);
- ipcMain.emit("getversion");
-
- for (let i = 0; i < settings.excludes.length; i++) {
- let exclude = path.join(settings.gamepath + "/" + settings.excludes[i]);
- if (fs.existsSync(exclude + ".excluded")) {
- fs.renameSync(exclude + ".excluded", exclude)
- }
+ stream.on("finish", () => {
+ stream.close();
+ winLog(lang("gui.update.extracting"));
+ console.log(lang("cli.update.downloaddone"));
+ fs.createReadStream(settings.zip).pipe(unzip.Extract({path: settings.gamepath}))
+ .on("finish", () => {
+ fs.writeFileSync(path.join(settings.gamepath, "ns_version.txt"), latestAvailableVersion);
+ ipcMain.emit("getversion");
+
+ for (let i = 0; i < settings.excludes.length; i++) {
+ let exclude = path.join(settings.gamepath + "/" + settings.excludes[i]);
+ if (fs.existsSync(exclude + ".excluded")) {
+ fs.renameSync(exclude + ".excluded", exclude)
}
+ }
- ipcMain.emit("ns-updated");
- winLog(lang("gui.update.finished"));
- console.log(lang("cli.update.finished"));
- cli.exit();
- });
- })
+ ipcMain.emit("ns-updated");
+ winLog(lang("gui.update.finished"));
+ console.log(lang("cli.update.finished"));
+ cli.exit();
+ });
})
})
}