From e5ca1180be919853399d7e912f5559ada285bc1f Mon Sep 17 00:00:00 2001 From: Alexander O'Mara Date: Wed, 17 May 2017 20:43:06 -0400 Subject: [PATCH 1/3] zlib: option for engine in convenience methods Added option to expose engine to convenience methods Refs: https://github.com/nodejs/node/issues/8874 --- lib/zlib.js | 11 ++++- .../parallel/test-zlib-convenience-methods.js | 44 +++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 00be56dffc44fb..ceb88480efe6b4 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -77,6 +77,13 @@ function isInvalidStrategy(strategy) { // constants.Z_DEFAULT_STRATEGY (0) } +function responseData(engine, buffer) { + if (engine._opts.info) { + return { buffer, engine }; + } + return buffer; +} + function zlibBuffer(engine, buffer, callback) { // Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a // Buffer without copying. @@ -121,7 +128,7 @@ function zlibBuffer(engine, buffer, callback) { buffers = []; engine.close(); - callback(err, buf); + callback(err, responseData(engine, buf)); } } @@ -134,7 +141,7 @@ function zlibBufferSync(engine, buffer) { var flushFlag = engine._finishFlushFlag; - return engine._processChunk(buffer, flushFlag); + return responseData(engine, engine._processChunk(buffer, flushFlag)); } function zlibOnError(message, errno) { diff --git a/test/parallel/test-zlib-convenience-methods.js b/test/parallel/test-zlib-convenience-methods.js index 5612575d8e7db4..5c1f7395062d9d 100644 --- a/test/parallel/test-zlib-convenience-methods.js +++ b/test/parallel/test-zlib-convenience-methods.js @@ -36,6 +36,10 @@ const opts = { chunkSize: 1024, }; +const optsInfo = { + info: true +}; + for (const [type, expect] of [ ['string', expectStr], ['Buffer', expectBuf], @@ -44,10 +48,10 @@ for (const [type, expect] of [ ) ]) { for (const method of [ - ['gzip', 'gunzip'], - ['gzip', 'unzip'], - ['deflate', 'inflate'], - ['deflateRaw', 'inflateRaw'], + ['gzip', 'gunzip', 'Gzip', 'Gunzip'], + ['gzip', 'unzip', 'Gzip', 'Unzip'], + ['deflate', 'inflate', 'Deflate', 'Inflate'], + ['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'], ]) { zlib[method[0]](expect, opts, common.mustCall((err, result) => { zlib[method[1]](result, opts, common.mustCall((err, result) => { @@ -65,6 +69,22 @@ for (const [type, expect] of [ })); })); + zlib[method[0]](expect, optsInfo, common.mustCall((err, result) => { + assert.ok(result.engine instanceof zlib[method[2]], + `Should get engine ${method[2]} after ${method[0]} ` + + `${type} with info option.`); + + const compressed = result.buffer; + zlib[method[1]](compressed, optsInfo, common.mustCall((err, result) => { + assert.strictEqual(result.buffer.toString(), expectStr, + `Should get original string after ${method[0]}/` + + `${method[1]} ${type} with info option.`); + assert.ok(result.engine instanceof zlib[method[3]], + `Should get engine ${method[3]} after ${method[0]} ` + + `${type} with info option.`); + })); + })); + { const compressed = zlib[`${method[0]}Sync`](expect, opts); const decompressed = zlib[`${method[1]}Sync`](compressed, opts); @@ -81,5 +101,21 @@ for (const [type, expect] of [ `Should get original string after ${method[0]}Sync/` + `${method[1]}Sync ${type} without options.`); } + + + { + const compressed = zlib[`${method[0]}Sync`](expect, optsInfo); + assert.ok(compressed.engine instanceof zlib[method[2]], + `Should get engine ${method[2]} after ${method[0]} ` + + `${type} with info option.`); + const decompressed = zlib[`${method[1]}Sync`](compressed.buffer, + optsInfo); + assert.strictEqual(decompressed.buffer.toString(), expectStr, + `Should get original string after ${method[0]}Sync/` + + `${method[1]}Sync ${type} without options.`); + assert.ok(decompressed.engine instanceof zlib[method[3]], + `Should get engine ${method[3]} after ${method[0]} ` + + `${type} with info option.`); + } } } From 6773df11cbb438217065c173d4fdeb5687c044df Mon Sep 17 00:00:00 2001 From: Alexander O'Mara Date: Sun, 21 May 2017 23:49:25 -0400 Subject: [PATCH 2/3] zlib: document convenience methods info option Refs: https://github.com/nodejs/node/issues/8874 --- doc/api/zlib.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/zlib.md b/doc/api/zlib.md index abfb6e2038b6d3..fc9d7df35efd5d 100644 --- a/doc/api/zlib.md +++ b/doc/api/zlib.md @@ -301,6 +301,7 @@ ignored by the decompression classes. * `strategy` {integer} (compression only) * `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by default) +* `info` {boolean} (It `true`, returns object with buffer and engine) See the description of `deflateInit2` and `inflateInit2` at for more information on these. From 3d923310246c5d913b58c90ce518419ae9564012 Mon Sep 17 00:00:00 2001 From: Alexander O'Mara Date: Mon, 22 May 2017 01:11:28 -0400 Subject: [PATCH 3/3] zlib: adjusted info option description Refs: https://github.com/nodejs/node/issues/8874 --- doc/api/zlib.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/zlib.md b/doc/api/zlib.md index fc9d7df35efd5d..6ce217ee048864 100644 --- a/doc/api/zlib.md +++ b/doc/api/zlib.md @@ -301,7 +301,7 @@ ignored by the decompression classes. * `strategy` {integer} (compression only) * `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by default) -* `info` {boolean} (It `true`, returns object with buffer and engine) +* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`) See the description of `deflateInit2` and `inflateInit2` at for more information on these.