util.inherits(Zlib, Transform);
-Zlib.prototype.reset = function reset() {
+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() {
++ assert(!self._closed, 'zlib binding closed');
+ self._handle.params(level, strategy);
+ if (!self._hadError) {
+ self._level = level;
+ self._strategy = strategy;
+ if (callback) callback();
+ }
+ });
+ } else {
+ process.nextTick(callback);
+ }
+};
+
+Zlib.prototype.reset = function() {
+ assert(!this._closed, 'zlib binding closed');
- return this._binding.reset();
+ return this._handle.reset();
};
// This is the _flush function called by the transform class,
var ending = ws.ending || ws.ended;
var last = ending && (!chunk || ws.length === chunk.length);
- if (chunk !== null && !Buffer.isBuffer(chunk))
+ if (!util.isNull(chunk) && !util.isBuffer(chunk))
return cb(new Error('invalid input'));
++ if (this._closed)
++ return cb(new Error('zlib binding closed'));
++
// 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
var availOutBefore = this._chunkSize - this._offset;
var inOff = 0;
- if (this._closed)
- return cb(new Error('zlib binding closed'));
- var req = this._binding.write(flushFlag,
- chunk, // in
- inOff, // in_off
- availInBefore, // in_len
- this._buffer, // out
- this._offset, //out_off
- availOutBefore); // out_len
+ var self = this;
+
+ var async = util.isFunction(cb);
+
+ if (!async) {
+ var buffers = [];
+ var nread = 0;
+
+ var error;
+ this.on('error', function(er) {
+ error = er;
+ });
+
++ assert(!this._closed, '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;
+ }
+
+ var buf = Buffer.concat(buffers, nread);
+ this.close();
+
+ return buf;
+ }
+
++ assert(!this._closed, '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;