cache fetched central.json

Massimo Melina committed Oct 16, 2024 at 23:41 UTC c4c3a7d97d8e77cb3eac463c2a6a780741cbdcce
1 file changed +11 -6
src/github.ts
+11 -6
@@ -1,7 +1,9 @@
1 // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import events from './events'
4 -import { DAY, httpString, httpStream, unzip, AsapStream, debounceAsync, asyncGeneratorToArray, wait, popKey } from './misc'
4 +import {
5 + DAY, httpString, httpStream, unzip, AsapStream, debounceAsync, asyncGeneratorToArray, wait, popKey, onlyTruthy
6 +} from './misc'
7 import {
8 DISABLING_SUFFIX, enablePlugin, findPluginByRepo, getAvailablePlugins, getPluginInfo, isPluginEnabled, mapPlugins,
9 parsePluginSource, PATH as PLUGINS_PATH, Repo, startPlugin, stopPlugin, STORAGE_FOLDER
@@ -227,15 +229,17 @@ export async function searchPlugins(text='', { skipRepos=[''] }={}) {
229 }
230
231 export const alerts = storedMap.singleSync<string[]>('alerts', [])
232 +const cachedCentralInfo = storedMap.singleSync('cachedCentralInfo', '')
233 // centralized hosted information, to be used as little as possible
234 const FN = 'central.json'
235 let builtIn = JSON.parse(readFileSync(join(__dirname, '..', FN), 'utf8'))
233 -let lastResult: any
236 export const getProjectInfo = debounceAsync(
237 () => readGithubFile(`${HFS_REPO}/${HFS_REPO_BRANCH}/${FN}`)
238 .then(JSON.parse, () => null)
239 .then(o => {
238 - o = lastResult = Object.assign({ ...builtIn, ...lastResult }, o) // fall back to built-in
240 + if (o)
241 + cachedCentralInfo.set(o)
242 + o ||= { ...cachedCentralInfo.get() || builtIn } // fall back to built-in
243 // merge byVersions info in the main object, but collect alerts separately, to preserve multiple instances
244 const allAlerts: string[] = [o.alert]
245 for (const [ver, more] of Object.entries(popKey(o, 'byVersion') || {}))
@@ -250,9 +254,10 @@ export const getProjectInfo = debounceAsync(
254 console.log("ALERT:", a)
255 return allAlerts
256 })
253 - for (const k of Object.keys(o.repo_blacklist || {})) {
254 - const p = findPluginByRepo(k)
255 - if (p)
257 + const black = onlyTruthy(Object.keys(o.repo_blacklist || {}).map(findPluginByRepo))
258 + if (black.length) {
259 + console.log("blacklisted plugins found:", black.join(', '))
260 + for (const p of black)
261 enablePlugin(p.id, false)
262 }
263 return o