Browse Source

zlib: separate sync/async methods

v0.11.12-release
Fedor Indutny 11 years ago
parent
commit
2315703035
  1. 30
      doc/api/zlib.markdown
  2. 34
      lib/zlib.js
  3. 8
      test/simple/test-zlib-convenience-methods.js

30
doc/api/zlib.markdown

@ -195,36 +195,44 @@ the header.
<!--type=misc--> <!--type=misc-->
All of these take a string or buffer as the first argument, an optional second All of these take a string or buffer as the first argument, an optional second
argument to supply options to the zlib classes and an optional callback. If a argument to supply options to the zlib classes and will call the supplied
callback is supplied, they will call it asynchronously with callback with `callback(error, result)`.
`callback(error, result)`, otherwise they will return the result or throw the
error synchronously.
## zlib.deflate(buf, [options], [callback]) Every method has a `*Sync` counterpart, which accept the same arguments, but
without a callback.
## zlib.deflate(buf, [options], callback)
## zlib.deflateSync(buf, [options])
Compress a string with Deflate. Compress a string with Deflate.
## zlib.deflateRaw(buf, [options], [callback]) ## zlib.deflateRaw(buf, [options], callback)
## zlib.deflateRawSync(buf, [options])
Compress a string with DeflateRaw. Compress a string with DeflateRaw.
## zlib.gzip(buf, [options], [callback]) ## zlib.gzip(buf, [options], callback)
## zlib.gzipSync(buf, [options])
Compress a string with Gzip. Compress a string with Gzip.
## zlib.gunzip(buf, [options], [callback]) ## zlib.gunzip(buf, [options], callback)
## zlib.gunzipSync(buf, [options])
Decompress a raw Buffer with Gunzip. Decompress a raw Buffer with Gunzip.
## zlib.inflate(buf, [options], [callback]) ## zlib.inflate(buf, [options], callback)
## zlib.inflateSync(buf, [options])
Decompress a raw Buffer with Inflate. Decompress a raw Buffer with Inflate.
## zlib.inflateRaw(buf, [options], [callback]) ## zlib.inflateRaw(buf, [options], callback)
## zlib.inflateRawSync(buf, [options])
Decompress a raw Buffer with InflateRaw. Decompress a raw Buffer with InflateRaw.
## zlib.unzip(buf, [options], [callback]) ## zlib.unzip(buf, [options], callback)
## zlib.unzipSync(buf, [options])
Decompress a raw Buffer with Unzip. Decompress a raw Buffer with Unzip.

34
lib/zlib.js

@ -115,6 +115,10 @@ exports.deflate = function(buffer, opts, callback) {
return zlibBuffer(new Deflate(opts), buffer, callback); return zlibBuffer(new Deflate(opts), buffer, callback);
}; };
exports.deflateSync = function(buffer, opts) {
return zlibBufferSync(new Deflate(opts), buffer);
};
exports.gzip = function(buffer, opts, callback) { exports.gzip = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -123,6 +127,10 @@ exports.gzip = function(buffer, opts, callback) {
return zlibBuffer(new Gzip(opts), buffer, callback); return zlibBuffer(new Gzip(opts), buffer, callback);
}; };
exports.gzipSync = function(buffer, opts) {
return zlibBufferSync(new Gzip(opts), buffer);
};
exports.deflateRaw = function(buffer, opts, callback) { exports.deflateRaw = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -131,6 +139,10 @@ exports.deflateRaw = function(buffer, opts, callback) {
return zlibBuffer(new DeflateRaw(opts), buffer, callback); return zlibBuffer(new DeflateRaw(opts), buffer, callback);
}; };
exports.deflateRawSync = function(buffer, opts) {
return zlibBufferSync(new DeflateRaw(opts), buffer);
};
exports.unzip = function(buffer, opts, callback) { exports.unzip = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -139,6 +151,10 @@ exports.unzip = function(buffer, opts, callback) {
return zlibBuffer(new Unzip(opts), buffer, callback); return zlibBuffer(new Unzip(opts), buffer, callback);
}; };
exports.unzipSync = function(buffer, opts) {
return zlibBufferSync(new Unzip(opts), buffer);
};
exports.inflate = function(buffer, opts, callback) { exports.inflate = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -147,6 +163,10 @@ exports.inflate = function(buffer, opts, callback) {
return zlibBuffer(new Inflate(opts), buffer, callback); return zlibBuffer(new Inflate(opts), buffer, callback);
}; };
exports.inflateSync = function(buffer, opts) {
return zlibBufferSync(new Inflate(opts), buffer);
};
exports.gunzip = function(buffer, opts, callback) { exports.gunzip = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -155,6 +175,10 @@ exports.gunzip = function(buffer, opts, callback) {
return zlibBuffer(new Gunzip(opts), buffer, callback); return zlibBuffer(new Gunzip(opts), buffer, callback);
}; };
exports.gunzipSync = function(buffer, opts) {
return zlibBufferSync(new Gunzip(opts), buffer);
};
exports.inflateRaw = function(buffer, opts, callback) { exports.inflateRaw = function(buffer, opts, callback) {
if (util.isFunction(opts)) { if (util.isFunction(opts)) {
callback = opts; callback = opts;
@ -163,11 +187,11 @@ exports.inflateRaw = function(buffer, opts, callback) {
return zlibBuffer(new InflateRaw(opts), buffer, callback); return zlibBuffer(new InflateRaw(opts), buffer, callback);
}; };
function zlibBuffer(engine, buffer, callback) { exports.inflateRawSync = function(buffer, opts) {
if (!util.isFunction(callback)) { return zlibBufferSync(new InflateRaw(opts), buffer);
return zlibBufferSync(engine, buffer, callback); };
}
function zlibBuffer(engine, buffer, callback) {
var buffers = []; var buffers = [];
var nread = 0; var nread = 0;
@ -200,7 +224,7 @@ function zlibBuffer(engine, buffer, callback) {
} }
} }
function zlibBufferSync(engine, buffer, callback) { function zlibBufferSync(engine, buffer) {
if (util.isString(buffer)) if (util.isString(buffer))
buffer = new Buffer(buffer); buffer = new Buffer(buffer);
if (!util.isBuffer(buffer)) if (!util.isBuffer(buffer))

8
test/simple/test-zlib-convenience-methods.js

@ -58,15 +58,15 @@ var opts = {
}); });
}); });
var result = zlib[method[0]](expect, opts); var result = zlib[method[0] + 'Sync'](expect, opts);
result = zlib[method[1]](result, opts); result = zlib[method[1] + 'Sync'](result, opts);
assert.equal(result, expect, assert.equal(result, expect,
'Should get original string after ' + 'Should get original string after ' +
method[0] + '/' + method[1] + ' with options.'); method[0] + '/' + method[1] + ' with options.');
hadRun++; hadRun++;
result = zlib[method[0]](expect); result = zlib[method[0] + 'Sync'](expect);
result = zlib[method[1]](result); result = zlib[method[1] + 'Sync'](result);
assert.equal(result, expect, assert.equal(result, expect,
'Should get original string after ' + 'Should get original string after ' +
method[0] + '/' + method[1] + ' without options.'); method[0] + '/' + method[1] + ' without options.');

Loading…
Cancel
Save