CSRF-X Auth Library - v1.0.3
    Preparing search index...

    Class CacheManager

    Manages reading, writing, and deleting session cache on disk.

    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/promises APIs.

    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.

    const cache = new CacheManager();
    await cache.save(sessionData);
    const loaded = await cache.load();

    @public

    0.1.1

    Index

    Accessors

    • get cacheFilePath(): string

      Returns the full filesystem path to the session cache file.

      Returns string

      console.log(cache.cacheFilePath); // "/home/user/.cache/libts-csrfx-auth/session.json"
      
    • get cacheDirPath(): string

      Returns the directory containing the session cache.

      Returns string

      console.log(cache.cacheDirPath); // "/home/user/.cache/libts-csrfx-auth"
      

    Constructors

    • Creates a new cache manager.

      Parameters

      • OptionalcustomDir: string

        Optional custom cache directory.
        If not provided, defaults to ~/.cache/libts-csrfx-auth.

      Returns CacheManager

      The directory is created automatically when save is called.
      The cache file name is always session.json.

      // Default location (user home directory)
      const cache = new CacheManager();
      // Custom directory
      const cache = new CacheManager('./my-app-cache');

    Methods

    • Saves session data to disk.

      Parameters

      Returns Promise<void>

      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.

      await cache.save({
      cookies: [...],
      csrfToken: 'abc123',
      loggedIn: true,
      timestamp: Date.now()
      });
    • Loads session data from disk.

      Returns Promise<SessionData | null>

      The stored session, or null if the file does not exist, is corrupted,
      or cannot be read for any reason.

      If the file exists but contains invalid JSON, the function catches the error
      and returns null. No error is thrown to the caller.

      const session = await cache.load();
      if (session) {
      console.log(`Session from ${new Date(session.timestamp)}`);
      }
    • Deletes the cached session file if it exists.

      Returns Promise<void>

      A promise that resolves when the deletion is complete (or if the file did not exist).

      Does nothing if the file is not present. No error is thrown.

      await cache.clear(); // removes session.json
      
    • Updates the CSRF token in the cached session.

      Parameters

      • newToken: string

        The new CSRF token to store.

      Returns Promise<void>

      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.

      await cache.updateCsrfToken('new_token_456');
      
    • Loads the cached session only if it is fresh (age < maxAgeMs).

      Parameters

      • maxAgeMs: number

        Maximum allowed age in milliseconds.

      Returns Promise<SessionData | null>

      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
      }