From 04c715fdf5f5aeaee913f24c276755dbc3f45501 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 13 May 2019 19:41:48 +0200 Subject: [PATCH] http2: do no throw in writeHead if state.closed The http1 implementation does not throw if the connection is down. The http2 compat implementation should do the same. See: https://github.com/fastify/fastify-http-proxy/issues/51. See: https://github.com/fastify/fastify/issues/1494. --- lib/internal/http2/compat.js | 7 ++----- .../parallel/test-http2-compat-serverresponse-writehead.js | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 37f8242b0068f3..516954587108a8 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -589,14 +589,11 @@ class Http2ServerResponse extends Stream { writeHead(statusCode, statusMessage, headers) { const state = this[kState]; - if (state.closed) - throw new ERR_HTTP2_INVALID_STREAM(); + if (state.closed || this.stream.destroyed) + return this; if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); - if (this.stream.destroyed) - return this; - if (typeof statusMessage === 'string') statusMessageWarn(); diff --git a/test/parallel/test-http2-compat-serverresponse-writehead.js b/test/parallel/test-http2-compat-serverresponse-writehead.js index aabcf18ad9e0cf..e66f5f79cd9041 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead.js @@ -26,9 +26,9 @@ server.listen(0, common.mustCall(function() { response.on('finish', common.mustCall(function() { server.close(); process.nextTick(common.mustCall(() => { - common.expectsError(() => { response.writeHead(300); }, { - code: 'ERR_HTTP2_INVALID_STREAM' - }); + // The stream is invalid at this point, + // and this line verifies this does not throw. + response.writeHead(300); })); })); response.end();