From 2e265aaa214d3de0d3bbeef03fe2c1d630aff64c Mon Sep 17 00:00:00 2001 From: "Mark S. Everitt" Date: Wed, 6 Feb 2019 22:17:23 +0000 Subject: [PATCH 1/7] http: makes response.writeHead return the response Fixes: https://github.com/nodejs/node/issues/25935 --- doc/api/http.md | 15 ++++++++++--- lib/_http_server.js | 2 ++ ...st-http-response-writehead-returns-this.js | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-http-response-writehead-returns-this.js diff --git a/doc/api/http.md b/doc/api/http.md index 149fc536687f37..8a0e72ec56d186 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1446,6 +1446,9 @@ the request body should be sent. See the [`'checkContinue'`][] event on * `statusCode` {number} * `statusMessage` {string} * `headers` {Object} +* Returns: {http2.Http2ServerResponse} Sends a response header to the request. The status code is a 3-digit HTTP status code, like `404`. The last argument, `headers`, are the response headers. +Returns a reference to the `ServerResponse`, so that calls can be chained. + For compatibility with [HTTP/1][], a human-readable `statusMessage` may be passed as the second argument. However, because the `statusMessage` has no meaning within HTTP/2, the argument will have no effect and a process warning diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 23c4e2e0f08893..faecf2441ce4c8 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -568,10 +568,8 @@ class Http2ServerResponse extends Stream { if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); - // If the stream is destroyed, we return false, - // like require('http'). if (this.stream.destroyed) - return false; + return this; if (typeof statusMessage === 'string') statusMessageWarn(); @@ -596,6 +594,8 @@ class Http2ServerResponse extends Stream { state.statusCode = statusCode; this[kBeginSend](); + + return this; } write(chunk, encoding, cb) { diff --git a/test/parallel/test-http2-compat-serverresponse-writehead-array.js b/test/parallel/test-http2-compat-serverresponse-writehead-array.js index e024f8ab3952f0..d856165d090f46 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead-array.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead-array.js @@ -12,10 +12,11 @@ const server = h2.createServer(); server.listen(0, common.mustCall(() => { const port = server.address().port; server.once('request', common.mustCall((request, response) => { - response.writeHead(200, [ + const returnVal = response.writeHead(200, [ ['foo', 'bar'], ['ABC', 123] ]); + assert.strictEqual(returnVal, response); response.end(common.mustCall(() => { server.close(); })); })); diff --git a/test/parallel/test-http2-compat-serverresponse-writehead.js b/test/parallel/test-http2-compat-serverresponse-writehead.js index 5fd787e100350c..aabcf18ad9e0cf 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead.js @@ -13,7 +13,11 @@ server.listen(0, common.mustCall(function() { const port = server.address().port; server.once('request', common.mustCall(function(request, response) { response.setHeader('foo-bar', 'def456'); - response.writeHead(418, { 'foo-bar': 'abc123' }); // Override + + // Override + const returnVal = response.writeHead(418, { 'foo-bar': 'abc123' }); + + assert.strictEqual(returnVal, response); common.expectsError(() => { response.writeHead(300); }, { code: 'ERR_HTTP2_HEADERS_SENT' From 1ce1dcc8f8304b3ebb3f71e1b1a4857b9a3f136a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 7 Feb 2019 00:55:20 +0000 Subject: [PATCH 3/7] http: update to documentation Addresses PR feedback. Co-Authored-By: qubyte --- doc/api/http.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 8a0e72ec56d186..991f8bfcf23b87 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1446,7 +1446,7 @@ the request body should be sent. See the [`'checkContinue'`][] event on From 2f476b61f9f7c20df1f9c38742c1fa9529b539ed Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 7 Feb 2019 01:39:10 +0000 Subject: [PATCH 5/7] http2: update to documentation Addresses PR feedback. Co-Authored-By: qubyte --- doc/api/http2.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/http2.md b/doc/api/http2.md index 2af61069ffc453..35c801ef9b4dd3 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -3286,7 +3286,8 @@ added: v8.4.0 changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/25974 - description: Return `this` from `writeHead` to allow chaining with `end`. + description: Return `this` from `writeHead()` to allow chaining with + `end()`. --> * `statusCode` {number} From c5d4090ca03bd50ebd63ddc2877d0124642431fe Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 7 Feb 2019 01:40:07 +0000 Subject: [PATCH 6/7] http: update to documentation Addresses PR feedback. Co-Authored-By: qubyte --- doc/api/http.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 991f8bfcf23b87..9031f30deacade 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1448,7 +1448,8 @@ added: v0.1.30 changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/25974 - description: Return `this` from `writeHead` to allow chaining with `end`. + description: Return `this` from `writeHead()` to allow chaining with + `end()`. - version: v5.11.0, v4.4.5 pr-url: https://github.com/nodejs/node/pull/6291 description: A `RangeError` is thrown if `statusCode` is not a number in From df45bdc06156e2a3ff515e3ebfc7e15f6cf86ff3 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 7 Feb 2019 08:34:32 +0000 Subject: [PATCH 7/7] http2: update to documentation Addresses PR feedback. Co-Authored-By: qubyte --- doc/api/http2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http2.md b/doc/api/http2.md index 35c801ef9b4dd3..f8eb4ab1a3b550 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -3298,7 +3298,7 @@ changes: Sends a response header to the request. The status code is a 3-digit HTTP status code, like `404`. The last argument, `headers`, are the response headers. -Returns a reference to the `ServerResponse`, so that calls can be chained. +Returns a reference to the `Http2ServerResponse`, so that calls can be chained. For compatibility with [HTTP/1][], a human-readable `statusMessage` may be passed as the second argument. However, because the `statusMessage` has no