* new Deflate(options) * - options (Object): zlib deflate options. * * Creates new deflator instance with specified params. Throws exception * on bad params. Supported options: * * - `level` * - `windowBits` * - `memLevel` * - `strategy` * - `dictionary` * * [http://zlib.net/manual.html#
(options)
| 17118 | * ``` |
| 17119 | **/ |
| 17120 | function Deflate(options) { |
| 17121 | if (!(this instanceof Deflate)) return new Deflate(options); |
| 17122 | |
| 17123 | this.options = utils.assign({ |
| 17124 | level: Z_DEFAULT_COMPRESSION, |
| 17125 | method: Z_DEFLATED, |
| 17126 | chunkSize: 16384, |
| 17127 | windowBits: 15, |
| 17128 | memLevel: 8, |
| 17129 | strategy: Z_DEFAULT_STRATEGY, |
| 17130 | to: '' |
| 17131 | }, options || {}); |
| 17132 | |
| 17133 | var opt = this.options; |
| 17134 | |
| 17135 | if (opt.raw && (opt.windowBits > 0)) { |
| 17136 | opt.windowBits = -opt.windowBits; |
| 17137 | } |
| 17138 | |
| 17139 | else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) { |
| 17140 | opt.windowBits += 16; |
| 17141 | } |
| 17142 | |
| 17143 | this.err = 0; // error code, if happens (0 = Z_OK) |
| 17144 | this.msg = ''; // error message |
| 17145 | this.ended = false; // used to avoid multiple onEnd() calls |
| 17146 | this.chunks = []; // chunks of compressed data |
| 17147 | |
| 17148 | this.strm = new ZStream(); |
| 17149 | this.strm.avail_out = 0; |
| 17150 | |
| 17151 | var status = zlib_deflate.deflateInit2( |
| 17152 | this.strm, |
| 17153 | opt.level, |
| 17154 | opt.method, |
| 17155 | opt.windowBits, |
| 17156 | opt.memLevel, |
| 17157 | opt.strategy |
| 17158 | ); |
| 17159 | |
| 17160 | if (status !== Z_OK) { |
| 17161 | throw new Error(msg[status]); |
| 17162 | } |
| 17163 | |
| 17164 | if (opt.header) { |
| 17165 | zlib_deflate.deflateSetHeader(this.strm, opt.header); |
| 17166 | } |
| 17167 | |
| 17168 | if (opt.dictionary) { |
| 17169 | var dict; |
| 17170 | // Convert data if needed |
| 17171 | if (typeof opt.dictionary === 'string') { |
| 17172 | // If we need to compress text, change encoding to utf8. |
| 17173 | dict = strings.string2buf(opt.dictionary); |
| 17174 | } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { |
| 17175 | dict = new Uint8Array(opt.dictionary); |
| 17176 | } else { |
| 17177 | dict = opt.dictionary; |
nothing calls this directly
no outgoing calls
no test coverage detected