2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-29 17:35:43 +00:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2015-01-21 16:36:59 +00:00
|
|
|
const Transform = require('_stream_transform');
|
|
|
|
const binding = process.binding('zlib');
|
|
|
|
const util = require('util');
|
|
|
|
const assert = require('assert').ok;
|
2015-06-17 17:34:12 +00:00
|
|
|
const kMaxLength = require('buffer').kMaxLength;
|
2015-05-27 11:21:56 +00:00
|
|
|
const kRangeErrorMessage = 'Cannot create final Buffer. ' +
|
2015-10-14 21:10:25 +00:00
|
|
|
'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes';
|
2011-09-06 23:13:05 +00:00
|
|
|
|
|
|
|
// zlib doesn't provide these, so kludge them in following the same
|
|
|
|
// const naming scheme zlib uses.
|
|
|
|
binding.Z_MIN_WINDOWBITS = 8;
|
|
|
|
binding.Z_MAX_WINDOWBITS = 15;
|
|
|
|
binding.Z_DEFAULT_WINDOWBITS = 15;
|
|
|
|
|
|
|
|
// fewer than 64 bytes per chunk is stupid.
|
|
|
|
// technically it could work with as few as 8, but even 64 bytes
|
|
|
|
// is absurdly low. Usually a MB or more is best.
|
|
|
|
binding.Z_MIN_CHUNK = 64;
|
|
|
|
binding.Z_MAX_CHUNK = Infinity;
|
|
|
|
binding.Z_DEFAULT_CHUNK = (16 * 1024);
|
|
|
|
|
|
|
|
binding.Z_MIN_MEMLEVEL = 1;
|
|
|
|
binding.Z_MAX_MEMLEVEL = 9;
|
|
|
|
binding.Z_DEFAULT_MEMLEVEL = 8;
|
|
|
|
|
|
|
|
binding.Z_MIN_LEVEL = -1;
|
|
|
|
binding.Z_MAX_LEVEL = 9;
|
|
|
|
binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
|
|
|
|
|
|
|
|
// expose all the zlib constants
|
2015-01-21 16:36:59 +00:00
|
|
|
const bkeys = Object.keys(binding);
|
2014-08-14 03:15:24 +00:00
|
|
|
for (var bk = 0; bk < bkeys.length; bk++) {
|
|
|
|
var bkey = bkeys[bk];
|
2015-04-07 05:48:52 +00:00
|
|
|
if (bkey.match(/^Z/)) {
|
|
|
|
Object.defineProperty(exports, bkey, {
|
|
|
|
enumerable: true, value: binding[bkey], writable: false
|
|
|
|
});
|
|
|
|
}
|
2014-08-14 03:15:24 +00:00
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2012-04-01 04:01:55 +00:00
|
|
|
// translation table for return codes.
|
2015-04-07 05:48:52 +00:00
|
|
|
const codes = {
|
2012-04-01 04:01:55 +00:00
|
|
|
Z_OK: binding.Z_OK,
|
|
|
|
Z_STREAM_END: binding.Z_STREAM_END,
|
|
|
|
Z_NEED_DICT: binding.Z_NEED_DICT,
|
|
|
|
Z_ERRNO: binding.Z_ERRNO,
|
|
|
|
Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
|
|
|
|
Z_DATA_ERROR: binding.Z_DATA_ERROR,
|
|
|
|
Z_MEM_ERROR: binding.Z_MEM_ERROR,
|
|
|
|
Z_BUF_ERROR: binding.Z_BUF_ERROR,
|
|
|
|
Z_VERSION_ERROR: binding.Z_VERSION_ERROR
|
|
|
|
};
|
|
|
|
|
2015-04-07 05:48:52 +00:00
|
|
|
const ckeys = Object.keys(codes);
|
2014-08-14 03:15:24 +00:00
|
|
|
for (var ck = 0; ck < ckeys.length; ck++) {
|
|
|
|
var ckey = ckeys[ck];
|
2015-04-07 05:48:52 +00:00
|
|
|
codes[codes[ckey]] = ckey;
|
2014-08-14 03:15:24 +00:00
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2015-04-07 05:48:52 +00:00
|
|
|
Object.defineProperty(exports, 'codes', {
|
|
|
|
enumerable: true, value: Object.freeze(codes), writable: false
|
|
|
|
});
|
|
|
|
|
2011-09-06 23:13:05 +00:00
|
|
|
exports.Deflate = Deflate;
|
|
|
|
exports.Inflate = Inflate;
|
|
|
|
exports.Gzip = Gzip;
|
|
|
|
exports.Gunzip = Gunzip;
|
|
|
|
exports.DeflateRaw = DeflateRaw;
|
|
|
|
exports.InflateRaw = InflateRaw;
|
|
|
|
exports.Unzip = Unzip;
|
|
|
|
|
2011-09-18 06:03:36 +00:00
|
|
|
exports.createDeflate = function(o) {
|
|
|
|
return new Deflate(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createInflate = function(o) {
|
|
|
|
return new Inflate(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createDeflateRaw = function(o) {
|
|
|
|
return new DeflateRaw(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createInflateRaw = function(o) {
|
|
|
|
return new InflateRaw(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createGzip = function(o) {
|
|
|
|
return new Gzip(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createGunzip = function(o) {
|
|
|
|
return new Gunzip(o);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.createUnzip = function(o) {
|
|
|
|
return new Unzip(o);
|
|
|
|
};
|
2011-09-06 23:13:05 +00:00
|
|
|
|
|
|
|
|
2011-10-24 16:29:24 +00:00
|
|
|
// Convenience methods.
|
|
|
|
// compress/decompress a string or buffer in one step.
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.deflate = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new Deflate(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.deflateSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new Deflate(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.gzip = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new Gzip(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.gzipSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new Gzip(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.deflateRaw = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new DeflateRaw(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.deflateRawSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new DeflateRaw(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.unzip = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new Unzip(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.unzipSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new Unzip(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.inflate = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new Inflate(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.inflateSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new Inflate(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.gunzip = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new Gunzip(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.gunzipSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new Gunzip(opts), buffer);
|
|
|
|
};
|
|
|
|
|
2013-03-25 19:15:10 +00:00
|
|
|
exports.inflateRaw = function(buffer, opts, callback) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts === 'function') {
|
2013-03-25 19:15:10 +00:00
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
return zlibBuffer(new InflateRaw(opts), buffer, callback);
|
2011-10-24 16:29:24 +00:00
|
|
|
};
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
exports.inflateRawSync = function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new InflateRaw(opts), buffer);
|
|
|
|
};
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
function zlibBuffer(engine, buffer, callback) {
|
2011-10-24 16:29:24 +00:00
|
|
|
var buffers = [];
|
|
|
|
var nread = 0;
|
|
|
|
|
2012-10-02 23:15:39 +00:00
|
|
|
engine.on('error', onError);
|
|
|
|
engine.on('end', onEnd);
|
|
|
|
|
|
|
|
engine.end(buffer);
|
|
|
|
flow();
|
|
|
|
|
|
|
|
function flow() {
|
|
|
|
var chunk;
|
|
|
|
while (null !== (chunk = engine.read())) {
|
|
|
|
buffers.push(chunk);
|
|
|
|
nread += chunk.length;
|
|
|
|
}
|
|
|
|
engine.once('readable', flow);
|
|
|
|
}
|
|
|
|
|
2012-01-20 14:36:28 +00:00
|
|
|
function onError(err) {
|
|
|
|
engine.removeListener('end', onEnd);
|
2012-10-02 23:15:39 +00:00
|
|
|
engine.removeListener('readable', flow);
|
2011-10-24 16:29:24 +00:00
|
|
|
callback(err);
|
2012-01-20 14:36:28 +00:00
|
|
|
}
|
2011-10-24 16:29:24 +00:00
|
|
|
|
2012-01-20 14:36:28 +00:00
|
|
|
function onEnd() {
|
2015-05-27 11:21:56 +00:00
|
|
|
var buf;
|
|
|
|
var err = null;
|
|
|
|
|
|
|
|
if (nread >= kMaxLength) {
|
|
|
|
err = new RangeError(kRangeErrorMessage);
|
|
|
|
} else {
|
|
|
|
buf = Buffer.concat(buffers, nread);
|
|
|
|
}
|
|
|
|
|
zlib: reduce memory consumption, release early
In zlibBuffer(), don't wait for the garbage collector to reclaim the zlib memory
but release it manually. Reduces memory consumption by a factor of 10 or more
with some workloads.
Test case:
function f() {
require('zlib').deflate('xxx', g);
}
function g() {
setTimeout(f, 5);
}
f();
Observe RSS memory usage with and without this commit. After 10,000 iterations,
RSS stabilizes at ~35 MB with this commit. Without, RSS is over 300 MB and keeps
growing.
Cause: whenever the JS object heap hits the high-water mark, the V8 GC sweeps
it clean, then tries to grow it in order to avoid more sweeps in the near
future. Rule of thumb: the bigger the JS heap, the lazier the GC can be.
A side effect of a bigger heap is that objects now live longer. This is harmless
in general but it affects zlib context objects because those are tied to large
buffers that live outside the JS heap, on the order of 16K per context object.
Ergo, don't wait for the GC to reclaim the memory - it may take a long time.
Fixes #4172.
2012-10-30 00:19:01 +00:00
|
|
|
buffers = [];
|
2013-02-07 00:29:30 +00:00
|
|
|
engine.close();
|
2015-05-27 11:21:56 +00:00
|
|
|
callback(err, buf);
|
2012-01-20 14:36:28 +00:00
|
|
|
}
|
2011-10-24 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
function zlibBufferSync(engine, buffer) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof buffer === 'string')
|
2014-01-28 17:35:51 +00:00
|
|
|
buffer = new Buffer(buffer);
|
2015-01-29 01:05:53 +00:00
|
|
|
if (!(buffer instanceof Buffer))
|
2014-01-28 17:35:51 +00:00
|
|
|
throw new TypeError('Not a string or buffer');
|
|
|
|
|
|
|
|
var flushFlag = binding.Z_FINISH;
|
|
|
|
|
|
|
|
return engine._processChunk(buffer, flushFlag);
|
|
|
|
}
|
2011-10-24 16:29:24 +00:00
|
|
|
|
2011-09-06 23:13:05 +00:00
|
|
|
// generic zlib
|
|
|
|
// minimal 2-byte header
|
|
|
|
function Deflate(opts) {
|
2011-09-26 18:50:20 +00:00
|
|
|
if (!(this instanceof Deflate)) return new Deflate(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.DEFLATE);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Inflate(opts) {
|
|
|
|
if (!(this instanceof Inflate)) return new Inflate(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.INFLATE);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// gzip - bigger header, same deflate compression
|
|
|
|
function Gzip(opts) {
|
|
|
|
if (!(this instanceof Gzip)) return new Gzip(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.GZIP);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Gunzip(opts) {
|
|
|
|
if (!(this instanceof Gunzip)) return new Gunzip(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.GUNZIP);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// raw - no header
|
|
|
|
function DeflateRaw(opts) {
|
|
|
|
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.DEFLATERAW);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InflateRaw(opts) {
|
|
|
|
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.INFLATERAW);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// auto-detect header.
|
|
|
|
function Unzip(opts) {
|
|
|
|
if (!(this instanceof Unzip)) return new Unzip(opts);
|
2012-03-21 15:35:47 +00:00
|
|
|
Zlib.call(this, opts, binding.UNZIP);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// the Zlib class they all inherit from
|
|
|
|
// This thing manages the queue of requests, and returns
|
|
|
|
// true or false if there is anything in the queue when
|
|
|
|
// you call the .write() method.
|
|
|
|
|
2012-03-21 15:35:47 +00:00
|
|
|
function Zlib(opts, mode) {
|
2011-09-06 23:13:05 +00:00
|
|
|
this._opts = opts = opts || {};
|
2012-10-02 23:15:39 +00:00
|
|
|
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
|
|
|
|
|
|
|
|
Transform.call(this, opts);
|
|
|
|
|
2013-03-08 15:55:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
|
|
|
if (opts.chunkSize) {
|
|
|
|
if (opts.chunkSize < exports.Z_MIN_CHUNK ||
|
|
|
|
opts.chunkSize > exports.Z_MAX_CHUNK) {
|
2011-09-18 06:03:36 +00:00
|
|
|
throw new Error('Invalid chunk size: ' + opts.chunkSize);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.windowBits) {
|
|
|
|
if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
|
|
|
|
opts.windowBits > exports.Z_MAX_WINDOWBITS) {
|
2011-09-18 06:03:36 +00:00
|
|
|
throw new Error('Invalid windowBits: ' + opts.windowBits);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.level) {
|
|
|
|
if (opts.level < exports.Z_MIN_LEVEL ||
|
|
|
|
opts.level > exports.Z_MAX_LEVEL) {
|
2011-09-18 06:03:36 +00:00
|
|
|
throw new Error('Invalid compression level: ' + opts.level);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.memLevel) {
|
|
|
|
if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
|
|
|
|
opts.memLevel > exports.Z_MAX_MEMLEVEL) {
|
2011-09-18 06:03:36 +00:00
|
|
|
throw new Error('Invalid memLevel: ' + opts.memLevel);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.strategy) {
|
|
|
|
if (opts.strategy != exports.Z_FILTERED &&
|
|
|
|
opts.strategy != exports.Z_HUFFMAN_ONLY &&
|
|
|
|
opts.strategy != exports.Z_RLE &&
|
|
|
|
opts.strategy != exports.Z_FIXED &&
|
|
|
|
opts.strategy != exports.Z_DEFAULT_STRATEGY) {
|
2011-09-18 06:03:36 +00:00
|
|
|
throw new Error('Invalid strategy: ' + opts.strategy);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 07:53:56 +00:00
|
|
|
if (opts.dictionary) {
|
2015-01-29 01:05:53 +00:00
|
|
|
if (!(opts.dictionary instanceof Buffer)) {
|
2011-12-02 07:53:56 +00:00
|
|
|
throw new Error('Invalid dictionary: it should be a Buffer instance');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 22:57:16 +00:00
|
|
|
this._handle = new binding.Zlib(mode);
|
2012-04-01 04:01:55 +00:00
|
|
|
|
|
|
|
var self = this;
|
2012-10-02 23:15:39 +00:00
|
|
|
this._hadError = false;
|
2014-04-30 22:57:16 +00:00
|
|
|
this._handle.onerror = function(message, errno) {
|
2012-04-01 04:01:55 +00:00
|
|
|
// there is no way to cleanly recover.
|
|
|
|
// continuing only obscures problems.
|
2014-04-30 22:57:16 +00:00
|
|
|
self._handle = null;
|
2012-04-01 04:01:55 +00:00
|
|
|
self._hadError = true;
|
|
|
|
|
|
|
|
var error = new Error(message);
|
|
|
|
error.errno = errno;
|
|
|
|
error.code = exports.codes[errno];
|
|
|
|
self.emit('error', error);
|
|
|
|
};
|
|
|
|
|
2013-07-01 09:42:19 +00:00
|
|
|
var level = exports.Z_DEFAULT_COMPRESSION;
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts.level === 'number') level = opts.level;
|
2013-07-01 09:42:19 +00:00
|
|
|
|
|
|
|
var strategy = exports.Z_DEFAULT_STRATEGY;
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof opts.strategy === 'number') strategy = opts.strategy;
|
2013-07-01 09:42:19 +00:00
|
|
|
|
2014-04-30 22:57:16 +00:00
|
|
|
this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
|
|
|
|
level,
|
|
|
|
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
|
|
|
|
strategy,
|
|
|
|
opts.dictionary);
|
2011-09-06 23:13:05 +00:00
|
|
|
|
|
|
|
this._buffer = new Buffer(this._chunkSize);
|
|
|
|
this._offset = 0;
|
2012-10-30 17:16:28 +00:00
|
|
|
this._closed = false;
|
2013-07-01 09:44:17 +00:00
|
|
|
this._level = level;
|
|
|
|
this._strategy = strategy;
|
2012-10-30 17:16:28 +00:00
|
|
|
|
|
|
|
this.once('end', this.close);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 23:15:39 +00:00
|
|
|
util.inherits(Zlib, Transform);
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2013-07-01 09:44:17 +00:00
|
|
|
Zlib.prototype.params = function(level, strategy, callback) {
|
|
|
|
if (level < exports.Z_MIN_LEVEL ||
|
|
|
|
level > exports.Z_MAX_LEVEL) {
|
|
|
|
throw new RangeError('Invalid compression level: ' + level);
|
|
|
|
}
|
|
|
|
if (strategy != exports.Z_FILTERED &&
|
|
|
|
strategy != exports.Z_HUFFMAN_ONLY &&
|
|
|
|
strategy != exports.Z_RLE &&
|
|
|
|
strategy != exports.Z_FIXED &&
|
|
|
|
strategy != exports.Z_DEFAULT_STRATEGY) {
|
|
|
|
throw new TypeError('Invalid strategy: ' + strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._level !== level || this._strategy !== strategy) {
|
|
|
|
var self = this;
|
|
|
|
this.flush(binding.Z_SYNC_FLUSH, function() {
|
2014-07-29 08:50:49 +00:00
|
|
|
assert(!self._closed, 'zlib binding closed');
|
2014-04-30 22:57:16 +00:00
|
|
|
self._handle.params(level, strategy);
|
2013-07-01 09:44:17 +00:00
|
|
|
if (!self._hadError) {
|
|
|
|
self._level = level;
|
|
|
|
self._strategy = strategy;
|
|
|
|
if (callback) callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
process.nextTick(callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Zlib.prototype.reset = function() {
|
2014-06-12 04:11:28 +00:00
|
|
|
assert(!this._closed, 'zlib binding closed');
|
2014-04-30 22:57:16 +00:00
|
|
|
return this._handle.reset();
|
2012-01-11 21:12:13 +00:00
|
|
|
};
|
|
|
|
|
2013-03-08 15:55:45 +00:00
|
|
|
// This is the _flush function called by the transform class,
|
|
|
|
// internally, when the last chunk has been written.
|
2013-03-04 03:05:44 +00:00
|
|
|
Zlib.prototype._flush = function(callback) {
|
2013-03-08 15:55:45 +00:00
|
|
|
this._transform(new Buffer(0), '', callback);
|
2011-09-06 23:13:05 +00:00
|
|
|
};
|
|
|
|
|
2013-07-01 09:44:03 +00:00
|
|
|
Zlib.prototype.flush = function(kind, callback) {
|
2012-10-02 23:15:39 +00:00
|
|
|
var ws = this._writableState;
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
2013-07-01 09:44:03 +00:00
|
|
|
callback = kind;
|
|
|
|
kind = binding.Z_FULL_FLUSH;
|
|
|
|
}
|
|
|
|
|
2013-03-08 15:55:45 +00:00
|
|
|
if (ws.ended) {
|
|
|
|
if (callback)
|
|
|
|
process.nextTick(callback);
|
|
|
|
} else if (ws.ending) {
|
|
|
|
if (callback)
|
|
|
|
this.once('end', callback);
|
|
|
|
} else if (ws.needDrain) {
|
2015-10-26 21:40:56 +00:00
|
|
|
if (callback) {
|
|
|
|
this.once('drain', () => this.flush(kind, callback));
|
|
|
|
}
|
2013-03-08 15:55:45 +00:00
|
|
|
} else {
|
2013-07-01 09:44:03 +00:00
|
|
|
this._flushFlag = kind;
|
2013-03-08 15:55:45 +00:00
|
|
|
this.write(new Buffer(0), '', callback);
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
};
|
|
|
|
|
2012-10-30 17:16:28 +00:00
|
|
|
Zlib.prototype.close = function(callback) {
|
|
|
|
if (callback)
|
|
|
|
process.nextTick(callback);
|
|
|
|
|
|
|
|
if (this._closed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._closed = true;
|
|
|
|
|
2014-04-30 22:57:16 +00:00
|
|
|
this._handle.close();
|
2012-10-30 17:16:28 +00:00
|
|
|
|
2015-03-05 21:07:27 +00:00
|
|
|
process.nextTick(emitCloseNT, this);
|
zlib: reduce memory consumption, release early
In zlibBuffer(), don't wait for the garbage collector to reclaim the zlib memory
but release it manually. Reduces memory consumption by a factor of 10 or more
with some workloads.
Test case:
function f() {
require('zlib').deflate('xxx', g);
}
function g() {
setTimeout(f, 5);
}
f();
Observe RSS memory usage with and without this commit. After 10,000 iterations,
RSS stabilizes at ~35 MB with this commit. Without, RSS is over 300 MB and keeps
growing.
Cause: whenever the JS object heap hits the high-water mark, the V8 GC sweeps
it clean, then tries to grow it in order to avoid more sweeps in the near
future. Rule of thumb: the bigger the JS heap, the lazier the GC can be.
A side effect of a bigger heap is that objects now live longer. This is harmless
in general but it affects zlib context objects because those are tied to large
buffers that live outside the JS heap, on the order of 16K per context object.
Ergo, don't wait for the GC to reclaim the memory - it may take a long time.
Fixes #4172.
2012-10-30 00:19:01 +00:00
|
|
|
};
|
|
|
|
|
2015-03-05 21:07:27 +00:00
|
|
|
function emitCloseNT(self) {
|
|
|
|
self.emit('close');
|
|
|
|
}
|
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
Zlib.prototype._transform = function(chunk, encoding, cb) {
|
2012-10-02 23:15:39 +00:00
|
|
|
var flushFlag;
|
|
|
|
var ws = this._writableState;
|
|
|
|
var ending = ws.ending || ws.ended;
|
|
|
|
var last = ending && (!chunk || ws.length === chunk.length);
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (chunk !== null && !(chunk instanceof Buffer))
|
2012-10-02 23:15:39 +00:00
|
|
|
return cb(new Error('invalid input'));
|
|
|
|
|
2014-07-29 08:50:49 +00:00
|
|
|
if (this._closed)
|
|
|
|
return cb(new Error('zlib binding closed'));
|
|
|
|
|
2012-10-02 23:15:39 +00:00
|
|
|
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
|
|
|
|
// 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;
|
2013-03-08 15:55:45 +00:00
|
|
|
else {
|
|
|
|
flushFlag = this._flushFlag;
|
|
|
|
// once we've flushed the last of the queue, stop flushing and
|
|
|
|
// go back to the normal behavior.
|
|
|
|
if (chunk.length >= ws.length) {
|
|
|
|
this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
|
|
|
|
}
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2014-01-28 17:35:51 +00:00
|
|
|
this._processChunk(chunk, flushFlag, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
|
2011-09-06 23:13:05 +00:00
|
|
|
var availInBefore = chunk && chunk.length;
|
|
|
|
var availOutBefore = this._chunkSize - this._offset;
|
|
|
|
var inOff = 0;
|
2012-10-02 23:15:39 +00:00
|
|
|
|
2014-01-28 17:35:51 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
var async = typeof cb === 'function';
|
2014-01-28 17:35:51 +00:00
|
|
|
|
|
|
|
if (!async) {
|
|
|
|
var buffers = [];
|
|
|
|
var nread = 0;
|
|
|
|
|
|
|
|
var error;
|
|
|
|
this.on('error', function(er) {
|
|
|
|
error = er;
|
|
|
|
});
|
|
|
|
|
2014-07-29 08:50:49 +00:00
|
|
|
assert(!this._closed, 'zlib binding closed');
|
2014-01-28 17:35:51 +00:00
|
|
|
do {
|
2014-04-30 22:57:16 +00:00
|
|
|
var res = this._handle.writeSync(flushFlag,
|
|
|
|
chunk, // in
|
|
|
|
inOff, // in_off
|
|
|
|
availInBefore, // in_len
|
|
|
|
this._buffer, // out
|
|
|
|
this._offset, //out_off
|
|
|
|
availOutBefore); // out_len
|
2014-01-28 17:35:51 +00:00
|
|
|
} while (!this._hadError && callback(res[0], res[1]));
|
|
|
|
|
|
|
|
if (this._hadError) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:21:56 +00:00
|
|
|
if (nread >= kMaxLength) {
|
|
|
|
this.close();
|
|
|
|
throw new RangeError(kRangeErrorMessage);
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:35:51 +00:00
|
|
|
var buf = Buffer.concat(buffers, nread);
|
|
|
|
this.close();
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-07-29 08:50:49 +00:00
|
|
|
assert(!this._closed, 'zlib binding closed');
|
2014-04-30 22:57:16 +00:00
|
|
|
var req = this._handle.write(flushFlag,
|
|
|
|
chunk, // in
|
|
|
|
inOff, // in_off
|
|
|
|
availInBefore, // in_len
|
|
|
|
this._buffer, // out
|
|
|
|
this._offset, //out_off
|
|
|
|
availOutBefore); // out_len
|
2011-09-06 23:13:05 +00:00
|
|
|
|
|
|
|
req.buffer = chunk;
|
|
|
|
req.callback = callback;
|
|
|
|
|
2013-07-01 09:42:57 +00:00
|
|
|
function callback(availInAfter, availOutAfter) {
|
2012-10-02 23:15:39 +00:00
|
|
|
if (self._hadError)
|
|
|
|
return;
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2011-09-18 06:03:23 +00:00
|
|
|
var have = availOutBefore - availOutAfter;
|
|
|
|
assert(have >= 0, 'have should not go down');
|
|
|
|
|
2011-09-06 23:13:05 +00:00
|
|
|
if (have > 0) {
|
|
|
|
var out = self._buffer.slice(self._offset, self._offset + have);
|
|
|
|
self._offset += have;
|
2012-10-02 23:15:39 +00:00
|
|
|
// serve some output to the consumer.
|
2014-01-28 17:35:51 +00:00
|
|
|
if (async) {
|
|
|
|
self.push(out);
|
|
|
|
} else {
|
|
|
|
buffers.push(out);
|
|
|
|
nread += out.length;
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 23:15:39 +00:00
|
|
|
// exhausted the output buffer, or used all the input create a new one.
|
2011-09-06 23:13:05 +00:00
|
|
|
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
2011-09-18 06:03:23 +00:00
|
|
|
availOutBefore = self._chunkSize;
|
2011-09-06 23:13:05 +00:00
|
|
|
self._offset = 0;
|
|
|
|
self._buffer = new Buffer(self._chunkSize);
|
|
|
|
}
|
|
|
|
|
2015-01-06 21:15:09 +00:00
|
|
|
if (availOutAfter === 0) {
|
2011-09-06 23:13:05 +00:00
|
|
|
// Not actually done. Need to reprocess.
|
2011-10-13 23:37:49 +00:00
|
|
|
// Also, update the availInBefore to the availInAfter value,
|
|
|
|
// so that if we have to hit it a third (fourth, etc.) time,
|
|
|
|
// it'll have the correct byte counts.
|
2011-09-06 23:13:05 +00:00
|
|
|
inOff += (availInBefore - availInAfter);
|
2011-10-13 23:37:49 +00:00
|
|
|
availInBefore = availInAfter;
|
|
|
|
|
2014-01-28 17:35:51 +00:00
|
|
|
if (!async)
|
|
|
|
return true;
|
|
|
|
|
2014-04-30 22:57:16 +00:00
|
|
|
var newReq = self._handle.write(flushFlag,
|
|
|
|
chunk,
|
|
|
|
inOff,
|
|
|
|
availInBefore,
|
|
|
|
self._buffer,
|
|
|
|
self._offset,
|
|
|
|
self._chunkSize);
|
2011-09-06 23:13:05 +00:00
|
|
|
newReq.callback = callback; // this same function
|
|
|
|
newReq.buffer = chunk;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:35:51 +00:00
|
|
|
if (!async)
|
|
|
|
return false;
|
|
|
|
|
2011-09-06 23:13:05 +00:00
|
|
|
// finished with the chunk.
|
2012-10-02 23:15:39 +00:00
|
|
|
cb();
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
util.inherits(Deflate, Zlib);
|
|
|
|
util.inherits(Inflate, Zlib);
|
|
|
|
util.inherits(Gzip, Zlib);
|
|
|
|
util.inherits(Gunzip, Zlib);
|
|
|
|
util.inherits(DeflateRaw, Zlib);
|
|
|
|
util.inherits(InflateRaw, Zlib);
|
|
|
|
util.inherits(Unzip, Zlib);
|