mirror of
https://github.com/nodejs/node.git
synced 2025-05-10 17:57:53 +00:00

Previously, flushing on zlib streams was implemented through stream 'drain' handlers. This has a number of downsides; in particular, it is complex, and could lead to unpredictable behaviour, since it meant that in a sequence like ```js compressor.write('abc'); compressor.flush(); waitForMoreDataAsynchronously(() => { compressor.write('def'); }); ``` it was not fully deterministic whether the flush happens after the second chunk is written or the first one. This commit replaces this mechanism by one that piggy-backs along the stream’s write queue, using a “special” `Buffer` instance that signals that a flush is currently due. PR-URL: https://github.com/nodejs/node/pull/23186 Reviewed-By: James M Snell <jasnell@gmail.com>
49 lines
926 B
JavaScript
49 lines
926 B
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const zlib = require('zlib');
|
|
|
|
const bigData = Buffer.alloc(10240, 'x');
|
|
|
|
const opts = {
|
|
level: 0,
|
|
highWaterMark: 16
|
|
};
|
|
|
|
const deflater = zlib.createDeflate(opts);
|
|
|
|
// shim deflater.flush so we can count times executed
|
|
let flushCount = 0;
|
|
let drainCount = 0;
|
|
|
|
const flush = deflater.flush;
|
|
deflater.flush = function(kind, callback) {
|
|
flushCount++;
|
|
flush.call(this, kind, callback);
|
|
};
|
|
|
|
deflater.write(bigData);
|
|
|
|
const ws = deflater._writableState;
|
|
const beforeFlush = ws.needDrain;
|
|
let afterFlush = ws.needDrain;
|
|
|
|
deflater.flush(function(err) {
|
|
afterFlush = ws.needDrain;
|
|
});
|
|
|
|
deflater.on('drain', function() {
|
|
drainCount++;
|
|
});
|
|
|
|
process.once('exit', function() {
|
|
assert.strictEqual(
|
|
beforeFlush, true);
|
|
assert.strictEqual(
|
|
afterFlush, false);
|
|
assert.strictEqual(
|
|
drainCount, 1);
|
|
assert.strictEqual(
|
|
flushCount, 1);
|
|
});
|