Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore unexpected EOF errors #1

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we should rather use res.destroy() here. https://nodejs.org/api/http.html#http_message_destroy_error

Copy link
Contributor Author

@kevva kevva Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , that's also inconsistent sometimes, not all streams implement .destroy(). nodejs/readable-stream#124.

Copy link
Contributor Author

@kevva kevva Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it doesn't exist on res: TypeError: res.destroy is not a function. Why can't they all behave like you think they should...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But but, documented. Ugh. Never mind then. Lol.

});

res.on('close', function () {
unzip.emit('close');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, not sure you should use unzip.emit('close') here, end is probably better.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it better? I've yet to figure out the difference. Wouldn't res.end() be more correct then?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, seems there's no official way: http://stackoverflow.com/questions/19277094/how-to-close-a-readable-stream-before-end Node.js streams really are the worst...

Copy link
Contributor Author

@kevva kevva Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've encountered less inconsistencies when using end rather than close. I actually tested using res.emit('close') at first, but then it never exited.

Wouldn't res.end() be more correct then?

Yes. If it's a writable stream, res.end() wouldn't work since it's a readable.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in a major bump, just to be sure.

});
Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
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