From f9a3a6aaaef19184ad244e85022d978d86abe2b6 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 25 Sep 2023 11:05:33 +0000 Subject: [PATCH] Improve update check (#5945) * Send etherpad version in update check request * Cache etherpad informations for one hour * Change format of User-Agent * Improve exception handling --- src/node/utils/UpdateCheck.js | 47 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/node/utils/UpdateCheck.js b/src/node/utils/UpdateCheck.js index 44248d29c..9290380d8 100644 --- a/src/node/utils/UpdateCheck.js +++ b/src/node/utils/UpdateCheck.js @@ -2,28 +2,39 @@ const semver = require('semver'); const settings = require('./Settings'); const axios = require('axios'); -let infos; +const headers = { + 'User-Agent': 'Etherpad/' + settings.getEpVersion(), +} -const loadEtherpadInformations = () => - axios.get('https://static.etherpad.org/info.json') - .then(async resp => { - try { - infos = await resp.data; - if (infos === undefined || infos === null) { - await Promise.reject("Could not retrieve current version") - return - } - return await Promise.resolve(infos); - } - catch (err) { - return await Promise.reject(err); - } - }) +const updateInterval = 60 * 60 * 1000; // 1 hour +let infos; +let lastLoadingTime = null; + +const loadEtherpadInformations = () => { + if (lastLoadingTime !== null && Date.now() - lastLoadingTime < updateInterval) { + return Promise.resolve(infos); + } + + return axios.get('https://static.etherpad.org/info.json', {headers: headers}) + .then(async resp => { + infos = await resp.data; + if (infos === undefined || infos === null) { + await Promise.reject("Could not retrieve current version") + return + } + + lastLoadingTime = Date.now(); + return await Promise.resolve(infos); + }) + .catch(async err => { + return await Promise.reject(err); + }); +} exports.getLatestVersion = () => { - exports.needsUpdate(); - return infos.latestVersion; + exports.needsUpdate().catch(); + return infos?.latestVersion; }; exports.needsUpdate = async (cb) => {