silverwolf-x/project / packages /next /src /server /dev /require-cache.ts
silverwolf-x's picture
download
raw
1.97 kB
import isError from '../../lib/is-error'
import { realpathSync } from '../../lib/realpath'
import { clearManifestCache } from '../load-manifest.external'
/**
* Batch delete modules from require.cache with a single scan.
*
* When deleting N modules, this performs ONE scan of require.cache
* instead of N scans, reducing complexity from O(N * C) to O(C + N)
* where C = size of require.cache.
*/
function deleteFromRequireCache(filePaths: string[]): void {
// Phase 1: Resolve all paths and collect modules to delete
const resolvedPaths: string[] = []
const modsToDelete = new Set<NodeModule>()
for (let filePath of filePaths) {
try {
filePath = realpathSync(filePath)
} catch (e) {
if (isError(e) && e.code !== 'ENOENT') throw e
}
const mod = require.cache[filePath]
if (mod) {
resolvedPaths.push(filePath)
modsToDelete.add(mod)
}
}
if (modsToDelete.size === 0) return
// Phase 2: Single scan of require.cache to remove child references
const modules = Object.values(require.cache)
for (let m = 0; m < modules.length; m++) {
const children = modules[m]?.children
if (children && children.length) {
let len = children.length
for (let i = 0; i < len; i++) {
if (modsToDelete.has(children[i])) {
children[i] = children[--len]
i-- // re-check swapped element
}
}
children.length = len
}
}
// Phase 3: Clear parent references from children and delete cache entries
for (const mod of modsToDelete) {
const children = mod.children
for (let i = 0; i < children.length; i++) {
if (children[i].parent === mod) {
children[i].parent = null
}
}
}
for (const filePath of resolvedPaths) {
delete require.cache[filePath]
}
}
export function deleteCache(filePaths: string[]) {
for (const filePath of filePaths) {
clearManifestCache(filePath)
}
deleteFromRequireCache(filePaths)
}

Xet Storage Details

Size:
1.97 kB
·
Xet hash:
fa9b7ab71cbd7c715775118b83f49dae3b620e74429feac8813d042778dc1221

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.