From 4c869c8d9e22db8616bb19d119b3c77bea971f03 Mon Sep 17 00:00:00 2001 From: Wing Date: Wed, 17 Aug 2022 20:22:53 +0200 Subject: [PATCH] http: add writeEarlyHints function to ServerResponse Co-Authored-By: Matteo Collina Co-Authored-By: Livia Medeiros PR-URL: https://github.com/nodejs/node/pull/44180 Reviewed-By: Robert Nagy Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina Reviewed-By: LiviaMedeiros --- doc/api/http.md | 33 +++- doc/api/http2.md | 26 ++++ lib/_http_server.js | 40 ++++- lib/internal/http2/compat.js | 45 ++++++ lib/internal/validators.js | 16 ++ ...-http-early-hints-invalid-argument-type.js | 33 ++++ .../test-http-early-hints-invalid-argument.js | 33 ++++ test/parallel/test-http-early-hints.js | 135 +++++++++++++++++ ...write-early-hints-invalid-argument-type.js | 38 +++++ ...mpat-write-early-hints-invalid-argument.js | 38 +++++ .../test-http2-compat-write-early-hints.js | 141 ++++++++++++++++++ 11 files changed, 576 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-http-early-hints-invalid-argument-type.js create mode 100644 test/parallel/test-http-early-hints-invalid-argument.js create mode 100644 test/parallel/test-http-early-hints.js create mode 100644 test/parallel/test-http2-compat-write-early-hints-invalid-argument-type.js create mode 100644 test/parallel/test-http2-compat-write-early-hints-invalid-argument.js create mode 100644 test/parallel/test-http2-compat-write-early-hints.js diff --git a/doc/api/http.md b/doc/api/http.md index f892c217b79c22..0576ab54a3b8e0 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2120,10 +2120,41 @@ buffer. Returns `false` if all or part of the data was queued in user memory. added: v0.3.0 --> -Sends a HTTP/1.1 100 Continue message to the client, indicating that +Sends an HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent. See the [`'checkContinue'`][] event on `Server`. +### `response.writeEarlyHints(links[, callback])` + + + +* `links` {string|Array} +* `callback` {Function} + +Sends an HTTP/1.1 103 Early Hints message to the client with a Link header, +indicating that the user agent can preload/preconnect the linked resources. +The `links` can be a string or an array of strings containing the values +of the `Link` header. The optional `callback` argument will be called when +the response message has been written. + +**Example** + +```js +const earlyHintsLink = '; rel=preload; as=style'; +response.writeEarlyHints(earlyHintsLink); + +const earlyHintsLinks = [ + '; rel=preload; as=style', + '; rel=preload; as=script', +]; +response.writeEarlyHints(earlyHintsLinks); + +const earlyHintsCallback = () => console.log('early hints message sent'); +response.writeEarlyHints(earlyHintsLinks, earlyHintsCallback); +``` + ### `response.writeHead(statusCode[, statusMessage][, headers])` + +* `links` {string|Array} + +Sends a status `103 Early Hints` to the client with a Link header, +indicating that the user agent can preload/preconnect the linked resources. +The `links` can be a string or an array of strings containing the values +of the `Link` header. + +**Example** + +```js +const earlyHintsLink = '; rel=preload; as=style'; +response.writeEarlyHints(earlyHintsLink); + +const earlyHintsLinks = [ + '; rel=preload; as=style', + '; rel=preload; as=script', +]; +response.writeEarlyHints(earlyHintsLinks); +``` + #### `response.writeHead(statusCode[, statusMessage][, headers])`