mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
Fix plugin upgrade
Correctly decache all plugin paths
This commit is contained in:
parent
8406a9e8ee
commit
ca87d95bcb
6 changed files with 85 additions and 19 deletions
78
server/helpers/decache.ts
Normal file
78
server/helpers/decache.ts
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Thanks: https://github.com/dwyl/decache
|
||||
// We reuse this file to also uncache plugin base path
|
||||
|
||||
import { extname } from 'path'
|
||||
|
||||
function decachePlugin (pluginPath: string, libraryPath: string) {
|
||||
const moduleName = find(libraryPath)
|
||||
|
||||
if (!moduleName) return
|
||||
|
||||
searchCache(moduleName, function (mod) {
|
||||
delete require.cache[mod.id]
|
||||
})
|
||||
|
||||
removeCachedPath(pluginPath)
|
||||
}
|
||||
|
||||
function decacheModule (name: string) {
|
||||
const moduleName = find(name)
|
||||
|
||||
if (!moduleName) return
|
||||
|
||||
searchCache(moduleName, function (mod) {
|
||||
delete require.cache[mod.id]
|
||||
})
|
||||
|
||||
removeCachedPath(moduleName)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
decacheModule,
|
||||
decachePlugin
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function find (moduleName: string) {
|
||||
try {
|
||||
return require.resolve(moduleName)
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function searchCache (moduleName: string, callback: (current: NodeModule) => void) {
|
||||
const resolvedModule = require.resolve(moduleName)
|
||||
let mod: NodeModule
|
||||
const visited = {}
|
||||
|
||||
if (resolvedModule && ((mod = require.cache[resolvedModule]) !== undefined)) {
|
||||
// Recursively go over the results
|
||||
(function run (current) {
|
||||
visited[current.id] = true
|
||||
|
||||
current.children.forEach(function (child) {
|
||||
if (extname(child.filename) !== '.node' && !visited[child.id]) {
|
||||
run(child)
|
||||
}
|
||||
})
|
||||
|
||||
// Call the specified callback providing the
|
||||
// found module
|
||||
callback(current)
|
||||
})(mod)
|
||||
}
|
||||
};
|
||||
|
||||
function removeCachedPath (pluginPath: string) {
|
||||
const pathCache = (module.constructor as any)._pathCache
|
||||
|
||||
Object.keys(pathCache).forEach(function (cacheKey) {
|
||||
if (cacheKey.includes(pluginPath)) {
|
||||
delete pathCache[cacheKey]
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue