Skip to content

Commit

Permalink
Fix await download() when calling item.cancel() (#164)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
k2s and sindresorhus authored Oct 5, 2023
1 parent 43048d2 commit 9e84f84
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,18 @@ declare namespace electronDl {
}
}

/**
Error thrown if `item.cancel()` was called.
*/
declare class CancelError extends Error {}

// eslint-disable-next-line no-redeclare
declare const electronDl: {
/**
Error thrown if `item.cancel()` was called.
*/
CancelError: typeof CancelError;

/**
Register the helper for all windows.
Expand All @@ -157,6 +167,8 @@ declare const electronDl: {
@param window - Window to register the behavior on.
@param url - URL to download.
@returns A promise for the downloaded file.
@throws {CancelError} An error if the user calls `item.cancel()`.
@throws {Error} An error if the download fails.
@example
```
Expand Down
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const unusedFilename = require('unused-filename');
const pupa = require('pupa');
const extName = require('ext-name');

class CancelError extends Error {}

const getFilenameFromMime = (name, mime) => {
const extensions = extName.mime(mime);

Expand Down Expand Up @@ -92,10 +94,6 @@ function registerListener(session, options, callback = () => {}) {
item.setSavePath(filePath);
}

if (typeof options.onStarted === 'function') {
options.onStarted(item);
}

item.on('updated', () => {
receivedBytes = completedBytes;
for (const item of downloadItems) {
Expand Down Expand Up @@ -154,6 +152,7 @@ function registerListener(session, options, callback = () => {}) {
if (typeof options.onCancel === 'function') {
options.onCancel(item);
}
callback(new CancelError());

Check failure on line 155 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 14

Expected blank line before this statement.

Check failure on line 155 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 12

Expected blank line before this statement.
} else if (state === 'interrupted') {
const message = pupa(errorMessage, {filename: path.basename(filePath)});
callback(new Error(message));
Expand Down Expand Up @@ -182,6 +181,10 @@ function registerListener(session, options, callback = () => {}) {
callback(null, item);
}
});

if (typeof options.onStarted === 'function') {
options.onStarted(item);
}
};

session.on('will-download', listener);
Expand Down Expand Up @@ -214,3 +217,5 @@ module.exports.download = (window_, url, options) => new Promise((resolve, rejec

window_.webContents.downloadURL(url);
});

module.exports.CancelError = CancelError;
12 changes: 10 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ const {download} = require('electron-dl');

ipcMain.on('download-button', async (event, {url}) => {
const win = BrowserWindow.getFocusedWindow();
console.log(await download(win, url));
try {
console.log(await download(win, url));
} catch (error) {
if (error instanceof electronDl.CancelError) {
console.info('item.cancel() was called');
} else {
console.error(error);
}
}
});
```

Expand Down Expand Up @@ -128,7 +136,7 @@ Note: Error dialog will not be shown in `electronDl.download()`. Please handle e
Type: `Function`

Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item).
You can use this for advanced handling such as canceling the item like `item.cancel()`.
You can use this for advanced handling such as canceling the item like `item.cancel()` which will throw `electronDl.CancelError` from the `electronDl.download()` method.

#### onProgress

Expand Down

0 comments on commit 9e84f84

Please sign in to comment.