Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import { readdir, readFile, rm, stat } from "node:fs/promises"
|
|
import { join } from "node:path"
|
|
|
|
const EMPTY_STORE_MAX_BYTES = 128
|
|
const DRAFT_RETENTION_MS = 30 * 24 * 60 * 60 * 1000
|
|
const DRAFT_KEEP_RECENT = 100
|
|
|
|
type StoreKind = "draft" | "workspace"
|
|
type StoreCandidate = {
|
|
name: string
|
|
path: string
|
|
kind: StoreKind
|
|
modified: number
|
|
empty: boolean
|
|
}
|
|
|
|
export async function cleanupStoreFiles(userDataPath: string, now = Date.now()) {
|
|
const entries = await readdir(userDataPath, { withFileTypes: true }).catch(() => [])
|
|
const candidates = (
|
|
await Promise.all(
|
|
entries
|
|
.filter((entry) => entry.isFile())
|
|
.map(async (entry) => {
|
|
const kind = storeKind(entry.name)
|
|
if (!kind) return
|
|
|
|
const file = join(userDataPath, entry.name)
|
|
const stats = await stat(file).catch(() => undefined)
|
|
if (!stats?.isFile()) return
|
|
|
|
return {
|
|
name: entry.name,
|
|
path: file,
|
|
kind,
|
|
modified: stats.mtimeMs,
|
|
empty: await isEmptyStore(file, stats.size),
|
|
}
|
|
}),
|
|
)
|
|
).filter((candidate) => !!candidate)
|
|
|
|
const stale = new Set<StoreCandidate>()
|
|
for (const candidate of candidates) {
|
|
if (candidate.empty) stale.add(candidate)
|
|
if (candidate.kind === "draft" && now - candidate.modified > DRAFT_RETENTION_MS) stale.add(candidate)
|
|
}
|
|
|
|
candidates
|
|
.filter((candidate) => candidate.kind === "draft" && !candidate.empty)
|
|
.sort((a, b) => b.modified - a.modified)
|
|
.slice(DRAFT_KEEP_RECENT)
|
|
.forEach((candidate) => stale.add(candidate))
|
|
|
|
const deleted = await Promise.all(
|
|
[...stale].map(async (candidate) => {
|
|
await rm(candidate.path, { force: true })
|
|
return candidate.name
|
|
}),
|
|
)
|
|
|
|
return { scanned: candidates.length, deleted }
|
|
}
|
|
|
|
export async function deleteStoreFileIfEmpty(userDataPath: string, name: string) {
|
|
if (!storeKind(name)) return false
|
|
|
|
const file = join(userDataPath, name)
|
|
const stats = await stat(file).catch(() => undefined)
|
|
if (!stats?.isFile()) return false
|
|
if (!(await isEmptyStore(file, stats.size))) return false
|
|
|
|
await rm(file, { force: true })
|
|
return true
|
|
}
|
|
|
|
function storeKind(name: string): StoreKind | undefined {
|
|
if (/^opencode\.draft\..+\.dat$/.test(name)) return "draft"
|
|
if (/^opencode\.workspace\..+\.dat$/.test(name)) return "workspace"
|
|
}
|
|
|
|
async function isEmptyStore(file: string, size: number) {
|
|
if (size > EMPTY_STORE_MAX_BYTES) return false
|
|
|
|
const raw = await readFile(file, "utf8").catch(() => undefined)
|
|
if (raw === undefined) return false
|
|
if (raw.trim() === "") return true
|
|
|
|
try {
|
|
const parsed = JSON.parse(raw) as unknown
|
|
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) && Object.keys(parsed).length === 0
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|