< Summary - Envilder Core (TypeScript)

Information
Class: src/envilder/core/domain/EnvironmentVariable.ts
Assembly: Default
File(s): src/envilder/core/domain/EnvironmentVariable.ts
Tag: 151_24479375065
Line coverage
93%
Covered lines: 15
Uncovered lines: 1
Coverable lines: 16
Total lines: 82
Line coverage: 93.7%
Branch coverage
92%
Covered branches: 12
Total branches: 13
Branch coverage: 92.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/domain/EnvironmentVariable.ts

#LineLine coverage
 1/**
 2 * Represents an environment variable with validation and business rules.
 3 */
 4export class EnvironmentVariable {
 5  private readonly _name: string;
 6  private readonly _value: string;
 7  private readonly _isSecret: boolean;
 8
 9  /**
 10   * Creates a new environment variable
 11   *
 12   * @param name - The name of the environment variable
 13   * @param value - The value of the environment variable
 14   * @param isSecret - Whether this variable should be treated as sensitive information
 15   */
 16  constructor(name: string, value: string, isSecret: boolean = false) {
 4417    this.validate(name, value);
 4418    this._name = name;
 4419    this._value = value;
 4420    this._isSecret = isSecret;
 21  }
 22
 23  /**
 24   * Gets the name of the environment variable
 25   */
 26  get name(): string {
 827    return this._name;
 28  }
 29
 30  /**
 31   * Gets the value of the environment variable
 32   */
 33  get value(): string {
 134    return this._value;
 35  }
 36
 37  /**
 38   * Gets whether this variable is sensitive information
 39   */
 40  get isSecret(): boolean {
 241    return this._isSecret;
 42  }
 43
 44  /**
 45   * Returns a masked representation of the value for logging
 46   */
 47  get maskedValue(): string {
 2448    if (!this._isSecret) {
 049      return this._value;
 50    }
 51
 2452    return EnvironmentVariable.mask(this._value, 10);
 53  }
 54
 55  /**
 56   * Returns a masked representation of a secret path for safe logging.
 57   */
 58  static maskSecretPath(path: string): string {
 1159    return EnvironmentVariable.mask(path, 3);
 60  }
 61
 62  private static mask(value: string, minLengthToShowTail: number): string {
 3563    return value.length > minLengthToShowTail
 64      ? '*'.repeat(value.length - 3) + value.slice(-3)
 65      : '*'.repeat(value.length);
 66  }
 67
 68  /**
 69   * Validates the environment variable
 70   */
 71  private validate(name: string, value: string): void {
 4472    if (!name || name.trim() === '') {
 273      throw new Error('Environment variable name cannot be empty');
 74    }
 75
 4276    if (value === undefined || value === null) {
 277      throw new Error(
 78        `Value for environment variable ${name} cannot be null or undefined`,
 79      );
 80    }
 81  }
 82}