@@ -316,15 +316,28 @@ ZlibBase.prototype._flush = function(callback) {
316316// Z_SYNC_FLUSH < Z_FULL_FLUSH < Z_FINISH
317317const flushiness = [ ] ;
318318let i = 0 ;
319- for ( const flushFlag of [ Z_NO_FLUSH , Z_BLOCK , Z_PARTIAL_FLUSH ,
320- Z_SYNC_FLUSH , Z_FULL_FLUSH , Z_FINISH ] ) {
319+ const kFlushFlagList = [ Z_NO_FLUSH , Z_BLOCK , Z_PARTIAL_FLUSH ,
320+ Z_SYNC_FLUSH , Z_FULL_FLUSH , Z_FINISH ] ;
321+ for ( const flushFlag of kFlushFlagList ) {
321322 flushiness [ flushFlag ] = i ++ ;
322323}
323324
324325function maxFlush ( a , b ) {
325326 return flushiness [ a ] > flushiness [ b ] ? a : b ;
326327}
327328
329+ // Set up a list of 'special' buffers that can be written using .write()
330+ // from the .flush() code as a way of introducing flushing operations into the
331+ // write sequence.
332+ const kFlushBuffers = [ ] ;
333+ {
334+ const dummyArrayBuffer = new ArrayBuffer ( ) ;
335+ for ( const flushFlag of kFlushFlagList ) {
336+ kFlushBuffers [ flushFlag ] = Buffer . from ( dummyArrayBuffer ) ;
337+ kFlushBuffers [ flushFlag ] [ kFlushFlag ] = flushFlag ;
338+ }
339+ }
340+
328341ZlibBase . prototype . flush = function ( kind , callback ) {
329342 const ws = this . _writableState ;
330343
@@ -340,9 +353,7 @@ ZlibBase.prototype.flush = function(kind, callback) {
340353 if ( callback )
341354 this . once ( 'end' , callback ) ;
342355 } else {
343- const flushBuffer = Buffer . alloc ( 0 ) ;
344- flushBuffer [ kFlushFlag ] = kind ;
345- this . write ( flushBuffer , '' , callback ) ;
356+ this . write ( kFlushBuffers [ kind ] , '' , callback ) ;
346357 }
347358} ;
348359
0 commit comments