From b9c2fa41aa2353c5c4b3048612e432c39c387c31 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 25 Jun 2019 00:24:52 +0200 Subject: [PATCH] msg.finished is only finished if socket has been detached --- index.js | 13 ++++++++++++- test/test.js | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a6724cf..13de6fa 100644 --- a/index.js +++ b/index.js @@ -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') { 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() })