@@ -61,6 +61,8 @@ class PerMessageDeflate {
6161
6262    this . params  =  null ; 
6363
64+     this . _stopWriting  =  false ; 
65+ 
6466    if  ( ! zlibLimiter )  { 
6567      const  concurrency  = 
6668        this . _options . concurrencyLimit  !==  undefined 
@@ -127,12 +129,18 @@ class PerMessageDeflate {
127129   * @public  
128130   */ 
129131  cleanup ( )  { 
132+     this . _stopWriting  =  true ; 
133+ 
130134    if  ( this . _inflate )  { 
131135      this . _inflate . close ( ) ; 
132136      this . _inflate  =  null ; 
133137    } 
134138
135139    if  ( this . _deflate )  { 
140+       if  ( this . _deflate [ kCallback ] )  { 
141+         this . _deflate [ kCallback ] ( ) ; 
142+       } 
143+ 
136144      this . _deflate . close ( ) ; 
137145      this . _deflate  =  null ; 
138146    } 
@@ -312,7 +320,9 @@ class PerMessageDeflate {
312320    zlibLimiter . push ( ( done )  =>  { 
313321      this . _compress ( data ,  fin ,  ( err ,  result )  =>  { 
314322        done ( ) ; 
315-         callback ( err ,  result ) ; 
323+         if  ( ! this . _stopWriting )  { 
324+           callback ( err ,  result ) ; 
325+         } 
316326      } ) ; 
317327    } ) ; 
318328  } 
@@ -419,6 +429,8 @@ class PerMessageDeflate {
419429      this . _deflate . on ( 'data' ,  deflateOnData ) ; 
420430    } 
421431
432+     this . _deflate [ kCallback ]  =  callback ; 
433+ 
422434    this . _deflate . write ( data ) ; 
423435    this . _deflate . flush ( zlib . Z_SYNC_FLUSH ,  ( )  =>  { 
424436      if  ( ! this . _deflate )  { 
@@ -438,6 +450,11 @@ class PerMessageDeflate {
438450
439451      if  ( fin )  data  =  data . slice ( 0 ,  data . length  -  4 ) ; 
440452
453+       // Set callback to null so we won't call it twice if socket is closed 
454+       // (see PerMessageDeflate#cleanup) 
455+       // ideally, we would set it to `null` before calling, but we might set it to null 
456+       this . _deflate [ kCallback ]  =  null ; 
457+ 
441458      if  ( fin  &&  this . params [ `${ endpoint }  ] )  { 
442459        this . _deflate . close ( ) ; 
443460        this . _deflate  =  null ; 
0 commit comments