UNPKG

autotel

Version:
35 lines (31 loc) 1.21 kB
/** Standalone string redaction for use outside the span processor pipeline. */ import { REDACTOR_PRESETS, type AttributeRedactorConfig, type AttributeRedactorPreset, type ValuePatternConfig, } from './attribute-redacting-processor'; export type StringRedactor = (value: string) => string; export function createStringRedactor( config: AttributeRedactorConfig | AttributeRedactorPreset, ): StringRedactor { const resolved = typeof config === 'string' ? REDACTOR_PRESETS[config] : config; const valuePatterns: ValuePatternConfig[] = resolved.valuePatterns ?? []; const defaultReplacement = resolved.replacement ?? '[REDACTED]'; return (value: string): string => { let result = value; for (const { pattern, replacement, mask } of valuePatterns) { pattern.lastIndex = 0; // Smart masks (e.g. email → a***@***.com) take precedence over the // static replacement so callers see the same output as the // span-attribute redactor does. if (mask) { result = result.replaceAll(pattern, (match) => mask(match)); } else { result = result.replaceAll(pattern, replacement ?? defaultReplacement); } } return result; }; }