diff --git a/lib/zlib.js b/lib/zlib.js index a10d9118d6194e..197595f3cceafd 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -209,7 +209,8 @@ function zlibBuffer(engine, buffer, callback) { function onError(err) { engine.removeListener('end', onEnd); engine.removeListener('readable', flow); - callback(err); + if (typeof callback === 'function') + callback(err); } function onEnd() { @@ -224,7 +225,8 @@ function zlibBuffer(engine, buffer, callback) { buffers = []; engine.close(); - callback(err, buf); + if (typeof callback === 'function') + callback(err, buf); } } diff --git a/test/parallel/test-zlib-function-only-callbacks.js b/test/parallel/test-zlib-function-only-callbacks.js new file mode 100644 index 00000000000000..586382e0119821 --- /dev/null +++ b/test/parallel/test-zlib-function-only-callbacks.js @@ -0,0 +1,9 @@ +'use strict'; +var common = require('../common'); +var assert = require('assert'); +var zlib = require('zlib'); + +// test callback in `onEnd` +zlib.deflate('abc', {}, 'not a function'); +// test callback in `onError` +zlib.deflate({}, {}, 'not a function');