133 lines
3.5 KiB
TypeScript
133 lines
3.5 KiB
TypeScript
import fs from "node:fs/promises"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
|
|
const DEFAULT_MAX_MB = 2048
|
|
const ORPHAN_MAX_AGE_MS = 60 * 60 * 1000
|
|
/** Prefix for in-progress entries. Pruning removes old unpinned ones as abandoned. */
|
|
export const CACHE_ORPHAN_PREFIX = "convert-"
|
|
|
|
type CacheState = {
|
|
pinned: Map<string, number>
|
|
pruning: Promise<void> | null
|
|
}
|
|
|
|
const globalState = globalThis as typeof globalThis & {
|
|
__otunesCache?: CacheState
|
|
}
|
|
const state = (globalState.__otunesCache ??= {
|
|
pinned: new Map(),
|
|
pruning: null,
|
|
})
|
|
|
|
/**
|
|
* Transcode cache root. Set OTUNES_CACHE_DIR to a persistent volume to keep
|
|
* transcodes across restarts.
|
|
*/
|
|
export function cacheRoot() {
|
|
return process.env.OTUNES_CACHE_DIR || path.join(os.tmpdir(), "otunes-cache")
|
|
}
|
|
|
|
export function cacheDir(kind: "hls" | "lossless") {
|
|
return path.join(cacheRoot(), kind)
|
|
}
|
|
|
|
function maxCacheBytes() {
|
|
const configured = Number(process.env.OTUNES_CACHE_MAX_MB)
|
|
const mb = Number.isFinite(configured) && configured > 0 ? configured : DEFAULT_MAX_MB
|
|
return mb * 1024 * 1024
|
|
}
|
|
|
|
/** Pinned entries are never pruned. Pins are reference counted. */
|
|
export function pinCacheEntry(entry: string) {
|
|
state.pinned.set(entry, (state.pinned.get(entry) ?? 0) + 1)
|
|
}
|
|
|
|
export function unpinCacheEntry(entry: string) {
|
|
const count = state.pinned.get(entry) ?? 0
|
|
if (count <= 1) {
|
|
state.pinned.delete(entry)
|
|
} else {
|
|
state.pinned.set(entry, count - 1)
|
|
}
|
|
}
|
|
|
|
/** Marks an entry as recently used so pruning removes it last. */
|
|
export async function touchCacheEntry(entry: string) {
|
|
const now = new Date()
|
|
await fs.utimes(entry, now, now)
|
|
}
|
|
|
|
async function entrySize(entry: string): Promise<number> {
|
|
const stat = await fs.stat(entry).catch(() => null)
|
|
if (!stat) {
|
|
return 0
|
|
}
|
|
if (!stat.isDirectory()) {
|
|
return stat.size
|
|
}
|
|
const names = await fs.readdir(entry).catch(() => [])
|
|
const sizes = await Promise.all(
|
|
names.map((name) => entrySize(path.join(entry, name)))
|
|
)
|
|
return sizes.reduce((total, size) => total + size, 0)
|
|
}
|
|
|
|
async function prune() {
|
|
const entries: {
|
|
path: string
|
|
size: number
|
|
mtimeMs: number
|
|
orphan: boolean
|
|
}[] = []
|
|
|
|
for (const kind of ["hls", "lossless"] as const) {
|
|
const dir = cacheDir(kind)
|
|
const names = await fs.readdir(dir).catch(() => [])
|
|
for (const name of names) {
|
|
const entry = path.join(dir, name)
|
|
const stat = await fs.stat(entry).catch(() => null)
|
|
if (!stat) {
|
|
continue
|
|
}
|
|
entries.push({
|
|
path: entry,
|
|
size: await entrySize(entry),
|
|
mtimeMs: stat.mtimeMs,
|
|
orphan: name.startsWith(CACHE_ORPHAN_PREFIX),
|
|
})
|
|
}
|
|
}
|
|
|
|
const maxBytes = maxCacheBytes()
|
|
const now = Date.now()
|
|
let total = entries.reduce((sum, entry) => sum + entry.size, 0)
|
|
entries.sort((a, b) => a.mtimeMs - b.mtimeMs)
|
|
|
|
for (const entry of entries) {
|
|
if (state.pinned.has(entry.path)) {
|
|
continue
|
|
}
|
|
const remove = entry.orphan
|
|
? now - entry.mtimeMs > ORPHAN_MAX_AGE_MS
|
|
: total > maxBytes
|
|
if (remove) {
|
|
await fs.rm(entry.path, { recursive: true, force: true }).catch(() => {})
|
|
total -= entry.size
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes least recently used entries until the cache fits under
|
|
* OTUNES_CACHE_MAX_MB, plus abandoned in-progress entries.
|
|
*/
|
|
export function pruneCache() {
|
|
state.pruning ??= prune()
|
|
.catch((error) => console.error("Transcode cache prune failed", error))
|
|
.finally(() => {
|
|
state.pruning = null
|
|
})
|
|
return state.pruning
|
|
}
|