< Summary - Envilder CLI

Information
Class: src/envilder/core/domain/EnvironmentVariable.ts
Assembly: Default
File(s): src/envilder/core/domain/EnvironmentVariable.ts
Tag: 561_41292037308
Line coverage
93%
Covered lines: 15
Uncovered lines: 1
Coverable lines: 16
Total lines: 97
Line coverage: 93.7%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/domain/EnvironmentVariable.ts

#LineLine coverage
 1import {
 2  invalidEnvironmentVariableNameMessage,
 3  isValidEnvironmentVariableName,
 4} from './EnvironmentVariableName.js';
 5import { InvalidArgumentError } from './errors/DomainErrors.js';
 6
 7/**
 8 * Represents an environment variable with validation and business rules.
 9 */
 10export class EnvironmentVariable {
 11  private readonly _name: string;
 12  private readonly _value: string;
 13
 14  /**
 15   * Creates a new environment variable
 16   *
 17   * @param name - The name of the environment variable
 18   * @param value - The value of the environment variable
 19   */
 20  constructor(name: string, value: string) {
 18721    this.validate(name, value);
 18722    this._name = name;
 18723    this._value = value;
 24  }
 25
 26  static secret(name: string, value: string): EnvironmentVariable {
 18027    return new SecretEnvironmentVariable(name, value);
 28  }
 29
 30  /**
 31   * Gets the name of the environment variable
 32   */
 33  get name(): string {
 134    return this._name;
 35  }
 36
 37  /**
 38   * Gets the value of the environment variable
 39   */
 40  get value(): string {
 15741    return this._value;
 42  }
 43
 44  /**
 45   * Gets whether this variable is sensitive information
 46   */
 47  get isSecret(): boolean {
 148    return false;
 49  }
 50
 51  /**
 52   * Returns a masked representation of the value for logging
 53   */
 54  get maskedValue(): string {
 055    return this._value;
 56  }
 57
 58  /**
 59   * Returns a masked representation of a secret path for safe logging.
 60   */
 61  static maskSecretPath(path: string): string {
 7262    return EnvironmentVariable.mask(path, 3);
 63  }
 64
 65  protected static mask(value: string, minLengthToShowTail: number): string {
 22866    return value.length > minLengthToShowTail
 67      ? '*'.repeat(value.length - 3) + value.slice(-3)
 68      : '*'.repeat(value.length);
 69  }
 70
 71  /**
 72   * Validates the environment variable
 73   */
 74  private validate(name: string, value: string): void {
 18775    if (!isValidEnvironmentVariableName(name)) {
 576      throw new InvalidArgumentError(
 77        invalidEnvironmentVariableNameMessage(name),
 78      );
 79    }
 80
 18281    if (value === undefined || value === null) {
 282      throw new Error(
 83        `Value for environment variable ${name} cannot be null or undefined`,
 84      );
 85    }
 86  }
 87}
 88
 89class SecretEnvironmentVariable extends EnvironmentVariable {
 90  override get isSecret(): boolean {
 191    return true;
 92  }
 93
 94  override get maskedValue(): string {
 15695    return EnvironmentVariable.mask(this.value, 10);
 96  }
 97}