| | | 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 | | |
| | | 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) { |
| | 187 | 21 | | this.validate(name, value); |
| | 187 | 22 | | this._name = name; |
| | 187 | 23 | | this._value = value; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | static secret(name: string, value: string): EnvironmentVariable { |
| | 180 | 27 | | return new SecretEnvironmentVariable(name, value); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /** |
| | | 31 | | * Gets the name of the environment variable |
| | | 32 | | */ |
| | | 33 | | get name(): string { |
| | 1 | 34 | | return this._name; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /** |
| | | 38 | | * Gets the value of the environment variable |
| | | 39 | | */ |
| | | 40 | | get value(): string { |
| | 157 | 41 | | return this._value; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /** |
| | | 45 | | * Gets whether this variable is sensitive information |
| | | 46 | | */ |
| | | 47 | | get isSecret(): boolean { |
| | 1 | 48 | | return false; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /** |
| | | 52 | | * Returns a masked representation of the value for logging |
| | | 53 | | */ |
| | | 54 | | get maskedValue(): string { |
| | 0 | 55 | | 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 { |
| | 72 | 62 | | return EnvironmentVariable.mask(path, 3); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | protected static mask(value: string, minLengthToShowTail: number): string { |
| | 228 | 66 | | 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 { |
| | 187 | 75 | | if (!isValidEnvironmentVariableName(name)) { |
| | 5 | 76 | | throw new InvalidArgumentError( |
| | | 77 | | invalidEnvironmentVariableNameMessage(name), |
| | | 78 | | ); |
| | | 79 | | } |
| | | 80 | | |
| | 182 | 81 | | if (value === undefined || value === null) { |
| | 2 | 82 | | throw new Error( |
| | | 83 | | `Value for environment variable ${name} cannot be null or undefined`, |
| | | 84 | | ); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | class SecretEnvironmentVariable extends EnvironmentVariable { |
| | | 90 | | override get isSecret(): boolean { |
| | 1 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | override get maskedValue(): string { |
| | 156 | 95 | | return EnvironmentVariable.mask(this.value, 10); |
| | | 96 | | } |
| | | 97 | | } |