// pathCache — the one shape for a module-level memo of something read from a // file: ONE entry per key (a path, a video id), bounded by count and by bytes, // least recently used first out. // // Why it exists (umtool's idle heap kept growing): the memos keyed themselves // by `path|mtime|size`, so every change to a file ADDED an entry beside the // old one and nothing ever let go -- a corpus that keeps moving (re-synced // captions, a manifest saved from the bench) left every stale version of // every cue file parsed in the heap until a restart. Here the key is the path // and the file's signature (`mtime|size`) is a field of the entry: a new // signature REPLACES the entry, a read with a stale one is a miss that frees // it, and the count/byte bounds keep even a walk over the whole corpus from // holding all of it. // // Plain ESM with JSDoc types: node --test, the CLI and the Next server all // import it. /** * @template V * @typedef {{ * get(key: string, sig?: string): V | undefined, * has(key: string, sig?: string): boolean, * set(key: string, sig: string, value: V, bytes?: number): V, * delete(key: string): void, * clear(): void, * readonly size: number, * readonly bytes: number, * }} PathCache */ /** * @template V * @param {{ max?: number, maxBytes?: number, sizeOf?: (value: V) => number }} [opts] * `max` entries at most (default 256); `maxBytes` of their sizes at most * (default unbounded) -- each entry's size is the `bytes` given to `set`, * else `sizeOf(value)`, else 0. The newest entry is kept even when it alone * is over `maxBytes`: a cache that cannot hold the file it was just asked * for would read it again on every call. * @returns {PathCache} */ export function pathCache({ max = 256, maxBytes = Infinity, sizeOf } = {}) { /** @type {Map} */ const entries = new Map(); let bytes = 0; /** @param {string} key */ const drop = (key) => { const e = entries.get(key); if (!e) return; bytes -= e.bytes; entries.delete(key); }; /** @param {string} key @param {string} sig */ const live = (key, sig) => { const e = entries.get(key); if (!e) return undefined; // A stale signature is the file changed under us: the old value is let go now. if (e.sig !== sig) { drop(key); return undefined; } // Most recently used last, so the first key is the one to evict. entries.delete(key); entries.set(key, e); return e; }; return { get(key, sig = "") { return live(key, sig)?.value; }, has(key, sig = "") { return live(key, sig) !== undefined; }, set(key, sig, value, size) { drop(key); const n = Number(size ?? (sizeOf ? sizeOf(value) : 0)); const b = Number.isFinite(n) && n > 0 ? n : 0; entries.set(key, { sig, value, bytes: b }); bytes += b; while (entries.size > max || (bytes > maxBytes && entries.size > 1)) { const oldest = entries.keys().next().value; if (oldest === undefined || oldest === key) break; drop(oldest); } return value; }, delete: drop, clear() { entries.clear(); bytes = 0; }, get size() { return entries.size; }, get bytes() { return bytes; }, }; } /** A file's signature for a pathCache entry: its mtime (ms, rounded) and size. */ export const fileSig = (/** @type {{ mtimeMs: number, size: number }} */ st) => `${Math.round(st.mtimeMs)}|${st.size}`;