Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ describe('BulkActionsService', () => {
'Content-Disposition',
`attachment; filename="${expectedFilename}"`,
);
expect(mockResponse.setHeader).toHaveBeenCalledWith(
'Access-Control-Expose-Headers',
'Content-Disposition',
);
expect(mockResponse.setHeader).toHaveBeenCalledWith(
'Transfer-Encoding',
'chunked',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class BulkActionsService {
'Content-Disposition',
`attachment; filename="bulk-delete-report-${timestamp}.txt"`,
);
res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition');
res.setHeader('Transfer-Encoding', 'chunked');

// Attach the response stream to the bulk action
Expand Down
35 changes: 32 additions & 3 deletions redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import { CustomHeaders } from 'uiSrc/constants/api'

/**
* Triggers a file download from a URL by creating a temporary link element
* Triggers a file download from a URL using fetch with proper headers
* This is necessary for Electron app where window ID authentication is required
* @param url The full URL to download from
*/
export const triggerDownloadFromUrl = (url: string): void => {
export const triggerDownloadFromUrl = async (url: string): Promise<void> => {

Check warning on line 8 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const headers: Record<string, string> = {}

Check warning on line 9 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

// Add window ID header for Electron app authentication
if (window.windowId) {
headers[CustomHeaders.WindowId] = window.windowId

Check warning on line 13 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 14 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 14 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const response = await fetch(url, { headers })

Check warning on line 16 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

if (!response.ok) {
throw new Error(`Download failed: ${response.statusText}`)

Check warning on line 19 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 20 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 20 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

// Extract filename from Content-Disposition header
const contentDisposition = response.headers.get('content-disposition') || ''

Check warning on line 23 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 23 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 23 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
const filenameMatch = contentDisposition.match(/filename="?([^";\n]+)"?/)

Check warning on line 24 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const filename = filenameMatch?.[1] || 'download'

Check warning on line 25 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 25 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 25 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

// Convert response to blob and trigger download
const blob = await response.blob()

Check warning on line 28 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const blobUrl = URL.createObjectURL(blob)

Check warning on line 29 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

const link = document.createElement('a')
link.href = url
link.href = blobUrl

Check warning on line 32 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
link.download = filename

Check warning on line 33 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
link.style.display = 'none'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)

// Clean up the blob URL
URL.revokeObjectURL(blobUrl)

Check warning on line 40 in redisinsight/ui/src/utils/dom/triggerDownloadFromUrl.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Blob URL revoked immediately may fail download

The URL.revokeObjectURL(blobUrl) is called synchronously right after link.click(). However, click() only initiates the download and returns immediately - it doesn't wait for the download to complete or even start. In some browsers or environments (including Electron, which this PR specifically targets), the blob URL may be revoked before the browser has finished reading the blob data, causing the download to fail silently. A common solution is to delay the revocation using setTimeout or defer it until after a brief period.

Fix in Cursor Fix in Web

}
Loading