| | | 1 | | import { |
| | | 2 | | invalidEnvironmentVariableNameMessage, |
| | | 3 | | isValidEnvironmentVariableName, |
| | | 4 | | } from './EnvironmentVariableName.js'; |
| | | 5 | | import { InvalidArgumentError } from './errors/DomainErrors.js'; |
| | | 6 | | |
| | | 7 | | /** |
| | | 8 | | * Represents an environment variable with validation and business rules. |
| | | 9 | | */ |
| | | 10 | | export 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) { |
| | 50 | 23 | | this.validate(name, value); |
| | 50 | 24 | | this._name = name; |
| | 50 | 25 | | this._value = value; |
| | 50 | 26 | | this._isSecret = isSecret; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /** |
| | | 30 | | * Gets the name of the environment variable |
| | | 31 | | */ |
| | | 32 | | get name(): string { |
| | 1 | 33 | | return this._name; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /** |
| | | 37 | | * Gets the value of the environment variable |
| | | 38 | | */ |
| | | 39 | | get value(): string { |
| | 1 | 40 | | return this._value; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /** |
| | | 44 | | * Gets whether this variable is sensitive information |
| | | 45 | | */ |
| | | 46 | | get isSecret(): boolean { |
| | 2 | 47 | | return this._isSecret; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /** |
| | | 51 | | * Returns a masked representation of the value for logging |
| | | 52 | | */ |
| | | 53 | | get maskedValue(): string { |
| | 28 | 54 | | if (!this._isSecret) { |
| | 0 | 55 | | return this._value; |
| | | 56 | | } |
| | | 57 | | |
| | 28 | 58 | | 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 { |
| | 42 | 65 | | return EnvironmentVariable.mask(path, 3); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private static mask(value: string, minLengthToShowTail: number): string { |
| | 70 | 69 | | 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 { |
| | 50 | 78 | | if (!isValidEnvironmentVariableName(name)) { |
| | 4 | 79 | | throw new InvalidArgumentError( |
| | | 80 | | invalidEnvironmentVariableNameMessage(name), |
| | | 81 | | ); |
| | | 82 | | } |
| | | 83 | | |
| | 46 | 84 | | if (value === undefined || value === null) { |
| | 2 | 85 | | throw new Error( |
| | | 86 | | `Value for environment variable ${name} cannot be null or undefined`, |
| | | 87 | | ); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |