* 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, )
| 5189 | * escapes are replaced with spaces and the result is trimmed. |
| 5190 | */ |
| 5191 | function extractJsonStringFieldPrefix( |
| 5192 | text: string, |
| 5193 | key: string, |
| 5194 | maxLen: number, |
| 5195 | ): string { |
| 5196 | const patterns = [`"${key}":"`, `"${key}": "`] |
| 5197 | for (const pattern of patterns) { |
| 5198 | const idx = text.indexOf(pattern) |
| 5199 | if (idx < 0) continue |
| 5200 | |
| 5201 | const valueStart = idx + pattern.length |
| 5202 | // Grab up to maxLen characters from the value, stopping at closing quote |
| 5203 | let i = valueStart |
| 5204 | let collected = 0 |
| 5205 | while (i < text.length && collected < maxLen) { |
| 5206 | if (text[i] === '\\') { |
| 5207 | i += 2 // skip escaped char |
| 5208 | collected++ |
| 5209 | continue |
| 5210 | } |
| 5211 | if (text[i] === '"') break |
| 5212 | i++ |
| 5213 | collected++ |
| 5214 | } |
| 5215 | const raw = text.slice(valueStart, i) |
| 5216 | return raw.replace(/\\n/g, ' ').replace(/\\t/g, ' ').trim() |
| 5217 | } |
| 5218 | return '' |
| 5219 | } |
| 5220 | |
| 5221 | /** |
| 5222 | * Deduplicates logs by sessionId, keeping the entry with the newest |