fix: simplified code in wrapped promise

This commit is contained in:
SamTV12345 2025-04-08 18:54:46 +02:00
parent 9a97760ede
commit 77a81cd0fc
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 {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 log4js from 'log4js';
import {MapArrayType} from "../../types/MapType";
const pluginDefs = require('../../../static/js/pluginfw/plugin_defs');
const logger = log4js.getLogger('adminPlugins');
@ -21,7 +22,13 @@ exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {
if (!isAdmin) return;
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) => {
if (!results[plugin]) return false;

View file

@ -44,7 +44,6 @@
"find-root": "1.1.0",
"formidable": "^3.5.2",
"http-errors": "^2.0.0",
"https-proxy-agent": "^7.0.6",
"jose": "^5.10.0",
"js-cookie": "^3.0.5",
"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;
let cacheTimestamp = 0;
export const getAvailablePlugins = (maxCacheAge: number|false) => {
export const getAvailablePlugins = async (maxCacheAge: number | false) => {
const nowTimestamp = Math.round(Date.now() / 1000);
return new Promise<MapArrayType<PackageInfo>>(async (resolve, reject) => {
// check cache age before making any request
if (availablePlugins && maxCacheAge && (nowTimestamp - cacheTimestamp) <= maxCacheAge) {
return resolve(availablePlugins);
return availablePlugins;
}
await axios.get(`${settings.updateServer}/plugins.json`, {headers})
.then((pluginsLoaded: AxiosResponse<MapArrayType<PackageInfo>>) => {
const pluginsLoaded: AxiosResponse<MapArrayType<PackageInfo>> = await axios.get(`${settings.updateServer}/plugins.json`, {headers})
availablePlugins = pluginsLoaded.data;
cacheTimestamp = nowTimestamp;
resolve(availablePlugins);
})
.catch(async (err) => {
logger.error(`Error fetching available plugins: ${err}`);
});
});
return availablePlugins;
};
@ -213,4 +206,7 @@ export const search = (searchTerm: string, maxCacheAge: number) => getAvailableP
return res;
}
);
).catch((err)=>{
logger.error(`Error searching plugins: ${err}`);
return {} as MapArrayType<PackageInfo>;
});