wget-improved
Advanced tools
Comparing version 0.9.3 to 1.0.0
@@ -7,11 +7,20 @@ 'use strict' | ||
var url = require('url'); | ||
var util = require('util'); | ||
var zlib = require('zlib'); | ||
var fs = require('fs'); | ||
var EventEmitter = require('events').EventEmitter; | ||
function download(src, output, options, redirects) { | ||
/** | ||
* Downloads a file using http get and request | ||
* @param {string} src - The http URL to download from | ||
* @param {string} output - The filepath to save to | ||
* @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 {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; | ||
} | ||
var downloader = new EventEmitter(), | ||
var downloader = _parentEvent || new EventEmitter(), | ||
srcUrl, | ||
@@ -36,9 +45,11 @@ tunnelAgent, | ||
var fileSize, writeStream, downloadedSize; | ||
var gunzip = zlib.createGunzip(); | ||
// Handle 302 redirects | ||
if(res.statusCode === 302) { | ||
redirects++; | ||
if(redirects >= 6) { | ||
return downloader.emit('error', 'Infinite redirect loop detected'); | ||
if(redirects >= 10) { | ||
downloader.emit('error', 'Infinite redirect loop detected'); | ||
} | ||
return download(res.headers.location, output, options, redirects); | ||
download(res.headers.location, output, options, downloader, redirects); | ||
} | ||
@@ -50,3 +61,3 @@ | ||
writeStream = fs.createWriteStream(output, { | ||
flags: 'a', | ||
flags: 'w+', | ||
encoding: 'binary' | ||
@@ -59,10 +70,27 @@ }); | ||
}); | ||
var encoding = ""; | ||
if(typeof res.headers['content-encoding'] === "string") { | ||
encoding = res.headers['content-encoding']; | ||
} | ||
// If the user has specified to unzip, and the file is gzip encoded, pipe to gunzip | ||
if(options.gunzip === true && encoding === "gzip") { | ||
res.pipe(gunzip); | ||
} else { | ||
res.pipe(writeStream); | ||
} | ||
// Data handlers | ||
res.on('data', function(chunk) { | ||
downloadedSize += chunk.length; | ||
downloader.emit('progress', downloadedSize/fileSize); | ||
}); | ||
gunzip.on('data', function(chunk) { | ||
writeStream.write(chunk); | ||
}); | ||
res.on('end', function() { | ||
writeStream.on('finish', function() { | ||
writeStream.end(); | ||
downloader.emit('end', output); | ||
downloader.emit('end', "Finished writing to disk"); | ||
req.end('finished'); | ||
}); | ||
@@ -74,3 +102,3 @@ } else { | ||
req.end(); | ||
req.end('done'); | ||
req.on('error', function(err) { | ||
@@ -77,0 +105,0 @@ downloader.emit('error', err); |
{ | ||
"name": "wget-improved", | ||
"version": "0.9.3", | ||
"version": "1.0.0", | ||
"description": "wget in nodejs, forked from wget to add improvements and help maintain the project", | ||
@@ -5,0 +5,0 @@ "keywords": ["download", "http", "https", "ftp", "proxy", "wget"], |
@@ -9,2 +9,4 @@ # wget-improved | ||
- Better error reporting | ||
- Does not write using append (now uses w+ identical to wget) | ||
- Handles gzip compression, allow you to automatically gunzip the stream | ||
@@ -73,2 +75,4 @@ | ||
options = {} | ||
// Set to true to have any gzip stream automatically decompressed before saving | ||
options.gunzip = false; | ||
options.proxy = {}; | ||
@@ -81,1 +85,5 @@ options.proxy.protocol = 'http'; | ||
``` | ||
#Todo | ||
Enable gzip when using request method |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11090
193
1
86