Comparing version 0.1.18 to 0.2.0
179
index.js
'use strict'; | ||
var decompress = require('decompress'); | ||
var eachAsync = require('each-async'); | ||
var fs = require('fs'); | ||
var mkdir = require('mkdirp'); | ||
var assign = require('object-assign'); | ||
var Decompress = require('decompress'); | ||
var each = require('each-async'); | ||
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
var through = require('through2'); | ||
/** | ||
* Download a file to a given destination | ||
* Initialize Download | ||
* | ||
* Options: | ||
* @param {Object} opts | ||
* @api public | ||
*/ | ||
function Download(opts) { | ||
this._url = []; | ||
this.opts = opts || {}; | ||
this.opts.encoding = null; | ||
this.opts.proxy = process.env.HTTPS_PROXY || | ||
process.env.https_proxy || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy; | ||
} | ||
/** | ||
* Add a URL to download | ||
* | ||
* - `extract` Try extracting the file | ||
* - `mode` Set mode on the downloaded files | ||
* - `strip` Equivalent to --strip-components for tar | ||
* | ||
* @param {String|Array|Object} url | ||
* @param {String|Object} file | ||
* @param {String} dest | ||
@@ -25,68 +35,74 @@ * @param {Object} opts | ||
module.exports = function (url, dest, opts) { | ||
url = Array.isArray(url) ? url : [url]; | ||
Download.prototype.get = function (file, dest, opts) { | ||
if (!arguments.length) { | ||
return this._url; | ||
} | ||
dest = dest || process.cwd(); | ||
opts = opts || {}; | ||
var request = require('request'); | ||
var stream = through(); | ||
var strip = +opts.strip || '0'; | ||
if (file.url && file.name) { | ||
this._url.push({ url: file.url, name: file.name, dest: dest, opts: opts }); | ||
} else { | ||
this._url.push({ url: file, dest: dest, opts: opts }); | ||
} | ||
eachAsync(url, function (url, i, done) { | ||
var req; | ||
var target = path.join(dest, path.basename(url)); | ||
return this; | ||
}; | ||
opts.url = url; | ||
opts.proxy = process.env.HTTPS_PROXY || | ||
process.env.https_proxy || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy; | ||
/** | ||
* Set proxy | ||
* | ||
* @param {String} proxy | ||
* @api public | ||
*/ | ||
if (url.url && url.name) { | ||
target = path.join(dest, url.name); | ||
opts.url = url.url; | ||
} | ||
Download.prototype.proxy = function (proxy) { | ||
if (!arguments.length) { | ||
return this.opts.proxy; | ||
} | ||
req = request.get(opts); | ||
this.opts.proxy = proxy; | ||
return this; | ||
}; | ||
req.on('data', function (data) { | ||
stream.emit('data', data); | ||
}); | ||
/** | ||
* Run | ||
* | ||
* @param {Function} cb | ||
* @api public | ||
*/ | ||
req.on('error', function (err) { | ||
stream.emit('error', err); | ||
}); | ||
Download.prototype.run = function (cb) { | ||
cb = cb || function () {}; | ||
req.on('response', function (res) { | ||
var mime = res.headers['content-type']; | ||
var status = res.statusCode; | ||
var end; | ||
var request = require('request'); | ||
var self = this; | ||
if (status < 200 || status >= 300) { | ||
stream.emit('error', status); | ||
return; | ||
each(this.get(), function (obj, i, done) { | ||
var name = obj.name || path.basename(obj.url); | ||
var opts = assign(self.opts, obj.opts); | ||
request.get(obj.url, opts, function (err, res, data) { | ||
if (err) { | ||
return done(err); | ||
} | ||
stream.emit('response', res); | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
return done(res.statusCode); | ||
} | ||
if (opts.extract && (decompress.canExtract(opts.url, mime) || opts.ext)) { | ||
var ext = decompress.canExtract(opts.url) ? opts.url : mime; | ||
if (opts.extract) { | ||
return self._extract(data, obj.dest, opts, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
end = decompress({ | ||
ext: opts.ext || ext, | ||
path: dest, | ||
strip: strip | ||
done(err); | ||
}); | ||
} else { | ||
if (!fs.existsSync(dest)) { | ||
mkdir.sync(dest); | ||
} | ||
end = fs.createWriteStream(target); | ||
} | ||
req.pipe(end); | ||
end.on('close', function () { | ||
if (!opts.extract && opts.mode) { | ||
fs.chmodSync(target, opts.mode); | ||
fs.outputFile(path.join(obj.dest, name), data, function (err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
@@ -97,7 +113,42 @@ | ||
}); | ||
}, function () { | ||
stream.emit('close'); | ||
}, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(); | ||
}); | ||
}; | ||
return stream; | ||
/** | ||
* Extract archive | ||
* | ||
* @param {Buffer} buf | ||
* @param {String} dest | ||
* @param {Object} opts | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
Download.prototype._extract = function (buf, dest, opts, cb) { | ||
var decompress = new Decompress() | ||
.src(buf) | ||
.dest(dest) | ||
.use(Decompress.tar(opts)) | ||
.use(Decompress.targz(opts)) | ||
.use(Decompress.zip(opts)); | ||
decompress.decompress(function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(); | ||
}); | ||
}; | ||
/** | ||
* Module exports | ||
*/ | ||
module.exports = Download; |
{ | ||
"name": "download", | ||
"version": "0.1.18", | ||
"version": "0.2.0", | ||
"description": "Download and extract files effortlessly", | ||
@@ -15,5 +15,2 @@ "license": "MIT", | ||
}, | ||
"bin": { | ||
"download": "cli.js" | ||
}, | ||
"scripts": { | ||
@@ -23,3 +20,2 @@ "test": "mocha --reporter list" | ||
"files": [ | ||
"cli.js", | ||
"index.js" | ||
@@ -35,10 +31,7 @@ ], | ||
"dependencies": { | ||
"decompress": "^0.2.0", | ||
"decompress": "^0.3.0", | ||
"each-async": "^0.1.1", | ||
"get-stdin": "^0.1.0", | ||
"get-urls": "^0.1.1", | ||
"mkdirp": "^0.3.5", | ||
"nopt": "^2.2.0", | ||
"request": "^2.34.0", | ||
"through2": "^0.4.0" | ||
"fs-extra": "^0.10.0", | ||
"object-assign": "^0.3.1", | ||
"request": "^2.34.0" | ||
}, | ||
@@ -45,0 +38,0 @@ "devDependencies": { |
100
README.md
@@ -17,26 +17,16 @@ # download [![Build Status](https://travis-ci.org/kevva/download.svg?branch=master)](https://travis-ci.org/kevva/download) | ||
```js | ||
var download = require('download'); | ||
var Download = require('download'); | ||
// download and extract `foo.tar.gz` into `bar/` | ||
download('foo.tar.gz', 'bar', { extract: true }); | ||
var download = new Download() | ||
.get('http://example.com/foo.zip', 'destFolder', { extract: true, strip: 1 }) | ||
.get('http://example.com/bar.jpg', 'destFolder') | ||
.get({ url: 'http://example.com/bar.jpg', name: 'foobar.jpg' }, 'destFolder'); | ||
// download and save `foo.exe` into `bar/foo.exe` with mode `0755` | ||
download('foo.exe', 'bar', { mode: '0755' }); | ||
download.run(function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
// download and save `foo.zip` into `bar/foobar.zip` | ||
download({ url: 'foo.zip', name: 'foobar.zip' }, 'bar'); | ||
// download and save an array of files in `bar/` | ||
var files = ['foo.jpg', 'bar.jpg', 'cat.jpg']; | ||
download(files, 'bar'); | ||
// download, save and rename an array of files in `bar/` | ||
var files = [{ | ||
url: 'foo.jpg', | ||
name: 'foobar.jpg' | ||
}, { | ||
url: 'cat.jpg', | ||
name: 'dog.jpg' | ||
}]; | ||
download(files, 'bar'); | ||
console.log('Download complete!'); | ||
}); | ||
``` | ||
@@ -46,42 +36,33 @@ | ||
### download(url, dest, opts) | ||
### new Download(opts) | ||
Download a file or an array of files to a given destination. Returns an EventEmitter | ||
that emits the following possible events: | ||
Creates a new `Download` instance. Options defined here will be applied to all | ||
downloads. | ||
* `response` — Relayed when the underlying `http.ClientRequest` emits the same | ||
event. Listeners called with a `http.IncomingMessage` instance. | ||
* `data` — Relayed when the underlying `http.IncomingMessage` emits the same | ||
event. Listeners called with a `Buffer` instance. | ||
* `error` — Relayed when the underlying `http.ClientRequest` emits the same event | ||
or when the response status code is not in the 200s. Listeners called with an | ||
`Error` instance (in the first case) or the response status code. | ||
* `close` — Relayed when the underlying `stream.Duplex` emits the same event. | ||
### .get(file, dest, opts) | ||
## Options | ||
Add a file to download. The `file` argument accepts a `String` containing a URL | ||
or an `Object` with a URL and a desired name. For example `{ url: http://example.com/file.zip, name: 'foo.zip' }`. | ||
You can define options accepted by the [request](https://github.com/mikeal/request/) module besides from the options below. | ||
Options defined here will only apply to the specified file. | ||
### extract | ||
### .proxy(proxy) | ||
Type: `Boolean` | ||
Default: `false` | ||
Set proxy settings. Defaults to `process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy;`. | ||
If set to `true`, try extracting the file using [decompress](https://github.com/kevva/decompress/). | ||
### .run(cb) | ||
### ext | ||
Downloads your files and returns an error if something has gone wrong. | ||
Type: `String` | ||
Default: `undefined` | ||
## Options | ||
Sometimes you may be downloading an `application/octet-stream` and you want to | ||
extract it, eg it may be a zip, this option allows you to specify the extention | ||
of the file to be downloaded. | ||
You can define options accepted by the [request](https://github.com/mikeal/request#requestoptions-callback) | ||
module besides from the options below. | ||
### mode | ||
### extract | ||
Type: `String` | ||
Default: `undefined` | ||
Type: `Boolean` | ||
Default: `false` | ||
Set mode on the downloaded files. | ||
If set to `true`, try extracting the file using [decompress](https://github.com/kevva/decompress/). | ||
@@ -95,27 +76,4 @@ ### strip | ||
## CLI | ||
```bash | ||
$ npm install --global download | ||
``` | ||
```bash | ||
$ download --help | ||
Usage | ||
$ download <url> | ||
$ cat <file> | download> | ||
Example | ||
$ download --out dist --extract https://github.com/kevva/download/archive/master.zip | ||
$ cat urls.txt | download --out dist | ||
Options | ||
-e, --extract Extract archive files on download | ||
-o, --out Path to download or extract the files to | ||
-s, --strip <number> Strip path segments from root when extracting | ||
``` | ||
## License | ||
MIT © [Kevin Mårtensson](http://kevinmartensson.com) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5
5951
3
124
77
1
+ Addedfs-extra@^0.10.0
+ Addedobject-assign@^0.3.1
+ Addedarchive-type@0.1.4(transitive)
+ Addeddecompress@0.3.3(transitive)
+ Addeddecompress-tar@0.1.1(transitive)
+ Addeddecompress-targz@0.1.1(transitive)
+ Addeddecompress-unzip@0.1.2(transitive)
+ Addedeach-async@1.1.1(transitive)
+ Addedfs-extra@0.10.00.8.1(transitive)
+ Addedget-stdin@1.0.0(transitive)
+ Addedis-7zip@0.1.0(transitive)
+ Addedis-bzip2@0.1.0(transitive)
+ Addedis-gzip@0.1.0(transitive)
+ Addedis-rar@0.1.0(transitive)
+ Addedis-tar@0.1.1(transitive)
+ Addedis-zip@0.1.0(transitive)
+ Addedjsonfile@1.1.11.2.0(transitive)
+ Addedncp@0.4.20.5.1(transitive)
+ Addedonetime@1.1.0(transitive)
+ Addedread-chunk@0.1.0(transitive)
+ Addedrimraf@2.2.8(transitive)
+ Addedset-immediate-shim@1.0.1(transitive)
+ Addedsimple-bufferstream@0.0.4(transitive)
+ Addedtemp-write@0.3.1(transitive)
+ Addedware@0.2.1(transitive)
- Removedget-stdin@^0.1.0
- Removedget-urls@^0.1.1
- Removedmkdirp@^0.3.5
- Removednopt@^2.2.0
- Removedthrough2@^0.4.0
- Removedabbrev@1.1.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecompress@0.2.5(transitive)
- Removedduplexer@0.1.2(transitive)
- Removedext-list@0.2.0(transitive)
- Removedext-name@1.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedget-stdin@0.1.0(transitive)
- Removedget-urls@0.1.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedgot@0.2.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedisarray@0.0.1(transitive)
- Removedminimatch@3.1.2(transitive)
- Removednopt@2.2.1(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedstream-combiner@0.0.4(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedtempfile@0.1.3(transitive)
- Removedthrough2@0.4.2(transitive)
- Removedunderscore.string@2.3.3(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@2.1.2(transitive)
Updateddecompress@^0.3.0