* Like extractJsonStringField but returns the first `maxLen` characters of the * value even when the closing quote is missing (truncated buffer). Newline * escapes are replaced with spaces and the result is trimmed.
( text: string, key: string, maxLen: number, )
| 5032 | * escapes are replaced with spaces and the result is trimmed. |
| 5033 | */ |
| 5034 | function extractJsonStringFieldPrefix( |
| 5035 | text: string, |
| 5036 | key: string, |
| 5037 | maxLen: number, |
| 5038 | ): string { |
| 5039 | const patterns = [`"${key}":"`, `"${key}": "`] |
| 5040 | for (const pattern of patterns) { |
| 5041 | const idx = text.indexOf(pattern) |
| 5042 | if (idx < 0) continue |
| 5043 | |
| 5044 | const valueStart = idx + pattern.length |
| 5045 | // Grab up to maxLen characters from the value, stopping at closing quote |
| 5046 | let i = valueStart |
| 5047 | let collected = 0 |
| 5048 | while (i < text.length && collected < maxLen) { |
| 5049 | if (text[i] === '\\') { |
| 5050 | i += 2 // skip escaped char |
| 5051 | collected++ |
| 5052 | continue |
| 5053 | } |
| 5054 | if (text[i] === '"') break |
| 5055 | i++ |
| 5056 | collected++ |
| 5057 | } |
| 5058 | const raw = text.slice(valueStart, i) |
| 5059 | return raw.replace(/\\n/g, ' ').replace(/\\t/g, ' ').trim() |
| 5060 | } |
| 5061 | return '' |
| 5062 | } |
| 5063 | |
| 5064 | /** |
| 5065 | * Deduplicates logs by sessionId, keeping the entry with the newest |