wget-improved
Advanced tools
Comparing version 3.3.1 to 3.4.0
@@ -11,2 +11,5 @@ 'use strict'; | ||
const redirectCodes = [301, 302, 303, 307]; | ||
const validCodes = [200, 201]; | ||
const handledCodes = validCodes.concat(redirectCodes); | ||
/** | ||
@@ -17,14 +20,8 @@ * Downloads a file using http get and request | ||
* @param {object} options - Options object | ||
* @param {object} _parentEvent - Used for when their is a 302 redirect and need to maintain state to a new request | ||
* @param {object} parentEvent - Used for when there is a 302 redirect and need to maintain state to a new request | ||
* @param {number} redirects - The number of redirects, used to prevent infinite loops | ||
* @returns {*|EventEmitter} | ||
*/ | ||
function download(src, output, options, _parentEvent, redirects) { | ||
if (typeof redirects === 'undefined') { | ||
redirects = 0; | ||
} | ||
let downloader = _parentEvent || new EventEmitter(), | ||
srcUrl, | ||
req; | ||
function download(src, output, options, parentEvent, redirects = 0) { | ||
let downloader = parentEvent || new EventEmitter(); | ||
if (options) { | ||
@@ -37,4 +34,4 @@ options = parseOptions('download', options); | ||
} | ||
srcUrl = url.parse(src); | ||
if (!srcUrl.protocol || srcUrl.prototype === null) { | ||
let srcUrl = url.parse(src); | ||
if (!srcUrl.protocol || srcUrl.protocol === null) { | ||
downloader.emit( | ||
@@ -50,3 +47,3 @@ 'error', | ||
req = request( | ||
let req = request( | ||
{ | ||
@@ -62,12 +59,9 @@ protocol: srcUrl.protocol, | ||
function(res) { | ||
let fileSize, writeStream, downloadedSize; | ||
let downloadedSize; | ||
let gunzip = zlib.createGunzip(); | ||
// Handle 302 redirects | ||
if ( | ||
res.statusCode === 301 || | ||
res.statusCode === 302 || | ||
res.statusCode === 307 | ||
) { | ||
var newLocation = res.headers.location; | ||
// Handle all common redirects | ||
if (redirectCodes.includes(res.statusCode)) { | ||
let newLocation = res.headers.location; | ||
// This enables support for relative redirects #11 | ||
if (res.headers.location.indexOf('/') === 0) { | ||
@@ -89,3 +83,3 @@ newLocation = | ||
} | ||
let fileSize, writeStream; | ||
if (res.statusCode === 200) { | ||
@@ -109,3 +103,3 @@ downloadedSize = 0; | ||
var encoding = ''; | ||
let encoding = ''; | ||
if (typeof res.headers['content-encoding'] === 'string') { | ||
@@ -142,9 +136,4 @@ encoding = res.headers['content-encoding']; | ||
downloader.emit('end', 'Finished writing to disk'); | ||
req.end('finished'); | ||
}); | ||
} else if ( | ||
res.statusCode !== 200 && | ||
res.statusCode !== 301 && | ||
res.statusCode !== 302 | ||
) { | ||
} else if (!handledCodes.includes(res.statusCode)) { | ||
downloader.emit( | ||
@@ -270,3 +259,3 @@ 'error', | ||
if (fileSize === null) { | ||
var length = String(totalDownloaded).length; | ||
const length = String(totalDownloaded).length; | ||
// This guarantees that totalDownloaded is never greater than or equal to fileSize. | ||
@@ -273,0 +262,0 @@ fileSize = Math.pow(10, length) + 1; |
{ | ||
"name": "wget-improved", | ||
"version": "3.3.1", | ||
"version": "3.4.0", | ||
"description": "wget in nodejs, forked from wuchengwei/node-wget to add improvements and help maintain the project", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -30,9 +30,13 @@ const crypto = require('crypto'); | ||
let count = 1; | ||
let type = 302; | ||
if (typeof req.query.count === 'string') { | ||
count = Number(req.query.count); | ||
} | ||
if (count === 0 && req.query.type) { | ||
type = Number(req.query.type); | ||
} | ||
if (count > 3) { | ||
res.send(redirectFile); | ||
} else { | ||
res.redirect( | ||
res.redirect(type, | ||
'http://localhost:8994/file/redirect?count=' + String(count + 1) | ||
@@ -39,0 +43,0 @@ ); |
@@ -122,2 +122,26 @@ // @ts-check | ||
it('Should handle 303 redirects that end with file download.', function(done) { | ||
let download = wget.download( | ||
'http://localhost:8994/file/redirect?test=303&count=0', | ||
'/tmp/wget-test-file2.bin' | ||
); | ||
download.on('end', function(output) { | ||
request(baseHTTP + '/file/redirect/metadata', function( | ||
err, | ||
res, | ||
body | ||
) { | ||
let meta = JSON.parse(body); | ||
let file = fs.readFileSync('/tmp/wget-test-file2.bin'); | ||
let hash = crypto | ||
.createHash('sha256') | ||
.update(file) | ||
.digest('hex'); | ||
expect(output).to.equal('Finished writing to disk'); | ||
expect(hash).to.equal(meta.hash); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('Should handle infinite redirects', function(done) { | ||
@@ -124,0 +148,0 @@ let download = wget.download( |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26098
539