2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-29 17:35:43 +00:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2017-02-15 01:36:12 +00:00
|
|
|
const internalUtil = require('internal/util');
|
2015-01-21 16:36:59 +00:00
|
|
|
const Transform = require('_stream_transform');
|
2017-03-23 02:45:03 +00:00
|
|
|
const { isUint8Array } = process.binding('util');
|
2015-01-21 16:36:59 +00:00
|
|
|
const binding = process.binding('zlib');
|
|
|
|
const assert = require('assert').ok;
|
2015-06-17 17:34:12 +00:00
|
|
|
const kMaxLength = require('buffer').kMaxLength;
|
2016-04-16 04:21:12 +00:00
|
|
|
const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' +
|
2017-02-15 01:36:12 +00:00
|
|
|
`than 0x${kMaxLength.toString(16)} bytes`;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2016-06-07 21:38:00 +00:00
|
|
|
const constants = process.binding('constants').zlib;
|
2017-02-15 01:36:12 +00:00
|
|
|
const createClassWrapper = internalUtil.createClassWrapper;
|
2016-06-07 21:38:00 +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 = {
|
2016-06-07 21:38:00 +00:00
|
|
|
Z_OK: constants.Z_OK,
|
|
|
|
Z_STREAM_END: constants.Z_STREAM_END,
|
|
|
|
Z_NEED_DICT: constants.Z_NEED_DICT,
|
|
|
|
Z_ERRNO: constants.Z_ERRNO,
|
|
|
|
Z_STREAM_ERROR: constants.Z_STREAM_ERROR,
|
|
|
|
Z_DATA_ERROR: constants.Z_DATA_ERROR,
|
|
|
|
Z_MEM_ERROR: constants.Z_MEM_ERROR,
|
|
|
|
Z_BUF_ERROR: constants.Z_BUF_ERROR,
|
|
|
|
Z_VERSION_ERROR: constants.Z_VERSION_ERROR
|
2012-04-01 04:01:55 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2017-02-15 01:58:15 +00:00
|
|
|
function isInvalidFlushFlag(flag) {
|
|
|
|
return typeof flag !== 'number' ||
|
|
|
|
flag < constants.Z_NO_FLUSH ||
|
|
|
|
flag > constants.Z_BLOCK;
|
2017-02-15 01:36:12 +00:00
|
|
|
|
|
|
|
// Covers: constants.Z_NO_FLUSH (0),
|
|
|
|
// constants.Z_PARTIAL_FLUSH (1),
|
|
|
|
// constants.Z_SYNC_FLUSH (2),
|
|
|
|
// constants.Z_FULL_FLUSH (3),
|
|
|
|
// constants.Z_FINISH (4), and
|
|
|
|
// constants.Z_BLOCK (5)
|
|
|
|
}
|
2014-02-03 07:55:47 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
function isInvalidStrategy(strategy) {
|
|
|
|
return typeof strategy !== 'number' ||
|
|
|
|
strategy < constants.Z_DEFAULT_STRATEGY ||
|
|
|
|
strategy > constants.Z_FIXED;
|
2011-10-24 16:29:24 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// Covers: constants.Z_FILTERED, (1)
|
|
|
|
// constants.Z_HUFFMAN_ONLY (2),
|
|
|
|
// constants.Z_RLE (3),
|
|
|
|
// constants.Z_FIXED (4), and
|
|
|
|
// constants.Z_DEFAULT_STRATEGY (0)
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2014-02-03 07:55:47 +00:00
|
|
|
function zlibBuffer(engine, buffer, callback) {
|
2017-03-23 02:45:03 +00:00
|
|
|
// Streams do not support non-Buffer Uint8Arrays yet. Convert it to a
|
|
|
|
// Buffer without copying.
|
|
|
|
if (isUint8Array(buffer) &&
|
|
|
|
Object.getPrototypeOf(buffer) !== Buffer.prototype) {
|
|
|
|
buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
|
|
}
|
|
|
|
|
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')
|
2016-01-25 23:00:06 +00:00
|
|
|
buffer = Buffer.from(buffer);
|
2017-03-23 02:45:03 +00:00
|
|
|
else if (!isUint8Array(buffer))
|
|
|
|
throw new TypeError('"buffer" argument must be a string, Buffer, or ' +
|
|
|
|
'Uint8Array');
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2016-04-05 22:54:34 +00:00
|
|
|
var flushFlag = engine._finishFlushFlag;
|
2014-01-28 17:35:51 +00:00
|
|
|
|
|
|
|
return engine._processChunk(buffer, flushFlag);
|
|
|
|
}
|
2011-10-24 16:29:24 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
function zlibOnError(message, errno) {
|
|
|
|
// there is no way to cleanly recover.
|
|
|
|
// continuing only obscures problems.
|
|
|
|
_close(this);
|
|
|
|
this._hadError = true;
|
|
|
|
|
|
|
|
var error = new Error(message);
|
|
|
|
error.errno = errno;
|
|
|
|
error.code = codes[errno];
|
|
|
|
this.emit('error', error);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
function flushCallback(level, strategy, callback) {
|
|
|
|
assert(this._handle, 'zlib binding closed');
|
|
|
|
this._handle.params(level, strategy);
|
|
|
|
if (!this._hadError) {
|
|
|
|
this._level = level;
|
|
|
|
this._strategy = strategy;
|
|
|
|
if (callback) callback();
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +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.
|
|
|
|
class Zlib extends Transform {
|
|
|
|
constructor(opts, mode) {
|
|
|
|
opts = opts || {};
|
|
|
|
super(opts);
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._opts = opts;
|
|
|
|
this._chunkSize = opts.chunkSize || constants.Z_DEFAULT_CHUNK;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:58:15 +00:00
|
|
|
if (opts.flush && isInvalidFlushFlag(opts.flush)) {
|
|
|
|
throw new RangeError('Invalid flush flag: ' + opts.flush);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
2017-02-15 01:58:15 +00:00
|
|
|
if (opts.finishFlush && isInvalidFlushFlag(opts.finishFlush)) {
|
|
|
|
throw new RangeError('Invalid flush flag: ' + opts.finishFlush);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._flushFlag = opts.flush || constants.Z_NO_FLUSH;
|
|
|
|
this._finishFlushFlag = opts.finishFlush !== undefined ?
|
|
|
|
opts.finishFlush : constants.Z_FINISH;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.chunkSize) {
|
|
|
|
if (opts.chunkSize < constants.Z_MIN_CHUNK) {
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new RangeError('Invalid chunk size: ' + opts.chunkSize);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.windowBits) {
|
|
|
|
if (opts.windowBits < constants.Z_MIN_WINDOWBITS ||
|
|
|
|
opts.windowBits > constants.Z_MAX_WINDOWBITS) {
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new RangeError('Invalid windowBits: ' + opts.windowBits);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.level) {
|
|
|
|
if (opts.level < constants.Z_MIN_LEVEL ||
|
|
|
|
opts.level > constants.Z_MAX_LEVEL) {
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new RangeError('Invalid compression level: ' + opts.level);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.memLevel) {
|
|
|
|
if (opts.memLevel < constants.Z_MIN_MEMLEVEL ||
|
|
|
|
opts.memLevel > constants.Z_MAX_MEMLEVEL) {
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new RangeError('Invalid memLevel: ' + opts.memLevel);
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.strategy && isInvalidStrategy(opts.strategy))
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new TypeError('Invalid strategy: ' + opts.strategy);
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (opts.dictionary) {
|
2017-03-23 02:45:03 +00:00
|
|
|
if (!isUint8Array(opts.dictionary)) {
|
2017-02-15 01:58:15 +00:00
|
|
|
throw new TypeError(
|
2017-03-23 02:45:03 +00:00
|
|
|
'Invalid dictionary: it should be a Buffer or an Uint8Array');
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-21 05:54:56 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._handle = new binding.Zlib(mode);
|
|
|
|
this._handle.onerror = zlibOnError.bind(this);
|
|
|
|
this._hadError = false;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
var level = constants.Z_DEFAULT_COMPRESSION;
|
|
|
|
if (typeof opts.level === 'number') level = opts.level;
|
2012-10-02 23:15:39 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
var strategy = constants.Z_DEFAULT_STRATEGY;
|
|
|
|
if (typeof opts.strategy === 'number') strategy = opts.strategy;
|
2012-10-02 23:15:39 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._handle.init(opts.windowBits || constants.Z_DEFAULT_WINDOWBITS,
|
|
|
|
level,
|
|
|
|
opts.memLevel || constants.Z_DEFAULT_MEMLEVEL,
|
|
|
|
strategy,
|
|
|
|
opts.dictionary);
|
2016-04-05 22:54:34 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._buffer = Buffer.allocUnsafe(this._chunkSize);
|
|
|
|
this._offset = 0;
|
|
|
|
this._level = level;
|
|
|
|
this._strategy = strategy;
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this.once('end', this.close);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
get _closed() {
|
|
|
|
return !this._handle;
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
params(level, strategy, callback) {
|
|
|
|
if (level < constants.Z_MIN_LEVEL ||
|
|
|
|
level > constants.Z_MAX_LEVEL) {
|
|
|
|
throw new RangeError('Invalid compression level: ' + level);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
if (isInvalidStrategy(strategy))
|
|
|
|
throw new TypeError('Invalid strategy: ' + strategy);
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (this._level !== level || this._strategy !== strategy) {
|
|
|
|
this.flush(constants.Z_SYNC_FLUSH,
|
|
|
|
flushCallback.bind(this, level, strategy, callback));
|
|
|
|
} else {
|
|
|
|
process.nextTick(callback);
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
reset() {
|
|
|
|
assert(this._handle, 'zlib binding closed');
|
|
|
|
return this._handle.reset();
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// This is the _flush function called by the transform class,
|
|
|
|
// internally, when the last chunk has been written.
|
|
|
|
_flush(callback) {
|
|
|
|
this._transform(Buffer.alloc(0), '', callback);
|
2011-12-02 07:53:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
flush(kind, callback) {
|
|
|
|
var ws = this._writableState;
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (typeof kind === 'function' || (kind === undefined && !callback)) {
|
|
|
|
callback = kind;
|
|
|
|
kind = constants.Z_FULL_FLUSH;
|
|
|
|
}
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (ws.ended) {
|
|
|
|
if (callback)
|
|
|
|
process.nextTick(callback);
|
|
|
|
} else if (ws.ending) {
|
|
|
|
if (callback)
|
|
|
|
this.once('end', callback);
|
|
|
|
} else if (ws.needDrain) {
|
|
|
|
if (callback) {
|
|
|
|
const drainHandler = () => this.flush(kind, callback);
|
|
|
|
this.once('drain', drainHandler);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._flushFlag = kind;
|
|
|
|
this.write(Buffer.alloc(0), '', callback);
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
close(callback) {
|
|
|
|
_close(this, callback);
|
|
|
|
process.nextTick(emitCloseNT, this);
|
|
|
|
}
|
2013-07-01 09:42:19 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
_transform(chunk, encoding, cb) {
|
|
|
|
var flushFlag;
|
|
|
|
var ws = this._writableState;
|
|
|
|
var ending = ws.ending || ws.ended;
|
|
|
|
var last = ending && (!chunk || ws.length === chunk.length);
|
|
|
|
|
2017-03-23 02:45:03 +00:00
|
|
|
if (chunk !== null && !isUint8Array(chunk))
|
2017-02-15 01:58:15 +00:00
|
|
|
return cb(new TypeError('invalid input'));
|
2017-02-15 01:36:12 +00:00
|
|
|
|
|
|
|
if (!this._handle)
|
|
|
|
return cb(new Error('zlib binding closed'));
|
|
|
|
|
|
|
|
// 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 = this._finishFlushFlag;
|
|
|
|
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 || constants.Z_NO_FLUSH;
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 09:42:19 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
this._processChunk(chunk, flushFlag, cb);
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
_processChunk(chunk, flushFlag, cb) {
|
|
|
|
var availInBefore = chunk && chunk.length;
|
|
|
|
var availOutBefore = this._chunkSize - this._offset;
|
|
|
|
var inOff = 0;
|
2012-10-30 17:16:28 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
var self = this;
|
2016-05-04 19:18:21 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
var async = typeof cb === 'function';
|
|
|
|
|
|
|
|
if (!async) {
|
|
|
|
var buffers = [];
|
|
|
|
var nread = 0;
|
|
|
|
|
|
|
|
var error;
|
|
|
|
this.on('error', function(er) {
|
|
|
|
error = er;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(this._handle, 'zlib binding closed');
|
|
|
|
do {
|
|
|
|
var res = this._handle.writeSync(flushFlag,
|
|
|
|
chunk, // in
|
|
|
|
inOff, // in_off
|
|
|
|
availInBefore, // in_len
|
|
|
|
this._buffer, // out
|
|
|
|
this._offset, //out_off
|
|
|
|
availOutBefore); // out_len
|
|
|
|
} while (!this._hadError && callback(res[0], res[1]));
|
|
|
|
|
|
|
|
if (this._hadError) {
|
|
|
|
throw error;
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (nread >= kMaxLength) {
|
|
|
|
_close(this);
|
|
|
|
throw new RangeError(kRangeErrorMessage);
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
var buf = Buffer.concat(buffers, nread);
|
|
|
|
_close(this);
|
2013-07-01 09:44:17 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this._handle, 'zlib binding closed');
|
|
|
|
var req = this._handle.write(flushFlag,
|
|
|
|
chunk, // in
|
|
|
|
inOff, // in_off
|
|
|
|
availInBefore, // in_len
|
|
|
|
this._buffer, // out
|
|
|
|
this._offset, //out_off
|
|
|
|
availOutBefore); // out_len
|
|
|
|
|
|
|
|
req.buffer = chunk;
|
|
|
|
req.callback = callback;
|
|
|
|
|
|
|
|
function callback(availInAfter, availOutAfter) {
|
|
|
|
// When the callback is used in an async write, the callback's
|
|
|
|
// context is the `req` object that was created. The req object
|
|
|
|
// is === this._handle, and that's why it's important to null
|
|
|
|
// out the values after they are done being used. `this._handle`
|
|
|
|
// can stay in memory longer than the callback and buffer are needed.
|
|
|
|
if (this) {
|
|
|
|
this.buffer = null;
|
|
|
|
this.callback = null;
|
2013-07-01 09:44:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (self._hadError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var have = availOutBefore - availOutAfter;
|
|
|
|
assert(have >= 0, 'have should not go down');
|
|
|
|
|
|
|
|
if (have > 0) {
|
|
|
|
var out = self._buffer.slice(self._offset, self._offset + have);
|
|
|
|
self._offset += have;
|
|
|
|
// serve some output to the consumer.
|
|
|
|
if (async) {
|
|
|
|
self.push(out);
|
|
|
|
} else {
|
|
|
|
buffers.push(out);
|
|
|
|
nread += out.length;
|
|
|
|
}
|
|
|
|
}
|
2012-01-11 21:12:13 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// exhausted the output buffer, or used all the input create a new one.
|
|
|
|
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
|
|
|
availOutBefore = self._chunkSize;
|
|
|
|
self._offset = 0;
|
|
|
|
self._buffer = Buffer.allocUnsafe(self._chunkSize);
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (availOutAfter === 0) {
|
|
|
|
// Not actually done. Need to reprocess.
|
|
|
|
// 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.
|
|
|
|
inOff += (availInBefore - availInAfter);
|
|
|
|
availInBefore = availInAfter;
|
|
|
|
|
|
|
|
if (!async)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
var newReq = self._handle.write(flushFlag,
|
|
|
|
chunk,
|
|
|
|
inOff,
|
|
|
|
availInBefore,
|
|
|
|
self._buffer,
|
|
|
|
self._offset,
|
|
|
|
self._chunkSize);
|
|
|
|
newReq.callback = callback; // this same function
|
|
|
|
newReq.buffer = chunk;
|
|
|
|
return;
|
|
|
|
}
|
2012-04-01 04:01:55 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
if (!async)
|
|
|
|
return false;
|
2013-07-01 09:44:03 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// finished with the chunk.
|
|
|
|
cb();
|
2015-10-26 21:40:56 +00:00
|
|
|
}
|
2013-03-08 15:55:45 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
2016-03-15 00:11:07 +00:00
|
|
|
|
|
|
|
function _close(engine, callback) {
|
2012-10-30 17:16:28 +00:00
|
|
|
if (callback)
|
|
|
|
process.nextTick(callback);
|
|
|
|
|
2016-05-04 19:18:21 +00:00
|
|
|
// Caller may invoke .close after a zlib error (which will null _handle).
|
|
|
|
if (!engine._handle)
|
2012-10-30 17:16:28 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-04 19:18:21 +00:00
|
|
|
engine._handle.close();
|
|
|
|
engine._handle = null;
|
2016-03-15 00:11:07 +00:00
|
|
|
}
|
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');
|
|
|
|
}
|
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// generic zlib
|
|
|
|
// minimal 2-byte header
|
|
|
|
class Deflate extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.DEFLATE);
|
2013-03-08 15:55:45 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class Inflate extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.INFLATE);
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class Gzip extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.GZIP);
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class Gunzip extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.GUNZIP);
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 11:21:56 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class DeflateRaw extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.DEFLATERAW);
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class InflateRaw extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.INFLATERAW);
|
2014-01-28 17:35:51 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
}
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
class Unzip extends Zlib {
|
|
|
|
constructor(opts) {
|
|
|
|
super(opts, constants.UNZIP);
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 15:27:00 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
function createConvenienceMethod(type, sync) {
|
|
|
|
if (sync) {
|
|
|
|
return function(buffer, opts) {
|
|
|
|
return zlibBufferSync(new type(opts), buffer);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return function(buffer, opts, callback) {
|
|
|
|
if (typeof opts === 'function') {
|
|
|
|
callback = opts;
|
|
|
|
opts = {};
|
2014-01-28 17:35:51 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
return zlibBuffer(new type(opts), buffer, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2011-10-13 23:37:49 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
function createProperty(type) {
|
|
|
|
return {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: type
|
|
|
|
};
|
|
|
|
}
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
module.exports = {
|
|
|
|
Deflate: createClassWrapper(Deflate),
|
|
|
|
Inflate: createClassWrapper(Inflate),
|
|
|
|
Gzip: createClassWrapper(Gzip),
|
|
|
|
Gunzip: createClassWrapper(Gunzip),
|
|
|
|
DeflateRaw: createClassWrapper(DeflateRaw),
|
|
|
|
InflateRaw: createClassWrapper(InflateRaw),
|
|
|
|
Unzip: createClassWrapper(Unzip),
|
|
|
|
|
|
|
|
// Convenience methods.
|
|
|
|
// compress/decompress a string or buffer in one step.
|
|
|
|
deflate: createConvenienceMethod(Deflate, false),
|
|
|
|
deflateSync: createConvenienceMethod(Deflate, true),
|
|
|
|
gzip: createConvenienceMethod(Gzip, false),
|
|
|
|
gzipSync: createConvenienceMethod(Gzip, true),
|
|
|
|
deflateRaw: createConvenienceMethod(DeflateRaw, false),
|
|
|
|
deflateRawSync: createConvenienceMethod(DeflateRaw, true),
|
|
|
|
unzip: createConvenienceMethod(Unzip, false),
|
|
|
|
unzipSync: createConvenienceMethod(Unzip, true),
|
|
|
|
inflate: createConvenienceMethod(Inflate, false),
|
|
|
|
inflateSync: createConvenienceMethod(Inflate, true),
|
|
|
|
gunzip: createConvenienceMethod(Gunzip, false),
|
|
|
|
gunzipSync: createConvenienceMethod(Gunzip, true),
|
|
|
|
inflateRaw: createConvenienceMethod(InflateRaw, false),
|
|
|
|
inflateRawSync: createConvenienceMethod(InflateRaw, true)
|
|
|
|
};
|
2014-01-28 17:35:51 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
Object.defineProperties(module.exports, {
|
|
|
|
createDeflate: createProperty(module.exports.Deflate),
|
|
|
|
createInflate: createProperty(module.exports.Inflate),
|
|
|
|
createDeflateRaw: createProperty(module.exports.DeflateRaw),
|
|
|
|
createInflateRaw: createProperty(module.exports.InflateRaw),
|
|
|
|
createGzip: createProperty(module.exports.Gzip),
|
|
|
|
createGunzip: createProperty(module.exports.Gunzip),
|
|
|
|
createUnzip: createProperty(module.exports.Unzip),
|
|
|
|
constants: {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value: constants
|
|
|
|
},
|
|
|
|
codes: {
|
|
|
|
enumerable: true,
|
|
|
|
writable: false,
|
|
|
|
value: Object.freeze(codes)
|
2011-09-06 23:13:05 +00:00
|
|
|
}
|
2017-02-15 01:36:12 +00:00
|
|
|
});
|
2011-09-06 23:13:05 +00:00
|
|
|
|
2017-02-15 01:36:12 +00:00
|
|
|
// These should be considered deprecated
|
|
|
|
// expose all the zlib constants
|
|
|
|
const bkeys = Object.keys(constants);
|
|
|
|
for (var bk = 0; bk < bkeys.length; bk++) {
|
|
|
|
var bkey = bkeys[bk];
|
|
|
|
Object.defineProperty(module.exports, bkey, {
|
|
|
|
enumerable: true, value: constants[bkey], writable: false
|
|
|
|
});
|
|
|
|
}
|