Skip to content

Commit

Permalink
fix: fix sometimes making an extra request on redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Nov 20, 2020
1 parent e72c5f6 commit 37b7f5d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {

const cleanup = () => {
activeRequest.removeListener('error', onError);
activeRequest.removeListener('close', onRequestClose);
activeResponse?.removeListener('data', onData);
activeDecodedStream?.removeListener('end', onEnd);
activeDecodedStream?.removeListener('error', onError);
Expand All @@ -221,23 +222,23 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
setTimeout(doDownload, res.headers['retry-after'] ? parseInt(res.headers['retry-after'], 10) * 1000: 0);
stream.emit('redirect', url);
}
return;
return cleanup();

// Check for rate limiting.
} else if (retryStatusCodes.has(res.statusCode)) {
if (!retryRequest({ retryAfter: parseInt(res.headers['retry-after'], 10) })) {
let err = new Miniget.MinigetError('Status code: ' + res.statusCode, res.statusCode);
stream.emit('error', err);
}
return;
return cleanup();
} else if (res.statusCode < 200 || 400 <= res.statusCode) {
let err = new Miniget.MinigetError('Status code: ' + res.statusCode, res.statusCode);
if (res.statusCode >= 500) {
onError(err, res.statusCode);
} else {
stream.emit('error', err);
}
return;
return cleanup();
}

activeDecodedStream = res as unknown as Transform;
Expand Down

0 comments on commit 37b7f5d

Please sign in to comment.