Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -5187,8 +5187,8 @@ Sends an arbitrary HTTP 1xx informational response, equivalent in HTTP/2 to a
multiple times before the final response. After the final response headers
have been sent, this method is a no-op and returns `false`.

This is the generic equivalent of [`response.writeContinue()`][] and
[`response.writeEarlyHints()`][].
This is the generic equivalent of [`response.writeContinue()`][],
[`response.writeProcessing()`][] and [`response.writeEarlyHints()`][].

```js
response.writeInformation(110, { 'X-Progress': '50%' });
Expand Down Expand Up @@ -5260,6 +5260,20 @@ const server = http2.createServer((req, res) => {
Attempting to set a header field name or value that contains invalid characters
will result in a [`TypeError`][] being thrown.

#### `response.writeProcessing([callback])`

<!-- YAML
added: REPLACEME
-->

* `callback` {Function} Optional, called once the informational headers
have been written to the socket, or with an error if they could not be
written. It is not called if nothing is sent.
* `err` {Error|null}

Sends a status `102 Processing` to the client, indicating that the request
has been received and is still being processed.

## Collecting HTTP/2 performance metrics

The [Performance Observer][] API can be used to collect basic performance
Expand Down Expand Up @@ -5410,6 +5424,7 @@ you need to implement any fall-back behavior yourself.
[`response.writeContinue()`]: #responsewritecontinuecallback
[`response.writeEarlyHints()`]: #responsewriteearlyhintshints-callback
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
[`response.writeProcessing()`]: #responsewriteprocessingcallback
[`server.close()`]: #serverclosecallback
[`server.maxHeadersCount`]: http.md#servermaxheaderscount
[`tls.Server.close()`]: tls.md#serverclosecallback
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
HTTP_STATUS_EXPECTATION_FAILED,
HTTP_STATUS_METHOD_NOT_ALLOWED,
HTTP_STATUS_OK,
HTTP_STATUS_PROCESSING,
},
} = internalBinding('http2');
const {
Expand Down Expand Up @@ -1046,6 +1047,10 @@ class Http2ServerResponse extends Stream {
return this.writeInformation(HTTP_STATUS_CONTINUE, undefined, cb);
}

writeProcessing(cb) {
return this.writeInformation(HTTP_STATUS_PROCESSING, undefined, cb);
}

writeEarlyHints(hints, cb) {
validateObject(hints, 'hints');

Expand Down
75 changes: 75 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-write-processing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');

const assert = require('node:assert');
const http2 = require('node:http2');

// writeProcessing() sends a 102 Processing informational response, with or
// without a callback, and the callback is called asynchronously once it has
// been written to the socket.
{
const server = http2.createServer();

server.on('request', common.mustCall((req, res) => {
let sync = true;

assert.strictEqual(res.writeProcessing(), true);
assert.strictEqual(res.writeProcessing(common.mustCall((err) => {
assert.strictEqual(err, null);
assert.strictEqual(sync, false);
res.end('done');
})), true);
sync = false;
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const req = client.request();

const seen = [];
req.on('headers', (headers) => seen.push(headers[':status']));
req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
assert.deepStrictEqual(seen, [102, 102]);
}));

req.resume();
req.on('end', common.mustCall(() => {
client.close();
server.close();
}));
}));
}

// A non-function callback is rejected, and nothing is sent once the final
// response headers have been sent.
{
const server = http2.createServer();

server.on('request', common.mustCall((req, res) => {
for (const cb of [null, 1, 'cb', {}]) {
assert.throws(() => res.writeProcessing(cb),
{ code: 'ERR_INVALID_ARG_TYPE' });
}

res.writeHead(200);
assert.strictEqual(res.writeProcessing(common.mustNotCall()), false);
res.end();
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.on('headers', common.mustNotCall());
req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
}));
req.resume();
req.on('end', common.mustCall(() => {
client.close();
server.close();
}));
}));
}
Loading