electron-dl-manager
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -17,2 +17,5 @@ "use strict"; | ||
getTotalBytes: jest.fn().mockReturnValue(1000), | ||
getCurrentBytesPerSecond: jest.fn(), | ||
getPercentComplete: jest.fn(), | ||
getStartTime: jest.fn(), | ||
cancel: jest.fn(), | ||
@@ -19,0 +22,0 @@ pause: jest.fn(), |
@@ -203,11 +203,8 @@ "use strict"; | ||
const { item } = this.downloadData; | ||
const input = { | ||
downloadedBytes: item.getReceivedBytes(), | ||
totalBytes: item.getTotalBytes(), | ||
startTimeSecs: item.getStartTime(), | ||
}; | ||
const metrics = (0, utils_1.calculateDownloadMetrics)(input); | ||
if (input.downloadedBytes > input.totalBytes) { | ||
const metrics = (0, utils_1.calculateDownloadMetrics)(item); | ||
const downloadedBytes = item.getReceivedBytes(); | ||
const totalBytes = item.getTotalBytes(); | ||
if (downloadedBytes > item.getTotalBytes()) { | ||
// Note: This situation will happen when using data: URIs | ||
this.log(`Downloaded bytes (${input.downloadedBytes}) is greater than total bytes (${input.totalBytes})`); | ||
this.log(`Downloaded bytes (${downloadedBytes}) is greater than total bytes (${totalBytes})`); | ||
} | ||
@@ -214,0 +211,0 @@ this.downloadData.downloadRateBytesPerSecond = metrics.downloadRateBytesPerSecond; |
@@ -59,17 +59,18 @@ "use strict"; | ||
/** | ||
* Calculates the download rate and estimated time remaining, using the start time and current time to determine elapsed time. | ||
* | ||
* @param {object} params - An object containing the parameters for the calculation. | ||
* @param {number} params.totalBytes - The total size of the download in bytes. | ||
* @param {number} params.downloadedBytes - The amount of data downloaded so far in bytes. | ||
* @param {number} params.startTimeSecs - The start time of the download in seconds. | ||
* Calculates the download rate and estimated time remaining for a download. | ||
* @returns {object} An object containing the download rate in bytes per second and the estimated time remaining in seconds. | ||
*/ | ||
function calculateDownloadMetrics({ totalBytes, downloadedBytes, startTimeSecs, }) { | ||
function calculateDownloadMetrics(item) { | ||
const downloadedBytes = item.getReceivedBytes(); | ||
const totalBytes = item.getTotalBytes(); | ||
const startTimeSecs = item.getStartTime(); | ||
const currentTimeSecs = Math.floor(new Date().getTime() / 1000); | ||
const elapsedTimeSecs = currentTimeSecs - startTimeSecs; | ||
let downloadRateBytesPerSecond = 0; | ||
// Avail in Electron 30.3.0+ | ||
let downloadRateBytesPerSecond = item.getCurrentBytesPerSecond ? item.getCurrentBytesPerSecond() : 0; | ||
let estimatedTimeRemainingSeconds = 0; | ||
if (elapsedTimeSecs > 0) { | ||
downloadRateBytesPerSecond = downloadedBytes / elapsedTimeSecs; | ||
if (!downloadRateBytesPerSecond) { | ||
downloadRateBytesPerSecond = downloadedBytes / elapsedTimeSecs; | ||
} | ||
if (downloadRateBytesPerSecond > 0) { | ||
@@ -79,3 +80,11 @@ estimatedTimeRemainingSeconds = (totalBytes - downloadedBytes) / downloadRateBytesPerSecond; | ||
} | ||
const percentCompleted = totalBytes > 0 ? Math.min(Number.parseFloat(((downloadedBytes / totalBytes) * 100).toFixed(2)), 100) : 0; | ||
let percentCompleted = 0; | ||
// Avail in Electron 30.3.0+ | ||
if (item.getPercentComplete) { | ||
percentCompleted = item.getPercentComplete(); | ||
} | ||
else { | ||
percentCompleted = | ||
totalBytes > 0 ? Math.min(Number.parseFloat(((downloadedBytes / totalBytes) * 100).toFixed(2)), 100) : 0; | ||
} | ||
return { | ||
@@ -82,0 +91,0 @@ percentCompleted, |
@@ -14,2 +14,5 @@ import { EventEmitter } from "node:events"; | ||
getTotalBytes: jest.fn().mockReturnValue(1000), | ||
getCurrentBytesPerSecond: jest.fn(), | ||
getPercentComplete: jest.fn(), | ||
getStartTime: jest.fn(), | ||
cancel: jest.fn(), | ||
@@ -16,0 +19,0 @@ pause: jest.fn(), |
@@ -177,11 +177,8 @@ import * as path from "node:path"; | ||
const { item } = this.downloadData; | ||
const input = { | ||
downloadedBytes: item.getReceivedBytes(), | ||
totalBytes: item.getTotalBytes(), | ||
startTimeSecs: item.getStartTime(), | ||
}; | ||
const metrics = calculateDownloadMetrics(input); | ||
if (input.downloadedBytes > input.totalBytes) { | ||
const metrics = calculateDownloadMetrics(item); | ||
const downloadedBytes = item.getReceivedBytes(); | ||
const totalBytes = item.getTotalBytes(); | ||
if (downloadedBytes > item.getTotalBytes()) { | ||
// Note: This situation will happen when using data: URIs | ||
this.log(`Downloaded bytes (${input.downloadedBytes}) is greater than total bytes (${input.totalBytes})`); | ||
this.log(`Downloaded bytes (${downloadedBytes}) is greater than total bytes (${totalBytes})`); | ||
} | ||
@@ -188,0 +185,0 @@ this.downloadData.downloadRateBytesPerSecond = metrics.downloadRateBytesPerSecond; |
@@ -49,17 +49,18 @@ import crypto from "node:crypto"; | ||
/** | ||
* Calculates the download rate and estimated time remaining, using the start time and current time to determine elapsed time. | ||
* | ||
* @param {object} params - An object containing the parameters for the calculation. | ||
* @param {number} params.totalBytes - The total size of the download in bytes. | ||
* @param {number} params.downloadedBytes - The amount of data downloaded so far in bytes. | ||
* @param {number} params.startTimeSecs - The start time of the download in seconds. | ||
* Calculates the download rate and estimated time remaining for a download. | ||
* @returns {object} An object containing the download rate in bytes per second and the estimated time remaining in seconds. | ||
*/ | ||
export function calculateDownloadMetrics({ totalBytes, downloadedBytes, startTimeSecs, }) { | ||
export function calculateDownloadMetrics(item) { | ||
const downloadedBytes = item.getReceivedBytes(); | ||
const totalBytes = item.getTotalBytes(); | ||
const startTimeSecs = item.getStartTime(); | ||
const currentTimeSecs = Math.floor(new Date().getTime() / 1000); | ||
const elapsedTimeSecs = currentTimeSecs - startTimeSecs; | ||
let downloadRateBytesPerSecond = 0; | ||
// Avail in Electron 30.3.0+ | ||
let downloadRateBytesPerSecond = item.getCurrentBytesPerSecond ? item.getCurrentBytesPerSecond() : 0; | ||
let estimatedTimeRemainingSeconds = 0; | ||
if (elapsedTimeSecs > 0) { | ||
downloadRateBytesPerSecond = downloadedBytes / elapsedTimeSecs; | ||
if (!downloadRateBytesPerSecond) { | ||
downloadRateBytesPerSecond = downloadedBytes / elapsedTimeSecs; | ||
} | ||
if (downloadRateBytesPerSecond > 0) { | ||
@@ -69,3 +70,11 @@ estimatedTimeRemainingSeconds = (totalBytes - downloadedBytes) / downloadRateBytesPerSecond; | ||
} | ||
const percentCompleted = totalBytes > 0 ? Math.min(Number.parseFloat(((downloadedBytes / totalBytes) * 100).toFixed(2)), 100) : 0; | ||
let percentCompleted = 0; | ||
// Avail in Electron 30.3.0+ | ||
if (item.getPercentComplete) { | ||
percentCompleted = item.getPercentComplete(); | ||
} | ||
else { | ||
percentCompleted = | ||
totalBytes > 0 ? Math.min(Number.parseFloat(((downloadedBytes / totalBytes) * 100).toFixed(2)), 100) : 0; | ||
} | ||
return { | ||
@@ -72,0 +81,0 @@ percentCompleted, |
@@ -15,15 +15,6 @@ import { type DownloadItem } from "electron"; | ||
/** | ||
* Calculates the download rate and estimated time remaining, using the start time and current time to determine elapsed time. | ||
* | ||
* @param {object} params - An object containing the parameters for the calculation. | ||
* @param {number} params.totalBytes - The total size of the download in bytes. | ||
* @param {number} params.downloadedBytes - The amount of data downloaded so far in bytes. | ||
* @param {number} params.startTimeSecs - The start time of the download in seconds. | ||
* Calculates the download rate and estimated time remaining for a download. | ||
* @returns {object} An object containing the download rate in bytes per second and the estimated time remaining in seconds. | ||
*/ | ||
export declare function calculateDownloadMetrics({ totalBytes, downloadedBytes, startTimeSecs, }: { | ||
totalBytes: number; | ||
downloadedBytes: number; | ||
startTimeSecs: number; | ||
}): { | ||
export declare function calculateDownloadMetrics(item: DownloadItem): { | ||
percentCompleted: number; | ||
@@ -30,0 +21,0 @@ downloadRateBytesPerSecond: number; |
{ | ||
"name": "electron-dl-manager", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "A library for implementing file downloads in Electron with 'save as' dialog and id support.", | ||
@@ -61,3 +61,3 @@ "main": "dist/cjs/index.js", | ||
"add": "^2.0.6", | ||
"electron": "28.2.7", | ||
"electron": "30.3.0", | ||
"jest": "^29.7.0", | ||
@@ -64,0 +64,0 @@ "ts-jest": "^29.1.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
297887
2028