mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
79
server/core/helpers/decache.ts
Normal file
79
server/core/helpers/decache.ts
Normal file
|
@ -0,0 +1,79 @@
|
|||
// Thanks: https://github.com/dwyl/decache
|
||||
// We reuse this file to also uncache plugin base path
|
||||
|
||||
import { Module } from 'module'
|
||||
import { extname } from 'path'
|
||||
|
||||
function decachePlugin (require: NodeRequire, libraryPath: string) {
|
||||
const moduleName = find(require, libraryPath)
|
||||
|
||||
if (!moduleName) return
|
||||
|
||||
searchCache(require, moduleName, function (mod) {
|
||||
delete require.cache[mod.id]
|
||||
|
||||
removeCachedPath(mod.path)
|
||||
})
|
||||
}
|
||||
|
||||
function decacheModule (require: NodeRequire, name: string) {
|
||||
const moduleName = find(require, name)
|
||||
|
||||
if (!moduleName) return
|
||||
|
||||
searchCache(require, moduleName, function (mod) {
|
||||
delete require.cache[mod.id]
|
||||
|
||||
removeCachedPath(mod.path)
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
decacheModule,
|
||||
decachePlugin
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function find (require: NodeRequire, moduleName: string) {
|
||||
try {
|
||||
return require.resolve(moduleName)
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function searchCache (require: NodeRequire, 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 as any)._pathCache as { [ id: string ]: string[] }
|
||||
|
||||
Object.keys(pathCache).forEach(function (cacheKey) {
|
||||
if (cacheKey.includes(pluginPath)) {
|
||||
delete pathCache[cacheKey]
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue