diff options
author | Mark S. Everitt <mark.s.everitt@gmail.com> | 2019-02-07 01:24:50 +0300 |
---|---|---|
committer | Beth Griggs <Bethany.Griggs@uk.ibm.com> | 2019-10-07 14:04:49 +0300 |
commit | 925849650bf867c1ecc3525a75d2ddce2357c4b0 (patch) | |
tree | e09f36b6121446cdf7537a8d4c1a98dbd48b39b2 | |
parent | ccf2823f8318a3881301144e6fda1e1be9506bb5 (diff) |
http2: makes response.writeHead return the response
Fixes: https://github.com/nodejs/node/issues/25935
PR-URL: https://github.com/nodejs/node/pull/25974
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
-rw-r--r-- | doc/api/http2.md | 8 | ||||
-rw-r--r-- | lib/internal/http2/compat.js | 6 | ||||
-rw-r--r-- | test/parallel/test-http2-compat-serverresponse-writehead-array.js | 3 | ||||
-rw-r--r-- | test/parallel/test-http2-compat-serverresponse-writehead.js | 6 |
4 files changed, 18 insertions, 5 deletions
diff --git a/doc/api/http2.md b/doc/api/http2.md index 897ccb5584c..d5f602316fd 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -3284,15 +3284,23 @@ should be sent. See the [`'checkContinue'`][] event on `Http2Server` and #### response.writeHead(statusCode[, statusMessage][, headers]) <!-- YAML 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()`. --> * `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 `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 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 3e18ca36544..b100498e978 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -593,10 +593,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(); @@ -621,6 +619,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 e024f8ab395..d856165d090 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 5fd787e1003..aabcf18ad9e 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' |