@logtape/redaction
Version:
Redact sensitive data from log messages
94 lines (92 loc) • 3.46 kB
TypeScript
import { Sink } from "@logtape/logtape";
//#region src/field.d.ts
/**
* The type for a field pattern used in redaction. A string or a regular
* expression that matches field names.
* @since 0.10.0
*/
type FieldPattern = string | RegExp;
/**
* An array of field patterns used for redaction. Each pattern can be
* a string or a regular expression that matches field names.
* @since 0.10.0
*/
type FieldPatterns = FieldPattern[];
/**
* Default field patterns for redaction. These patterns will match
* common sensitive fields such as passwords, tokens, and personal
* information.
* @since 0.10.0
*/
declare const DEFAULT_REDACT_FIELDS: FieldPatterns;
/**
* Options for redacting fields in a {@link LogRecord}. Used by
* the {@link redactByField} function.
* @since 0.10.0
*/
interface FieldRedactionOptions {
/**
* The field patterns to match against. This can be an array of
* strings or regular expressions. If a field matches any of the
* patterns, it will be redacted.
* @defaultValue {@link DEFAULT_REDACT_FIELDS}
*/
readonly fieldPatterns: FieldPatterns;
/**
* The action to perform on the matched fields. If not provided,
* the default action is to delete the field from the properties.
* If a function is provided, it will be called with the
* value of the field, and the return value will be used to replace
* the field in the properties.
* If the action is `"delete"`, the field will be removed from the
* properties.
* @default `"delete"`
*/
readonly action?: "delete" | ((value: unknown) => unknown);
}
/**
* Redacts properties and message values in a {@link LogRecord} based on the
* provided field patterns and action.
*
* Note that it is a decorator which wraps the sink and redacts properties
* and message values before passing them to the sink.
*
* For string templates (e.g., `"Hello, {name}!"`), placeholder names are
* matched against the field patterns to determine which values to redact.
*
* For tagged template literals (e.g., `` `Hello, ${name}!` ``), redaction
* is performed by comparing message values with redacted property values.
*
* @example
* ```ts
* import { getConsoleSink } from "@logtape/logtape";
* import { redactByField } from "@logtape/redaction";
*
* const sink = redactByField(getConsoleSink());
* ```
*
* @param sink The sink to wrap.
* @param options The redaction options.
* @returns The wrapped sink.
* @since 0.10.0
*/
declare function redactByField(sink: Sink | Sink & Disposable | Sink & AsyncDisposable, options?: FieldRedactionOptions | FieldPatterns): Sink | Sink & Disposable | Sink & AsyncDisposable;
/**
* Redacts properties from an object based on specified field patterns.
*
* This function creates a shallow copy of the input object and applies
* redaction rules to its properties. For properties that match the redaction
* patterns, the function either removes them or transforms their values based
* on the provided action.
*
* The redaction process is recursive and will be applied to nested objects
* as well, allowing for deep redaction of sensitive data in complex object
* structures.
* @param properties The properties to redact.
* @param options The redaction options.
* @returns The redacted properties.
* @since 0.10.0
*/
//#endregion
export { DEFAULT_REDACT_FIELDS, FieldPattern, FieldPatterns, FieldRedactionOptions, redactByField };
//# sourceMappingURL=field.d.ts.map