diff --git a/doc/api/zlib.markdown b/doc/api/zlib.markdown index 3dd8d2118c..92569b7d73 100644 --- a/doc/api/zlib.markdown +++ b/doc/api/zlib.markdown @@ -144,7 +144,9 @@ Returns a new [Unzip](#zlib_class_zlib_unzip) object with an Not exported by the `zlib` module. It is documented here because it is the base class of the compressor/decompressor classes. -### zlib.flush(callback) +### zlib.flush([kind], callback) + +`kind` defaults to `zlib.Z_FULL_FLUSH`. Flush pending data. Don't call this frivolously, premature flushes negatively impact the effectiveness of the compression algorithm. diff --git a/lib/zlib.js b/lib/zlib.js index bbe09ef2c2..ef4edd6d7e 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -357,9 +357,14 @@ Zlib.prototype._flush = function(callback) { this._transform(new Buffer(0), '', callback); }; -Zlib.prototype.flush = function(callback) { +Zlib.prototype.flush = function(kind, callback) { var ws = this._writableState; + if (typeof kind === 'function' || (kind === undefined && !callback)) { + callback = kind; + kind = binding.Z_FULL_FLUSH; + } + if (ws.ended) { if (callback) process.nextTick(callback); @@ -372,7 +377,7 @@ Zlib.prototype.flush = function(callback) { self.flush(callback); }); } else { - this._flushFlag = binding.Z_FULL_FLUSH; + this._flushFlag = kind; this.write(new Buffer(0), '', callback); } }; diff --git a/test/simple/test-zlib-flush.js b/test/simple/test-zlib-flush.js new file mode 100644 index 0000000000..0b189cecdd --- /dev/null +++ b/test/simple/test-zlib-flush.js @@ -0,0 +1,35 @@ +var common = require('../common.js'); +var assert = require('assert'); +var zlib = require('zlib'); +var path = require('path'); +var fs = require('fs'); + +var file = fs.readFileSync(path.resolve(common.fixturesDir, 'person.jpg')), + chunkSize = 16, + opts = { level: 0 }, + deflater = zlib.createDeflate(opts); + +var chunk = file.slice(0, chunkSize), + expectedNone = new Buffer([0x78, 0x01]), + blkhdr = new Buffer([0x00, 0x10, 0x00, 0xef, 0xff]), + adler32 = new Buffer([0x00, 0x00, 0x00, 0xff, 0xff]), + expectedFull = Buffer.concat([blkhdr, chunk, adler32]), + actualNone, + actualFull; + +deflater.write(chunk, function() { + deflater.flush(zlib.Z_NO_FLUSH, function() { + actualNone = deflater.read(); + deflater.flush(function() { + var bufs = [], buf; + while (buf = deflater.read()) + bufs.push(buf); + actualFull = Buffer.concat(bufs); + }); + }); +}); + +process.once('exit', function() { + assert.deepEqual(actualNone, expectedNone); + assert.deepEqual(actualFull, expectedFull); +});