diff --git a/doc/api/http2.md b/doc/api/http2.md index 03f971a07542..7d4291f918e1 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -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])` + + + +* `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 diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 113a2af560b1..e7ff3bc1f0c1 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -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'); diff --git a/test/parallel/test-http2-compat-serverresponse-write-processing.js b/test/parallel/test-http2-compat-serverresponse-write-processing.js new file mode 100644 index 000000000000..a947a4803795 --- /dev/null +++ b/test/parallel/test-http2-compat-serverresponse-write-processing.js @@ -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(); + })); + })); +}