diff --git a/index.js b/index.js index a6724cf..73a2495 100644 --- a/index.js +++ b/index.js @@ -64,10 +64,17 @@ 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 Boolean((!socket && msg.finished && msg.outputSize === 0) || (socket && !socket.writable)) } if (typeof msg.complete === 'boolean') { diff --git a/test/test.js b/test/test.js index a3b2eab..69505f0 100644 --- a/test/test.js +++ b/test/test.js @@ -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() })