Skip to content

Commit 237b5e6

Browse files
qubytetargos
authored andcommitted
http2: makes response.writeHead return the response
Fixes: #25935 PR-URL: #25974 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent a5247cc commit 237b5e6

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
@@ -3283,15 +3283,23 @@ should be sent. See the [`'checkContinue'`][] event on `Http2Server` and
32833283
#### response.writeHead(statusCode[, statusMessage][, headers])
32843284
<!-- YAML
32853285
added: v8.4.0
3286+
changes:
3287+
- version: REPLACEME
3288+
pr-url: https://github.com/nodejs/node/pull/25974
3289+
description: Return `this` from `writeHead()` to allow chaining with
3290+
`end()`.
32863291
-->
32873292

32883293
* `statusCode` {number}
32893294
* `statusMessage` {string}
32903295
* `headers` {Object}
3296+
* Returns: {http2.Http2ServerResponse}
32913297

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

3301+
Returns a reference to the `Http2ServerResponse`, so that calls can be chained.
3302+
32953303
For compatibility with [HTTP/1][], a human-readable `statusMessage` may be
32963304
passed as the second argument. However, because the `statusMessage` has no
32973305
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
@@ -568,10 +568,8 @@ class Http2ServerResponse extends Stream {
568568
if (this[kStream].headersSent)
569569
throw new ERR_HTTP2_HEADERS_SENT();
570570

571-
// If the stream is destroyed, we return false,
572-
// like require('http').
573571
if (this.stream.destroyed)
574-
return false;
572+
return this;
575573

576574
if (typeof statusMessage === 'string')
577575
statusMessageWarn();
@@ -596,6 +594,8 @@ class Http2ServerResponse extends Stream {
596594

597595
state.statusCode = statusCode;
598596
this[kBeginSend]();
597+
598+
return this;
599599
}
600600

601601
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)