mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-03-21 17:25:10 -05:00
Utils: Explicitly traverse the module tree to ensure no references are held long-term
This commit is contained in:
parent
071d7e3c94
commit
effb8ed103
25
lib/utils.ts
25
lib/utils.ts
|
|
@ -306,18 +306,25 @@ export function clearRequireCache(options: {exclude?: string[]} = {}) {
|
|||
excludes.push('/node_modules/');
|
||||
|
||||
for (const path in require.cache) {
|
||||
let skip = false;
|
||||
for (const exclude of excludes) {
|
||||
if (path.includes(exclude)) {
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!skip) delete require.cache[path];
|
||||
if (excludes.some(p => path.includes(p))) continue;
|
||||
const mod = require.cache[path]; // have to ref to appease ts
|
||||
if (!mod) continue;
|
||||
uncacheModuleTree(mod, excludes);
|
||||
delete require.cache[path];
|
||||
}
|
||||
}
|
||||
|
||||
export function uncacheModuleTree(mod: NodeJS.Module, excludes: string[], depth = 0) {
|
||||
depth++;
|
||||
if (depth >= 10) return;
|
||||
if (!mod.children || excludes.some(p => mod.filename.includes(p))) return;
|
||||
for (const child of mod.children) {
|
||||
if (excludes.some(p => child.filename.includes(p))) continue;
|
||||
uncacheModuleTree(child, excludes, depth);
|
||||
}
|
||||
delete (mod as any).children;
|
||||
}
|
||||
|
||||
export function deepClone(obj: any): any {
|
||||
if (obj === null || typeof obj !== 'object') return obj;
|
||||
if (Array.isArray(obj)) return obj.map(prop => deepClone(prop));
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user