Socket
Socket
Sign inDemoInstall

download

Package Overview
Dependencies
7
Maintainers
3
Versions
70
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.4.3 to 5.0.0

235

index.js
'use strict';
var path = require('path');
var url = require('url');
var caw = require('caw');
var concatStream = require('concat-stream');
var decompress = require('gulp-decompress');
var eachAsync = require('each-async');
var filenamify = require('filenamify');
var got = require('got');
var isUrl = require('is-url');
var objectAssign = require('object-assign');
var readAllStream = require('read-all-stream');
var rename = require('gulp-rename');
var streamCombiner = require('stream-combiner2');
var PassThrough = require('readable-stream/passthrough');
var Vinyl = require('vinyl');
var vinylFs = require('vinyl-fs');
var Ware = require('ware');
const fs = require('fs');
const path = require('path');
const url = require('url');
const caw = require('caw');
const decompress = require('decompress');
const filenamify = require('filenamify');
const getStream = require('get-stream');
const got = require('got');
const mkdirp = require('mkdirp');
const pify = require('pify');
const fsP = pify(fs);
/**
* Initialize a new `Download`
*
* @param {Object} opts
* @api public
*/
const createPromise = (uri, output, stream, opts) => new Promise((resolve, reject) => {
stream.on('response', res => {
const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
stream.then(resolve).catch(reject);
});
function Download(opts) {
if (!(this instanceof Download)) {
return new Download(opts);
stream.on('error', reject);
}).then(data => {
if (!output && opts.extract) {
return decompress(data, opts);
}
this.opts = objectAssign({encoding: null}, opts);
this.ware = new Ware();
}
module.exports = Download;
/**
* Get or set URL to download
*
* @param {String} url
* @param {String} dest
* @api public
*/
Download.prototype.get = function (url, dest) {
if (!arguments.length) {
return this._get;
if (!output) {
return data;
}
this._get = this._get || [];
this._get.push({
url: url,
dest: dest
});
return this;
};
/**
* Get or set the destination folder
*
* @param {String} dir
* @api public
*/
Download.prototype.dest = function (dir) {
if (!arguments.length) {
return this._dest;
if (opts.extract) {
return decompress(data, path.dirname(output), opts);
}
this._dest = dir;
return this;
};
return pify(mkdirp)(path.dirname(output))
.then(() => fsP.writeFile(output, data))
.then(() => data);
});
/**
* Rename the downloaded file
*
* @param {Function|String} name
* @api public
*/
Download.prototype.rename = function (name) {
if (!arguments.length) {
return this._rename;
module.exports = (uri, output, opts) => {
if (typeof output === 'object') {
opts = output;
output = null;
}
this._rename = name;
return this;
};
opts = Object.assign({encoding: null}, opts);
/**
* Add a plugin to the middleware stack
*
* @param {Function} plugin
* @api public
*/
let protocol = url.parse(uri).protocol;
Download.prototype.use = function (plugin) {
this.ware.use(plugin);
return this;
};
/**
* Run
*
* @param {Function} cb
* @api public
*/
Download.prototype.run = function (cb) {
cb = cb || function () {};
var files = [];
eachAsync(this.get(), function (get, i, done) {
if (!isUrl(get.url)) {
done(new Error('Specify a valid URL'));
return;
}
var protocol = url.parse(get.url).protocol;
if (protocol) {
protocol = protocol.slice(0, -1);
}
var agent = caw(this.opts.proxy, {protocol: protocol});
var stream = got.stream(get.url, objectAssign(this.opts, {agent: agent}));
stream.on('response', function (res) {
stream.headers = res.headers;
stream.statusCode = res.statusCode;
this.ware.run(stream, get.url);
}.bind(this));
var hasHttpError = false;
readAllStream(stream, null, function (err, data) {
if (hasHttpError) {
return;
}
if (err) {
if (err instanceof got.HTTPError) {
hasHttpError = true;
}
done(err);
return;
}
var dest = get.dest || this.dest();
var fileStream = this.createStream(this.createFile(get.url, data), dest);
fileStream.on('error', done);
fileStream.pipe(concatStream({encoding: 'object'}, function (items) {
files = files.concat(items);
done();
}));
}.bind(this));
}.bind(this), function (err) {
if (err) {
cb(err);
return;
}
cb(null, files);
});
};
/**
* Create vinyl file
*
* @param {String} url
* @param {Buffer} data
* @api private
*/
Download.prototype.createFile = function (url, data) {
return objectAssign(new Vinyl({
contents: data,
path: filenamify(path.basename(url))
}), {url: url});
};
/**
* Create stream
*
* @param {Object} file
* @param {String} dest
* @api private
*/
Download.prototype.createStream = function (file, dest) {
var stream = new PassThrough({objectMode: true});
var streams = [stream];
stream.end(file);
if (this.opts.extract) {
streams.push(decompress(this.opts));
if (protocol) {
protocol = protocol.slice(0, -1);
}
if (this.rename()) {
streams.push(rename(this.rename()));
}
const agent = caw(opts.proxy, {protocol});
const stream = got.stream(uri, Object.assign(opts, {agent}));
const dest = output ? path.join(output, filenamify(path.basename(uri))) : null;
const promise = createPromise(uri, dest, stream, opts);
if (dest) {
streams.push(vinylFs.dest(dest, this.opts));
}
stream.then = promise.then.bind(promise);
stream.catch = promise.catch.bind(promise);
return streamCombiner.obj(streams);
return stream;
};

33

package.json
{
"name": "download",
"version": "4.4.3",
"version": "5.0.0",
"description": "Download and extract files",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},

@@ -30,24 +30,21 @@ "scripts": {

"dependencies": {
"caw": "^1.0.1",
"concat-stream": "^1.4.7",
"each-async": "^1.0.0",
"filenamify": "^1.0.1",
"got": "^5.0.0",
"gulp-decompress": "^1.2.0",
"gulp-rename": "^1.2.0",
"is-url": "^1.2.0",
"object-assign": "^4.0.1",
"read-all-stream": "^3.0.0",
"readable-stream": "^2.0.2",
"stream-combiner2": "^1.1.1",
"vinyl": "^1.0.0",
"vinyl-fs": "^2.2.0",
"ware": "^1.2.0"
"caw": "^1.2.0",
"decompress": "^4.0.0",
"filenamify": "^1.2.1",
"get-stream": "^2.2.0",
"got": "^6.3.0",
"mkdirp": "^0.5.1",
"pify": "^2.3.0"
},
"devDependencies": {
"ava": "*",
"nock": "^3.1.0",
"is-zip": "^1.0.0",
"nock": "^8.0.0",
"path-exists": "^3.0.0",
"rimraf": "^2.2.8",
"xo": "*"
},
"xo": {
"esnext": true
}
}

@@ -17,51 +17,28 @@ # download [![Build Status](https://travis-ci.org/kevva/download.svg?branch=master)](https://travis-ci.org/kevva/download)

If you're fetching an archive you can set `extract: true` in options and
it'll extract it for you.
```js
var Download = require('download');
const fs = require('fs');
const download = require('download');
new Download({mode: '755'})
.get('http://example.com/foo.zip')
.get('http://example.com/cat.jpg')
.dest('dest')
.run();
```
download('http://unicorn.com/foo.jpg', 'dist').then(() => {
console.log('done!');
});
download('http://unicorn.com/foo.jpg').then(data => {
fs.writeFileSync('dist/foo.jpg', data);
});
## API
download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
### new Download(options)
Promise.all(['unicorn.com/foo.jpg', 'cats.com/dancing.gif'].map(x => download(x, 'dist'))).then(() => {
console.log('files downloaded!');
});
```
Creates a new `Download` instance.
#### options
## API
Type: `object`
### download(url, [destination], [options])
Options for [`got`](https://github.com/sindresorhus/got) or the underlying [`http`](https://nodejs.org/api/http.html#http_http_request_options_callback)/[`https`](https://nodejs.org/api/https.html#https_https_request_options_callback) request can be specified,
as well as options specific to the `download` module as described below.
Returns both a Promise for a Buffer and a [Duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with [additional events](https://github.com/sindresorhus/got#streams).
##### options.extract
Type: `boolean`
Default: `false`
If set to `true`, try extracting the file using [decompress](https://github.com/kevva/decompress/).
##### options.mode
Type: `string`
Set mode on the downloaded file, i.e `{mode: '755'}`.
##### options.strip
Type: `number`
Default: `0`
Remove leading directory components from extracted files.
### .get(url, [dest])
#### url

@@ -71,56 +48,24 @@

Add a URL to download.
URL to download.
#### dest
#### destination
Type: `string`
Set an optional destination folder that will take precedence over the one set in
`.dest()`.
Path to where your file will be written.
### .dest(dir)
#### options
#### dir
Same options as [got](https://github.com/sindresorhus/got) in addition to the ones below.
Type: `string`
##### extract
Set the destination folder to where your files will be downloaded.
Type: `boolean`<br>
Default: `false`
### .rename(name)
If set to `true`, try extracting the file using [decompress](https://github.com/kevva/decompress/).
#### name
Type: `function` or `string`
Rename your files using [gulp-rename](https://github.com/hparra/gulp-rename).
### .use(plugin)
#### plugin(response, url)
Type: `function`
Add a plugin to the middleware stack.
##### response
The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).
##### url
The requested URL.
### .run(callback)
#### callback(err, files)
Type: `function`
##### files
Contains an array of vinyl files.
## License
MIT © [Kevin Mårtensson](http://github.com/kevva)
SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc