Skip to content

Commit

Permalink
Added resume behavior when the download finishes incomplete #11, #88
Browse files Browse the repository at this point in the history
  • Loading branch information
hgouveia committed Aug 4, 2022
1 parent dd5827a commit 6099ba0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Node.js CI

on:
push:
branches: [ main, dev ]
pull_request:
branches: [ main, dev ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 15.x, 16.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install --no-optional
- run: npm test
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![NPM Version](https://img.shields.io/npm/v/node-downloader-helper.svg?style=flat-square "npm version")](https://www.npmjs.com/package/node-downloader-helper)
![npm](https://img.shields.io/npm/dw/node-downloader-helper?style=flat-square "npm download")
![GitHub Actions Build](https://github.com/github/docs/actions/workflows/test.yml/badge.svg "GitHub Actions Build")
[![Build Status](https://img.shields.io/travis/hgouveia/node-downloader-helper/master.svg?style=flat-square "Build Status")](https://travis-ci.org/hgouveia/node-downloader-helper)
[![Windows Build Status](https://img.shields.io/appveyor/ci/hgouveia/node-downloader-helper/master.svg?label=windows&style=flat-square "Windows Build Status")](https://ci.appveyor.com/project/hgouveia/node-downloader-helper) [![Join the chat at https://gitter.im/node-downloader-helper/Lobby](https://badges.gitter.im/node-downloader-helper/Lobby.svg)](https://gitter.im/node-downloader-helper/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fhgouveia%2Fnode-downloader-helper.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fhgouveia%2Fnode-downloader-helper?ref=badge_shield)
Expand Down Expand Up @@ -64,6 +65,8 @@ these are the default values
method: 'GET', // Request Method Verb
headers: {}, // Custom HTTP Header ex: Authorization, User-Agent
timeout: -1, // Request timeout in milliseconds (-1 use default), is the equivalent of 'httpRequestOptions: { timeout: value }' (also applied to https)
resumeOnIncomplete: true, // Resume download if the file is incomplete (set false if using any pipe that modifies the file)
resumeOnIncompleteMaxRetry: 5, // Max retry when resumeOnIncomplete is true
resumeIfFileExists: false, // it will resume if a file already exists and is not completed, you might want to set removeOnStop and removeOnFail to false. If you used pipe for compression it will produce corrupted files
fileName: string|cb(fileName, filePath, contentType)|{name, ext}, // Custom filename when saved
retry: false, // { maxRetries: number, delay: number in ms } or false to disable (default)
Expand Down
18 changes: 16 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ export class DownloaderHelper extends EventEmitter {
progressThrottle: 1000,
httpRequestOptions: {},
httpsRequestOptions: {},
resumeOnIncomplete: true,
resumeIfFileExists: false,
resumeOnIncompleteMaxRetry: 5,
};
this.__opts = Object.assign({}, this.__defaultOpts);
this.__pipes = [];
this.__total = 0;
this.__downloaded = 0;
this.__progress = 0;
this.__retryCount = 0;
this.__resumeRetryCount = 0;
this.__states = DH_STATES;
this.__promise = null;
this.__request = null;
Expand Down Expand Up @@ -547,7 +550,8 @@ export class DownloaderHelper extends EventEmitter {
return (this.state !== this.__states.PAUSED &&
this.state !== this.__states.STOPPED &&
this.state !== this.__states.RETRY &&
this.state !== this.__states.FAILED);
this.state !== this.__states.FAILED &&
this.state !== this.__states.RESUMED);
}


Expand Down Expand Up @@ -580,13 +584,22 @@ export class DownloaderHelper extends EventEmitter {
return reject(_err);
}
if (this.__hasFinished()) {
const isIncomplete = !this.__total ? false : this.__downloaded !== this.__total;

if (isIncomplete && this.__isResumable && this.__opts.resumeOnIncomplete &&
this.__resumeRetryCount <= this.__opts.resumeOnIncompleteMaxRetry) {
this.__resumeRetryCount++;
this.emit('warning', new Error('uncomplete download, retrying'));
return this.resume();
}

this.__setState(this.__states.FINISHED);
this.__pipes = [];
this.emit('end', {
fileName: this.__fileName,
filePath: this.__filePath,
totalSize: this.__total,
incomplete: !this.__total ? false : this.__downloaded !== this.__total,
incomplete: isIncomplete,
onDiskSize: this.__getFilesizeInBytes(this.__filePath),
downloadedSize: this.__downloaded,
});
Expand Down Expand Up @@ -730,6 +743,7 @@ export class DownloaderHelper extends EventEmitter {
this.__retryCount = 0;
this.__downloaded = 0;
this.__progress = 0;
this.__resumeRetryCount = 0;
this.__statsEstimate = {
time: 0,
bytes: 0,
Expand Down

0 comments on commit 6099ba0

Please sign in to comment.