< Summary - Envilder CLI

Information
Class: src/envilder/core/domain/EnvironmentVariable.ts
Assembly: Default
File(s): src/envilder/core/domain/EnvironmentVariable.ts
Tag: 502_37137557711
Line coverage
93%
Covered lines: 15
Uncovered lines: 1
Coverable lines: 16
Total lines: 90
Line coverage: 93.7%
Branch coverage
90%
Covered branches: 10
Total branches: 11
Branch coverage: 90.9%
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  private readonly _isSecret: boolean;
 14
 15  /**
 16   * Creates a new environment variable
 17   *
 18   * @param name - The name of the environment variable
 19   * @param value - The value of the environment variable
 20   * @param isSecret - Whether this variable should be treated as sensitive information
 21   */
 22  constructor(name: string, value: string, isSecret: boolean = false) {
 5023    this.validate(name, value);
 5024    this._name = name;
 5025    this._value = value;
 5026    this._isSecret = isSecret;
 27  }
 28
 29  /**
 30   * Gets the name of the environment variable
 31   */
 32  get name(): string {
 133    return this._name;
 34  }
 35
 36  /**
 37   * Gets the value of the environment variable
 38   */
 39  get value(): string {
 140    return this._value;
 41  }
 42
 43  /**
 44   * Gets whether this variable is sensitive information
 45   */
 46  get isSecret(): boolean {
 247    return this._isSecret;
 48  }
 49
 50  /**
 51   * Returns a masked representation of the value for logging
 52   */
 53  get maskedValue(): string {
 2854    if (!this._isSecret) {
 055      return this._value;
 56    }
 57
 2858    return EnvironmentVariable.mask(this._value, 10);
 59  }
 60
 61  /**
 62   * Returns a masked representation of a secret path for safe logging.
 63   */
 64  static maskSecretPath(path: string): string {
 4265    return EnvironmentVariable.mask(path, 3);
 66  }
 67
 68  private static mask(value: string, minLengthToShowTail: number): string {
 7069    return value.length > minLengthToShowTail
 70      ? '*'.repeat(value.length - 3) + value.slice(-3)
 71      : '*'.repeat(value.length);
 72  }
 73
 74  /**
 75   * Validates the environment variable
 76   */
 77  private validate(name: string, value: string): void {
 5078    if (!isValidEnvironmentVariableName(name)) {
 479      throw new InvalidArgumentError(
 80        invalidEnvironmentVariableNameMessage(name),
 81      );
 82    }
 83
 4684    if (value === undefined || value === null) {
 285      throw new Error(
 86        `Value for environment variable ${name} cannot be null or undefined`,
 87      );
 88    }
 89  }
 90}