0
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-11-21 18:58:58 +01:00
uptime-kuma/server/check-version.js

70 lines
1.8 KiB
JavaScript
Raw Normal View History

const { setSetting, setting } = require("./util-server");
2021-08-21 13:50:22 +02:00
const axios = require("axios");
2022-03-07 09:24:24 +01:00
const compareVersions = require("compare-versions");
const { log } = require("../src/util");
2021-08-21 13:50:22 +02:00
exports.version = require("../package.json").version;
exports.latestVersion = null;
// 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 13:50:22 +02:00
let interval;
exports.startInterval = () => {
let check = async () => {
if (await setting("checkUpdate") === false) {
return;
}
log.debug("update-checker", "Retrieving latest versions");
2021-08-21 13:50:22 +02:00
try {
const res = await axios.get(UPDATE_CHECKER_LATEST_VERSION_URL);
2021-08-21 13:50:22 +02:00
// For debug
if (process.env.TEST_CHECK_VERSION === "1") {
res.data.slow = "1000.0.0";
}
let checkBeta = await setting("checkBeta");
2022-03-07 09:24:24 +01:00
if (checkBeta && res.data.beta) {
2022-12-05 12:17:24 +01:00
if (compareVersions.compare(res.data.beta, res.data.slow, ">")) {
2022-03-07 09:24:24 +01:00
exports.latestVersion = res.data.beta;
return;
}
}
if (res.data.slow) {
exports.latestVersion = res.data.slow;
2021-08-21 13:50:22 +02:00
}
} catch (_) {
log.info("update-checker", "Failed to check for new versions");
}
2021-08-21 13:50:22 +02:00
};
check();
interval = setInterval(check, UPDATE_CHECKER_INTERVAL_MS);
2021-08-21 13:50:22 +02:00
};
/**
* Enable the check update feature
* @param {boolean} value Should the check update feature be enabled?
* @returns {Promise<void>}
*/
2021-08-21 13:50:22 +02:00
exports.enableCheckUpdate = async (value) => {
await setSetting("checkUpdate", value);
2021-08-21 13:50:22 +02:00
clearInterval(interval);
if (value) {
exports.startInterval();
}
};
exports.socket = null;