|
| 1 | +import { rightTrim } from './string'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Maximum string length before appending ellipsis |
| 5 | + */ |
| 6 | +const MAX_STRING_LENGTH = 200; |
| 7 | + |
| 8 | +/** |
| 9 | + * Maximum number of object keys to keep, the rest are reported via META_FIELD |
| 10 | + */ |
| 11 | +const MAX_OBJECT_KEYS_COUNT = 20; |
| 12 | + |
| 13 | +/** |
| 14 | + * Key added to an object to report how many of its keys were skipped |
| 15 | + */ |
| 16 | +const META_FIELD = '__meta'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Maximum depth of sanitized objects |
| 20 | + */ |
| 21 | +const MAX_DEPTH = 5; |
| 22 | + |
| 23 | +/** |
| 24 | + * Maximum length of sanitized arrays |
| 25 | + */ |
| 26 | +const MAX_ARRAY_LENGTH = 10; |
| 27 | + |
| 28 | +/** |
| 29 | + * Checks that the value is a plain object |
| 30 | + * |
| 31 | + * @param target - value to check |
| 32 | + */ |
| 33 | +function isPlainObject(target: unknown): target is Record<string, unknown> { |
| 34 | + return Object.prototype.toString.call(target) === '[object Object]'; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Values that can be tracked by WeakSet to detect circular references |
| 39 | + */ |
| 40 | +type ObjectLike = Record<string, unknown> | unknown[]; |
| 41 | + |
| 42 | +/** |
| 43 | + * Prepares event data for storing: trims long strings, slices long arrays, |
| 44 | + * cuts off extra object keys and replaces too deep objects and circular |
| 45 | + * references with placeholders. |
| 46 | + */ |
| 47 | +export class Sanitizer { |
| 48 | + /** |
| 49 | + * Apply sanitizing for array/object/primitives |
| 50 | + * |
| 51 | + * @param data - any value to sanitize |
| 52 | + * @param depth - current depth of recursion |
| 53 | + * @param seen - already visited objects |
| 54 | + */ |
| 55 | + public static sanitize(data: unknown, depth = 0, seen = new WeakSet<ObjectLike>()): unknown { |
| 56 | + if (data !== null && typeof data === 'object') { |
| 57 | + if (seen.has(data as ObjectLike)) { |
| 58 | + return '<circular>'; |
| 59 | + } |
| 60 | + seen.add(data as ObjectLike); |
| 61 | + } |
| 62 | + |
| 63 | + if (Array.isArray(data)) { |
| 64 | + return Sanitizer.sanitizeArray(data, depth + 1, seen); |
| 65 | + } |
| 66 | + |
| 67 | + if (isPlainObject(data)) { |
| 68 | + return Sanitizer.sanitizeObject(data, depth + 1, seen); |
| 69 | + } |
| 70 | + |
| 71 | + if (typeof data === 'string') { |
| 72 | + return rightTrim(data, MAX_STRING_LENGTH); |
| 73 | + } |
| 74 | + |
| 75 | + return data; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Slices array to the maximum length and sanitizes each element |
| 80 | + * |
| 81 | + * @param arr - array to sanitize |
| 82 | + * @param depth - current depth of recursion |
| 83 | + * @param seen - already visited objects |
| 84 | + */ |
| 85 | + private static sanitizeArray(arr: unknown[], depth: number, seen: WeakSet<ObjectLike>): unknown[] { |
| 86 | + const length = arr.length; |
| 87 | + |
| 88 | + if (length > MAX_ARRAY_LENGTH) { |
| 89 | + arr = arr.slice(0, MAX_ARRAY_LENGTH); |
| 90 | + arr.push(`<${length - MAX_ARRAY_LENGTH} more items...>`); |
| 91 | + } |
| 92 | + |
| 93 | + return arr.map((item) => { |
| 94 | + return Sanitizer.sanitize(item, depth, seen); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sanitizes object values recursively |
| 100 | + * |
| 101 | + * @param data - object to sanitize |
| 102 | + * @param depth - current depth of recursion |
| 103 | + * @param seen - already visited objects |
| 104 | + */ |
| 105 | + private static sanitizeObject( |
| 106 | + data: Record<string, unknown>, |
| 107 | + depth: number, |
| 108 | + seen: WeakSet<ObjectLike> |
| 109 | + ): Record<string, unknown> | '<deep object>' { |
| 110 | + if (depth > MAX_DEPTH) { |
| 111 | + return '<deep object>'; |
| 112 | + } |
| 113 | + |
| 114 | + const keys = Object.keys(data); |
| 115 | + const result: Record<string, unknown> = {}; |
| 116 | + |
| 117 | + for (const key of keys.slice(0, MAX_OBJECT_KEYS_COUNT)) { |
| 118 | + result[key] = Sanitizer.sanitize(data[key], depth, seen); |
| 119 | + } |
| 120 | + |
| 121 | + const skippedKeysCount = keys.length - MAX_OBJECT_KEYS_COUNT; |
| 122 | + |
| 123 | + if (skippedKeysCount > 0) { |
| 124 | + result[META_FIELD] = `${skippedKeysCount} more key(s) skipped`; |
| 125 | + } |
| 126 | + |
| 127 | + return result; |
| 128 | + } |
| 129 | +} |
0 commit comments