fix: admin/plugins: entries are duplicated at each reload

Massimo Melina committed Apr 14, 2022 at 22:44 UTC 94e87f7e5c8e83d71e5841ea8528da2ebc36cd0b
2 files changed +8 -3
server/src/adminApis.ts
+5 -1
@@ -56,7 +56,7 @@ export const adminApis: ApiHandlers = {
56 frpDetected: getConfig('localhost_admin') && !getProxyDetected()
57 && getConnections().every(c => isLocalHost(c.ctx || c.socket.remoteAddress || ''))
58 && await frpDebounced(),
59 - }
59 + }
60
61 function serverStatus(h: typeof st.httpSrv, configuredPort?: number) {
62 return {
@@ -153,6 +153,10 @@ export const adminApis: ApiHandlers = {
153 const list = sendList([ ...mapPlugins(serialize), ...getAvailablePlugins() ])
154 return list.events(ctx, {
155 pluginLoaded: p => list.add(serialize(p)),
156 + pluginReloaded: p => {
157 + const { id, ...rest } = serialize(p)
158 + list.update({ id }, rest)
159 + },
160 pluginUnloaded: id => list.remove({ id }),
161 pluginAvailableNoMore: p => list.remove({ id: p.id }),
162 pluginAvailable: p => list.add(p),
server/src/plugins.ts
+3 -2
@@ -78,7 +78,8 @@ export class Plugin {
78 constructor(readonly id:string, private readonly data:any, private unwatch:()=>void){
79 if (!data) throw 'invalid data'
80 // if a previous instance is present, we are going to overwrite it, but first call its unload callback
81 - try { plugins[id]?.data?.unload?.() }
81 + const old = plugins[id]
82 + try { old?.data?.unload?.() } // we don't want all the effects of the Plugin.unload
83 catch(e){
84 console.debug('error unloading plugin', id, String(e))
85 }
@@ -94,7 +95,7 @@ export class Plugin {
95 console.warn('invalid', k)
96 }
97 }
97 - events.emit('pluginLoaded', this)
98 + events.emit(old ? 'pluginReloaded' : 'pluginLoaded', this)
99 }
100 get middleware(): undefined | PluginMiddleware {
101 return this.data?.middleware