@logtape/redaction
Version:
Redact sensitive data from log messages
145 lines (143 loc) • 4.61 kB
text/typescript
import { RedactionTraversalOptions } from "./traversal.cjs";
import { ConsoleFormatter, TextFormatter } from "@logtape/logtape";
//#region src/pattern.d.ts
/**
* A redaction pattern, which is a pair of regular expression and replacement
* string or function.
* @since 0.10.0
*/
interface RedactionPattern {
/**
* The regular expression to match against. Note that it must have the
* `g` (global) flag set, otherwise it will throw a `TypeError`.
*/
readonly pattern: RegExp;
/**
* The replacement string or function. If the replacement is a function,
* it will be called with the matched string and any capture groups (the same
* signature as `String.prototype.replaceAll()`).
*/
readonly replacement: string | ((match: string, ...rest: readonly any[]) => string);
}
/**
* A redaction pattern for email addresses.
* @since 0.10.0
*/
declare const EMAIL_ADDRESS_PATTERN: RedactionPattern;
/**
* A redaction pattern for credit card numbers (including American Express).
* @since 0.10.0
*/
declare const CREDIT_CARD_NUMBER_PATTERN: RedactionPattern;
/**
* A redaction pattern for U.S. Social Security numbers.
* @since 0.10.0
*/
declare const US_SSN_PATTERN: RedactionPattern;
/**
* A redaction pattern for South Korean resident registration numbers
* (住民登錄番號).
* @since 0.10.0
*/
declare const KR_RRN_PATTERN: RedactionPattern;
/**
* A redaction pattern for JSON Web Tokens (JWT).
* @since 0.10.0
*/
declare const JWT_PATTERN: RedactionPattern;
/**
* A list of {@link RedactionPattern}s.
* @since 0.10.0
*/
type RedactionPatterns = readonly RedactionPattern[];
/**
* Options for pattern-based redaction.
* @since 2.2.0
*/
interface PatternRedactionOptions extends RedactionTraversalOptions {
/**
* Maximum recursion depth for object and array traversal.
* @default `20`
*/
readonly maxDepth?: number;
/**
* Maximum number of properties or array elements to process per object.
* @default `1000`
*/
readonly maxProperties?: number;
}
/**
* Applies data redaction to a {@link TextFormatter}.
*
* Note that there are some built-in redaction patterns:
*
* - {@link CREDIT_CARD_NUMBER_PATTERN}
* - {@link EMAIL_ADDRESS_PATTERN}
* - {@link JWT_PATTERN}
* - {@link KR_RRN_PATTERN}
* - {@link US_SSN_PATTERN}
*
* @example
* ```ts
* import { getFileSink } from "@logtape/file";
* import { getAnsiColorFormatter } from "@logtape/logtape";
* import {
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* redactByPattern,
* } from "@logtape/redaction";
*
* const formatter = redactByPattern(getAnsiConsoleFormatter(), [
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* ]);
* const sink = getFileSink("my-app.log", { formatter });
* ```
* @param formatter The text formatter to apply redaction to.
* @param patterns The redaction patterns to apply.
* @param options Options for bounding recursive traversal of formatter output.
* @returns The redacted text formatter.
* @since 0.10.0
*/
declare function redactByPattern(formatter: TextFormatter, patterns: RedactionPatterns, options?: PatternRedactionOptions): TextFormatter;
/**
* Applies data redaction to a {@link ConsoleFormatter}.
*
* Note that there are some built-in redaction patterns:
*
* - {@link CREDIT_CARD_NUMBER_PATTERN}
* - {@link EMAIL_ADDRESS_PATTERN}
* - {@link JWT_PATTERN}
* - {@link KR_RRN_PATTERN}
* - {@link US_SSN_PATTERN}
*
* @example
* ```ts
* import { defaultConsoleFormatter, getConsoleSink } from "@logtape/logtape";
* import {
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* redactByPattern,
* } from "@logtape/redaction";
*
* const formatter = redactByPattern(defaultConsoleFormatter, [
* CREDIT_CARD_NUMBER_PATTERN,
* EMAIL_ADDRESS_PATTERN,
* JWT_PATTERN,
* ]);
* const sink = getConsoleSink({ formatter });
* ```
* @param formatter The console formatter to apply redaction to.
* @param patterns The redaction patterns to apply.
* @param options Options for bounding recursive traversal of formatter output.
* @returns The redacted console formatter.
* @since 0.10.0
*/
declare function redactByPattern(formatter: ConsoleFormatter, patterns: RedactionPatterns, options?: PatternRedactionOptions): ConsoleFormatter;
//# sourceMappingURL=pattern.d.ts.map
//#endregion
export { CREDIT_CARD_NUMBER_PATTERN, EMAIL_ADDRESS_PATTERN, JWT_PATTERN, KR_RRN_PATTERN, PatternRedactionOptions, RedactionPattern, RedactionPatterns, US_SSN_PATTERN, redactByPattern };
//# sourceMappingURL=pattern.d.cts.map