From b13e0106f693cb19969a9a1def2b397b9461778f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 17 Jul 2017 19:25:21 +0200 Subject: [PATCH] zlib: check if the stream is destroyed before push If the stream is destroyed while the transform is still being applied, push() should not be called, and the internal state should be cleared. See: https://github.com/koajs/compress/issues/60 --- lib/zlib.js | 5 +++++ test/parallel/test-zlib-destroy-pipe.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/parallel/test-zlib-destroy-pipe.js diff --git a/lib/zlib.js b/lib/zlib.js index c57e83661bfa9b..80ffb962b38c6c 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -472,6 +472,11 @@ function processCallback() { return; } + if (self.destroyed) { + this.buffer = null; + return; + } + var availOutAfter = state[0]; var availInAfter = state[1]; diff --git a/test/parallel/test-zlib-destroy-pipe.js b/test/parallel/test-zlib-destroy-pipe.js new file mode 100644 index 00000000000000..38b8a5b4926e51 --- /dev/null +++ b/test/parallel/test-zlib-destroy-pipe.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const zlib = require('zlib'); +const { Writable } = require('stream'); + +// verify that the zlib transform does not error in case +// it is destroyed with data still in flight + +const ts = zlib.createGzip(); + +const ws = new Writable({ + write: common.mustCall((chunk, enc, cb) => { + setImmediate(cb); + ts.destroy(); + }) +}); + +const buf = Buffer.allocUnsafe(1024 * 1024 * 20); +ts.end(buf); +ts.pipe(ws);