Skip to content

Commit 31bbae7

Browse files
addaleaxBridgeAR
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 <shishugi@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent d131877 commit 31bbae7

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

‎lib/zlib.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
275275
this._defaultFlushFlag=flush;
276276
this._finishFlushFlag=finishFlush;
277277
this._defaultFullFlushFlag=fullFlush;
278-
this.once('end',this.close);
278+
this.once('end',_close.bind(null,this));
279279
this._info=opts&&opts.info;
280280
}
281281
ObjectSetPrototypeOf(ZlibBase.prototype,Transform.prototype);
@@ -487,7 +487,7 @@ function processChunkSync(self, chunk, flushFlag) {
487487

488488
functionprocessChunk(self,chunk,flushFlag,cb){
489489
consthandle=self._handle;
490-
assert(handle,'zlib binding closed');
490+
if(!handle)returnprocess.nextTick(cb);
491491

492492
handle.buffer=chunk;
493493
handle.cb=cb;
@@ -513,13 +513,9 @@ function processCallback() {
513513
constself=this[owner_symbol];
514514
conststate=self._writeState;
515515

516-
if(self._hadError){
517-
this.buffer=null;
518-
return;
519-
}
520-
521-
if(self.destroyed){
516+
if(self._hadError||self.destroyed){
522517
this.buffer=null;
518+
this.cb();
523519
return;
524520
}
525521

@@ -539,6 +535,7 @@ function processCallback() {
539535
}
540536

541537
if(self.destroyed){
538+
this.cb();
542539
return;
543540
}
544541

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)