Comparing version 0.2.1 to 0.2.2
84
index.js
@@ -8,2 +8,3 @@ 'use strict'; | ||
var path = require('path'); | ||
var Ware = require('ware'); | ||
@@ -19,2 +20,3 @@ /** | ||
this._url = []; | ||
this.ware = new Ware(); | ||
this.opts = opts || {}; | ||
@@ -56,2 +58,14 @@ this.opts.encoding = null; | ||
/** | ||
* Add a plugin to the middleware stack | ||
* | ||
* @param {Function} plugin | ||
* @api public | ||
*/ | ||
Download.prototype.use = function (plugin) { | ||
this.ware.use(plugin); | ||
return this; | ||
}; | ||
/** | ||
* Set proxy | ||
@@ -88,29 +102,22 @@ * | ||
var opts = assign(self.opts, obj.opts); | ||
var ret = []; | ||
request.get(obj.url, opts, function (err, res, data) { | ||
if (err) { | ||
return done(err); | ||
} | ||
request.get(obj.url, opts) | ||
.on('error', done) | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
return done(res.statusCode); | ||
} | ||
.on('data', function (data) { | ||
ret.push(data); | ||
}) | ||
if (opts.extract) { | ||
return self._extract(data, obj.dest, opts, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
.on('response', function (res) { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
return done(res.statusCode); | ||
} | ||
done(err); | ||
}); | ||
} | ||
self._run(res); | ||
}) | ||
fs.outputFile(path.join(obj.dest, name), data, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
if (opts.mode) { | ||
return fs.chmod(path.join(obj.dest, name), opts.mode, function (err) { | ||
.on('end', function () { | ||
if (opts.extract) { | ||
return self._extract(Buffer.concat(ret), obj.dest, opts, function (err) { | ||
if (err) { | ||
@@ -120,9 +127,24 @@ return done(err); | ||
done(); | ||
done(err); | ||
}); | ||
} | ||
done(); | ||
fs.outputFile(path.join(obj.dest, name), Buffer.concat(ret), function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
if (opts.mode) { | ||
return fs.chmod(path.join(obj.dest, name), opts.mode, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
done(); | ||
}); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}, function (err) { | ||
@@ -138,2 +160,14 @@ if (err) { | ||
/** | ||
* Run the response through the middleware | ||
* | ||
* @param {Object} res | ||
* @param {Function} cb | ||
* @api public | ||
*/ | ||
Download.prototype._run = function (res) { | ||
this.ware.run(res, this); | ||
}; | ||
/** | ||
* Extract archive | ||
@@ -140,0 +174,0 @@ * |
{ | ||
"name": "download", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Download and extract files effortlessly", | ||
@@ -33,3 +33,4 @@ "license": "MIT", | ||
"object-assign": "^0.3.1", | ||
"request": "^2.34.0" | ||
"request": "^2.34.0", | ||
"ware": "^0.3.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "devDependencies": { |
@@ -22,3 +22,4 @@ # download [![Build Status](https://travis-ci.org/kevva/download.svg?branch=master)](https://travis-ci.org/kevva/download) | ||
.get('http://example.com/bar.jpg', 'destFolder') | ||
.get({ url: 'http://example.com/bar.jpg', name: 'foobar.jpg' }, 'destFolder'); | ||
.get({ url: 'http://example.com/bar.jpg', name: 'foobar.jpg' }, 'destFolder') | ||
.use(Download.progress()); | ||
@@ -48,2 +49,6 @@ download.run(function (err) { | ||
### .use(plugin) | ||
Adds a plugin to the middleware stack. | ||
### .proxy(proxy) | ||
@@ -50,0 +55,0 @@ |
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
7301
161
89
6
+ Addedware@^0.3.0
+ Addedware@0.3.0(transitive)