Skip to content

Commit

Permalink
Fixed #86, when resuming from file is not following the redirect corr…
Browse files Browse the repository at this point in the history
…ectly
  • Loading branch information
hgouveia committed Apr 6, 2022
1 parent d622e9c commit 44b2624
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
3 changes: 1 addition & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ const pauseResumeTimer = (_dl, wait) => {
}, wait);
};

// these are the default options
const options = {
method: 'GET', // Request Method Verb
// Custom HTTP Header ex: Authorization, User-Agent
headers: {
'user-agent': pkg.name + '@' + pkg.version
'user-agent': pkg.name + '/' + pkg.version
},
retry: { maxRetries: 3, delay: 3000 }, // { maxRetries: number, delay: number in ms } or false to disable (default)
fileName: filename => `${filename}.gz`, // Custom filename when saved
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-downloader-helper",
"version": "2.1.0",
"version": "2.1.1",
"description": "A simple http file downloader for node.js",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
48 changes: 21 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,35 +311,29 @@ export class DownloaderHelper extends EventEmitter {
if (headers.hasOwnProperty('range')) {
delete headers['range'];
}
const options = this.__getOptions('HEAD', this.url, headers);
return new Promise((resolve, reject) => {
const request = this.__protocol.request(options, response => {
if (this.__isRequireRedirect(response)) {
const redirectedURL = /^https?:\/\//.test(response.headers.location)
? response.headers.location
: new URL(response.headers.location, this.url).href;
const options = this.__getOptions('HEAD', redirectedURL, headers);
const request = this.__protocol.request(options, response => {
if (response.statusCode !== 200) {
reject(new Error(`Response status was ${response.statusCode}`));
}
resolve({
name: this.__getFileNameFromHeaders(response.headers, response),
total: parseInt(response.headers['content-length']) || null
});
})
request.end();
return;
}
if (response.statusCode !== 200) {
reject(new Error(`Response status was ${response.statusCode}`));
}
resolve({
name: this.__getFileNameFromHeaders(response.headers, response),
total: parseInt(response.headers['content-length']) || null
const getRequest = (url, options) => {
const req = this.__protocol.request(options, response => {
if (this.__isRequireRedirect(response)) {
const redirectedURL = /^https?:\/\//.test(response.headers.location)
? response.headers.location
: new URL(response.headers.location, url).href;
return getRequest(redirectedURL, this.__getOptions('HEAD', redirectedURL, headers));
}
if (response.statusCode !== 200) {
return reject(new Error(`Response status was ${response.statusCode}`));
}
resolve({
name: this.__getFileNameFromHeaders(response.headers, response),
total: parseInt(response.headers['content-length']) || null
});
});
});
request.end();
req.on('error', (err) => reject(err));
req.on('timeout', () => reject(new Error('timeout')));
req.on('uncaughtException', (err) => reject(err));
req.end();
};
getRequest(this.url, this.__getOptions('HEAD', this.url, headers));
});
}

Expand Down

0 comments on commit 44b2624

Please sign in to comment.