Socket
Socket
Sign inDemoInstall

decompress

Package Overview
Dependencies
Maintainers
2
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decompress - npm Package Compare versions

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
}
}

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

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc