refactor(core): dedupe final pairing and sandbox media clones

This commit is contained in:
Peter Steinberger
2026-03-02 21:36:13 +00:00
parent 453a1c179d
commit 5897eed6e9
2 changed files with 69 additions and 69 deletions

View File

@@ -30,6 +30,7 @@ type AllowFromReadCacheEntry = {
size: number | null;
entries: string[];
};
type AllowFromStatLike = { mtimeMs: number; size: number } | null;
const allowFromReadCache = new Map<string, AllowFromReadCacheEntry>();
@@ -321,6 +322,31 @@ function resolveAllowFromReadCacheHit(params: {
return cloneAllowFromCacheEntry(cached);
}
function resolveAllowFromReadCacheOrMissing(
filePath: string,
stat: AllowFromStatLike,
): { entries: string[]; exists: boolean } | null {
const cached = resolveAllowFromReadCacheHit({
filePath,
exists: Boolean(stat),
mtimeMs: stat?.mtimeMs ?? null,
size: stat?.size ?? null,
});
if (cached) {
return { entries: cached.entries, exists: cached.exists };
}
if (!stat) {
setAllowFromReadCache(filePath, {
exists: false,
mtimeMs: null,
size: null,
entries: [],
});
return { entries: [], exists: false };
}
return null;
}
async function readAllowFromStateForPathWithExists(
channel: PairingChannel,
filePath: string,
@@ -335,24 +361,9 @@ async function readAllowFromStateForPathWithExists(
}
}
const cached = resolveAllowFromReadCacheHit({
filePath,
exists: Boolean(stat),
mtimeMs: stat?.mtimeMs ?? null,
size: stat?.size ?? null,
});
if (cached) {
return { entries: cached.entries, exists: cached.exists };
}
if (!stat) {
setAllowFromReadCache(filePath, {
exists: false,
mtimeMs: null,
size: null,
entries: [],
});
return { entries: [], exists: false };
const cachedOrMissing = resolveAllowFromReadCacheOrMissing(filePath, stat);
if (cachedOrMissing) {
return cachedOrMissing;
}
const { value, exists } = await readJsonFile<AllowFromStore>(filePath, {
@@ -387,24 +398,9 @@ function readAllowFromStateForPathSyncWithExists(
}
}
const cached = resolveAllowFromReadCacheHit({
filePath,
exists: Boolean(stat),
mtimeMs: stat?.mtimeMs ?? null,
size: stat?.size ?? null,
});
if (cached) {
return { entries: cached.entries, exists: cached.exists };
}
if (!stat) {
setAllowFromReadCache(filePath, {
exists: false,
mtimeMs: null,
size: null,
entries: [],
});
return { entries: [], exists: false };
const cachedOrMissing = resolveAllowFromReadCacheOrMissing(filePath, stat);
if (cachedOrMissing) {
return cachedOrMissing;
}
let raw = "";