decompress-unzip
Advanced tools
Comparing version 3.4.0 to 4.0.0
156
index.js
'use strict'; | ||
const getStream = require('get-stream'); | ||
const isZip = require('is-zip'); | ||
const pify = require('pify'); | ||
const yauzl = require('yauzl'); | ||
var fs = require('fs'); | ||
var isZip = require('is-zip'); | ||
var StatMode = require('stat-mode'); | ||
var readAllStream = require('read-all-stream'); | ||
var stripDirs = require('strip-dirs'); | ||
var through = require('through2'); | ||
var Vinyl = require('vinyl'); | ||
var yauzl = require('yauzl'); | ||
const getType = (entry, mode) => { | ||
const IFMT = 61440; | ||
const IFDIR = 16384; | ||
const IFLNK = 40960; | ||
const madeBy = entry.versionMadeBy >> 8; | ||
module.exports = function (opts) { | ||
opts = opts || {}; | ||
opts.strip = Number(opts.strip) || 0; | ||
if ((mode & IFMT) === IFLNK) { | ||
return 'symlink'; | ||
} | ||
return through.obj(function (file, enc, cb) { | ||
var self = this; | ||
if ((mode & IFMT) === IFDIR || (madeBy === 0 && entry.externalFileAttributes === 16)) { | ||
return 'directory'; | ||
} | ||
if (file.isNull()) { | ||
cb(null, file); | ||
return; | ||
} | ||
return 'file'; | ||
}; | ||
if (file.isStream()) { | ||
cb(new Error('Streaming is not supported')); | ||
return; | ||
} | ||
const extractEntry = (entry, zip) => { | ||
const file = { | ||
mode: (entry.externalFileAttributes >> 16) & 0xFFFF, | ||
mtime: entry.getLastModDate(), | ||
path: entry.fileName | ||
}; | ||
if (!file.extract || !isZip(file.contents)) { | ||
cb(null, file); | ||
return; | ||
} | ||
file.type = getType(entry, file.mode); | ||
yauzl.fromBuffer(file.contents, function (err, zipFile) { | ||
var count = 0; | ||
if (file.mode === 0 && file.type === 'directory') { | ||
file.mode = 493; | ||
} | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (file.mode === 0) { | ||
file.mode = 420; | ||
} | ||
zipFile.on('error', cb); | ||
zipFile.on('entry', function (entry) { | ||
var filePath = stripDirs(entry.fileName, opts.strip); | ||
return pify(zip.openReadStream.bind(zip))(entry) | ||
.then(getStream.buffer) | ||
.then(buf => { | ||
file.data = buf; | ||
if (filePath === '.') { | ||
if (++count === zipFile.entryCount) { | ||
cb(); | ||
} | ||
if (file.type === 'symlink') { | ||
file.linkname = buf.toString(); | ||
} | ||
return; | ||
} | ||
return file; | ||
}) | ||
.catch(err => { | ||
zip.close(); | ||
throw err; | ||
}); | ||
}; | ||
var stat = new fs.Stats(); | ||
var mode = (entry.externalFileAttributes >> 16) & 0xFFFF; | ||
const extractFile = zip => new Promise((resolve, reject) => { | ||
const files = []; | ||
stat.mode = mode; | ||
zip.readEntry(); | ||
if (entry.getLastModDate()) { | ||
stat.mtime = entry.getLastModDate(); | ||
} | ||
zip.on('entry', entry => { | ||
extractEntry(entry, zip) | ||
.catch(reject) | ||
.then(file => { | ||
files.push(file); | ||
zip.readEntry(); | ||
}); | ||
}); | ||
if (entry.fileName.charAt(entry.fileName.length - 1) === '/') { | ||
if (!mode) { | ||
new StatMode(stat).isDirectory(true); | ||
} | ||
zip.on('error', reject); | ||
zip.on('end', () => resolve(files)); | ||
}); | ||
self.push(new Vinyl({ | ||
path: filePath, | ||
stat: stat | ||
})); | ||
module.exports = () => buf => { | ||
if (!Buffer.isBuffer(buf)) { | ||
return Promise.reject(new TypeError('Expected a buffer')); | ||
} | ||
if (++count === zipFile.entryCount) { | ||
cb(); | ||
} | ||
if (!isZip(buf)) { | ||
return Promise.resolve([]); | ||
} | ||
return; | ||
} | ||
zipFile.openReadStream(entry, function (err, readStream) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
readAllStream(readStream, null, function (err, data) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (!mode) { | ||
new StatMode(stat).isFile(true); | ||
} | ||
self.push(new Vinyl({ | ||
contents: data, | ||
path: filePath, | ||
stat: stat | ||
})); | ||
if (++count === zipFile.entryCount) { | ||
cb(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
return pify(yauzl.fromBuffer)(buf, {lazyEntries: true}).then(extractFile); | ||
}; |
{ | ||
"name": "decompress-unzip", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "decompress zip plugin", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -26,22 +26,18 @@ "scripts": { | ||
"extract", | ||
"gulpplugin", | ||
"zip" | ||
], | ||
"dependencies": { | ||
"get-stream": "^2.2.0", | ||
"is-zip": "^1.0.0", | ||
"read-all-stream": "^3.0.0", | ||
"stat-mode": "^0.2.0", | ||
"strip-dirs": "^1.0.0", | ||
"through2": "^2.0.0", | ||
"vinyl": "^1.0.0", | ||
"yauzl": "^2.2.1" | ||
"pify": "^2.3.0", | ||
"yauzl": "^2.4.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.2.0", | ||
"buffer-equal": "^0.0.1", | ||
"got": "^4.2.0", | ||
"ava": "*", | ||
"is-jpg": "^1.0.0", | ||
"vinyl-file": "^1.1.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -16,24 +16,11 @@ # decompress-unzip [![Build Status](https://travis-ci.org/kevva/decompress-unzip.svg?branch=master)](https://travis-ci.org/kevva/decompress-unzip) | ||
```js | ||
const Decompress = require('decompress'); | ||
const decompress = require('decompress'); | ||
const decompressUnzip = require('decompress-unzip'); | ||
new Decompress() | ||
.src('foo.zip') | ||
.dest('dest') | ||
.use(decompressUnzip({strip: 1})) | ||
.run(); | ||
``` | ||
You can also use this plugin with [gulp](http://gulpjs.com): | ||
```js | ||
const decompressUnzip = require('decompress-unzip'); | ||
const gulp = require('gulp'); | ||
const vinylAssign = require('vinyl-assign'); | ||
gulp.task('default', () => { | ||
return gulp.src('foo.zip') | ||
.pipe(vinylAssign({extract: true})) | ||
.pipe(decompressUnzip({strip: 1})) | ||
.pipe(gulp.dest('dest')); | ||
decompress('unicorn.zip', 'dist', { | ||
plugins: [ | ||
decompressUnzip() | ||
] | ||
}).then(() => { | ||
console.log('Files decompressed'); | ||
}); | ||
@@ -45,10 +32,9 @@ ``` | ||
### decompressUnzip(options) | ||
### decompressUnzip()(buf) | ||
#### options.strip | ||
#### buf | ||
Type: `number` | ||
Default: `0` | ||
Type: `Buffer` | ||
Remove leading directory components from extracted files. | ||
Buffer to decompress. | ||
@@ -55,0 +41,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
4
3
0
4135
68
43
+ Addedget-stream@^2.2.0
+ Addedpify@^2.3.0
+ Addedget-stream@2.3.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedpify@2.3.0(transitive)
- Removedread-all-stream@^3.0.0
- Removedstat-mode@^0.2.0
- Removedstrip-dirs@^1.0.0
- Removedthrough2@^2.0.0
- Removedvinyl@^1.0.0
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedclone@1.0.4(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-absolute@0.1.7(transitive)
- Removedis-natural-number@2.1.1(transitive)
- Removedis-relative@0.1.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedread-all-stream@3.1.0(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedreplace-ext@0.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstat-mode@0.2.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-dirs@1.1.1(transitive)
- Removedsum-up@1.0.3(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedvinyl@1.2.0(transitive)
- Removedxtend@4.0.2(transitive)
Updatedyauzl@^2.4.2