From 8269f3a65ba9f65e9074a113e87051bd3ae8b673 Mon Sep 17 00:00:00 2001 From: Will Li Date: Mon, 4 Aug 2025 10:06:22 -0700 Subject: [PATCH] fix: address PR review comments - Fix Hindi translation punctuation - Fix race condition by checking stream.destroyed - Optimize newline counting with regex - Performance improvements for large file handling - Defensive programming for end parameter already in place --- src/i18n/locales/hi/tools.json | 2 +- src/integrations/misc/read-partial-content.ts | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/i18n/locales/hi/tools.json b/src/i18n/locales/hi/tools.json index 7840a55d63..7970b36474 100644 --- a/src/i18n/locales/hi/tools.json +++ b/src/i18n/locales/hi/tools.json @@ -3,7 +3,7 @@ "linesRange": " (पंक्तियाँ {{start}}-{{end}})", "definitionsOnly": " (केवल परिभाषाएँ)", "maxLines": " (अधिकतम {{max}} पंक्तियाँ)", - "showingOnlyLines": "कुल {{total}} पंक्तियों में से केवल {{shown}} दिखा रहे हैं। यदि आपको अधिक पंक्तियाँ पढ़नी हैं तो line_range का उपयोग करें", + "showingOnlyLines": "कुल {{total}} पंक्तियों में से केवल {{shown}} दिखा रहे हैं। यदि आपको अधिक पंक्तियाँ पढ़नी हैं तो line_range का उपयोग करें।", "imageTooLarge": "छवि फ़ाइल बहुत बड़ी है ({{size}} MB)। अधिकतम अनुमतित आकार {{max}} MB है।", "imageWithSize": "छवि फ़ाइल ({{size}} KB)" }, diff --git a/src/integrations/misc/read-partial-content.ts b/src/integrations/misc/read-partial-content.ts index e8db809982..34f3585f48 100644 --- a/src/integrations/misc/read-partial-content.ts +++ b/src/integrations/misc/read-partial-content.ts @@ -57,8 +57,8 @@ export async function readPartialContent(filePath: string, maxChars: number): Pr let hasContent = false stream.on("data", (chunk: string | Buffer) => { - // Early exit if stream was already destroyed - if (streamDestroyed) { + // Check stream state immediately + if (streamDestroyed || stream.destroyed) { return } @@ -95,11 +95,8 @@ export async function readPartialContent(filePath: string, maxChars: number): Pr } // Count newlines in the chunk we're adding - for (let i = 0; i < chunkToAdd.length; i++) { - if (chunkToAdd[i] === "\n") { - currentLine++ - } - } + const newlineCount = (chunkToAdd.match(/\n/g) || []).length + currentLine += newlineCount content += chunkToAdd @@ -114,11 +111,8 @@ export async function readPartialContent(filePath: string, maxChars: number): Pr // Recount lines in the final content currentLine = 1 hasContent = content.length > 0 - for (let i = 0; i < content.length; i++) { - if (content[i] === "\n") { - currentLine++ - } - } + const finalNewlineCount = (content.match(/\n/g) || []).length + currentLine += finalNewlineCount } resolve({