@rollercoaster-dev/rd-logger
Version:
A neurodivergent-friendly logger for Rollercoaster.dev projects
56 lines (55 loc) • 1.6 kB
JavaScript
/**
* A wrapper class for sensitive values that prevents them from being accidentally logged
*/
export class SensitiveValue {
/**
* Create a new SensitiveValue
* @param value The sensitive value to wrap
* @param redactedValue The value to show instead of the actual value (default: '[REDACTED]')
*/
constructor(value, redactedValue = '[REDACTED]') {
this.value = value;
this.redactedValue = redactedValue;
}
/**
* Get the actual sensitive value
* Only use this method when you absolutely need the actual value for operations
* @returns The actual sensitive value
*/
getValue() {
return this.value;
}
/**
* Override toString to return the redacted value
*/
toString() {
return this.redactedValue;
}
/**
* Override toJSON to return the redacted value
*/
toJSON() {
return this.redactedValue;
}
/**
* Override Node.js util.inspect custom handler to return the redacted value
*/
[Symbol.for('nodejs.util.inspect.custom')]() {
return this.redactedValue;
}
/**
* Override valueOf to return the redacted value
*/
valueOf() {
return this.redactedValue;
}
/**
* Factory method to create a new SensitiveValue
* @param value The sensitive value to wrap
* @param redactedValue The value to show instead of the actual value
* @returns A new SensitiveValue instance
*/
static from(value, redactedValue) {
return new SensitiveValue(value, redactedValue);
}
}