Skip to content

Commit

Permalink
Ignore unexpected EOF errors (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and sindresorhus committed Sep 6, 2016
1 parent 99e7e77 commit c434b79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.js
Expand Up @@ -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');
});
Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit c434b79

Please sign in to comment.