diff --git a/lib/zlib.js b/lib/zlib.js index 3b9e522a06c7e5..e85cfa9b1f1b5c 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -275,7 +275,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) { this._defaultFlushFlag = flush; this._finishFlushFlag = finishFlush; this._defaultFullFlushFlag = fullFlush; - this.once('end', this.close); + this.once('end', _close.bind(null, this)); this._info = opts && opts.info; } ObjectSetPrototypeOf(ZlibBase.prototype, Transform.prototype); @@ -487,7 +487,7 @@ function processChunkSync(self, chunk, flushFlag) { function processChunk(self, chunk, flushFlag, cb) { const handle = self._handle; - assert(handle, 'zlib binding closed'); + if (!handle) return process.nextTick(cb); handle.buffer = chunk; handle.cb = cb; @@ -513,13 +513,9 @@ function processCallback() { const self = this[owner_symbol]; const state = self._writeState; - if (self._hadError) { - this.buffer = null; - return; - } - - if (self.destroyed) { + if (self._hadError || self.destroyed) { this.buffer = null; + this.cb(); return; } @@ -539,6 +535,7 @@ function processCallback() { } if (self.destroyed) { + this.cb(); return; } diff --git a/test/parallel/test-zlib-write-after-end.js b/test/parallel/test-zlib-write-after-end.js new file mode 100644 index 00000000000000..2b31ff30dc8591 --- /dev/null +++ b/test/parallel/test-zlib-write-after-end.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const zlib = require('zlib'); + +// Regression test for https://github.com/nodejs/node/issues/30976 +// Writes to a stream should finish even after the readable side has been ended. + +const data = zlib.deflateRawSync('Welcome'); + +const inflate = zlib.createInflateRaw(); + +inflate.resume(); +inflate.write(data, common.mustCall()); +inflate.write(Buffer.from([0x00]), common.mustCall()); +inflate.write(Buffer.from([0x00]), common.mustCall()); +inflate.flush(common.mustCall());