Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: option for engine in convenience methods #13089

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/zlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -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} (If `true`, returns an object with `buffer` and `engine`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I’d drop the parentheses, and maybe add properties at the end to be explicit


See the description of `deflateInit2` and `inflateInit2` at
<http://zlib.net/manual.html#Advanced> for more information on these.
Expand Down
11 changes: 9 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -121,7 +128,7 @@ function zlibBuffer(engine, buffer, callback) {

buffers = [];
engine.close();
callback(err, buf);
callback(err, responseData(engine, buf));
}
}

Expand All @@ -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) {
Expand Down
44 changes: 40 additions & 4 deletions test/parallel/test-zlib-convenience-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const opts = {
chunkSize: 1024,
};

const optsInfo = {
info: true
};

for (const [type, expect] of [
['string', expectStr],
['Buffer', expectBuf],
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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.`);
}
}
}