mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
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: https://github.com/nodejs/node/issues/29829 Refs: https://github.com/nodejs/node/pull/67705#issuecomment-7098482431 Signed-off-by: inoway46 <inoueyuya416@gmail.com> Assisted-by: Claude Code PR-URL: https://github.com/nodejs/node/pull/67728 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
1 parent
18bb2e5e0e
commit
bf201ef3e3
3 files changed
+97
-2
No files matched your search
+17
-2
@@ -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%' });
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -28,6 +28,7 @@ const {
|
||||
HTTP_STATUS_EXPECTATION_FAILED,
|
||||
HTTP_STATUS_METHOD_NOT_ALLOWED,
|
||||
HTTP_STATUS_OK,
|
||||
HTTP_STATUS_PROCESSING,
|
||||
},
|
||||
} = internalBinding('http2');
|
||||
const {
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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();
|
||||
}));
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user