Files
immich-folio/lib/admin/parsedFileCache.ts
Ralksta ad8b09700b release: v0.19.0
Release v0.19.0. See the `## [0.19.0] — 2026-09-29` section of
[CHANGELOG.md](https://github.com/ralksta/immich-folio/blob/dev/CHANGELOG.md)
for the full list.

Highlights: client proofing links (#683), custom content pages (#738),
asset picker search (#740), concurrent-edit detection (#742), env-locked
settings (#739), an accessibility baseline (#741), three security fixes
with advisories (#737, #755, #759), and a pre-release quality pass
(#780–#797).

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: ralksta <ralksta@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Ralf <rohlenbo79@gmail.com>
Co-authored-by: Paul Thies <110185070+ImScheinox@users.noreply.github.com>
Co-authored-by: lancetm714 <108419425+lancetm714@users.noreply.github.com>
Co-authored-by: Lance Martin <lancetaylor95@gmail.com>
2026-09-29 22:50:30 +02:00

58 lines
2.2 KiB
TypeScript

/**
* Per-file memo for the journal and page listings.
*
* `listJournalEntries()` and `listPages()` run on every public page render —
* the header nav asks whether a journal exists and which pages to link — and
* used to read and parse every markdown file each time. The parse is the
* expensive part and depends only on the file's bytes, so it is kept here,
* keyed on the file's identity and stamp: inode, size and mtime in
* nanoseconds.
*
* An admin save goes through atomicWrite (temp file + rename), which always
* produces a new inode, so a save is seen by the very next listing, in this
* module instance or any other. An edit in place changes mtime. The stat is
* taken before the read, so a write landing between the two at worst stores
* the new bytes under the old stamp — which the next stat no longer matches,
* so the file is simply read again. Stale bytes under a current stamp cannot
* happen.
*
* Values are handed out as copies: a caller that mutates an entry must not
* change what the next request sees.
*/
import fs from 'fs/promises';
interface Cached<T> {
stamp: string;
value: T;
}
export class ParsedFileCache<T> {
private readonly entries = new Map<string, Cached<T>>();
constructor(private readonly parse: (raw: string) => T) {}
/**
* Parsed contents of `filePath`, from the memo when the file is unchanged.
* Throws like `fs.readFile` for a missing or unreadable file, and like
* `parse` for one that does not parse — neither outcome is cached.
*/
async read(filePath: string): Promise<T> {
const st = await fs.stat(filePath, { bigint: true });
const stamp = `${st.ino}:${st.size}:${st.mtimeNs}`;
const hit = this.entries.get(filePath);
if (hit && hit.stamp === stamp) return structuredClone(hit.value);
const value = this.parse(await fs.readFile(filePath, 'utf8'));
this.entries.set(filePath, { stamp, value });
return structuredClone(value);
}
/** Drop entries for files a listing no longer found, so deletes do not pile up. */
retain(filePaths: Set<string>): void {
for (const key of this.entries.keys()) {
if (!filePaths.has(key)) this.entries.delete(key);
}
}
}