Node.js zlib.flush() Method
The flush() method is called on a compression stream so that it can compel zlib to return as much output as achievable at present. This output may be returned at the cost of corrupt compression quality but it can be useful when data is required to be accessible as early as possible.
Syntax:
zlib.flush()
Parameters: This method does not accept any parameters.
Return Value: It returns the data as much as possible at present.
Below examples illustrate the use of zlib.flush() method in Node.js:
Example 1:
const zlib = require( 'zlib' );
const input = zlib.createGzip();
const output = zlib.createGunzip();
input.pipe(output);
input.write( 'GeeksforGeeks' );
input.flush();
output.on( 'data' , (d) => {
console.log( 'Input: Data flush received '
+ d.length + ' bytes' );
});
console.log( "Program Complete!" );
|
Output:
Program Complete!
Input: Data flush received 13 bytes
Example 2:
const zlib = require( 'zlib' );
const input = zlib.createGzip();
const output = zlib.createGunzip();
input.pipe(output);
input.write( 'G' .repeat(19000));
input.flush(() => {});
output.on( 'data' , (d) => {
console.log( 'Input: Data flush with callback received '
+ d.length + ' bytes' );
});
console.log( "Program Complete!" );
|
Output: So, after the byte size exceeds 16384 bytes then you need to callback flush method else data won’t be flushed fully.
Program Complete!
Input: Data flush with callback received 16384 bytes
Input: Data flush with callback received 2616 bytes
Reference: https://nodejs.org/api/zlib.html#zlib_flushing
Last Updated :
28 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...