diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-04-06 00:54:34 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2016-04-17 17:01:55 +0200 |
commit | 978166796edb5a867a9152f8a2a1ae35899e4000 (patch) | |
tree | 243f50dcfdbd147c88906a224a06d3b61858440b /lib | |
parent | f49a1d050178cbaab7732e8643c4db33c4b81ede (diff) | |
download | android-node-v8-978166796edb5a867a9152f8a2a1ae35899e4000.tar.gz android-node-v8-978166796edb5a867a9152f8a2a1ae35899e4000.tar.bz2 android-node-v8-978166796edb5a867a9152f8a2a1ae35899e4000.zip |
zlib: Make the finish flush flag configurable
Up to now, `Z_FINISH` was always the flushing flag that was used
for the last chunk of input data. This patch makes this choice
configurable so that advanced users can perform e.g. decompression of
partial data using `Z_SYNC_FLUSH`, if that suits their needs.
Add tests to make sure that an error is thrown upon encountering
invalid `flush` or `finishFlush` flags.
Fixes: https://github.com/nodejs/node/issues/5761
PR-URL: https://github.com/nodejs/node/pull/6069
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/zlib.js | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/lib/zlib.js b/lib/zlib.js index 79c78ea4a5..a786a0c911 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -234,7 +234,7 @@ function zlibBufferSync(engine, buffer) { if (!(buffer instanceof Buffer)) throw new TypeError('Not a string or buffer'); - var flushFlag = binding.Z_FINISH; + var flushFlag = engine._finishFlushFlag; return engine._processChunk(buffer, flushFlag); } @@ -282,6 +282,14 @@ function Unzip(opts) { Zlib.call(this, opts, binding.UNZIP); } +function isValidFlushFlag(flag) { + return flag === binding.Z_NO_FLUSH || + flag === binding.Z_PARTIAL_FLUSH || + flag === binding.Z_SYNC_FLUSH || + flag === binding.Z_FULL_FLUSH || + flag === binding.Z_FINISH || + flag === binding.Z_BLOCK; +} // the Zlib class they all inherit from // This thing manages the queue of requests, and returns @@ -294,17 +302,16 @@ function Zlib(opts, mode) { Transform.call(this, opts); - if (opts.flush) { - if (opts.flush !== binding.Z_NO_FLUSH && - opts.flush !== binding.Z_PARTIAL_FLUSH && - opts.flush !== binding.Z_SYNC_FLUSH && - opts.flush !== binding.Z_FULL_FLUSH && - opts.flush !== binding.Z_FINISH && - opts.flush !== binding.Z_BLOCK) { - throw new Error('Invalid flush flag: ' + opts.flush); - } + if (opts.flush && !isValidFlushFlag(opts.flush)) { + throw new Error('Invalid flush flag: ' + opts.flush); } + if (opts.finishFlush && !isValidFlushFlag(opts.finishFlush)) { + throw new Error('Invalid flush flag: ' + opts.finishFlush); + } + this._flushFlag = opts.flush || binding.Z_NO_FLUSH; + this._finishFlushFlag = typeof opts.finishFlush !== 'undefined' ? + opts.finishFlush : binding.Z_FINISH; if (opts.chunkSize) { if (opts.chunkSize < exports.Z_MIN_CHUNK || @@ -486,12 +493,13 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { if (this._closed) return cb(new Error('zlib binding closed')); - // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag. + // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag + // (or whatever flag was provided using opts.finishFlush). // If it's explicitly flushing at some other time, then we use // Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression // goodness. if (last) - flushFlag = binding.Z_FINISH; + flushFlag = this._finishFlushFlag; else { flushFlag = this._flushFlag; // once we've flushed the last of the queue, stop flushing and |