fix: simplified code in wrapped promise

This commit is contained in:
SamTV12345 2025-04-08 18:54:46 +02:00 committed by SamTV12345
parent d62d5a0460
commit 7fde6ee9bd
3 changed files with 22 additions and 20 deletions

View file

@ -5,9 +5,10 @@ import {ErrorCaused} from "../../types/ErrorCaused";
import {QueryType} from "../../types/QueryType"; import {QueryType} from "../../types/QueryType";
import {getAvailablePlugins, install, search, uninstall} from "../../../static/js/pluginfw/installer"; import {getAvailablePlugins, install, search, uninstall} from "../../../static/js/pluginfw/installer";
import {PackageData} from "../../types/PackageInfo"; import {PackageData, PackageInfo} from "../../types/PackageInfo";
import semver from 'semver'; import semver from 'semver';
import log4js from 'log4js'; import log4js from 'log4js';
import {MapArrayType} from "../../types/MapType";
const pluginDefs = require('../../../static/js/pluginfw/plugin_defs'); const pluginDefs = require('../../../static/js/pluginfw/plugin_defs');
const logger = log4js.getLogger('adminPlugins'); const logger = log4js.getLogger('adminPlugins');
@ -21,7 +22,13 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
if (!isAdmin) return; if (!isAdmin) return;
const checkPluginForUpdates = async () => { const checkPluginForUpdates = async () => {
const results = await getAvailablePlugins(/* maxCacheAge:*/ 60 * 10); let results: MapArrayType<PackageInfo>
try {
results = await getAvailablePlugins(/* maxCacheAge:*/ 60 * 10);
} catch (error) {
console.error('Error checking for plugin updates:', error);
return [];
}
return Object.keys(pluginDefs.plugins).filter((plugin) => { return Object.keys(pluginDefs.plugins).filter((plugin) => {
if (!results[plugin]) return false; if (!results[plugin]) return false;

View file

@ -44,7 +44,6 @@
"find-root": "1.1.0", "find-root": "1.1.0",
"formidable": "^3.5.2", "formidable": "^3.5.2",
"http-errors": "^2.0.0", "http-errors": "^2.0.0",
"https-proxy-agent": "^7.0.6",
"jose": "^5.10.0", "jose": "^5.10.0",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"jsdom": "^26.0.0", "jsdom": "^26.0.0",

View file

@ -162,25 +162,18 @@ export const install = async (pluginName: string, cb:Function|null = null) => {
export let availablePlugins:MapArrayType<PackageInfo>|null = null; export let availablePlugins:MapArrayType<PackageInfo>|null = null;
let cacheTimestamp = 0; let cacheTimestamp = 0;
export const getAvailablePlugins = (maxCacheAge: number|false) => { export const getAvailablePlugins = async (maxCacheAge: number | false) => {
const nowTimestamp = Math.round(Date.now() / 1000); const nowTimestamp = Math.round(Date.now() / 1000);
return new Promise<MapArrayType<PackageInfo>>(async (resolve, reject) => { // check cache age before making any request
// check cache age before making any request if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) { return availablePlugins;
return resolve(availablePlugins); }
}
await axios.get(`${settings.updateServer}/plugins.json`, {headers}) const pluginsLoaded: AxiosResponse<MapArrayType<PackageInfo>> = await axios.get(`${settings.updateServer}/plugins.json`, {headers})
.then((pluginsLoaded: AxiosResponse<MapArrayType<PackageInfo>>) => { availablePlugins = pluginsLoaded.data;
availablePlugins = pluginsLoaded.data; cacheTimestamp = nowTimestamp;
cacheTimestamp = nowTimestamp; return availablePlugins;
resolve(availablePlugins);
})
.catch(async (err) => {
logger.error(`Error fetching available plugins: ${err}`);
});
});
}; };
@ -213,4 +206,7 @@ export const search = (searchTerm: string, maxCacheAge: number) => getAvailableP
return res; return res;
} }
); ).catch((err)=>{
logger.error(`Error searching plugins: ${err}`);
return {} as MapArrayType<PackageInfo>;
});