electron-dl-manager
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -44,2 +44,10 @@ "use strict"; | ||
} | ||
function generateRandomId() { | ||
const currentTime = new Date().getTime(); | ||
const randomNum = Math.floor(Math.random() * 1000); | ||
const combinedValue = currentTime.toString() + randomNum.toString(); | ||
const hash = crypto_1.default.createHash('sha256'); | ||
hash.update(combinedValue); | ||
return hash.digest('hex').substring(0, 12); | ||
} | ||
// Copied from https://github.com/sindresorhus/electron-dl/blob/main/index.js#L10 | ||
@@ -76,2 +84,4 @@ const getFilenameFromMime = (name, mime) => { | ||
* the saveAs dialog will show up first. | ||
* | ||
* Returns the id of the download. | ||
*/ | ||
@@ -82,5 +92,7 @@ download(params) { | ||
} | ||
const id = generateRandomId(); | ||
this.log(`Registering download for url: ${tUrl(params.url)}`); | ||
params.window.webContents.session.once('will-download', this.onWillDownload(params)); | ||
params.window.webContents.session.once('will-download', this.onWillDownload(id, params)); | ||
params.window.webContents.downloadURL(params.url, params.downloadURLOptions); | ||
return id; | ||
} | ||
@@ -126,2 +138,8 @@ /** | ||
} | ||
/** | ||
* Returns the number of active downloads | ||
*/ | ||
getActiveDownloadCount() { | ||
return Object.values(this.idToDownloadItems).filter((item) => item.getState() === 'progressing').length; | ||
} | ||
handleError(callbacks, error, data) { | ||
@@ -161,2 +179,7 @@ if (callbacks.onError) { | ||
} | ||
updateBadgeCount() { | ||
if (process.platform === 'darwin' || process.platform === 'linux') { | ||
electron_1.app.setBadgeCount(this.getActiveDownloadCount()); | ||
} | ||
} | ||
/** | ||
@@ -166,3 +189,3 @@ * Handles when the user initiates a download action by adding the developer-defined | ||
*/ | ||
onWillDownload({ window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, }) { | ||
onWillDownload(id, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
return async (event, item, webContents) => { | ||
@@ -211,3 +234,2 @@ // Begin adapted code from | ||
// End adapted code from https://github.com/sindresorhus/electron-dl/blob/main/index.js#L73 | ||
const id = this.calculateId(item); | ||
const resolvedFilename = path.basename(item.getSavePath()) || item.getFilename(); | ||
@@ -239,13 +261,15 @@ this.log(`[${tId(id)}] Associating ${id} to ${resolvedFilename}`); | ||
id, | ||
event: event, | ||
item: item, | ||
webContents: webContents, | ||
callbacks: callbacks, | ||
event, | ||
item, | ||
webContents, | ||
callbacks, | ||
showBadge, | ||
}); | ||
const doneHandler = this.itemOnDone({ | ||
id, | ||
event: event, | ||
item: item, | ||
webContents: webContents, | ||
callbacks: callbacks, | ||
event, | ||
item, | ||
webContents, | ||
callbacks, | ||
showBadge, | ||
}); | ||
@@ -261,3 +285,3 @@ this.listeners.set(item, { | ||
} | ||
itemOnUpdated({ id, event, item, webContents, callbacks }) { | ||
itemOnUpdated({ id, event, item, webContents, callbacks, showBadge }) { | ||
return async (_event, state) => { | ||
@@ -304,5 +328,8 @@ switch (state) { | ||
} | ||
if (showBadge) { | ||
this.updateBadgeCount(); | ||
} | ||
}; | ||
} | ||
itemOnDone({ id, event, item, webContents, callbacks }) { | ||
itemOnDone({ id, event, item, webContents, callbacks, showBadge }) { | ||
return async (_event, state) => { | ||
@@ -346,2 +373,5 @@ switch (state) { | ||
} | ||
if (showBadge) { | ||
this.updateBadgeCount(); | ||
} | ||
this.cleanup(item); | ||
@@ -356,6 +386,2 @@ }; | ||
} | ||
calculateId(item) { | ||
const toHash = item.getURL() + item.getETag() + item.getFilename() + item.getSavePath(); | ||
return crypto_1.default.createHash('sha256').update(toHash).digest('hex'); | ||
} | ||
cleanup(item) { | ||
@@ -362,0 +388,0 @@ const listeners = this.listeners.get(item); |
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./ElectronDownloadManager"), exports); | ||
__exportStar(require("./ElectronDownloadManagerMock"), exports); |
@@ -15,2 +15,10 @@ import crypto from 'crypto'; | ||
} | ||
function generateRandomId() { | ||
const currentTime = new Date().getTime(); | ||
const randomNum = Math.floor(Math.random() * 1000); | ||
const combinedValue = currentTime.toString() + randomNum.toString(); | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(combinedValue); | ||
return hash.digest('hex').substring(0, 12); | ||
} | ||
// Copied from https://github.com/sindresorhus/electron-dl/blob/main/index.js#L10 | ||
@@ -47,2 +55,4 @@ const getFilenameFromMime = (name, mime) => { | ||
* the saveAs dialog will show up first. | ||
* | ||
* Returns the id of the download. | ||
*/ | ||
@@ -53,5 +63,7 @@ download(params) { | ||
} | ||
const id = generateRandomId(); | ||
this.log(`Registering download for url: ${tUrl(params.url)}`); | ||
params.window.webContents.session.once('will-download', this.onWillDownload(params)); | ||
params.window.webContents.session.once('will-download', this.onWillDownload(id, params)); | ||
params.window.webContents.downloadURL(params.url, params.downloadURLOptions); | ||
return id; | ||
} | ||
@@ -97,2 +109,8 @@ /** | ||
} | ||
/** | ||
* Returns the number of active downloads | ||
*/ | ||
getActiveDownloadCount() { | ||
return Object.values(this.idToDownloadItems).filter((item) => item.getState() === 'progressing').length; | ||
} | ||
handleError(callbacks, error, data) { | ||
@@ -132,2 +150,7 @@ if (callbacks.onError) { | ||
} | ||
updateBadgeCount() { | ||
if (process.platform === 'darwin' || process.platform === 'linux') { | ||
app.setBadgeCount(this.getActiveDownloadCount()); | ||
} | ||
} | ||
/** | ||
@@ -137,3 +160,3 @@ * Handles when the user initiates a download action by adding the developer-defined | ||
*/ | ||
onWillDownload({ window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, }) { | ||
onWillDownload(id, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
return async (event, item, webContents) => { | ||
@@ -182,3 +205,2 @@ // Begin adapted code from | ||
// End adapted code from https://github.com/sindresorhus/electron-dl/blob/main/index.js#L73 | ||
const id = this.calculateId(item); | ||
const resolvedFilename = path.basename(item.getSavePath()) || item.getFilename(); | ||
@@ -210,13 +232,15 @@ this.log(`[${tId(id)}] Associating ${id} to ${resolvedFilename}`); | ||
id, | ||
event: event, | ||
item: item, | ||
webContents: webContents, | ||
callbacks: callbacks, | ||
event, | ||
item, | ||
webContents, | ||
callbacks, | ||
showBadge, | ||
}); | ||
const doneHandler = this.itemOnDone({ | ||
id, | ||
event: event, | ||
item: item, | ||
webContents: webContents, | ||
callbacks: callbacks, | ||
event, | ||
item, | ||
webContents, | ||
callbacks, | ||
showBadge, | ||
}); | ||
@@ -232,3 +256,3 @@ this.listeners.set(item, { | ||
} | ||
itemOnUpdated({ id, event, item, webContents, callbacks }) { | ||
itemOnUpdated({ id, event, item, webContents, callbacks, showBadge }) { | ||
return async (_event, state) => { | ||
@@ -275,5 +299,8 @@ switch (state) { | ||
} | ||
if (showBadge) { | ||
this.updateBadgeCount(); | ||
} | ||
}; | ||
} | ||
itemOnDone({ id, event, item, webContents, callbacks }) { | ||
itemOnDone({ id, event, item, webContents, callbacks, showBadge }) { | ||
return async (_event, state) => { | ||
@@ -317,2 +344,5 @@ switch (state) { | ||
} | ||
if (showBadge) { | ||
this.updateBadgeCount(); | ||
} | ||
this.cleanup(item); | ||
@@ -327,6 +357,2 @@ }; | ||
} | ||
calculateId(item) { | ||
const toHash = item.getURL() + item.getETag() + item.getFilename() + item.getSavePath(); | ||
return crypto.createHash('sha256').update(toHash).digest('hex'); | ||
} | ||
cleanup(item) { | ||
@@ -333,0 +359,0 @@ const listeners = this.listeners.get(item); |
export * from './types'; | ||
export * from './ElectronDownloadManager'; | ||
export * from './ElectronDownloadManagerMock'; |
import type { BrowserWindow, DownloadItem, Event, WebContents } from 'electron'; | ||
import { DownloadManagerCallbackData, DownloadManagerCallbacks, DownloadManagerConstructorParams, DownloadManagerItem, DownloadParams } from './types'; | ||
import { DownloadManagerCallbackData, DownloadManagerCallbacks, DownloadManagerConstructorParams, DownloadManagerItem, DownloadParams, IElectronDownloadManager } from './types'; | ||
type DoneEventFn = (_event: Event, state: 'completed' | 'cancelled' | 'interrupted') => Promise<void>; | ||
@@ -11,2 +11,3 @@ type UpdatedEventFn = (_event: Event, state: 'progressing' | 'interrupted') => Promise<void>; | ||
callbacks: DownloadManagerCallbacks; | ||
showBadge?: boolean; | ||
} | ||
@@ -16,3 +17,3 @@ /** | ||
*/ | ||
export declare class ElectronDownloadManager { | ||
export declare class ElectronDownloadManager implements IElectronDownloadManager { | ||
protected downloadItems: WeakMap<DownloadItem, DownloadManagerItem>; | ||
@@ -31,4 +32,6 @@ protected idToDownloadItems: Record<string, DownloadItem>; | ||
* the saveAs dialog will show up first. | ||
* | ||
* Returns the id of the download. | ||
*/ | ||
download(params: DownloadParams): void; | ||
download(params: DownloadParams): string; | ||
/** | ||
@@ -46,2 +49,6 @@ * Cancels a download | ||
resumeDownload(id: string): void; | ||
/** | ||
* Returns the number of active downloads | ||
*/ | ||
getActiveDownloadCount(): number; | ||
protected handleError(callbacks: DownloadManagerCallbacks, error: Error, data?: Partial<DownloadManagerCallbackData>): void; | ||
@@ -70,2 +77,3 @@ /** | ||
static disableThrottle(window: BrowserWindow): Promise<void>; | ||
protected updateBadgeCount(): void; | ||
/** | ||
@@ -75,16 +83,8 @@ * Handles when the user initiates a download action by adding the developer-defined | ||
*/ | ||
protected onWillDownload({ window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, }: { | ||
window: BrowserWindow; | ||
overwrite?: boolean; | ||
directory?: string; | ||
saveAsFilename?: string; | ||
callbacks: DownloadManagerCallbacks; | ||
saveDialogOptions?: Electron.CrossProcessExports.SaveDialogOptions; | ||
}): (event: Event, item: DownloadItem, webContents: WebContents) => Promise<void>; | ||
protected itemOnUpdated({ id, event, item, webContents, callbacks }: ItemHandlerParams): (_event: Event, state: 'progressing' | 'interrupted') => Promise<void>; | ||
protected itemOnDone({ id, event, item, webContents, callbacks }: ItemHandlerParams): (_event: Event, state: 'completed' | 'cancelled' | 'interrupted') => Promise<void>; | ||
protected onWillDownload(id: string, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }: DownloadParams): (event: Event, item: DownloadItem, webContents: WebContents) => Promise<void>; | ||
protected itemOnUpdated({ id, event, item, webContents, callbacks, showBadge }: ItemHandlerParams): (_event: Event, state: 'progressing' | 'interrupted') => Promise<void>; | ||
protected itemOnDone({ id, event, item, webContents, callbacks, showBadge }: ItemHandlerParams): (_event: Event, state: 'completed' | 'cancelled' | 'interrupted') => Promise<void>; | ||
protected updateProgress(item: DownloadItem): void; | ||
protected calculateId(item: DownloadItem): string; | ||
protected cleanup(item: DownloadItem): void; | ||
} | ||
export {}; |
export * from './types'; | ||
export * from './ElectronDownloadManager'; | ||
export * from './ElectronDownloadManagerMock'; |
@@ -133,2 +133,34 @@ import type { DownloadItem, SaveDialogOptions, WebContents, Event, BrowserWindow } from 'electron'; | ||
overwrite?: boolean; | ||
/** | ||
* If true, will show a badge on the dock icon when the download is in progress | ||
* under MacOS and linux. | ||
* | ||
* @default false | ||
*/ | ||
showBadge?: boolean; | ||
} | ||
export interface IElectronDownloadManager { | ||
/** | ||
* Starts a download. If saveDialogOptions has been defined in the config, | ||
* the saveAs dialog will show up first. | ||
* | ||
* Returns the id of the download. | ||
*/ | ||
download(params: DownloadParams): string; | ||
/** | ||
* Cancels a download | ||
*/ | ||
cancelDownload(id: string): void; | ||
/** | ||
* Pauses a download | ||
*/ | ||
pauseDownload(id: string): void; | ||
/** | ||
* Resumes a download | ||
*/ | ||
resumeDownload(id: string): void; | ||
/** | ||
* Returns the number of active downloads | ||
*/ | ||
getActiveDownloadCount(): number; | ||
} |
{ | ||
"name": "electron-dl-manager", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A library for implementing file downloads in Electron with 'save as' dialog and id support.", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -64,2 +64,3 @@ # Electron File Download Manager | ||
+ [`resumeDownload()`](#-resumedownload---) | ||
+ [`getActiveDownloadCount()`](#-getactivedownloadcount---) | ||
- [Acknowledgments](#acknowledgments) | ||
@@ -216,2 +217,9 @@ | ||
overwrite?: boolean | ||
/** | ||
* If true, will show a badge on the dock icon when the download is in progress | ||
* under MacOS and linux. | ||
* | ||
* @default false | ||
*/ | ||
showBadge?: boolean | ||
} | ||
@@ -292,2 +300,18 @@ ``` | ||
### `getActiveDownloadCount()` | ||
Returns the number of active downloads. | ||
```typescript | ||
getActiveDownloadCount(): number | ||
``` | ||
# Mock class | ||
If you need to mock out `ElectronDownloadManager` in your tests, you can use the `ElectronDownloadManagerMock` class. | ||
`import { ElectronDownloadManagerMock } from 'electron-dl-manager'` | ||
```typescript | ||
# Acknowledgments | ||
@@ -294,0 +318,0 @@ |
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
209065
17
1070
320