fix(core): truncate large telemetry log entries (#16769)

This commit is contained in:
Sehoon Shon
2026-01-16 14:30:48 -05:00
committed by GitHub
parent 59da61602f
commit 42fd647373
4 changed files with 247 additions and 1 deletions

View File

@@ -5,7 +5,7 @@
*/
import { describe, it, expect } from 'vitest';
import { safeLiteralReplace } from './textUtils.js';
import { safeLiteralReplace, truncateString } from './textUtils.js';
describe('safeLiteralReplace', () => {
it('returns original string when oldString empty or not found', () => {
@@ -77,3 +77,25 @@ describe('safeLiteralReplace', () => {
expect(safeLiteralReplace('abc', 'b', '$$')).toBe('a$$c');
});
});
describe('truncateString', () => {
it('should not truncate string shorter than maxLength', () => {
expect(truncateString('abc', 5)).toBe('abc');
});
it('should not truncate string equal to maxLength', () => {
expect(truncateString('abcde', 5)).toBe('abcde');
});
it('should truncate string longer than maxLength and append default suffix', () => {
expect(truncateString('abcdef', 5)).toBe('abcde...[TRUNCATED]');
});
it('should truncate string longer than maxLength and append custom suffix', () => {
expect(truncateString('abcdef', 5, '...')).toBe('abcde...');
});
it('should handle empty string', () => {
expect(truncateString('', 5)).toBe('');
});
});

View File

@@ -53,3 +53,21 @@ export function isBinary(
// If no NULL bytes were found in the sample, we assume it's text.
return false;
}
/**
* Truncates a string to a maximum length, appending a suffix if truncated.
* @param str The string to truncate.
* @param maxLength The maximum length of the string.
* @param suffix The suffix to append if truncated (default: '...[TRUNCATED]').
* @returns The truncated string.
*/
export function truncateString(
str: string,
maxLength: number,
suffix = '...[TRUNCATED]',
): string {
if (str.length <= maxLength) {
return str;
}
return str.slice(0, maxLength) + suffix;
}