Skip to content

Commit bb77d6c

Browse files
AlexanderOMarajasnell
authored andcommitted
zlib: option for engine in convenience methods
Added option to expose engine to convenience methods Refs: #8874 PR-URL: #13089 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 9df8e2a commit bb77d6c

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

doc/api/zlib.md

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ ignored by the decompression classes.
301301
* `strategy` {integer} (compression only)
302302
* `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by
303303
default)
304+
* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`)
304305

305306
See the description of `deflateInit2` and `inflateInit2` at
306307
<http://zlib.net/manual.html#Advanced> for more information on these.

lib/zlib.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ function isInvalidStrategy(strategy) {
7777
// constants.Z_DEFAULT_STRATEGY (0)
7878
}
7979

80+
function responseData(engine, buffer) {
81+
if (engine._opts.info) {
82+
return { buffer, engine };
83+
}
84+
return buffer;
85+
}
86+
8087
function zlibBuffer(engine, buffer, callback) {
8188
// Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a
8289
// Buffer without copying.
@@ -121,7 +128,7 @@ function zlibBuffer(engine, buffer, callback) {
121128

122129
buffers = [];
123130
engine.close();
124-
callback(err, buf);
131+
callback(err, responseData(engine, buf));
125132
}
126133
}
127134

@@ -134,7 +141,7 @@ function zlibBufferSync(engine, buffer) {
134141

135142
var flushFlag = engine._finishFlushFlag;
136143

137-
return engine._processChunk(buffer, flushFlag);
144+
return responseData(engine, engine._processChunk(buffer, flushFlag));
138145
}
139146

140147
function zlibOnError(message, errno) {

test/parallel/test-zlib-convenience-methods.js

+40-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const opts = {
3636
chunkSize: 1024,
3737
};
3838

39+
const optsInfo = {
40+
info: true
41+
};
42+
3943
for (const [type, expect] of [
4044
['string', expectStr],
4145
['Buffer', expectBuf],
@@ -44,10 +48,10 @@ for (const [type, expect] of [
4448
)
4549
]) {
4650
for (const method of [
47-
['gzip', 'gunzip'],
48-
['gzip', 'unzip'],
49-
['deflate', 'inflate'],
50-
['deflateRaw', 'inflateRaw'],
51+
['gzip', 'gunzip', 'Gzip', 'Gunzip'],
52+
['gzip', 'unzip', 'Gzip', 'Unzip'],
53+
['deflate', 'inflate', 'Deflate', 'Inflate'],
54+
['deflateRaw', 'inflateRaw', 'DeflateRaw', 'InflateRaw'],
5155
]) {
5256
zlib[method[0]](expect, opts, common.mustCall((err, result) => {
5357
zlib[method[1]](result, opts, common.mustCall((err, result) => {
@@ -65,6 +69,22 @@ for (const [type, expect] of [
6569
}));
6670
}));
6771

72+
zlib[method[0]](expect, optsInfo, common.mustCall((err, result) => {
73+
assert.ok(result.engine instanceof zlib[method[2]],
74+
`Should get engine ${method[2]} after ${method[0]} ` +
75+
`${type} with info option.`);
76+
77+
const compressed = result.buffer;
78+
zlib[method[1]](compressed, optsInfo, common.mustCall((err, result) => {
79+
assert.strictEqual(result.buffer.toString(), expectStr,
80+
`Should get original string after ${method[0]}/` +
81+
`${method[1]} ${type} with info option.`);
82+
assert.ok(result.engine instanceof zlib[method[3]],
83+
`Should get engine ${method[3]} after ${method[0]} ` +
84+
`${type} with info option.`);
85+
}));
86+
}));
87+
6888
{
6989
const compressed = zlib[`${method[0]}Sync`](expect, opts);
7090
const decompressed = zlib[`${method[1]}Sync`](compressed, opts);
@@ -81,5 +101,21 @@ for (const [type, expect] of [
81101
`Should get original string after ${method[0]}Sync/` +
82102
`${method[1]}Sync ${type} without options.`);
83103
}
104+
105+
106+
{
107+
const compressed = zlib[`${method[0]}Sync`](expect, optsInfo);
108+
assert.ok(compressed.engine instanceof zlib[method[2]],
109+
`Should get engine ${method[2]} after ${method[0]} ` +
110+
`${type} with info option.`);
111+
const decompressed = zlib[`${method[1]}Sync`](compressed.buffer,
112+
optsInfo);
113+
assert.strictEqual(decompressed.buffer.toString(), expectStr,
114+
`Should get original string after ${method[0]}Sync/` +
115+
`${method[1]}Sync ${type} without options.`);
116+
assert.ok(decompressed.engine instanceof zlib[method[3]],
117+
`Should get engine ${method[3]} after ${method[0]} ` +
118+
`${type} with info option.`);
119+
}
84120
}
85121
}

0 commit comments

Comments
 (0)