11cb0ef41Sopenharmony_ci# HTTP/2 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!-- YAML 41cb0ef41Sopenharmony_ciadded: v8.4.0 51cb0ef41Sopenharmony_cichanges: 61cb0ef41Sopenharmony_ci - version: 71cb0ef41Sopenharmony_ci - v15.3.0 81cb0ef41Sopenharmony_ci - v14.17.0 91cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/36070 101cb0ef41Sopenharmony_ci description: It is possible to abort a request with an AbortSignal. 111cb0ef41Sopenharmony_ci - version: v15.0.0 121cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34664 131cb0ef41Sopenharmony_ci description: Requests with the `host` header (with or without 141cb0ef41Sopenharmony_ci `:authority`) can now be sent/received. 151cb0ef41Sopenharmony_ci - version: v10.10.0 161cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22466 171cb0ef41Sopenharmony_ci description: HTTP/2 is now Stable. Previously, it had been Experimental. 181cb0ef41Sopenharmony_ci--> 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci<!--introduced_in=v8.4.0--> 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci> Stability: 2 - Stable 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci<!-- source_link=lib/http2.js --> 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciThe `node:http2` module provides an implementation of the [HTTP/2][] protocol. 271cb0ef41Sopenharmony_ciIt can be accessed using: 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci```js 301cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 311cb0ef41Sopenharmony_ci``` 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci## Determining if crypto support is unavailable 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciIt is possible for Node.js to be built without including support for the 361cb0ef41Sopenharmony_ci`node:crypto` module. In such cases, attempting to `import` from `node:http2` or 371cb0ef41Sopenharmony_cicalling `require('node:http2')` will result in an error being thrown. 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciWhen using CommonJS, the error thrown can be caught using try/catch: 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci```cjs 421cb0ef41Sopenharmony_cilet http2; 431cb0ef41Sopenharmony_citry { 441cb0ef41Sopenharmony_ci http2 = require('node:http2'); 451cb0ef41Sopenharmony_ci} catch (err) { 461cb0ef41Sopenharmony_ci console.error('http2 support is disabled!'); 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci``` 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciWhen using the lexical ESM `import` keyword, the error can only be 511cb0ef41Sopenharmony_cicaught if a handler for `process.on('uncaughtException')` is registered 521cb0ef41Sopenharmony_ci_before_ any attempt to load the module is made (using, for instance, 531cb0ef41Sopenharmony_cia preload module). 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciWhen using ESM, if there is a chance that the code may be run on a build 561cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the 571cb0ef41Sopenharmony_ci[`import()`][] function instead of the lexical `import` keyword: 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci```mjs 601cb0ef41Sopenharmony_cilet http2; 611cb0ef41Sopenharmony_citry { 621cb0ef41Sopenharmony_ci http2 = await import('node:http2'); 631cb0ef41Sopenharmony_ci} catch (err) { 641cb0ef41Sopenharmony_ci console.error('http2 support is disabled!'); 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci``` 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci## Core API 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciThe Core API provides a low-level interface designed specifically around 711cb0ef41Sopenharmony_cisupport for HTTP/2 protocol features. It is specifically _not_ designed for 721cb0ef41Sopenharmony_cicompatibility with the existing [HTTP/1][] module API. However, 731cb0ef41Sopenharmony_cithe [Compatibility API][] is. 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciThe `http2` Core API is much more symmetric between client and server than the 761cb0ef41Sopenharmony_ci`http` API. For instance, most events, like `'error'`, `'connect'` and 771cb0ef41Sopenharmony_ci`'stream'`, can be emitted either by client-side code or server-side code. 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci### Server-side example 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciThe following illustrates a simple HTTP/2 server using the Core API. 821cb0ef41Sopenharmony_ciSince there are no browsers known that support 831cb0ef41Sopenharmony_ci[unencrypted HTTP/2][HTTP/2 Unencrypted], the use of 841cb0ef41Sopenharmony_ci[`http2.createSecureServer()`][] is necessary when communicating 851cb0ef41Sopenharmony_ciwith browser clients. 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci```js 881cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 891cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ciconst server = http2.createSecureServer({ 921cb0ef41Sopenharmony_ci key: fs.readFileSync('localhost-privkey.pem'), 931cb0ef41Sopenharmony_ci cert: fs.readFileSync('localhost-cert.pem'), 941cb0ef41Sopenharmony_ci}); 951cb0ef41Sopenharmony_ciserver.on('error', (err) => console.error(err)); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 981cb0ef41Sopenharmony_ci // stream is a Duplex 991cb0ef41Sopenharmony_ci stream.respond({ 1001cb0ef41Sopenharmony_ci 'content-type': 'text/html; charset=utf-8', 1011cb0ef41Sopenharmony_ci ':status': 200, 1021cb0ef41Sopenharmony_ci }); 1031cb0ef41Sopenharmony_ci stream.end('<h1>Hello World</h1>'); 1041cb0ef41Sopenharmony_ci}); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ciserver.listen(8443); 1071cb0ef41Sopenharmony_ci``` 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciTo generate the certificate and key for this example, run: 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci```bash 1121cb0ef41Sopenharmony_ciopenssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \ 1131cb0ef41Sopenharmony_ci -keyout localhost-privkey.pem -out localhost-cert.pem 1141cb0ef41Sopenharmony_ci``` 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci### Client-side example 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ciThe following illustrates an HTTP/2 client: 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci```js 1211cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 1221cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 1231cb0ef41Sopenharmony_ciconst client = http2.connect('https://localhost:8443', { 1241cb0ef41Sopenharmony_ci ca: fs.readFileSync('localhost-cert.pem'), 1251cb0ef41Sopenharmony_ci}); 1261cb0ef41Sopenharmony_ciclient.on('error', (err) => console.error(err)); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciconst req = client.request({ ':path': '/' }); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_cireq.on('response', (headers, flags) => { 1311cb0ef41Sopenharmony_ci for (const name in headers) { 1321cb0ef41Sopenharmony_ci console.log(`${name}: ${headers[name]}`); 1331cb0ef41Sopenharmony_ci } 1341cb0ef41Sopenharmony_ci}); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_cireq.setEncoding('utf8'); 1371cb0ef41Sopenharmony_cilet data = ''; 1381cb0ef41Sopenharmony_cireq.on('data', (chunk) => { data += chunk; }); 1391cb0ef41Sopenharmony_cireq.on('end', () => { 1401cb0ef41Sopenharmony_ci console.log(`\n${data}`); 1411cb0ef41Sopenharmony_ci client.close(); 1421cb0ef41Sopenharmony_ci}); 1431cb0ef41Sopenharmony_cireq.end(); 1441cb0ef41Sopenharmony_ci``` 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci### Class: `Http2Session` 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci<!-- YAML 1491cb0ef41Sopenharmony_ciadded: v8.4.0 1501cb0ef41Sopenharmony_ci--> 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci* Extends: {EventEmitter} 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciInstances of the `http2.Http2Session` class represent an active communications 1551cb0ef41Sopenharmony_cisession between an HTTP/2 client and server. Instances of this class are _not_ 1561cb0ef41Sopenharmony_ciintended to be constructed directly by user code. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ciEach `Http2Session` instance will exhibit slightly different behaviors 1591cb0ef41Sopenharmony_cidepending on whether it is operating as a server or a client. The 1601cb0ef41Sopenharmony_ci`http2session.type` property can be used to determine the mode in which an 1611cb0ef41Sopenharmony_ci`Http2Session` is operating. On the server side, user code should rarely 1621cb0ef41Sopenharmony_cihave occasion to work with the `Http2Session` object directly, with most 1631cb0ef41Sopenharmony_ciactions typically taken through interactions with either the `Http2Server` or 1641cb0ef41Sopenharmony_ci`Http2Stream` objects. 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciUser code will not create `Http2Session` instances directly. Server-side 1671cb0ef41Sopenharmony_ci`Http2Session` instances are created by the `Http2Server` instance when a 1681cb0ef41Sopenharmony_cinew HTTP/2 connection is received. Client-side `Http2Session` instances are 1691cb0ef41Sopenharmony_cicreated using the `http2.connect()` method. 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci#### `Http2Session` and sockets 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciEvery `Http2Session` instance is associated with exactly one [`net.Socket`][] or 1741cb0ef41Sopenharmony_ci[`tls.TLSSocket`][] when it is created. When either the `Socket` or the 1751cb0ef41Sopenharmony_ci`Http2Session` are destroyed, both will be destroyed. 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ciBecause of the specific serialization and processing requirements imposed 1781cb0ef41Sopenharmony_ciby the HTTP/2 protocol, it is not recommended for user code to read data from 1791cb0ef41Sopenharmony_cior write data to a `Socket` instance bound to a `Http2Session`. Doing so can 1801cb0ef41Sopenharmony_ciput the HTTP/2 session into an indeterminate state causing the session and 1811cb0ef41Sopenharmony_cithe socket to become unusable. 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ciOnce a `Socket` has been bound to an `Http2Session`, user code should rely 1841cb0ef41Sopenharmony_cisolely on the API of the `Http2Session`. 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci#### Event: `'close'` 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci<!-- YAML 1891cb0ef41Sopenharmony_ciadded: v8.4.0 1901cb0ef41Sopenharmony_ci--> 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ciThe `'close'` event is emitted once the `Http2Session` has been destroyed. Its 1931cb0ef41Sopenharmony_cilistener does not expect any arguments. 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci#### Event: `'connect'` 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci<!-- YAML 1981cb0ef41Sopenharmony_ciadded: v8.4.0 1991cb0ef41Sopenharmony_ci--> 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci* `session` {Http2Session} 2021cb0ef41Sopenharmony_ci* `socket` {net.Socket} 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciThe `'connect'` event is emitted once the `Http2Session` has been successfully 2051cb0ef41Sopenharmony_ciconnected to the remote peer and communication may begin. 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciUser code will typically not listen for this event directly. 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci#### Event: `'error'` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci<!-- YAML 2121cb0ef41Sopenharmony_ciadded: v8.4.0 2131cb0ef41Sopenharmony_ci--> 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci* `error` {Error} 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciThe `'error'` event is emitted when an error occurs during the processing of 2181cb0ef41Sopenharmony_cian `Http2Session`. 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci#### Event: `'frameError'` 2211cb0ef41Sopenharmony_ci 2221cb0ef41Sopenharmony_ci<!-- YAML 2231cb0ef41Sopenharmony_ciadded: v8.4.0 2241cb0ef41Sopenharmony_ci--> 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci* `type` {integer} The frame type. 2271cb0ef41Sopenharmony_ci* `code` {integer} The error code. 2281cb0ef41Sopenharmony_ci* `id` {integer} The stream id (or `0` if the frame isn't associated with a 2291cb0ef41Sopenharmony_ci stream). 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ciThe `'frameError'` event is emitted when an error occurs while attempting to 2321cb0ef41Sopenharmony_cisend a frame on the session. If the frame that could not be sent is associated 2331cb0ef41Sopenharmony_ciwith a specific `Http2Stream`, an attempt to emit a `'frameError'` event on the 2341cb0ef41Sopenharmony_ci`Http2Stream` is made. 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ciIf the `'frameError'` event is associated with a stream, the stream will be 2371cb0ef41Sopenharmony_ciclosed and destroyed immediately following the `'frameError'` event. If the 2381cb0ef41Sopenharmony_cievent is not associated with a stream, the `Http2Session` will be shut down 2391cb0ef41Sopenharmony_ciimmediately following the `'frameError'` event. 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci#### Event: `'goaway'` 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ci<!-- YAML 2441cb0ef41Sopenharmony_ciadded: v8.4.0 2451cb0ef41Sopenharmony_ci--> 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci* `errorCode` {number} The HTTP/2 error code specified in the `GOAWAY` frame. 2481cb0ef41Sopenharmony_ci* `lastStreamID` {number} The ID of the last stream the remote peer successfully 2491cb0ef41Sopenharmony_ci processed (or `0` if no ID is specified). 2501cb0ef41Sopenharmony_ci* `opaqueData` {Buffer} If additional opaque data was included in the `GOAWAY` 2511cb0ef41Sopenharmony_ci frame, a `Buffer` instance will be passed containing that data. 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ciThe `'goaway'` event is emitted when a `GOAWAY` frame is received. 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ciThe `Http2Session` instance will be shut down automatically when the `'goaway'` 2561cb0ef41Sopenharmony_cievent is emitted. 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci#### Event: `'localSettings'` 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci<!-- YAML 2611cb0ef41Sopenharmony_ciadded: v8.4.0 2621cb0ef41Sopenharmony_ci--> 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} A copy of the `SETTINGS` frame received. 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ciThe `'localSettings'` event is emitted when an acknowledgment `SETTINGS` frame 2671cb0ef41Sopenharmony_cihas been received. 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ciWhen using `http2session.settings()` to submit new settings, the modified 2701cb0ef41Sopenharmony_cisettings do not take effect until the `'localSettings'` event is emitted. 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ci```js 2731cb0ef41Sopenharmony_cisession.settings({ enablePush: false }); 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_cisession.on('localSettings', (settings) => { 2761cb0ef41Sopenharmony_ci /* Use the new settings */ 2771cb0ef41Sopenharmony_ci}); 2781cb0ef41Sopenharmony_ci``` 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci#### Event: `'ping'` 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci<!-- YAML 2831cb0ef41Sopenharmony_ciadded: v10.12.0 2841cb0ef41Sopenharmony_ci--> 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci* `payload` {Buffer} The `PING` frame 8-byte payload 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ciThe `'ping'` event is emitted whenever a `PING` frame is received from the 2891cb0ef41Sopenharmony_ciconnected peer. 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci#### Event: `'remoteSettings'` 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci<!-- YAML 2941cb0ef41Sopenharmony_ciadded: v8.4.0 2951cb0ef41Sopenharmony_ci--> 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} A copy of the `SETTINGS` frame received. 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ciThe `'remoteSettings'` event is emitted when a new `SETTINGS` frame is received 3001cb0ef41Sopenharmony_cifrom the connected peer. 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci```js 3031cb0ef41Sopenharmony_cisession.on('remoteSettings', (settings) => { 3041cb0ef41Sopenharmony_ci /* Use the new settings */ 3051cb0ef41Sopenharmony_ci}); 3061cb0ef41Sopenharmony_ci``` 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci#### Event: `'stream'` 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci<!-- YAML 3111cb0ef41Sopenharmony_ciadded: v8.4.0 3121cb0ef41Sopenharmony_ci--> 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci* `stream` {Http2Stream} A reference to the stream 3151cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} An object describing the headers 3161cb0ef41Sopenharmony_ci* `flags` {number} The associated numeric flags 3171cb0ef41Sopenharmony_ci* `rawHeaders` {Array} An array containing the raw header names followed by 3181cb0ef41Sopenharmony_ci their respective values. 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ciThe `'stream'` event is emitted when a new `Http2Stream` is created. 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci```js 3231cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 3241cb0ef41Sopenharmony_cisession.on('stream', (stream, headers, flags) => { 3251cb0ef41Sopenharmony_ci const method = headers[':method']; 3261cb0ef41Sopenharmony_ci const path = headers[':path']; 3271cb0ef41Sopenharmony_ci // ... 3281cb0ef41Sopenharmony_ci stream.respond({ 3291cb0ef41Sopenharmony_ci ':status': 200, 3301cb0ef41Sopenharmony_ci 'content-type': 'text/plain; charset=utf-8', 3311cb0ef41Sopenharmony_ci }); 3321cb0ef41Sopenharmony_ci stream.write('hello '); 3331cb0ef41Sopenharmony_ci stream.end('world'); 3341cb0ef41Sopenharmony_ci}); 3351cb0ef41Sopenharmony_ci``` 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ciOn the server side, user code will typically not listen for this event directly, 3381cb0ef41Sopenharmony_ciand would instead register a handler for the `'stream'` event emitted by the 3391cb0ef41Sopenharmony_ci`net.Server` or `tls.Server` instances returned by `http2.createServer()` and 3401cb0ef41Sopenharmony_ci`http2.createSecureServer()`, respectively, as in the example below: 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_ci```js 3431cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci// Create an unencrypted HTTP/2 server 3461cb0ef41Sopenharmony_ciconst server = http2.createServer(); 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 3491cb0ef41Sopenharmony_ci stream.respond({ 3501cb0ef41Sopenharmony_ci 'content-type': 'text/html; charset=utf-8', 3511cb0ef41Sopenharmony_ci ':status': 200, 3521cb0ef41Sopenharmony_ci }); 3531cb0ef41Sopenharmony_ci stream.on('error', (error) => console.error(error)); 3541cb0ef41Sopenharmony_ci stream.end('<h1>Hello World</h1>'); 3551cb0ef41Sopenharmony_ci}); 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ciserver.listen(8000); 3581cb0ef41Sopenharmony_ci``` 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ciEven though HTTP/2 streams and network sockets are not in a 1:1 correspondence, 3611cb0ef41Sopenharmony_cia network error will destroy each individual stream and must be handled on the 3621cb0ef41Sopenharmony_cistream level, as shown above. 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci#### Event: `'timeout'` 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ci<!-- YAML 3671cb0ef41Sopenharmony_ciadded: v8.4.0 3681cb0ef41Sopenharmony_ci--> 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ciAfter the `http2session.setTimeout()` method is used to set the timeout period 3711cb0ef41Sopenharmony_cifor this `Http2Session`, the `'timeout'` event is emitted if there is no 3721cb0ef41Sopenharmony_ciactivity on the `Http2Session` after the configured number of milliseconds. 3731cb0ef41Sopenharmony_ciIts listener does not expect any arguments. 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci```js 3761cb0ef41Sopenharmony_cisession.setTimeout(2000); 3771cb0ef41Sopenharmony_cisession.on('timeout', () => { /* .. */ }); 3781cb0ef41Sopenharmony_ci``` 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci#### `http2session.alpnProtocol` 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ci<!-- YAML 3831cb0ef41Sopenharmony_ciadded: v9.4.0 3841cb0ef41Sopenharmony_ci--> 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ci* {string|undefined} 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ciValue will be `undefined` if the `Http2Session` is not yet connected to a 3891cb0ef41Sopenharmony_cisocket, `h2c` if the `Http2Session` is not connected to a `TLSSocket`, or 3901cb0ef41Sopenharmony_ciwill return the value of the connected `TLSSocket`'s own `alpnProtocol` 3911cb0ef41Sopenharmony_ciproperty. 3921cb0ef41Sopenharmony_ci 3931cb0ef41Sopenharmony_ci#### `http2session.close([callback])` 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci<!-- YAML 3961cb0ef41Sopenharmony_ciadded: v9.4.0 3971cb0ef41Sopenharmony_ci--> 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci* `callback` {Function} 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ciGracefully closes the `Http2Session`, allowing any existing streams to 4021cb0ef41Sopenharmony_cicomplete on their own and preventing new `Http2Stream` instances from being 4031cb0ef41Sopenharmony_cicreated. Once closed, `http2session.destroy()` _might_ be called if there 4041cb0ef41Sopenharmony_ciare no open `Http2Stream` instances. 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ciIf specified, the `callback` function is registered as a handler for the 4071cb0ef41Sopenharmony_ci`'close'` event. 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci#### `http2session.closed` 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci<!-- YAML 4121cb0ef41Sopenharmony_ciadded: v9.4.0 4131cb0ef41Sopenharmony_ci--> 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ci* {boolean} 4161cb0ef41Sopenharmony_ci 4171cb0ef41Sopenharmony_ciWill be `true` if this `Http2Session` instance has been closed, otherwise 4181cb0ef41Sopenharmony_ci`false`. 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci#### `http2session.connecting` 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci<!-- YAML 4231cb0ef41Sopenharmony_ciadded: v10.0.0 4241cb0ef41Sopenharmony_ci--> 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci* {boolean} 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ciWill be `true` if this `Http2Session` instance is still connecting, will be set 4291cb0ef41Sopenharmony_cito `false` before emitting `connect` event and/or calling the `http2.connect` 4301cb0ef41Sopenharmony_cicallback. 4311cb0ef41Sopenharmony_ci 4321cb0ef41Sopenharmony_ci#### `http2session.destroy([error][, code])` 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ci<!-- YAML 4351cb0ef41Sopenharmony_ciadded: v8.4.0 4361cb0ef41Sopenharmony_ci--> 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ci* `error` {Error} An `Error` object if the `Http2Session` is being destroyed 4391cb0ef41Sopenharmony_ci due to an error. 4401cb0ef41Sopenharmony_ci* `code` {number} The HTTP/2 error code to send in the final `GOAWAY` frame. 4411cb0ef41Sopenharmony_ci If unspecified, and `error` is not undefined, the default is `INTERNAL_ERROR`, 4421cb0ef41Sopenharmony_ci otherwise defaults to `NO_ERROR`. 4431cb0ef41Sopenharmony_ci 4441cb0ef41Sopenharmony_ciImmediately terminates the `Http2Session` and the associated `net.Socket` or 4451cb0ef41Sopenharmony_ci`tls.TLSSocket`. 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ciOnce destroyed, the `Http2Session` will emit the `'close'` event. If `error` 4481cb0ef41Sopenharmony_ciis not undefined, an `'error'` event will be emitted immediately before the 4491cb0ef41Sopenharmony_ci`'close'` event. 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ciIf there are any remaining open `Http2Streams` associated with the 4521cb0ef41Sopenharmony_ci`Http2Session`, those will also be destroyed. 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci#### `http2session.destroyed` 4551cb0ef41Sopenharmony_ci 4561cb0ef41Sopenharmony_ci<!-- YAML 4571cb0ef41Sopenharmony_ciadded: v8.4.0 4581cb0ef41Sopenharmony_ci--> 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci* {boolean} 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ciWill be `true` if this `Http2Session` instance has been destroyed and must no 4631cb0ef41Sopenharmony_cilonger be used, otherwise `false`. 4641cb0ef41Sopenharmony_ci 4651cb0ef41Sopenharmony_ci#### `http2session.encrypted` 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ci<!-- YAML 4681cb0ef41Sopenharmony_ciadded: v9.4.0 4691cb0ef41Sopenharmony_ci--> 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_ci* {boolean|undefined} 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ciValue is `undefined` if the `Http2Session` session socket has not yet been 4741cb0ef41Sopenharmony_ciconnected, `true` if the `Http2Session` is connected with a `TLSSocket`, 4751cb0ef41Sopenharmony_ciand `false` if the `Http2Session` is connected to any other kind of socket 4761cb0ef41Sopenharmony_cior stream. 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci#### `http2session.goaway([code[, lastStreamID[, opaqueData]]])` 4791cb0ef41Sopenharmony_ci 4801cb0ef41Sopenharmony_ci<!-- YAML 4811cb0ef41Sopenharmony_ciadded: v9.4.0 4821cb0ef41Sopenharmony_ci--> 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ci* `code` {number} An HTTP/2 error code 4851cb0ef41Sopenharmony_ci* `lastStreamID` {number} The numeric ID of the last processed `Http2Stream` 4861cb0ef41Sopenharmony_ci* `opaqueData` {Buffer|TypedArray|DataView} A `TypedArray` or `DataView` 4871cb0ef41Sopenharmony_ci instance containing additional data to be carried within the `GOAWAY` frame. 4881cb0ef41Sopenharmony_ci 4891cb0ef41Sopenharmony_ciTransmits a `GOAWAY` frame to the connected peer _without_ shutting down the 4901cb0ef41Sopenharmony_ci`Http2Session`. 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci#### `http2session.localSettings` 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci<!-- YAML 4951cb0ef41Sopenharmony_ciadded: v8.4.0 4961cb0ef41Sopenharmony_ci--> 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci* {HTTP/2 Settings Object} 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ciA prototype-less object describing the current local settings of this 5011cb0ef41Sopenharmony_ci`Http2Session`. The local settings are local to _this_ `Http2Session` instance. 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci#### `http2session.originSet` 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci<!-- YAML 5061cb0ef41Sopenharmony_ciadded: v9.4.0 5071cb0ef41Sopenharmony_ci--> 5081cb0ef41Sopenharmony_ci 5091cb0ef41Sopenharmony_ci* {string\[]|undefined} 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ciIf the `Http2Session` is connected to a `TLSSocket`, the `originSet` property 5121cb0ef41Sopenharmony_ciwill return an `Array` of origins for which the `Http2Session` may be 5131cb0ef41Sopenharmony_ciconsidered authoritative. 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ciThe `originSet` property is only available when using a secure TLS connection. 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ci#### `http2session.pendingSettingsAck` 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci<!-- YAML 5201cb0ef41Sopenharmony_ciadded: v8.4.0 5211cb0ef41Sopenharmony_ci--> 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci* {boolean} 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ciIndicates whether the `Http2Session` is currently waiting for acknowledgment of 5261cb0ef41Sopenharmony_cia sent `SETTINGS` frame. Will be `true` after calling the 5271cb0ef41Sopenharmony_ci`http2session.settings()` method. Will be `false` once all sent `SETTINGS` 5281cb0ef41Sopenharmony_ciframes have been acknowledged. 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci#### `http2session.ping([payload, ]callback)` 5311cb0ef41Sopenharmony_ci 5321cb0ef41Sopenharmony_ci<!-- YAML 5331cb0ef41Sopenharmony_ciadded: v8.9.3 5341cb0ef41Sopenharmony_cichanges: 5351cb0ef41Sopenharmony_ci - version: v18.0.0 5361cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 5371cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 5381cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 5391cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 5401cb0ef41Sopenharmony_ci--> 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci* `payload` {Buffer|TypedArray|DataView} Optional ping payload. 5431cb0ef41Sopenharmony_ci* `callback` {Function} 5441cb0ef41Sopenharmony_ci* Returns: {boolean} 5451cb0ef41Sopenharmony_ci 5461cb0ef41Sopenharmony_ciSends a `PING` frame to the connected HTTP/2 peer. A `callback` function must 5471cb0ef41Sopenharmony_cibe provided. The method will return `true` if the `PING` was sent, `false` 5481cb0ef41Sopenharmony_ciotherwise. 5491cb0ef41Sopenharmony_ci 5501cb0ef41Sopenharmony_ciThe maximum number of outstanding (unacknowledged) pings is determined by the 5511cb0ef41Sopenharmony_ci`maxOutstandingPings` configuration option. The default maximum is 10. 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ciIf provided, the `payload` must be a `Buffer`, `TypedArray`, or `DataView` 5541cb0ef41Sopenharmony_cicontaining 8 bytes of data that will be transmitted with the `PING` and 5551cb0ef41Sopenharmony_cireturned with the ping acknowledgment. 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ciThe callback will be invoked with three arguments: an error argument that will 5581cb0ef41Sopenharmony_cibe `null` if the `PING` was successfully acknowledged, a `duration` argument 5591cb0ef41Sopenharmony_cithat reports the number of milliseconds elapsed since the ping was sent and the 5601cb0ef41Sopenharmony_ciacknowledgment was received, and a `Buffer` containing the 8-byte `PING` 5611cb0ef41Sopenharmony_cipayload. 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci```js 5641cb0ef41Sopenharmony_cisession.ping(Buffer.from('abcdefgh'), (err, duration, payload) => { 5651cb0ef41Sopenharmony_ci if (!err) { 5661cb0ef41Sopenharmony_ci console.log(`Ping acknowledged in ${duration} milliseconds`); 5671cb0ef41Sopenharmony_ci console.log(`With payload '${payload.toString()}'`); 5681cb0ef41Sopenharmony_ci } 5691cb0ef41Sopenharmony_ci}); 5701cb0ef41Sopenharmony_ci``` 5711cb0ef41Sopenharmony_ci 5721cb0ef41Sopenharmony_ciIf the `payload` argument is not specified, the default payload will be the 5731cb0ef41Sopenharmony_ci64-bit timestamp (little endian) marking the start of the `PING` duration. 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ci#### `http2session.ref()` 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci<!-- YAML 5781cb0ef41Sopenharmony_ciadded: v9.4.0 5791cb0ef41Sopenharmony_ci--> 5801cb0ef41Sopenharmony_ci 5811cb0ef41Sopenharmony_ciCalls [`ref()`][`net.Socket.prototype.ref()`] on this `Http2Session` 5821cb0ef41Sopenharmony_ciinstance's underlying [`net.Socket`][]. 5831cb0ef41Sopenharmony_ci 5841cb0ef41Sopenharmony_ci#### `http2session.remoteSettings` 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci<!-- YAML 5871cb0ef41Sopenharmony_ciadded: v8.4.0 5881cb0ef41Sopenharmony_ci--> 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ci* {HTTP/2 Settings Object} 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ciA prototype-less object describing the current remote settings of this 5931cb0ef41Sopenharmony_ci`Http2Session`. The remote settings are set by the _connected_ HTTP/2 peer. 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci#### `http2session.setLocalWindowSize(windowSize)` 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci<!-- YAML 5981cb0ef41Sopenharmony_ciadded: 5991cb0ef41Sopenharmony_ci - v15.3.0 6001cb0ef41Sopenharmony_ci - v14.18.0 6011cb0ef41Sopenharmony_ci--> 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci* `windowSize` {number} 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ciSets the local endpoint's window size. 6061cb0ef41Sopenharmony_ciThe `windowSize` is the total window size to set, not 6071cb0ef41Sopenharmony_cithe delta. 6081cb0ef41Sopenharmony_ci 6091cb0ef41Sopenharmony_ci```js 6101cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ciconst server = http2.createServer(); 6131cb0ef41Sopenharmony_ciconst expectedWindowSize = 2 ** 20; 6141cb0ef41Sopenharmony_ciserver.on('connect', (session) => { 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci // Set local window size to be 2 ** 20 6171cb0ef41Sopenharmony_ci session.setLocalWindowSize(expectedWindowSize); 6181cb0ef41Sopenharmony_ci}); 6191cb0ef41Sopenharmony_ci``` 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci#### `http2session.setTimeout(msecs, callback)` 6221cb0ef41Sopenharmony_ci 6231cb0ef41Sopenharmony_ci<!-- YAML 6241cb0ef41Sopenharmony_ciadded: v8.4.0 6251cb0ef41Sopenharmony_cichanges: 6261cb0ef41Sopenharmony_ci - version: v18.0.0 6271cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 6281cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 6291cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 6301cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 6311cb0ef41Sopenharmony_ci--> 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci* `msecs` {number} 6341cb0ef41Sopenharmony_ci* `callback` {Function} 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ciUsed to set a callback function that is called when there is no activity on 6371cb0ef41Sopenharmony_cithe `Http2Session` after `msecs` milliseconds. The given `callback` is 6381cb0ef41Sopenharmony_ciregistered as a listener on the `'timeout'` event. 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_ci#### `http2session.socket` 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci<!-- YAML 6431cb0ef41Sopenharmony_ciadded: v8.4.0 6441cb0ef41Sopenharmony_ci--> 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ci* {net.Socket|tls.TLSSocket} 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ciReturns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but 6491cb0ef41Sopenharmony_cilimits available methods to ones safe to use with HTTP/2. 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ci`destroy`, `emit`, `end`, `pause`, `read`, `resume`, and `write` will throw 6521cb0ef41Sopenharmony_cian error with code `ERR_HTTP2_NO_SOCKET_MANIPULATION`. See 6531cb0ef41Sopenharmony_ci[`Http2Session` and Sockets][] for more information. 6541cb0ef41Sopenharmony_ci 6551cb0ef41Sopenharmony_ci`setTimeout` method will be called on this `Http2Session`. 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_ciAll other interactions will be routed directly to the socket. 6581cb0ef41Sopenharmony_ci 6591cb0ef41Sopenharmony_ci#### `http2session.state` 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci<!-- YAML 6621cb0ef41Sopenharmony_ciadded: v8.4.0 6631cb0ef41Sopenharmony_ci--> 6641cb0ef41Sopenharmony_ci 6651cb0ef41Sopenharmony_ciProvides miscellaneous information about the current state of the 6661cb0ef41Sopenharmony_ci`Http2Session`. 6671cb0ef41Sopenharmony_ci 6681cb0ef41Sopenharmony_ci* {Object} 6691cb0ef41Sopenharmony_ci * `effectiveLocalWindowSize` {number} The current local (receive) 6701cb0ef41Sopenharmony_ci flow control window size for the `Http2Session`. 6711cb0ef41Sopenharmony_ci * `effectiveRecvDataLength` {number} The current number of bytes 6721cb0ef41Sopenharmony_ci that have been received since the last flow control `WINDOW_UPDATE`. 6731cb0ef41Sopenharmony_ci * `nextStreamID` {number} The numeric identifier to be used the 6741cb0ef41Sopenharmony_ci next time a new `Http2Stream` is created by this `Http2Session`. 6751cb0ef41Sopenharmony_ci * `localWindowSize` {number} The number of bytes that the remote peer can 6761cb0ef41Sopenharmony_ci send without receiving a `WINDOW_UPDATE`. 6771cb0ef41Sopenharmony_ci * `lastProcStreamID` {number} The numeric id of the `Http2Stream` 6781cb0ef41Sopenharmony_ci for which a `HEADERS` or `DATA` frame was most recently received. 6791cb0ef41Sopenharmony_ci * `remoteWindowSize` {number} The number of bytes that this `Http2Session` 6801cb0ef41Sopenharmony_ci may send without receiving a `WINDOW_UPDATE`. 6811cb0ef41Sopenharmony_ci * `outboundQueueSize` {number} The number of frames currently within the 6821cb0ef41Sopenharmony_ci outbound queue for this `Http2Session`. 6831cb0ef41Sopenharmony_ci * `deflateDynamicTableSize` {number} The current size in bytes of the 6841cb0ef41Sopenharmony_ci outbound header compression state table. 6851cb0ef41Sopenharmony_ci * `inflateDynamicTableSize` {number} The current size in bytes of the 6861cb0ef41Sopenharmony_ci inbound header compression state table. 6871cb0ef41Sopenharmony_ci 6881cb0ef41Sopenharmony_ciAn object describing the current status of this `Http2Session`. 6891cb0ef41Sopenharmony_ci 6901cb0ef41Sopenharmony_ci#### `http2session.settings([settings][, callback])` 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci<!-- YAML 6931cb0ef41Sopenharmony_ciadded: v8.4.0 6941cb0ef41Sopenharmony_cichanges: 6951cb0ef41Sopenharmony_ci - version: v18.0.0 6961cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 6971cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 6981cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 6991cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 7001cb0ef41Sopenharmony_ci--> 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} 7031cb0ef41Sopenharmony_ci* `callback` {Function} Callback that is called once the session is connected or 7041cb0ef41Sopenharmony_ci right away if the session is already connected. 7051cb0ef41Sopenharmony_ci * `err` {Error|null} 7061cb0ef41Sopenharmony_ci * `settings` {HTTP/2 Settings Object} The updated `settings` object. 7071cb0ef41Sopenharmony_ci * `duration` {integer} 7081cb0ef41Sopenharmony_ci 7091cb0ef41Sopenharmony_ciUpdates the current local settings for this `Http2Session` and sends a new 7101cb0ef41Sopenharmony_ci`SETTINGS` frame to the connected HTTP/2 peer. 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ciOnce called, the `http2session.pendingSettingsAck` property will be `true` 7131cb0ef41Sopenharmony_ciwhile the session is waiting for the remote peer to acknowledge the new 7141cb0ef41Sopenharmony_cisettings. 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ciThe new settings will not become effective until the `SETTINGS` acknowledgment 7171cb0ef41Sopenharmony_ciis received and the `'localSettings'` event is emitted. It is possible to send 7181cb0ef41Sopenharmony_cimultiple `SETTINGS` frames while acknowledgment is still pending. 7191cb0ef41Sopenharmony_ci 7201cb0ef41Sopenharmony_ci#### `http2session.type` 7211cb0ef41Sopenharmony_ci 7221cb0ef41Sopenharmony_ci<!-- YAML 7231cb0ef41Sopenharmony_ciadded: v8.4.0 7241cb0ef41Sopenharmony_ci--> 7251cb0ef41Sopenharmony_ci 7261cb0ef41Sopenharmony_ci* {number} 7271cb0ef41Sopenharmony_ci 7281cb0ef41Sopenharmony_ciThe `http2session.type` will be equal to 7291cb0ef41Sopenharmony_ci`http2.constants.NGHTTP2_SESSION_SERVER` if this `Http2Session` instance is a 7301cb0ef41Sopenharmony_ciserver, and `http2.constants.NGHTTP2_SESSION_CLIENT` if the instance is a 7311cb0ef41Sopenharmony_ciclient. 7321cb0ef41Sopenharmony_ci 7331cb0ef41Sopenharmony_ci#### `http2session.unref()` 7341cb0ef41Sopenharmony_ci 7351cb0ef41Sopenharmony_ci<!-- YAML 7361cb0ef41Sopenharmony_ciadded: v9.4.0 7371cb0ef41Sopenharmony_ci--> 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ciCalls [`unref()`][`net.Socket.prototype.unref()`] on this `Http2Session` 7401cb0ef41Sopenharmony_ciinstance's underlying [`net.Socket`][]. 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ci### Class: `ServerHttp2Session` 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci<!-- YAML 7451cb0ef41Sopenharmony_ciadded: v8.4.0 7461cb0ef41Sopenharmony_ci--> 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci* Extends: {Http2Session} 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ci#### `serverhttp2session.altsvc(alt, originOrStream)` 7511cb0ef41Sopenharmony_ci 7521cb0ef41Sopenharmony_ci<!-- YAML 7531cb0ef41Sopenharmony_ciadded: v9.4.0 7541cb0ef41Sopenharmony_ci--> 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci* `alt` {string} A description of the alternative service configuration as 7571cb0ef41Sopenharmony_ci defined by [RFC 7838][]. 7581cb0ef41Sopenharmony_ci* `originOrStream` {number|string|URL|Object} Either a URL string specifying 7591cb0ef41Sopenharmony_ci the origin (or an `Object` with an `origin` property) or the numeric 7601cb0ef41Sopenharmony_ci identifier of an active `Http2Stream` as given by the `http2stream.id` 7611cb0ef41Sopenharmony_ci property. 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ciSubmits an `ALTSVC` frame (as defined by [RFC 7838][]) to the connected client. 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ci```js 7661cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 7671cb0ef41Sopenharmony_ci 7681cb0ef41Sopenharmony_ciconst server = http2.createServer(); 7691cb0ef41Sopenharmony_ciserver.on('session', (session) => { 7701cb0ef41Sopenharmony_ci // Set altsvc for origin https://example.org:80 7711cb0ef41Sopenharmony_ci session.altsvc('h2=":8000"', 'https://example.org:80'); 7721cb0ef41Sopenharmony_ci}); 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 7751cb0ef41Sopenharmony_ci // Set altsvc for a specific stream 7761cb0ef41Sopenharmony_ci stream.session.altsvc('h2=":8000"', stream.id); 7771cb0ef41Sopenharmony_ci}); 7781cb0ef41Sopenharmony_ci``` 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ciSending an `ALTSVC` frame with a specific stream ID indicates that the alternate 7811cb0ef41Sopenharmony_ciservice is associated with the origin of the given `Http2Stream`. 7821cb0ef41Sopenharmony_ci 7831cb0ef41Sopenharmony_ciThe `alt` and origin string _must_ contain only ASCII bytes and are 7841cb0ef41Sopenharmony_cistrictly interpreted as a sequence of ASCII bytes. The special value `'clear'` 7851cb0ef41Sopenharmony_cimay be passed to clear any previously set alternative service for a given 7861cb0ef41Sopenharmony_cidomain. 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ciWhen a string is passed for the `originOrStream` argument, it will be parsed as 7891cb0ef41Sopenharmony_cia URL and the origin will be derived. For instance, the origin for the 7901cb0ef41Sopenharmony_ciHTTP URL `'https://example.org/foo/bar'` is the ASCII string 7911cb0ef41Sopenharmony_ci`'https://example.org'`. An error will be thrown if either the given string 7921cb0ef41Sopenharmony_cicannot be parsed as a URL or if a valid origin cannot be derived. 7931cb0ef41Sopenharmony_ci 7941cb0ef41Sopenharmony_ciA `URL` object, or any object with an `origin` property, may be passed as 7951cb0ef41Sopenharmony_ci`originOrStream`, in which case the value of the `origin` property will be 7961cb0ef41Sopenharmony_ciused. The value of the `origin` property _must_ be a properly serialized 7971cb0ef41Sopenharmony_ciASCII origin. 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ci#### Specifying alternative services 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ciThe format of the `alt` parameter is strictly defined by [RFC 7838][] as an 8021cb0ef41Sopenharmony_ciASCII string containing a comma-delimited list of "alternative" protocols 8031cb0ef41Sopenharmony_ciassociated with a specific host and port. 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ciFor example, the value `'h2="example.org:81"'` indicates that the HTTP/2 8061cb0ef41Sopenharmony_ciprotocol is available on the host `'example.org'` on TCP/IP port 81. The 8071cb0ef41Sopenharmony_cihost and port _must_ be contained within the quote (`"`) characters. 8081cb0ef41Sopenharmony_ci 8091cb0ef41Sopenharmony_ciMultiple alternatives may be specified, for instance: `'h2="example.org:81", 8101cb0ef41Sopenharmony_cih2=":82"'`. 8111cb0ef41Sopenharmony_ci 8121cb0ef41Sopenharmony_ciThe protocol identifier (`'h2'` in the examples) may be any valid 8131cb0ef41Sopenharmony_ci[ALPN Protocol ID][]. 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ciThe syntax of these values is not validated by the Node.js implementation and 8161cb0ef41Sopenharmony_ciare passed through as provided by the user or received from the peer. 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ci#### `serverhttp2session.origin(...origins)` 8191cb0ef41Sopenharmony_ci 8201cb0ef41Sopenharmony_ci<!-- YAML 8211cb0ef41Sopenharmony_ciadded: v10.12.0 8221cb0ef41Sopenharmony_ci--> 8231cb0ef41Sopenharmony_ci 8241cb0ef41Sopenharmony_ci* `origins` { string | URL | Object } One or more URL Strings passed as 8251cb0ef41Sopenharmony_ci separate arguments. 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ciSubmits an `ORIGIN` frame (as defined by [RFC 8336][]) to the connected client 8281cb0ef41Sopenharmony_cito advertise the set of origins for which the server is capable of providing 8291cb0ef41Sopenharmony_ciauthoritative responses. 8301cb0ef41Sopenharmony_ci 8311cb0ef41Sopenharmony_ci```js 8321cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 8331cb0ef41Sopenharmony_ciconst options = getSecureOptionsSomehow(); 8341cb0ef41Sopenharmony_ciconst server = http2.createSecureServer(options); 8351cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 8361cb0ef41Sopenharmony_ci stream.respond(); 8371cb0ef41Sopenharmony_ci stream.end('ok'); 8381cb0ef41Sopenharmony_ci}); 8391cb0ef41Sopenharmony_ciserver.on('session', (session) => { 8401cb0ef41Sopenharmony_ci session.origin('https://example.com', 'https://example.org'); 8411cb0ef41Sopenharmony_ci}); 8421cb0ef41Sopenharmony_ci``` 8431cb0ef41Sopenharmony_ci 8441cb0ef41Sopenharmony_ciWhen a string is passed as an `origin`, it will be parsed as a URL and the 8451cb0ef41Sopenharmony_ciorigin will be derived. For instance, the origin for the HTTP URL 8461cb0ef41Sopenharmony_ci`'https://example.org/foo/bar'` is the ASCII string 8471cb0ef41Sopenharmony_ci`'https://example.org'`. An error will be thrown if either the given string 8481cb0ef41Sopenharmony_cicannot be parsed as a URL or if a valid origin cannot be derived. 8491cb0ef41Sopenharmony_ci 8501cb0ef41Sopenharmony_ciA `URL` object, or any object with an `origin` property, may be passed as 8511cb0ef41Sopenharmony_cian `origin`, in which case the value of the `origin` property will be 8521cb0ef41Sopenharmony_ciused. The value of the `origin` property _must_ be a properly serialized 8531cb0ef41Sopenharmony_ciASCII origin. 8541cb0ef41Sopenharmony_ci 8551cb0ef41Sopenharmony_ciAlternatively, the `origins` option may be used when creating a new HTTP/2 8561cb0ef41Sopenharmony_ciserver using the `http2.createSecureServer()` method: 8571cb0ef41Sopenharmony_ci 8581cb0ef41Sopenharmony_ci```js 8591cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 8601cb0ef41Sopenharmony_ciconst options = getSecureOptionsSomehow(); 8611cb0ef41Sopenharmony_cioptions.origins = ['https://example.com', 'https://example.org']; 8621cb0ef41Sopenharmony_ciconst server = http2.createSecureServer(options); 8631cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 8641cb0ef41Sopenharmony_ci stream.respond(); 8651cb0ef41Sopenharmony_ci stream.end('ok'); 8661cb0ef41Sopenharmony_ci}); 8671cb0ef41Sopenharmony_ci``` 8681cb0ef41Sopenharmony_ci 8691cb0ef41Sopenharmony_ci### Class: `ClientHttp2Session` 8701cb0ef41Sopenharmony_ci 8711cb0ef41Sopenharmony_ci<!-- YAML 8721cb0ef41Sopenharmony_ciadded: v8.4.0 8731cb0ef41Sopenharmony_ci--> 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ci* Extends: {Http2Session} 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ci#### Event: `'altsvc'` 8781cb0ef41Sopenharmony_ci 8791cb0ef41Sopenharmony_ci<!-- YAML 8801cb0ef41Sopenharmony_ciadded: v9.4.0 8811cb0ef41Sopenharmony_ci--> 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci* `alt` {string} 8841cb0ef41Sopenharmony_ci* `origin` {string} 8851cb0ef41Sopenharmony_ci* `streamId` {number} 8861cb0ef41Sopenharmony_ci 8871cb0ef41Sopenharmony_ciThe `'altsvc'` event is emitted whenever an `ALTSVC` frame is received by 8881cb0ef41Sopenharmony_cithe client. The event is emitted with the `ALTSVC` value, origin, and stream 8891cb0ef41Sopenharmony_ciID. If no `origin` is provided in the `ALTSVC` frame, `origin` will 8901cb0ef41Sopenharmony_cibe an empty string. 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ci```js 8931cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 8941cb0ef41Sopenharmony_ciconst client = http2.connect('https://example.org'); 8951cb0ef41Sopenharmony_ci 8961cb0ef41Sopenharmony_ciclient.on('altsvc', (alt, origin, streamId) => { 8971cb0ef41Sopenharmony_ci console.log(alt); 8981cb0ef41Sopenharmony_ci console.log(origin); 8991cb0ef41Sopenharmony_ci console.log(streamId); 9001cb0ef41Sopenharmony_ci}); 9011cb0ef41Sopenharmony_ci``` 9021cb0ef41Sopenharmony_ci 9031cb0ef41Sopenharmony_ci#### Event: `'origin'` 9041cb0ef41Sopenharmony_ci 9051cb0ef41Sopenharmony_ci<!-- YAML 9061cb0ef41Sopenharmony_ciadded: v10.12.0 9071cb0ef41Sopenharmony_ci--> 9081cb0ef41Sopenharmony_ci 9091cb0ef41Sopenharmony_ci* `origins` {string\[]} 9101cb0ef41Sopenharmony_ci 9111cb0ef41Sopenharmony_ciThe `'origin'` event is emitted whenever an `ORIGIN` frame is received by 9121cb0ef41Sopenharmony_cithe client. The event is emitted with an array of `origin` strings. The 9131cb0ef41Sopenharmony_ci`http2session.originSet` will be updated to include the received 9141cb0ef41Sopenharmony_ciorigins. 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci```js 9171cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 9181cb0ef41Sopenharmony_ciconst client = http2.connect('https://example.org'); 9191cb0ef41Sopenharmony_ci 9201cb0ef41Sopenharmony_ciclient.on('origin', (origins) => { 9211cb0ef41Sopenharmony_ci for (let n = 0; n < origins.length; n++) 9221cb0ef41Sopenharmony_ci console.log(origins[n]); 9231cb0ef41Sopenharmony_ci}); 9241cb0ef41Sopenharmony_ci``` 9251cb0ef41Sopenharmony_ci 9261cb0ef41Sopenharmony_ciThe `'origin'` event is only emitted when using a secure TLS connection. 9271cb0ef41Sopenharmony_ci 9281cb0ef41Sopenharmony_ci#### `clienthttp2session.request(headers[, options])` 9291cb0ef41Sopenharmony_ci 9301cb0ef41Sopenharmony_ci<!-- YAML 9311cb0ef41Sopenharmony_ciadded: v8.4.0 9321cb0ef41Sopenharmony_ci--> 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 9351cb0ef41Sopenharmony_ci 9361cb0ef41Sopenharmony_ci* `options` {Object} 9371cb0ef41Sopenharmony_ci * `endStream` {boolean} `true` if the `Http2Stream` _writable_ side should 9381cb0ef41Sopenharmony_ci be closed initially, such as when sending a `GET` request that should not 9391cb0ef41Sopenharmony_ci expect a payload body. 9401cb0ef41Sopenharmony_ci * `exclusive` {boolean} When `true` and `parent` identifies a parent Stream, 9411cb0ef41Sopenharmony_ci the created stream is made the sole direct dependency of the parent, with 9421cb0ef41Sopenharmony_ci all other existing dependents made a dependent of the newly created stream. 9431cb0ef41Sopenharmony_ci **Default:** `false`. 9441cb0ef41Sopenharmony_ci * `parent` {number} Specifies the numeric identifier of a stream the newly 9451cb0ef41Sopenharmony_ci created stream is dependent on. 9461cb0ef41Sopenharmony_ci * `weight` {number} Specifies the relative dependency of a stream in relation 9471cb0ef41Sopenharmony_ci to other streams with the same `parent`. The value is a number between `1` 9481cb0ef41Sopenharmony_ci and `256` (inclusive). 9491cb0ef41Sopenharmony_ci * `waitForTrailers` {boolean} When `true`, the `Http2Stream` will emit the 9501cb0ef41Sopenharmony_ci `'wantTrailers'` event after the final `DATA` frame has been sent. 9511cb0ef41Sopenharmony_ci * `signal` {AbortSignal} An AbortSignal that may be used to abort an ongoing 9521cb0ef41Sopenharmony_ci request. 9531cb0ef41Sopenharmony_ci 9541cb0ef41Sopenharmony_ci* Returns: {ClientHttp2Stream} 9551cb0ef41Sopenharmony_ci 9561cb0ef41Sopenharmony_ciFor HTTP/2 Client `Http2Session` instances only, the `http2session.request()` 9571cb0ef41Sopenharmony_cicreates and returns an `Http2Stream` instance that can be used to send an 9581cb0ef41Sopenharmony_ciHTTP/2 request to the connected server. 9591cb0ef41Sopenharmony_ci 9601cb0ef41Sopenharmony_ciWhen a `ClientHttp2Session` is first created, the socket may not yet be 9611cb0ef41Sopenharmony_ciconnected. if `clienthttp2session.request()` is called during this time, the 9621cb0ef41Sopenharmony_ciactual request will be deferred until the socket is ready to go. 9631cb0ef41Sopenharmony_ciIf the `session` is closed before the actual request be executed, an 9641cb0ef41Sopenharmony_ci`ERR_HTTP2_GOAWAY_SESSION` is thrown. 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ciThis method is only available if `http2session.type` is equal to 9671cb0ef41Sopenharmony_ci`http2.constants.NGHTTP2_SESSION_CLIENT`. 9681cb0ef41Sopenharmony_ci 9691cb0ef41Sopenharmony_ci```js 9701cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 9711cb0ef41Sopenharmony_ciconst clientSession = http2.connect('https://localhost:1234'); 9721cb0ef41Sopenharmony_ciconst { 9731cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 9741cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS, 9751cb0ef41Sopenharmony_ci} = http2.constants; 9761cb0ef41Sopenharmony_ci 9771cb0ef41Sopenharmony_ciconst req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' }); 9781cb0ef41Sopenharmony_cireq.on('response', (headers) => { 9791cb0ef41Sopenharmony_ci console.log(headers[HTTP2_HEADER_STATUS]); 9801cb0ef41Sopenharmony_ci req.on('data', (chunk) => { /* .. */ }); 9811cb0ef41Sopenharmony_ci req.on('end', () => { /* .. */ }); 9821cb0ef41Sopenharmony_ci}); 9831cb0ef41Sopenharmony_ci``` 9841cb0ef41Sopenharmony_ci 9851cb0ef41Sopenharmony_ciWhen the `options.waitForTrailers` option is set, the `'wantTrailers'` event 9861cb0ef41Sopenharmony_ciis emitted immediately after queuing the last chunk of payload data to be sent. 9871cb0ef41Sopenharmony_ciThe `http2stream.sendTrailers()` method can then be called to send trailing 9881cb0ef41Sopenharmony_ciheaders to the peer. 9891cb0ef41Sopenharmony_ci 9901cb0ef41Sopenharmony_ciWhen `options.waitForTrailers` is set, the `Http2Stream` will not automatically 9911cb0ef41Sopenharmony_ciclose when the final `DATA` frame is transmitted. User code must call either 9921cb0ef41Sopenharmony_ci`http2stream.sendTrailers()` or `http2stream.close()` to close the 9931cb0ef41Sopenharmony_ci`Http2Stream`. 9941cb0ef41Sopenharmony_ci 9951cb0ef41Sopenharmony_ciWhen `options.signal` is set with an `AbortSignal` and then `abort` on the 9961cb0ef41Sopenharmony_cicorresponding `AbortController` is called, the request will emit an `'error'` 9971cb0ef41Sopenharmony_cievent with an `AbortError` error. 9981cb0ef41Sopenharmony_ci 9991cb0ef41Sopenharmony_ciThe `:method` and `:path` pseudo-headers are not specified within `headers`, 10001cb0ef41Sopenharmony_cithey respectively default to: 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci* `:method` = `'GET'` 10031cb0ef41Sopenharmony_ci* `:path` = `/` 10041cb0ef41Sopenharmony_ci 10051cb0ef41Sopenharmony_ci### Class: `Http2Stream` 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_ci<!-- YAML 10081cb0ef41Sopenharmony_ciadded: v8.4.0 10091cb0ef41Sopenharmony_ci--> 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ci* Extends: {stream.Duplex} 10121cb0ef41Sopenharmony_ci 10131cb0ef41Sopenharmony_ciEach instance of the `Http2Stream` class represents a bidirectional HTTP/2 10141cb0ef41Sopenharmony_cicommunications stream over an `Http2Session` instance. Any single `Http2Session` 10151cb0ef41Sopenharmony_cimay have up to 2<sup>31</sup>-1 `Http2Stream` instances over its lifetime. 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ciUser code will not construct `Http2Stream` instances directly. Rather, these 10181cb0ef41Sopenharmony_ciare created, managed, and provided to user code through the `Http2Session` 10191cb0ef41Sopenharmony_ciinstance. On the server, `Http2Stream` instances are created either in response 10201cb0ef41Sopenharmony_cito an incoming HTTP request (and handed off to user code via the `'stream'` 10211cb0ef41Sopenharmony_cievent), or in response to a call to the `http2stream.pushStream()` method. 10221cb0ef41Sopenharmony_ciOn the client, `Http2Stream` instances are created and returned when either the 10231cb0ef41Sopenharmony_ci`http2session.request()` method is called, or in response to an incoming 10241cb0ef41Sopenharmony_ci`'push'` event. 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ciThe `Http2Stream` class is a base for the [`ServerHttp2Stream`][] and 10271cb0ef41Sopenharmony_ci[`ClientHttp2Stream`][] classes, each of which is used specifically by either 10281cb0ef41Sopenharmony_cithe Server or Client side, respectively. 10291cb0ef41Sopenharmony_ci 10301cb0ef41Sopenharmony_ciAll `Http2Stream` instances are [`Duplex`][] streams. The `Writable` side of the 10311cb0ef41Sopenharmony_ci`Duplex` is used to send data to the connected peer, while the `Readable` side 10321cb0ef41Sopenharmony_ciis used to receive data sent by the connected peer. 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ciThe default text character encoding for an `Http2Stream` is UTF-8. When using an 10351cb0ef41Sopenharmony_ci`Http2Stream` to send text, use the `'content-type'` header to set the character 10361cb0ef41Sopenharmony_ciencoding. 10371cb0ef41Sopenharmony_ci 10381cb0ef41Sopenharmony_ci```js 10391cb0ef41Sopenharmony_cistream.respond({ 10401cb0ef41Sopenharmony_ci 'content-type': 'text/html; charset=utf-8', 10411cb0ef41Sopenharmony_ci ':status': 200, 10421cb0ef41Sopenharmony_ci}); 10431cb0ef41Sopenharmony_ci``` 10441cb0ef41Sopenharmony_ci 10451cb0ef41Sopenharmony_ci#### `Http2Stream` Lifecycle 10461cb0ef41Sopenharmony_ci 10471cb0ef41Sopenharmony_ci##### Creation 10481cb0ef41Sopenharmony_ci 10491cb0ef41Sopenharmony_ciOn the server side, instances of [`ServerHttp2Stream`][] are created either 10501cb0ef41Sopenharmony_ciwhen: 10511cb0ef41Sopenharmony_ci 10521cb0ef41Sopenharmony_ci* A new HTTP/2 `HEADERS` frame with a previously unused stream ID is received; 10531cb0ef41Sopenharmony_ci* The `http2stream.pushStream()` method is called. 10541cb0ef41Sopenharmony_ci 10551cb0ef41Sopenharmony_ciOn the client side, instances of [`ClientHttp2Stream`][] are created when the 10561cb0ef41Sopenharmony_ci`http2session.request()` method is called. 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ciOn the client, the `Http2Stream` instance returned by `http2session.request()` 10591cb0ef41Sopenharmony_cimay not be immediately ready for use if the parent `Http2Session` has not yet 10601cb0ef41Sopenharmony_cibeen fully established. In such cases, operations called on the `Http2Stream` 10611cb0ef41Sopenharmony_ciwill be buffered until the `'ready'` event is emitted. User code should rarely, 10621cb0ef41Sopenharmony_ciif ever, need to handle the `'ready'` event directly. The ready status of an 10631cb0ef41Sopenharmony_ci`Http2Stream` can be determined by checking the value of `http2stream.id`. If 10641cb0ef41Sopenharmony_cithe value is `undefined`, the stream is not yet ready for use. 10651cb0ef41Sopenharmony_ci 10661cb0ef41Sopenharmony_ci##### Destruction 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ciAll [`Http2Stream`][] instances are destroyed either when: 10691cb0ef41Sopenharmony_ci 10701cb0ef41Sopenharmony_ci* An `RST_STREAM` frame for the stream is received by the connected peer, 10711cb0ef41Sopenharmony_ci and (for client streams only) pending data has been read. 10721cb0ef41Sopenharmony_ci* The `http2stream.close()` method is called, and (for client streams only) 10731cb0ef41Sopenharmony_ci pending data has been read. 10741cb0ef41Sopenharmony_ci* The `http2stream.destroy()` or `http2session.destroy()` methods are called. 10751cb0ef41Sopenharmony_ci 10761cb0ef41Sopenharmony_ciWhen an `Http2Stream` instance is destroyed, an attempt will be made to send an 10771cb0ef41Sopenharmony_ci`RST_STREAM` frame to the connected peer. 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ciWhen the `Http2Stream` instance is destroyed, the `'close'` event will 10801cb0ef41Sopenharmony_cibe emitted. Because `Http2Stream` is an instance of `stream.Duplex`, the 10811cb0ef41Sopenharmony_ci`'end'` event will also be emitted if the stream data is currently flowing. 10821cb0ef41Sopenharmony_ciThe `'error'` event may also be emitted if `http2stream.destroy()` was called 10831cb0ef41Sopenharmony_ciwith an `Error` passed as the first argument. 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ciAfter the `Http2Stream` has been destroyed, the `http2stream.destroyed` 10861cb0ef41Sopenharmony_ciproperty will be `true` and the `http2stream.rstCode` property will specify the 10871cb0ef41Sopenharmony_ci`RST_STREAM` error code. The `Http2Stream` instance is no longer usable once 10881cb0ef41Sopenharmony_cidestroyed. 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ci#### Event: `'aborted'` 10911cb0ef41Sopenharmony_ci 10921cb0ef41Sopenharmony_ci<!-- YAML 10931cb0ef41Sopenharmony_ciadded: v8.4.0 10941cb0ef41Sopenharmony_ci--> 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_ciThe `'aborted'` event is emitted whenever a `Http2Stream` instance is 10971cb0ef41Sopenharmony_ciabnormally aborted in mid-communication. 10981cb0ef41Sopenharmony_ciIts listener does not expect any arguments. 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_ciThe `'aborted'` event will only be emitted if the `Http2Stream` writable side 11011cb0ef41Sopenharmony_cihas not been ended. 11021cb0ef41Sopenharmony_ci 11031cb0ef41Sopenharmony_ci#### Event: `'close'` 11041cb0ef41Sopenharmony_ci 11051cb0ef41Sopenharmony_ci<!-- YAML 11061cb0ef41Sopenharmony_ciadded: v8.4.0 11071cb0ef41Sopenharmony_ci--> 11081cb0ef41Sopenharmony_ci 11091cb0ef41Sopenharmony_ciThe `'close'` event is emitted when the `Http2Stream` is destroyed. Once 11101cb0ef41Sopenharmony_cithis event is emitted, the `Http2Stream` instance is no longer usable. 11111cb0ef41Sopenharmony_ci 11121cb0ef41Sopenharmony_ciThe HTTP/2 error code used when closing the stream can be retrieved using 11131cb0ef41Sopenharmony_cithe `http2stream.rstCode` property. If the code is any value other than 11141cb0ef41Sopenharmony_ci`NGHTTP2_NO_ERROR` (`0`), an `'error'` event will have also been emitted. 11151cb0ef41Sopenharmony_ci 11161cb0ef41Sopenharmony_ci#### Event: `'error'` 11171cb0ef41Sopenharmony_ci 11181cb0ef41Sopenharmony_ci<!-- YAML 11191cb0ef41Sopenharmony_ciadded: v8.4.0 11201cb0ef41Sopenharmony_ci--> 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ci* `error` {Error} 11231cb0ef41Sopenharmony_ci 11241cb0ef41Sopenharmony_ciThe `'error'` event is emitted when an error occurs during the processing of 11251cb0ef41Sopenharmony_cian `Http2Stream`. 11261cb0ef41Sopenharmony_ci 11271cb0ef41Sopenharmony_ci#### Event: `'frameError'` 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci<!-- YAML 11301cb0ef41Sopenharmony_ciadded: v8.4.0 11311cb0ef41Sopenharmony_ci--> 11321cb0ef41Sopenharmony_ci 11331cb0ef41Sopenharmony_ci* `type` {integer} The frame type. 11341cb0ef41Sopenharmony_ci* `code` {integer} The error code. 11351cb0ef41Sopenharmony_ci* `id` {integer} The stream id (or `0` if the frame isn't associated with a 11361cb0ef41Sopenharmony_ci stream). 11371cb0ef41Sopenharmony_ci 11381cb0ef41Sopenharmony_ciThe `'frameError'` event is emitted when an error occurs while attempting to 11391cb0ef41Sopenharmony_cisend a frame. When invoked, the handler function will receive an integer 11401cb0ef41Sopenharmony_ciargument identifying the frame type, and an integer argument identifying the 11411cb0ef41Sopenharmony_cierror code. The `Http2Stream` instance will be destroyed immediately after the 11421cb0ef41Sopenharmony_ci`'frameError'` event is emitted. 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ci#### Event: `'ready'` 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci<!-- YAML 11471cb0ef41Sopenharmony_ciadded: v8.4.0 11481cb0ef41Sopenharmony_ci--> 11491cb0ef41Sopenharmony_ci 11501cb0ef41Sopenharmony_ciThe `'ready'` event is emitted when the `Http2Stream` has been opened, has 11511cb0ef41Sopenharmony_cibeen assigned an `id`, and can be used. The listener does not expect any 11521cb0ef41Sopenharmony_ciarguments. 11531cb0ef41Sopenharmony_ci 11541cb0ef41Sopenharmony_ci#### Event: `'timeout'` 11551cb0ef41Sopenharmony_ci 11561cb0ef41Sopenharmony_ci<!-- YAML 11571cb0ef41Sopenharmony_ciadded: v8.4.0 11581cb0ef41Sopenharmony_ci--> 11591cb0ef41Sopenharmony_ci 11601cb0ef41Sopenharmony_ciThe `'timeout'` event is emitted after no activity is received for this 11611cb0ef41Sopenharmony_ci`Http2Stream` within the number of milliseconds set using 11621cb0ef41Sopenharmony_ci`http2stream.setTimeout()`. 11631cb0ef41Sopenharmony_ciIts listener does not expect any arguments. 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci#### Event: `'trailers'` 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_ci<!-- YAML 11681cb0ef41Sopenharmony_ciadded: v8.4.0 11691cb0ef41Sopenharmony_ci--> 11701cb0ef41Sopenharmony_ci 11711cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} An object describing the headers 11721cb0ef41Sopenharmony_ci* `flags` {number} The associated numeric flags 11731cb0ef41Sopenharmony_ci 11741cb0ef41Sopenharmony_ciThe `'trailers'` event is emitted when a block of headers associated with 11751cb0ef41Sopenharmony_citrailing header fields is received. The listener callback is passed the 11761cb0ef41Sopenharmony_ci[HTTP/2 Headers Object][] and flags associated with the headers. 11771cb0ef41Sopenharmony_ci 11781cb0ef41Sopenharmony_ciThis event might not be emitted if `http2stream.end()` is called 11791cb0ef41Sopenharmony_cibefore trailers are received and the incoming data is not being read or 11801cb0ef41Sopenharmony_cilistened for. 11811cb0ef41Sopenharmony_ci 11821cb0ef41Sopenharmony_ci```js 11831cb0ef41Sopenharmony_cistream.on('trailers', (headers, flags) => { 11841cb0ef41Sopenharmony_ci console.log(headers); 11851cb0ef41Sopenharmony_ci}); 11861cb0ef41Sopenharmony_ci``` 11871cb0ef41Sopenharmony_ci 11881cb0ef41Sopenharmony_ci#### Event: `'wantTrailers'` 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ci<!-- YAML 11911cb0ef41Sopenharmony_ciadded: v10.0.0 11921cb0ef41Sopenharmony_ci--> 11931cb0ef41Sopenharmony_ci 11941cb0ef41Sopenharmony_ciThe `'wantTrailers'` event is emitted when the `Http2Stream` has queued the 11951cb0ef41Sopenharmony_cifinal `DATA` frame to be sent on a frame and the `Http2Stream` is ready to send 11961cb0ef41Sopenharmony_citrailing headers. When initiating a request or response, the `waitForTrailers` 11971cb0ef41Sopenharmony_cioption must be set for this event to be emitted. 11981cb0ef41Sopenharmony_ci 11991cb0ef41Sopenharmony_ci#### `http2stream.aborted` 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ci<!-- YAML 12021cb0ef41Sopenharmony_ciadded: v8.4.0 12031cb0ef41Sopenharmony_ci--> 12041cb0ef41Sopenharmony_ci 12051cb0ef41Sopenharmony_ci* {boolean} 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ciSet to `true` if the `Http2Stream` instance was aborted abnormally. When set, 12081cb0ef41Sopenharmony_cithe `'aborted'` event will have been emitted. 12091cb0ef41Sopenharmony_ci 12101cb0ef41Sopenharmony_ci#### `http2stream.bufferSize` 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ci<!-- YAML 12131cb0ef41Sopenharmony_ciadded: 12141cb0ef41Sopenharmony_ci - v11.2.0 12151cb0ef41Sopenharmony_ci - v10.16.0 12161cb0ef41Sopenharmony_ci--> 12171cb0ef41Sopenharmony_ci 12181cb0ef41Sopenharmony_ci* {number} 12191cb0ef41Sopenharmony_ci 12201cb0ef41Sopenharmony_ciThis property shows the number of characters currently buffered to be written. 12211cb0ef41Sopenharmony_ciSee [`net.Socket.bufferSize`][] for details. 12221cb0ef41Sopenharmony_ci 12231cb0ef41Sopenharmony_ci#### `http2stream.close(code[, callback])` 12241cb0ef41Sopenharmony_ci 12251cb0ef41Sopenharmony_ci<!-- YAML 12261cb0ef41Sopenharmony_ciadded: v8.4.0 12271cb0ef41Sopenharmony_cichanges: 12281cb0ef41Sopenharmony_ci - version: v18.0.0 12291cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 12301cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 12311cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 12321cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 12331cb0ef41Sopenharmony_ci--> 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_ci* `code` {number} Unsigned 32-bit integer identifying the error code. 12361cb0ef41Sopenharmony_ci **Default:** `http2.constants.NGHTTP2_NO_ERROR` (`0x00`). 12371cb0ef41Sopenharmony_ci* `callback` {Function} An optional function registered to listen for the 12381cb0ef41Sopenharmony_ci `'close'` event. 12391cb0ef41Sopenharmony_ci 12401cb0ef41Sopenharmony_ciCloses the `Http2Stream` instance by sending an `RST_STREAM` frame to the 12411cb0ef41Sopenharmony_ciconnected HTTP/2 peer. 12421cb0ef41Sopenharmony_ci 12431cb0ef41Sopenharmony_ci#### `http2stream.closed` 12441cb0ef41Sopenharmony_ci 12451cb0ef41Sopenharmony_ci<!-- YAML 12461cb0ef41Sopenharmony_ciadded: v9.4.0 12471cb0ef41Sopenharmony_ci--> 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ci* {boolean} 12501cb0ef41Sopenharmony_ci 12511cb0ef41Sopenharmony_ciSet to `true` if the `Http2Stream` instance has been closed. 12521cb0ef41Sopenharmony_ci 12531cb0ef41Sopenharmony_ci#### `http2stream.destroyed` 12541cb0ef41Sopenharmony_ci 12551cb0ef41Sopenharmony_ci<!-- YAML 12561cb0ef41Sopenharmony_ciadded: v8.4.0 12571cb0ef41Sopenharmony_ci--> 12581cb0ef41Sopenharmony_ci 12591cb0ef41Sopenharmony_ci* {boolean} 12601cb0ef41Sopenharmony_ci 12611cb0ef41Sopenharmony_ciSet to `true` if the `Http2Stream` instance has been destroyed and is no longer 12621cb0ef41Sopenharmony_ciusable. 12631cb0ef41Sopenharmony_ci 12641cb0ef41Sopenharmony_ci#### `http2stream.endAfterHeaders` 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ci<!-- YAML 12671cb0ef41Sopenharmony_ciadded: v10.11.0 12681cb0ef41Sopenharmony_ci--> 12691cb0ef41Sopenharmony_ci 12701cb0ef41Sopenharmony_ci* {boolean} 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_ciSet to `true` if the `END_STREAM` flag was set in the request or response 12731cb0ef41Sopenharmony_ciHEADERS frame received, indicating that no additional data should be received 12741cb0ef41Sopenharmony_ciand the readable side of the `Http2Stream` will be closed. 12751cb0ef41Sopenharmony_ci 12761cb0ef41Sopenharmony_ci#### `http2stream.id` 12771cb0ef41Sopenharmony_ci 12781cb0ef41Sopenharmony_ci<!-- YAML 12791cb0ef41Sopenharmony_ciadded: v8.4.0 12801cb0ef41Sopenharmony_ci--> 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ci* {number|undefined} 12831cb0ef41Sopenharmony_ci 12841cb0ef41Sopenharmony_ciThe numeric stream identifier of this `Http2Stream` instance. Set to `undefined` 12851cb0ef41Sopenharmony_ciif the stream identifier has not yet been assigned. 12861cb0ef41Sopenharmony_ci 12871cb0ef41Sopenharmony_ci#### `http2stream.pending` 12881cb0ef41Sopenharmony_ci 12891cb0ef41Sopenharmony_ci<!-- YAML 12901cb0ef41Sopenharmony_ciadded: v9.4.0 12911cb0ef41Sopenharmony_ci--> 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci* {boolean} 12941cb0ef41Sopenharmony_ci 12951cb0ef41Sopenharmony_ciSet to `true` if the `Http2Stream` instance has not yet been assigned a 12961cb0ef41Sopenharmony_cinumeric stream identifier. 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ci#### `http2stream.priority(options)` 12991cb0ef41Sopenharmony_ci 13001cb0ef41Sopenharmony_ci<!-- YAML 13011cb0ef41Sopenharmony_ciadded: v8.4.0 13021cb0ef41Sopenharmony_ci--> 13031cb0ef41Sopenharmony_ci 13041cb0ef41Sopenharmony_ci* `options` {Object} 13051cb0ef41Sopenharmony_ci * `exclusive` {boolean} When `true` and `parent` identifies a parent Stream, 13061cb0ef41Sopenharmony_ci this stream is made the sole direct dependency of the parent, with 13071cb0ef41Sopenharmony_ci all other existing dependents made a dependent of this stream. **Default:** 13081cb0ef41Sopenharmony_ci `false`. 13091cb0ef41Sopenharmony_ci * `parent` {number} Specifies the numeric identifier of a stream this stream 13101cb0ef41Sopenharmony_ci is dependent on. 13111cb0ef41Sopenharmony_ci * `weight` {number} Specifies the relative dependency of a stream in relation 13121cb0ef41Sopenharmony_ci to other streams with the same `parent`. The value is a number between `1` 13131cb0ef41Sopenharmony_ci and `256` (inclusive). 13141cb0ef41Sopenharmony_ci * `silent` {boolean} When `true`, changes the priority locally without 13151cb0ef41Sopenharmony_ci sending a `PRIORITY` frame to the connected peer. 13161cb0ef41Sopenharmony_ci 13171cb0ef41Sopenharmony_ciUpdates the priority for this `Http2Stream` instance. 13181cb0ef41Sopenharmony_ci 13191cb0ef41Sopenharmony_ci#### `http2stream.rstCode` 13201cb0ef41Sopenharmony_ci 13211cb0ef41Sopenharmony_ci<!-- YAML 13221cb0ef41Sopenharmony_ciadded: v8.4.0 13231cb0ef41Sopenharmony_ci--> 13241cb0ef41Sopenharmony_ci 13251cb0ef41Sopenharmony_ci* {number} 13261cb0ef41Sopenharmony_ci 13271cb0ef41Sopenharmony_ciSet to the `RST_STREAM` [error code][] reported when the `Http2Stream` is 13281cb0ef41Sopenharmony_cidestroyed after either receiving an `RST_STREAM` frame from the connected peer, 13291cb0ef41Sopenharmony_cicalling `http2stream.close()`, or `http2stream.destroy()`. Will be 13301cb0ef41Sopenharmony_ci`undefined` if the `Http2Stream` has not been closed. 13311cb0ef41Sopenharmony_ci 13321cb0ef41Sopenharmony_ci#### `http2stream.sentHeaders` 13331cb0ef41Sopenharmony_ci 13341cb0ef41Sopenharmony_ci<!-- YAML 13351cb0ef41Sopenharmony_ciadded: v9.5.0 13361cb0ef41Sopenharmony_ci--> 13371cb0ef41Sopenharmony_ci 13381cb0ef41Sopenharmony_ci* {HTTP/2 Headers Object} 13391cb0ef41Sopenharmony_ci 13401cb0ef41Sopenharmony_ciAn object containing the outbound headers sent for this `Http2Stream`. 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_ci#### `http2stream.sentInfoHeaders` 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ci<!-- YAML 13451cb0ef41Sopenharmony_ciadded: v9.5.0 13461cb0ef41Sopenharmony_ci--> 13471cb0ef41Sopenharmony_ci 13481cb0ef41Sopenharmony_ci* {HTTP/2 Headers Object\[]} 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ciAn array of objects containing the outbound informational (additional) headers 13511cb0ef41Sopenharmony_cisent for this `Http2Stream`. 13521cb0ef41Sopenharmony_ci 13531cb0ef41Sopenharmony_ci#### `http2stream.sentTrailers` 13541cb0ef41Sopenharmony_ci 13551cb0ef41Sopenharmony_ci<!-- YAML 13561cb0ef41Sopenharmony_ciadded: v9.5.0 13571cb0ef41Sopenharmony_ci--> 13581cb0ef41Sopenharmony_ci 13591cb0ef41Sopenharmony_ci* {HTTP/2 Headers Object} 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ciAn object containing the outbound trailers sent for this `HttpStream`. 13621cb0ef41Sopenharmony_ci 13631cb0ef41Sopenharmony_ci#### `http2stream.session` 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ci<!-- YAML 13661cb0ef41Sopenharmony_ciadded: v8.4.0 13671cb0ef41Sopenharmony_ci--> 13681cb0ef41Sopenharmony_ci 13691cb0ef41Sopenharmony_ci* {Http2Session} 13701cb0ef41Sopenharmony_ci 13711cb0ef41Sopenharmony_ciA reference to the `Http2Session` instance that owns this `Http2Stream`. The 13721cb0ef41Sopenharmony_civalue will be `undefined` after the `Http2Stream` instance is destroyed. 13731cb0ef41Sopenharmony_ci 13741cb0ef41Sopenharmony_ci#### `http2stream.setTimeout(msecs, callback)` 13751cb0ef41Sopenharmony_ci 13761cb0ef41Sopenharmony_ci<!-- YAML 13771cb0ef41Sopenharmony_ciadded: v8.4.0 13781cb0ef41Sopenharmony_cichanges: 13791cb0ef41Sopenharmony_ci - version: v18.0.0 13801cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 13811cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 13821cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 13831cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 13841cb0ef41Sopenharmony_ci--> 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci* `msecs` {number} 13871cb0ef41Sopenharmony_ci* `callback` {Function} 13881cb0ef41Sopenharmony_ci 13891cb0ef41Sopenharmony_ci```js 13901cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 13911cb0ef41Sopenharmony_ciconst client = http2.connect('http://example.org:8000'); 13921cb0ef41Sopenharmony_ciconst { NGHTTP2_CANCEL } = http2.constants; 13931cb0ef41Sopenharmony_ciconst req = client.request({ ':path': '/' }); 13941cb0ef41Sopenharmony_ci 13951cb0ef41Sopenharmony_ci// Cancel the stream if there's no activity after 5 seconds 13961cb0ef41Sopenharmony_cireq.setTimeout(5000, () => req.close(NGHTTP2_CANCEL)); 13971cb0ef41Sopenharmony_ci``` 13981cb0ef41Sopenharmony_ci 13991cb0ef41Sopenharmony_ci#### `http2stream.state` 14001cb0ef41Sopenharmony_ci 14011cb0ef41Sopenharmony_ci<!-- YAML 14021cb0ef41Sopenharmony_ciadded: v8.4.0 14031cb0ef41Sopenharmony_ci--> 14041cb0ef41Sopenharmony_ci 14051cb0ef41Sopenharmony_ciProvides miscellaneous information about the current state of the 14061cb0ef41Sopenharmony_ci`Http2Stream`. 14071cb0ef41Sopenharmony_ci 14081cb0ef41Sopenharmony_ci* {Object} 14091cb0ef41Sopenharmony_ci * `localWindowSize` {number} The number of bytes the connected peer may send 14101cb0ef41Sopenharmony_ci for this `Http2Stream` without receiving a `WINDOW_UPDATE`. 14111cb0ef41Sopenharmony_ci * `state` {number} A flag indicating the low-level current state of the 14121cb0ef41Sopenharmony_ci `Http2Stream` as determined by `nghttp2`. 14131cb0ef41Sopenharmony_ci * `localClose` {number} `1` if this `Http2Stream` has been closed locally. 14141cb0ef41Sopenharmony_ci * `remoteClose` {number} `1` if this `Http2Stream` has been closed 14151cb0ef41Sopenharmony_ci remotely. 14161cb0ef41Sopenharmony_ci * `sumDependencyWeight` {number} The sum weight of all `Http2Stream` 14171cb0ef41Sopenharmony_ci instances that depend on this `Http2Stream` as specified using 14181cb0ef41Sopenharmony_ci `PRIORITY` frames. 14191cb0ef41Sopenharmony_ci * `weight` {number} The priority weight of this `Http2Stream`. 14201cb0ef41Sopenharmony_ci 14211cb0ef41Sopenharmony_ciA current state of this `Http2Stream`. 14221cb0ef41Sopenharmony_ci 14231cb0ef41Sopenharmony_ci#### `http2stream.sendTrailers(headers)` 14241cb0ef41Sopenharmony_ci 14251cb0ef41Sopenharmony_ci<!-- YAML 14261cb0ef41Sopenharmony_ciadded: v10.0.0 14271cb0ef41Sopenharmony_ci--> 14281cb0ef41Sopenharmony_ci 14291cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 14301cb0ef41Sopenharmony_ci 14311cb0ef41Sopenharmony_ciSends a trailing `HEADERS` frame to the connected HTTP/2 peer. This method 14321cb0ef41Sopenharmony_ciwill cause the `Http2Stream` to be immediately closed and must only be 14331cb0ef41Sopenharmony_cicalled after the `'wantTrailers'` event has been emitted. When sending a 14341cb0ef41Sopenharmony_cirequest or sending a response, the `options.waitForTrailers` option must be set 14351cb0ef41Sopenharmony_ciin order to keep the `Http2Stream` open after the final `DATA` frame so that 14361cb0ef41Sopenharmony_citrailers can be sent. 14371cb0ef41Sopenharmony_ci 14381cb0ef41Sopenharmony_ci```js 14391cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 14401cb0ef41Sopenharmony_ciconst server = http2.createServer(); 14411cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 14421cb0ef41Sopenharmony_ci stream.respond(undefined, { waitForTrailers: true }); 14431cb0ef41Sopenharmony_ci stream.on('wantTrailers', () => { 14441cb0ef41Sopenharmony_ci stream.sendTrailers({ xyz: 'abc' }); 14451cb0ef41Sopenharmony_ci }); 14461cb0ef41Sopenharmony_ci stream.end('Hello World'); 14471cb0ef41Sopenharmony_ci}); 14481cb0ef41Sopenharmony_ci``` 14491cb0ef41Sopenharmony_ci 14501cb0ef41Sopenharmony_ciThe HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header 14511cb0ef41Sopenharmony_cifields (e.g. `':method'`, `':path'`, etc). 14521cb0ef41Sopenharmony_ci 14531cb0ef41Sopenharmony_ci### Class: `ClientHttp2Stream` 14541cb0ef41Sopenharmony_ci 14551cb0ef41Sopenharmony_ci<!-- YAML 14561cb0ef41Sopenharmony_ciadded: v8.4.0 14571cb0ef41Sopenharmony_ci--> 14581cb0ef41Sopenharmony_ci 14591cb0ef41Sopenharmony_ci* Extends {Http2Stream} 14601cb0ef41Sopenharmony_ci 14611cb0ef41Sopenharmony_ciThe `ClientHttp2Stream` class is an extension of `Http2Stream` that is 14621cb0ef41Sopenharmony_ciused exclusively on HTTP/2 Clients. `Http2Stream` instances on the client 14631cb0ef41Sopenharmony_ciprovide events such as `'response'` and `'push'` that are only relevant on 14641cb0ef41Sopenharmony_cithe client. 14651cb0ef41Sopenharmony_ci 14661cb0ef41Sopenharmony_ci#### Event: `'continue'` 14671cb0ef41Sopenharmony_ci 14681cb0ef41Sopenharmony_ci<!-- YAML 14691cb0ef41Sopenharmony_ciadded: v8.5.0 14701cb0ef41Sopenharmony_ci--> 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ciEmitted when the server sends a `100 Continue` status, usually because 14731cb0ef41Sopenharmony_cithe request contained `Expect: 100-continue`. This is an instruction that 14741cb0ef41Sopenharmony_cithe client should send the request body. 14751cb0ef41Sopenharmony_ci 14761cb0ef41Sopenharmony_ci#### Event: `'headers'` 14771cb0ef41Sopenharmony_ci 14781cb0ef41Sopenharmony_ci<!-- YAML 14791cb0ef41Sopenharmony_ciadded: v8.4.0 14801cb0ef41Sopenharmony_ci--> 14811cb0ef41Sopenharmony_ci 14821cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 14831cb0ef41Sopenharmony_ci* `flags` {number} 14841cb0ef41Sopenharmony_ci 14851cb0ef41Sopenharmony_ciThe `'headers'` event is emitted when an additional block of headers is received 14861cb0ef41Sopenharmony_cifor a stream, such as when a block of `1xx` informational headers is received. 14871cb0ef41Sopenharmony_ciThe listener callback is passed the [HTTP/2 Headers Object][] and flags 14881cb0ef41Sopenharmony_ciassociated with the headers. 14891cb0ef41Sopenharmony_ci 14901cb0ef41Sopenharmony_ci```js 14911cb0ef41Sopenharmony_cistream.on('headers', (headers, flags) => { 14921cb0ef41Sopenharmony_ci console.log(headers); 14931cb0ef41Sopenharmony_ci}); 14941cb0ef41Sopenharmony_ci``` 14951cb0ef41Sopenharmony_ci 14961cb0ef41Sopenharmony_ci#### Event: `'push'` 14971cb0ef41Sopenharmony_ci 14981cb0ef41Sopenharmony_ci<!-- YAML 14991cb0ef41Sopenharmony_ciadded: v8.4.0 15001cb0ef41Sopenharmony_ci--> 15011cb0ef41Sopenharmony_ci 15021cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 15031cb0ef41Sopenharmony_ci* `flags` {number} 15041cb0ef41Sopenharmony_ci 15051cb0ef41Sopenharmony_ciThe `'push'` event is emitted when response headers for a Server Push stream 15061cb0ef41Sopenharmony_ciare received. The listener callback is passed the [HTTP/2 Headers Object][] and 15071cb0ef41Sopenharmony_ciflags associated with the headers. 15081cb0ef41Sopenharmony_ci 15091cb0ef41Sopenharmony_ci```js 15101cb0ef41Sopenharmony_cistream.on('push', (headers, flags) => { 15111cb0ef41Sopenharmony_ci console.log(headers); 15121cb0ef41Sopenharmony_ci}); 15131cb0ef41Sopenharmony_ci``` 15141cb0ef41Sopenharmony_ci 15151cb0ef41Sopenharmony_ci#### Event: `'response'` 15161cb0ef41Sopenharmony_ci 15171cb0ef41Sopenharmony_ci<!-- YAML 15181cb0ef41Sopenharmony_ciadded: v8.4.0 15191cb0ef41Sopenharmony_ci--> 15201cb0ef41Sopenharmony_ci 15211cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 15221cb0ef41Sopenharmony_ci* `flags` {number} 15231cb0ef41Sopenharmony_ci 15241cb0ef41Sopenharmony_ciThe `'response'` event is emitted when a response `HEADERS` frame has been 15251cb0ef41Sopenharmony_cireceived for this stream from the connected HTTP/2 server. The listener is 15261cb0ef41Sopenharmony_ciinvoked with two arguments: an `Object` containing the received 15271cb0ef41Sopenharmony_ci[HTTP/2 Headers Object][], and flags associated with the headers. 15281cb0ef41Sopenharmony_ci 15291cb0ef41Sopenharmony_ci```js 15301cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 15311cb0ef41Sopenharmony_ciconst client = http2.connect('https://localhost'); 15321cb0ef41Sopenharmony_ciconst req = client.request({ ':path': '/' }); 15331cb0ef41Sopenharmony_cireq.on('response', (headers, flags) => { 15341cb0ef41Sopenharmony_ci console.log(headers[':status']); 15351cb0ef41Sopenharmony_ci}); 15361cb0ef41Sopenharmony_ci``` 15371cb0ef41Sopenharmony_ci 15381cb0ef41Sopenharmony_ci### Class: `ServerHttp2Stream` 15391cb0ef41Sopenharmony_ci 15401cb0ef41Sopenharmony_ci<!-- YAML 15411cb0ef41Sopenharmony_ciadded: v8.4.0 15421cb0ef41Sopenharmony_ci--> 15431cb0ef41Sopenharmony_ci 15441cb0ef41Sopenharmony_ci* Extends: {Http2Stream} 15451cb0ef41Sopenharmony_ci 15461cb0ef41Sopenharmony_ciThe `ServerHttp2Stream` class is an extension of [`Http2Stream`][] that is 15471cb0ef41Sopenharmony_ciused exclusively on HTTP/2 Servers. `Http2Stream` instances on the server 15481cb0ef41Sopenharmony_ciprovide additional methods such as `http2stream.pushStream()` and 15491cb0ef41Sopenharmony_ci`http2stream.respond()` that are only relevant on the server. 15501cb0ef41Sopenharmony_ci 15511cb0ef41Sopenharmony_ci#### `http2stream.additionalHeaders(headers)` 15521cb0ef41Sopenharmony_ci 15531cb0ef41Sopenharmony_ci<!-- YAML 15541cb0ef41Sopenharmony_ciadded: v8.4.0 15551cb0ef41Sopenharmony_ci--> 15561cb0ef41Sopenharmony_ci 15571cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 15581cb0ef41Sopenharmony_ci 15591cb0ef41Sopenharmony_ciSends an additional informational `HEADERS` frame to the connected HTTP/2 peer. 15601cb0ef41Sopenharmony_ci 15611cb0ef41Sopenharmony_ci#### `http2stream.headersSent` 15621cb0ef41Sopenharmony_ci 15631cb0ef41Sopenharmony_ci<!-- YAML 15641cb0ef41Sopenharmony_ciadded: v8.4.0 15651cb0ef41Sopenharmony_ci--> 15661cb0ef41Sopenharmony_ci 15671cb0ef41Sopenharmony_ci* {boolean} 15681cb0ef41Sopenharmony_ci 15691cb0ef41Sopenharmony_ciTrue if headers were sent, false otherwise (read-only). 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_ci#### `http2stream.pushAllowed` 15721cb0ef41Sopenharmony_ci 15731cb0ef41Sopenharmony_ci<!-- YAML 15741cb0ef41Sopenharmony_ciadded: v8.4.0 15751cb0ef41Sopenharmony_ci--> 15761cb0ef41Sopenharmony_ci 15771cb0ef41Sopenharmony_ci* {boolean} 15781cb0ef41Sopenharmony_ci 15791cb0ef41Sopenharmony_ciRead-only property mapped to the `SETTINGS_ENABLE_PUSH` flag of the remote 15801cb0ef41Sopenharmony_ciclient's most recent `SETTINGS` frame. Will be `true` if the remote peer 15811cb0ef41Sopenharmony_ciaccepts push streams, `false` otherwise. Settings are the same for every 15821cb0ef41Sopenharmony_ci`Http2Stream` in the same `Http2Session`. 15831cb0ef41Sopenharmony_ci 15841cb0ef41Sopenharmony_ci#### `http2stream.pushStream(headers[, options], callback)` 15851cb0ef41Sopenharmony_ci 15861cb0ef41Sopenharmony_ci<!-- YAML 15871cb0ef41Sopenharmony_ciadded: v8.4.0 15881cb0ef41Sopenharmony_cichanges: 15891cb0ef41Sopenharmony_ci - version: v18.0.0 15901cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 15911cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 15921cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 15931cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 15941cb0ef41Sopenharmony_ci--> 15951cb0ef41Sopenharmony_ci 15961cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 15971cb0ef41Sopenharmony_ci* `options` {Object} 15981cb0ef41Sopenharmony_ci * `exclusive` {boolean} When `true` and `parent` identifies a parent Stream, 15991cb0ef41Sopenharmony_ci the created stream is made the sole direct dependency of the parent, with 16001cb0ef41Sopenharmony_ci all other existing dependents made a dependent of the newly created stream. 16011cb0ef41Sopenharmony_ci **Default:** `false`. 16021cb0ef41Sopenharmony_ci * `parent` {number} Specifies the numeric identifier of a stream the newly 16031cb0ef41Sopenharmony_ci created stream is dependent on. 16041cb0ef41Sopenharmony_ci* `callback` {Function} Callback that is called once the push stream has been 16051cb0ef41Sopenharmony_ci initiated. 16061cb0ef41Sopenharmony_ci * `err` {Error} 16071cb0ef41Sopenharmony_ci * `pushStream` {ServerHttp2Stream} The returned `pushStream` object. 16081cb0ef41Sopenharmony_ci * `headers` {HTTP/2 Headers Object} Headers object the `pushStream` was 16091cb0ef41Sopenharmony_ci initiated with. 16101cb0ef41Sopenharmony_ci 16111cb0ef41Sopenharmony_ciInitiates a push stream. The callback is invoked with the new `Http2Stream` 16121cb0ef41Sopenharmony_ciinstance created for the push stream passed as the second argument, or an 16131cb0ef41Sopenharmony_ci`Error` passed as the first argument. 16141cb0ef41Sopenharmony_ci 16151cb0ef41Sopenharmony_ci```js 16161cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 16171cb0ef41Sopenharmony_ciconst server = http2.createServer(); 16181cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 16191cb0ef41Sopenharmony_ci stream.respond({ ':status': 200 }); 16201cb0ef41Sopenharmony_ci stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => { 16211cb0ef41Sopenharmony_ci if (err) throw err; 16221cb0ef41Sopenharmony_ci pushStream.respond({ ':status': 200 }); 16231cb0ef41Sopenharmony_ci pushStream.end('some pushed data'); 16241cb0ef41Sopenharmony_ci }); 16251cb0ef41Sopenharmony_ci stream.end('some data'); 16261cb0ef41Sopenharmony_ci}); 16271cb0ef41Sopenharmony_ci``` 16281cb0ef41Sopenharmony_ci 16291cb0ef41Sopenharmony_ciSetting the weight of a push stream is not allowed in the `HEADERS` frame. Pass 16301cb0ef41Sopenharmony_cia `weight` value to `http2stream.priority` with the `silent` option set to 16311cb0ef41Sopenharmony_ci`true` to enable server-side bandwidth balancing between concurrent streams. 16321cb0ef41Sopenharmony_ci 16331cb0ef41Sopenharmony_ciCalling `http2stream.pushStream()` from within a pushed stream is not permitted 16341cb0ef41Sopenharmony_ciand will throw an error. 16351cb0ef41Sopenharmony_ci 16361cb0ef41Sopenharmony_ci#### `http2stream.respond([headers[, options]])` 16371cb0ef41Sopenharmony_ci 16381cb0ef41Sopenharmony_ci<!-- YAML 16391cb0ef41Sopenharmony_ciadded: v8.4.0 16401cb0ef41Sopenharmony_cichanges: 16411cb0ef41Sopenharmony_ci - version: 16421cb0ef41Sopenharmony_ci - v14.5.0 16431cb0ef41Sopenharmony_ci - v12.19.0 16441cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33160 16451cb0ef41Sopenharmony_ci description: Allow explicitly setting date headers. 16461cb0ef41Sopenharmony_ci--> 16471cb0ef41Sopenharmony_ci 16481cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 16491cb0ef41Sopenharmony_ci* `options` {Object} 16501cb0ef41Sopenharmony_ci * `endStream` {boolean} Set to `true` to indicate that the response will not 16511cb0ef41Sopenharmony_ci include payload data. 16521cb0ef41Sopenharmony_ci * `waitForTrailers` {boolean} When `true`, the `Http2Stream` will emit the 16531cb0ef41Sopenharmony_ci `'wantTrailers'` event after the final `DATA` frame has been sent. 16541cb0ef41Sopenharmony_ci 16551cb0ef41Sopenharmony_ci```js 16561cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 16571cb0ef41Sopenharmony_ciconst server = http2.createServer(); 16581cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 16591cb0ef41Sopenharmony_ci stream.respond({ ':status': 200 }); 16601cb0ef41Sopenharmony_ci stream.end('some data'); 16611cb0ef41Sopenharmony_ci}); 16621cb0ef41Sopenharmony_ci``` 16631cb0ef41Sopenharmony_ci 16641cb0ef41Sopenharmony_ciInitiates a response. When the `options.waitForTrailers` option is set, the 16651cb0ef41Sopenharmony_ci`'wantTrailers'` event will be emitted immediately after queuing the last chunk 16661cb0ef41Sopenharmony_ciof payload data to be sent. The `http2stream.sendTrailers()` method can then be 16671cb0ef41Sopenharmony_ciused to sent trailing header fields to the peer. 16681cb0ef41Sopenharmony_ci 16691cb0ef41Sopenharmony_ciWhen `options.waitForTrailers` is set, the `Http2Stream` will not automatically 16701cb0ef41Sopenharmony_ciclose when the final `DATA` frame is transmitted. User code must call either 16711cb0ef41Sopenharmony_ci`http2stream.sendTrailers()` or `http2stream.close()` to close the 16721cb0ef41Sopenharmony_ci`Http2Stream`. 16731cb0ef41Sopenharmony_ci 16741cb0ef41Sopenharmony_ci```js 16751cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 16761cb0ef41Sopenharmony_ciconst server = http2.createServer(); 16771cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 16781cb0ef41Sopenharmony_ci stream.respond({ ':status': 200 }, { waitForTrailers: true }); 16791cb0ef41Sopenharmony_ci stream.on('wantTrailers', () => { 16801cb0ef41Sopenharmony_ci stream.sendTrailers({ ABC: 'some value to send' }); 16811cb0ef41Sopenharmony_ci }); 16821cb0ef41Sopenharmony_ci stream.end('some data'); 16831cb0ef41Sopenharmony_ci}); 16841cb0ef41Sopenharmony_ci``` 16851cb0ef41Sopenharmony_ci 16861cb0ef41Sopenharmony_ci#### `http2stream.respondWithFD(fd[, headers[, options]])` 16871cb0ef41Sopenharmony_ci 16881cb0ef41Sopenharmony_ci<!-- YAML 16891cb0ef41Sopenharmony_ciadded: v8.4.0 16901cb0ef41Sopenharmony_cichanges: 16911cb0ef41Sopenharmony_ci - version: 16921cb0ef41Sopenharmony_ci - v14.5.0 16931cb0ef41Sopenharmony_ci - v12.19.0 16941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33160 16951cb0ef41Sopenharmony_ci description: Allow explicitly setting date headers. 16961cb0ef41Sopenharmony_ci - version: v12.12.0 16971cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29876 16981cb0ef41Sopenharmony_ci description: The `fd` option may now be a `FileHandle`. 16991cb0ef41Sopenharmony_ci - version: v10.0.0 17001cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18936 17011cb0ef41Sopenharmony_ci description: Any readable file descriptor, not necessarily for a 17021cb0ef41Sopenharmony_ci regular file, is supported now. 17031cb0ef41Sopenharmony_ci--> 17041cb0ef41Sopenharmony_ci 17051cb0ef41Sopenharmony_ci* `fd` {number|FileHandle} A readable file descriptor. 17061cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 17071cb0ef41Sopenharmony_ci* `options` {Object} 17081cb0ef41Sopenharmony_ci * `statCheck` {Function} 17091cb0ef41Sopenharmony_ci * `waitForTrailers` {boolean} When `true`, the `Http2Stream` will emit the 17101cb0ef41Sopenharmony_ci `'wantTrailers'` event after the final `DATA` frame has been sent. 17111cb0ef41Sopenharmony_ci * `offset` {number} The offset position at which to begin reading. 17121cb0ef41Sopenharmony_ci * `length` {number} The amount of data from the fd to send. 17131cb0ef41Sopenharmony_ci 17141cb0ef41Sopenharmony_ciInitiates a response whose data is read from the given file descriptor. No 17151cb0ef41Sopenharmony_civalidation is performed on the given file descriptor. If an error occurs while 17161cb0ef41Sopenharmony_ciattempting to read data using the file descriptor, the `Http2Stream` will be 17171cb0ef41Sopenharmony_ciclosed using an `RST_STREAM` frame using the standard `INTERNAL_ERROR` code. 17181cb0ef41Sopenharmony_ci 17191cb0ef41Sopenharmony_ciWhen used, the `Http2Stream` object's `Duplex` interface will be closed 17201cb0ef41Sopenharmony_ciautomatically. 17211cb0ef41Sopenharmony_ci 17221cb0ef41Sopenharmony_ci```js 17231cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 17241cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 17251cb0ef41Sopenharmony_ci 17261cb0ef41Sopenharmony_ciconst server = http2.createServer(); 17271cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 17281cb0ef41Sopenharmony_ci const fd = fs.openSync('/some/file', 'r'); 17291cb0ef41Sopenharmony_ci 17301cb0ef41Sopenharmony_ci const stat = fs.fstatSync(fd); 17311cb0ef41Sopenharmony_ci const headers = { 17321cb0ef41Sopenharmony_ci 'content-length': stat.size, 17331cb0ef41Sopenharmony_ci 'last-modified': stat.mtime.toUTCString(), 17341cb0ef41Sopenharmony_ci 'content-type': 'text/plain; charset=utf-8', 17351cb0ef41Sopenharmony_ci }; 17361cb0ef41Sopenharmony_ci stream.respondWithFD(fd, headers); 17371cb0ef41Sopenharmony_ci stream.on('close', () => fs.closeSync(fd)); 17381cb0ef41Sopenharmony_ci}); 17391cb0ef41Sopenharmony_ci``` 17401cb0ef41Sopenharmony_ci 17411cb0ef41Sopenharmony_ciThe optional `options.statCheck` function may be specified to give user code 17421cb0ef41Sopenharmony_cian opportunity to set additional content headers based on the `fs.Stat` details 17431cb0ef41Sopenharmony_ciof the given fd. If the `statCheck` function is provided, the 17441cb0ef41Sopenharmony_ci`http2stream.respondWithFD()` method will perform an `fs.fstat()` call to 17451cb0ef41Sopenharmony_cicollect details on the provided file descriptor. 17461cb0ef41Sopenharmony_ci 17471cb0ef41Sopenharmony_ciThe `offset` and `length` options may be used to limit the response to a 17481cb0ef41Sopenharmony_cispecific range subset. This can be used, for instance, to support HTTP Range 17491cb0ef41Sopenharmony_cirequests. 17501cb0ef41Sopenharmony_ci 17511cb0ef41Sopenharmony_ciThe file descriptor or `FileHandle` is not closed when the stream is closed, 17521cb0ef41Sopenharmony_ciso it will need to be closed manually once it is no longer needed. 17531cb0ef41Sopenharmony_ciUsing the same file descriptor concurrently for multiple streams 17541cb0ef41Sopenharmony_ciis not supported and may result in data loss. Re-using a file descriptor 17551cb0ef41Sopenharmony_ciafter a stream has finished is supported. 17561cb0ef41Sopenharmony_ci 17571cb0ef41Sopenharmony_ciWhen the `options.waitForTrailers` option is set, the `'wantTrailers'` event 17581cb0ef41Sopenharmony_ciwill be emitted immediately after queuing the last chunk of payload data to be 17591cb0ef41Sopenharmony_cisent. The `http2stream.sendTrailers()` method can then be used to sent trailing 17601cb0ef41Sopenharmony_ciheader fields to the peer. 17611cb0ef41Sopenharmony_ci 17621cb0ef41Sopenharmony_ciWhen `options.waitForTrailers` is set, the `Http2Stream` will not automatically 17631cb0ef41Sopenharmony_ciclose when the final `DATA` frame is transmitted. User code _must_ call either 17641cb0ef41Sopenharmony_ci`http2stream.sendTrailers()` or `http2stream.close()` to close the 17651cb0ef41Sopenharmony_ci`Http2Stream`. 17661cb0ef41Sopenharmony_ci 17671cb0ef41Sopenharmony_ci```js 17681cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 17691cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 17701cb0ef41Sopenharmony_ci 17711cb0ef41Sopenharmony_ciconst server = http2.createServer(); 17721cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 17731cb0ef41Sopenharmony_ci const fd = fs.openSync('/some/file', 'r'); 17741cb0ef41Sopenharmony_ci 17751cb0ef41Sopenharmony_ci const stat = fs.fstatSync(fd); 17761cb0ef41Sopenharmony_ci const headers = { 17771cb0ef41Sopenharmony_ci 'content-length': stat.size, 17781cb0ef41Sopenharmony_ci 'last-modified': stat.mtime.toUTCString(), 17791cb0ef41Sopenharmony_ci 'content-type': 'text/plain; charset=utf-8', 17801cb0ef41Sopenharmony_ci }; 17811cb0ef41Sopenharmony_ci stream.respondWithFD(fd, headers, { waitForTrailers: true }); 17821cb0ef41Sopenharmony_ci stream.on('wantTrailers', () => { 17831cb0ef41Sopenharmony_ci stream.sendTrailers({ ABC: 'some value to send' }); 17841cb0ef41Sopenharmony_ci }); 17851cb0ef41Sopenharmony_ci 17861cb0ef41Sopenharmony_ci stream.on('close', () => fs.closeSync(fd)); 17871cb0ef41Sopenharmony_ci}); 17881cb0ef41Sopenharmony_ci``` 17891cb0ef41Sopenharmony_ci 17901cb0ef41Sopenharmony_ci#### `http2stream.respondWithFile(path[, headers[, options]])` 17911cb0ef41Sopenharmony_ci 17921cb0ef41Sopenharmony_ci<!-- YAML 17931cb0ef41Sopenharmony_ciadded: v8.4.0 17941cb0ef41Sopenharmony_cichanges: 17951cb0ef41Sopenharmony_ci - version: 17961cb0ef41Sopenharmony_ci - v14.5.0 17971cb0ef41Sopenharmony_ci - v12.19.0 17981cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33160 17991cb0ef41Sopenharmony_ci description: Allow explicitly setting date headers. 18001cb0ef41Sopenharmony_ci - version: v10.0.0 18011cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18936 18021cb0ef41Sopenharmony_ci description: Any readable file, not necessarily a 18031cb0ef41Sopenharmony_ci regular file, is supported now. 18041cb0ef41Sopenharmony_ci--> 18051cb0ef41Sopenharmony_ci 18061cb0ef41Sopenharmony_ci* `path` {string|Buffer|URL} 18071cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} 18081cb0ef41Sopenharmony_ci* `options` {Object} 18091cb0ef41Sopenharmony_ci * `statCheck` {Function} 18101cb0ef41Sopenharmony_ci * `onError` {Function} Callback function invoked in the case of an 18111cb0ef41Sopenharmony_ci error before send. 18121cb0ef41Sopenharmony_ci * `waitForTrailers` {boolean} When `true`, the `Http2Stream` will emit the 18131cb0ef41Sopenharmony_ci `'wantTrailers'` event after the final `DATA` frame has been sent. 18141cb0ef41Sopenharmony_ci * `offset` {number} The offset position at which to begin reading. 18151cb0ef41Sopenharmony_ci * `length` {number} The amount of data from the fd to send. 18161cb0ef41Sopenharmony_ci 18171cb0ef41Sopenharmony_ciSends a regular file as the response. The `path` must specify a regular file 18181cb0ef41Sopenharmony_cior an `'error'` event will be emitted on the `Http2Stream` object. 18191cb0ef41Sopenharmony_ci 18201cb0ef41Sopenharmony_ciWhen used, the `Http2Stream` object's `Duplex` interface will be closed 18211cb0ef41Sopenharmony_ciautomatically. 18221cb0ef41Sopenharmony_ci 18231cb0ef41Sopenharmony_ciThe optional `options.statCheck` function may be specified to give user code 18241cb0ef41Sopenharmony_cian opportunity to set additional content headers based on the `fs.Stat` details 18251cb0ef41Sopenharmony_ciof the given file: 18261cb0ef41Sopenharmony_ci 18271cb0ef41Sopenharmony_ciIf an error occurs while attempting to read the file data, the `Http2Stream` 18281cb0ef41Sopenharmony_ciwill be closed using an `RST_STREAM` frame using the standard `INTERNAL_ERROR` 18291cb0ef41Sopenharmony_cicode. If the `onError` callback is defined, then it will be called. Otherwise 18301cb0ef41Sopenharmony_cithe stream will be destroyed. 18311cb0ef41Sopenharmony_ci 18321cb0ef41Sopenharmony_ciExample using a file path: 18331cb0ef41Sopenharmony_ci 18341cb0ef41Sopenharmony_ci```js 18351cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 18361cb0ef41Sopenharmony_ciconst server = http2.createServer(); 18371cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 18381cb0ef41Sopenharmony_ci function statCheck(stat, headers) { 18391cb0ef41Sopenharmony_ci headers['last-modified'] = stat.mtime.toUTCString(); 18401cb0ef41Sopenharmony_ci } 18411cb0ef41Sopenharmony_ci 18421cb0ef41Sopenharmony_ci function onError(err) { 18431cb0ef41Sopenharmony_ci // stream.respond() can throw if the stream has been destroyed by 18441cb0ef41Sopenharmony_ci // the other side. 18451cb0ef41Sopenharmony_ci try { 18461cb0ef41Sopenharmony_ci if (err.code === 'ENOENT') { 18471cb0ef41Sopenharmony_ci stream.respond({ ':status': 404 }); 18481cb0ef41Sopenharmony_ci } else { 18491cb0ef41Sopenharmony_ci stream.respond({ ':status': 500 }); 18501cb0ef41Sopenharmony_ci } 18511cb0ef41Sopenharmony_ci } catch (err) { 18521cb0ef41Sopenharmony_ci // Perform actual error handling. 18531cb0ef41Sopenharmony_ci console.error(err); 18541cb0ef41Sopenharmony_ci } 18551cb0ef41Sopenharmony_ci stream.end(); 18561cb0ef41Sopenharmony_ci } 18571cb0ef41Sopenharmony_ci 18581cb0ef41Sopenharmony_ci stream.respondWithFile('/some/file', 18591cb0ef41Sopenharmony_ci { 'content-type': 'text/plain; charset=utf-8' }, 18601cb0ef41Sopenharmony_ci { statCheck, onError }); 18611cb0ef41Sopenharmony_ci}); 18621cb0ef41Sopenharmony_ci``` 18631cb0ef41Sopenharmony_ci 18641cb0ef41Sopenharmony_ciThe `options.statCheck` function may also be used to cancel the send operation 18651cb0ef41Sopenharmony_ciby returning `false`. For instance, a conditional request may check the stat 18661cb0ef41Sopenharmony_ciresults to determine if the file has been modified to return an appropriate 18671cb0ef41Sopenharmony_ci`304` response: 18681cb0ef41Sopenharmony_ci 18691cb0ef41Sopenharmony_ci```js 18701cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 18711cb0ef41Sopenharmony_ciconst server = http2.createServer(); 18721cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 18731cb0ef41Sopenharmony_ci function statCheck(stat, headers) { 18741cb0ef41Sopenharmony_ci // Check the stat here... 18751cb0ef41Sopenharmony_ci stream.respond({ ':status': 304 }); 18761cb0ef41Sopenharmony_ci return false; // Cancel the send operation 18771cb0ef41Sopenharmony_ci } 18781cb0ef41Sopenharmony_ci stream.respondWithFile('/some/file', 18791cb0ef41Sopenharmony_ci { 'content-type': 'text/plain; charset=utf-8' }, 18801cb0ef41Sopenharmony_ci { statCheck }); 18811cb0ef41Sopenharmony_ci}); 18821cb0ef41Sopenharmony_ci``` 18831cb0ef41Sopenharmony_ci 18841cb0ef41Sopenharmony_ciThe `content-length` header field will be automatically set. 18851cb0ef41Sopenharmony_ci 18861cb0ef41Sopenharmony_ciThe `offset` and `length` options may be used to limit the response to a 18871cb0ef41Sopenharmony_cispecific range subset. This can be used, for instance, to support HTTP Range 18881cb0ef41Sopenharmony_cirequests. 18891cb0ef41Sopenharmony_ci 18901cb0ef41Sopenharmony_ciThe `options.onError` function may also be used to handle all the errors 18911cb0ef41Sopenharmony_cithat could happen before the delivery of the file is initiated. The 18921cb0ef41Sopenharmony_cidefault behavior is to destroy the stream. 18931cb0ef41Sopenharmony_ci 18941cb0ef41Sopenharmony_ciWhen the `options.waitForTrailers` option is set, the `'wantTrailers'` event 18951cb0ef41Sopenharmony_ciwill be emitted immediately after queuing the last chunk of payload data to be 18961cb0ef41Sopenharmony_cisent. The `http2stream.sendTrailers()` method can then be used to sent trailing 18971cb0ef41Sopenharmony_ciheader fields to the peer. 18981cb0ef41Sopenharmony_ci 18991cb0ef41Sopenharmony_ciWhen `options.waitForTrailers` is set, the `Http2Stream` will not automatically 19001cb0ef41Sopenharmony_ciclose when the final `DATA` frame is transmitted. User code must call either 19011cb0ef41Sopenharmony_ci`http2stream.sendTrailers()` or `http2stream.close()` to close the 19021cb0ef41Sopenharmony_ci`Http2Stream`. 19031cb0ef41Sopenharmony_ci 19041cb0ef41Sopenharmony_ci```js 19051cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 19061cb0ef41Sopenharmony_ciconst server = http2.createServer(); 19071cb0ef41Sopenharmony_ciserver.on('stream', (stream) => { 19081cb0ef41Sopenharmony_ci stream.respondWithFile('/some/file', 19091cb0ef41Sopenharmony_ci { 'content-type': 'text/plain; charset=utf-8' }, 19101cb0ef41Sopenharmony_ci { waitForTrailers: true }); 19111cb0ef41Sopenharmony_ci stream.on('wantTrailers', () => { 19121cb0ef41Sopenharmony_ci stream.sendTrailers({ ABC: 'some value to send' }); 19131cb0ef41Sopenharmony_ci }); 19141cb0ef41Sopenharmony_ci}); 19151cb0ef41Sopenharmony_ci``` 19161cb0ef41Sopenharmony_ci 19171cb0ef41Sopenharmony_ci### Class: `Http2Server` 19181cb0ef41Sopenharmony_ci 19191cb0ef41Sopenharmony_ci<!-- YAML 19201cb0ef41Sopenharmony_ciadded: v8.4.0 19211cb0ef41Sopenharmony_ci--> 19221cb0ef41Sopenharmony_ci 19231cb0ef41Sopenharmony_ci* Extends: {net.Server} 19241cb0ef41Sopenharmony_ci 19251cb0ef41Sopenharmony_ciInstances of `Http2Server` are created using the `http2.createServer()` 19261cb0ef41Sopenharmony_cifunction. The `Http2Server` class is not exported directly by the 19271cb0ef41Sopenharmony_ci`node:http2` module. 19281cb0ef41Sopenharmony_ci 19291cb0ef41Sopenharmony_ci#### Event: `'checkContinue'` 19301cb0ef41Sopenharmony_ci 19311cb0ef41Sopenharmony_ci<!-- YAML 19321cb0ef41Sopenharmony_ciadded: v8.5.0 19331cb0ef41Sopenharmony_ci--> 19341cb0ef41Sopenharmony_ci 19351cb0ef41Sopenharmony_ci* `request` {http2.Http2ServerRequest} 19361cb0ef41Sopenharmony_ci* `response` {http2.Http2ServerResponse} 19371cb0ef41Sopenharmony_ci 19381cb0ef41Sopenharmony_ciIf a [`'request'`][] listener is registered or [`http2.createServer()`][] is 19391cb0ef41Sopenharmony_cisupplied a callback function, the `'checkContinue'` event is emitted each time 19401cb0ef41Sopenharmony_cia request with an HTTP `Expect: 100-continue` is received. If this event is 19411cb0ef41Sopenharmony_cinot listened for, the server will automatically respond with a status 19421cb0ef41Sopenharmony_ci`100 Continue` as appropriate. 19431cb0ef41Sopenharmony_ci 19441cb0ef41Sopenharmony_ciHandling this event involves calling [`response.writeContinue()`][] if the 19451cb0ef41Sopenharmony_ciclient should continue to send the request body, or generating an appropriate 19461cb0ef41Sopenharmony_ciHTTP response (e.g. 400 Bad Request) if the client should not continue to send 19471cb0ef41Sopenharmony_cithe request body. 19481cb0ef41Sopenharmony_ci 19491cb0ef41Sopenharmony_ciWhen this event is emitted and handled, the [`'request'`][] event will 19501cb0ef41Sopenharmony_cinot be emitted. 19511cb0ef41Sopenharmony_ci 19521cb0ef41Sopenharmony_ci#### Event: `'connection'` 19531cb0ef41Sopenharmony_ci 19541cb0ef41Sopenharmony_ci<!-- YAML 19551cb0ef41Sopenharmony_ciadded: v8.4.0 19561cb0ef41Sopenharmony_ci--> 19571cb0ef41Sopenharmony_ci 19581cb0ef41Sopenharmony_ci* `socket` {stream.Duplex} 19591cb0ef41Sopenharmony_ci 19601cb0ef41Sopenharmony_ciThis event is emitted when a new TCP stream is established. `socket` is 19611cb0ef41Sopenharmony_citypically an object of type [`net.Socket`][]. Usually users will not want to 19621cb0ef41Sopenharmony_ciaccess this event. 19631cb0ef41Sopenharmony_ci 19641cb0ef41Sopenharmony_ciThis event can also be explicitly emitted by users to inject connections 19651cb0ef41Sopenharmony_ciinto the HTTP server. In that case, any [`Duplex`][] stream can be passed. 19661cb0ef41Sopenharmony_ci 19671cb0ef41Sopenharmony_ci#### Event: `'request'` 19681cb0ef41Sopenharmony_ci 19691cb0ef41Sopenharmony_ci<!-- YAML 19701cb0ef41Sopenharmony_ciadded: v8.4.0 19711cb0ef41Sopenharmony_ci--> 19721cb0ef41Sopenharmony_ci 19731cb0ef41Sopenharmony_ci* `request` {http2.Http2ServerRequest} 19741cb0ef41Sopenharmony_ci* `response` {http2.Http2ServerResponse} 19751cb0ef41Sopenharmony_ci 19761cb0ef41Sopenharmony_ciEmitted each time there is a request. There may be multiple requests 19771cb0ef41Sopenharmony_ciper session. See the [Compatibility API][]. 19781cb0ef41Sopenharmony_ci 19791cb0ef41Sopenharmony_ci#### Event: `'session'` 19801cb0ef41Sopenharmony_ci 19811cb0ef41Sopenharmony_ci<!-- YAML 19821cb0ef41Sopenharmony_ciadded: v8.4.0 19831cb0ef41Sopenharmony_ci--> 19841cb0ef41Sopenharmony_ci 19851cb0ef41Sopenharmony_ci* `session` {ServerHttp2Session} 19861cb0ef41Sopenharmony_ci 19871cb0ef41Sopenharmony_ciThe `'session'` event is emitted when a new `Http2Session` is created by the 19881cb0ef41Sopenharmony_ci`Http2Server`. 19891cb0ef41Sopenharmony_ci 19901cb0ef41Sopenharmony_ci#### Event: `'sessionError'` 19911cb0ef41Sopenharmony_ci 19921cb0ef41Sopenharmony_ci<!-- YAML 19931cb0ef41Sopenharmony_ciadded: v8.4.0 19941cb0ef41Sopenharmony_ci--> 19951cb0ef41Sopenharmony_ci 19961cb0ef41Sopenharmony_ci* `error` {Error} 19971cb0ef41Sopenharmony_ci* `session` {ServerHttp2Session} 19981cb0ef41Sopenharmony_ci 19991cb0ef41Sopenharmony_ciThe `'sessionError'` event is emitted when an `'error'` event is emitted by 20001cb0ef41Sopenharmony_cian `Http2Session` object associated with the `Http2Server`. 20011cb0ef41Sopenharmony_ci 20021cb0ef41Sopenharmony_ci#### Event: `'stream'` 20031cb0ef41Sopenharmony_ci 20041cb0ef41Sopenharmony_ci<!-- YAML 20051cb0ef41Sopenharmony_ciadded: v8.4.0 20061cb0ef41Sopenharmony_ci--> 20071cb0ef41Sopenharmony_ci 20081cb0ef41Sopenharmony_ci* `stream` {Http2Stream} A reference to the stream 20091cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} An object describing the headers 20101cb0ef41Sopenharmony_ci* `flags` {number} The associated numeric flags 20111cb0ef41Sopenharmony_ci* `rawHeaders` {Array} An array containing the raw header names followed by 20121cb0ef41Sopenharmony_ci their respective values. 20131cb0ef41Sopenharmony_ci 20141cb0ef41Sopenharmony_ciThe `'stream'` event is emitted when a `'stream'` event has been emitted by 20151cb0ef41Sopenharmony_cian `Http2Session` associated with the server. 20161cb0ef41Sopenharmony_ci 20171cb0ef41Sopenharmony_ciSee also [`Http2Session`'s `'stream'` event][]. 20181cb0ef41Sopenharmony_ci 20191cb0ef41Sopenharmony_ci```js 20201cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 20211cb0ef41Sopenharmony_ciconst { 20221cb0ef41Sopenharmony_ci HTTP2_HEADER_METHOD, 20231cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 20241cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS, 20251cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_TYPE, 20261cb0ef41Sopenharmony_ci} = http2.constants; 20271cb0ef41Sopenharmony_ci 20281cb0ef41Sopenharmony_ciconst server = http2.createServer(); 20291cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers, flags) => { 20301cb0ef41Sopenharmony_ci const method = headers[HTTP2_HEADER_METHOD]; 20311cb0ef41Sopenharmony_ci const path = headers[HTTP2_HEADER_PATH]; 20321cb0ef41Sopenharmony_ci // ... 20331cb0ef41Sopenharmony_ci stream.respond({ 20341cb0ef41Sopenharmony_ci [HTTP2_HEADER_STATUS]: 200, 20351cb0ef41Sopenharmony_ci [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8', 20361cb0ef41Sopenharmony_ci }); 20371cb0ef41Sopenharmony_ci stream.write('hello '); 20381cb0ef41Sopenharmony_ci stream.end('world'); 20391cb0ef41Sopenharmony_ci}); 20401cb0ef41Sopenharmony_ci``` 20411cb0ef41Sopenharmony_ci 20421cb0ef41Sopenharmony_ci#### Event: `'timeout'` 20431cb0ef41Sopenharmony_ci 20441cb0ef41Sopenharmony_ci<!-- YAML 20451cb0ef41Sopenharmony_ciadded: v8.4.0 20461cb0ef41Sopenharmony_cichanges: 20471cb0ef41Sopenharmony_ci - version: v13.0.0 20481cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27558 20491cb0ef41Sopenharmony_ci description: The default timeout changed from 120s to 0 (no timeout). 20501cb0ef41Sopenharmony_ci--> 20511cb0ef41Sopenharmony_ci 20521cb0ef41Sopenharmony_ciThe `'timeout'` event is emitted when there is no activity on the Server for 20531cb0ef41Sopenharmony_cia given number of milliseconds set using `http2server.setTimeout()`. 20541cb0ef41Sopenharmony_ci**Default:** 0 (no timeout) 20551cb0ef41Sopenharmony_ci 20561cb0ef41Sopenharmony_ci#### `server.close([callback])` 20571cb0ef41Sopenharmony_ci 20581cb0ef41Sopenharmony_ci<!-- YAML 20591cb0ef41Sopenharmony_ciadded: v8.4.0 20601cb0ef41Sopenharmony_ci--> 20611cb0ef41Sopenharmony_ci 20621cb0ef41Sopenharmony_ci* `callback` {Function} 20631cb0ef41Sopenharmony_ci 20641cb0ef41Sopenharmony_ciStops the server from establishing new sessions. This does not prevent new 20651cb0ef41Sopenharmony_cirequest streams from being created due to the persistent nature of HTTP/2 20661cb0ef41Sopenharmony_cisessions. To gracefully shut down the server, call [`http2session.close()`][] on 20671cb0ef41Sopenharmony_ciall active sessions. 20681cb0ef41Sopenharmony_ci 20691cb0ef41Sopenharmony_ciIf `callback` is provided, it is not invoked until all active sessions have been 20701cb0ef41Sopenharmony_ciclosed, although the server has already stopped allowing new sessions. See 20711cb0ef41Sopenharmony_ci[`net.Server.close()`][] for more details. 20721cb0ef41Sopenharmony_ci 20731cb0ef41Sopenharmony_ci#### `server.setTimeout([msecs][, callback])` 20741cb0ef41Sopenharmony_ci 20751cb0ef41Sopenharmony_ci<!-- YAML 20761cb0ef41Sopenharmony_ciadded: v8.4.0 20771cb0ef41Sopenharmony_cichanges: 20781cb0ef41Sopenharmony_ci - version: v18.0.0 20791cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 20801cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 20811cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 20821cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 20831cb0ef41Sopenharmony_ci - version: v13.0.0 20841cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27558 20851cb0ef41Sopenharmony_ci description: The default timeout changed from 120s to 0 (no timeout). 20861cb0ef41Sopenharmony_ci--> 20871cb0ef41Sopenharmony_ci 20881cb0ef41Sopenharmony_ci* `msecs` {number} **Default:** 0 (no timeout) 20891cb0ef41Sopenharmony_ci* `callback` {Function} 20901cb0ef41Sopenharmony_ci* Returns: {Http2Server} 20911cb0ef41Sopenharmony_ci 20921cb0ef41Sopenharmony_ciUsed to set the timeout value for http2 server requests, 20931cb0ef41Sopenharmony_ciand sets a callback function that is called when there is no activity 20941cb0ef41Sopenharmony_cion the `Http2Server` after `msecs` milliseconds. 20951cb0ef41Sopenharmony_ci 20961cb0ef41Sopenharmony_ciThe given callback is registered as a listener on the `'timeout'` event. 20971cb0ef41Sopenharmony_ci 20981cb0ef41Sopenharmony_ciIn case if `callback` is not a function, a new `ERR_INVALID_ARG_TYPE` 20991cb0ef41Sopenharmony_cierror will be thrown. 21001cb0ef41Sopenharmony_ci 21011cb0ef41Sopenharmony_ci#### `server.timeout` 21021cb0ef41Sopenharmony_ci 21031cb0ef41Sopenharmony_ci<!-- YAML 21041cb0ef41Sopenharmony_ciadded: v8.4.0 21051cb0ef41Sopenharmony_cichanges: 21061cb0ef41Sopenharmony_ci - version: v13.0.0 21071cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27558 21081cb0ef41Sopenharmony_ci description: The default timeout changed from 120s to 0 (no timeout). 21091cb0ef41Sopenharmony_ci--> 21101cb0ef41Sopenharmony_ci 21111cb0ef41Sopenharmony_ci* {number} Timeout in milliseconds. **Default:** 0 (no timeout) 21121cb0ef41Sopenharmony_ci 21131cb0ef41Sopenharmony_ciThe number of milliseconds of inactivity before a socket is presumed 21141cb0ef41Sopenharmony_cito have timed out. 21151cb0ef41Sopenharmony_ci 21161cb0ef41Sopenharmony_ciA value of `0` will disable the timeout behavior on incoming connections. 21171cb0ef41Sopenharmony_ci 21181cb0ef41Sopenharmony_ciThe socket timeout logic is set up on connection, so changing this 21191cb0ef41Sopenharmony_civalue only affects new connections to the server, not any existing connections. 21201cb0ef41Sopenharmony_ci 21211cb0ef41Sopenharmony_ci#### `server.updateSettings([settings])` 21221cb0ef41Sopenharmony_ci 21231cb0ef41Sopenharmony_ci<!-- YAML 21241cb0ef41Sopenharmony_ciadded: 21251cb0ef41Sopenharmony_ci - v15.1.0 21261cb0ef41Sopenharmony_ci - v14.17.0 21271cb0ef41Sopenharmony_ci--> 21281cb0ef41Sopenharmony_ci 21291cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} 21301cb0ef41Sopenharmony_ci 21311cb0ef41Sopenharmony_ciUsed to update the server with the provided settings. 21321cb0ef41Sopenharmony_ci 21331cb0ef41Sopenharmony_ciThrows `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values. 21341cb0ef41Sopenharmony_ci 21351cb0ef41Sopenharmony_ciThrows `ERR_INVALID_ARG_TYPE` for invalid `settings` argument. 21361cb0ef41Sopenharmony_ci 21371cb0ef41Sopenharmony_ci### Class: `Http2SecureServer` 21381cb0ef41Sopenharmony_ci 21391cb0ef41Sopenharmony_ci<!-- YAML 21401cb0ef41Sopenharmony_ciadded: v8.4.0 21411cb0ef41Sopenharmony_ci--> 21421cb0ef41Sopenharmony_ci 21431cb0ef41Sopenharmony_ci* Extends: {tls.Server} 21441cb0ef41Sopenharmony_ci 21451cb0ef41Sopenharmony_ciInstances of `Http2SecureServer` are created using the 21461cb0ef41Sopenharmony_ci`http2.createSecureServer()` function. The `Http2SecureServer` class is not 21471cb0ef41Sopenharmony_ciexported directly by the `node:http2` module. 21481cb0ef41Sopenharmony_ci 21491cb0ef41Sopenharmony_ci#### Event: `'checkContinue'` 21501cb0ef41Sopenharmony_ci 21511cb0ef41Sopenharmony_ci<!-- YAML 21521cb0ef41Sopenharmony_ciadded: v8.5.0 21531cb0ef41Sopenharmony_ci--> 21541cb0ef41Sopenharmony_ci 21551cb0ef41Sopenharmony_ci* `request` {http2.Http2ServerRequest} 21561cb0ef41Sopenharmony_ci* `response` {http2.Http2ServerResponse} 21571cb0ef41Sopenharmony_ci 21581cb0ef41Sopenharmony_ciIf a [`'request'`][] listener is registered or [`http2.createSecureServer()`][] 21591cb0ef41Sopenharmony_ciis supplied a callback function, the `'checkContinue'` event is emitted each 21601cb0ef41Sopenharmony_citime a request with an HTTP `Expect: 100-continue` is received. If this event 21611cb0ef41Sopenharmony_ciis not listened for, the server will automatically respond with a status 21621cb0ef41Sopenharmony_ci`100 Continue` as appropriate. 21631cb0ef41Sopenharmony_ci 21641cb0ef41Sopenharmony_ciHandling this event involves calling [`response.writeContinue()`][] if the 21651cb0ef41Sopenharmony_ciclient should continue to send the request body, or generating an appropriate 21661cb0ef41Sopenharmony_ciHTTP response (e.g. 400 Bad Request) if the client should not continue to send 21671cb0ef41Sopenharmony_cithe request body. 21681cb0ef41Sopenharmony_ci 21691cb0ef41Sopenharmony_ciWhen this event is emitted and handled, the [`'request'`][] event will 21701cb0ef41Sopenharmony_cinot be emitted. 21711cb0ef41Sopenharmony_ci 21721cb0ef41Sopenharmony_ci#### Event: `'connection'` 21731cb0ef41Sopenharmony_ci 21741cb0ef41Sopenharmony_ci<!-- YAML 21751cb0ef41Sopenharmony_ciadded: v8.4.0 21761cb0ef41Sopenharmony_ci--> 21771cb0ef41Sopenharmony_ci 21781cb0ef41Sopenharmony_ci* `socket` {stream.Duplex} 21791cb0ef41Sopenharmony_ci 21801cb0ef41Sopenharmony_ciThis event is emitted when a new TCP stream is established, before the TLS 21811cb0ef41Sopenharmony_cihandshake begins. `socket` is typically an object of type [`net.Socket`][]. 21821cb0ef41Sopenharmony_ciUsually users will not want to access this event. 21831cb0ef41Sopenharmony_ci 21841cb0ef41Sopenharmony_ciThis event can also be explicitly emitted by users to inject connections 21851cb0ef41Sopenharmony_ciinto the HTTP server. In that case, any [`Duplex`][] stream can be passed. 21861cb0ef41Sopenharmony_ci 21871cb0ef41Sopenharmony_ci#### Event: `'request'` 21881cb0ef41Sopenharmony_ci 21891cb0ef41Sopenharmony_ci<!-- YAML 21901cb0ef41Sopenharmony_ciadded: v8.4.0 21911cb0ef41Sopenharmony_ci--> 21921cb0ef41Sopenharmony_ci 21931cb0ef41Sopenharmony_ci* `request` {http2.Http2ServerRequest} 21941cb0ef41Sopenharmony_ci* `response` {http2.Http2ServerResponse} 21951cb0ef41Sopenharmony_ci 21961cb0ef41Sopenharmony_ciEmitted each time there is a request. There may be multiple requests 21971cb0ef41Sopenharmony_ciper session. See the [Compatibility API][]. 21981cb0ef41Sopenharmony_ci 21991cb0ef41Sopenharmony_ci#### Event: `'session'` 22001cb0ef41Sopenharmony_ci 22011cb0ef41Sopenharmony_ci<!-- YAML 22021cb0ef41Sopenharmony_ciadded: v8.4.0 22031cb0ef41Sopenharmony_ci--> 22041cb0ef41Sopenharmony_ci 22051cb0ef41Sopenharmony_ci* `session` {ServerHttp2Session} 22061cb0ef41Sopenharmony_ci 22071cb0ef41Sopenharmony_ciThe `'session'` event is emitted when a new `Http2Session` is created by the 22081cb0ef41Sopenharmony_ci`Http2SecureServer`. 22091cb0ef41Sopenharmony_ci 22101cb0ef41Sopenharmony_ci#### Event: `'sessionError'` 22111cb0ef41Sopenharmony_ci 22121cb0ef41Sopenharmony_ci<!-- YAML 22131cb0ef41Sopenharmony_ciadded: v8.4.0 22141cb0ef41Sopenharmony_ci--> 22151cb0ef41Sopenharmony_ci 22161cb0ef41Sopenharmony_ci* `error` {Error} 22171cb0ef41Sopenharmony_ci* `session` {ServerHttp2Session} 22181cb0ef41Sopenharmony_ci 22191cb0ef41Sopenharmony_ciThe `'sessionError'` event is emitted when an `'error'` event is emitted by 22201cb0ef41Sopenharmony_cian `Http2Session` object associated with the `Http2SecureServer`. 22211cb0ef41Sopenharmony_ci 22221cb0ef41Sopenharmony_ci#### Event: `'stream'` 22231cb0ef41Sopenharmony_ci 22241cb0ef41Sopenharmony_ci<!-- YAML 22251cb0ef41Sopenharmony_ciadded: v8.4.0 22261cb0ef41Sopenharmony_ci--> 22271cb0ef41Sopenharmony_ci 22281cb0ef41Sopenharmony_ci* `stream` {Http2Stream} A reference to the stream 22291cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} An object describing the headers 22301cb0ef41Sopenharmony_ci* `flags` {number} The associated numeric flags 22311cb0ef41Sopenharmony_ci* `rawHeaders` {Array} An array containing the raw header names followed by 22321cb0ef41Sopenharmony_ci their respective values. 22331cb0ef41Sopenharmony_ci 22341cb0ef41Sopenharmony_ciThe `'stream'` event is emitted when a `'stream'` event has been emitted by 22351cb0ef41Sopenharmony_cian `Http2Session` associated with the server. 22361cb0ef41Sopenharmony_ci 22371cb0ef41Sopenharmony_ciSee also [`Http2Session`'s `'stream'` event][]. 22381cb0ef41Sopenharmony_ci 22391cb0ef41Sopenharmony_ci```js 22401cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 22411cb0ef41Sopenharmony_ciconst { 22421cb0ef41Sopenharmony_ci HTTP2_HEADER_METHOD, 22431cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 22441cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS, 22451cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_TYPE, 22461cb0ef41Sopenharmony_ci} = http2.constants; 22471cb0ef41Sopenharmony_ci 22481cb0ef41Sopenharmony_ciconst options = getOptionsSomehow(); 22491cb0ef41Sopenharmony_ci 22501cb0ef41Sopenharmony_ciconst server = http2.createSecureServer(options); 22511cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers, flags) => { 22521cb0ef41Sopenharmony_ci const method = headers[HTTP2_HEADER_METHOD]; 22531cb0ef41Sopenharmony_ci const path = headers[HTTP2_HEADER_PATH]; 22541cb0ef41Sopenharmony_ci // ... 22551cb0ef41Sopenharmony_ci stream.respond({ 22561cb0ef41Sopenharmony_ci [HTTP2_HEADER_STATUS]: 200, 22571cb0ef41Sopenharmony_ci [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8', 22581cb0ef41Sopenharmony_ci }); 22591cb0ef41Sopenharmony_ci stream.write('hello '); 22601cb0ef41Sopenharmony_ci stream.end('world'); 22611cb0ef41Sopenharmony_ci}); 22621cb0ef41Sopenharmony_ci``` 22631cb0ef41Sopenharmony_ci 22641cb0ef41Sopenharmony_ci#### Event: `'timeout'` 22651cb0ef41Sopenharmony_ci 22661cb0ef41Sopenharmony_ci<!-- YAML 22671cb0ef41Sopenharmony_ciadded: v8.4.0 22681cb0ef41Sopenharmony_ci--> 22691cb0ef41Sopenharmony_ci 22701cb0ef41Sopenharmony_ciThe `'timeout'` event is emitted when there is no activity on the Server for 22711cb0ef41Sopenharmony_cia given number of milliseconds set using `http2secureServer.setTimeout()`. 22721cb0ef41Sopenharmony_ci**Default:** 2 minutes. 22731cb0ef41Sopenharmony_ci 22741cb0ef41Sopenharmony_ci#### Event: `'unknownProtocol'` 22751cb0ef41Sopenharmony_ci 22761cb0ef41Sopenharmony_ci<!-- YAML 22771cb0ef41Sopenharmony_ciadded: v8.4.0 22781cb0ef41Sopenharmony_ci--> 22791cb0ef41Sopenharmony_ci 22801cb0ef41Sopenharmony_ci* `socket` {stream.Duplex} 22811cb0ef41Sopenharmony_ci 22821cb0ef41Sopenharmony_ciThe `'unknownProtocol'` event is emitted when a connecting client fails to 22831cb0ef41Sopenharmony_cinegotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler 22841cb0ef41Sopenharmony_cireceives the socket for handling. If no listener is registered for this event, 22851cb0ef41Sopenharmony_cithe connection is terminated. A timeout may be specified using the 22861cb0ef41Sopenharmony_ci`'unknownProtocolTimeout'` option passed to [`http2.createSecureServer()`][]. 22871cb0ef41Sopenharmony_ciSee the [Compatibility API][]. 22881cb0ef41Sopenharmony_ci 22891cb0ef41Sopenharmony_ci#### `server.close([callback])` 22901cb0ef41Sopenharmony_ci 22911cb0ef41Sopenharmony_ci<!-- YAML 22921cb0ef41Sopenharmony_ciadded: v8.4.0 22931cb0ef41Sopenharmony_ci--> 22941cb0ef41Sopenharmony_ci 22951cb0ef41Sopenharmony_ci* `callback` {Function} 22961cb0ef41Sopenharmony_ci 22971cb0ef41Sopenharmony_ciStops the server from establishing new sessions. This does not prevent new 22981cb0ef41Sopenharmony_cirequest streams from being created due to the persistent nature of HTTP/2 22991cb0ef41Sopenharmony_cisessions. To gracefully shut down the server, call [`http2session.close()`][] on 23001cb0ef41Sopenharmony_ciall active sessions. 23011cb0ef41Sopenharmony_ci 23021cb0ef41Sopenharmony_ciIf `callback` is provided, it is not invoked until all active sessions have been 23031cb0ef41Sopenharmony_ciclosed, although the server has already stopped allowing new sessions. See 23041cb0ef41Sopenharmony_ci[`tls.Server.close()`][] for more details. 23051cb0ef41Sopenharmony_ci 23061cb0ef41Sopenharmony_ci#### `server.setTimeout([msecs][, callback])` 23071cb0ef41Sopenharmony_ci 23081cb0ef41Sopenharmony_ci<!-- YAML 23091cb0ef41Sopenharmony_ciadded: v8.4.0 23101cb0ef41Sopenharmony_cichanges: 23111cb0ef41Sopenharmony_ci - version: v18.0.0 23121cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 23131cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 23141cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 23151cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 23161cb0ef41Sopenharmony_ci--> 23171cb0ef41Sopenharmony_ci 23181cb0ef41Sopenharmony_ci* `msecs` {number} **Default:** `120000` (2 minutes) 23191cb0ef41Sopenharmony_ci* `callback` {Function} 23201cb0ef41Sopenharmony_ci* Returns: {Http2SecureServer} 23211cb0ef41Sopenharmony_ci 23221cb0ef41Sopenharmony_ciUsed to set the timeout value for http2 secure server requests, 23231cb0ef41Sopenharmony_ciand sets a callback function that is called when there is no activity 23241cb0ef41Sopenharmony_cion the `Http2SecureServer` after `msecs` milliseconds. 23251cb0ef41Sopenharmony_ci 23261cb0ef41Sopenharmony_ciThe given callback is registered as a listener on the `'timeout'` event. 23271cb0ef41Sopenharmony_ci 23281cb0ef41Sopenharmony_ciIn case if `callback` is not a function, a new `ERR_INVALID_ARG_TYPE` 23291cb0ef41Sopenharmony_cierror will be thrown. 23301cb0ef41Sopenharmony_ci 23311cb0ef41Sopenharmony_ci#### `server.timeout` 23321cb0ef41Sopenharmony_ci 23331cb0ef41Sopenharmony_ci<!-- YAML 23341cb0ef41Sopenharmony_ciadded: v8.4.0 23351cb0ef41Sopenharmony_cichanges: 23361cb0ef41Sopenharmony_ci - version: v13.0.0 23371cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27558 23381cb0ef41Sopenharmony_ci description: The default timeout changed from 120s to 0 (no timeout). 23391cb0ef41Sopenharmony_ci--> 23401cb0ef41Sopenharmony_ci 23411cb0ef41Sopenharmony_ci* {number} Timeout in milliseconds. **Default:** 0 (no timeout) 23421cb0ef41Sopenharmony_ci 23431cb0ef41Sopenharmony_ciThe number of milliseconds of inactivity before a socket is presumed 23441cb0ef41Sopenharmony_cito have timed out. 23451cb0ef41Sopenharmony_ci 23461cb0ef41Sopenharmony_ciA value of `0` will disable the timeout behavior on incoming connections. 23471cb0ef41Sopenharmony_ci 23481cb0ef41Sopenharmony_ciThe socket timeout logic is set up on connection, so changing this 23491cb0ef41Sopenharmony_civalue only affects new connections to the server, not any existing connections. 23501cb0ef41Sopenharmony_ci 23511cb0ef41Sopenharmony_ci#### `server.updateSettings([settings])` 23521cb0ef41Sopenharmony_ci 23531cb0ef41Sopenharmony_ci<!-- YAML 23541cb0ef41Sopenharmony_ciadded: 23551cb0ef41Sopenharmony_ci - v15.1.0 23561cb0ef41Sopenharmony_ci - v14.17.0 23571cb0ef41Sopenharmony_ci--> 23581cb0ef41Sopenharmony_ci 23591cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} 23601cb0ef41Sopenharmony_ci 23611cb0ef41Sopenharmony_ciUsed to update the server with the provided settings. 23621cb0ef41Sopenharmony_ci 23631cb0ef41Sopenharmony_ciThrows `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values. 23641cb0ef41Sopenharmony_ci 23651cb0ef41Sopenharmony_ciThrows `ERR_INVALID_ARG_TYPE` for invalid `settings` argument. 23661cb0ef41Sopenharmony_ci 23671cb0ef41Sopenharmony_ci### `http2.createServer([options][, onRequestHandler])` 23681cb0ef41Sopenharmony_ci 23691cb0ef41Sopenharmony_ci<!-- YAML 23701cb0ef41Sopenharmony_ciadded: v8.4.0 23711cb0ef41Sopenharmony_cichanges: 23721cb0ef41Sopenharmony_ci - version: 23731cb0ef41Sopenharmony_ci - v15.10.0 23741cb0ef41Sopenharmony_ci - v14.16.0 23751cb0ef41Sopenharmony_ci - v12.21.0 23761cb0ef41Sopenharmony_ci - v10.24.0 23771cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/246 23781cb0ef41Sopenharmony_ci description: Added `unknownProtocolTimeout` option with a default of 10000. 23791cb0ef41Sopenharmony_ci - version: 23801cb0ef41Sopenharmony_ci - v14.4.0 23811cb0ef41Sopenharmony_ci - v12.18.0 23821cb0ef41Sopenharmony_ci - v10.21.0 23831cb0ef41Sopenharmony_ci commit: 3948830ce6408be620b09a70bf66158623022af0 23841cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/204 23851cb0ef41Sopenharmony_ci description: Added `maxSettings` option with a default of 32. 23861cb0ef41Sopenharmony_ci - version: 23871cb0ef41Sopenharmony_ci - v13.3.0 23881cb0ef41Sopenharmony_ci - v12.16.0 23891cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30534 23901cb0ef41Sopenharmony_ci description: Added `maxSessionRejectedStreams` option with a default of 100. 23911cb0ef41Sopenharmony_ci - version: 23921cb0ef41Sopenharmony_ci - v13.3.0 23931cb0ef41Sopenharmony_ci - v12.16.0 23941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30534 23951cb0ef41Sopenharmony_ci description: Added `maxSessionInvalidFrames` option with a default of 1000. 23961cb0ef41Sopenharmony_ci - version: v13.0.0 23971cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29144 23981cb0ef41Sopenharmony_ci description: The `PADDING_STRATEGY_CALLBACK` has been made equivalent to 23991cb0ef41Sopenharmony_ci providing `PADDING_STRATEGY_ALIGNED` and `selectPadding` 24001cb0ef41Sopenharmony_ci has been removed. 24011cb0ef41Sopenharmony_ci - version: v12.4.0 24021cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/27782 24031cb0ef41Sopenharmony_ci description: The `options` parameter now supports `net.createServer()` 24041cb0ef41Sopenharmony_ci options. 24051cb0ef41Sopenharmony_ci - version: v9.6.0 24061cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/15752 24071cb0ef41Sopenharmony_ci description: Added the `Http1IncomingMessage` and `Http1ServerResponse` 24081cb0ef41Sopenharmony_ci option. 24091cb0ef41Sopenharmony_ci - version: v8.9.3 24101cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/17105 24111cb0ef41Sopenharmony_ci description: Added the `maxOutstandingPings` option with a default limit of 24121cb0ef41Sopenharmony_ci 10. 24131cb0ef41Sopenharmony_ci - version: v8.9.3 24141cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16676 24151cb0ef41Sopenharmony_ci description: Added the `maxHeaderListPairs` option with a default limit of 24161cb0ef41Sopenharmony_ci 128 header pairs. 24171cb0ef41Sopenharmony_ci--> 24181cb0ef41Sopenharmony_ci 24191cb0ef41Sopenharmony_ci* `options` {Object} 24201cb0ef41Sopenharmony_ci * `maxDeflateDynamicTableSize` {number} Sets the maximum dynamic table size 24211cb0ef41Sopenharmony_ci for deflating header fields. **Default:** `4Kib`. 24221cb0ef41Sopenharmony_ci * `maxSettings` {number} Sets the maximum number of settings entries per 24231cb0ef41Sopenharmony_ci `SETTINGS` frame. The minimum value allowed is `1`. **Default:** `32`. 24241cb0ef41Sopenharmony_ci * `maxSessionMemory`{number} Sets the maximum memory that the `Http2Session` 24251cb0ef41Sopenharmony_ci is permitted to use. The value is expressed in terms of number of megabytes, 24261cb0ef41Sopenharmony_ci e.g. `1` equal 1 megabyte. The minimum value allowed is `1`. 24271cb0ef41Sopenharmony_ci This is a credit based limit, existing `Http2Stream`s may cause this 24281cb0ef41Sopenharmony_ci limit to be exceeded, but new `Http2Stream` instances will be rejected 24291cb0ef41Sopenharmony_ci while this limit is exceeded. The current number of `Http2Stream` sessions, 24301cb0ef41Sopenharmony_ci the current memory use of the header compression tables, current data 24311cb0ef41Sopenharmony_ci queued to be sent, and unacknowledged `PING` and `SETTINGS` frames are all 24321cb0ef41Sopenharmony_ci counted towards the current limit. **Default:** `10`. 24331cb0ef41Sopenharmony_ci * `maxHeaderListPairs` {number} Sets the maximum number of header entries. 24341cb0ef41Sopenharmony_ci This is similar to [`server.maxHeadersCount`][] or 24351cb0ef41Sopenharmony_ci [`request.maxHeadersCount`][] in the `node:http` module. The minimum value 24361cb0ef41Sopenharmony_ci is `4`. **Default:** `128`. 24371cb0ef41Sopenharmony_ci * `maxOutstandingPings` {number} Sets the maximum number of outstanding, 24381cb0ef41Sopenharmony_ci unacknowledged pings. **Default:** `10`. 24391cb0ef41Sopenharmony_ci * `maxSendHeaderBlockLength` {number} Sets the maximum allowed size for a 24401cb0ef41Sopenharmony_ci serialized, compressed block of headers. Attempts to send headers that 24411cb0ef41Sopenharmony_ci exceed this limit will result in a `'frameError'` event being emitted 24421cb0ef41Sopenharmony_ci and the stream being closed and destroyed. 24431cb0ef41Sopenharmony_ci While this sets the maximum allowed size to the entire block of headers, 24441cb0ef41Sopenharmony_ci `nghttp2` (the internal http2 library) has a limit of `65536` 24451cb0ef41Sopenharmony_ci for each decompressed key/value pair. 24461cb0ef41Sopenharmony_ci * `paddingStrategy` {number} The strategy used for determining the amount of 24471cb0ef41Sopenharmony_ci padding to use for `HEADERS` and `DATA` frames. **Default:** 24481cb0ef41Sopenharmony_ci `http2.constants.PADDING_STRATEGY_NONE`. Value may be one of: 24491cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_NONE`: No padding is applied. 24501cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_MAX`: The maximum amount of padding, 24511cb0ef41Sopenharmony_ci determined by the internal implementation, is applied. 24521cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_ALIGNED`: Attempts to apply enough 24531cb0ef41Sopenharmony_ci padding to ensure that the total frame length, including the 9-byte 24541cb0ef41Sopenharmony_ci header, is a multiple of 8. For each frame, there is a maximum allowed 24551cb0ef41Sopenharmony_ci number of padding bytes that is determined by current flow control state 24561cb0ef41Sopenharmony_ci and settings. If this maximum is less than the calculated amount needed to 24571cb0ef41Sopenharmony_ci ensure alignment, the maximum is used and the total frame length is not 24581cb0ef41Sopenharmony_ci necessarily aligned at 8 bytes. 24591cb0ef41Sopenharmony_ci * `peerMaxConcurrentStreams` {number} Sets the maximum number of concurrent 24601cb0ef41Sopenharmony_ci streams for the remote peer as if a `SETTINGS` frame had been received. Will 24611cb0ef41Sopenharmony_ci be overridden if the remote peer sets its own value for 24621cb0ef41Sopenharmony_ci `maxConcurrentStreams`. **Default:** `100`. 24631cb0ef41Sopenharmony_ci * `maxSessionInvalidFrames` {integer} Sets the maximum number of invalid 24641cb0ef41Sopenharmony_ci frames that will be tolerated before the session is closed. 24651cb0ef41Sopenharmony_ci **Default:** `1000`. 24661cb0ef41Sopenharmony_ci * `maxSessionRejectedStreams` {integer} Sets the maximum number of rejected 24671cb0ef41Sopenharmony_ci upon creation streams that will be tolerated before the session is closed. 24681cb0ef41Sopenharmony_ci Each rejection is associated with an `NGHTTP2_ENHANCE_YOUR_CALM` 24691cb0ef41Sopenharmony_ci error that should tell the peer to not open any more streams, continuing 24701cb0ef41Sopenharmony_ci to open streams is therefore regarded as a sign of a misbehaving peer. 24711cb0ef41Sopenharmony_ci **Default:** `100`. 24721cb0ef41Sopenharmony_ci * `settings` {HTTP/2 Settings Object} The initial settings to send to the 24731cb0ef41Sopenharmony_ci remote peer upon connection. 24741cb0ef41Sopenharmony_ci * `Http1IncomingMessage` {http.IncomingMessage} Specifies the 24751cb0ef41Sopenharmony_ci `IncomingMessage` class to used for HTTP/1 fallback. Useful for extending 24761cb0ef41Sopenharmony_ci the original `http.IncomingMessage`. **Default:** `http.IncomingMessage`. 24771cb0ef41Sopenharmony_ci * `Http1ServerResponse` {http.ServerResponse} Specifies the `ServerResponse` 24781cb0ef41Sopenharmony_ci class to used for HTTP/1 fallback. Useful for extending the original 24791cb0ef41Sopenharmony_ci `http.ServerResponse`. **Default:** `http.ServerResponse`. 24801cb0ef41Sopenharmony_ci * `Http2ServerRequest` {http2.Http2ServerRequest} Specifies the 24811cb0ef41Sopenharmony_ci `Http2ServerRequest` class to use. 24821cb0ef41Sopenharmony_ci Useful for extending the original `Http2ServerRequest`. 24831cb0ef41Sopenharmony_ci **Default:** `Http2ServerRequest`. 24841cb0ef41Sopenharmony_ci * `Http2ServerResponse` {http2.Http2ServerResponse} Specifies the 24851cb0ef41Sopenharmony_ci `Http2ServerResponse` class to use. 24861cb0ef41Sopenharmony_ci Useful for extending the original `Http2ServerResponse`. 24871cb0ef41Sopenharmony_ci **Default:** `Http2ServerResponse`. 24881cb0ef41Sopenharmony_ci * `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that 24891cb0ef41Sopenharmony_ci a server should wait when an [`'unknownProtocol'`][] is emitted. If the 24901cb0ef41Sopenharmony_ci socket has not been destroyed by that time the server will destroy it. 24911cb0ef41Sopenharmony_ci **Default:** `10000`. 24921cb0ef41Sopenharmony_ci * ...: Any [`net.createServer()`][] option can be provided. 24931cb0ef41Sopenharmony_ci* `onRequestHandler` {Function} See [Compatibility API][] 24941cb0ef41Sopenharmony_ci* Returns: {Http2Server} 24951cb0ef41Sopenharmony_ci 24961cb0ef41Sopenharmony_ciReturns a `net.Server` instance that creates and manages `Http2Session` 24971cb0ef41Sopenharmony_ciinstances. 24981cb0ef41Sopenharmony_ci 24991cb0ef41Sopenharmony_ciSince there are no browsers known that support 25001cb0ef41Sopenharmony_ci[unencrypted HTTP/2][HTTP/2 Unencrypted], the use of 25011cb0ef41Sopenharmony_ci[`http2.createSecureServer()`][] is necessary when communicating 25021cb0ef41Sopenharmony_ciwith browser clients. 25031cb0ef41Sopenharmony_ci 25041cb0ef41Sopenharmony_ci```js 25051cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 25061cb0ef41Sopenharmony_ci 25071cb0ef41Sopenharmony_ci// Create an unencrypted HTTP/2 server. 25081cb0ef41Sopenharmony_ci// Since there are no browsers known that support 25091cb0ef41Sopenharmony_ci// unencrypted HTTP/2, the use of `http2.createSecureServer()` 25101cb0ef41Sopenharmony_ci// is necessary when communicating with browser clients. 25111cb0ef41Sopenharmony_ciconst server = http2.createServer(); 25121cb0ef41Sopenharmony_ci 25131cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 25141cb0ef41Sopenharmony_ci stream.respond({ 25151cb0ef41Sopenharmony_ci 'content-type': 'text/html; charset=utf-8', 25161cb0ef41Sopenharmony_ci ':status': 200, 25171cb0ef41Sopenharmony_ci }); 25181cb0ef41Sopenharmony_ci stream.end('<h1>Hello World</h1>'); 25191cb0ef41Sopenharmony_ci}); 25201cb0ef41Sopenharmony_ci 25211cb0ef41Sopenharmony_ciserver.listen(8000); 25221cb0ef41Sopenharmony_ci``` 25231cb0ef41Sopenharmony_ci 25241cb0ef41Sopenharmony_ci### `http2.createSecureServer(options[, onRequestHandler])` 25251cb0ef41Sopenharmony_ci 25261cb0ef41Sopenharmony_ci<!-- YAML 25271cb0ef41Sopenharmony_ciadded: v8.4.0 25281cb0ef41Sopenharmony_cichanges: 25291cb0ef41Sopenharmony_ci - version: 25301cb0ef41Sopenharmony_ci - v15.10.0 25311cb0ef41Sopenharmony_ci - v14.16.0 25321cb0ef41Sopenharmony_ci - v12.21.0 25331cb0ef41Sopenharmony_ci - v10.24.0 25341cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/246 25351cb0ef41Sopenharmony_ci description: Added `unknownProtocolTimeout` option with a default of 10000. 25361cb0ef41Sopenharmony_ci - version: 25371cb0ef41Sopenharmony_ci - v14.4.0 25381cb0ef41Sopenharmony_ci - v12.18.0 25391cb0ef41Sopenharmony_ci - v10.21.0 25401cb0ef41Sopenharmony_ci commit: 3948830ce6408be620b09a70bf66158623022af0 25411cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/204 25421cb0ef41Sopenharmony_ci description: Added `maxSettings` option with a default of 32. 25431cb0ef41Sopenharmony_ci - version: 25441cb0ef41Sopenharmony_ci - v13.3.0 25451cb0ef41Sopenharmony_ci - v12.16.0 25461cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30534 25471cb0ef41Sopenharmony_ci description: Added `maxSessionRejectedStreams` option with a default of 100. 25481cb0ef41Sopenharmony_ci - version: 25491cb0ef41Sopenharmony_ci - v13.3.0 25501cb0ef41Sopenharmony_ci - v12.16.0 25511cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30534 25521cb0ef41Sopenharmony_ci description: Added `maxSessionInvalidFrames` option with a default of 1000. 25531cb0ef41Sopenharmony_ci - version: v13.0.0 25541cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29144 25551cb0ef41Sopenharmony_ci description: The `PADDING_STRATEGY_CALLBACK` has been made equivalent to 25561cb0ef41Sopenharmony_ci providing `PADDING_STRATEGY_ALIGNED` and `selectPadding` 25571cb0ef41Sopenharmony_ci has been removed. 25581cb0ef41Sopenharmony_ci - version: v10.12.0 25591cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/22956 25601cb0ef41Sopenharmony_ci description: Added the `origins` option to automatically send an `ORIGIN` 25611cb0ef41Sopenharmony_ci frame on `Http2Session` startup. 25621cb0ef41Sopenharmony_ci - version: v8.9.3 25631cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/17105 25641cb0ef41Sopenharmony_ci description: Added the `maxOutstandingPings` option with a default limit of 25651cb0ef41Sopenharmony_ci 10. 25661cb0ef41Sopenharmony_ci - version: v8.9.3 25671cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16676 25681cb0ef41Sopenharmony_ci description: Added the `maxHeaderListPairs` option with a default limit of 25691cb0ef41Sopenharmony_ci 128 header pairs. 25701cb0ef41Sopenharmony_ci--> 25711cb0ef41Sopenharmony_ci 25721cb0ef41Sopenharmony_ci* `options` {Object} 25731cb0ef41Sopenharmony_ci * `allowHTTP1` {boolean} Incoming client connections that do not support 25741cb0ef41Sopenharmony_ci HTTP/2 will be downgraded to HTTP/1.x when set to `true`. 25751cb0ef41Sopenharmony_ci See the [`'unknownProtocol'`][] event. See [ALPN negotiation][]. 25761cb0ef41Sopenharmony_ci **Default:** `false`. 25771cb0ef41Sopenharmony_ci * `maxDeflateDynamicTableSize` {number} Sets the maximum dynamic table size 25781cb0ef41Sopenharmony_ci for deflating header fields. **Default:** `4Kib`. 25791cb0ef41Sopenharmony_ci * `maxSettings` {number} Sets the maximum number of settings entries per 25801cb0ef41Sopenharmony_ci `SETTINGS` frame. The minimum value allowed is `1`. **Default:** `32`. 25811cb0ef41Sopenharmony_ci * `maxSessionMemory`{number} Sets the maximum memory that the `Http2Session` 25821cb0ef41Sopenharmony_ci is permitted to use. The value is expressed in terms of number of megabytes, 25831cb0ef41Sopenharmony_ci e.g. `1` equal 1 megabyte. The minimum value allowed is `1`. This is a 25841cb0ef41Sopenharmony_ci credit based limit, existing `Http2Stream`s may cause this 25851cb0ef41Sopenharmony_ci limit to be exceeded, but new `Http2Stream` instances will be rejected 25861cb0ef41Sopenharmony_ci while this limit is exceeded. The current number of `Http2Stream` sessions, 25871cb0ef41Sopenharmony_ci the current memory use of the header compression tables, current data 25881cb0ef41Sopenharmony_ci queued to be sent, and unacknowledged `PING` and `SETTINGS` frames are all 25891cb0ef41Sopenharmony_ci counted towards the current limit. **Default:** `10`. 25901cb0ef41Sopenharmony_ci * `maxHeaderListPairs` {number} Sets the maximum number of header entries. 25911cb0ef41Sopenharmony_ci This is similar to [`server.maxHeadersCount`][] or 25921cb0ef41Sopenharmony_ci [`request.maxHeadersCount`][] in the `node:http` module. The minimum value 25931cb0ef41Sopenharmony_ci is `4`. **Default:** `128`. 25941cb0ef41Sopenharmony_ci * `maxOutstandingPings` {number} Sets the maximum number of outstanding, 25951cb0ef41Sopenharmony_ci unacknowledged pings. **Default:** `10`. 25961cb0ef41Sopenharmony_ci * `maxSendHeaderBlockLength` {number} Sets the maximum allowed size for a 25971cb0ef41Sopenharmony_ci serialized, compressed block of headers. Attempts to send headers that 25981cb0ef41Sopenharmony_ci exceed this limit will result in a `'frameError'` event being emitted 25991cb0ef41Sopenharmony_ci and the stream being closed and destroyed. 26001cb0ef41Sopenharmony_ci * `paddingStrategy` {number} Strategy used for determining the amount of 26011cb0ef41Sopenharmony_ci padding to use for `HEADERS` and `DATA` frames. **Default:** 26021cb0ef41Sopenharmony_ci `http2.constants.PADDING_STRATEGY_NONE`. Value may be one of: 26031cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_NONE`: No padding is applied. 26041cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_MAX`: The maximum amount of padding, 26051cb0ef41Sopenharmony_ci determined by the internal implementation, is applied. 26061cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_ALIGNED`: Attempts to apply enough 26071cb0ef41Sopenharmony_ci padding to ensure that the total frame length, including the 26081cb0ef41Sopenharmony_ci 9-byte header, is a multiple of 8. For each frame, there is a maximum 26091cb0ef41Sopenharmony_ci allowed number of padding bytes that is determined by current flow control 26101cb0ef41Sopenharmony_ci state and settings. If this maximum is less than the calculated amount 26111cb0ef41Sopenharmony_ci needed to ensure alignment, the maximum is used and the total frame length 26121cb0ef41Sopenharmony_ci is not necessarily aligned at 8 bytes. 26131cb0ef41Sopenharmony_ci * `peerMaxConcurrentStreams` {number} Sets the maximum number of concurrent 26141cb0ef41Sopenharmony_ci streams for the remote peer as if a `SETTINGS` frame had been received. Will 26151cb0ef41Sopenharmony_ci be overridden if the remote peer sets its own value for 26161cb0ef41Sopenharmony_ci `maxConcurrentStreams`. **Default:** `100`. 26171cb0ef41Sopenharmony_ci * `maxSessionInvalidFrames` {integer} Sets the maximum number of invalid 26181cb0ef41Sopenharmony_ci frames that will be tolerated before the session is closed. 26191cb0ef41Sopenharmony_ci **Default:** `1000`. 26201cb0ef41Sopenharmony_ci * `maxSessionRejectedStreams` {integer} Sets the maximum number of rejected 26211cb0ef41Sopenharmony_ci upon creation streams that will be tolerated before the session is closed. 26221cb0ef41Sopenharmony_ci Each rejection is associated with an `NGHTTP2_ENHANCE_YOUR_CALM` 26231cb0ef41Sopenharmony_ci error that should tell the peer to not open any more streams, continuing 26241cb0ef41Sopenharmony_ci to open streams is therefore regarded as a sign of a misbehaving peer. 26251cb0ef41Sopenharmony_ci **Default:** `100`. 26261cb0ef41Sopenharmony_ci * `settings` {HTTP/2 Settings Object} The initial settings to send to the 26271cb0ef41Sopenharmony_ci remote peer upon connection. 26281cb0ef41Sopenharmony_ci * ...: Any [`tls.createServer()`][] options can be provided. For 26291cb0ef41Sopenharmony_ci servers, the identity options (`pfx` or `key`/`cert`) are usually required. 26301cb0ef41Sopenharmony_ci * `origins` {string\[]} An array of origin strings to send within an `ORIGIN` 26311cb0ef41Sopenharmony_ci frame immediately following creation of a new server `Http2Session`. 26321cb0ef41Sopenharmony_ci * `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that 26331cb0ef41Sopenharmony_ci a server should wait when an [`'unknownProtocol'`][] event is emitted. If 26341cb0ef41Sopenharmony_ci the socket has not been destroyed by that time the server will destroy it. 26351cb0ef41Sopenharmony_ci **Default:** `10000`. 26361cb0ef41Sopenharmony_ci* `onRequestHandler` {Function} See [Compatibility API][] 26371cb0ef41Sopenharmony_ci* Returns: {Http2SecureServer} 26381cb0ef41Sopenharmony_ci 26391cb0ef41Sopenharmony_ciReturns a `tls.Server` instance that creates and manages `Http2Session` 26401cb0ef41Sopenharmony_ciinstances. 26411cb0ef41Sopenharmony_ci 26421cb0ef41Sopenharmony_ci```js 26431cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 26441cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 26451cb0ef41Sopenharmony_ci 26461cb0ef41Sopenharmony_ciconst options = { 26471cb0ef41Sopenharmony_ci key: fs.readFileSync('server-key.pem'), 26481cb0ef41Sopenharmony_ci cert: fs.readFileSync('server-cert.pem'), 26491cb0ef41Sopenharmony_ci}; 26501cb0ef41Sopenharmony_ci 26511cb0ef41Sopenharmony_ci// Create a secure HTTP/2 server 26521cb0ef41Sopenharmony_ciconst server = http2.createSecureServer(options); 26531cb0ef41Sopenharmony_ci 26541cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 26551cb0ef41Sopenharmony_ci stream.respond({ 26561cb0ef41Sopenharmony_ci 'content-type': 'text/html; charset=utf-8', 26571cb0ef41Sopenharmony_ci ':status': 200, 26581cb0ef41Sopenharmony_ci }); 26591cb0ef41Sopenharmony_ci stream.end('<h1>Hello World</h1>'); 26601cb0ef41Sopenharmony_ci}); 26611cb0ef41Sopenharmony_ci 26621cb0ef41Sopenharmony_ciserver.listen(8443); 26631cb0ef41Sopenharmony_ci``` 26641cb0ef41Sopenharmony_ci 26651cb0ef41Sopenharmony_ci### `http2.connect(authority[, options][, listener])` 26661cb0ef41Sopenharmony_ci 26671cb0ef41Sopenharmony_ci<!-- YAML 26681cb0ef41Sopenharmony_ciadded: v8.4.0 26691cb0ef41Sopenharmony_cichanges: 26701cb0ef41Sopenharmony_ci - version: 26711cb0ef41Sopenharmony_ci - v15.10.0 26721cb0ef41Sopenharmony_ci - v14.16.0 26731cb0ef41Sopenharmony_ci - v12.21.0 26741cb0ef41Sopenharmony_ci - v10.24.0 26751cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/246 26761cb0ef41Sopenharmony_ci description: Added `unknownProtocolTimeout` option with a default of 10000. 26771cb0ef41Sopenharmony_ci - version: 26781cb0ef41Sopenharmony_ci - v14.4.0 26791cb0ef41Sopenharmony_ci - v12.18.0 26801cb0ef41Sopenharmony_ci - v10.21.0 26811cb0ef41Sopenharmony_ci commit: 3948830ce6408be620b09a70bf66158623022af0 26821cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs-private/node-private/pull/204 26831cb0ef41Sopenharmony_ci description: Added `maxSettings` option with a default of 32. 26841cb0ef41Sopenharmony_ci - version: v13.0.0 26851cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29144 26861cb0ef41Sopenharmony_ci description: The `PADDING_STRATEGY_CALLBACK` has been made equivalent to 26871cb0ef41Sopenharmony_ci providing `PADDING_STRATEGY_ALIGNED` and `selectPadding` 26881cb0ef41Sopenharmony_ci has been removed. 26891cb0ef41Sopenharmony_ci - version: v8.9.3 26901cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/17105 26911cb0ef41Sopenharmony_ci description: Added the `maxOutstandingPings` option with a default limit of 26921cb0ef41Sopenharmony_ci 10. 26931cb0ef41Sopenharmony_ci - version: v8.9.3 26941cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16676 26951cb0ef41Sopenharmony_ci description: Added the `maxHeaderListPairs` option with a default limit of 26961cb0ef41Sopenharmony_ci 128 header pairs. 26971cb0ef41Sopenharmony_ci--> 26981cb0ef41Sopenharmony_ci 26991cb0ef41Sopenharmony_ci* `authority` {string|URL} The remote HTTP/2 server to connect to. This must 27001cb0ef41Sopenharmony_ci be in the form of a minimal, valid URL with the `http://` or `https://` 27011cb0ef41Sopenharmony_ci prefix, host name, and IP port (if a non-default port is used). Userinfo 27021cb0ef41Sopenharmony_ci (user ID and password), path, querystring, and fragment details in the 27031cb0ef41Sopenharmony_ci URL will be ignored. 27041cb0ef41Sopenharmony_ci* `options` {Object} 27051cb0ef41Sopenharmony_ci * `maxDeflateDynamicTableSize` {number} Sets the maximum dynamic table size 27061cb0ef41Sopenharmony_ci for deflating header fields. **Default:** `4Kib`. 27071cb0ef41Sopenharmony_ci * `maxSettings` {number} Sets the maximum number of settings entries per 27081cb0ef41Sopenharmony_ci `SETTINGS` frame. The minimum value allowed is `1`. **Default:** `32`. 27091cb0ef41Sopenharmony_ci * `maxSessionMemory`{number} Sets the maximum memory that the `Http2Session` 27101cb0ef41Sopenharmony_ci is permitted to use. The value is expressed in terms of number of megabytes, 27111cb0ef41Sopenharmony_ci e.g. `1` equal 1 megabyte. The minimum value allowed is `1`. 27121cb0ef41Sopenharmony_ci This is a credit based limit, existing `Http2Stream`s may cause this 27131cb0ef41Sopenharmony_ci limit to be exceeded, but new `Http2Stream` instances will be rejected 27141cb0ef41Sopenharmony_ci while this limit is exceeded. The current number of `Http2Stream` sessions, 27151cb0ef41Sopenharmony_ci the current memory use of the header compression tables, current data 27161cb0ef41Sopenharmony_ci queued to be sent, and unacknowledged `PING` and `SETTINGS` frames are all 27171cb0ef41Sopenharmony_ci counted towards the current limit. **Default:** `10`. 27181cb0ef41Sopenharmony_ci * `maxHeaderListPairs` {number} Sets the maximum number of header entries. 27191cb0ef41Sopenharmony_ci This is similar to [`server.maxHeadersCount`][] or 27201cb0ef41Sopenharmony_ci [`request.maxHeadersCount`][] in the `node:http` module. The minimum value 27211cb0ef41Sopenharmony_ci is `1`. **Default:** `128`. 27221cb0ef41Sopenharmony_ci * `maxOutstandingPings` {number} Sets the maximum number of outstanding, 27231cb0ef41Sopenharmony_ci unacknowledged pings. **Default:** `10`. 27241cb0ef41Sopenharmony_ci * `maxReservedRemoteStreams` {number} Sets the maximum number of reserved push 27251cb0ef41Sopenharmony_ci streams the client will accept at any given time. Once the current number of 27261cb0ef41Sopenharmony_ci currently reserved push streams exceeds reaches this limit, new push streams 27271cb0ef41Sopenharmony_ci sent by the server will be automatically rejected. The minimum allowed value 27281cb0ef41Sopenharmony_ci is 0. The maximum allowed value is 2<sup>32</sup>-1. A negative value sets 27291cb0ef41Sopenharmony_ci this option to the maximum allowed value. **Default:** `200`. 27301cb0ef41Sopenharmony_ci * `maxSendHeaderBlockLength` {number} Sets the maximum allowed size for a 27311cb0ef41Sopenharmony_ci serialized, compressed block of headers. Attempts to send headers that 27321cb0ef41Sopenharmony_ci exceed this limit will result in a `'frameError'` event being emitted 27331cb0ef41Sopenharmony_ci and the stream being closed and destroyed. 27341cb0ef41Sopenharmony_ci * `paddingStrategy` {number} Strategy used for determining the amount of 27351cb0ef41Sopenharmony_ci padding to use for `HEADERS` and `DATA` frames. **Default:** 27361cb0ef41Sopenharmony_ci `http2.constants.PADDING_STRATEGY_NONE`. Value may be one of: 27371cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_NONE`: No padding is applied. 27381cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_MAX`: The maximum amount of padding, 27391cb0ef41Sopenharmony_ci determined by the internal implementation, is applied. 27401cb0ef41Sopenharmony_ci * `http2.constants.PADDING_STRATEGY_ALIGNED`: Attempts to apply enough 27411cb0ef41Sopenharmony_ci padding to ensure that the total frame length, including the 27421cb0ef41Sopenharmony_ci 9-byte header, is a multiple of 8. For each frame, there is a maximum 27431cb0ef41Sopenharmony_ci allowed number of padding bytes that is determined by current flow control 27441cb0ef41Sopenharmony_ci state and settings. If this maximum is less than the calculated amount 27451cb0ef41Sopenharmony_ci needed to ensure alignment, the maximum is used and the total frame length 27461cb0ef41Sopenharmony_ci is not necessarily aligned at 8 bytes. 27471cb0ef41Sopenharmony_ci * `peerMaxConcurrentStreams` {number} Sets the maximum number of concurrent 27481cb0ef41Sopenharmony_ci streams for the remote peer as if a `SETTINGS` frame had been received. Will 27491cb0ef41Sopenharmony_ci be overridden if the remote peer sets its own value for 27501cb0ef41Sopenharmony_ci `maxConcurrentStreams`. **Default:** `100`. 27511cb0ef41Sopenharmony_ci * `protocol` {string} The protocol to connect with, if not set in the 27521cb0ef41Sopenharmony_ci `authority`. Value may be either `'http:'` or `'https:'`. **Default:** 27531cb0ef41Sopenharmony_ci `'https:'` 27541cb0ef41Sopenharmony_ci * `settings` {HTTP/2 Settings Object} The initial settings to send to the 27551cb0ef41Sopenharmony_ci remote peer upon connection. 27561cb0ef41Sopenharmony_ci * `createConnection` {Function} An optional callback that receives the `URL` 27571cb0ef41Sopenharmony_ci instance passed to `connect` and the `options` object, and returns any 27581cb0ef41Sopenharmony_ci [`Duplex`][] stream that is to be used as the connection for this session. 27591cb0ef41Sopenharmony_ci * ...: Any [`net.connect()`][] or [`tls.connect()`][] options can be provided. 27601cb0ef41Sopenharmony_ci * `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that 27611cb0ef41Sopenharmony_ci a server should wait when an [`'unknownProtocol'`][] event is emitted. If 27621cb0ef41Sopenharmony_ci the socket has not been destroyed by that time the server will destroy it. 27631cb0ef41Sopenharmony_ci **Default:** `10000`. 27641cb0ef41Sopenharmony_ci* `listener` {Function} Will be registered as a one-time listener of the 27651cb0ef41Sopenharmony_ci [`'connect'`][] event. 27661cb0ef41Sopenharmony_ci* Returns: {ClientHttp2Session} 27671cb0ef41Sopenharmony_ci 27681cb0ef41Sopenharmony_ciReturns a `ClientHttp2Session` instance. 27691cb0ef41Sopenharmony_ci 27701cb0ef41Sopenharmony_ci```js 27711cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 27721cb0ef41Sopenharmony_ciconst client = http2.connect('https://localhost:1234'); 27731cb0ef41Sopenharmony_ci 27741cb0ef41Sopenharmony_ci/* Use the client */ 27751cb0ef41Sopenharmony_ci 27761cb0ef41Sopenharmony_ciclient.close(); 27771cb0ef41Sopenharmony_ci``` 27781cb0ef41Sopenharmony_ci 27791cb0ef41Sopenharmony_ci### `http2.constants` 27801cb0ef41Sopenharmony_ci 27811cb0ef41Sopenharmony_ci<!-- YAML 27821cb0ef41Sopenharmony_ciadded: v8.4.0 27831cb0ef41Sopenharmony_ci--> 27841cb0ef41Sopenharmony_ci 27851cb0ef41Sopenharmony_ci#### Error codes for `RST_STREAM` and `GOAWAY` 27861cb0ef41Sopenharmony_ci 27871cb0ef41Sopenharmony_ci| Value | Name | Constant | 27881cb0ef41Sopenharmony_ci| ------ | ------------------- | --------------------------------------------- | 27891cb0ef41Sopenharmony_ci| `0x00` | No Error | `http2.constants.NGHTTP2_NO_ERROR` | 27901cb0ef41Sopenharmony_ci| `0x01` | Protocol Error | `http2.constants.NGHTTP2_PROTOCOL_ERROR` | 27911cb0ef41Sopenharmony_ci| `0x02` | Internal Error | `http2.constants.NGHTTP2_INTERNAL_ERROR` | 27921cb0ef41Sopenharmony_ci| `0x03` | Flow Control Error | `http2.constants.NGHTTP2_FLOW_CONTROL_ERROR` | 27931cb0ef41Sopenharmony_ci| `0x04` | Settings Timeout | `http2.constants.NGHTTP2_SETTINGS_TIMEOUT` | 27941cb0ef41Sopenharmony_ci| `0x05` | Stream Closed | `http2.constants.NGHTTP2_STREAM_CLOSED` | 27951cb0ef41Sopenharmony_ci| `0x06` | Frame Size Error | `http2.constants.NGHTTP2_FRAME_SIZE_ERROR` | 27961cb0ef41Sopenharmony_ci| `0x07` | Refused Stream | `http2.constants.NGHTTP2_REFUSED_STREAM` | 27971cb0ef41Sopenharmony_ci| `0x08` | Cancel | `http2.constants.NGHTTP2_CANCEL` | 27981cb0ef41Sopenharmony_ci| `0x09` | Compression Error | `http2.constants.NGHTTP2_COMPRESSION_ERROR` | 27991cb0ef41Sopenharmony_ci| `0x0a` | Connect Error | `http2.constants.NGHTTP2_CONNECT_ERROR` | 28001cb0ef41Sopenharmony_ci| `0x0b` | Enhance Your Calm | `http2.constants.NGHTTP2_ENHANCE_YOUR_CALM` | 28011cb0ef41Sopenharmony_ci| `0x0c` | Inadequate Security | `http2.constants.NGHTTP2_INADEQUATE_SECURITY` | 28021cb0ef41Sopenharmony_ci| `0x0d` | HTTP/1.1 Required | `http2.constants.NGHTTP2_HTTP_1_1_REQUIRED` | 28031cb0ef41Sopenharmony_ci 28041cb0ef41Sopenharmony_ciThe `'timeout'` event is emitted when there is no activity on the Server for 28051cb0ef41Sopenharmony_cia given number of milliseconds set using `http2server.setTimeout()`. 28061cb0ef41Sopenharmony_ci 28071cb0ef41Sopenharmony_ci### `http2.getDefaultSettings()` 28081cb0ef41Sopenharmony_ci 28091cb0ef41Sopenharmony_ci<!-- YAML 28101cb0ef41Sopenharmony_ciadded: v8.4.0 28111cb0ef41Sopenharmony_ci--> 28121cb0ef41Sopenharmony_ci 28131cb0ef41Sopenharmony_ci* Returns: {HTTP/2 Settings Object} 28141cb0ef41Sopenharmony_ci 28151cb0ef41Sopenharmony_ciReturns an object containing the default settings for an `Http2Session` 28161cb0ef41Sopenharmony_ciinstance. This method returns a new object instance every time it is called 28171cb0ef41Sopenharmony_ciso instances returned may be safely modified for use. 28181cb0ef41Sopenharmony_ci 28191cb0ef41Sopenharmony_ci### `http2.getPackedSettings([settings])` 28201cb0ef41Sopenharmony_ci 28211cb0ef41Sopenharmony_ci<!-- YAML 28221cb0ef41Sopenharmony_ciadded: v8.4.0 28231cb0ef41Sopenharmony_ci--> 28241cb0ef41Sopenharmony_ci 28251cb0ef41Sopenharmony_ci* `settings` {HTTP/2 Settings Object} 28261cb0ef41Sopenharmony_ci* Returns: {Buffer} 28271cb0ef41Sopenharmony_ci 28281cb0ef41Sopenharmony_ciReturns a `Buffer` instance containing serialized representation of the given 28291cb0ef41Sopenharmony_ciHTTP/2 settings as specified in the [HTTP/2][] specification. This is intended 28301cb0ef41Sopenharmony_cifor use with the `HTTP2-Settings` header field. 28311cb0ef41Sopenharmony_ci 28321cb0ef41Sopenharmony_ci```js 28331cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 28341cb0ef41Sopenharmony_ci 28351cb0ef41Sopenharmony_ciconst packed = http2.getPackedSettings({ enablePush: false }); 28361cb0ef41Sopenharmony_ci 28371cb0ef41Sopenharmony_ciconsole.log(packed.toString('base64')); 28381cb0ef41Sopenharmony_ci// Prints: AAIAAAAA 28391cb0ef41Sopenharmony_ci``` 28401cb0ef41Sopenharmony_ci 28411cb0ef41Sopenharmony_ci### `http2.getUnpackedSettings(buf)` 28421cb0ef41Sopenharmony_ci 28431cb0ef41Sopenharmony_ci<!-- YAML 28441cb0ef41Sopenharmony_ciadded: v8.4.0 28451cb0ef41Sopenharmony_ci--> 28461cb0ef41Sopenharmony_ci 28471cb0ef41Sopenharmony_ci* `buf` {Buffer|TypedArray} The packed settings. 28481cb0ef41Sopenharmony_ci* Returns: {HTTP/2 Settings Object} 28491cb0ef41Sopenharmony_ci 28501cb0ef41Sopenharmony_ciReturns a [HTTP/2 Settings Object][] containing the deserialized settings from 28511cb0ef41Sopenharmony_cithe given `Buffer` as generated by `http2.getPackedSettings()`. 28521cb0ef41Sopenharmony_ci 28531cb0ef41Sopenharmony_ci### `http2.sensitiveHeaders` 28541cb0ef41Sopenharmony_ci 28551cb0ef41Sopenharmony_ci<!-- YAML 28561cb0ef41Sopenharmony_ciadded: 28571cb0ef41Sopenharmony_ci - v15.0.0 28581cb0ef41Sopenharmony_ci - v14.18.0 28591cb0ef41Sopenharmony_ci--> 28601cb0ef41Sopenharmony_ci 28611cb0ef41Sopenharmony_ci* {symbol} 28621cb0ef41Sopenharmony_ci 28631cb0ef41Sopenharmony_ciThis symbol can be set as a property on the HTTP/2 headers object with an array 28641cb0ef41Sopenharmony_civalue in order to provide a list of headers considered sensitive. 28651cb0ef41Sopenharmony_ciSee [Sensitive headers][] for more details. 28661cb0ef41Sopenharmony_ci 28671cb0ef41Sopenharmony_ci### Headers object 28681cb0ef41Sopenharmony_ci 28691cb0ef41Sopenharmony_ciHeaders are represented as own-properties on JavaScript objects. The property 28701cb0ef41Sopenharmony_cikeys will be serialized to lower-case. Property values should be strings (if 28711cb0ef41Sopenharmony_cithey are not they will be coerced to strings) or an `Array` of strings (in order 28721cb0ef41Sopenharmony_cito send more than one value per header field). 28731cb0ef41Sopenharmony_ci 28741cb0ef41Sopenharmony_ci```js 28751cb0ef41Sopenharmony_ciconst headers = { 28761cb0ef41Sopenharmony_ci ':status': '200', 28771cb0ef41Sopenharmony_ci 'content-type': 'text-plain', 28781cb0ef41Sopenharmony_ci 'ABC': ['has', 'more', 'than', 'one', 'value'], 28791cb0ef41Sopenharmony_ci}; 28801cb0ef41Sopenharmony_ci 28811cb0ef41Sopenharmony_cistream.respond(headers); 28821cb0ef41Sopenharmony_ci``` 28831cb0ef41Sopenharmony_ci 28841cb0ef41Sopenharmony_ciHeader objects passed to callback functions will have a `null` prototype. This 28851cb0ef41Sopenharmony_cimeans that normal JavaScript object methods such as 28861cb0ef41Sopenharmony_ci`Object.prototype.toString()` and `Object.prototype.hasOwnProperty()` will 28871cb0ef41Sopenharmony_cinot work. 28881cb0ef41Sopenharmony_ci 28891cb0ef41Sopenharmony_ciFor incoming headers: 28901cb0ef41Sopenharmony_ci 28911cb0ef41Sopenharmony_ci* The `:status` header is converted to `number`. 28921cb0ef41Sopenharmony_ci* Duplicates of `:status`, `:method`, `:authority`, `:scheme`, `:path`, 28931cb0ef41Sopenharmony_ci `:protocol`, `age`, `authorization`, `access-control-allow-credentials`, 28941cb0ef41Sopenharmony_ci `access-control-max-age`, `access-control-request-method`, `content-encoding`, 28951cb0ef41Sopenharmony_ci `content-language`, `content-length`, `content-location`, `content-md5`, 28961cb0ef41Sopenharmony_ci `content-range`, `content-type`, `date`, `dnt`, `etag`, `expires`, `from`, 28971cb0ef41Sopenharmony_ci `host`, `if-match`, `if-modified-since`, `if-none-match`, `if-range`, 28981cb0ef41Sopenharmony_ci `if-unmodified-since`, `last-modified`, `location`, `max-forwards`, 28991cb0ef41Sopenharmony_ci `proxy-authorization`, `range`, `referer`,`retry-after`, `tk`, 29001cb0ef41Sopenharmony_ci `upgrade-insecure-requests`, `user-agent` or `x-content-type-options` are 29011cb0ef41Sopenharmony_ci discarded. 29021cb0ef41Sopenharmony_ci* `set-cookie` is always an array. Duplicates are added to the array. 29031cb0ef41Sopenharmony_ci* For duplicate `cookie` headers, the values are joined together with '; '. 29041cb0ef41Sopenharmony_ci* For all other headers, the values are joined together with ', '. 29051cb0ef41Sopenharmony_ci 29061cb0ef41Sopenharmony_ci```js 29071cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 29081cb0ef41Sopenharmony_ciconst server = http2.createServer(); 29091cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 29101cb0ef41Sopenharmony_ci console.log(headers[':path']); 29111cb0ef41Sopenharmony_ci console.log(headers.ABC); 29121cb0ef41Sopenharmony_ci}); 29131cb0ef41Sopenharmony_ci``` 29141cb0ef41Sopenharmony_ci 29151cb0ef41Sopenharmony_ci#### Sensitive headers 29161cb0ef41Sopenharmony_ci 29171cb0ef41Sopenharmony_ciHTTP2 headers can be marked as sensitive, which means that the HTTP/2 29181cb0ef41Sopenharmony_ciheader compression algorithm will never index them. This can make sense for 29191cb0ef41Sopenharmony_ciheader values with low entropy and that may be considered valuable to an 29201cb0ef41Sopenharmony_ciattacker, for example `Cookie` or `Authorization`. To achieve this, add 29211cb0ef41Sopenharmony_cithe header name to the `[http2.sensitiveHeaders]` property as an array: 29221cb0ef41Sopenharmony_ci 29231cb0ef41Sopenharmony_ci```js 29241cb0ef41Sopenharmony_ciconst headers = { 29251cb0ef41Sopenharmony_ci ':status': '200', 29261cb0ef41Sopenharmony_ci 'content-type': 'text-plain', 29271cb0ef41Sopenharmony_ci 'cookie': 'some-cookie', 29281cb0ef41Sopenharmony_ci 'other-sensitive-header': 'very secret data', 29291cb0ef41Sopenharmony_ci [http2.sensitiveHeaders]: ['cookie', 'other-sensitive-header'], 29301cb0ef41Sopenharmony_ci}; 29311cb0ef41Sopenharmony_ci 29321cb0ef41Sopenharmony_cistream.respond(headers); 29331cb0ef41Sopenharmony_ci``` 29341cb0ef41Sopenharmony_ci 29351cb0ef41Sopenharmony_ciFor some headers, such as `Authorization` and short `Cookie` headers, 29361cb0ef41Sopenharmony_cithis flag is set automatically. 29371cb0ef41Sopenharmony_ci 29381cb0ef41Sopenharmony_ciThis property is also set for received headers. It will contain the names of 29391cb0ef41Sopenharmony_ciall headers marked as sensitive, including ones marked that way automatically. 29401cb0ef41Sopenharmony_ci 29411cb0ef41Sopenharmony_ci### Settings object 29421cb0ef41Sopenharmony_ci 29431cb0ef41Sopenharmony_ci<!-- YAML 29441cb0ef41Sopenharmony_ciadded: v8.4.0 29451cb0ef41Sopenharmony_cichanges: 29461cb0ef41Sopenharmony_ci - version: v12.12.0 29471cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/29833 29481cb0ef41Sopenharmony_ci description: The `maxConcurrentStreams` setting is stricter. 29491cb0ef41Sopenharmony_ci - version: v8.9.3 29501cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/16676 29511cb0ef41Sopenharmony_ci description: The `maxHeaderListSize` setting is now strictly enforced. 29521cb0ef41Sopenharmony_ci--> 29531cb0ef41Sopenharmony_ci 29541cb0ef41Sopenharmony_ciThe `http2.getDefaultSettings()`, `http2.getPackedSettings()`, 29551cb0ef41Sopenharmony_ci`http2.createServer()`, `http2.createSecureServer()`, 29561cb0ef41Sopenharmony_ci`http2session.settings()`, `http2session.localSettings`, and 29571cb0ef41Sopenharmony_ci`http2session.remoteSettings` APIs either return or receive as input an 29581cb0ef41Sopenharmony_ciobject that defines configuration settings for an `Http2Session` object. 29591cb0ef41Sopenharmony_ciThese objects are ordinary JavaScript objects containing the following 29601cb0ef41Sopenharmony_ciproperties. 29611cb0ef41Sopenharmony_ci 29621cb0ef41Sopenharmony_ci* `headerTableSize` {number} Specifies the maximum number of bytes used for 29631cb0ef41Sopenharmony_ci header compression. The minimum allowed value is 0. The maximum allowed value 29641cb0ef41Sopenharmony_ci is 2<sup>32</sup>-1. **Default:** `4096`. 29651cb0ef41Sopenharmony_ci* `enablePush` {boolean} Specifies `true` if HTTP/2 Push Streams are to be 29661cb0ef41Sopenharmony_ci permitted on the `Http2Session` instances. **Default:** `true`. 29671cb0ef41Sopenharmony_ci* `initialWindowSize` {number} Specifies the _sender's_ initial window size in 29681cb0ef41Sopenharmony_ci bytes for stream-level flow control. The minimum allowed value is 0. The 29691cb0ef41Sopenharmony_ci maximum allowed value is 2<sup>32</sup>-1. **Default:** `65535`. 29701cb0ef41Sopenharmony_ci* `maxFrameSize` {number} Specifies the size in bytes of the largest frame 29711cb0ef41Sopenharmony_ci payload. The minimum allowed value is 16,384. The maximum allowed value is 29721cb0ef41Sopenharmony_ci 2<sup>24</sup>-1. **Default:** `16384`. 29731cb0ef41Sopenharmony_ci* `maxConcurrentStreams` {number} Specifies the maximum number of concurrent 29741cb0ef41Sopenharmony_ci streams permitted on an `Http2Session`. There is no default value which 29751cb0ef41Sopenharmony_ci implies, at least theoretically, 2<sup>32</sup>-1 streams may be open 29761cb0ef41Sopenharmony_ci concurrently at any given time in an `Http2Session`. The minimum value 29771cb0ef41Sopenharmony_ci is 0. The maximum allowed value is 2<sup>32</sup>-1. **Default:** 29781cb0ef41Sopenharmony_ci `4294967295`. 29791cb0ef41Sopenharmony_ci* `maxHeaderListSize` {number} Specifies the maximum size (uncompressed octets) 29801cb0ef41Sopenharmony_ci of header list that will be accepted. The minimum allowed value is 0. The 29811cb0ef41Sopenharmony_ci maximum allowed value is 2<sup>32</sup>-1. **Default:** `65535`. 29821cb0ef41Sopenharmony_ci* `maxHeaderSize` {number} Alias for `maxHeaderListSize`. 29831cb0ef41Sopenharmony_ci* `enableConnectProtocol`{boolean} Specifies `true` if the "Extended Connect 29841cb0ef41Sopenharmony_ci Protocol" defined by [RFC 8441][] is to be enabled. This setting is only 29851cb0ef41Sopenharmony_ci meaningful if sent by the server. Once the `enableConnectProtocol` setting 29861cb0ef41Sopenharmony_ci has been enabled for a given `Http2Session`, it cannot be disabled. 29871cb0ef41Sopenharmony_ci **Default:** `false`. 29881cb0ef41Sopenharmony_ci 29891cb0ef41Sopenharmony_ciAll additional properties on the settings object are ignored. 29901cb0ef41Sopenharmony_ci 29911cb0ef41Sopenharmony_ci### Error handling 29921cb0ef41Sopenharmony_ci 29931cb0ef41Sopenharmony_ciThere are several types of error conditions that may arise when using the 29941cb0ef41Sopenharmony_ci`node:http2` module: 29951cb0ef41Sopenharmony_ci 29961cb0ef41Sopenharmony_ciValidation errors occur when an incorrect argument, option, or setting value is 29971cb0ef41Sopenharmony_cipassed in. These will always be reported by a synchronous `throw`. 29981cb0ef41Sopenharmony_ci 29991cb0ef41Sopenharmony_ciState errors occur when an action is attempted at an incorrect time (for 30001cb0ef41Sopenharmony_ciinstance, attempting to send data on a stream after it has closed). These will 30011cb0ef41Sopenharmony_cibe reported using either a synchronous `throw` or via an `'error'` event on 30021cb0ef41Sopenharmony_cithe `Http2Stream`, `Http2Session` or HTTP/2 Server objects, depending on where 30031cb0ef41Sopenharmony_ciand when the error occurs. 30041cb0ef41Sopenharmony_ci 30051cb0ef41Sopenharmony_ciInternal errors occur when an HTTP/2 session fails unexpectedly. These will be 30061cb0ef41Sopenharmony_cireported via an `'error'` event on the `Http2Session` or HTTP/2 Server objects. 30071cb0ef41Sopenharmony_ci 30081cb0ef41Sopenharmony_ciProtocol errors occur when various HTTP/2 protocol constraints are violated. 30091cb0ef41Sopenharmony_ciThese will be reported using either a synchronous `throw` or via an `'error'` 30101cb0ef41Sopenharmony_cievent on the `Http2Stream`, `Http2Session` or HTTP/2 Server objects, depending 30111cb0ef41Sopenharmony_cion where and when the error occurs. 30121cb0ef41Sopenharmony_ci 30131cb0ef41Sopenharmony_ci### Invalid character handling in header names and values 30141cb0ef41Sopenharmony_ci 30151cb0ef41Sopenharmony_ciThe HTTP/2 implementation applies stricter handling of invalid characters in 30161cb0ef41Sopenharmony_ciHTTP header names and values than the HTTP/1 implementation. 30171cb0ef41Sopenharmony_ci 30181cb0ef41Sopenharmony_ciHeader field names are _case-insensitive_ and are transmitted over the wire 30191cb0ef41Sopenharmony_cistrictly as lower-case strings. The API provided by Node.js allows header 30201cb0ef41Sopenharmony_cinames to be set as mixed-case strings (e.g. `Content-Type`) but will convert 30211cb0ef41Sopenharmony_cithose to lower-case (e.g. `content-type`) upon transmission. 30221cb0ef41Sopenharmony_ci 30231cb0ef41Sopenharmony_ciHeader field-names _must only_ contain one or more of the following ASCII 30241cb0ef41Sopenharmony_cicharacters: `a`-`z`, `A`-`Z`, `0`-`9`, `!`, `#`, `$`, `%`, `&`, `'`, `*`, `+`, 30251cb0ef41Sopenharmony_ci`-`, `.`, `^`, `_`, `` ` `` (backtick), `|`, and `~`. 30261cb0ef41Sopenharmony_ci 30271cb0ef41Sopenharmony_ciUsing invalid characters within an HTTP header field name will cause the 30281cb0ef41Sopenharmony_cistream to be closed with a protocol error being reported. 30291cb0ef41Sopenharmony_ci 30301cb0ef41Sopenharmony_ciHeader field values are handled with more leniency but _should_ not contain 30311cb0ef41Sopenharmony_cinew-line or carriage return characters and _should_ be limited to US-ASCII 30321cb0ef41Sopenharmony_cicharacters, per the requirements of the HTTP specification. 30331cb0ef41Sopenharmony_ci 30341cb0ef41Sopenharmony_ci### Push streams on the client 30351cb0ef41Sopenharmony_ci 30361cb0ef41Sopenharmony_ciTo receive pushed streams on the client, set a listener for the `'stream'` 30371cb0ef41Sopenharmony_cievent on the `ClientHttp2Session`: 30381cb0ef41Sopenharmony_ci 30391cb0ef41Sopenharmony_ci```js 30401cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 30411cb0ef41Sopenharmony_ci 30421cb0ef41Sopenharmony_ciconst client = http2.connect('http://localhost'); 30431cb0ef41Sopenharmony_ci 30441cb0ef41Sopenharmony_ciclient.on('stream', (pushedStream, requestHeaders) => { 30451cb0ef41Sopenharmony_ci pushedStream.on('push', (responseHeaders) => { 30461cb0ef41Sopenharmony_ci // Process response headers 30471cb0ef41Sopenharmony_ci }); 30481cb0ef41Sopenharmony_ci pushedStream.on('data', (chunk) => { /* handle pushed data */ }); 30491cb0ef41Sopenharmony_ci}); 30501cb0ef41Sopenharmony_ci 30511cb0ef41Sopenharmony_ciconst req = client.request({ ':path': '/' }); 30521cb0ef41Sopenharmony_ci``` 30531cb0ef41Sopenharmony_ci 30541cb0ef41Sopenharmony_ci### Supporting the `CONNECT` method 30551cb0ef41Sopenharmony_ci 30561cb0ef41Sopenharmony_ciThe `CONNECT` method is used to allow an HTTP/2 server to be used as a proxy 30571cb0ef41Sopenharmony_cifor TCP/IP connections. 30581cb0ef41Sopenharmony_ci 30591cb0ef41Sopenharmony_ciA simple TCP Server: 30601cb0ef41Sopenharmony_ci 30611cb0ef41Sopenharmony_ci```js 30621cb0ef41Sopenharmony_ciconst net = require('node:net'); 30631cb0ef41Sopenharmony_ci 30641cb0ef41Sopenharmony_ciconst server = net.createServer((socket) => { 30651cb0ef41Sopenharmony_ci let name = ''; 30661cb0ef41Sopenharmony_ci socket.setEncoding('utf8'); 30671cb0ef41Sopenharmony_ci socket.on('data', (chunk) => name += chunk); 30681cb0ef41Sopenharmony_ci socket.on('end', () => socket.end(`hello ${name}`)); 30691cb0ef41Sopenharmony_ci}); 30701cb0ef41Sopenharmony_ci 30711cb0ef41Sopenharmony_ciserver.listen(8000); 30721cb0ef41Sopenharmony_ci``` 30731cb0ef41Sopenharmony_ci 30741cb0ef41Sopenharmony_ciAn HTTP/2 CONNECT proxy: 30751cb0ef41Sopenharmony_ci 30761cb0ef41Sopenharmony_ci```js 30771cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 30781cb0ef41Sopenharmony_ciconst { NGHTTP2_REFUSED_STREAM } = http2.constants; 30791cb0ef41Sopenharmony_ciconst net = require('node:net'); 30801cb0ef41Sopenharmony_ci 30811cb0ef41Sopenharmony_ciconst proxy = http2.createServer(); 30821cb0ef41Sopenharmony_ciproxy.on('stream', (stream, headers) => { 30831cb0ef41Sopenharmony_ci if (headers[':method'] !== 'CONNECT') { 30841cb0ef41Sopenharmony_ci // Only accept CONNECT requests 30851cb0ef41Sopenharmony_ci stream.close(NGHTTP2_REFUSED_STREAM); 30861cb0ef41Sopenharmony_ci return; 30871cb0ef41Sopenharmony_ci } 30881cb0ef41Sopenharmony_ci const auth = new URL(`tcp://${headers[':authority']}`); 30891cb0ef41Sopenharmony_ci // It's a very good idea to verify that hostname and port are 30901cb0ef41Sopenharmony_ci // things this proxy should be connecting to. 30911cb0ef41Sopenharmony_ci const socket = net.connect(auth.port, auth.hostname, () => { 30921cb0ef41Sopenharmony_ci stream.respond(); 30931cb0ef41Sopenharmony_ci socket.pipe(stream); 30941cb0ef41Sopenharmony_ci stream.pipe(socket); 30951cb0ef41Sopenharmony_ci }); 30961cb0ef41Sopenharmony_ci socket.on('error', (error) => { 30971cb0ef41Sopenharmony_ci stream.close(http2.constants.NGHTTP2_CONNECT_ERROR); 30981cb0ef41Sopenharmony_ci }); 30991cb0ef41Sopenharmony_ci}); 31001cb0ef41Sopenharmony_ci 31011cb0ef41Sopenharmony_ciproxy.listen(8001); 31021cb0ef41Sopenharmony_ci``` 31031cb0ef41Sopenharmony_ci 31041cb0ef41Sopenharmony_ciAn HTTP/2 CONNECT client: 31051cb0ef41Sopenharmony_ci 31061cb0ef41Sopenharmony_ci```js 31071cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 31081cb0ef41Sopenharmony_ci 31091cb0ef41Sopenharmony_ciconst client = http2.connect('http://localhost:8001'); 31101cb0ef41Sopenharmony_ci 31111cb0ef41Sopenharmony_ci// Must not specify the ':path' and ':scheme' headers 31121cb0ef41Sopenharmony_ci// for CONNECT requests or an error will be thrown. 31131cb0ef41Sopenharmony_ciconst req = client.request({ 31141cb0ef41Sopenharmony_ci ':method': 'CONNECT', 31151cb0ef41Sopenharmony_ci ':authority': 'localhost:8000', 31161cb0ef41Sopenharmony_ci}); 31171cb0ef41Sopenharmony_ci 31181cb0ef41Sopenharmony_cireq.on('response', (headers) => { 31191cb0ef41Sopenharmony_ci console.log(headers[http2.constants.HTTP2_HEADER_STATUS]); 31201cb0ef41Sopenharmony_ci}); 31211cb0ef41Sopenharmony_cilet data = ''; 31221cb0ef41Sopenharmony_cireq.setEncoding('utf8'); 31231cb0ef41Sopenharmony_cireq.on('data', (chunk) => data += chunk); 31241cb0ef41Sopenharmony_cireq.on('end', () => { 31251cb0ef41Sopenharmony_ci console.log(`The server says: ${data}`); 31261cb0ef41Sopenharmony_ci client.close(); 31271cb0ef41Sopenharmony_ci}); 31281cb0ef41Sopenharmony_cireq.end('Jane'); 31291cb0ef41Sopenharmony_ci``` 31301cb0ef41Sopenharmony_ci 31311cb0ef41Sopenharmony_ci### The extended `CONNECT` protocol 31321cb0ef41Sopenharmony_ci 31331cb0ef41Sopenharmony_ci[RFC 8441][] defines an "Extended CONNECT Protocol" extension to HTTP/2 that 31341cb0ef41Sopenharmony_cimay be used to bootstrap the use of an `Http2Stream` using the `CONNECT` 31351cb0ef41Sopenharmony_cimethod as a tunnel for other communication protocols (such as WebSockets). 31361cb0ef41Sopenharmony_ci 31371cb0ef41Sopenharmony_ciThe use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using 31381cb0ef41Sopenharmony_cithe `enableConnectProtocol` setting: 31391cb0ef41Sopenharmony_ci 31401cb0ef41Sopenharmony_ci```js 31411cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 31421cb0ef41Sopenharmony_ciconst settings = { enableConnectProtocol: true }; 31431cb0ef41Sopenharmony_ciconst server = http2.createServer({ settings }); 31441cb0ef41Sopenharmony_ci``` 31451cb0ef41Sopenharmony_ci 31461cb0ef41Sopenharmony_ciOnce the client receives the `SETTINGS` frame from the server indicating that 31471cb0ef41Sopenharmony_cithe extended CONNECT may be used, it may send `CONNECT` requests that use the 31481cb0ef41Sopenharmony_ci`':protocol'` HTTP/2 pseudo-header: 31491cb0ef41Sopenharmony_ci 31501cb0ef41Sopenharmony_ci```js 31511cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 31521cb0ef41Sopenharmony_ciconst client = http2.connect('http://localhost:8080'); 31531cb0ef41Sopenharmony_ciclient.on('remoteSettings', (settings) => { 31541cb0ef41Sopenharmony_ci if (settings.enableConnectProtocol) { 31551cb0ef41Sopenharmony_ci const req = client.request({ ':method': 'CONNECT', ':protocol': 'foo' }); 31561cb0ef41Sopenharmony_ci // ... 31571cb0ef41Sopenharmony_ci } 31581cb0ef41Sopenharmony_ci}); 31591cb0ef41Sopenharmony_ci``` 31601cb0ef41Sopenharmony_ci 31611cb0ef41Sopenharmony_ci## Compatibility API 31621cb0ef41Sopenharmony_ci 31631cb0ef41Sopenharmony_ciThe Compatibility API has the goal of providing a similar developer experience 31641cb0ef41Sopenharmony_ciof HTTP/1 when using HTTP/2, making it possible to develop applications 31651cb0ef41Sopenharmony_cithat support both [HTTP/1][] and HTTP/2. This API targets only the 31661cb0ef41Sopenharmony_ci**public API** of the [HTTP/1][]. However many modules use internal 31671cb0ef41Sopenharmony_cimethods or state, and those _are not supported_ as it is a completely 31681cb0ef41Sopenharmony_cidifferent implementation. 31691cb0ef41Sopenharmony_ci 31701cb0ef41Sopenharmony_ciThe following example creates an HTTP/2 server using the compatibility 31711cb0ef41Sopenharmony_ciAPI: 31721cb0ef41Sopenharmony_ci 31731cb0ef41Sopenharmony_ci```js 31741cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 31751cb0ef41Sopenharmony_ciconst server = http2.createServer((req, res) => { 31761cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/html'); 31771cb0ef41Sopenharmony_ci res.setHeader('X-Foo', 'bar'); 31781cb0ef41Sopenharmony_ci res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); 31791cb0ef41Sopenharmony_ci res.end('ok'); 31801cb0ef41Sopenharmony_ci}); 31811cb0ef41Sopenharmony_ci``` 31821cb0ef41Sopenharmony_ci 31831cb0ef41Sopenharmony_ciIn order to create a mixed [HTTPS][] and HTTP/2 server, refer to the 31841cb0ef41Sopenharmony_ci[ALPN negotiation][] section. 31851cb0ef41Sopenharmony_ciUpgrading from non-tls HTTP/1 servers is not supported. 31861cb0ef41Sopenharmony_ci 31871cb0ef41Sopenharmony_ciThe HTTP/2 compatibility API is composed of [`Http2ServerRequest`][] and 31881cb0ef41Sopenharmony_ci[`Http2ServerResponse`][]. They aim at API compatibility with HTTP/1, but 31891cb0ef41Sopenharmony_cithey do not hide the differences between the protocols. As an example, 31901cb0ef41Sopenharmony_cithe status message for HTTP codes is ignored. 31911cb0ef41Sopenharmony_ci 31921cb0ef41Sopenharmony_ci### ALPN negotiation 31931cb0ef41Sopenharmony_ci 31941cb0ef41Sopenharmony_ciALPN negotiation allows supporting both [HTTPS][] and HTTP/2 over 31951cb0ef41Sopenharmony_cithe same socket. The `req` and `res` objects can be either HTTP/1 or 31961cb0ef41Sopenharmony_ciHTTP/2, and an application **must** restrict itself to the public API of 31971cb0ef41Sopenharmony_ci[HTTP/1][], and detect if it is possible to use the more advanced 31981cb0ef41Sopenharmony_cifeatures of HTTP/2. 31991cb0ef41Sopenharmony_ci 32001cb0ef41Sopenharmony_ciThe following example creates a server that supports both protocols: 32011cb0ef41Sopenharmony_ci 32021cb0ef41Sopenharmony_ci```js 32031cb0ef41Sopenharmony_ciconst { createSecureServer } = require('node:http2'); 32041cb0ef41Sopenharmony_ciconst { readFileSync } = require('node:fs'); 32051cb0ef41Sopenharmony_ci 32061cb0ef41Sopenharmony_ciconst cert = readFileSync('./cert.pem'); 32071cb0ef41Sopenharmony_ciconst key = readFileSync('./key.pem'); 32081cb0ef41Sopenharmony_ci 32091cb0ef41Sopenharmony_ciconst server = createSecureServer( 32101cb0ef41Sopenharmony_ci { cert, key, allowHTTP1: true }, 32111cb0ef41Sopenharmony_ci onRequest, 32121cb0ef41Sopenharmony_ci).listen(4443); 32131cb0ef41Sopenharmony_ci 32141cb0ef41Sopenharmony_cifunction onRequest(req, res) { 32151cb0ef41Sopenharmony_ci // Detects if it is a HTTPS request or HTTP/2 32161cb0ef41Sopenharmony_ci const { socket: { alpnProtocol } } = req.httpVersion === '2.0' ? 32171cb0ef41Sopenharmony_ci req.stream.session : req; 32181cb0ef41Sopenharmony_ci res.writeHead(200, { 'content-type': 'application/json' }); 32191cb0ef41Sopenharmony_ci res.end(JSON.stringify({ 32201cb0ef41Sopenharmony_ci alpnProtocol, 32211cb0ef41Sopenharmony_ci httpVersion: req.httpVersion, 32221cb0ef41Sopenharmony_ci })); 32231cb0ef41Sopenharmony_ci} 32241cb0ef41Sopenharmony_ci``` 32251cb0ef41Sopenharmony_ci 32261cb0ef41Sopenharmony_ciThe `'request'` event works identically on both [HTTPS][] and 32271cb0ef41Sopenharmony_ciHTTP/2. 32281cb0ef41Sopenharmony_ci 32291cb0ef41Sopenharmony_ci### Class: `http2.Http2ServerRequest` 32301cb0ef41Sopenharmony_ci 32311cb0ef41Sopenharmony_ci<!-- YAML 32321cb0ef41Sopenharmony_ciadded: v8.4.0 32331cb0ef41Sopenharmony_ci--> 32341cb0ef41Sopenharmony_ci 32351cb0ef41Sopenharmony_ci* Extends: {stream.Readable} 32361cb0ef41Sopenharmony_ci 32371cb0ef41Sopenharmony_ciA `Http2ServerRequest` object is created by [`http2.Server`][] or 32381cb0ef41Sopenharmony_ci[`http2.SecureServer`][] and passed as the first argument to the 32391cb0ef41Sopenharmony_ci[`'request'`][] event. It may be used to access a request status, headers, and 32401cb0ef41Sopenharmony_cidata. 32411cb0ef41Sopenharmony_ci 32421cb0ef41Sopenharmony_ci#### Event: `'aborted'` 32431cb0ef41Sopenharmony_ci 32441cb0ef41Sopenharmony_ci<!-- YAML 32451cb0ef41Sopenharmony_ciadded: v8.4.0 32461cb0ef41Sopenharmony_ci--> 32471cb0ef41Sopenharmony_ci 32481cb0ef41Sopenharmony_ciThe `'aborted'` event is emitted whenever a `Http2ServerRequest` instance is 32491cb0ef41Sopenharmony_ciabnormally aborted in mid-communication. 32501cb0ef41Sopenharmony_ci 32511cb0ef41Sopenharmony_ciThe `'aborted'` event will only be emitted if the `Http2ServerRequest` writable 32521cb0ef41Sopenharmony_ciside has not been ended. 32531cb0ef41Sopenharmony_ci 32541cb0ef41Sopenharmony_ci#### Event: `'close'` 32551cb0ef41Sopenharmony_ci 32561cb0ef41Sopenharmony_ci<!-- YAML 32571cb0ef41Sopenharmony_ciadded: v8.4.0 32581cb0ef41Sopenharmony_ci--> 32591cb0ef41Sopenharmony_ci 32601cb0ef41Sopenharmony_ciIndicates that the underlying [`Http2Stream`][] was closed. 32611cb0ef41Sopenharmony_ciJust like `'end'`, this event occurs only once per response. 32621cb0ef41Sopenharmony_ci 32631cb0ef41Sopenharmony_ci#### `request.aborted` 32641cb0ef41Sopenharmony_ci 32651cb0ef41Sopenharmony_ci<!-- YAML 32661cb0ef41Sopenharmony_ciadded: v10.1.0 32671cb0ef41Sopenharmony_ci--> 32681cb0ef41Sopenharmony_ci 32691cb0ef41Sopenharmony_ci* {boolean} 32701cb0ef41Sopenharmony_ci 32711cb0ef41Sopenharmony_ciThe `request.aborted` property will be `true` if the request has 32721cb0ef41Sopenharmony_cibeen aborted. 32731cb0ef41Sopenharmony_ci 32741cb0ef41Sopenharmony_ci#### `request.authority` 32751cb0ef41Sopenharmony_ci 32761cb0ef41Sopenharmony_ci<!-- YAML 32771cb0ef41Sopenharmony_ciadded: v8.4.0 32781cb0ef41Sopenharmony_ci--> 32791cb0ef41Sopenharmony_ci 32801cb0ef41Sopenharmony_ci* {string} 32811cb0ef41Sopenharmony_ci 32821cb0ef41Sopenharmony_ciThe request authority pseudo header field. Because HTTP/2 allows requests 32831cb0ef41Sopenharmony_cito set either `:authority` or `host`, this value is derived from 32841cb0ef41Sopenharmony_ci`req.headers[':authority']` if present. Otherwise, it is derived from 32851cb0ef41Sopenharmony_ci`req.headers['host']`. 32861cb0ef41Sopenharmony_ci 32871cb0ef41Sopenharmony_ci#### `request.complete` 32881cb0ef41Sopenharmony_ci 32891cb0ef41Sopenharmony_ci<!-- YAML 32901cb0ef41Sopenharmony_ciadded: v12.10.0 32911cb0ef41Sopenharmony_ci--> 32921cb0ef41Sopenharmony_ci 32931cb0ef41Sopenharmony_ci* {boolean} 32941cb0ef41Sopenharmony_ci 32951cb0ef41Sopenharmony_ciThe `request.complete` property will be `true` if the request has 32961cb0ef41Sopenharmony_cibeen completed, aborted, or destroyed. 32971cb0ef41Sopenharmony_ci 32981cb0ef41Sopenharmony_ci#### `request.connection` 32991cb0ef41Sopenharmony_ci 33001cb0ef41Sopenharmony_ci<!-- YAML 33011cb0ef41Sopenharmony_ciadded: v8.4.0 33021cb0ef41Sopenharmony_cideprecated: v13.0.0 33031cb0ef41Sopenharmony_ci--> 33041cb0ef41Sopenharmony_ci 33051cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated. Use [`request.socket`][]. 33061cb0ef41Sopenharmony_ci 33071cb0ef41Sopenharmony_ci* {net.Socket|tls.TLSSocket} 33081cb0ef41Sopenharmony_ci 33091cb0ef41Sopenharmony_ciSee [`request.socket`][]. 33101cb0ef41Sopenharmony_ci 33111cb0ef41Sopenharmony_ci#### `request.destroy([error])` 33121cb0ef41Sopenharmony_ci 33131cb0ef41Sopenharmony_ci<!-- YAML 33141cb0ef41Sopenharmony_ciadded: v8.4.0 33151cb0ef41Sopenharmony_ci--> 33161cb0ef41Sopenharmony_ci 33171cb0ef41Sopenharmony_ci* `error` {Error} 33181cb0ef41Sopenharmony_ci 33191cb0ef41Sopenharmony_ciCalls `destroy()` on the [`Http2Stream`][] that received 33201cb0ef41Sopenharmony_cithe [`Http2ServerRequest`][]. If `error` is provided, an `'error'` event 33211cb0ef41Sopenharmony_ciis emitted and `error` is passed as an argument to any listeners on the event. 33221cb0ef41Sopenharmony_ci 33231cb0ef41Sopenharmony_ciIt does nothing if the stream was already destroyed. 33241cb0ef41Sopenharmony_ci 33251cb0ef41Sopenharmony_ci#### `request.headers` 33261cb0ef41Sopenharmony_ci 33271cb0ef41Sopenharmony_ci<!-- YAML 33281cb0ef41Sopenharmony_ciadded: v8.4.0 33291cb0ef41Sopenharmony_ci--> 33301cb0ef41Sopenharmony_ci 33311cb0ef41Sopenharmony_ci* {Object} 33321cb0ef41Sopenharmony_ci 33331cb0ef41Sopenharmony_ciThe request/response headers object. 33341cb0ef41Sopenharmony_ci 33351cb0ef41Sopenharmony_ciKey-value pairs of header names and values. Header names are lower-cased. 33361cb0ef41Sopenharmony_ci 33371cb0ef41Sopenharmony_ci```js 33381cb0ef41Sopenharmony_ci// Prints something like: 33391cb0ef41Sopenharmony_ci// 33401cb0ef41Sopenharmony_ci// { 'user-agent': 'curl/7.22.0', 33411cb0ef41Sopenharmony_ci// host: '127.0.0.1:8000', 33421cb0ef41Sopenharmony_ci// accept: '*/*' } 33431cb0ef41Sopenharmony_ciconsole.log(request.headers); 33441cb0ef41Sopenharmony_ci``` 33451cb0ef41Sopenharmony_ci 33461cb0ef41Sopenharmony_ciSee [HTTP/2 Headers Object][]. 33471cb0ef41Sopenharmony_ci 33481cb0ef41Sopenharmony_ciIn HTTP/2, the request path, host name, protocol, and method are represented as 33491cb0ef41Sopenharmony_cispecial headers prefixed with the `:` character (e.g. `':path'`). These special 33501cb0ef41Sopenharmony_ciheaders will be included in the `request.headers` object. Care must be taken not 33511cb0ef41Sopenharmony_cito inadvertently modify these special headers or errors may occur. For instance, 33521cb0ef41Sopenharmony_ciremoving all headers from the request will cause errors to occur: 33531cb0ef41Sopenharmony_ci 33541cb0ef41Sopenharmony_ci```js 33551cb0ef41Sopenharmony_ciremoveAllHeaders(request.headers); 33561cb0ef41Sopenharmony_ciassert(request.url); // Fails because the :path header has been removed 33571cb0ef41Sopenharmony_ci``` 33581cb0ef41Sopenharmony_ci 33591cb0ef41Sopenharmony_ci#### `request.httpVersion` 33601cb0ef41Sopenharmony_ci 33611cb0ef41Sopenharmony_ci<!-- YAML 33621cb0ef41Sopenharmony_ciadded: v8.4.0 33631cb0ef41Sopenharmony_ci--> 33641cb0ef41Sopenharmony_ci 33651cb0ef41Sopenharmony_ci* {string} 33661cb0ef41Sopenharmony_ci 33671cb0ef41Sopenharmony_ciIn case of server request, the HTTP version sent by the client. In the case of 33681cb0ef41Sopenharmony_ciclient response, the HTTP version of the connected-to server. Returns 33691cb0ef41Sopenharmony_ci`'2.0'`. 33701cb0ef41Sopenharmony_ci 33711cb0ef41Sopenharmony_ciAlso `message.httpVersionMajor` is the first integer and 33721cb0ef41Sopenharmony_ci`message.httpVersionMinor` is the second. 33731cb0ef41Sopenharmony_ci 33741cb0ef41Sopenharmony_ci#### `request.method` 33751cb0ef41Sopenharmony_ci 33761cb0ef41Sopenharmony_ci<!-- YAML 33771cb0ef41Sopenharmony_ciadded: v8.4.0 33781cb0ef41Sopenharmony_ci--> 33791cb0ef41Sopenharmony_ci 33801cb0ef41Sopenharmony_ci* {string} 33811cb0ef41Sopenharmony_ci 33821cb0ef41Sopenharmony_ciThe request method as a string. Read-only. Examples: `'GET'`, `'DELETE'`. 33831cb0ef41Sopenharmony_ci 33841cb0ef41Sopenharmony_ci#### `request.rawHeaders` 33851cb0ef41Sopenharmony_ci 33861cb0ef41Sopenharmony_ci<!-- YAML 33871cb0ef41Sopenharmony_ciadded: v8.4.0 33881cb0ef41Sopenharmony_ci--> 33891cb0ef41Sopenharmony_ci 33901cb0ef41Sopenharmony_ci* {string\[]} 33911cb0ef41Sopenharmony_ci 33921cb0ef41Sopenharmony_ciThe raw request/response headers list exactly as they were received. 33931cb0ef41Sopenharmony_ci 33941cb0ef41Sopenharmony_ciThe keys and values are in the same list. It is _not_ a 33951cb0ef41Sopenharmony_cilist of tuples. So, the even-numbered offsets are key values, and the 33961cb0ef41Sopenharmony_ciodd-numbered offsets are the associated values. 33971cb0ef41Sopenharmony_ci 33981cb0ef41Sopenharmony_ciHeader names are not lowercased, and duplicates are not merged. 33991cb0ef41Sopenharmony_ci 34001cb0ef41Sopenharmony_ci```js 34011cb0ef41Sopenharmony_ci// Prints something like: 34021cb0ef41Sopenharmony_ci// 34031cb0ef41Sopenharmony_ci// [ 'user-agent', 34041cb0ef41Sopenharmony_ci// 'this is invalid because there can be only one', 34051cb0ef41Sopenharmony_ci// 'User-Agent', 34061cb0ef41Sopenharmony_ci// 'curl/7.22.0', 34071cb0ef41Sopenharmony_ci// 'Host', 34081cb0ef41Sopenharmony_ci// '127.0.0.1:8000', 34091cb0ef41Sopenharmony_ci// 'ACCEPT', 34101cb0ef41Sopenharmony_ci// '*/*' ] 34111cb0ef41Sopenharmony_ciconsole.log(request.rawHeaders); 34121cb0ef41Sopenharmony_ci``` 34131cb0ef41Sopenharmony_ci 34141cb0ef41Sopenharmony_ci#### `request.rawTrailers` 34151cb0ef41Sopenharmony_ci 34161cb0ef41Sopenharmony_ci<!-- YAML 34171cb0ef41Sopenharmony_ciadded: v8.4.0 34181cb0ef41Sopenharmony_ci--> 34191cb0ef41Sopenharmony_ci 34201cb0ef41Sopenharmony_ci* {string\[]} 34211cb0ef41Sopenharmony_ci 34221cb0ef41Sopenharmony_ciThe raw request/response trailer keys and values exactly as they were 34231cb0ef41Sopenharmony_cireceived. Only populated at the `'end'` event. 34241cb0ef41Sopenharmony_ci 34251cb0ef41Sopenharmony_ci#### `request.scheme` 34261cb0ef41Sopenharmony_ci 34271cb0ef41Sopenharmony_ci<!-- YAML 34281cb0ef41Sopenharmony_ciadded: v8.4.0 34291cb0ef41Sopenharmony_ci--> 34301cb0ef41Sopenharmony_ci 34311cb0ef41Sopenharmony_ci* {string} 34321cb0ef41Sopenharmony_ci 34331cb0ef41Sopenharmony_ciThe request scheme pseudo header field indicating the scheme 34341cb0ef41Sopenharmony_ciportion of the target URL. 34351cb0ef41Sopenharmony_ci 34361cb0ef41Sopenharmony_ci#### `request.setTimeout(msecs, callback)` 34371cb0ef41Sopenharmony_ci 34381cb0ef41Sopenharmony_ci<!-- YAML 34391cb0ef41Sopenharmony_ciadded: v8.4.0 34401cb0ef41Sopenharmony_ci--> 34411cb0ef41Sopenharmony_ci 34421cb0ef41Sopenharmony_ci* `msecs` {number} 34431cb0ef41Sopenharmony_ci* `callback` {Function} 34441cb0ef41Sopenharmony_ci* Returns: {http2.Http2ServerRequest} 34451cb0ef41Sopenharmony_ci 34461cb0ef41Sopenharmony_ciSets the [`Http2Stream`][]'s timeout value to `msecs`. If a callback is 34471cb0ef41Sopenharmony_ciprovided, then it is added as a listener on the `'timeout'` event on 34481cb0ef41Sopenharmony_cithe response object. 34491cb0ef41Sopenharmony_ci 34501cb0ef41Sopenharmony_ciIf no `'timeout'` listener is added to the request, the response, or 34511cb0ef41Sopenharmony_cithe server, then [`Http2Stream`][]s are destroyed when they time out. If a 34521cb0ef41Sopenharmony_cihandler is assigned to the request, the response, or the server's `'timeout'` 34531cb0ef41Sopenharmony_cievents, timed out sockets must be handled explicitly. 34541cb0ef41Sopenharmony_ci 34551cb0ef41Sopenharmony_ci#### `request.socket` 34561cb0ef41Sopenharmony_ci 34571cb0ef41Sopenharmony_ci<!-- YAML 34581cb0ef41Sopenharmony_ciadded: v8.4.0 34591cb0ef41Sopenharmony_ci--> 34601cb0ef41Sopenharmony_ci 34611cb0ef41Sopenharmony_ci* {net.Socket|tls.TLSSocket} 34621cb0ef41Sopenharmony_ci 34631cb0ef41Sopenharmony_ciReturns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but 34641cb0ef41Sopenharmony_ciapplies getters, setters, and methods based on HTTP/2 logic. 34651cb0ef41Sopenharmony_ci 34661cb0ef41Sopenharmony_ci`destroyed`, `readable`, and `writable` properties will be retrieved from and 34671cb0ef41Sopenharmony_ciset on `request.stream`. 34681cb0ef41Sopenharmony_ci 34691cb0ef41Sopenharmony_ci`destroy`, `emit`, `end`, `on` and `once` methods will be called on 34701cb0ef41Sopenharmony_ci`request.stream`. 34711cb0ef41Sopenharmony_ci 34721cb0ef41Sopenharmony_ci`setTimeout` method will be called on `request.stream.session`. 34731cb0ef41Sopenharmony_ci 34741cb0ef41Sopenharmony_ci`pause`, `read`, `resume`, and `write` will throw an error with code 34751cb0ef41Sopenharmony_ci`ERR_HTTP2_NO_SOCKET_MANIPULATION`. See [`Http2Session` and Sockets][] for 34761cb0ef41Sopenharmony_cimore information. 34771cb0ef41Sopenharmony_ci 34781cb0ef41Sopenharmony_ciAll other interactions will be routed directly to the socket. With TLS support, 34791cb0ef41Sopenharmony_ciuse [`request.socket.getPeerCertificate()`][] to obtain the client's 34801cb0ef41Sopenharmony_ciauthentication details. 34811cb0ef41Sopenharmony_ci 34821cb0ef41Sopenharmony_ci#### `request.stream` 34831cb0ef41Sopenharmony_ci 34841cb0ef41Sopenharmony_ci<!-- YAML 34851cb0ef41Sopenharmony_ciadded: v8.4.0 34861cb0ef41Sopenharmony_ci--> 34871cb0ef41Sopenharmony_ci 34881cb0ef41Sopenharmony_ci* {Http2Stream} 34891cb0ef41Sopenharmony_ci 34901cb0ef41Sopenharmony_ciThe [`Http2Stream`][] object backing the request. 34911cb0ef41Sopenharmony_ci 34921cb0ef41Sopenharmony_ci#### `request.trailers` 34931cb0ef41Sopenharmony_ci 34941cb0ef41Sopenharmony_ci<!-- YAML 34951cb0ef41Sopenharmony_ciadded: v8.4.0 34961cb0ef41Sopenharmony_ci--> 34971cb0ef41Sopenharmony_ci 34981cb0ef41Sopenharmony_ci* {Object} 34991cb0ef41Sopenharmony_ci 35001cb0ef41Sopenharmony_ciThe request/response trailers object. Only populated at the `'end'` event. 35011cb0ef41Sopenharmony_ci 35021cb0ef41Sopenharmony_ci#### `request.url` 35031cb0ef41Sopenharmony_ci 35041cb0ef41Sopenharmony_ci<!-- YAML 35051cb0ef41Sopenharmony_ciadded: v8.4.0 35061cb0ef41Sopenharmony_ci--> 35071cb0ef41Sopenharmony_ci 35081cb0ef41Sopenharmony_ci* {string} 35091cb0ef41Sopenharmony_ci 35101cb0ef41Sopenharmony_ciRequest URL string. This contains only the URL that is present in the actual 35111cb0ef41Sopenharmony_ciHTTP request. If the request is: 35121cb0ef41Sopenharmony_ci 35131cb0ef41Sopenharmony_ci```http 35141cb0ef41Sopenharmony_ciGET /status?name=ryan HTTP/1.1 35151cb0ef41Sopenharmony_ciAccept: text/plain 35161cb0ef41Sopenharmony_ci``` 35171cb0ef41Sopenharmony_ci 35181cb0ef41Sopenharmony_ciThen `request.url` will be: 35191cb0ef41Sopenharmony_ci 35201cb0ef41Sopenharmony_ci<!-- eslint-disable semi --> 35211cb0ef41Sopenharmony_ci 35221cb0ef41Sopenharmony_ci```js 35231cb0ef41Sopenharmony_ci'/status?name=ryan' 35241cb0ef41Sopenharmony_ci``` 35251cb0ef41Sopenharmony_ci 35261cb0ef41Sopenharmony_ciTo parse the url into its parts, `new URL()` can be used: 35271cb0ef41Sopenharmony_ci 35281cb0ef41Sopenharmony_ci```console 35291cb0ef41Sopenharmony_ci$ node 35301cb0ef41Sopenharmony_ci> new URL('/status?name=ryan', 'http://example.com') 35311cb0ef41Sopenharmony_ciURL { 35321cb0ef41Sopenharmony_ci href: 'http://example.com/status?name=ryan', 35331cb0ef41Sopenharmony_ci origin: 'http://example.com', 35341cb0ef41Sopenharmony_ci protocol: 'http:', 35351cb0ef41Sopenharmony_ci username: '', 35361cb0ef41Sopenharmony_ci password: '', 35371cb0ef41Sopenharmony_ci host: 'example.com', 35381cb0ef41Sopenharmony_ci hostname: 'example.com', 35391cb0ef41Sopenharmony_ci port: '', 35401cb0ef41Sopenharmony_ci pathname: '/status', 35411cb0ef41Sopenharmony_ci search: '?name=ryan', 35421cb0ef41Sopenharmony_ci searchParams: URLSearchParams { 'name' => 'ryan' }, 35431cb0ef41Sopenharmony_ci hash: '' 35441cb0ef41Sopenharmony_ci} 35451cb0ef41Sopenharmony_ci``` 35461cb0ef41Sopenharmony_ci 35471cb0ef41Sopenharmony_ci### Class: `http2.Http2ServerResponse` 35481cb0ef41Sopenharmony_ci 35491cb0ef41Sopenharmony_ci<!-- YAML 35501cb0ef41Sopenharmony_ciadded: v8.4.0 35511cb0ef41Sopenharmony_ci--> 35521cb0ef41Sopenharmony_ci 35531cb0ef41Sopenharmony_ci* Extends: {Stream} 35541cb0ef41Sopenharmony_ci 35551cb0ef41Sopenharmony_ciThis object is created internally by an HTTP server, not by the user. It is 35561cb0ef41Sopenharmony_cipassed as the second parameter to the [`'request'`][] event. 35571cb0ef41Sopenharmony_ci 35581cb0ef41Sopenharmony_ci#### Event: `'close'` 35591cb0ef41Sopenharmony_ci 35601cb0ef41Sopenharmony_ci<!-- YAML 35611cb0ef41Sopenharmony_ciadded: v8.4.0 35621cb0ef41Sopenharmony_ci--> 35631cb0ef41Sopenharmony_ci 35641cb0ef41Sopenharmony_ciIndicates that the underlying [`Http2Stream`][] was terminated before 35651cb0ef41Sopenharmony_ci[`response.end()`][] was called or able to flush. 35661cb0ef41Sopenharmony_ci 35671cb0ef41Sopenharmony_ci#### Event: `'finish'` 35681cb0ef41Sopenharmony_ci 35691cb0ef41Sopenharmony_ci<!-- YAML 35701cb0ef41Sopenharmony_ciadded: v8.4.0 35711cb0ef41Sopenharmony_ci--> 35721cb0ef41Sopenharmony_ci 35731cb0ef41Sopenharmony_ciEmitted when the response has been sent. More specifically, this event is 35741cb0ef41Sopenharmony_ciemitted when the last segment of the response headers and body have been 35751cb0ef41Sopenharmony_cihanded off to the HTTP/2 multiplexing for transmission over the network. It 35761cb0ef41Sopenharmony_cidoes not imply that the client has received anything yet. 35771cb0ef41Sopenharmony_ci 35781cb0ef41Sopenharmony_ciAfter this event, no more events will be emitted on the response object. 35791cb0ef41Sopenharmony_ci 35801cb0ef41Sopenharmony_ci#### `response.addTrailers(headers)` 35811cb0ef41Sopenharmony_ci 35821cb0ef41Sopenharmony_ci<!-- YAML 35831cb0ef41Sopenharmony_ciadded: v8.4.0 35841cb0ef41Sopenharmony_ci--> 35851cb0ef41Sopenharmony_ci 35861cb0ef41Sopenharmony_ci* `headers` {Object} 35871cb0ef41Sopenharmony_ci 35881cb0ef41Sopenharmony_ciThis method adds HTTP trailing headers (a header but at the end of the 35891cb0ef41Sopenharmony_cimessage) to the response. 35901cb0ef41Sopenharmony_ci 35911cb0ef41Sopenharmony_ciAttempting to set a header field name or value that contains invalid characters 35921cb0ef41Sopenharmony_ciwill result in a [`TypeError`][] being thrown. 35931cb0ef41Sopenharmony_ci 35941cb0ef41Sopenharmony_ci#### `response.connection` 35951cb0ef41Sopenharmony_ci 35961cb0ef41Sopenharmony_ci<!-- YAML 35971cb0ef41Sopenharmony_ciadded: v8.4.0 35981cb0ef41Sopenharmony_cideprecated: v13.0.0 35991cb0ef41Sopenharmony_ci--> 36001cb0ef41Sopenharmony_ci 36011cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated. Use [`response.socket`][]. 36021cb0ef41Sopenharmony_ci 36031cb0ef41Sopenharmony_ci* {net.Socket|tls.TLSSocket} 36041cb0ef41Sopenharmony_ci 36051cb0ef41Sopenharmony_ciSee [`response.socket`][]. 36061cb0ef41Sopenharmony_ci 36071cb0ef41Sopenharmony_ci#### `response.createPushResponse(headers, callback)` 36081cb0ef41Sopenharmony_ci 36091cb0ef41Sopenharmony_ci<!-- YAML 36101cb0ef41Sopenharmony_ciadded: v8.4.0 36111cb0ef41Sopenharmony_cichanges: 36121cb0ef41Sopenharmony_ci - version: v18.0.0 36131cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 36141cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 36151cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 36161cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 36171cb0ef41Sopenharmony_ci--> 36181cb0ef41Sopenharmony_ci 36191cb0ef41Sopenharmony_ci* `headers` {HTTP/2 Headers Object} An object describing the headers 36201cb0ef41Sopenharmony_ci* `callback` {Function} Called once `http2stream.pushStream()` is finished, 36211cb0ef41Sopenharmony_ci or either when the attempt to create the pushed `Http2Stream` has failed or 36221cb0ef41Sopenharmony_ci has been rejected, or the state of `Http2ServerRequest` is closed prior to 36231cb0ef41Sopenharmony_ci calling the `http2stream.pushStream()` method 36241cb0ef41Sopenharmony_ci * `err` {Error} 36251cb0ef41Sopenharmony_ci * `res` {http2.Http2ServerResponse} The newly-created `Http2ServerResponse` 36261cb0ef41Sopenharmony_ci object 36271cb0ef41Sopenharmony_ci 36281cb0ef41Sopenharmony_ciCall [`http2stream.pushStream()`][] with the given headers, and wrap the 36291cb0ef41Sopenharmony_cigiven [`Http2Stream`][] on a newly created `Http2ServerResponse` as the callback 36301cb0ef41Sopenharmony_ciparameter if successful. When `Http2ServerRequest` is closed, the callback is 36311cb0ef41Sopenharmony_cicalled with an error `ERR_HTTP2_INVALID_STREAM`. 36321cb0ef41Sopenharmony_ci 36331cb0ef41Sopenharmony_ci#### `response.end([data[, encoding]][, callback])` 36341cb0ef41Sopenharmony_ci 36351cb0ef41Sopenharmony_ci<!-- YAML 36361cb0ef41Sopenharmony_ciadded: v8.4.0 36371cb0ef41Sopenharmony_cichanges: 36381cb0ef41Sopenharmony_ci - version: v10.0.0 36391cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/18780 36401cb0ef41Sopenharmony_ci description: This method now returns a reference to `ServerResponse`. 36411cb0ef41Sopenharmony_ci--> 36421cb0ef41Sopenharmony_ci 36431cb0ef41Sopenharmony_ci* `data` {string|Buffer|Uint8Array} 36441cb0ef41Sopenharmony_ci* `encoding` {string} 36451cb0ef41Sopenharmony_ci* `callback` {Function} 36461cb0ef41Sopenharmony_ci* Returns: {this} 36471cb0ef41Sopenharmony_ci 36481cb0ef41Sopenharmony_ciThis method signals to the server that all of the response headers and body 36491cb0ef41Sopenharmony_cihave been sent; that server should consider this message complete. 36501cb0ef41Sopenharmony_ciThe method, `response.end()`, MUST be called on each response. 36511cb0ef41Sopenharmony_ci 36521cb0ef41Sopenharmony_ciIf `data` is specified, it is equivalent to calling 36531cb0ef41Sopenharmony_ci[`response.write(data, encoding)`][] followed by `response.end(callback)`. 36541cb0ef41Sopenharmony_ci 36551cb0ef41Sopenharmony_ciIf `callback` is specified, it will be called when the response stream 36561cb0ef41Sopenharmony_ciis finished. 36571cb0ef41Sopenharmony_ci 36581cb0ef41Sopenharmony_ci#### `response.finished` 36591cb0ef41Sopenharmony_ci 36601cb0ef41Sopenharmony_ci<!-- YAML 36611cb0ef41Sopenharmony_ciadded: v8.4.0 36621cb0ef41Sopenharmony_cideprecated: 36631cb0ef41Sopenharmony_ci - v13.4.0 36641cb0ef41Sopenharmony_ci - v12.16.0 36651cb0ef41Sopenharmony_ci--> 36661cb0ef41Sopenharmony_ci 36671cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated. Use [`response.writableEnded`][]. 36681cb0ef41Sopenharmony_ci 36691cb0ef41Sopenharmony_ci* {boolean} 36701cb0ef41Sopenharmony_ci 36711cb0ef41Sopenharmony_ciBoolean value that indicates whether the response has completed. Starts 36721cb0ef41Sopenharmony_cias `false`. After [`response.end()`][] executes, the value will be `true`. 36731cb0ef41Sopenharmony_ci 36741cb0ef41Sopenharmony_ci#### `response.getHeader(name)` 36751cb0ef41Sopenharmony_ci 36761cb0ef41Sopenharmony_ci<!-- YAML 36771cb0ef41Sopenharmony_ciadded: v8.4.0 36781cb0ef41Sopenharmony_ci--> 36791cb0ef41Sopenharmony_ci 36801cb0ef41Sopenharmony_ci* `name` {string} 36811cb0ef41Sopenharmony_ci* Returns: {string} 36821cb0ef41Sopenharmony_ci 36831cb0ef41Sopenharmony_ciReads out a header that has already been queued but not sent to the client. 36841cb0ef41Sopenharmony_ciThe name is case-insensitive. 36851cb0ef41Sopenharmony_ci 36861cb0ef41Sopenharmony_ci```js 36871cb0ef41Sopenharmony_ciconst contentType = response.getHeader('content-type'); 36881cb0ef41Sopenharmony_ci``` 36891cb0ef41Sopenharmony_ci 36901cb0ef41Sopenharmony_ci#### `response.getHeaderNames()` 36911cb0ef41Sopenharmony_ci 36921cb0ef41Sopenharmony_ci<!-- YAML 36931cb0ef41Sopenharmony_ciadded: v8.4.0 36941cb0ef41Sopenharmony_ci--> 36951cb0ef41Sopenharmony_ci 36961cb0ef41Sopenharmony_ci* Returns: {string\[]} 36971cb0ef41Sopenharmony_ci 36981cb0ef41Sopenharmony_ciReturns an array containing the unique names of the current outgoing headers. 36991cb0ef41Sopenharmony_ciAll header names are lowercase. 37001cb0ef41Sopenharmony_ci 37011cb0ef41Sopenharmony_ci```js 37021cb0ef41Sopenharmony_ciresponse.setHeader('Foo', 'bar'); 37031cb0ef41Sopenharmony_ciresponse.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']); 37041cb0ef41Sopenharmony_ci 37051cb0ef41Sopenharmony_ciconst headerNames = response.getHeaderNames(); 37061cb0ef41Sopenharmony_ci// headerNames === ['foo', 'set-cookie'] 37071cb0ef41Sopenharmony_ci``` 37081cb0ef41Sopenharmony_ci 37091cb0ef41Sopenharmony_ci#### `response.getHeaders()` 37101cb0ef41Sopenharmony_ci 37111cb0ef41Sopenharmony_ci<!-- YAML 37121cb0ef41Sopenharmony_ciadded: v8.4.0 37131cb0ef41Sopenharmony_ci--> 37141cb0ef41Sopenharmony_ci 37151cb0ef41Sopenharmony_ci* Returns: {Object} 37161cb0ef41Sopenharmony_ci 37171cb0ef41Sopenharmony_ciReturns a shallow copy of the current outgoing headers. Since a shallow copy 37181cb0ef41Sopenharmony_ciis used, array values may be mutated without additional calls to various 37191cb0ef41Sopenharmony_ciheader-related http module methods. The keys of the returned object are the 37201cb0ef41Sopenharmony_ciheader names and the values are the respective header values. All header names 37211cb0ef41Sopenharmony_ciare lowercase. 37221cb0ef41Sopenharmony_ci 37231cb0ef41Sopenharmony_ciThe object returned by the `response.getHeaders()` method _does not_ 37241cb0ef41Sopenharmony_ciprototypically inherit from the JavaScript `Object`. This means that typical 37251cb0ef41Sopenharmony_ci`Object` methods such as `obj.toString()`, `obj.hasOwnProperty()`, and others 37261cb0ef41Sopenharmony_ciare not defined and _will not work_. 37271cb0ef41Sopenharmony_ci 37281cb0ef41Sopenharmony_ci```js 37291cb0ef41Sopenharmony_ciresponse.setHeader('Foo', 'bar'); 37301cb0ef41Sopenharmony_ciresponse.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']); 37311cb0ef41Sopenharmony_ci 37321cb0ef41Sopenharmony_ciconst headers = response.getHeaders(); 37331cb0ef41Sopenharmony_ci// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] } 37341cb0ef41Sopenharmony_ci``` 37351cb0ef41Sopenharmony_ci 37361cb0ef41Sopenharmony_ci#### `response.hasHeader(name)` 37371cb0ef41Sopenharmony_ci 37381cb0ef41Sopenharmony_ci<!-- YAML 37391cb0ef41Sopenharmony_ciadded: v8.4.0 37401cb0ef41Sopenharmony_ci--> 37411cb0ef41Sopenharmony_ci 37421cb0ef41Sopenharmony_ci* `name` {string} 37431cb0ef41Sopenharmony_ci* Returns: {boolean} 37441cb0ef41Sopenharmony_ci 37451cb0ef41Sopenharmony_ciReturns `true` if the header identified by `name` is currently set in the 37461cb0ef41Sopenharmony_cioutgoing headers. The header name matching is case-insensitive. 37471cb0ef41Sopenharmony_ci 37481cb0ef41Sopenharmony_ci```js 37491cb0ef41Sopenharmony_ciconst hasContentType = response.hasHeader('content-type'); 37501cb0ef41Sopenharmony_ci``` 37511cb0ef41Sopenharmony_ci 37521cb0ef41Sopenharmony_ci#### `response.headersSent` 37531cb0ef41Sopenharmony_ci 37541cb0ef41Sopenharmony_ci<!-- YAML 37551cb0ef41Sopenharmony_ciadded: v8.4.0 37561cb0ef41Sopenharmony_ci--> 37571cb0ef41Sopenharmony_ci 37581cb0ef41Sopenharmony_ci* {boolean} 37591cb0ef41Sopenharmony_ci 37601cb0ef41Sopenharmony_ciTrue if headers were sent, false otherwise (read-only). 37611cb0ef41Sopenharmony_ci 37621cb0ef41Sopenharmony_ci#### `response.removeHeader(name)` 37631cb0ef41Sopenharmony_ci 37641cb0ef41Sopenharmony_ci<!-- YAML 37651cb0ef41Sopenharmony_ciadded: v8.4.0 37661cb0ef41Sopenharmony_ci--> 37671cb0ef41Sopenharmony_ci 37681cb0ef41Sopenharmony_ci* `name` {string} 37691cb0ef41Sopenharmony_ci 37701cb0ef41Sopenharmony_ciRemoves a header that has been queued for implicit sending. 37711cb0ef41Sopenharmony_ci 37721cb0ef41Sopenharmony_ci```js 37731cb0ef41Sopenharmony_ciresponse.removeHeader('Content-Encoding'); 37741cb0ef41Sopenharmony_ci``` 37751cb0ef41Sopenharmony_ci 37761cb0ef41Sopenharmony_ci#### `response.req` 37771cb0ef41Sopenharmony_ci 37781cb0ef41Sopenharmony_ci<!-- YAML 37791cb0ef41Sopenharmony_ciadded: v15.7.0 37801cb0ef41Sopenharmony_ci--> 37811cb0ef41Sopenharmony_ci 37821cb0ef41Sopenharmony_ci* {http2.Http2ServerRequest} 37831cb0ef41Sopenharmony_ci 37841cb0ef41Sopenharmony_ciA reference to the original HTTP2 `request` object. 37851cb0ef41Sopenharmony_ci 37861cb0ef41Sopenharmony_ci#### `response.sendDate` 37871cb0ef41Sopenharmony_ci 37881cb0ef41Sopenharmony_ci<!-- YAML 37891cb0ef41Sopenharmony_ciadded: v8.4.0 37901cb0ef41Sopenharmony_ci--> 37911cb0ef41Sopenharmony_ci 37921cb0ef41Sopenharmony_ci* {boolean} 37931cb0ef41Sopenharmony_ci 37941cb0ef41Sopenharmony_ciWhen true, the Date header will be automatically generated and sent in 37951cb0ef41Sopenharmony_cithe response if it is not already present in the headers. Defaults to true. 37961cb0ef41Sopenharmony_ci 37971cb0ef41Sopenharmony_ciThis should only be disabled for testing; HTTP requires the Date header 37981cb0ef41Sopenharmony_ciin responses. 37991cb0ef41Sopenharmony_ci 38001cb0ef41Sopenharmony_ci#### `response.setHeader(name, value)` 38011cb0ef41Sopenharmony_ci 38021cb0ef41Sopenharmony_ci<!-- YAML 38031cb0ef41Sopenharmony_ciadded: v8.4.0 38041cb0ef41Sopenharmony_ci--> 38051cb0ef41Sopenharmony_ci 38061cb0ef41Sopenharmony_ci* `name` {string} 38071cb0ef41Sopenharmony_ci* `value` {string|string\[]} 38081cb0ef41Sopenharmony_ci 38091cb0ef41Sopenharmony_ciSets a single header value for implicit headers. If this header already exists 38101cb0ef41Sopenharmony_ciin the to-be-sent headers, its value will be replaced. Use an array of strings 38111cb0ef41Sopenharmony_cihere to send multiple headers with the same name. 38121cb0ef41Sopenharmony_ci 38131cb0ef41Sopenharmony_ci```js 38141cb0ef41Sopenharmony_ciresponse.setHeader('Content-Type', 'text/html; charset=utf-8'); 38151cb0ef41Sopenharmony_ci``` 38161cb0ef41Sopenharmony_ci 38171cb0ef41Sopenharmony_cior 38181cb0ef41Sopenharmony_ci 38191cb0ef41Sopenharmony_ci```js 38201cb0ef41Sopenharmony_ciresponse.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']); 38211cb0ef41Sopenharmony_ci``` 38221cb0ef41Sopenharmony_ci 38231cb0ef41Sopenharmony_ciAttempting to set a header field name or value that contains invalid characters 38241cb0ef41Sopenharmony_ciwill result in a [`TypeError`][] being thrown. 38251cb0ef41Sopenharmony_ci 38261cb0ef41Sopenharmony_ciWhen headers have been set with [`response.setHeader()`][], they will be merged 38271cb0ef41Sopenharmony_ciwith any headers passed to [`response.writeHead()`][], with the headers passed 38281cb0ef41Sopenharmony_cito [`response.writeHead()`][] given precedence. 38291cb0ef41Sopenharmony_ci 38301cb0ef41Sopenharmony_ci```js 38311cb0ef41Sopenharmony_ci// Returns content-type = text/plain 38321cb0ef41Sopenharmony_ciconst server = http2.createServer((req, res) => { 38331cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/html; charset=utf-8'); 38341cb0ef41Sopenharmony_ci res.setHeader('X-Foo', 'bar'); 38351cb0ef41Sopenharmony_ci res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); 38361cb0ef41Sopenharmony_ci res.end('ok'); 38371cb0ef41Sopenharmony_ci}); 38381cb0ef41Sopenharmony_ci``` 38391cb0ef41Sopenharmony_ci 38401cb0ef41Sopenharmony_ci#### `response.setTimeout(msecs[, callback])` 38411cb0ef41Sopenharmony_ci 38421cb0ef41Sopenharmony_ci<!-- YAML 38431cb0ef41Sopenharmony_ciadded: v8.4.0 38441cb0ef41Sopenharmony_ci--> 38451cb0ef41Sopenharmony_ci 38461cb0ef41Sopenharmony_ci* `msecs` {number} 38471cb0ef41Sopenharmony_ci* `callback` {Function} 38481cb0ef41Sopenharmony_ci* Returns: {http2.Http2ServerResponse} 38491cb0ef41Sopenharmony_ci 38501cb0ef41Sopenharmony_ciSets the [`Http2Stream`][]'s timeout value to `msecs`. If a callback is 38511cb0ef41Sopenharmony_ciprovided, then it is added as a listener on the `'timeout'` event on 38521cb0ef41Sopenharmony_cithe response object. 38531cb0ef41Sopenharmony_ci 38541cb0ef41Sopenharmony_ciIf no `'timeout'` listener is added to the request, the response, or 38551cb0ef41Sopenharmony_cithe server, then [`Http2Stream`][]s are destroyed when they time out. If a 38561cb0ef41Sopenharmony_cihandler is assigned to the request, the response, or the server's `'timeout'` 38571cb0ef41Sopenharmony_cievents, timed out sockets must be handled explicitly. 38581cb0ef41Sopenharmony_ci 38591cb0ef41Sopenharmony_ci#### `response.socket` 38601cb0ef41Sopenharmony_ci 38611cb0ef41Sopenharmony_ci<!-- YAML 38621cb0ef41Sopenharmony_ciadded: v8.4.0 38631cb0ef41Sopenharmony_ci--> 38641cb0ef41Sopenharmony_ci 38651cb0ef41Sopenharmony_ci* {net.Socket|tls.TLSSocket} 38661cb0ef41Sopenharmony_ci 38671cb0ef41Sopenharmony_ciReturns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but 38681cb0ef41Sopenharmony_ciapplies getters, setters, and methods based on HTTP/2 logic. 38691cb0ef41Sopenharmony_ci 38701cb0ef41Sopenharmony_ci`destroyed`, `readable`, and `writable` properties will be retrieved from and 38711cb0ef41Sopenharmony_ciset on `response.stream`. 38721cb0ef41Sopenharmony_ci 38731cb0ef41Sopenharmony_ci`destroy`, `emit`, `end`, `on` and `once` methods will be called on 38741cb0ef41Sopenharmony_ci`response.stream`. 38751cb0ef41Sopenharmony_ci 38761cb0ef41Sopenharmony_ci`setTimeout` method will be called on `response.stream.session`. 38771cb0ef41Sopenharmony_ci 38781cb0ef41Sopenharmony_ci`pause`, `read`, `resume`, and `write` will throw an error with code 38791cb0ef41Sopenharmony_ci`ERR_HTTP2_NO_SOCKET_MANIPULATION`. See [`Http2Session` and Sockets][] for 38801cb0ef41Sopenharmony_cimore information. 38811cb0ef41Sopenharmony_ci 38821cb0ef41Sopenharmony_ciAll other interactions will be routed directly to the socket. 38831cb0ef41Sopenharmony_ci 38841cb0ef41Sopenharmony_ci```js 38851cb0ef41Sopenharmony_ciconst http2 = require('node:http2'); 38861cb0ef41Sopenharmony_ciconst server = http2.createServer((req, res) => { 38871cb0ef41Sopenharmony_ci const ip = req.socket.remoteAddress; 38881cb0ef41Sopenharmony_ci const port = req.socket.remotePort; 38891cb0ef41Sopenharmony_ci res.end(`Your IP address is ${ip} and your source port is ${port}.`); 38901cb0ef41Sopenharmony_ci}).listen(3000); 38911cb0ef41Sopenharmony_ci``` 38921cb0ef41Sopenharmony_ci 38931cb0ef41Sopenharmony_ci#### `response.statusCode` 38941cb0ef41Sopenharmony_ci 38951cb0ef41Sopenharmony_ci<!-- YAML 38961cb0ef41Sopenharmony_ciadded: v8.4.0 38971cb0ef41Sopenharmony_ci--> 38981cb0ef41Sopenharmony_ci 38991cb0ef41Sopenharmony_ci* {number} 39001cb0ef41Sopenharmony_ci 39011cb0ef41Sopenharmony_ciWhen using implicit headers (not calling [`response.writeHead()`][] explicitly), 39021cb0ef41Sopenharmony_cithis property controls the status code that will be sent to the client when 39031cb0ef41Sopenharmony_cithe headers get flushed. 39041cb0ef41Sopenharmony_ci 39051cb0ef41Sopenharmony_ci```js 39061cb0ef41Sopenharmony_ciresponse.statusCode = 404; 39071cb0ef41Sopenharmony_ci``` 39081cb0ef41Sopenharmony_ci 39091cb0ef41Sopenharmony_ciAfter response header was sent to the client, this property indicates the 39101cb0ef41Sopenharmony_cistatus code which was sent out. 39111cb0ef41Sopenharmony_ci 39121cb0ef41Sopenharmony_ci#### `response.statusMessage` 39131cb0ef41Sopenharmony_ci 39141cb0ef41Sopenharmony_ci<!-- YAML 39151cb0ef41Sopenharmony_ciadded: v8.4.0 39161cb0ef41Sopenharmony_ci--> 39171cb0ef41Sopenharmony_ci 39181cb0ef41Sopenharmony_ci* {string} 39191cb0ef41Sopenharmony_ci 39201cb0ef41Sopenharmony_ciStatus message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns 39211cb0ef41Sopenharmony_cian empty string. 39221cb0ef41Sopenharmony_ci 39231cb0ef41Sopenharmony_ci#### `response.stream` 39241cb0ef41Sopenharmony_ci 39251cb0ef41Sopenharmony_ci<!-- YAML 39261cb0ef41Sopenharmony_ciadded: v8.4.0 39271cb0ef41Sopenharmony_ci--> 39281cb0ef41Sopenharmony_ci 39291cb0ef41Sopenharmony_ci* {Http2Stream} 39301cb0ef41Sopenharmony_ci 39311cb0ef41Sopenharmony_ciThe [`Http2Stream`][] object backing the response. 39321cb0ef41Sopenharmony_ci 39331cb0ef41Sopenharmony_ci#### `response.writableEnded` 39341cb0ef41Sopenharmony_ci 39351cb0ef41Sopenharmony_ci<!-- YAML 39361cb0ef41Sopenharmony_ciadded: v12.9.0 39371cb0ef41Sopenharmony_ci--> 39381cb0ef41Sopenharmony_ci 39391cb0ef41Sopenharmony_ci* {boolean} 39401cb0ef41Sopenharmony_ci 39411cb0ef41Sopenharmony_ciIs `true` after [`response.end()`][] has been called. This property 39421cb0ef41Sopenharmony_cidoes not indicate whether the data has been flushed, for this use 39431cb0ef41Sopenharmony_ci[`writable.writableFinished`][] instead. 39441cb0ef41Sopenharmony_ci 39451cb0ef41Sopenharmony_ci#### `response.write(chunk[, encoding][, callback])` 39461cb0ef41Sopenharmony_ci 39471cb0ef41Sopenharmony_ci<!-- YAML 39481cb0ef41Sopenharmony_ciadded: v8.4.0 39491cb0ef41Sopenharmony_ci--> 39501cb0ef41Sopenharmony_ci 39511cb0ef41Sopenharmony_ci* `chunk` {string|Buffer|Uint8Array} 39521cb0ef41Sopenharmony_ci* `encoding` {string} 39531cb0ef41Sopenharmony_ci* `callback` {Function} 39541cb0ef41Sopenharmony_ci* Returns: {boolean} 39551cb0ef41Sopenharmony_ci 39561cb0ef41Sopenharmony_ciIf this method is called and [`response.writeHead()`][] has not been called, 39571cb0ef41Sopenharmony_ciit will switch to implicit header mode and flush the implicit headers. 39581cb0ef41Sopenharmony_ci 39591cb0ef41Sopenharmony_ciThis sends a chunk of the response body. This method may 39601cb0ef41Sopenharmony_cibe called multiple times to provide successive parts of the body. 39611cb0ef41Sopenharmony_ci 39621cb0ef41Sopenharmony_ciIn the `node:http` module, the response body is omitted when the 39631cb0ef41Sopenharmony_cirequest is a HEAD request. Similarly, the `204` and `304` responses 39641cb0ef41Sopenharmony_ci_must not_ include a message body. 39651cb0ef41Sopenharmony_ci 39661cb0ef41Sopenharmony_ci`chunk` can be a string or a buffer. If `chunk` is a string, 39671cb0ef41Sopenharmony_cithe second parameter specifies how to encode it into a byte stream. 39681cb0ef41Sopenharmony_ciBy default the `encoding` is `'utf8'`. `callback` will be called when this chunk 39691cb0ef41Sopenharmony_ciof data is flushed. 39701cb0ef41Sopenharmony_ci 39711cb0ef41Sopenharmony_ciThis is the raw HTTP body and has nothing to do with higher-level multi-part 39721cb0ef41Sopenharmony_cibody encodings that may be used. 39731cb0ef41Sopenharmony_ci 39741cb0ef41Sopenharmony_ciThe first time [`response.write()`][] is called, it will send the buffered 39751cb0ef41Sopenharmony_ciheader information and the first chunk of the body to the client. The second 39761cb0ef41Sopenharmony_citime [`response.write()`][] is called, Node.js assumes data will be streamed, 39771cb0ef41Sopenharmony_ciand sends the new data separately. That is, the response is buffered up to the 39781cb0ef41Sopenharmony_cifirst chunk of the body. 39791cb0ef41Sopenharmony_ci 39801cb0ef41Sopenharmony_ciReturns `true` if the entire data was flushed successfully to the kernel 39811cb0ef41Sopenharmony_cibuffer. Returns `false` if all or part of the data was queued in user memory. 39821cb0ef41Sopenharmony_ci`'drain'` will be emitted when the buffer is free again. 39831cb0ef41Sopenharmony_ci 39841cb0ef41Sopenharmony_ci#### `response.writeContinue()` 39851cb0ef41Sopenharmony_ci 39861cb0ef41Sopenharmony_ci<!-- YAML 39871cb0ef41Sopenharmony_ciadded: v8.4.0 39881cb0ef41Sopenharmony_ci--> 39891cb0ef41Sopenharmony_ci 39901cb0ef41Sopenharmony_ciSends a status `100 Continue` to the client, indicating that the request body 39911cb0ef41Sopenharmony_cishould be sent. See the [`'checkContinue'`][] event on `Http2Server` and 39921cb0ef41Sopenharmony_ci`Http2SecureServer`. 39931cb0ef41Sopenharmony_ci 39941cb0ef41Sopenharmony_ci#### `response.writeEarlyHints(hints)` 39951cb0ef41Sopenharmony_ci 39961cb0ef41Sopenharmony_ci<!-- YAML 39971cb0ef41Sopenharmony_ciadded: v18.11.0 39981cb0ef41Sopenharmony_ci--> 39991cb0ef41Sopenharmony_ci 40001cb0ef41Sopenharmony_ci* `hints` {Object} 40011cb0ef41Sopenharmony_ci 40021cb0ef41Sopenharmony_ciSends a status `103 Early Hints` to the client with a Link header, 40031cb0ef41Sopenharmony_ciindicating that the user agent can preload/preconnect the linked resources. 40041cb0ef41Sopenharmony_ciThe `hints` is an object containing the values of headers to be sent with 40051cb0ef41Sopenharmony_ciearly hints message. 40061cb0ef41Sopenharmony_ci 40071cb0ef41Sopenharmony_ci**Example** 40081cb0ef41Sopenharmony_ci 40091cb0ef41Sopenharmony_ci```js 40101cb0ef41Sopenharmony_ciconst earlyHintsLink = '</styles.css>; rel=preload; as=style'; 40111cb0ef41Sopenharmony_ciresponse.writeEarlyHints({ 40121cb0ef41Sopenharmony_ci 'link': earlyHintsLink, 40131cb0ef41Sopenharmony_ci}); 40141cb0ef41Sopenharmony_ci 40151cb0ef41Sopenharmony_ciconst earlyHintsLinks = [ 40161cb0ef41Sopenharmony_ci '</styles.css>; rel=preload; as=style', 40171cb0ef41Sopenharmony_ci '</scripts.js>; rel=preload; as=script', 40181cb0ef41Sopenharmony_ci]; 40191cb0ef41Sopenharmony_ciresponse.writeEarlyHints({ 40201cb0ef41Sopenharmony_ci 'link': earlyHintsLinks, 40211cb0ef41Sopenharmony_ci}); 40221cb0ef41Sopenharmony_ci``` 40231cb0ef41Sopenharmony_ci 40241cb0ef41Sopenharmony_ci#### `response.writeHead(statusCode[, statusMessage][, headers])` 40251cb0ef41Sopenharmony_ci 40261cb0ef41Sopenharmony_ci<!-- YAML 40271cb0ef41Sopenharmony_ciadded: v8.4.0 40281cb0ef41Sopenharmony_cichanges: 40291cb0ef41Sopenharmony_ci - version: 40301cb0ef41Sopenharmony_ci - v11.10.0 40311cb0ef41Sopenharmony_ci - v10.17.0 40321cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/25974 40331cb0ef41Sopenharmony_ci description: Return `this` from `writeHead()` to allow chaining with 40341cb0ef41Sopenharmony_ci `end()`. 40351cb0ef41Sopenharmony_ci--> 40361cb0ef41Sopenharmony_ci 40371cb0ef41Sopenharmony_ci* `statusCode` {number} 40381cb0ef41Sopenharmony_ci* `statusMessage` {string} 40391cb0ef41Sopenharmony_ci* `headers` {Object|Array} 40401cb0ef41Sopenharmony_ci* Returns: {http2.Http2ServerResponse} 40411cb0ef41Sopenharmony_ci 40421cb0ef41Sopenharmony_ciSends a response header to the request. The status code is a 3-digit HTTP 40431cb0ef41Sopenharmony_cistatus code, like `404`. The last argument, `headers`, are the response headers. 40441cb0ef41Sopenharmony_ci 40451cb0ef41Sopenharmony_ciReturns a reference to the `Http2ServerResponse`, so that calls can be chained. 40461cb0ef41Sopenharmony_ci 40471cb0ef41Sopenharmony_ciFor compatibility with [HTTP/1][], a human-readable `statusMessage` may be 40481cb0ef41Sopenharmony_cipassed as the second argument. However, because the `statusMessage` has no 40491cb0ef41Sopenharmony_cimeaning within HTTP/2, the argument will have no effect and a process warning 40501cb0ef41Sopenharmony_ciwill be emitted. 40511cb0ef41Sopenharmony_ci 40521cb0ef41Sopenharmony_ci```js 40531cb0ef41Sopenharmony_ciconst body = 'hello world'; 40541cb0ef41Sopenharmony_ciresponse.writeHead(200, { 40551cb0ef41Sopenharmony_ci 'Content-Length': Buffer.byteLength(body), 40561cb0ef41Sopenharmony_ci 'Content-Type': 'text/plain; charset=utf-8', 40571cb0ef41Sopenharmony_ci}); 40581cb0ef41Sopenharmony_ci``` 40591cb0ef41Sopenharmony_ci 40601cb0ef41Sopenharmony_ci`Content-Length` is given in bytes not characters. The 40611cb0ef41Sopenharmony_ci`Buffer.byteLength()` API may be used to determine the number of bytes in a 40621cb0ef41Sopenharmony_cigiven encoding. On outbound messages, Node.js does not check if Content-Length 40631cb0ef41Sopenharmony_ciand the length of the body being transmitted are equal or not. However, when 40641cb0ef41Sopenharmony_cireceiving messages, Node.js will automatically reject messages when the 40651cb0ef41Sopenharmony_ci`Content-Length` does not match the actual payload size. 40661cb0ef41Sopenharmony_ci 40671cb0ef41Sopenharmony_ciThis method may be called at most one time on a message before 40681cb0ef41Sopenharmony_ci[`response.end()`][] is called. 40691cb0ef41Sopenharmony_ci 40701cb0ef41Sopenharmony_ciIf [`response.write()`][] or [`response.end()`][] are called before calling 40711cb0ef41Sopenharmony_cithis, the implicit/mutable headers will be calculated and call this function. 40721cb0ef41Sopenharmony_ci 40731cb0ef41Sopenharmony_ciWhen headers have been set with [`response.setHeader()`][], they will be merged 40741cb0ef41Sopenharmony_ciwith any headers passed to [`response.writeHead()`][], with the headers passed 40751cb0ef41Sopenharmony_cito [`response.writeHead()`][] given precedence. 40761cb0ef41Sopenharmony_ci 40771cb0ef41Sopenharmony_ci```js 40781cb0ef41Sopenharmony_ci// Returns content-type = text/plain 40791cb0ef41Sopenharmony_ciconst server = http2.createServer((req, res) => { 40801cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/html; charset=utf-8'); 40811cb0ef41Sopenharmony_ci res.setHeader('X-Foo', 'bar'); 40821cb0ef41Sopenharmony_ci res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); 40831cb0ef41Sopenharmony_ci res.end('ok'); 40841cb0ef41Sopenharmony_ci}); 40851cb0ef41Sopenharmony_ci``` 40861cb0ef41Sopenharmony_ci 40871cb0ef41Sopenharmony_ciAttempting to set a header field name or value that contains invalid characters 40881cb0ef41Sopenharmony_ciwill result in a [`TypeError`][] being thrown. 40891cb0ef41Sopenharmony_ci 40901cb0ef41Sopenharmony_ci## Collecting HTTP/2 performance metrics 40911cb0ef41Sopenharmony_ci 40921cb0ef41Sopenharmony_ciThe [Performance Observer][] API can be used to collect basic performance 40931cb0ef41Sopenharmony_cimetrics for each `Http2Session` and `Http2Stream` instance. 40941cb0ef41Sopenharmony_ci 40951cb0ef41Sopenharmony_ci```js 40961cb0ef41Sopenharmony_ciconst { PerformanceObserver } = require('node:perf_hooks'); 40971cb0ef41Sopenharmony_ci 40981cb0ef41Sopenharmony_ciconst obs = new PerformanceObserver((items) => { 40991cb0ef41Sopenharmony_ci const entry = items.getEntries()[0]; 41001cb0ef41Sopenharmony_ci console.log(entry.entryType); // prints 'http2' 41011cb0ef41Sopenharmony_ci if (entry.name === 'Http2Session') { 41021cb0ef41Sopenharmony_ci // Entry contains statistics about the Http2Session 41031cb0ef41Sopenharmony_ci } else if (entry.name === 'Http2Stream') { 41041cb0ef41Sopenharmony_ci // Entry contains statistics about the Http2Stream 41051cb0ef41Sopenharmony_ci } 41061cb0ef41Sopenharmony_ci}); 41071cb0ef41Sopenharmony_ciobs.observe({ entryTypes: ['http2'] }); 41081cb0ef41Sopenharmony_ci``` 41091cb0ef41Sopenharmony_ci 41101cb0ef41Sopenharmony_ciThe `entryType` property of the `PerformanceEntry` will be equal to `'http2'`. 41111cb0ef41Sopenharmony_ci 41121cb0ef41Sopenharmony_ciThe `name` property of the `PerformanceEntry` will be equal to either 41131cb0ef41Sopenharmony_ci`'Http2Stream'` or `'Http2Session'`. 41141cb0ef41Sopenharmony_ci 41151cb0ef41Sopenharmony_ciIf `name` is equal to `Http2Stream`, the `PerformanceEntry` will contain the 41161cb0ef41Sopenharmony_cifollowing additional properties: 41171cb0ef41Sopenharmony_ci 41181cb0ef41Sopenharmony_ci* `bytesRead` {number} The number of `DATA` frame bytes received for this 41191cb0ef41Sopenharmony_ci `Http2Stream`. 41201cb0ef41Sopenharmony_ci* `bytesWritten` {number} The number of `DATA` frame bytes sent for this 41211cb0ef41Sopenharmony_ci `Http2Stream`. 41221cb0ef41Sopenharmony_ci* `id` {number} The identifier of the associated `Http2Stream` 41231cb0ef41Sopenharmony_ci* `timeToFirstByte` {number} The number of milliseconds elapsed between the 41241cb0ef41Sopenharmony_ci `PerformanceEntry` `startTime` and the reception of the first `DATA` frame. 41251cb0ef41Sopenharmony_ci* `timeToFirstByteSent` {number} The number of milliseconds elapsed between 41261cb0ef41Sopenharmony_ci the `PerformanceEntry` `startTime` and sending of the first `DATA` frame. 41271cb0ef41Sopenharmony_ci* `timeToFirstHeader` {number} The number of milliseconds elapsed between the 41281cb0ef41Sopenharmony_ci `PerformanceEntry` `startTime` and the reception of the first header. 41291cb0ef41Sopenharmony_ci 41301cb0ef41Sopenharmony_ciIf `name` is equal to `Http2Session`, the `PerformanceEntry` will contain the 41311cb0ef41Sopenharmony_cifollowing additional properties: 41321cb0ef41Sopenharmony_ci 41331cb0ef41Sopenharmony_ci* `bytesRead` {number} The number of bytes received for this `Http2Session`. 41341cb0ef41Sopenharmony_ci* `bytesWritten` {number} The number of bytes sent for this `Http2Session`. 41351cb0ef41Sopenharmony_ci* `framesReceived` {number} The number of HTTP/2 frames received by the 41361cb0ef41Sopenharmony_ci `Http2Session`. 41371cb0ef41Sopenharmony_ci* `framesSent` {number} The number of HTTP/2 frames sent by the `Http2Session`. 41381cb0ef41Sopenharmony_ci* `maxConcurrentStreams` {number} The maximum number of streams concurrently 41391cb0ef41Sopenharmony_ci open during the lifetime of the `Http2Session`. 41401cb0ef41Sopenharmony_ci* `pingRTT` {number} The number of milliseconds elapsed since the transmission 41411cb0ef41Sopenharmony_ci of a `PING` frame and the reception of its acknowledgment. Only present if 41421cb0ef41Sopenharmony_ci a `PING` frame has been sent on the `Http2Session`. 41431cb0ef41Sopenharmony_ci* `streamAverageDuration` {number} The average duration (in milliseconds) for 41441cb0ef41Sopenharmony_ci all `Http2Stream` instances. 41451cb0ef41Sopenharmony_ci* `streamCount` {number} The number of `Http2Stream` instances processed by 41461cb0ef41Sopenharmony_ci the `Http2Session`. 41471cb0ef41Sopenharmony_ci* `type` {string} Either `'server'` or `'client'` to identify the type of 41481cb0ef41Sopenharmony_ci `Http2Session`. 41491cb0ef41Sopenharmony_ci 41501cb0ef41Sopenharmony_ci## Note on `:authority` and `host` 41511cb0ef41Sopenharmony_ci 41521cb0ef41Sopenharmony_ciHTTP/2 requires requests to have either the `:authority` pseudo-header 41531cb0ef41Sopenharmony_cior the `host` header. Prefer `:authority` when constructing an HTTP/2 41541cb0ef41Sopenharmony_cirequest directly, and `host` when converting from HTTP/1 (in proxies, 41551cb0ef41Sopenharmony_cifor instance). 41561cb0ef41Sopenharmony_ci 41571cb0ef41Sopenharmony_ciThe compatibility API falls back to `host` if `:authority` is not 41581cb0ef41Sopenharmony_cipresent. See [`request.authority`][] for more information. However, 41591cb0ef41Sopenharmony_ciif you don't use the compatibility API (or use `req.headers` directly), 41601cb0ef41Sopenharmony_ciyou need to implement any fall-back behavior yourself. 41611cb0ef41Sopenharmony_ci 41621cb0ef41Sopenharmony_ci[ALPN Protocol ID]: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids 41631cb0ef41Sopenharmony_ci[ALPN negotiation]: #alpn-negotiation 41641cb0ef41Sopenharmony_ci[Compatibility API]: #compatibility-api 41651cb0ef41Sopenharmony_ci[HTTP/1]: http.md 41661cb0ef41Sopenharmony_ci[HTTP/2]: https://tools.ietf.org/html/rfc7540 41671cb0ef41Sopenharmony_ci[HTTP/2 Headers Object]: #headers-object 41681cb0ef41Sopenharmony_ci[HTTP/2 Settings Object]: #settings-object 41691cb0ef41Sopenharmony_ci[HTTP/2 Unencrypted]: https://http2.github.io/faq/#does-http2-require-encryption 41701cb0ef41Sopenharmony_ci[HTTPS]: https.md 41711cb0ef41Sopenharmony_ci[Performance Observer]: perf_hooks.md 41721cb0ef41Sopenharmony_ci[RFC 7838]: https://tools.ietf.org/html/rfc7838 41731cb0ef41Sopenharmony_ci[RFC 8336]: https://tools.ietf.org/html/rfc8336 41741cb0ef41Sopenharmony_ci[RFC 8441]: https://tools.ietf.org/html/rfc8441 41751cb0ef41Sopenharmony_ci[Sensitive headers]: #sensitive-headers 41761cb0ef41Sopenharmony_ci[`'checkContinue'`]: #event-checkcontinue 41771cb0ef41Sopenharmony_ci[`'connect'`]: #event-connect 41781cb0ef41Sopenharmony_ci[`'request'`]: #event-request 41791cb0ef41Sopenharmony_ci[`'unknownProtocol'`]: #event-unknownprotocol 41801cb0ef41Sopenharmony_ci[`ClientHttp2Stream`]: #class-clienthttp2stream 41811cb0ef41Sopenharmony_ci[`Duplex`]: stream.md#class-streamduplex 41821cb0ef41Sopenharmony_ci[`Http2ServerRequest`]: #class-http2http2serverrequest 41831cb0ef41Sopenharmony_ci[`Http2ServerResponse`]: #class-http2http2serverresponse 41841cb0ef41Sopenharmony_ci[`Http2Session` and Sockets]: #http2session-and-sockets 41851cb0ef41Sopenharmony_ci[`Http2Session`'s `'stream'` event]: #event-stream 41861cb0ef41Sopenharmony_ci[`Http2Stream`]: #class-http2stream 41871cb0ef41Sopenharmony_ci[`ServerHttp2Stream`]: #class-serverhttp2stream 41881cb0ef41Sopenharmony_ci[`TypeError`]: errors.md#class-typeerror 41891cb0ef41Sopenharmony_ci[`http2.SecureServer`]: #class-http2secureserver 41901cb0ef41Sopenharmony_ci[`http2.Server`]: #class-http2server 41911cb0ef41Sopenharmony_ci[`http2.createSecureServer()`]: #http2createsecureserveroptions-onrequesthandler 41921cb0ef41Sopenharmony_ci[`http2.createServer()`]: #http2createserveroptions-onrequesthandler 41931cb0ef41Sopenharmony_ci[`http2session.close()`]: #http2sessionclosecallback 41941cb0ef41Sopenharmony_ci[`http2stream.pushStream()`]: #http2streampushstreamheaders-options-callback 41951cb0ef41Sopenharmony_ci[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import 41961cb0ef41Sopenharmony_ci[`net.Server.close()`]: net.md#serverclosecallback 41971cb0ef41Sopenharmony_ci[`net.Socket.bufferSize`]: net.md#socketbuffersize 41981cb0ef41Sopenharmony_ci[`net.Socket.prototype.ref()`]: net.md#socketref 41991cb0ef41Sopenharmony_ci[`net.Socket.prototype.unref()`]: net.md#socketunref 42001cb0ef41Sopenharmony_ci[`net.Socket`]: net.md#class-netsocket 42011cb0ef41Sopenharmony_ci[`net.connect()`]: net.md#netconnect 42021cb0ef41Sopenharmony_ci[`net.createServer()`]: net.md#netcreateserveroptions-connectionlistener 42031cb0ef41Sopenharmony_ci[`request.authority`]: #requestauthority 42041cb0ef41Sopenharmony_ci[`request.maxHeadersCount`]: http.md#requestmaxheaderscount 42051cb0ef41Sopenharmony_ci[`request.socket.getPeerCertificate()`]: tls.md#tlssocketgetpeercertificatedetailed 42061cb0ef41Sopenharmony_ci[`request.socket`]: #requestsocket 42071cb0ef41Sopenharmony_ci[`response.end()`]: #responseenddata-encoding-callback 42081cb0ef41Sopenharmony_ci[`response.setHeader()`]: #responsesetheadername-value 42091cb0ef41Sopenharmony_ci[`response.socket`]: #responsesocket 42101cb0ef41Sopenharmony_ci[`response.writableEnded`]: #responsewritableended 42111cb0ef41Sopenharmony_ci[`response.write()`]: #responsewritechunk-encoding-callback 42121cb0ef41Sopenharmony_ci[`response.write(data, encoding)`]: http.md#responsewritechunk-encoding-callback 42131cb0ef41Sopenharmony_ci[`response.writeContinue()`]: #responsewritecontinue 42141cb0ef41Sopenharmony_ci[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers 42151cb0ef41Sopenharmony_ci[`server.maxHeadersCount`]: http.md#servermaxheaderscount 42161cb0ef41Sopenharmony_ci[`tls.Server.close()`]: tls.md#serverclosecallback 42171cb0ef41Sopenharmony_ci[`tls.TLSSocket`]: tls.md#class-tlstlssocket 42181cb0ef41Sopenharmony_ci[`tls.connect()`]: tls.md#tlsconnectoptions-callback 42191cb0ef41Sopenharmony_ci[`tls.createServer()`]: tls.md#tlscreateserveroptions-secureconnectionlistener 42201cb0ef41Sopenharmony_ci[`writable.writableFinished`]: stream.md#writablewritablefinished 42211cb0ef41Sopenharmony_ci[error code]: #error-codes-for-rst_stream-and-goaway 4222