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 <[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 d131877 commit 31bbae7

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/zlib.js

+5-8
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
function processChunk(self, chunk, flushFlag, cb) {
489489
const handle = self._handle;
490-
assert(handle, 'zlib binding closed');
490+
if (!handle) return process.nextTick(cb);
491491

492492
handle.buffer = chunk;
493493
handle.cb = cb;
@@ -513,13 +513,9 @@ function processCallback() {
513513
const self = this[owner_symbol];
514514
const state = 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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const zlib = 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+
const data = zlib.deflateRawSync('Welcome');
9+
10+
const inflate = 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)