|
|
@ -112,7 +112,7 @@ exports.deflate = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new Deflate(opts), buffer, callback); |
|
|
|
return zlibBuffer(new Deflate(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.gzip = function(buffer, opts, callback) { |
|
|
@ -120,7 +120,7 @@ exports.gzip = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new Gzip(opts), buffer, callback); |
|
|
|
return zlibBuffer(new Gzip(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.deflateRaw = function(buffer, opts, callback) { |
|
|
@ -128,7 +128,7 @@ exports.deflateRaw = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new DeflateRaw(opts), buffer, callback); |
|
|
|
return zlibBuffer(new DeflateRaw(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.unzip = function(buffer, opts, callback) { |
|
|
@ -136,7 +136,7 @@ exports.unzip = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new Unzip(opts), buffer, callback); |
|
|
|
return zlibBuffer(new Unzip(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.inflate = function(buffer, opts, callback) { |
|
|
@ -144,7 +144,7 @@ exports.inflate = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new Inflate(opts), buffer, callback); |
|
|
|
return zlibBuffer(new Inflate(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.gunzip = function(buffer, opts, callback) { |
|
|
@ -152,7 +152,7 @@ exports.gunzip = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new Gunzip(opts), buffer, callback); |
|
|
|
return zlibBuffer(new Gunzip(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
exports.inflateRaw = function(buffer, opts, callback) { |
|
|
@ -160,10 +160,14 @@ exports.inflateRaw = function(buffer, opts, callback) { |
|
|
|
callback = opts; |
|
|
|
opts = {}; |
|
|
|
} |
|
|
|
zlibBuffer(new InflateRaw(opts), buffer, callback); |
|
|
|
return zlibBuffer(new InflateRaw(opts), buffer, callback); |
|
|
|
}; |
|
|
|
|
|
|
|
function zlibBuffer(engine, buffer, callback) { |
|
|
|
if (!util.isFunction(callback)) { |
|
|
|
return zlibBufferSync(engine, buffer, callback); |
|
|
|
} |
|
|
|
|
|
|
|
var buffers = []; |
|
|
|
var nread = 0; |
|
|
|
|
|
|
@ -196,6 +200,16 @@ function zlibBuffer(engine, buffer, callback) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function zlibBufferSync(engine, buffer, callback) { |
|
|
|
if (util.isString(buffer)) |
|
|
|
buffer = new Buffer(buffer); |
|
|
|
if (!util.isBuffer(buffer)) |
|
|
|
throw new TypeError('Not a string or buffer'); |
|
|
|
|
|
|
|
var flushFlag = binding.Z_FINISH; |
|
|
|
|
|
|
|
return engine._processChunk(buffer, flushFlag); |
|
|
|
} |
|
|
|
|
|
|
|
// generic zlib
|
|
|
|
// minimal 2-byte header
|
|
|
@ -453,10 +467,48 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var self = this; |
|
|
|
this._processChunk(chunk, flushFlag, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
Zlib.prototype._processChunk = function(chunk, flushFlag, cb) { |
|
|
|
var availInBefore = chunk && chunk.length; |
|
|
|
var availOutBefore = this._chunkSize - this._offset; |
|
|
|
var inOff = 0; |
|
|
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
var async = util.isFunction(cb); |
|
|
|
|
|
|
|
if (!async) { |
|
|
|
var buffers = []; |
|
|
|
var nread = 0; |
|
|
|
|
|
|
|
var error; |
|
|
|
this.on('error', function(er) { |
|
|
|
error = er; |
|
|
|
}); |
|
|
|
|
|
|
|
do { |
|
|
|
var res = this._binding.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; |
|
|
|
} |
|
|
|
|
|
|
|
var req = this._binding.write(flushFlag, |
|
|
|
chunk, // in
|
|
|
|
inOff, // in_off
|
|
|
@ -468,7 +520,6 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { |
|
|
|
req.buffer = chunk; |
|
|
|
req.callback = callback; |
|
|
|
|
|
|
|
var self = this; |
|
|
|
function callback(availInAfter, availOutAfter) { |
|
|
|
if (self._hadError) |
|
|
|
return; |
|
|
@ -480,7 +531,12 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { |
|
|
|
var out = self._buffer.slice(self._offset, self._offset + have); |
|
|
|
self._offset += have; |
|
|
|
// serve some output to the consumer.
|
|
|
|
self.push(out); |
|
|
|
if (async) { |
|
|
|
self.push(out); |
|
|
|
} else { |
|
|
|
buffers.push(out); |
|
|
|
nread += out.length; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// exhausted the output buffer, or used all the input create a new one.
|
|
|
@ -498,6 +554,9 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { |
|
|
|
inOff += (availInBefore - availInAfter); |
|
|
|
availInBefore = availInAfter; |
|
|
|
|
|
|
|
if (!async) |
|
|
|
return true; |
|
|
|
|
|
|
|
var newReq = self._binding.write(flushFlag, |
|
|
|
chunk, |
|
|
|
inOff, |
|
|
@ -510,6 +569,9 @@ Zlib.prototype._transform = function(chunk, encoding, cb) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (!async) |
|
|
|
return false; |
|
|
|
|
|
|
|
// finished with the chunk.
|
|
|
|
cb(); |
|
|
|
} |
|
|
|