From 7ddfa4e529b488b4dc758ba60e76700eca4e837b Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Mon, 3 Nov 2025 15:13:07 -0500 Subject: [PATCH] refactor: remove unnecessary caching from normalizeDataUrlsToFilePaths - Function is now primarily used for testing - Production code uses dual-storage (images + imagesBase64) - Removed redundant cache since same hash always produces same file path - Simplified implementation and removed obsolete test --- .../misc/__tests__/imageDataUrl.spec.ts | 17 --------- src/integrations/misc/imageDataUrl.ts | 37 +++++-------------- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/integrations/misc/__tests__/imageDataUrl.spec.ts b/src/integrations/misc/__tests__/imageDataUrl.spec.ts index 7d42451115..8aef7d69fc 100644 --- a/src/integrations/misc/__tests__/imageDataUrl.spec.ts +++ b/src/integrations/misc/__tests__/imageDataUrl.spec.ts @@ -118,23 +118,6 @@ describe("normalizeDataUrlsToFilePaths", () => { expect(result[1]).toMatch(/\.jpeg$/) // Correct extension from MIME type }) - it("should reuse cached file paths for same data URL", async () => { - const dataUrl = "data:image/png;base64,test123" - - // First call - should write file - const result1 = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath) - expect(fs.writeFile).toHaveBeenCalledTimes(1) - - // Mock file exists for second call - vi.mocked(fs.access).mockResolvedValue(undefined) - - // Second call - should use cached path - const result2 = await normalizeDataUrlsToFilePaths([dataUrl], testGlobalStoragePath) - - expect(result1[0]).toBe(result2[0]) // Same path returned - expect(fs.writeFile).toHaveBeenCalledTimes(1) // Not written again - }) - it("should handle errors gracefully", async () => { const dataUrl = "data:image/png;base64,test" diff --git a/src/integrations/misc/imageDataUrl.ts b/src/integrations/misc/imageDataUrl.ts index 06810aa289..a764b92d83 100644 --- a/src/integrations/misc/imageDataUrl.ts +++ b/src/integrations/misc/imageDataUrl.ts @@ -44,16 +44,13 @@ export async function normalizeImageRefsToDataUrls(imageRefs: string[]): Promise return results } -/** - * Cache mapping base64 data URLs to their temporary file paths - * This prevents writing the same image multiple times - */ -const base64ToFilePathCache = new Map() - /** * Converts base64 data URLs to file paths suitable for webview URIs. * Writes base64 images to temporary files and returns their paths. - * This is the reverse of normalizeImageRefsToDataUrls. + * + * NOTE: This function is primarily used for testing. In production, the dual-storage + * approach (images + imagesBase64 in ClineMessage) eliminates the need for this conversion. + * No caching is implemented since this is a test utility. */ export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalStoragePath: string): Promise { const results: string[] = [] @@ -75,22 +72,6 @@ export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalSto } try { - // Check cache first - const hash = crypto.createHash("md5").update(dataUrl).digest("hex") - let filePath = base64ToFilePathCache.get(hash) - - if (filePath) { - // Check if file still exists - try { - await fs.access(filePath) - results.push(filePath) - continue - } catch { - // File was deleted, need to recreate - base64ToFilePathCache.delete(hash) - } - } - // Extract base64 data and MIME type const commaIndex = dataUrl.indexOf(",") if (commaIndex === -1) { @@ -105,15 +86,15 @@ export async function normalizeDataUrlsToFilePaths(dataUrls: string[], globalSto const mimeMatch = header.match(/data:image\/([^;]+)/) const extension = mimeMatch ? `.${mimeMatch[1]}` : ".png" + // Create a unique hash for this image + const hash = crypto.createHash("md5").update(dataUrl).digest("hex") + // Write to temp file - filePath = path.join(tempDir, `${hash}${extension}`) + const filePath = path.join(tempDir, `${hash}${extension}`) const buffer = Buffer.from(base64Data, "base64") await fs.writeFile(filePath, buffer) - // Cache the mapping - base64ToFilePathCache.set(hash, filePath) - - // Also cache in the image cache for reverse lookups + // Cache the reverse mapping for image-cache lookups setImageBase64ForPath(filePath, dataUrl) results.push(filePath)