UNPKG

@ogcio/o11y-sdk-react

Version:

Opentelemetry standard instrumentation SDK for React based project

58 lines 2.09 kB
import { _containsEncodedComponents } from "./string-decoding.js"; const decoder = new TextDecoder(); const encoder = new TextEncoder(); /** * Cleans a string by redacting configured PIIs and emitting metrics for redacted values. * * If the string is URL-encoded, it will be decoded before redaction. * * @param {string} value - The input value to sanitize. * @param {string} source - The source context of the input, used in metrics. * @param {BasicRedactor[]} redactors - The string redactors containing the redaction logic. * * @returns {string} The cleaned string with any configured PII replaced by `[REDACTED PII_TYPE]`. */ export function _cleanStringPII(value, source, redactors) { if (typeof value !== "string") { return value; } let kind = "string"; let decodedValue = value; if (_containsEncodedComponents(value)) { decodedValue = decodeURIComponent(value); kind = "url"; } return redactors.reduce((redactedValue, currentRedactor) => currentRedactor.process(redactedValue, source, kind), decodedValue); } export function _recursiveObjectClean(value, source, redactors) { if (typeof value === "string") { return _cleanStringPII(value, source, redactors); } if (typeof value === "number" || typeof value === "boolean" || value == null) { return value; } if (ArrayBuffer.isView(value)) { try { const decoded = decoder.decode(value); const sanitized = _cleanStringPII(decoded, source, redactors); return encoder.encode(sanitized); } catch { return value; } } if (Array.isArray(value)) { return value.map((value) => _recursiveObjectClean(value, source, redactors)); } if (typeof value === "object") { const sanitized = {}; for (const [key, val] of Object.entries(value)) { sanitized[key] = _recursiveObjectClean(val, source, redactors); } return sanitized; } return value; } //# sourceMappingURL=data-structures.js.map