A tool for parsing traffic on the jetstream and applying a moderation workstream based on regexp based rules
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 44 lines 2.7 kB view raw view rendered
1# `session.ts` 2 3This module provides simple file-based session persistence. It allows the application to save its authentication session to disk and load it again on the next startup. This avoids the need to perform a fresh login every time the bot is restarted, making startup faster and reducing unnecessary authentication requests. 4 5## `SESSION_FILE_PATH` 6 7- A constant that defines the path to the session file: `.session` in the current working directory. 8 9## `SessionData` Interface 10 11- Defines the structure of the session object that is saved to and loaded from the file. It includes essential fields like `accessJwt`, `refreshJwt`, `did`, and `handle`. 12 13## Key Functions 14 15### `loadSession(): SessionData | null` 16 17- **Purpose**: To load and validate the session from the `.session` file. 18- **Logic**: 19 1. It checks if the `.session` file exists. If not, it returns `null`. 20 2. It reads the file content, parses it as JSON, and casts it to the `SessionData` type. 21 3. It performs a basic validation to ensure the essential JWT and DID fields are present. If not, it logs a warning and returns `null`. 22 4. If the session is loaded and valid, it returns the `SessionData` object. 23- **Error Handling**: If any part of the process fails (e.g., file read error, JSON parse error), it logs the error and returns `null`, forcing a fresh authentication. 24 25### `saveSession(session: SessionData): void` 26 27- **Purpose**: To save the current authentication session to the `.session` file. 28- **Usage**: This is called from `agent.ts` whenever a new session is created (after a fresh login) or an existing session is successfully refreshed. 29- **Logic**: 30 1. It serializes the `session` object into a formatted JSON string. 31 2. It writes the string to the `.session` file. 32 3. It sets the file permissions to `0o600` (`-rw-------`) using `chmodSync` to ensure the session file, which contains sensitive tokens, is only readable and writable by the user running the application. 33 34### `clearSession(): void` 35 36- **Purpose**: To delete the `.session` file from disk. 37- **Usage**: This function is exported but does not appear to be actively used in the current application flow. It could be used to implement a "logout" feature. 38- **Logic**: It checks if the file exists and, if so, deletes it using `unlinkSync`. 39 40## Dependencies 41 42- **`node:fs`**: Provides the file system functions for reading, writing, deleting, and changing permissions of the session file. 43- **`node:path`**: Used to construct the absolute path to the session file. 44- **`./logger.js`**: For logging session management activities and errors.