fix: some plugins' config resulted in garbage in the frontend #654
Massimo Melina committed
Jun 27, 2024 at 12:59 UTC
8ae3464382788efd6fc7b3626fc8aeb33b95eeca
2 files changed
+9
-6
src/debounceAsync.ts
+1
-1
@@ -71,7 +71,7 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
71
try {
72
const args = whoIsWaiting
73
whoIsWaiting = undefined
74
- runningCallback = callback(...args)
74
+ runningCallback = Promise.resolve(callback(...args)) // cast to promise, in case callback was not really async (or hybrid)
75
runningCallback.then(() => latestHasFailed = false, () => latestHasFailed = true)
76
return await runningCallback as MaybeUndefined<R> // await necessary to go-finally at the right time and even on exceptions
77
}
src/serveGuiFiles.ts
+8
-5
@@ -9,7 +9,7 @@ import { getPluginConfigFields, getPluginInfo, mapPlugins, pluginsConfig } from
9
import { refresh_session } from './api.auth'
10
import { ApiError } from './apiMiddleware'
11
import { join, extname } from 'path'
12
-import { CFG, debounceAsync, FRONTEND_OPTIONS, newObj, onlyTruthy, parseFile } from './misc'
12
+import { CFG, debounceAsync, FRONTEND_OPTIONS, isPrimitive, newObj, onlyTruthy, parseFile } from './misc'
13
import { favicon, title } from './adminApis'
14
import { subscribe } from 'valtio/vanilla'
15
import { customHtmlState, getAllSections, getSection } from './customHtml'
@@ -119,12 +119,15 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
119
</script>
120
`
121
if (isBody && isOpen)
122
- return all + `
122
+ return `${all}
123
${isFrontend && getSection('top')}
124
<style>
125
:root {
126
${_.map(plugins, (configs, pluginName) => // make plugin configs accessible via css
127
- _.map(configs, (v,k) => `--${pluginName}-${k}: ${serializeCss(v)};`).join('\n')).join('')}
127
+ _.map(configs, (v, k) => {
128
+ v = serializeCss(v)
129
+ return typeof v === 'string' && `\n--${pluginName}-${k}: ${v};`
130
+ }).filter(Boolean).join('')).join('')}
131
}
132
${getSection('style')}
133
</style>
@@ -140,8 +143,8 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
143
}
144
145
function serializeCss(v: any) {
143
- return typeof v === 'string' && /^#[0-9a-fA-F]{3,8}|rgba?\(.+\)$/.test(v) ? v
144
- : JSON.stringify(v)
146
+ return typeof v === 'string' && /^#[0-9a-fA-F]{3,8}|rgba?\(.+\)$/.test(v) ? v // colors
147
+ : isPrimitive(v) ? JSON.stringify(v)?.replace(/</g, '<') : undefined
148
}
149
150
function serveProxied(port: string | undefined, uri: string) { // used for development only