2021-08-21 04:50:22 -07:00
|
|
|
const axios = require("axios");
|
2022-03-07 00:24:24 -08:00
|
|
|
const compareVersions = require("compare-versions");
|
2023-07-03 00:28:03 -07:00
|
|
|
const { log } = require("../src/util");
|
2024-09-24 02:51:21 -07:00
|
|
|
const { Settings } = require("./settings");
|
2021-08-21 04:50:22 -07:00
|
|
|
|
|
|
|
exports.version = require("../package.json").version;
|
|
|
|
exports.latestVersion = null;
|
|
|
|
|
2023-07-03 00:28:03 -07:00
|
|
|
// How much time in ms to wait between update checks
|
|
|
|
const UPDATE_CHECKER_INTERVAL_MS = 1000 * 60 * 60 * 48;
|
|
|
|
const UPDATE_CHECKER_LATEST_VERSION_URL = "https://uptime.kuma.pet/version";
|
|
|
|
|
2021-08-21 04:50:22 -07:00
|
|
|
let interval;
|
|
|
|
|
|
|
|
exports.startInterval = () => {
|
|
|
|
let check = async () => {
|
2024-09-24 02:51:21 -07:00
|
|
|
if (await Settings.get("checkUpdate") === false) {
|
2023-07-03 00:28:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("update-checker", "Retrieving latest versions");
|
|
|
|
|
2021-08-21 04:50:22 -07:00
|
|
|
try {
|
2023-07-03 00:28:03 -07:00
|
|
|
const res = await axios.get(UPDATE_CHECKER_LATEST_VERSION_URL);
|
2021-08-21 04:50:22 -07:00
|
|
|
|
|
|
|
// For debug
|
|
|
|
if (process.env.TEST_CHECK_VERSION === "1") {
|
2021-10-30 09:36:46 -07:00
|
|
|
res.data.slow = "1000.0.0";
|
|
|
|
}
|
|
|
|
|
2024-09-24 02:51:21 -07:00
|
|
|
let checkBeta = await Settings.get("checkBeta");
|
2022-03-07 00:24:24 -08:00
|
|
|
|
|
|
|
if (checkBeta && res.data.beta) {
|
2022-12-05 03:17:24 -08:00
|
|
|
if (compareVersions.compare(res.data.beta, res.data.slow, ">")) {
|
2022-03-07 00:24:24 -08:00
|
|
|
exports.latestVersion = res.data.beta;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:36:46 -07:00
|
|
|
if (res.data.slow) {
|
|
|
|
exports.latestVersion = res.data.slow;
|
2021-08-21 04:50:22 -07:00
|
|
|
}
|
|
|
|
|
2023-07-03 00:28:03 -07:00
|
|
|
} catch (_) {
|
|
|
|
log.info("update-checker", "Failed to check for new versions");
|
|
|
|
}
|
2021-08-21 04:50:22 -07:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
check();
|
2023-07-03 00:28:03 -07:00
|
|
|
interval = setInterval(check, UPDATE_CHECKER_INTERVAL_MS);
|
2021-08-21 04:50:22 -07:00
|
|
|
};
|
|
|
|
|
2022-04-20 11:56:40 -07:00
|
|
|
/**
|
|
|
|
* Enable the check update feature
|
|
|
|
* @param {boolean} value Should the check update feature be enabled?
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-08-21 04:50:22 -07:00
|
|
|
exports.enableCheckUpdate = async (value) => {
|
2024-09-24 02:51:21 -07:00
|
|
|
await Settings.set("checkUpdate", value);
|
2021-08-21 04:50:22 -07:00
|
|
|
|
|
|
|
clearInterval(interval);
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
exports.startInterval();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.socket = null;
|