Skip to content

Commit ba3e5c9

Browse files
inoway46aduh95
authored andcommitted
http2: add writeProcessing to compat response
The HTTP/1 ServerResponse has writeProcessing() to send a 102 Processing response, but Http2ServerResponse does not, so code moving to the compat API fails with a TypeError when it calls it. Add writeProcessing([callback]), which sends 102 through writeInformation() like writeContinue() does. Refs: #29829 Refs: #67705 (comment) Signed-off-by: inoway46 <inoueyuya416@gmail.com> Assisted-by: Claude Code PR-URL: #67728 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent eae0633 commit ba3e5c9

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎doc/api/http2.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5187,8 +5187,8 @@ Sends an arbitrary HTTP 1xx informational response, equivalent in HTTP/2 to a
51875187
multiple times before the final response. After the final response headers
51885188
have been sent, this method is a no-op and returns `false`.
51895189

5190-
This is the generic equivalent of [`response.writeContinue()`][] and
5191-
[`response.writeEarlyHints()`][].
5190+
This is the generic equivalent of [`response.writeContinue()`][],
5191+
[`response.writeProcessing()`][] and [`response.writeEarlyHints()`][].
51925192

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

5263+
#### `response.writeProcessing([callback])`
5264+
5265+
<!-- YAML
5266+
added: REPLACEME
5267+
-->
5268+
5269+
* `callback` {Function} Optional, called once the informational headers
5270+
have been written to the socket, or with an error if they could not be
5271+
written. It is not called if nothing is sent.
5272+
* `err` {Error|null}
5273+
5274+
Sends a status `102 Processing` to the client, indicating that the request
5275+
has been received and is still being processed.
5276+
52635277
## Collecting HTTP/2 performance metrics
52645278

52655279
The [Performance Observer][] API can be used to collect basic performance
@@ -5410,6 +5424,7 @@ you need to implement any fall-back behavior yourself.
54105424
[`response.writeContinue()`]: #responsewritecontinuecallback
54115425
[`response.writeEarlyHints()`]: #responsewriteearlyhintshints-callback
54125426
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
5427+
[`response.writeProcessing()`]: #responsewriteprocessingcallback
54135428
[`server.close()`]: #serverclosecallback
54145429
[`server.maxHeadersCount`]: http.md#servermaxheaderscount
54155430
[`tls.Server.close()`]: tls.md#serverclosecallback

‎lib/internal/http2/compat.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
HTTP_STATUS_EXPECTATION_FAILED,
2929
HTTP_STATUS_METHOD_NOT_ALLOWED,
3030
HTTP_STATUS_OK,
31+
HTTP_STATUS_PROCESSING,
3132
},
3233
} = internalBinding('http2');
3334
const {
@@ -1046,6 +1047,10 @@ class Http2ServerResponse extends Stream {
10461047
return this.writeInformation(HTTP_STATUS_CONTINUE, undefined, cb);
10471048
}
10481049

1050+
writeProcessing(cb) {
1051+
return this.writeInformation(HTTP_STATUS_PROCESSING, undefined, cb);
1052+
}
1053+
10491054
writeEarlyHints(hints, cb) {
10501055
validateObject(hints, 'hints');
10511056

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto) common.skip('missing crypto');
5+
6+
const assert = require('node:assert');
7+
const http2 = require('node:http2');
8+
9+
// writeProcessing() sends a 102 Processing informational response, with or
10+
// without a callback, and the callback is called asynchronously once it has
11+
// been written to the socket.
12+
{
13+
const server = http2.createServer();
14+
15+
server.on('request', common.mustCall((req, res) => {
16+
let sync = true;
17+
18+
assert.strictEqual(res.writeProcessing(), true);
19+
assert.strictEqual(res.writeProcessing(common.mustCall((err) => {
20+
assert.strictEqual(err, null);
21+
assert.strictEqual(sync, false);
22+
res.end('done');
23+
})), true);
24+
sync = false;
25+
}));
26+
27+
server.listen(0, common.mustCall(() => {
28+
const client = http2.connect(`http://localhost:${server.address().port}`);
29+
const req = client.request();
30+
31+
const seen = [];
32+
req.on('headers', (headers) => seen.push(headers[':status']));
33+
req.on('response', common.mustCall((headers) => {
34+
assert.strictEqual(headers[':status'], 200);
35+
assert.deepStrictEqual(seen, [102, 102]);
36+
}));
37+
38+
req.resume();
39+
req.on('end', common.mustCall(() => {
40+
client.close();
41+
server.close();
42+
}));
43+
}));
44+
}
45+
46+
// A non-function callback is rejected, and nothing is sent once the final
47+
// response headers have been sent.
48+
{
49+
const server = http2.createServer();
50+
51+
server.on('request', common.mustCall((req, res) => {
52+
for (const cb of [null, 1, 'cb', {}]) {
53+
assert.throws(() => res.writeProcessing(cb),
54+
{ code: 'ERR_INVALID_ARG_TYPE' });
55+
}
56+
57+
res.writeHead(200);
58+
assert.strictEqual(res.writeProcessing(common.mustNotCall()), false);
59+
res.end();
60+
}));
61+
62+
server.listen(0, common.mustCall(() => {
63+
const client = http2.connect(`http://localhost:${server.address().port}`);
64+
const req = client.request();
65+
req.on('headers', common.mustNotCall());
66+
req.on('response', common.mustCall((headers) => {
67+
assert.strictEqual(headers[':status'], 200);
68+
}));
69+
req.resume();
70+
req.on('end', common.mustCall(() => {
71+
client.close();
72+
server.close();
73+
}));
74+
}));
75+
}

0 commit comments

Comments
 (0)