From 806e23d9caa9bb35973b6b33c03b9d13889aaedf Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Fri, 14 Feb 2014 06:04:59 +0400 Subject: [PATCH] Leave intact constructor options + cosmetic changes --- .../implementations/deflate-zlib/index.js | 4 +-- lib/deflate.js | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/benchmark/implementations/deflate-zlib/index.js b/benchmark/implementations/deflate-zlib/index.js index 88f6134..6949854 100644 --- a/benchmark/implementations/deflate-zlib/index.js +++ b/benchmark/implementations/deflate-zlib/index.js @@ -8,8 +8,8 @@ exports.run = function(data, callback) { //zlib.deflate(new Buffer(data), callback); var zlibStream = zlib.createDeflate({ - chunkSize: 1*1024*1024/*, - level: 6*/ + /*chunkSize: 128*1024, + level: 0*/ }); var buffers = [], nread = 0; diff --git a/lib/deflate.js b/lib/deflate.js index 4a84152..130bbd5 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -15,22 +15,30 @@ var zstream = require('./zlib/zstream'); * @constructor */ var Deflate = function(options) { - options = utils.assign({ + + this.options = utils.assign({ level: 6, method: c.Z_DEFLATED, chunkSize: 16384, windowBits: 15, memLevel: 8, strategy: c.Z_DEFAULT_STRATEGY - },options || {}); - this.options = options; + }, options || {}); this.strm = new zstream(); 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); - if (ret !== c.Z_OK) { - throw new Error(msg[ret]); + var status = zlib_deflate.deflateInit2( + this.strm, + 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) { var strm = this.strm; + var out; strm.next_in = data_in; strm.next_in_index = 0; @@ -50,12 +59,13 @@ Deflate.prototype.push = function(data_in) { strm.avail_out = this.options.chunkSize; strm.next_out_index = 0; 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 */ //if (ret !== c.Z_STREAM_END && ret !== c.Z_OK) { // this.onEnd(ret); //} 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); this.onData(out); } @@ -64,16 +74,18 @@ Deflate.prototype.push = function(data_in) { Deflate.prototype.flush = function() { var strm = this.strm; + var out, status; + do { strm.avail_out = this.options.chunkSize; strm.next_out_index = 0; - var ret = zlib_deflate.deflate(strm, c.Z_FINISH); /* no bad return value */ - if (ret !== c.Z_STREAM_END && ret !== c.Z_OK) { - this.onEnd(ret); + status = zlib_deflate.deflate(strm, c.Z_FINISH); /* no bad return value */ + if (status !== c.Z_STREAM_END && status !== c.Z_OK) { + this.onEnd(status); } 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); this.onData(out); }