A tool for parsing traffic on the jetstream and applying a moderation workstream based on regexp based rules
session.ts#
This 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.
SESSION_FILE_PATH#
- A constant that defines the path to the session file:
.sessionin the current working directory.
SessionData Interface#
- Defines the structure of the session object that is saved to and loaded from the file. It includes essential fields like
accessJwt,refreshJwt,did, andhandle.
Key Functions#
loadSession(): SessionData | null#
- Purpose: To load and validate the session from the
.sessionfile. - Logic:
- It checks if the
.sessionfile exists. If not, it returnsnull. - It reads the file content, parses it as JSON, and casts it to the
SessionDatatype. - It performs a basic validation to ensure the essential JWT and DID fields are present. If not, it logs a warning and returns
null. - If the session is loaded and valid, it returns the
SessionDataobject.
- It checks if the
- 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.
saveSession(session: SessionData): void#
- Purpose: To save the current authentication session to the
.sessionfile. - Usage: This is called from
agent.tswhenever a new session is created (after a fresh login) or an existing session is successfully refreshed. - Logic:
- It serializes the
sessionobject into a formatted JSON string. - It writes the string to the
.sessionfile. - It sets the file permissions to
0o600(-rw-------) usingchmodSyncto ensure the session file, which contains sensitive tokens, is only readable and writable by the user running the application.
- It serializes the
clearSession(): void#
- Purpose: To delete the
.sessionfile from disk. - 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.
- Logic: It checks if the file exists and, if so, deletes it using
unlinkSync.
Dependencies#
node:fs: Provides the file system functions for reading, writing, deleting, and changing permissions of the session file.node:path: Used to construct the absolute path to the session file../logger.js: For logging session management activities and errors.