Socket
Socket
Sign inDemoInstall

download

Package Overview
Dependencies
91
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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": {

@@ -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)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc