Skip to content

Commit 7cad756

Browse files
addaleaxBethGriggs
authored andcommitted
zlib: allow writes after readable 'end' to finish
Call the callback for writes that occur after the stream is closed. This also requires changes to the code to not call `.destroy()` on the stream in `.on('end')`, and to ignore chunks written afterwards. Previously, these writes would just queue up silently, as their `_write()` callback would never have been called. Fixes: #30976 PR-URL: #31082 Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b1b7f67 commit 7cad756

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

‎lib/zlib.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function ZlibBase(opts, mode, handle,{flush, finishFlush, fullFlush }){
264264
this._defaultFlushFlag=flush;
265265
this._finishFlushFlag=finishFlush;
266266
this._defaultFullFlushFlag=fullFlush;
267-
this.once('end',this.close);
267+
this.once('end',_close.bind(null,this));
268268
this._info=opts&&opts.info;
269269
}
270270
Object.setPrototypeOf(ZlibBase.prototype,Transform.prototype);
@@ -476,7 +476,7 @@ function processChunkSync(self, chunk, flushFlag){
476476

477477
functionprocessChunk(self,chunk,flushFlag,cb){
478478
consthandle=self._handle;
479-
assert(handle,'zlib binding closed');
479+
if(!handle)returnprocess.nextTick(cb);
480480

481481
handle.buffer=chunk;
482482
handle.cb=cb;
@@ -502,13 +502,9 @@ function processCallback(){
502502
constself=this[owner_symbol];
503503
conststate=self._writeState;
504504

505-
if(self._hadError){
506-
this.buffer=null;
507-
return;
508-
}
509-
510-
if(self.destroyed){
505+
if(self._hadError||self.destroyed){
511506
this.buffer=null;
507+
this.cb();
512508
return;
513509
}
514510

@@ -528,6 +524,7 @@ function processCallback(){
528524
}
529525

530526
if(self.destroyed){
527+
this.cb();
531528
return;
532529
}
533530

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constzlib=require('zlib');
4+
5+
// Regression test for https://github.com/nodejs/node/issues/30976
6+
// Writes to a stream should finish even after the readable side has been ended.
7+
8+
constdata=zlib.deflateRawSync('Welcome');
9+
10+
constinflate=zlib.createInflateRaw();
11+
12+
inflate.resume();
13+
inflate.write(data,common.mustCall());
14+
inflate.write(Buffer.from([0x00]),common.mustCall());
15+
inflate.write(Buffer.from([0x00]),common.mustCall());
16+
inflate.flush(common.mustCall());

0 commit comments

Comments
(0)