diff --git a/index.js b/index.js index a3c9913..bacb674 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,15 @@ module.exports = function (res) { unzip.statusMessage = res.statusMessage; unzip.socket = res.socket; + unzip.once('error', function (err) { + if (err.code === 'Z_BUF_ERROR') { + res.emit('end'); + return; + } + + res.emit('error', err); + }); + res.on('close', function () { unzip.emit('close'); }); diff --git a/test/test.js b/test/test.js index 515d267..0b7769a 100644 --- a/test/test.js +++ b/test/test.js @@ -21,6 +21,19 @@ s.on('/', function (req, res) { }); }); +s.on('/missing-data', function (req, res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Content-Encoding', 'gzip'); + zlib.gzip(fixture, function (err, data) { + if (err) { + throw err; + } + + res.end(data.slice(0, -1)); + }); +}); + test('setup', function (t) { s.listen(s.port, function () { t.end(); @@ -43,6 +56,22 @@ test('unzip content', function (t) { }).on('err', t.error.bind(t)); }); +test('ignore missing data', function (t) { + http.get(s.url + '/missing-data', function (res) { + res = fn(res); + + t.ok(typeof res.httpVersion === 'string'); + t.ok(res.headers); + + res.setEncoding('utf8'); + + res.pipe(concatStream(function (data) { + t.equal(data, fixture); + t.end(); + })); + }).on('err', t.error.bind(t)); +}); + test('cleanup', function (t) { s.close(); t.end();