Comparing version 0.1.1 to 0.1.2
'use strict'; | ||
var decompress = require('decompress'); | ||
var fs = require('fs'); | ||
var mkdir = require('mkdirp'); | ||
var path = require('path'); | ||
var mkdir = require('mkdirp'); | ||
var request = require('request'); | ||
var decompress = require('decompress'); | ||
var stream = require('through2')(); | ||
/** | ||
* Download a file to a given destination | ||
* | ||
* Options: | ||
* | ||
* - `extract` Try extracting the file | ||
* | ||
* @param {String} url | ||
* @param {String} dest | ||
* @param {Object} opts | ||
*/ | ||
module.exports = function (url, dest, opts) { | ||
@@ -24,21 +36,26 @@ opts = opts || {}; | ||
}); | ||
req.on('response', function (res) { | ||
var mime = res.headers['content-type']; | ||
var status = res.statusCode; | ||
var end; | ||
if (status < 200 || status >= 300) { | ||
return; | ||
} | ||
if (opts.extract && decompress.canExtract(url, mime)) { | ||
req.pipe(decompress.extract({ type: mime, path: dest })) | ||
.on('close', function () { | ||
stream.emit('close'); | ||
}); | ||
end = decompress.extract({ ext: mime, path: dest }); | ||
} else { | ||
end = fs.createWriteStream(dest); | ||
if (!fs.existsSync(path.dirname(dest))) { | ||
mkdir.sync(path.dirname(dest)); | ||
} | ||
} | ||
req.pipe(fs.createWriteStream(dest)) | ||
.on('close', function () { | ||
stream.emit('close'); | ||
}); | ||
} | ||
req.pipe(end); | ||
end.on('close', function () { | ||
stream.emit('close'); | ||
}); | ||
}); | ||
@@ -45,0 +62,0 @@ |
{ | ||
"name": "download", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Download and extract files effortlessly", | ||
@@ -34,3 +34,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"mocha": "~1.12.0" | ||
"mocha": "~1.12.0", | ||
"rimraf": "~2.2.2" | ||
}, | ||
@@ -37,0 +38,0 @@ "engines": { |
@@ -0,0 +0,0 @@ # download [![Build Status](https://secure.travis-ci.org/kevva/download.png?branch=master)](http://travis-ci.org/kevva/download) |
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
3347
53
2