diff --git a/lib/zlib.js b/lib/zlib.js index a2d2c158e5d..75c3c6ed8e5 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -88,6 +88,8 @@ const { } = require('internal/zip'); const kFlushFlag = Symbol('kFlushFlag'); +const kParams = Symbol('kParams'); +const kRequestedParams = Symbol('kRequestedParams'); const kError = Symbol('kError'); const kBufferResult = Symbol('kBufferResult'); @@ -543,6 +545,24 @@ class ZlibBase extends Transform { if (this.writableEnded && this.writableLength === chunk.byteLength) { flushFlag = maxFlush(flushFlag, this._finishFlushFlag); } + + // Apply parameters before the next queued write can start. + const params = chunk[kParams]; + if (params !== undefined) { + processChunk(this, chunk, flushFlag, (err) => { + if (err || this.destroyed || !this._handle) { + cb(err); + return; + } + + this._handle.params(params.level, params.strategy); + this._level = params.level; + this._strategy = params.strategy; + cb(); + }); + return; + } + processChunk(this, chunk, flushFlag, cb); } @@ -785,22 +805,6 @@ const zlibDefaultOpts = { fullFlush: Z_FULL_FLUSH, }; -// This callback is used by `.params()` to wait until a full flush happened -// before adjusting the parameters. In particular, the call to the native -// `params()` function should not happen while a write is currently in progress -// on the threadpool. -function paramsAfterFlushCallback(level, strategy, callback, err) { - if (err || this.destroyed || !this._handle) { - if (callback) callback(err); - return; - } - - this._handle.params(level, strategy); - this._level = level; - this._strategy = strategy; - if (callback) callback(); -} - // Base class for all streams actually backed by zlib and using zlib-specific // parameters. class Zlib extends ZlibBase { @@ -880,11 +884,17 @@ class Zlib extends ZlibBase { checkRangesOrGetDefault(level, 'level', Z_MIN_LEVEL, Z_MAX_LEVEL); checkRangesOrGetDefault(strategy, 'strategy', Z_DEFAULT_STRATEGY, Z_FIXED); - if (this._level !== level || this._strategy !== strategy) { - this.flush( - Z_SYNC_FLUSH, - paramsAfterFlushCallback.bind(this, level, strategy, callback), - ); + // Compare against the last requested values, not the applied ones, since + // earlier changes may still be queued behind pending writes. + const requested = this[kRequestedParams] ?? + { level: this._level, strategy: this._strategy }; + + if (requested.level !== level || requested.strategy !== strategy) { + this[kRequestedParams] = { level, strategy }; + const params = Buffer.alloc(0); + params[kFlushFlag] = Z_SYNC_FLUSH; + params[kParams] = { level, strategy }; + this.write(params, '', callback); } else { process.nextTick(callback); } diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 72947b8b783..dcfaf92f8d9 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -736,6 +736,7 @@ class CompressionStream : public AsyncWrap, protected: CompressionContext* context() { return &ctx_; } + bool write_in_progress() const { return write_in_progress_; } void InitStream(uint32_t* write_result, Local write_js_callback) { write_result_ = write_result; @@ -878,6 +879,12 @@ class ZlibStream final : public CompressionStream { int strategy; if (!args[1]->Int32Value(context).To(&strategy)) return; + if (wrap->write_in_progress()) { + wrap->env()->ThrowError( + "Cannot change zlib params while a write is in progress"); + return; + } + AllocScope alloc_scope(wrap); const CompressionError err = wrap->context()->SetParams(level, strategy); if (err.IsError()) diff --git a/test/parallel/test-zlib-params-during-write.js b/test/parallel/test-zlib-params-during-write.js new file mode 100644 index 00000000000..98e72f54781 --- /dev/null +++ b/test/parallel/test-zlib-params-during-write.js @@ -0,0 +1,117 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { constants, createDeflateRaw, inflateRawSync } = require('zlib'); + +const input = Buffer.allocUnsafe(56489); +for (let i = 0; i < input.length; i++) { + input[i] = (Math.imul(i, 2654435761) >>> 8) & 0xff; +} + +{ + const output = []; + const stream = createDeflateRaw({ level: 2, windowBits: 13 }); + + stream.on('data', (chunk) => output.push(chunk)); + stream.on('end', common.mustCall(() => { + assert.deepStrictEqual(inflateRawSync(Buffer.concat(output)), input); + })); + stream.params(1, constants.Z_FILTERED, common.mustCall()); + stream.end(input); +} + +{ + const stream = createDeflateRaw(); + + stream.on('data', () => {}); + stream.on('error', common.mustNotCall()); + stream.write(input, common.mustCall()); + assert.throws(() => stream._handle.params(1, constants.Z_FILTERED), { + message: 'Cannot change zlib params while a write is in progress', + }); + stream.end(); +} + +{ + // A parameter change is applied, and its callback called, even when no + // other write follows it. + const input = Buffer.from('a'.repeat(4096)); + const output = []; + const stream = createDeflateRaw({ level: 6 }); + + stream.on('data', (chunk) => output.push(chunk)); + stream.write(input); + stream.params(1, constants.Z_FILTERED, common.mustSucceed(() => { + assert.strictEqual(stream._level, 1); + assert.strictEqual(stream._strategy, constants.Z_FILTERED); + stream.end(input); + })); + stream.on('end', common.mustCall(() => { + assert.deepStrictEqual(inflateRawSync(Buffer.concat(output)), + Buffer.concat([input, input])); + })); +} + +{ + // A flush, a parameter change and a following write queued together are + // processed in order. + const first = Buffer.from('first '.repeat(2000)); + const second = Buffer.from('second '.repeat(2000)); + const output = []; + const stream = createDeflateRaw({ level: 9 }); + const order = []; + + stream.on('data', (chunk) => output.push(chunk)); + stream.write(first); + stream.flush(common.mustCall(() => order.push('flush'))); + stream.params(1, constants.Z_DEFAULT_STRATEGY, common.mustCall(() => { + order.push('params'); + })); + stream.end(second); + stream.on('end', common.mustCall(() => { + assert.deepStrictEqual(order, ['flush', 'params']); + assert.deepStrictEqual(inflateRawSync(Buffer.concat(output)), + Buffer.concat([first, second])); + })); +} + +{ + // Destroying a stream with a pending parameter change must not apply it + // to a closed handle. + const stream = createDeflateRaw(); + + stream.on('data', () => {}); + stream.write(Buffer.allocUnsafe(1 << 20), common.mustCall()); + stream.params(1, constants.Z_FILTERED, common.mustCall((err) => { + assert.ok(err); + })); + stream.destroy(); + stream.on('close', common.mustCall()); +} + +{ + // Consecutive parameter changes are all applied in order, even when the + // last one goes back to the values the stream started with. + const input = Buffer.from('b'.repeat(2048)); + const output = []; + const stream = createDeflateRaw({ level: 6, strategy: constants.Z_DEFAULT_STRATEGY }); + const applied = []; + + stream.on('data', (chunk) => output.push(chunk)); + stream.write(input); + stream.params(1, constants.Z_FILTERED, common.mustSucceed(() => { + applied.push([stream._level, stream._strategy]); + })); + stream.params(6, constants.Z_DEFAULT_STRATEGY, common.mustSucceed(() => { + applied.push([stream._level, stream._strategy]); + })); + stream.end(input); + stream.on('end', common.mustCall(() => { + assert.deepStrictEqual(applied, [ + [1, constants.Z_FILTERED], + [6, constants.Z_DEFAULT_STRATEGY], + ]); + assert.deepStrictEqual(inflateRawSync(Buffer.concat(output)), + Buffer.concat([input, input])); + })); +}