Bump version, adjust current license in package.json, add error handling and use built-in checksum computation library

This commit is contained in:
X3F200C 2023-08-07 14:32:24 -04:00
parent 4134f25d39
commit b3e33c7d75
2 changed files with 44 additions and 19 deletions

View File

@ -2,7 +2,7 @@ const fs = require("node:fs");
const https = require("node:https");
const path = require("node:path");
const child_process = require("node:child_process");
const checksum = require("checksum");
const crypto = require("node:crypto");
const manifestURL = "https://cdn.plutonium.pw/updater/prod/info.json";
@ -66,7 +66,24 @@ function httpsDownload(url, filePath) {
});
}
function checkFiles(manifest) {
function getFileSHA1(filePath) {
return new Promise(function (resolve, reject) {
var fileStream = fs.createReadStream(filePath);
var hash = crypto.createHash("sha1");
hash.setEncoding("hex");
fileStream.on("end", function () {
hash.end();
resolve(hash.read());
});
fileStream.pipe(hash);
});
}
async function checkFiles(manifest) {
var filesToDownload = [];
for (var f = 0; f < manifest.files.length; f++) {
@ -79,9 +96,7 @@ function checkFiles(manifest) {
continue;
}
var sum = checksum(filePath, {
"algorithm": "sha1"
});
var sum = await getFileSHA1(filePath);
if (sum !== file.hash) {
console.log("File " + file.name + " needs to be downloaded again. ");
@ -125,14 +140,28 @@ httpsGet(manifestURL).then(async function (response) {
try {
var data = JSON.parse(response.body);
var wrongFiles = checkFiles(data);
checkFiles(data).then(function (wrongFiles) {
if (wrongFiles.length > 0) {
console.log("We have " + wrongFiles.length + " files to download");
await downloadFiles(data.baseUrl, wrongFiles);
}
console.log("We have " + wrongFiles.length + " files to download. ");
downloadFiles(data.baseUrl, wrongFiles).then(function () {
launchLauncher().then(function () {
await launchLauncher();
} catch (error) {
console.error(error);
}
}).catch(function (error) {
console.error("An error has occurred while opening the launcher. \n" + error.stack);
});
}).catch(function (error) {
console.error("An error has occurred while downloading a file. \n" + error.stack);
});
} else {
console.log("All files seem good ! ");
}
}).catch(function (error) {
console.error("An error has occurred while checking the files. \n" + error.stack);
});
} catch (error) {
console.error("An error has occurred while decoding the main manifest. \n" + error.stack);
}
}).catch(function (error) {
console.error("An error has occurred while getting the main manifest. \n" + error.stack);
});

View File

@ -1,11 +1,7 @@
{
"dependencies": {
"checksum": "^1.0.0"
},
"name": "open-plutonium-updater",
"version": "0.0.1",
"version": "0.2.0",
"main": "index.js",
"author": "X3F200C",
"license": "MIT",
"private": true
"license": "GPL-3.0"
}