GeezerVison is showing github.com — http2: add writeProcessing to compat response · nodejs/node@ba3e5c9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/http2.md +17-2Lines changed: 17 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -5187,8 +5187,8 @@ Sends an arbitrary HTTP 1xx informational response, equivalent in HTTP/2 to a | 5187 | 5187 | multiple times before the final response. After the final response headers | 5188 | 5188 | have been sent, this method is a no-op and returns `false`. | 5189 | 5189 |
| 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()`][]. | 5192 | 5192 |
| 5193 | 5193 | ```js | 5194 | 5194 | response.writeInformation(110, { 'X-Progress': '50%' }); | @@ -5260,6 +5260,20 @@ const server = http2.createServer((req, res) => { | 5260 | 5260 | Attempting to set a header field name or value that contains invalid characters | 5261 | 5261 | will result in a [`TypeError`][] being thrown. | 5262 | 5262 |
| | 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 | + | 5263 | 5277 | ## Collecting HTTP/2 performance metrics | 5264 | 5278 |
| 5265 | 5279 | The [Performance Observer][] API can be used to collect basic performance | @@ -5410,6 +5424,7 @@ you need to implement any fall-back behavior yourself. | 5410 | 5424 | [`response.writeContinue()`]: #responsewritecontinuecallback | 5411 | 5425 | [`response.writeEarlyHints()`]: #responsewriteearlyhintshints-callback | 5412 | 5426 | [`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers | | 5427 | +[`response.writeProcessing()`]: #responsewriteprocessingcallback | 5413 | 5428 | [`server.close()`]: #serverclosecallback | 5414 | 5429 | [`server.maxHeadersCount`]: http.md#servermaxheaderscount | 5415 | 5430 | [`tls.Server.close()`]: tls.md#serverclosecallback | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/compat.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ const { | 28 | 28 | HTTP_STATUS_EXPECTATION_FAILED, | 29 | 29 | HTTP_STATUS_METHOD_NOT_ALLOWED, | 30 | 30 | HTTP_STATUS_OK, | | 31 | + HTTP_STATUS_PROCESSING, | 31 | 32 | }, | 32 | 33 | } = internalBinding('http2'); | 33 | 34 | const { | @@ -1046,6 +1047,10 @@ class Http2ServerResponse extends Stream { | 1046 | 1047 | return this.writeInformation(HTTP_STATUS_CONTINUE, undefined, cb); | 1047 | 1048 | } | 1048 | 1049 |
| | 1050 | + writeProcessing(cb) { | | 1051 | + return this.writeInformation(HTTP_STATUS_PROCESSING, undefined, cb); | | 1052 | + } | | 1053 | + | 1049 | 1054 | writeEarlyHints(hints, cb) { | 1050 | 1055 | validateObject(hints, 'hints'); | 1051 | 1056 |
| |
Collapse file Copy file name to clipboard +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|