From a0596a58c4375548999d57e20ca2ebce1e492e95 Mon Sep 17 00:00:00 2001 From: Martin von Gagern Date: Fri, 28 Aug 2015 09:36:34 +0200 Subject: [PATCH] Introduce deflateFactory option to packer The main application motivating this addition is the creation of reproducible compression results. Since `zlib` is shipped with node.js, we have no control over its version via npm. To guarantee reproducible results, other libraries (like pako) could be used, with a fixed version number to guarantee consistent behavior. This modification provides a hook to allow such usage. --- README.md | 1 + lib/packer.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 22cf5f9..fbd3a3b 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ As input any color type is accepted (grayscale, rgb, palette, grayscale with alp - `deflateChunkSize` - chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32*1024 (default: 32 kB) - `deflateLevel` - compression level for delate (default: 9) - `deflateStrategy` - compression strategy for delate (default: 3) +- `deflateFactory` - deflate stream factory (default: `zlib.createDeflate`) - `filterType` - png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4) diff --git a/lib/packer.js b/lib/packer.js index ffaeba8..ac2ccc6 100755 --- a/lib/packer.js +++ b/lib/packer.js @@ -17,6 +17,7 @@ var Packer = module.exports = function(options) { options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9; options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3; + options.deflateFactory = options.deflateFactory || zlib.createDeflate; this.readable = true; }; @@ -37,7 +38,7 @@ Packer.prototype.pack = function(data, width, height, gamma) { var filteredData = filter(data, width, height, this._options); // compress it - var deflate = zlib.createDeflate({ + var deflate = this._options.deflateFactory({ chunkSize: this._options.deflateChunkSize, level: this._options.deflateLevel, strategy: this._options.deflateStrategy