Skip to content

Commit

Permalink
msg.finished is only finished if socket has been detached
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 14, 2019
1 parent 08345db commit b9c2fa4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,21 @@ function onFinished (msg, listener) {

function isFinished (msg) {
var socket = msg.socket
var stream = msg.stream

if (stream && typeof stream.closed === 'boolean') {
// Http2ServerRequest
// Http2ServerResponse
return stream.closed
}

if (typeof msg.finished === 'boolean') {
// OutgoingMessage
return Boolean(msg.finished || (socket && !socket.writable))
return (
msg.finished &&
msg.outputSize === 0 &&
(!socket || socket.writableLength === 0)
) || (socket && !socket.writable)
}

if (typeof msg.complete === 'boolean') {
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ describe('isFinished(res)', function () {

it('should be false before response finishes', function (done) {
var server = http.createServer(function (req, res) {
assert.ok(!onFinished.isFinished(res))
res.end()
assert.ok(!onFinished.isFinished(res))
done()
})

Expand Down

0 comments on commit b9c2fa4

Please sign in to comment.