UNPKG

@rbac/rbac

Version:

Blazing Fast, Zero dependency, Hierarchical Role-Based Access Control for Node.js

78 lines (51 loc) 5.09 kB
# Requirements Document ## Introduction This specification addresses the issue where the default logger displays raw ANSI escape codes in environments that don't support colored terminal output. The system currently uses hardcoded ANSI escape codes for colored logging, which results in unreadable output showing unicode escape sequences (e.g., `\x1b[1;32m`) in non-color-supporting environments such as certain CI systems, Windows terminals without ANSI support, or when output is redirected to files. The solution will implement automatic color support detection and gracefully fall back to plain text output when colors are not available, ensuring readable logs across all environments. ## Glossary - **Logger**: The `defaultLogger` function in `src/helpers.ts` that outputs RBAC permission check results - **ANSI_Escape_Codes**: Special character sequences (e.g., `\x1b[1;32m`) used to control terminal text formatting and colors - **Color_Support_Detection**: The process of determining whether the current environment can display ANSI color codes - **TTY**: A text terminal interface; when stdout is a TTY, it typically supports interactive features like colors - **CI_Environment**: Continuous Integration systems that may or may not support colored output - **Plain_Text_Output**: Log output without any ANSI escape codes or color formatting ## Requirements ### Requirement 1: Detect Color Support **User Story:** As a developer, I want the logger to automatically detect if my environment supports colors, so that I see properly formatted output regardless of where the code runs. #### Acceptance Criteria 1. WHEN the Logger initializes, THE System SHALL check if stdout is connected to a TTY 2. WHEN the Logger initializes, THE System SHALL check for the NO_COLOR environment variable 3. WHEN the Logger initializes, THE System SHALL check for the FORCE_COLOR environment variable 4. WHEN the Logger initializes, THE System SHALL check for common CI environment variables that indicate color support 5. WHEN FORCE_COLOR is set to a truthy value, THE System SHALL enable color output regardless of other conditions 6. WHEN NO_COLOR is set to any value, THE System SHALL disable color output regardless of other conditions except FORCE_COLOR 7. IF NO_COLOR is set AND FORCE_COLOR is set, THEN THE System SHALL prioritize FORCE_COLOR ### Requirement 2: Conditional Color Application **User Story:** As a developer, I want the logger to use colors only when supported, so that my logs are always readable. #### Acceptance Criteria 1. WHEN color support is detected, THE Logger SHALL apply ANSI escape codes to log output 2. WHEN color support is not detected, THE Logger SHALL output plain text without ANSI escape codes 3. WHILE outputting logs, THE Logger SHALL maintain consistent formatting structure regardless of color support 4. THE Logger SHALL preserve all information content in both colored and plain text modes ### Requirement 3: Maintain Existing Logger Interface **User Story:** As a developer using this library, I want the logger changes to be backward compatible, so that my existing code continues to work without modifications. #### Acceptance Criteria 1. THE Logger SHALL maintain the same function signature as the current `defaultLogger` function 2. THE Logger SHALL accept the same parameters: role (string), operation (string | RegExp), and result (boolean) 3. THE Logger SHALL produce output with the same information structure as the current implementation 4. WHEN called with the same inputs, THE Logger SHALL produce visually equivalent output in color-supporting environments ### Requirement 4: Handle Edge Cases **User Story:** As a developer, I want the logger to handle unusual environments gracefully, so that it never crashes or produces corrupted output. #### Acceptance Criteria 1. WHEN stdout is undefined or null, THE System SHALL default to plain text output 2. WHEN environment variables contain unexpected values, THE System SHALL handle them safely without throwing errors 3. WHEN the color detection logic encounters an error, THE System SHALL default to plain text output 4. THE System SHALL cache the color support detection result to avoid repeated checks ### Requirement 5: Support Standard Color Detection Conventions **User Story:** As a developer, I want the logger to follow standard conventions for color detection, so that it behaves consistently with other tools in my environment. #### Acceptance Criteria 1. THE System SHALL respect the NO_COLOR convention as defined by no-color.org 2. THE System SHALL respect the FORCE_COLOR convention used by popular Node.js tools 3. WHEN running in known CI environments (GITHUB_ACTIONS, GITLAB_CI, CIRCLECI, etc.), THE System SHALL check their specific color support indicators 4. THE System SHALL use TTY detection as the primary indicator when environment variables are not set 5. WHEN stdout.isTTY is false, THE System SHALL default to plain text output unless FORCE_COLOR is set