Skip to content

Commit a6b55c7

Browse files
addaleaxtargos
authored andcommitted
zlib: move, rename, document internal params() cb
Give the callback a more specific name, explain what it does and why it is necessary, and move it to a location much closer to its use site. PR-URL: #23187 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 06b5ef3 commit a6b55c7

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/zlib.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,6 @@ function zlibOnError(message, errno) {
156156
self.emit('error', error);
157157
}
158158

159-
function flushCallback(level, strategy, callback) {
160-
if (!this._handle)
161-
assert(false, 'zlib binding closed');
162-
this._handle.params(level, strategy);
163-
if (!this._hadError) {
164-
this._level = level;
165-
this._strategy = strategy;
166-
if (callback) callback();
167-
}
168-
}
169-
170159
// 1. Returns false for undefined and NaN
171160
// 2. Returns true for finite numbers
172161
// 3. Throws ERR_INVALID_ARG_TYPE for non-numbers
@@ -352,13 +341,28 @@ Object.defineProperty(Zlib.prototype, 'bytesRead', {
352341
}
353342
});
354343

344+
// This callback is used by `.params()` to wait until a full flush happened
345+
// before adjusting the parameters. In particular, the call to the native
346+
// `params()` function should not happen while a write is currently in progress
347+
// on the threadpool.
348+
function paramsAfterFlushCallback(level, strategy, callback) {
349+
if (!this._handle)
350+
assert(false, 'zlib binding closed');
351+
this._handle.params(level, strategy);
352+
if (!this._hadError) {
353+
this._level = level;
354+
this._strategy = strategy;
355+
if (callback) callback();
356+
}
357+
}
358+
355359
Zlib.prototype.params = function params(level, strategy, callback) {
356360
checkRangesOrGetDefault(level, 'level', Z_MIN_LEVEL, Z_MAX_LEVEL);
357361
checkRangesOrGetDefault(strategy, 'strategy', Z_DEFAULT_STRATEGY, Z_FIXED);
358362

359363
if (this._level !== level || this._strategy !== strategy) {
360364
this.flush(Z_SYNC_FLUSH,
361-
flushCallback.bind(this, level, strategy, callback));
365+
paramsAfterFlushCallback.bind(this, level, strategy, callback));
362366
} else {
363367
process.nextTick(callback);
364368
}

0 commit comments

Comments
 (0)