download
Advanced tools
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; | ||
}; |
{ | ||
"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 | ||
} | ||
} |
105
readme.md
@@ -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) |
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
7
5030
6
50
70
2
1
+ Addeddecompress@^4.0.0
+ Addedget-stream@^2.2.0
+ Addedmkdirp@^0.5.1
+ Addedpify@^2.3.0
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addeddecompress@4.2.1(transitive)
+ Addeddecompress-tar@4.1.1(transitive)
+ Addeddecompress-tarbz2@4.1.1(transitive)
+ Addeddecompress-targz@4.1.1(transitive)
+ Addeddecompress-unzip@4.0.1(transitive)
+ Addedduplexer3@0.1.5(transitive)
+ Addedfile-type@5.2.06.2.0(transitive)
+ Addedget-stream@2.3.13.0.0(transitive)
+ Addedgot@6.7.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedis-natural-number@4.0.1(transitive)
+ Addedmake-dir@1.3.0(transitive)
+ Addedpify@2.3.03.0.0(transitive)
+ Addedstrip-dirs@2.1.0(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedtimed-out@4.0.1(transitive)
+ Addedunbzip2-stream@1.4.3(transitive)
+ Addedunzip-response@2.0.1(transitive)
- Removedconcat-stream@^1.4.7
- Removedeach-async@^1.0.0
- Removedgulp-decompress@^1.2.0
- Removedgulp-rename@^1.2.0
- Removedis-url@^1.2.0
- Removedobject-assign@^4.0.1
- Removedread-all-stream@^3.0.0
- Removedreadable-stream@^2.0.2
- Removedstream-combiner2@^1.1.1
- Removedvinyl@^1.0.0
- Removedvinyl-fs@^2.2.0
- Removedware@^1.2.0
- Removedansi-gray@0.1.1(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedansi-wrap@0.1.0(transitive)
- Removedarchive-type@3.2.0(transitive)
- Removedarr-diff@2.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarray-differ@1.0.0(transitive)
- Removedarray-uniq@1.0.3(transitive)
- Removedarray-unique@0.2.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbeeper@1.1.1(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbraces@1.8.5(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedbuffer-to-vinyl@1.1.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedclone@0.2.01.0.4(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedco@3.1.0(transitive)
- Removedcolor-support@1.1.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedconvert-source-map@1.9.0(transitive)
- Removeddateformat@2.2.0(transitive)
- Removeddecompress@3.0.0(transitive)
- Removeddecompress-tar@3.1.0(transitive)
- Removeddecompress-tarbz2@3.1.0(transitive)
- Removeddecompress-targz@3.1.0(transitive)
- Removeddecompress-unzip@3.4.0(transitive)
- Removedduplexer2@0.0.20.1.4(transitive)
- Removedduplexify@3.7.1(transitive)
- Removedeach-async@1.1.1(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedexpand-brackets@0.1.5(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextend-shallow@2.0.1(transitive)
- Removedextglob@0.3.2(transitive)
- Removedfancy-log@1.3.3(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.4(transitive)
- Removedfirst-chunk-stream@1.0.0(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedglob@5.0.15(transitive)
- Removedglob-base@0.3.0(transitive)
- Removedglob-parent@2.0.03.1.0(transitive)
- Removedglob-stream@5.3.5(transitive)
- Removedglogg@1.0.2(transitive)
- Removedgot@5.7.1(transitive)
- Removedgulp-decompress@1.2.0(transitive)
- Removedgulp-rename@1.4.0(transitive)
- Removedgulp-sourcemaps@1.6.0(transitive)
- Removedgulp-util@3.0.8(transitive)
- Removedgulplog@1.0.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhas-gulplog@0.1.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedis-absolute@0.1.7(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-bzip2@1.0.0(transitive)
- Removedis-dotfile@1.0.3(transitive)
- Removedis-equal-shallow@0.1.3(transitive)
- Removedis-extendable@0.1.1(transitive)
- Removedis-extglob@1.0.02.1.1(transitive)
- Removedis-glob@2.0.13.1.0(transitive)
- Removedis-gzip@1.0.0(transitive)
- Removedis-natural-number@2.1.1(transitive)
- Removedis-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedis-relative@0.1.3(transitive)
- Removedis-tar@1.0.0(transitive)
- Removedis-url@1.2.4(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedis-valid-glob@0.3.0(transitive)
- Removedis-zip@1.0.0(transitive)
- Removedisarray@0.0.1(transitive)
- Removedisobject@2.1.0(transitive)
- Removedjson-stable-stringify-without-jsonify@1.0.1(transitive)
- Removedkind-of@3.2.26.0.3(transitive)
- Removedlazystream@1.0.1(transitive)
- Removedlodash._basecopy@3.0.1(transitive)
- Removedlodash._basetostring@3.0.1(transitive)
- Removedlodash._basevalues@3.0.0(transitive)
- Removedlodash._getnative@3.9.1(transitive)
- Removedlodash._isiterateecall@3.0.9(transitive)
- Removedlodash._reescape@3.0.0(transitive)
- Removedlodash._reevaluate@3.0.0(transitive)
- Removedlodash._reinterpolate@3.0.0(transitive)
- Removedlodash._root@3.0.1(transitive)
- Removedlodash.escape@3.2.0(transitive)
- Removedlodash.isarguments@3.1.0(transitive)
- Removedlodash.isarray@3.0.4(transitive)
- Removedlodash.isequal@4.5.0(transitive)
- Removedlodash.keys@3.1.2(transitive)
- Removedlodash.restparam@3.6.1(transitive)
- Removedlodash.template@3.6.2(transitive)
- Removedlodash.templatesettings@3.1.1(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmerge-stream@1.0.1(transitive)
- Removedmicromatch@2.3.11(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedmultipipe@0.1.2(transitive)
- Removednode-status-codes@1.0.0(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject-assign@2.1.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedonetime@1.1.0(transitive)
- Removedordered-read-streams@0.3.0(transitive)
- Removedparse-glob@3.0.4(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedparse-node-version@1.0.1(transitive)
- Removedpath-dirname@1.0.2(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedread-all-stream@3.1.0(transitive)
- Removedreadable-stream@1.0.341.1.143.6.2(transitive)
- Removedregex-cache@0.4.4(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedreplace-ext@0.0.1(transitive)
- Removedset-immediate-shim@1.0.1(transitive)
- Removedsparkles@1.0.1(transitive)
- Removedstat-mode@0.2.2(transitive)
- Removedstream-combiner2@1.1.1(transitive)
- Removedstream-shift@1.0.3(transitive)
- Removedstring_decoder@0.10.311.3.0(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-bom-stream@1.0.0(transitive)
- Removedstrip-dirs@1.1.1(transitive)
- Removedsum-up@1.0.3(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedthrough2@0.6.52.0.54.0.2(transitive)
- Removedthrough2-filter@2.0.03.1.0(transitive)
- Removedtime-stamp@1.1.0(transitive)
- Removedtimed-out@3.1.3(transitive)
- Removedto-absolute-glob@0.1.1(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removedunique-stream@2.3.1(transitive)
- Removedunzip-response@1.0.2(transitive)
- Removeduuid@2.0.3(transitive)
- Removedvali-date@1.0.0(transitive)
- Removedvinyl@0.4.60.5.31.2.0(transitive)
- Removedvinyl-assign@1.2.1(transitive)
- Removedvinyl-fs@2.4.4(transitive)
- Removedware@1.3.0(transitive)
- Removedwrap-fn@0.1.5(transitive)
Updatedcaw@^1.2.0
Updatedfilenamify@^1.2.1
Updatedgot@^6.3.0