Leave intact constructor options + cosmetic changes

This commit is contained in:
Vitaly Puzrin 2014-02-14 06:04:59 +04:00
parent a9c5e58eea
commit 806e23d9ca
2 changed files with 25 additions and 13 deletions

View file

@ -8,8 +8,8 @@ exports.run = function(data, callback) {
//zlib.deflate(new Buffer(data), callback); //zlib.deflate(new Buffer(data), callback);
var zlibStream = zlib.createDeflate({ var zlibStream = zlib.createDeflate({
chunkSize: 1*1024*1024/*, /*chunkSize: 128*1024,
level: 6*/ level: 0*/
}); });
var buffers = [], nread = 0; var buffers = [], nread = 0;

View file

@ -15,22 +15,30 @@ var zstream = require('./zlib/zstream');
* @constructor * @constructor
*/ */
var Deflate = function(options) { var Deflate = function(options) {
options = utils.assign({
this.options = utils.assign({
level: 6, level: 6,
method: c.Z_DEFLATED, method: c.Z_DEFLATED,
chunkSize: 16384, chunkSize: 16384,
windowBits: 15, windowBits: 15,
memLevel: 8, memLevel: 8,
strategy: c.Z_DEFAULT_STRATEGY strategy: c.Z_DEFAULT_STRATEGY
},options || {}); }, options || {});
this.options = options;
this.strm = new zstream(); this.strm = new zstream();
this.strm.next_out = utils.arrayCreate(this.options.chunkSize); this.strm.next_out = utils.arrayCreate(this.options.chunkSize);
var ret = zlib_deflate.deflateInit2(this.strm, options.level, options.method, options.windowBits, options.memLevel, options.strategy); var status = zlib_deflate.deflateInit2(
if (ret !== c.Z_OK) { this.strm,
throw new Error(msg[ret]); this.options.level,
this.options.method,
this.options.windowBits,
this.options.memLevel,
this.options.strategy
);
if (status !== c.Z_OK) {
throw new Error(msg[status]);
} }
}; };
@ -39,6 +47,7 @@ var Deflate = function(options) {
*/ */
Deflate.prototype.push = function(data_in) { Deflate.prototype.push = function(data_in) {
var strm = this.strm; var strm = this.strm;
var out;
strm.next_in = data_in; strm.next_in = data_in;
strm.next_in_index = 0; strm.next_in_index = 0;
@ -50,12 +59,13 @@ Deflate.prototype.push = function(data_in) {
strm.avail_out = this.options.chunkSize; strm.avail_out = this.options.chunkSize;
strm.next_out_index = 0; strm.next_out_index = 0;
zlib_deflate.deflate(strm, c.Z_NO_FLUSH); zlib_deflate.deflate(strm, c.Z_NO_FLUSH);
// TODO: check logic & why onEnd called. Check that onEnd not called twice.
//var ret = zlib_deflate.deflate(strm, c.Z_NO_FLUSH); /* no bad return value */ //var ret = zlib_deflate.deflate(strm, c.Z_NO_FLUSH); /* no bad return value */
//if (ret !== c.Z_STREAM_END && ret !== c.Z_OK) { //if (ret !== c.Z_STREAM_END && ret !== c.Z_OK) {
// this.onEnd(ret); // this.onEnd(ret);
//} //}
if(strm.next_out_index) { if(strm.next_out_index) {
var out = utils.arrayCreate(strm.next_out_index); out = utils.arrayCreate(strm.next_out_index);
utils.arraySet(out, strm.next_out, 0, strm.next_out_index, 0); utils.arraySet(out, strm.next_out, 0, strm.next_out_index, 0);
this.onData(out); this.onData(out);
} }
@ -64,16 +74,18 @@ Deflate.prototype.push = function(data_in) {
Deflate.prototype.flush = function() { Deflate.prototype.flush = function() {
var strm = this.strm; var strm = this.strm;
var out, status;
do { do {
strm.avail_out = this.options.chunkSize; strm.avail_out = this.options.chunkSize;
strm.next_out_index = 0; strm.next_out_index = 0;
var ret = zlib_deflate.deflate(strm, c.Z_FINISH); /* no bad return value */ status = zlib_deflate.deflate(strm, c.Z_FINISH); /* no bad return value */
if (ret !== c.Z_STREAM_END && ret !== c.Z_OK) { if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
this.onEnd(ret); this.onEnd(status);
} }
if(strm.next_out_index) { if(strm.next_out_index) {
var out = utils.arrayCreate(strm.next_out_index); out = utils.arrayCreate(strm.next_out_index);
utils.arraySet(out, strm.next_out, 0, strm.next_out_index, 0); utils.arraySet(out, strm.next_out, 0, strm.next_out_index, 0);
this.onData(out); this.onData(out);
} }