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>
This commit is contained in:
parent
f93df51155
commit
1aa11e1444
@ -3283,15 +3283,23 @@ should be sent. See the [`'checkContinue'`][] event on `Http2Server` and
|
|||||||
#### response.writeHead(statusCode[, statusMessage][, headers])
|
#### response.writeHead(statusCode[, statusMessage][, headers])
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v8.4.0
|
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}
|
* `statusCode` {number}
|
||||||
* `statusMessage` {string}
|
* `statusMessage` {string}
|
||||||
* `headers` {Object}
|
* `headers` {Object}
|
||||||
|
* Returns: {http2.Http2ServerResponse}
|
||||||
|
|
||||||
Sends a response header to the request. The status code is a 3-digit HTTP
|
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.
|
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
|
For compatibility with [HTTP/1][], a human-readable `statusMessage` may be
|
||||||
passed as the second argument. However, because the `statusMessage` has no
|
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
|
meaning within HTTP/2, the argument will have no effect and a process warning
|
||||||
|
@ -568,10 +568,8 @@ class Http2ServerResponse extends Stream {
|
|||||||
if (this[kStream].headersSent)
|
if (this[kStream].headersSent)
|
||||||
throw new ERR_HTTP2_HEADERS_SENT();
|
throw new ERR_HTTP2_HEADERS_SENT();
|
||||||
|
|
||||||
// If the stream is destroyed, we return false,
|
|
||||||
// like require('http').
|
|
||||||
if (this.stream.destroyed)
|
if (this.stream.destroyed)
|
||||||
return false;
|
return this;
|
||||||
|
|
||||||
if (typeof statusMessage === 'string')
|
if (typeof statusMessage === 'string')
|
||||||
statusMessageWarn();
|
statusMessageWarn();
|
||||||
@ -596,6 +594,8 @@ class Http2ServerResponse extends Stream {
|
|||||||
|
|
||||||
state.statusCode = statusCode;
|
state.statusCode = statusCode;
|
||||||
this[kBeginSend]();
|
this[kBeginSend]();
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
write(chunk, encoding, cb) {
|
write(chunk, encoding, cb) {
|
||||||
|
@ -12,10 +12,11 @@ const server = h2.createServer();
|
|||||||
server.listen(0, common.mustCall(() => {
|
server.listen(0, common.mustCall(() => {
|
||||||
const port = server.address().port;
|
const port = server.address().port;
|
||||||
server.once('request', common.mustCall((request, response) => {
|
server.once('request', common.mustCall((request, response) => {
|
||||||
response.writeHead(200, [
|
const returnVal = response.writeHead(200, [
|
||||||
['foo', 'bar'],
|
['foo', 'bar'],
|
||||||
['ABC', 123]
|
['ABC', 123]
|
||||||
]);
|
]);
|
||||||
|
assert.strictEqual(returnVal, response);
|
||||||
response.end(common.mustCall(() => { server.close(); }));
|
response.end(common.mustCall(() => { server.close(); }));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -13,7 +13,11 @@ server.listen(0, common.mustCall(function() {
|
|||||||
const port = server.address().port;
|
const port = server.address().port;
|
||||||
server.once('request', common.mustCall(function(request, response) {
|
server.once('request', common.mustCall(function(request, response) {
|
||||||
response.setHeader('foo-bar', 'def456');
|
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); }, {
|
common.expectsError(() => { response.writeHead(300); }, {
|
||||||
code: 'ERR_HTTP2_HEADERS_SENT'
|
code: 'ERR_HTTP2_HEADERS_SENT'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user