zip-stream
Advanced tools
Comparing version 0.3.7 to 0.4.0
@@ -18,4 +18,2 @@ /** | ||
util.debug = require('debug'); | ||
util.convertDateTimeDos = function(input) { | ||
@@ -22,0 +20,0 @@ return new Date( |
@@ -9,13 +9,8 @@ /** | ||
var inherits = require('util').inherits; | ||
var Transform = require('readable-stream').Transform; | ||
var crc32 = require('buffer-crc32'); | ||
var ChecksumStream = require('crc32-stream'); | ||
var DeflateCRC32Stream = require('deflate-crc32-stream'); | ||
var headers = require('./headers'); | ||
var ZipArchiveOutputStream = require('compress-commons').ZipArchiveOutputStream; | ||
var ZipArchiveEntry = require('compress-commons').ZipArchiveEntry; | ||
var util = require('./util'); | ||
var debug = util.debug('zip-stream:instance'); | ||
var debugEntry = util.debug('zip-stream:entry'); | ||
var ZipStream = module.exports = function(options) { | ||
@@ -26,147 +21,23 @@ if (!(this instanceof ZipStream)) { | ||
debug('init'); | ||
options = this.options = options || {}; | ||
options.zlib = options.zlib || {}; | ||
options = this.options = util.defaults(options, { | ||
highWaterMark: 1024 * 1024, | ||
comment: '', | ||
forceUTC: false, | ||
store: false | ||
}); | ||
ZipArchiveOutputStream.call(this, options); | ||
if (typeof options.zlib !== 'object') { | ||
options.zlib = {}; | ||
} | ||
if (typeof options.level === 'number' && options.level >= 0) { | ||
options.zlib.level = options.level; | ||
delete options.level; | ||
} else if (typeof options.zlib.level !== 'number') { | ||
options.zlib.level = 1; | ||
} | ||
if (options.zlib.level === 0) { | ||
if (options.zlib.level && options.zlib.level === 0) { | ||
options.store = true; | ||
} | ||
Transform.call(this, options); | ||
this.offset = 0; | ||
this.entries = []; | ||
this._finalize = false; | ||
this._finalized = false; | ||
this._processing = false; | ||
this.once('end', function() { | ||
debug('stats:' + this.entries.length + 'e:' + this.offset + 'b'); | ||
debug('end'); | ||
}); | ||
}; | ||
inherits(ZipStream, Transform); | ||
ZipStream.prototype._afterAppend = function(entry) { | ||
debugEntry('%s:finish', entry.name); | ||
this.entries.push(entry); | ||
this._processing = false; | ||
if (this._finalize) { | ||
this.finalize(); | ||
if (options.comment && options.comment.length > 0) { | ||
this.setComment(options.comment); | ||
} | ||
}; | ||
ZipStream.prototype._appendBuffer = function(source, data, callback) { | ||
var self = this; | ||
inherits(ZipStream, ZipArchiveOutputStream); | ||
data.offset = self.offset; | ||
if (source.length === 0) { | ||
data.store = true; | ||
data.compressionMethod = 0; | ||
} | ||
if (data.store) { | ||
data.uncompressedSize = source.length; | ||
data.compressedSize = data.uncompressedSize; | ||
data.crc32 = crc32.unsigned(source); | ||
} else { | ||
data.flags |= (1 << 3); | ||
} | ||
self._writeHeader('file', data); | ||
if (data.store) { | ||
self.write(source); | ||
self._afterAppend(data); | ||
callback(null, data); | ||
} else { | ||
var processStream = self._newProcessStream(data.store, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
data.crc32 = processStream.digest(); | ||
data.uncompressedSize = processStream.size(); | ||
data.compressedSize = processStream.compressedSize || data.uncompressedSize; | ||
self._writeHeader('fileDescriptor', data); | ||
self._afterAppend(data); | ||
callback(null, data); | ||
}); | ||
processStream.end(source); | ||
} | ||
}; | ||
ZipStream.prototype._appendStream = function(source, data, callback) { | ||
var self = this; | ||
data.flags |= (1 << 3); | ||
data.offset = self.offset; | ||
self._writeHeader('file', data); | ||
var processStream = self._newProcessStream(data.store, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
data.crc32 = processStream.digest(); | ||
data.uncompressedSize = processStream.size(); | ||
data.compressedSize = processStream.size(true); | ||
self._writeHeader('fileDescriptor', data); | ||
self._afterAppend(data); | ||
callback(null, data); | ||
}); | ||
source.pipe(processStream); | ||
}; | ||
ZipStream.prototype._emitErrorCallback = function(err, data) { | ||
if (err) { | ||
this.emit('error', err); | ||
} | ||
}; | ||
ZipStream.prototype._newProcessStream = function(store, callback) { | ||
var process; | ||
if (store) { | ||
process = new ChecksumStream(); | ||
} else { | ||
process = new DeflateCRC32Stream(this.options.zlib); | ||
} | ||
if (typeof callback === 'function') { | ||
process.once('error', callback); | ||
process.once('end', callback); | ||
} | ||
process.pipe(this, { end: false }); | ||
return process; | ||
}; | ||
ZipStream.prototype._normalizeFileData = function(data) { | ||
@@ -177,2 +48,3 @@ data = util.defaults(data, { | ||
date: null, | ||
mode: null, | ||
store: this.options.store, | ||
@@ -199,50 +71,7 @@ comment: '' | ||
if (typeof data.lastModifiedDate !== 'number') { | ||
data.lastModifiedDate = util.dosDateTime(data.date, this.options.forceUTC); | ||
} | ||
data.date = util.dateify(data.date); | ||
data.flags = 0; | ||
data.compressionMethod = data.store ? 0 : 8; | ||
data.uncompressedSize = 0; | ||
data.compressedSize = 0; | ||
return data; | ||
}; | ||
ZipStream.prototype._transform = function(chunk, encoding, callback) { | ||
callback(null, chunk); | ||
}; | ||
ZipStream.prototype._writeCentralDirectory = function() { | ||
var entries = this.entries; | ||
var comment = this.options.comment; | ||
var cdoffset = this.offset; | ||
var cdsize = 0; | ||
var centralDirectoryBuffer; | ||
for (var i = 0; i < entries.length; i++) { | ||
var entry = entries[i]; | ||
centralDirectoryBuffer = this._writeHeader('centralDirectory', entry); | ||
cdsize += centralDirectoryBuffer.length; | ||
} | ||
var centralDirectoryFooterData = { | ||
directoryRecordsDisk: entries.length, | ||
directoryRecords: entries.length, | ||
centralDirectorySize: cdsize, | ||
centralDirectoryOffset: cdoffset, | ||
comment: comment | ||
}; | ||
this._writeHeader('centralFooter', centralDirectoryFooterData); | ||
}; | ||
ZipStream.prototype._writeHeader = function(type, data) { | ||
var encoded = headers.encode(type, data); | ||
this.write(encoded); | ||
return encoded; | ||
}; | ||
ZipStream.prototype.entry = function(source, data, callback) { | ||
@@ -253,14 +82,3 @@ if (typeof callback !== 'function') { | ||
if (this._processing) { | ||
callback(new Error('already processing an entry')); | ||
return; | ||
} | ||
if (this._finalize || this._finalized) { | ||
callback(new Error('entry after finalize()')); | ||
return; | ||
} | ||
data = this._normalizeFileData(data); | ||
debugEntry('%s:start', data.name); | ||
@@ -277,37 +95,22 @@ if (data.type !== 'file' && data.type !== 'directory') { | ||
this._processing = true; | ||
source = util.normalizeInputSource(source); | ||
var entry = new ZipArchiveEntry(data.name); | ||
entry.setTime(data.date); | ||
if (Buffer.isBuffer(source)) { | ||
debugEntry('%s:source:buffer', data.name); | ||
this._appendBuffer(source, data, callback); | ||
} else if (util.isStream(source)) { | ||
debugEntry('%s:source:stream', data.name); | ||
this._appendStream(source, data, callback); | ||
} else { | ||
this._processing = false; | ||
callback(new Error('input source must be valid Stream or Buffer instance')); | ||
return; | ||
if (data.store) { | ||
entry.setMethod(0); | ||
} | ||
}; | ||
ZipStream.prototype.finalize = function() { | ||
if (this._processing) { | ||
this._finalize = true; | ||
return; | ||
if (data.comment.length > 0) { | ||
entry.setComment(data.comment); | ||
} | ||
debug('finalize'); | ||
this._writeCentralDirectory(); | ||
this._finalized = true; | ||
debug('finalized'); | ||
this.end(); | ||
if (typeof data.mode === 'number') { | ||
entry.setUnixMode(data.mode); | ||
} | ||
return ZipArchiveOutputStream.prototype.entry.call(this, entry, source, callback); | ||
}; | ||
ZipStream.prototype.write = function(chunk, cb) { | ||
if (chunk) { | ||
this.offset += chunk.length; | ||
} | ||
return Transform.prototype.write.call(this, chunk, cb); | ||
ZipStream.prototype.finalize = function() { | ||
this.finish(); | ||
}; |
{ | ||
"name": "zip-stream", | ||
"version": "0.3.7", | ||
"version": "0.4.0", | ||
"description": "a streaming zip archive generator.", | ||
@@ -35,6 +35,3 @@ "homepage": "https://github.com/ctalkington/node-zip-stream", | ||
"dependencies": { | ||
"buffer-crc32": "~0.2.1", | ||
"crc32-stream": "~0.2.0", | ||
"debug": "~1.0.2", | ||
"deflate-crc32-stream": "~0.1.0", | ||
"compress-commons": "~0.1.0", | ||
"lodash": "~2.4.1", | ||
@@ -41,0 +38,0 @@ "readable-stream": "~1.0.26" |
@@ -1,5 +0,7 @@ | ||
# zip-stream v0.3.7 [![Build Status](https://travis-ci.org/ctalkington/node-zip-stream.svg?branch=master)](https://travis-ci.org/ctalkington/node-zip-stream) | ||
# zip-stream v0.4.0 [![Build Status](https://travis-ci.org/ctalkington/node-zip-stream.svg?branch=master)](https://travis-ci.org/ctalkington/node-zip-stream) | ||
zip-stream is a streaming zip archive generator. It was built to be a successor to [zipstream](https://npmjs.org/package/zipstream). Dependencies are kept to a minimum through the use of many of node's built-in modules including the use of zlib module for compression. | ||
zip-stream is a streaming zip archive generator based on the `ZipArchiveOutputStream` prototype found in the [compress-commons](https://www.npmjs.org/package/compress-commons) project. | ||
It was originally created to be a successor to [zipstream](https://npmjs.org/package/zipstream). | ||
[![NPM](https://nodei.co/npm/zip-stream.png)](https://nodei.co/npm/zip-stream/) | ||
@@ -93,21 +95,4 @@ | ||
Sets the entry permissions. (experimental) | ||
Sets the entry permissions. | ||
## Debugging | ||
This library makes use of the [debug](https://npmjs.org/package/debug) module with a namespace of `zip-stream` which can be triggered by setting `DEBUG` in your environment like so: | ||
```shell | ||
# unix | ||
DEBUG=zip-stream:* node script | ||
# windows (powershell) | ||
$env:DEBUG="zip-stream:*" | ||
node script | ||
# windows (cmd) | ||
SET DEBUG="zip-stream:*" | ||
node script | ||
``` | ||
## Things of Interest | ||
@@ -114,0 +99,0 @@ |
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
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
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
3
11147
5
165
105
1
+ Addedcompress-commons@~0.1.0
+ Addedcompress-commons@0.1.6(transitive)
+ Addedcrc32-stream@0.3.4(transitive)
- Removedbuffer-crc32@~0.2.1
- Removedcrc32-stream@~0.2.0
- Removeddebug@~1.0.2
- Removeddeflate-crc32-stream@~0.1.0
- Removedcrc32-stream@0.2.0(transitive)
- Removeddebug@1.0.5(transitive)
- Removeddeflate-crc32-stream@0.1.2(transitive)
- Removedms@2.0.0(transitive)