11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst { createGzip, createGunzip, Z_PARTIAL_FLUSH } = require('zlib'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Verify that .flush() behaves like .write() in terms of ordering, e.g. in 71cb0ef41Sopenharmony_ci// a sequence like .write() + .flush() + .write() + .flush() each .flush() call 81cb0ef41Sopenharmony_ci// only affects the data written before it. 91cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/28478 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst compress = createGzip(); 121cb0ef41Sopenharmony_ciconst decompress = createGunzip(); 131cb0ef41Sopenharmony_cidecompress.setEncoding('utf8'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst events = []; 161cb0ef41Sopenharmony_ciconst compressedChunks = []; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifor (const chunk of ['abc', 'def', 'ghi']) { 191cb0ef41Sopenharmony_ci compress.write(chunk, common.mustCall(() => events.push({ written: chunk }))); 201cb0ef41Sopenharmony_ci compress.flush(Z_PARTIAL_FLUSH, common.mustCall(() => { 211cb0ef41Sopenharmony_ci events.push('flushed'); 221cb0ef41Sopenharmony_ci const chunk = compress.read(); 231cb0ef41Sopenharmony_ci if (chunk !== null) 241cb0ef41Sopenharmony_ci compressedChunks.push(chunk); 251cb0ef41Sopenharmony_ci })); 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cicompress.end(common.mustCall(() => { 291cb0ef41Sopenharmony_ci events.push('compress end'); 301cb0ef41Sopenharmony_ci writeToDecompress(); 311cb0ef41Sopenharmony_ci})); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_cifunction writeToDecompress() { 341cb0ef41Sopenharmony_ci // Write the compressed chunks to a decompressor, one by one, in order to 351cb0ef41Sopenharmony_ci // verify that the flushes actually worked. 361cb0ef41Sopenharmony_ci const chunk = compressedChunks.shift(); 371cb0ef41Sopenharmony_ci if (chunk === undefined) return decompress.end(); 381cb0ef41Sopenharmony_ci decompress.write(chunk, common.mustCall(() => { 391cb0ef41Sopenharmony_ci events.push({ read: decompress.read() }); 401cb0ef41Sopenharmony_ci writeToDecompress(); 411cb0ef41Sopenharmony_ci })); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciprocess.on('exit', () => { 451cb0ef41Sopenharmony_ci assert.deepStrictEqual(events, [ 461cb0ef41Sopenharmony_ci { written: 'abc' }, 471cb0ef41Sopenharmony_ci 'flushed', 481cb0ef41Sopenharmony_ci { written: 'def' }, 491cb0ef41Sopenharmony_ci 'flushed', 501cb0ef41Sopenharmony_ci { written: 'ghi' }, 511cb0ef41Sopenharmony_ci 'flushed', 521cb0ef41Sopenharmony_ci 'compress end', 531cb0ef41Sopenharmony_ci { read: 'abc' }, 541cb0ef41Sopenharmony_ci { read: 'def' }, 551cb0ef41Sopenharmony_ci { read: 'ghi' }, 561cb0ef41Sopenharmony_ci ]); 571cb0ef41Sopenharmony_ci}); 58