3 const Buffer = require('buffer').Buffer;
4 const Transform = require('_stream_transform');
5 const binding = process.binding('zlib');
6 const util = require('util');
7 const assert = require('assert').ok;
8 const kMaxLength = require('buffer').kMaxLength;
9 const kRangeErrorMessage = 'Cannot create final Buffer. ' +
10 'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes.';
12 // zlib doesn't provide these, so kludge them in following the same
13 // const naming scheme zlib uses.
14 binding.Z_MIN_WINDOWBITS = 8;
15 binding.Z_MAX_WINDOWBITS = 15;
16 binding.Z_DEFAULT_WINDOWBITS = 15;
18 // fewer than 64 bytes per chunk is stupid.
19 // technically it could work with as few as 8, but even 64 bytes
20 // is absurdly low. Usually a MB or more is best.
21 binding.Z_MIN_CHUNK = 64;
22 binding.Z_MAX_CHUNK = Infinity;
23 binding.Z_DEFAULT_CHUNK = (16 * 1024);
25 binding.Z_MIN_MEMLEVEL = 1;
26 binding.Z_MAX_MEMLEVEL = 9;
27 binding.Z_DEFAULT_MEMLEVEL = 8;
29 binding.Z_MIN_LEVEL = -1;
30 binding.Z_MAX_LEVEL = 9;
31 binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
33 // expose all the zlib constants
34 const bkeys = Object.keys(binding);
35 for (var bk = 0; bk < bkeys.length; bk++) {
37 if (bkey.match(/^Z/)) {
38 Object.defineProperty(exports, bkey, {
39 enumerable: true, value: binding[bkey], writable: false
44 // translation table for return codes.
47 Z_STREAM_END: binding.Z_STREAM_END,
48 Z_NEED_DICT: binding.Z_NEED_DICT,
49 Z_ERRNO: binding.Z_ERRNO,
50 Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
51 Z_DATA_ERROR: binding.Z_DATA_ERROR,
52 Z_MEM_ERROR: binding.Z_MEM_ERROR,
53 Z_BUF_ERROR: binding.Z_BUF_ERROR,
54 Z_VERSION_ERROR: binding.Z_VERSION_ERROR
57 const ckeys = Object.keys(codes);
58 for (var ck = 0; ck < ckeys.length; ck++) {
60 codes[codes[ckey]] = ckey;
63 Object.defineProperty(exports, 'codes', {
64 enumerable: true, value: Object.freeze(codes), writable: false
67 exports.Deflate = Deflate;
68 exports.Inflate = Inflate;
70 exports.Gunzip = Gunzip;
71 exports.DeflateRaw = DeflateRaw;
72 exports.InflateRaw = InflateRaw;
73 exports.Unzip = Unzip;
75 exports.createDeflate = function(o) {
76 return new Deflate(o);
79 exports.createInflate = function(o) {
80 return new Inflate(o);
83 exports.createDeflateRaw = function(o) {
84 return new DeflateRaw(o);
87 exports.createInflateRaw = function(o) {
88 return new InflateRaw(o);
91 exports.createGzip = function(o) {
95 exports.createGunzip = function(o) {
99 exports.createUnzip = function(o) {
104 // Convenience methods.
105 // compress/decompress a string or buffer in one step.
106 exports.deflate = function(buffer, opts, callback) {
107 if (typeof opts === 'function') {
111 return zlibBuffer(new Deflate(opts), buffer, callback);
114 exports.deflateSync = function(buffer, opts) {
115 return zlibBufferSync(new Deflate(opts), buffer);
118 exports.gzip = function(buffer, opts, callback) {
119 if (typeof opts === 'function') {
123 return zlibBuffer(new Gzip(opts), buffer, callback);
126 exports.gzipSync = function(buffer, opts) {
127 return zlibBufferSync(new Gzip(opts), buffer);
130 exports.deflateRaw = function(buffer, opts, callback) {
131 if (typeof opts === 'function') {
135 return zlibBuffer(new DeflateRaw(opts), buffer, callback);
138 exports.deflateRawSync = function(buffer, opts) {
139 return zlibBufferSync(new DeflateRaw(opts), buffer);
142 exports.unzip = function(buffer, opts, callback) {
143 if (typeof opts === 'function') {
147 return zlibBuffer(new Unzip(opts), buffer, callback);
150 exports.unzipSync = function(buffer, opts) {
151 return zlibBufferSync(new Unzip(opts), buffer);
154 exports.inflate = function(buffer, opts, callback) {
155 if (typeof opts === 'function') {
159 return zlibBuffer(new Inflate(opts), buffer, callback);
162 exports.inflateSync = function(buffer, opts) {
163 return zlibBufferSync(new Inflate(opts), buffer);
166 exports.gunzip = function(buffer, opts, callback) {
167 if (typeof opts === 'function') {
171 return zlibBuffer(new Gunzip(opts), buffer, callback);
174 exports.gunzipSync = function(buffer, opts) {
175 return zlibBufferSync(new Gunzip(opts), buffer);
178 exports.inflateRaw = function(buffer, opts, callback) {
179 if (typeof opts === 'function') {
183 return zlibBuffer(new InflateRaw(opts), buffer, callback);
186 exports.inflateRawSync = function(buffer, opts) {
187 return zlibBufferSync(new InflateRaw(opts), buffer);
190 function zlibBuffer(engine, buffer, callback) {
194 engine.on('error', onError);
195 engine.on('end', onEnd);
202 while (null !== (chunk = engine.read())) {
204 nread += chunk.length;
206 engine.once('readable', flow);
209 function onError(err) {
210 engine.removeListener('end', onEnd);
211 engine.removeListener('readable', flow);
219 if (nread >= kMaxLength) {
220 err = new RangeError(kRangeErrorMessage);
222 buf = Buffer.concat(buffers, nread);
231 function zlibBufferSync(engine, buffer) {
232 if (typeof buffer === 'string')
233 buffer = new Buffer(buffer);
234 if (!(buffer instanceof Buffer))
235 throw new TypeError('Not a string or buffer');
237 var flushFlag = binding.Z_FINISH;
239 return engine._processChunk(buffer, flushFlag);
243 // minimal 2-byte header
244 function Deflate(opts) {
245 if (!(this instanceof Deflate)) return new Deflate(opts);
246 Zlib.call(this, opts, binding.DEFLATE);
249 function Inflate(opts) {
250 if (!(this instanceof Inflate)) return new Inflate(opts);
251 Zlib.call(this, opts, binding.INFLATE);
255 // gzip - bigger header, same deflate compression
256 function Gzip(opts) {
257 if (!(this instanceof Gzip)) return new Gzip(opts);
258 Zlib.call(this, opts, binding.GZIP);
261 function Gunzip(opts) {
262 if (!(this instanceof Gunzip)) return new Gunzip(opts);
263 Zlib.call(this, opts, binding.GUNZIP);
268 function DeflateRaw(opts) {
269 if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
270 Zlib.call(this, opts, binding.DEFLATERAW);
273 function InflateRaw(opts) {
274 if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
275 Zlib.call(this, opts, binding.INFLATERAW);
279 // auto-detect header.
280 function Unzip(opts) {
281 if (!(this instanceof Unzip)) return new Unzip(opts);
282 Zlib.call(this, opts, binding.UNZIP);
286 // the Zlib class they all inherit from
287 // This thing manages the queue of requests, and returns
288 // true or false if there is anything in the queue when
289 // you call the .write() method.
291 function Zlib(opts, mode) {
292 this._opts = opts = opts || {};
293 this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
295 Transform.call(this, opts);
298 if (opts.flush !== binding.Z_NO_FLUSH &&
299 opts.flush !== binding.Z_PARTIAL_FLUSH &&
300 opts.flush !== binding.Z_SYNC_FLUSH &&
301 opts.flush !== binding.Z_FULL_FLUSH &&
302 opts.flush !== binding.Z_FINISH &&
303 opts.flush !== binding.Z_BLOCK) {
304 throw new Error('Invalid flush flag: ' + opts.flush);
307 this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
309 if (opts.chunkSize) {
310 if (opts.chunkSize < exports.Z_MIN_CHUNK ||
311 opts.chunkSize > exports.Z_MAX_CHUNK) {
312 throw new Error('Invalid chunk size: ' + opts.chunkSize);
316 if (opts.windowBits) {
317 if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
318 opts.windowBits > exports.Z_MAX_WINDOWBITS) {
319 throw new Error('Invalid windowBits: ' + opts.windowBits);
324 if (opts.level < exports.Z_MIN_LEVEL ||
325 opts.level > exports.Z_MAX_LEVEL) {
326 throw new Error('Invalid compression level: ' + opts.level);
331 if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
332 opts.memLevel > exports.Z_MAX_MEMLEVEL) {
333 throw new Error('Invalid memLevel: ' + opts.memLevel);
338 if (opts.strategy != exports.Z_FILTERED &&
339 opts.strategy != exports.Z_HUFFMAN_ONLY &&
340 opts.strategy != exports.Z_RLE &&
341 opts.strategy != exports.Z_FIXED &&
342 opts.strategy != exports.Z_DEFAULT_STRATEGY) {
343 throw new Error('Invalid strategy: ' + opts.strategy);
347 if (opts.dictionary) {
348 if (!(opts.dictionary instanceof Buffer)) {
349 throw new Error('Invalid dictionary: it should be a Buffer instance');
353 this._handle = new binding.Zlib(mode);
356 this._hadError = false;
357 this._handle.onerror = function(message, errno) {
358 // there is no way to cleanly recover.
359 // continuing only obscures problems.
361 self._hadError = true;
363 var error = new Error(message);
365 error.code = exports.codes[errno];
366 self.emit('error', error);
369 var level = exports.Z_DEFAULT_COMPRESSION;
370 if (typeof opts.level === 'number') level = opts.level;
372 var strategy = exports.Z_DEFAULT_STRATEGY;
373 if (typeof opts.strategy === 'number') strategy = opts.strategy;
375 this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
377 opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
381 this._buffer = new Buffer(this._chunkSize);
383 this._closed = false;
385 this._strategy = strategy;
387 this.once('end', this.close);
390 util.inherits(Zlib, Transform);
392 Zlib.prototype.params = function(level, strategy, callback) {
393 if (level < exports.Z_MIN_LEVEL ||
394 level > exports.Z_MAX_LEVEL) {
395 throw new RangeError('Invalid compression level: ' + level);
397 if (strategy != exports.Z_FILTERED &&
398 strategy != exports.Z_HUFFMAN_ONLY &&
399 strategy != exports.Z_RLE &&
400 strategy != exports.Z_FIXED &&
401 strategy != exports.Z_DEFAULT_STRATEGY) {
402 throw new TypeError('Invalid strategy: ' + strategy);
405 if (this._level !== level || this._strategy !== strategy) {
407 this.flush(binding.Z_SYNC_FLUSH, function() {
408 assert(!self._closed, 'zlib binding closed');
409 self._handle.params(level, strategy);
410 if (!self._hadError) {
412 self._strategy = strategy;
413 if (callback) callback();
417 process.nextTick(callback);
421 Zlib.prototype.reset = function() {
422 assert(!this._closed, 'zlib binding closed');
423 return this._handle.reset();
426 // This is the _flush function called by the transform class,
427 // internally, when the last chunk has been written.
428 Zlib.prototype._flush = function(callback) {
429 this._transform(new Buffer(0), '', callback);
432 Zlib.prototype.flush = function(kind, callback) {
433 var ws = this._writableState;
435 if (typeof kind === 'function' || (kind === undefined && !callback)) {
437 kind = binding.Z_FULL_FLUSH;
442 process.nextTick(callback);
443 } else if (ws.ending) {
445 this.once('end', callback);
446 } else if (ws.needDrain) {
448 this.once('drain', function() {
449 self.flush(callback);
452 this._flushFlag = kind;
453 this.write(new Buffer(0), '', callback);
457 Zlib.prototype.close = function(callback) {
459 process.nextTick(callback);
466 this._handle.close();
468 process.nextTick(emitCloseNT, this);
471 function emitCloseNT(self) {
475 Zlib.prototype._transform = function(chunk, encoding, cb) {
477 var ws = this._writableState;
478 var ending = ws.ending || ws.ended;
479 var last = ending && (!chunk || ws.length === chunk.length);
481 if (chunk !== null && !(chunk instanceof Buffer))
482 return cb(new Error('invalid input'));
485 return cb(new Error('zlib binding closed'));
487 // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
488 // If it's explicitly flushing at some other time, then we use
489 // Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
492 flushFlag = binding.Z_FINISH;
494 flushFlag = this._flushFlag;
495 // once we've flushed the last of the queue, stop flushing and
496 // go back to the normal behavior.
497 if (chunk.length >= ws.length) {
498 this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
502 this._processChunk(chunk, flushFlag, cb);
505 Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
506 var availInBefore = chunk && chunk.length;
507 var availOutBefore = this._chunkSize - this._offset;
512 var async = typeof cb === 'function';
519 this.on('error', function(er) {
523 assert(!this._closed, 'zlib binding closed');
525 var res = this._handle.writeSync(flushFlag,
528 availInBefore, // in_len
530 this._offset, //out_off
531 availOutBefore); // out_len
532 } while (!this._hadError && callback(res[0], res[1]));
534 if (this._hadError) {
538 if (nread >= kMaxLength) {
540 throw new RangeError(kRangeErrorMessage);
543 var buf = Buffer.concat(buffers, nread);
549 assert(!this._closed, 'zlib binding closed');
550 var req = this._handle.write(flushFlag,
553 availInBefore, // in_len
555 this._offset, //out_off
556 availOutBefore); // out_len
559 req.callback = callback;
561 function callback(availInAfter, availOutAfter) {
565 var have = availOutBefore - availOutAfter;
566 assert(have >= 0, 'have should not go down');
569 var out = self._buffer.slice(self._offset, self._offset + have);
570 self._offset += have;
571 // serve some output to the consumer.
580 // exhausted the output buffer, or used all the input create a new one.
581 if (availOutAfter === 0 || self._offset >= self._chunkSize) {
582 availOutBefore = self._chunkSize;
584 self._buffer = new Buffer(self._chunkSize);
587 if (availOutAfter === 0) {
588 // Not actually done. Need to reprocess.
589 // Also, update the availInBefore to the availInAfter value,
590 // so that if we have to hit it a third (fourth, etc.) time,
591 // it'll have the correct byte counts.
592 inOff += (availInBefore - availInAfter);
593 availInBefore = availInAfter;
598 var newReq = self._handle.write(flushFlag,
605 newReq.callback = callback; // this same function
606 newReq.buffer = chunk;
613 // finished with the chunk.
618 util.inherits(Deflate, Zlib);
619 util.inherits(Inflate, Zlib);
620 util.inherits(Gzip, Zlib);
621 util.inherits(Gunzip, Zlib);
622 util.inherits(DeflateRaw, Zlib);
623 util.inherits(InflateRaw, Zlib);
624 util.inherits(Unzip, Zlib);