decompress
Advanced tools
Comparing version 3.0.0 to 4.0.0
171
index.js
'use strict'; | ||
var bufferToVinyl = require('buffer-to-vinyl'); | ||
var concatStream = require('concat-stream'); | ||
var streamCombiner = require('stream-combiner2'); | ||
var vinylFs = require('vinyl-fs'); | ||
var vinylAssign = require('vinyl-assign'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const decompressTar = require('decompress-tar'); | ||
const decompressTarbz2 = require('decompress-tarbz2'); | ||
const decompressTargz = require('decompress-targz'); | ||
const decompressUnzip = require('decompress-unzip'); | ||
const mkdirp = require('mkdirp'); | ||
const pify = require('pify'); | ||
const stripDirs = require('strip-dirs'); | ||
const fsP = pify(fs); | ||
/** | ||
* Initialize Decompress | ||
* | ||
* @param {Object} opts | ||
* @api public | ||
*/ | ||
function Decompress(opts) { | ||
if (!(this instanceof Decompress)) { | ||
return new Decompress(opts); | ||
const runPlugins = (input, opts) => { | ||
if (opts.plugins.length === 0) { | ||
return Promise.resolve([]); | ||
} | ||
this.opts = opts || {}; | ||
this.streams = []; | ||
} | ||
return Promise.all(opts.plugins.map(x => x(input, opts))).then(files => files.reduce((a, b) => a.concat(b))); | ||
}; | ||
/** | ||
* Get or set the source files | ||
* | ||
* @param {Array|Buffer|String} file | ||
* @api public | ||
*/ | ||
const extractFile = (input, output, opts) => runPlugins(input, opts).then(files => { | ||
if (opts.strip > 0) { | ||
files = files | ||
.map(x => { | ||
x.path = stripDirs(x.path, opts.strip); | ||
return x; | ||
}) | ||
.filter(x => x.path !== '.'); | ||
} | ||
Decompress.prototype.src = function (file) { | ||
if (!arguments.length) { | ||
return this._src; | ||
if (typeof opts.filter === 'function') { | ||
files = files.filter(opts.filter); | ||
} | ||
this._src = file; | ||
return this; | ||
}; | ||
if (typeof opts.map === 'function') { | ||
files = files.map(opts.map); | ||
} | ||
/** | ||
* Get or set the destination folder | ||
* | ||
* @param {String} dir | ||
* @api public | ||
*/ | ||
Decompress.prototype.dest = function (dir) { | ||
if (!arguments.length) { | ||
return this._dest; | ||
if (!output) { | ||
return files; | ||
} | ||
this._dest = dir; | ||
return this; | ||
}; | ||
return Promise.all(files.map(x => { | ||
if (x.type === 'directory') { | ||
return pify(mkdirp)(path.join(output, x.path)).then(() => x); | ||
} | ||
/** | ||
* Add a plugin to the middleware stack | ||
* | ||
* @param {Function} plugin | ||
* @api public | ||
*/ | ||
const dest = path.join(output, x.path); | ||
const mode = x.mode & ~process.umask(); | ||
Decompress.prototype.use = function (plugin) { | ||
this.streams.push(plugin); | ||
return this; | ||
}; | ||
return pify(mkdirp)(path.dirname(dest)) | ||
.then(() => { | ||
if (x.type === 'link') { | ||
return fsP.link(x.linkname, dest); | ||
} | ||
/** | ||
* Decompress archive | ||
* | ||
* @param {Function} cb | ||
* @api public | ||
*/ | ||
if (x.type === 'symlink') { | ||
return fsP.symlink(x.linkname, dest); | ||
} | ||
Decompress.prototype.run = function (cb) { | ||
cb = cb || function () {}; | ||
return fsP.writeFile(dest, x.data, {mode}); | ||
}) | ||
.then(() => x); | ||
})); | ||
}); | ||
var stream = this.createStream(); | ||
stream.on('error', cb); | ||
stream.pipe(concatStream(cb.bind(null, null))); | ||
}; | ||
/** | ||
* Create stream | ||
* | ||
* @api private | ||
*/ | ||
Decompress.prototype.createStream = function () { | ||
this.streams.unshift(vinylAssign({extract: true})); | ||
this.streams.unshift(this.getFiles()); | ||
if (this.streams.length === 2) { | ||
this.use(Decompress.tar(this.opts)); | ||
this.use(Decompress.tarbz2(this.opts)); | ||
this.use(Decompress.targz(this.opts)); | ||
this.use(Decompress.zip(this.opts)); | ||
module.exports = (input, output, opts) => { | ||
if (typeof input !== 'string' && !Buffer.isBuffer(input)) { | ||
return Promise.reject(new TypeError('Input file required')); | ||
} | ||
if (this.dest()) { | ||
this.streams.push(vinylFs.dest(this.dest())); | ||
if (typeof output === 'object') { | ||
opts = output; | ||
output = null; | ||
} | ||
return streamCombiner.obj(this.streams); | ||
}; | ||
opts = Object.assign({plugins: [ | ||
decompressTar(), | ||
decompressTarbz2(), | ||
decompressTargz(), | ||
decompressUnzip() | ||
]}, opts); | ||
/** | ||
* Get files | ||
* | ||
* @api private | ||
*/ | ||
const read = typeof input === 'string' ? fsP.readFile(input) : Promise.resolve(input); | ||
Decompress.prototype.getFiles = function () { | ||
if (Buffer.isBuffer(this.src())) { | ||
return bufferToVinyl.stream(this.src()); | ||
} | ||
return vinylFs.src(this.src()); | ||
return read.then(buf => extractFile(buf, output, opts)); | ||
}; | ||
/** | ||
* Module exports | ||
*/ | ||
module.exports = Decompress; | ||
module.exports.tar = require('decompress-tar'); | ||
module.exports.tarbz2 = require('decompress-tarbz2'); | ||
module.exports.targz = require('decompress-targz'); | ||
module.exports.zip = require('decompress-unzip'); |
{ | ||
"name": "decompress", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Extracting archives made easy", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -34,17 +34,20 @@ "scripts": { | ||
"dependencies": { | ||
"buffer-to-vinyl": "^1.0.0", | ||
"concat-stream": "^1.4.6", | ||
"decompress-tar": "^3.0.0", | ||
"decompress-tarbz2": "^3.0.0", | ||
"decompress-targz": "^3.0.0", | ||
"decompress-unzip": "^3.0.0", | ||
"stream-combiner2": "^1.1.1", | ||
"vinyl-assign": "^1.0.1", | ||
"vinyl-fs": "^2.2.0" | ||
"decompress-tar": "^4.0.0", | ||
"decompress-tarbz2": "^4.0.0", | ||
"decompress-targz": "^4.0.0", | ||
"decompress-unzip": "^4.0.1", | ||
"mkdirp": "^0.5.1", | ||
"pify": "^2.3.0", | ||
"strip-dirs": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.2.0", | ||
"rimraf": "^2.2.8", | ||
"ava": "*", | ||
"is-jpg": "^1.0.0", | ||
"path-exists": "^3.0.0", | ||
"pify": "^2.3.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
131
readme.md
@@ -17,9 +17,7 @@ # decompress [![Build Status](https://travis-ci.org/kevva/decompress.svg?branch=master)](https://travis-ci.org/kevva/decompress) | ||
```js | ||
const Decompress = require('decompress'); | ||
const decompress = require('decompress'); | ||
new Decompress({mode: '755'}) | ||
.src('foo.zip') | ||
.dest('dest') | ||
.use(Decompress.zip({strip: 1})) | ||
.run(); | ||
decompress('unicorn.zip', 'dist').then(files => { | ||
console.log('done!'); | ||
}); | ||
``` | ||
@@ -30,109 +28,78 @@ | ||
### new Decompress(options) | ||
### decompress(input, [output], [options]) | ||
Creates a new `Decompress` instance. | ||
Returns a Promise for an array of files in the following format: | ||
#### options.mode | ||
```js | ||
{ | ||
data: Buffer, | ||
mode: Number, | ||
mtime: String, | ||
path: String, | ||
type: String | ||
} | ||
``` | ||
Type: `string` | ||
#### input | ||
Set mode on the extracted files, i.e `{ mode: '755' }`. | ||
Type: `string` `Buffer` | ||
#### options.strip | ||
File to decompress. | ||
Type: `number` | ||
#### output | ||
Equivalent to `--strip-components` for tar. | ||
### .src(files) | ||
#### files | ||
Type: `array`, `buffer` or `string` | ||
Set the files to be extracted. | ||
### .dest(path) | ||
#### path | ||
Type: `string` | ||
Set the destination to where your file will be extracted to. | ||
Output directory. | ||
### .use(plugin) | ||
#### options | ||
#### plugin | ||
##### filter | ||
Type: `function` | ||
Type: `Function` | ||
Add a `plugin` to the middleware stack. | ||
Filter out files before extracting. E.g: | ||
### .run(callback) | ||
Extract your file with the given settings. | ||
#### callback(err, files) | ||
Type: `function` | ||
The callback will return an array of vinyl files in `files`. | ||
## Plugins | ||
The following [plugins](https://www.npmjs.org/browse/keyword/decompressplugin) are bundled with decompress: | ||
* [tar](#tar) — Extract TAR files. | ||
* [tar.bz2](#tarbz2) — Extract TAR.BZ files. | ||
* [tar.gz](#targz) — Extract TAR.GZ files. | ||
* [zip](#zip) — Extract ZIP files. | ||
### .tar(options) | ||
Extract TAR files. | ||
```js | ||
const Decompress = require('decompress'); | ||
new Decompress() | ||
.use(Decompress.tar({strip: 1})); | ||
decompress('unicorn.zip', 'dist', { | ||
filter: file => path.extname(file.path) !== '.exe' | ||
}).then(files => { | ||
console.log('done!'); | ||
}); | ||
``` | ||
### .tarbz2(options) | ||
##### map | ||
Extract TAR.BZ files. | ||
Type: `Function` | ||
Map files before extracting: E.g: | ||
```js | ||
const Decompress = require('decompress'); | ||
new Decompress() | ||
.use(Decompress.tarbz2({strip: 1})); | ||
decompress('unicorn.zip', 'dist', { | ||
map: file => { | ||
file.path = `unicorn-${file.path}`; | ||
return file; | ||
} | ||
}).then(files => { | ||
console.log('done!'); | ||
}); | ||
``` | ||
### .targz(options) | ||
##### plugins | ||
Extract TAR.GZ files. | ||
Type: `Array`<br> | ||
Default: `[decompressTar(), decompressTarbz2(), decompressTargz(), decompressUnzip()]` | ||
```js | ||
const Decompress = require('decompress'); | ||
Array of [plugins](https://www.npmjs.com/browse/keyword/decompressplugin) to use. | ||
new Decompress() | ||
.use(Decompress.targz({strip: 1})); | ||
``` | ||
##### strip | ||
### .zip(options) | ||
Type: `number`<br> | ||
Default: `0` | ||
Extract ZIP files. | ||
Remove leading directory components from extracted files. | ||
```js | ||
const Decompress = require('decompress'); | ||
new Decompress() | ||
.use(Decompress.zip({strip: 1})); | ||
``` | ||
## License | ||
MIT © [Kevin Mårtensson](https://github.com/kevva) |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7
0
5765
5
71
104
2
+ Addedmkdirp@^0.5.1
+ Addedpify@^2.3.0
+ Addedstrip-dirs@^1.1.1
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@5.7.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)
+ Addedfile-type@5.2.06.2.0(transitive)
+ Addedget-stream@2.3.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedunbzip2-stream@1.4.3(transitive)
- Removedbuffer-to-vinyl@^1.0.0
- Removedconcat-stream@^1.4.6
- Removedstream-combiner2@^1.1.1
- Removedvinyl-assign@^1.0.1
- Removedvinyl-fs@^2.2.0
- Removedarr-diff@2.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarray-unique@0.2.1(transitive)
- Removedbalanced-match@1.0.2(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)
- Removedclone@0.2.01.0.4(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedconvert-source-map@1.9.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.1.4(transitive)
- Removedduplexify@3.7.1(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)
- 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)
- 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)
- Removedgraceful-fs@4.2.11(transitive)
- Removedgulp-sourcemaps@1.6.0(transitive)
- Removedinflight@1.0.6(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-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedis-tar@1.0.0(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.isequal@4.5.0(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmerge-stream@1.0.1(transitive)
- Removedmicromatch@2.3.11(transitive)
- Removedminimatch@3.1.2(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject-assign@2.1.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedordered-read-streams@0.3.0(transitive)
- Removedparse-glob@3.0.4(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.343.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)
- Removedstat-mode@0.2.2(transitive)
- Removedstream-combiner2@1.1.1(transitive)
- Removedstream-shift@1.0.3(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-bom-stream@1.0.0(transitive)
- Removedthrough2@0.6.52.0.54.0.2(transitive)
- Removedthrough2-filter@2.0.03.1.0(transitive)
- Removedto-absolute-glob@0.1.1(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removedunique-stream@2.3.1(transitive)
- Removeduuid@2.0.3(transitive)
- Removedvali-date@1.0.0(transitive)
- Removedvinyl@0.4.61.2.0(transitive)
- Removedvinyl-assign@1.2.1(transitive)
- Removedvinyl-fs@2.4.4(transitive)
Updateddecompress-tar@^4.0.0
Updateddecompress-tarbz2@^4.0.0
Updateddecompress-targz@^4.0.0
Updateddecompress-unzip@^4.0.1