Creates a new cache manager.
OptionalcustomDir: stringOptional custom cache directory.
If not provided, defaults to ~/.cache/libts-csrfx-auth.
The directory is created automatically when save is called.
The cache file name is always session.json.
Saves session data to disk.
The session data to save.
A promise that resolves when the write operation completes.
Creates the cache directory if it does not exist (using recursive: true).
Overwrites any existing file at the same path.
The session is serialized to JSON with 2‑space indentation for readability.
Loads session data from disk.
The stored session, or null if the file does not exist, is corrupted,
or cannot be read for any reason.
Updates the CSRF token in the cached session.
The new CSRF token to store.
A promise that resolves when the update is complete.
If no session is cached, this method does nothing (no error is thrown).
The timestamp of the session is updated to the current time.
Internally uses sessionWithCsrfToken and save.
Loads the cached session only if it is fresh (age < maxAgeMs).
Maximum allowed age in milliseconds.
The session if it exists and is fresh, otherwise null.
This is a convenience method that combines load and isSessionFresh.
If the session exists but is older than maxAgeMs, it returns null.
// Only use session if it's less than 1 hour old
const session = await cache.loadFresh(3600000);
if (session) {
// session is valid
}
Manages reading, writing, and deleting session cache on disk.
Remarks
The cache is stored as JSON in
~/.cache/libts-csrfx-auth/session.json(or a custom directory if provided). All operations are asynchronous and
use Node.js
fs/promisesAPIs.Thread safety: This class is not designed for concurrent access from
multiple processes. If you need to share sessions across processes, consider
using a database or distributed cache.
Example
Since
0.1.1