42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
const DEFAULT_VISIBLE_EDGE = 4;
|
|
|
|
export type RedactableValue = string | null | undefined;
|
|
|
|
export function redactSecret(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
|
return redactValue(value, { visibleEdge, label: "secret" });
|
|
}
|
|
|
|
export function redactPairingCode(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
|
return redactValue(value, { visibleEdge, label: "pairingCode" });
|
|
}
|
|
|
|
export function redactKey(value: RedactableValue, visibleEdge: number = DEFAULT_VISIBLE_EDGE): string {
|
|
return redactValue(value, { visibleEdge, label: "key" });
|
|
}
|
|
|
|
export function redactValue(
|
|
value: RedactableValue,
|
|
options: { visibleEdge?: number; label?: string } = {}
|
|
): string {
|
|
const visibleEdge = options.visibleEdge ?? DEFAULT_VISIBLE_EDGE;
|
|
const label = options.label ?? "value";
|
|
|
|
if (!value) {
|
|
return `<redacted:${label}:empty>`;
|
|
}
|
|
|
|
if (value.length <= visibleEdge * 2) {
|
|
return `<redacted:${label}:${value.length}>`;
|
|
}
|
|
|
|
return `${value.slice(0, visibleEdge)}…${value.slice(-visibleEdge)} <redacted:${label}:${value.length}>`;
|
|
}
|
|
|
|
export function safeErrorMessage(error: unknown): string {
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
|
|
return String(error);
|
|
}
|