Files
Moltbot/src/logging/redact-bounded.ts
2026-03-02 16:47:00 +00:00

27 lines
813 B
TypeScript

export const REDACT_REGEX_CHUNK_THRESHOLD = 32_768;
export const REDACT_REGEX_CHUNK_SIZE = 16_384;
type BoundedRedactOptions = {
chunkThreshold?: number;
chunkSize?: number;
};
export function replacePatternBounded(
text: string,
pattern: RegExp,
replacer: Parameters<string["replace"]>[1],
options?: BoundedRedactOptions,
): string {
const chunkThreshold = options?.chunkThreshold ?? REDACT_REGEX_CHUNK_THRESHOLD;
const chunkSize = options?.chunkSize ?? REDACT_REGEX_CHUNK_SIZE;
if (chunkThreshold <= 0 || chunkSize <= 0 || text.length <= chunkThreshold) {
return text.replace(pattern, replacer);
}
let output = "";
for (let index = 0; index < text.length; index += chunkSize) {
output += text.slice(index, index + chunkSize).replace(pattern, replacer);
}
return output;
}