Skip to content

Commit 9258496

Browse files
qubyteBethGriggs
authored andcommitted
http2: makes response.writeHead return the response
Fixes: #25935 PR-URL: #25974 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent ccf2823 commit 9258496

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

doc/api/http2.md

+8
Original file line numberDiff line numberDiff line change
@@ -3284,15 +3284,23 @@ should be sent. See the [`'checkContinue'`][] event on `Http2Server` and
32843284
#### response.writeHead(statusCode[, statusMessage][, headers])
32853285
<!-- YAML
32863286
added: v8.4.0
3287+
changes:
3288+
- version: REPLACEME
3289+
pr-url: https://github.com/nodejs/node/pull/25974
3290+
description: Return `this` from `writeHead()` to allow chaining with
3291+
`end()`.
32873292
-->
32883293

32893294
* `statusCode` {number}
32903295
* `statusMessage` {string}
32913296
* `headers` {Object}
3297+
* Returns: {http2.Http2ServerResponse}
32923298

32933299
Sends a response header to the request. The status code is a 3-digit HTTP
32943300
status code, like `404`. The last argument, `headers`, are the response headers.
32953301

3302+
Returns a reference to the `Http2ServerResponse`, so that calls can be chained.
3303+
32963304
For compatibility with [HTTP/1][], a human-readable `statusMessage` may be
32973305
passed as the second argument. However, because the `statusMessage` has no
32983306
meaning within HTTP/2, the argument will have no effect and a process warning

lib/internal/http2/compat.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,8 @@ class Http2ServerResponse extends Stream {
593593
if (this[kStream].headersSent)
594594
throw new ERR_HTTP2_HEADERS_SENT();
595595

596-
// If the stream is destroyed, we return false,
597-
// like require('http').
598596
if (this.stream.destroyed)
599-
return false;
597+
return this;
600598

601599
if (typeof statusMessage === 'string')
602600
statusMessageWarn();
@@ -621,6 +619,8 @@ class Http2ServerResponse extends Stream {
621619

622620
state.statusCode = statusCode;
623621
this[kBeginSend]();
622+
623+
return this;
624624
}
625625

626626
write(chunk, encoding, cb) {

test/parallel/test-http2-compat-serverresponse-writehead-array.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const server = h2.createServer();
1212
server.listen(0, common.mustCall(() => {
1313
const port = server.address().port;
1414
server.once('request', common.mustCall((request, response) => {
15-
response.writeHead(200, [
15+
const returnVal = response.writeHead(200, [
1616
['foo', 'bar'],
1717
['ABC', 123]
1818
]);
19+
assert.strictEqual(returnVal, response);
1920
response.end(common.mustCall(() => { server.close(); }));
2021
}));
2122

test/parallel/test-http2-compat-serverresponse-writehead.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ server.listen(0, common.mustCall(function() {
1313
const port = server.address().port;
1414
server.once('request', common.mustCall(function(request, response) {
1515
response.setHeader('foo-bar', 'def456');
16-
response.writeHead(418, { 'foo-bar': 'abc123' }); // Override
16+
17+
// Override
18+
const returnVal = response.writeHead(418, { 'foo-bar': 'abc123' });
19+
20+
assert.strictEqual(returnVal, response);
1721

1822
common.expectsError(() => { response.writeHead(300); }, {
1923
code: 'ERR_HTTP2_HEADERS_SENT'

0 commit comments

Comments
 (0)