@@ -311,10 +311,9 @@ function Zlib(opts, mode) { |
311 | 311 | this._level = level; |
312 | 312 | this._strategy = strategy; |
313 | 313 | this._chunkSize = chunkSize; |
314 | | - this._flushFlag = flush; |
315 | | - this._scheduledFlushFlag = Z_NO_FLUSH; |
316 | | - this._origFlushFlag = flush; |
| 314 | + this._defaultFlushFlag = flush; |
317 | 315 | this._finishFlushFlag = finishFlush; |
| 316 | + this._nextFlush = -1; |
318 | 317 | this._info = opts && opts.info; |
319 | 318 | this.once('end', this.close); |
320 | 319 | } |
@@ -398,6 +397,7 @@ function maxFlush(a, b) { |
398 | 397 | return flushiness[a] > flushiness[b] ? a : b; |
399 | 398 | } |
400 | 399 |
|
| 400 | +const flushBuffer = Buffer.alloc(0); |
401 | 401 | Zlib.prototype.flush = function flush(kind, callback) { |
402 | 402 | var ws = this._writableState; |
403 | 403 |
|
@@ -412,21 +412,13 @@ Zlib.prototype.flush = function flush(kind, callback) { |
412 | 412 | } else if (ws.ending) { |
413 | 413 | if (callback) |
414 | 414 | this.once('end', callback); |
415 | | - } else if (ws.needDrain) { |
416 | | - const alreadyHadFlushScheduled = this._scheduledFlushFlag !== Z_NO_FLUSH; |
417 | | - this._scheduledFlushFlag = maxFlush(kind, this._scheduledFlushFlag); |
418 | | - |
419 | | - // If a callback was passed, always register a new `drain` + flush handler, |
420 | | - // mostly because that's simpler and flush callbacks piling up is a rare |
421 | | - // thing anyway. |
422 | | - if (!alreadyHadFlushScheduled || callback) { |
423 | | - const drainHandler = () => this.flush(this._scheduledFlushFlag, callback); |
424 | | - this.once('drain', drainHandler); |
425 | | - } |
| 415 | + } else if (this._nextFlush !== -1) { |
| 416 | + // This means that there is a flush currently in the write queue. |
| 417 | + // We currently coalesce this flush into the pending one. |
| 418 | + this._nextFlush = maxFlush(this._nextFlush, kind); |
426 | 419 | } else { |
427 | | - this._flushFlag = kind; |
428 | | - this.write(Buffer.alloc(0), '', callback); |
429 | | - this._scheduledFlushFlag = Z_NO_FLUSH; |
| 420 | + this._nextFlush = kind; |
| 421 | + this.write(flushBuffer, '', callback); |
430 | 422 | } |
431 | 423 | }; |
432 | 424 |
|
@@ -436,20 +428,18 @@ Zlib.prototype.close = function close(callback) { |
436 | 428 | }; |
437 | 429 |
|
438 | 430 | Zlib.prototype._transform = function _transform(chunk, encoding, cb) { |
439 | | - // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag |
440 | | - // (or whatever flag was provided using opts.finishFlush). |
441 | | - // If it's explicitly flushing at some other time, then we use |
442 | | - // Z_FULL_FLUSH. Otherwise, use the original opts.flush flag. |
443 | | - var flushFlag; |
| 431 | + var flushFlag = this._defaultFlushFlag; |
| 432 | + // We use a 'fake' zero-length chunk to carry information about flushes from |
| 433 | + // the public API to the actual stream implementation. |
| 434 | + if (chunk === flushBuffer) { |
| 435 | + flushFlag = this._nextFlush; |
| 436 | + this._nextFlush = -1; |
| 437 | + } |
| 438 | + |
| 439 | + // For the last chunk, also apply `_finishFlushFlag`. |
444 | 440 | var ws = this._writableState; |
445 | 441 | if ((ws.ending || ws.ended) && ws.length === chunk.byteLength) { |
446 | | - flushFlag = this._finishFlushFlag; |
447 | | - } else { |
448 | | - flushFlag = this._flushFlag; |
449 | | - // once we've flushed the last of the queue, stop flushing and |
450 | | - // go back to the normal behavior. |
451 | | - if (chunk.byteLength >= ws.length) |
452 | | - this._flushFlag = this._origFlushFlag; |
| 442 | + flushFlag = maxFlush(flushFlag, this._finishFlushFlag); |
453 | 443 | } |
454 | 444 | processChunk(this, chunk, flushFlag, cb); |
455 | 445 | }; |
|