Skip to content

Commit

Permalink
Fixed muiltiple 'error' emits when retry #57
Browse files Browse the repository at this point in the history
  • Loading branch information
hgouveia committed Aug 20, 2021
1 parent e8c00d8 commit 9d1bd9c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
90 changes: 43 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,8 @@ export class DownloaderHelper extends EventEmitter {
*/
start() {
return new Promise((resolve, reject) => {
if (!this.__isRedirected &&
this.state !== this.__states.RESUMED) {
this.emit('start');
this.__setState(this.__states.STARTED);
} else if (this.state === this.__states.RESUMED && this.__promise) {
// recovering previous promise resolve/reject
const pResolve = this.__promise.resolve;
const pReject = this.__promise.reject;
const cResolve = resolve;
const cReject = reject;
resolve = (...args) => (pResolve(...args), cResolve(...args));
reject = (...args) => (pReject(...args), cReject(...args));
}

// Start the Download
this.__response = null;
this.__promise = { resolve, reject };
this.__request = this.__downloadRequest(resolve, reject);

// Error Handling
this.__request.on('error', this.__onError(resolve, reject));
this.__request.on('timeout', this.__onTimeout(resolve, reject));

this.__request.end();
this.__start();
});
}

Expand All @@ -116,9 +94,7 @@ export class DownloaderHelper extends EventEmitter {
* @memberof DownloaderHelper
*/
pause() {
if (this.__request) {
this.__request.abort();
}
this.__requestAbort();

if (this.__response) {
this.__response.unpipe();
Expand Down Expand Up @@ -149,7 +125,7 @@ export class DownloaderHelper extends EventEmitter {
this.__options['headers']['range'] = 'bytes=' + this.__downloaded + '-';
}
this.emit('resume', this.__isResumed);
return this.start();
return this.__start();
}

/**
Expand Down Expand Up @@ -184,9 +160,7 @@ export class DownloaderHelper extends EventEmitter {
});
});

if (this.__request) {
this.__request.abort();
}
this.__requestAbort();

return this.__closeFileStream().then(() => {
if (this.__opts.removeOnStop) {
Expand Down Expand Up @@ -330,6 +304,24 @@ export class DownloaderHelper extends EventEmitter {
});
}

__start() {
if (!this.__isRedirected &&
this.state !== this.__states.RESUMED) {
this.emit('start');
this.__setState(this.__states.STARTED);
}

// Start the Download
this.__response = null;
this.__request = this.__downloadRequest(this.__promise.resolve, this.__promise.reject);

// Error Handling
this.__request.on('error', this.__onError(this.__promise.resolve, this.__promise.reject));
this.__request.on('timeout', this.__onTimeout(this.__promise.resolve, this.__promise.reject));

this.__request.end();
}

/**
* Resolve pending promises from Start method
*
Expand Down Expand Up @@ -367,15 +359,7 @@ export class DownloaderHelper extends EventEmitter {
const redirectedURL = URL.resolve(this.url, response.headers.location);
this.__isRedirected = true;
this.__initProtocol(redirectedURL);
// returns a new promise of the start process with the new url
// and resolve this current promise when the new operation finishes
return this.start()
.then(() => resolve(true))
.catch(err => {
this.__setState(this.__states.FAILED);
this.emit('error', err);
return reject(err);
});
return this.__start();
}

// check if response wans't a success
Expand Down Expand Up @@ -518,7 +502,7 @@ export class DownloaderHelper extends EventEmitter {
downloadedSize: this.__downloaded,
});
}
return resolve(true);
return resolve(this.__downloaded === this.__total);
});
};
}
Expand Down Expand Up @@ -553,6 +537,11 @@ export class DownloaderHelper extends EventEmitter {
__onError(resolve, reject) {
return err => {
this.__pipes = [];

if (this.state === this.__states.STOPPED) {
return;
}

if (!this.__opts.retry) {
return this.__removeFile().then(() => {
this.__setState(this.__states.FAILED);
Expand All @@ -561,7 +550,6 @@ export class DownloaderHelper extends EventEmitter {
});
}
return this.__retry(err)
.then(() => resolve(true))
.catch(_err => {
this.__removeFile().then(() => {
this.__setState(this.__states.FAILED);
Expand All @@ -580,7 +568,7 @@ export class DownloaderHelper extends EventEmitter {
*/
__retry(err = null) {
if (!this.__opts.retry) {
return Promise.reject();
return Promise.reject(err);
}

if (typeof this.__opts.retry !== 'object' ||
Expand All @@ -599,7 +587,7 @@ export class DownloaderHelper extends EventEmitter {
this.emit('retry', this.__retryCount, this.__opts.retry, err);

return new Promise((resolve) =>
setTimeout(() => resolve(this.__downloaded > 0 ? this.resume() : this.start()), this.__opts.retry.delay)
setTimeout(() => resolve(this.__downloaded > 0 ? this.resume() : this.__start()), this.__opts.retry.delay)
);
}

Expand All @@ -612,9 +600,7 @@ export class DownloaderHelper extends EventEmitter {
*/
__onTimeout(resolve, reject) {
return () => {
if (this.__request) {
this.__request.abort();
}
this.__requestAbort();

if (!this.__opts.retry) {
return this.__removeFile().then(() => {
Expand All @@ -625,7 +611,6 @@ export class DownloaderHelper extends EventEmitter {
}

return this.__retry(new Error('timeout'))
.then(() => resolve(true))
.catch(_err => {
this.__removeFile().then(() => {
this.__setState(this.__states.FAILED);
Expand Down Expand Up @@ -963,4 +948,15 @@ export class DownloaderHelper extends EventEmitter {
});
});
}

/**
*
*
* @memberof DownloaderHelper
*/
__requestAbort() {
if (this.__request) {
this.__request.abort();
}
}
}
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface BaseStats {

interface DownloadInfoStats extends BaseStats {
/** if the download is a resume */
isResumed: boolean;
isResumed: boolean;
}

interface DownloadEndedStats extends BaseStats {
Expand Down Expand Up @@ -76,7 +76,7 @@ interface DownloadEvents {
/** The same as progress but emits every 1 second while is downloading */
"progress.throttled": (stats: Stats) => any;
/** Emitted when the download fails and retry is enabled */
retry: (attempt: any, retryOptions: RetryOptions) => any;
retry: (attempt: any, retryOptions: RetryOptions, error: Error | null) => any;
/** Emitted when the downloading has finished */
end: (stats: DownloadEndedStats) => any;
/** Emitted when there is any error */
Expand Down Expand Up @@ -234,7 +234,7 @@ export class DownloaderHelper extends EventEmitter {
* @memberof DownloaderHelper
*/
getTotalSize(): Promise<{ name: string; total: number }>;

/**
* Subscribes to events
*
Expand Down

0 comments on commit 9d1bd9c

Please sign in to comment.