• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.20.1">
7  <title>HTTP/2 | Node.js v14.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/http2.html">
12</head>
13<body class="alt apidoc" id="api-section-http2">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="http2" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.20.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="http2.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/http2.html">18.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v17.x/api/http2.html">17.x</a></li>
121<li><a href="https://nodejs.org/docs/latest-v16.x/api/http2.html">16.x <b>LTS</b></a></li>
122<li><a href="https://nodejs.org/docs/latest-v15.x/api/http2.html">15.x</a></li>
123<li><a href="https://nodejs.org/docs/latest-v14.x/api/http2.html">14.x <b>LTS</b></a></li>
124<li><a href="https://nodejs.org/docs/latest-v13.x/api/http2.html">13.x</a></li>
125<li><a href="https://nodejs.org/docs/latest-v12.x/api/http2.html">12.x <b>LTS</b></a></li>
126<li><a href="https://nodejs.org/docs/latest-v11.x/api/http2.html">11.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v10.x/api/http2.html">10.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v9.x/api/http2.html">9.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v8.x/api/http2.html">8.x</a></li></ol>
130    </li>
131
132            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/http2.md">Edit on GitHub</a></li>
133          </ul>
134        </div>
135        <hr>
136      </header>
137
138      <details id="toc" open><summary>Table of contents</summary><ul>
139<li><span class="stability_2"><a href="#http2_http_2">HTTP/2</a></span>
140<ul>
141<li><a href="#http2_core_api">Core API</a>
142<ul>
143<li><a href="#http2_server_side_example">Server-side example</a></li>
144<li><a href="#http2_client_side_example">Client-side example</a></li>
145<li><a href="#http2_class_http2session">Class: <code>Http2Session</code></a>
146<ul>
147<li><a href="#http2_http2session_and_sockets"><code>Http2Session</code> and sockets</a></li>
148<li><a href="#http2_event_close">Event: <code>'close'</code></a></li>
149<li><a href="#http2_event_connect">Event: <code>'connect'</code></a></li>
150<li><a href="#http2_event_error">Event: <code>'error'</code></a></li>
151<li><a href="#http2_event_frameerror">Event: <code>'frameError'</code></a></li>
152<li><a href="#http2_event_goaway">Event: <code>'goaway'</code></a></li>
153<li><a href="#http2_event_localsettings">Event: <code>'localSettings'</code></a></li>
154<li><a href="#http2_event_ping">Event: <code>'ping'</code></a></li>
155<li><a href="#http2_event_remotesettings">Event: <code>'remoteSettings'</code></a></li>
156<li><a href="#http2_event_stream">Event: <code>'stream'</code></a></li>
157<li><a href="#http2_event_timeout">Event: <code>'timeout'</code></a></li>
158<li><a href="#http2_http2session_alpnprotocol"><code>http2session.alpnProtocol</code></a></li>
159<li><a href="#http2_http2session_close_callback"><code>http2session.close([callback])</code></a></li>
160<li><a href="#http2_http2session_closed"><code>http2session.closed</code></a></li>
161<li><a href="#http2_http2session_connecting"><code>http2session.connecting</code></a></li>
162<li><a href="#http2_http2session_destroy_error_code"><code>http2session.destroy([error][, code])</code></a></li>
163<li><a href="#http2_http2session_destroyed"><code>http2session.destroyed</code></a></li>
164<li><a href="#http2_http2session_encrypted"><code>http2session.encrypted</code></a></li>
165<li><a href="#http2_http2session_goaway_code_laststreamid_opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li>
166<li><a href="#http2_http2session_localsettings"><code>http2session.localSettings</code></a></li>
167<li><a href="#http2_http2session_originset"><code>http2session.originSet</code></a></li>
168<li><a href="#http2_http2session_pendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li>
169<li><a href="#http2_http2session_ping_payload_callback"><code>http2session.ping([payload, ]callback)</code></a></li>
170<li><a href="#http2_http2session_ref"><code>http2session.ref()</code></a></li>
171<li><a href="#http2_http2session_remotesettings"><code>http2session.remoteSettings</code></a></li>
172<li><a href="#http2_http2session_setlocalwindowsize_windowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li>
173<li><a href="#http2_http2session_settimeout_msecs_callback"><code>http2session.setTimeout(msecs, callback)</code></a></li>
174<li><a href="#http2_http2session_socket"><code>http2session.socket</code></a></li>
175<li><a href="#http2_http2session_state"><code>http2session.state</code></a></li>
176<li><a href="#http2_http2session_settings_settings_callback"><code>http2session.settings([settings][, callback])</code></a></li>
177<li><a href="#http2_http2session_type"><code>http2session.type</code></a></li>
178<li><a href="#http2_http2session_unref"><code>http2session.unref()</code></a></li>
179</ul>
180</li>
181<li><a href="#http2_class_serverhttp2session">Class: <code>ServerHttp2Session</code></a>
182<ul>
183<li><a href="#http2_serverhttp2session_altsvc_alt_originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li>
184<li><a href="#http2_specifying_alternative_services">Specifying alternative services</a></li>
185<li><a href="#http2_serverhttp2session_origin_origins"><code>serverhttp2session.origin(...origins)</code></a></li>
186</ul>
187</li>
188<li><a href="#http2_class_clienthttp2session">Class: <code>ClientHttp2Session</code></a>
189<ul>
190<li><a href="#http2_event_altsvc">Event: <code>'altsvc'</code></a></li>
191<li><a href="#http2_event_origin">Event: <code>'origin'</code></a></li>
192<li><a href="#http2_clienthttp2session_request_headers_options"><code>clienthttp2session.request(headers[, options])</code></a></li>
193</ul>
194</li>
195<li><a href="#http2_class_http2stream">Class: <code>Http2Stream</code></a>
196<ul>
197<li><a href="#http2_http2stream_lifecycle"><code>Http2Stream</code> Lifecycle</a>
198<ul>
199<li><a href="#http2_creation">Creation</a></li>
200<li><a href="#http2_destruction">Destruction</a></li>
201</ul>
202</li>
203<li><a href="#http2_event_aborted">Event: <code>'aborted'</code></a></li>
204<li><a href="#http2_event_close_1">Event: <code>'close'</code></a></li>
205<li><a href="#http2_event_error_1">Event: <code>'error'</code></a></li>
206<li><a href="#http2_event_frameerror_1">Event: <code>'frameError'</code></a></li>
207<li><a href="#http2_event_ready">Event: <code>'ready'</code></a></li>
208<li><a href="#http2_event_timeout_1">Event: <code>'timeout'</code></a></li>
209<li><a href="#http2_event_trailers">Event: <code>'trailers'</code></a></li>
210<li><a href="#http2_event_wanttrailers">Event: <code>'wantTrailers'</code></a></li>
211<li><a href="#http2_http2stream_aborted"><code>http2stream.aborted</code></a></li>
212<li><a href="#http2_http2stream_buffersize"><code>http2stream.bufferSize</code></a></li>
213<li><a href="#http2_http2stream_close_code_callback"><code>http2stream.close(code[, callback])</code></a></li>
214<li><a href="#http2_http2stream_closed"><code>http2stream.closed</code></a></li>
215<li><a href="#http2_http2stream_destroyed"><code>http2stream.destroyed</code></a></li>
216<li><a href="#http2_http2stream_endafterheaders"><code>http2stream.endAfterHeaders</code></a></li>
217<li><a href="#http2_http2stream_id"><code>http2stream.id</code></a></li>
218<li><a href="#http2_http2stream_pending"><code>http2stream.pending</code></a></li>
219<li><a href="#http2_http2stream_priority_options"><code>http2stream.priority(options)</code></a></li>
220<li><a href="#http2_http2stream_rstcode"><code>http2stream.rstCode</code></a></li>
221<li><a href="#http2_http2stream_sentheaders"><code>http2stream.sentHeaders</code></a></li>
222<li><a href="#http2_http2stream_sentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li>
223<li><a href="#http2_http2stream_senttrailers"><code>http2stream.sentTrailers</code></a></li>
224<li><a href="#http2_http2stream_session"><code>http2stream.session</code></a></li>
225<li><a href="#http2_http2stream_settimeout_msecs_callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li>
226<li><a href="#http2_http2stream_state"><code>http2stream.state</code></a></li>
227<li><a href="#http2_http2stream_sendtrailers_headers"><code>http2stream.sendTrailers(headers)</code></a></li>
228</ul>
229</li>
230<li><a href="#http2_class_clienthttp2stream">Class: <code>ClientHttp2Stream</code></a>
231<ul>
232<li><a href="#http2_event_continue">Event: <code>'continue'</code></a></li>
233<li><a href="#http2_event_headers">Event: <code>'headers'</code></a></li>
234<li><a href="#http2_event_push">Event: <code>'push'</code></a></li>
235<li><a href="#http2_event_response">Event: <code>'response'</code></a></li>
236</ul>
237</li>
238<li><a href="#http2_class_serverhttp2stream">Class: <code>ServerHttp2Stream</code></a>
239<ul>
240<li><a href="#http2_http2stream_additionalheaders_headers"><code>http2stream.additionalHeaders(headers)</code></a></li>
241<li><a href="#http2_http2stream_headerssent"><code>http2stream.headersSent</code></a></li>
242<li><a href="#http2_http2stream_pushallowed"><code>http2stream.pushAllowed</code></a></li>
243<li><a href="#http2_http2stream_pushstream_headers_options_callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li>
244<li><a href="#http2_http2stream_respond_headers_options"><code>http2stream.respond([headers[, options]])</code></a></li>
245<li><a href="#http2_http2stream_respondwithfd_fd_headers_options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li>
246<li><a href="#http2_http2stream_respondwithfile_path_headers_options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li>
247</ul>
248</li>
249<li><a href="#http2_class_http2server">Class: <code>Http2Server</code></a>
250<ul>
251<li><a href="#http2_event_checkcontinue">Event: <code>'checkContinue'</code></a></li>
252<li><a href="#http2_event_connection">Event: <code>'connection'</code></a></li>
253<li><a href="#http2_event_request">Event: <code>'request'</code></a></li>
254<li><a href="#http2_event_session">Event: <code>'session'</code></a></li>
255<li><a href="#http2_event_sessionerror">Event: <code>'sessionError'</code></a></li>
256<li><a href="#http2_event_stream_1">Event: <code>'stream'</code></a></li>
257<li><a href="#http2_event_timeout_2">Event: <code>'timeout'</code></a></li>
258<li><a href="#http2_server_close_callback"><code>server.close([callback])</code></a></li>
259<li><a href="#http2_server_settimeout_msecs_callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
260<li><a href="#http2_server_timeout"><code>server.timeout</code></a></li>
261<li><a href="#http2_server_updatesettings_settings"><code>server.updateSettings([settings])</code></a></li>
262</ul>
263</li>
264<li><a href="#http2_class_http2secureserver">Class: <code>Http2SecureServer</code></a>
265<ul>
266<li><a href="#http2_event_checkcontinue_1">Event: <code>'checkContinue'</code></a></li>
267<li><a href="#http2_event_connection_1">Event: <code>'connection'</code></a></li>
268<li><a href="#http2_event_request_1">Event: <code>'request'</code></a></li>
269<li><a href="#http2_event_session_1">Event: <code>'session'</code></a></li>
270<li><a href="#http2_event_sessionerror_1">Event: <code>'sessionError'</code></a></li>
271<li><a href="#http2_event_stream_2">Event: <code>'stream'</code></a></li>
272<li><a href="#http2_event_timeout_3">Event: <code>'timeout'</code></a></li>
273<li><a href="#http2_event_unknownprotocol">Event: <code>'unknownProtocol'</code></a></li>
274<li><a href="#http2_server_close_callback_1"><code>server.close([callback])</code></a></li>
275<li><a href="#http2_server_settimeout_msecs_callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li>
276<li><a href="#http2_server_timeout_1"><code>server.timeout</code></a></li>
277<li><a href="#http2_server_updatesettings_settings_1"><code>server.updateSettings([settings])</code></a></li>
278</ul>
279</li>
280<li><a href="#http2_http2_createserver_options_onrequesthandler"><code>http2.createServer(options[, onRequestHandler])</code></a></li>
281<li><a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li>
282<li><a href="#http2_http2_connect_authority_options_listener"><code>http2.connect(authority[, options][, listener])</code></a></li>
283<li><a href="#http2_http2_constants"><code>http2.constants</code></a>
284<ul>
285<li><a href="#http2_error_codes_for_rst_stream_and_goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li>
286</ul>
287</li>
288<li><a href="#http2_http2_getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li>
289<li><a href="#http2_http2_getpackedsettings_settings"><code>http2.getPackedSettings([settings])</code></a></li>
290<li><a href="#http2_http2_getunpackedsettings_buf"><code>http2.getUnpackedSettings(buf)</code></a></li>
291<li><a href="#http2_http2_sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li>
292<li><a href="#http2_headers_object">Headers object</a>
293<ul>
294<li><a href="#http2_sensitive_headers">Sensitive headers</a></li>
295</ul>
296</li>
297<li><a href="#http2_settings_object">Settings object</a></li>
298<li><a href="#http2_error_handling">Error handling</a></li>
299<li><a href="#http2_invalid_character_handling_in_header_names_and_values">Invalid character handling in header names and values</a></li>
300<li><a href="#http2_push_streams_on_the_client">Push streams on the client</a></li>
301<li><a href="#http2_supporting_the_connect_method">Supporting the <code>CONNECT</code> method</a></li>
302<li><a href="#http2_the_extended_connect_protocol">The extended <code>CONNECT</code> protocol</a></li>
303</ul>
304</li>
305<li><a href="#http2_compatibility_api">Compatibility API</a>
306<ul>
307<li><a href="#http2_alpn_negotiation">ALPN negotiation</a></li>
308<li><a href="#http2_class_http2_http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a>
309<ul>
310<li><a href="#http2_event_aborted_1">Event: <code>'aborted'</code></a></li>
311<li><a href="#http2_event_close_2">Event: <code>'close'</code></a></li>
312<li><a href="#http2_request_aborted"><code>request.aborted</code></a></li>
313<li><a href="#http2_request_authority"><code>request.authority</code></a></li>
314<li><a href="#http2_request_complete"><code>request.complete</code></a></li>
315<li><span class="stability_0"><a href="#http2_request_connection"><code>request.connection</code></a></span></li>
316<li><a href="#http2_request_destroy_error"><code>request.destroy([error])</code></a></li>
317<li><a href="#http2_request_headers"><code>request.headers</code></a></li>
318<li><a href="#http2_request_httpversion"><code>request.httpVersion</code></a></li>
319<li><a href="#http2_request_method"><code>request.method</code></a></li>
320<li><a href="#http2_request_rawheaders"><code>request.rawHeaders</code></a></li>
321<li><a href="#http2_request_rawtrailers"><code>request.rawTrailers</code></a></li>
322<li><a href="#http2_request_scheme"><code>request.scheme</code></a></li>
323<li><a href="#http2_request_settimeout_msecs_callback"><code>request.setTimeout(msecs, callback)</code></a></li>
324<li><a href="#http2_request_socket"><code>request.socket</code></a></li>
325<li><a href="#http2_request_stream"><code>request.stream</code></a></li>
326<li><a href="#http2_request_trailers"><code>request.trailers</code></a></li>
327<li><a href="#http2_request_url"><code>request.url</code></a></li>
328</ul>
329</li>
330<li><a href="#http2_class_http2_http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a>
331<ul>
332<li><a href="#http2_event_close_3">Event: <code>'close'</code></a></li>
333<li><a href="#http2_event_finish">Event: <code>'finish'</code></a></li>
334<li><a href="#http2_response_addtrailers_headers"><code>response.addTrailers(headers)</code></a></li>
335<li><span class="stability_0"><a href="#http2_response_connection"><code>response.connection</code></a></span></li>
336<li><a href="#http2_response_createpushresponse_headers_callback"><code>response.createPushResponse(headers, callback)</code></a></li>
337<li><a href="#http2_response_end_data_encoding_callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
338<li><span class="stability_0"><a href="#http2_response_finished"><code>response.finished</code></a></span></li>
339<li><a href="#http2_response_getheader_name"><code>response.getHeader(name)</code></a></li>
340<li><a href="#http2_response_getheadernames"><code>response.getHeaderNames()</code></a></li>
341<li><a href="#http2_response_getheaders"><code>response.getHeaders()</code></a></li>
342<li><a href="#http2_response_hasheader_name"><code>response.hasHeader(name)</code></a></li>
343<li><a href="#http2_response_headerssent"><code>response.headersSent</code></a></li>
344<li><a href="#http2_response_removeheader_name"><code>response.removeHeader(name)</code></a></li>
345<li><a href="#http2_response_senddate"><code>response.sendDate</code></a></li>
346<li><a href="#http2_response_setheader_name_value"><code>response.setHeader(name, value)</code></a></li>
347<li><a href="#http2_response_settimeout_msecs_callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
348<li><a href="#http2_response_socket"><code>response.socket</code></a></li>
349<li><a href="#http2_response_statuscode"><code>response.statusCode</code></a></li>
350<li><a href="#http2_response_statusmessage"><code>response.statusMessage</code></a></li>
351<li><a href="#http2_response_stream"><code>response.stream</code></a></li>
352<li><a href="#http2_response_writableended"><code>response.writableEnded</code></a></li>
353<li><a href="#http2_response_write_chunk_encoding_callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
354<li><a href="#http2_response_writecontinue"><code>response.writeContinue()</code></a></li>
355<li><a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
356</ul>
357</li>
358</ul>
359</li>
360<li><a href="#http2_collecting_http_2_performance_metrics">Collecting HTTP/2 performance metrics</a></li>
361</ul>
362</li>
363</ul></details>
364
365      <div id="apicontent">
366        <h2>HTTP/2<span><a class="mark" href="#http2_http_2" id="http2_http_2">#</a></span></h2>
367<div class="api_metadata">
368<details class="changelog"><summary>History</summary>
369<table>
370<tbody><tr><th>Version</th><th>Changes</th></tr>
371<tr><td>v14.17.0</td>
372<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr>
373<tr><td>v10.10.0</td>
374<td><p>HTTP/2 is now Stable. Previously, it had been Experimental.</p></td></tr>
375<tr><td>v8.4.0</td>
376<td><p><span>Added in: v8.4.0</span></p></td></tr>
377</tbody></table>
378</details>
379</div>
380
381<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
382<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/http2.js">lib/http2.js</a></p>
383<p>The <code>http2</code> module provides an implementation of the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> protocol. It
384can be accessed using:</p>
385<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);</code></pre>
386<section><h3>Core API<span><a class="mark" href="#http2_core_api" id="http2_core_api">#</a></span></h3>
387<p>The Core API provides a low-level interface designed specifically around
388support for HTTP/2 protocol features. It is specifically <em>not</em> designed for
389compatibility with the existing <a href="http.html">HTTP/1</a> module API. However,
390the <a href="#http2_compatibility_api">Compatibility API</a> is.</p>
391<p>The <code>http2</code> Core API is much more symmetric between client and server than the
392<code>http</code> API. For instance, most events, like <code>'error'</code>, <code>'connect'</code> and
393<code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
394<h4>Server-side example<span><a class="mark" href="#http2_server_side_example" id="http2_server_side_example">#</a></span></h4>
395<p>The following illustrates a simple HTTP/2 server using the Core API.
396Since there are no browsers known that support
397<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
398<a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
399with browser clients.</p>
400<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
401<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
402
403<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>({
404  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-privkey.pem'</span>),
405  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>)
406});
407server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err));
408
409server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
410  <span class="hljs-comment">// stream is a Duplex</span>
411  stream.<span class="hljs-title function_">respond</span>({
412    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
413    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
414  });
415  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
416});
417
418server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code></pre>
419<p>To generate the certificate and key for this example, run:</p>
420<pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span class="hljs-string">'/CN=localhost'</span> \
421  -keyout localhost-privkey.pem -out localhost-cert.pem</code></pre>
422<h4>Client-side example<span><a class="mark" href="#http2_client_side_example" id="http2_client_side_example">#</a></span></h4>
423<p>The following illustrates an HTTP/2 client:</p>
424<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
425<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
426<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:8443'</span>, {
427  <span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>)
428});
429client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err));
430
431<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });
432
433req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> {
434  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) {
435    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${name}</span>: <span class="hljs-subst">${headers[name]}</span>`</span>);
436  }
437});
438
439req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
440<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
441req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { data += chunk; });
442req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
443  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`\n<span class="hljs-subst">${data}</span>`</span>);
444  client.<span class="hljs-title function_">close</span>();
445});
446req.<span class="hljs-title function_">end</span>();</code></pre>
447<h4>Class: <code>Http2Session</code><span><a class="mark" href="#http2_class_http2session" id="http2_class_http2session">#</a></span></h4>
448<div class="api_metadata">
449<span>Added in: v8.4.0</span>
450</div>
451<ul>
452<li>Extends: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
453</ul>
454<p>Instances of the <code>http2.Http2Session</code> class represent an active communications
455session between an HTTP/2 client and server. Instances of this class are <em>not</em>
456intended to be constructed directly by user code.</p>
457<p>Each <code>Http2Session</code> instance will exhibit slightly different behaviors
458depending on whether it is operating as a server or a client. The
459<code>http2session.type</code> property can be used to determine the mode in which an
460<code>Http2Session</code> is operating. On the server side, user code should rarely
461have occasion to work with the <code>Http2Session</code> object directly, with most
462actions typically taken through interactions with either the <code>Http2Server</code> or
463<code>Http2Stream</code> objects.</p>
464<p>User code will not create <code>Http2Session</code> instances directly. Server-side
465<code>Http2Session</code> instances are created by the <code>Http2Server</code> instance when a
466new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
467created using the <code>http2.connect()</code> method.</p>
468<h5><code>Http2Session</code> and sockets<span><a class="mark" href="#http2_http2session_and_sockets" id="http2_http2session_and_sockets">#</a></span></h5>
469<p>Every <code>Http2Session</code> instance is associated with exactly one <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> or
470<a href="tls.html#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> when it is created. When either the <code>Socket</code> or the
471<code>Http2Session</code> are destroyed, both will be destroyed.</p>
472<p>Because of the specific serialization and processing requirements imposed
473by the HTTP/2 protocol, it is not recommended for user code to read data from
474or write data to a <code>Socket</code> instance bound to a <code>Http2Session</code>. Doing so can
475put the HTTP/2 session into an indeterminate state causing the session and
476the socket to become unusable.</p>
477<p>Once a <code>Socket</code> has been bound to an <code>Http2Session</code>, user code should rely
478solely on the API of the <code>Http2Session</code>.</p>
479<h5>Event: <code>'close'</code><span><a class="mark" href="#http2_event_close" id="http2_event_close">#</a></span></h5>
480<div class="api_metadata">
481<span>Added in: v8.4.0</span>
482</div>
483<p>The <code>'close'</code> event is emitted once the <code>Http2Session</code> has been destroyed. Its
484listener does not expect any arguments.</p>
485<h5>Event: <code>'connect'</code><span><a class="mark" href="#http2_event_connect" id="http2_event_connect">#</a></span></h5>
486<div class="api_metadata">
487<span>Added in: v8.4.0</span>
488</div>
489<ul>
490<li><code>session</code> <a href="http2.html#http2_class_http2session" class="type">&#x3C;Http2Session></a></li>
491<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a></li>
492</ul>
493<p>The <code>'connect'</code> event is emitted once the <code>Http2Session</code> has been successfully
494connected to the remote peer and communication may begin.</p>
495<p>User code will typically not listen for this event directly.</p>
496<h5>Event: <code>'error'</code><span><a class="mark" href="#http2_event_error" id="http2_event_error">#</a></span></h5>
497<div class="api_metadata">
498<span>Added in: v8.4.0</span>
499</div>
500<ul>
501<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
502</ul>
503<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
504an <code>Http2Session</code>.</p>
505<h5>Event: <code>'frameError'</code><span><a class="mark" href="#http2_event_frameerror" id="http2_event_frameerror">#</a></span></h5>
506<div class="api_metadata">
507<span>Added in: v8.4.0</span>
508</div>
509<ul>
510<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The frame type.</li>
511<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The error code.</li>
512<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The stream id (or <code>0</code> if the frame isn't associated with a
513stream).</li>
514</ul>
515<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
516send a frame on the session. If the frame that could not be sent is associated
517with a specific <code>Http2Stream</code>, an attempt to emit a <code>'frameError'</code> event on the
518<code>Http2Stream</code> is made.</p>
519<p>If the <code>'frameError'</code> event is associated with a stream, the stream will be
520closed and destroyed immediately following the <code>'frameError'</code> event. If the
521event is not associated with a stream, the <code>Http2Session</code> will be shut down
522immediately following the <code>'frameError'</code> event.</p>
523<h5>Event: <code>'goaway'</code><span><a class="mark" href="#http2_event_goaway" id="http2_event_goaway">#</a></span></h5>
524<div class="api_metadata">
525<span>Added in: v8.4.0</span>
526</div>
527<ul>
528<li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The HTTP/2 error code specified in the <code>GOAWAY</code> frame.</li>
529<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The ID of the last stream the remote peer successfully
530processed (or <code>0</code> if no ID is specified).</li>
531<li><code>opaqueData</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> If additional opaque data was included in the <code>GOAWAY</code>
532frame, a <code>Buffer</code> instance will be passed containing that data.</li>
533</ul>
534<p>The <code>'goaway'</code> event is emitted when a <code>GOAWAY</code> frame is received.</p>
535<p>The <code>Http2Session</code> instance will be shut down automatically when the <code>'goaway'</code>
536event is emitted.</p>
537<h5>Event: <code>'localSettings'</code><span><a class="mark" href="#http2_event_localsettings" id="http2_event_localsettings">#</a></span></h5>
538<div class="api_metadata">
539<span>Added in: v8.4.0</span>
540</div>
541<ul>
542<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
543</ul>
544<p>The <code>'localSettings'</code> event is emitted when an acknowledgment <code>SETTINGS</code> frame
545has been received.</p>
546<p>When using <code>http2session.settings()</code> to submit new settings, the modified
547settings do not take effect until the <code>'localSettings'</code> event is emitted.</p>
548<pre><code class="language-js">session.<span class="hljs-title function_">settings</span>({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> });
549
550session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
551  <span class="hljs-comment">/* Use the new settings */</span>
552});</code></pre>
553<h5>Event: <code>'ping'</code><span><a class="mark" href="#http2_event_ping" id="http2_event_ping">#</a></span></h5>
554<div class="api_metadata">
555<span>Added in: v10.12.0</span>
556</div>
557<ul>
558<li><code>payload</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> The <code>PING</code> frame 8-byte payload</li>
559</ul>
560<p>The <code>'ping'</code> event is emitted whenever a <code>PING</code> frame is received from the
561connected peer.</p>
562<h5>Event: <code>'remoteSettings'</code><span><a class="mark" href="#http2_event_remotesettings" id="http2_event_remotesettings">#</a></span></h5>
563<div class="api_metadata">
564<span>Added in: v8.4.0</span>
565</div>
566<ul>
567<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
568</ul>
569<p>The <code>'remoteSettings'</code> event is emitted when a new <code>SETTINGS</code> frame is received
570from the connected peer.</p>
571<pre><code class="language-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
572  <span class="hljs-comment">/* Use the new settings */</span>
573});</code></pre>
574<h5>Event: <code>'stream'</code><span><a class="mark" href="#http2_event_stream" id="http2_event_stream">#</a></span></h5>
575<div class="api_metadata">
576<span>Added in: v8.4.0</span>
577</div>
578<ul>
579<li><code>stream</code> <a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
580<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
581<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
582<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
583their respective values.</li>
584</ul>
585<p>The <code>'stream'</code> event is emitted when a new <code>Http2Stream</code> is created.</p>
586<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
587session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> {
588  <span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span>];
589  <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
590  <span class="hljs-comment">// ...</span>
591  stream.<span class="hljs-title function_">respond</span>({
592    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
593    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>
594  });
595  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
596  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
597});</code></pre>
598<p>On the server side, user code will typically not listen for this event directly,
599and would instead register a handler for the <code>'stream'</code> event emitted by the
600<code>net.Server</code> or <code>tls.Server</code> instances returned by <code>http2.createServer()</code> and
601<code>http2.createSecureServer()</code>, respectively, as in the example below:</p>
602<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
603
604<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
605<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
606
607server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
608  stream.<span class="hljs-title function_">respond</span>({
609    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
610    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
611  });
612  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error));
613  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
614});
615
616server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code></pre>
617<p>Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence,
618a network error will destroy each individual stream and must be handled on the
619stream level, as shown above.</p>
620<h5>Event: <code>'timeout'</code><span><a class="mark" href="#http2_event_timeout" id="http2_event_timeout">#</a></span></h5>
621<div class="api_metadata">
622<span>Added in: v8.4.0</span>
623</div>
624<p>After the <code>http2session.setTimeout()</code> method is used to set the timeout period
625for this <code>Http2Session</code>, the <code>'timeout'</code> event is emitted if there is no
626activity on the <code>Http2Session</code> after the configured number of milliseconds.
627Its listener does not expect any arguments.</p>
628<pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">2000</span>);
629session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });</code></pre>
630<h5><code>http2session.alpnProtocol</code><span><a class="mark" href="#http2_http2session_alpnprotocol" id="http2_http2session_alpnprotocol">#</a></span></h5>
631<div class="api_metadata">
632<span>Added in: v9.4.0</span>
633</div>
634<ul>
635<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
636</ul>
637<p>Value will be <code>undefined</code> if the <code>Http2Session</code> is not yet connected to a
638socket, <code>h2c</code> if the <code>Http2Session</code> is not connected to a <code>TLSSocket</code>, or
639will return the value of the connected <code>TLSSocket</code>'s own <code>alpnProtocol</code>
640property.</p>
641<h5><code>http2session.close([callback])</code><span><a class="mark" href="#http2_http2session_close_callback" id="http2_http2session_close_callback">#</a></span></h5>
642<div class="api_metadata">
643<span>Added in: v9.4.0</span>
644</div>
645<ul>
646<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
647</ul>
648<p>Gracefully closes the <code>Http2Session</code>, allowing any existing streams to
649complete on their own and preventing new <code>Http2Stream</code> instances from being
650created. Once closed, <code>http2session.destroy()</code> <em>might</em> be called if there
651are no open <code>Http2Stream</code> instances.</p>
652<p>If specified, the <code>callback</code> function is registered as a handler for the
653<code>'close'</code> event.</p>
654<h5><code>http2session.closed</code><span><a class="mark" href="#http2_http2session_closed" id="http2_http2session_closed">#</a></span></h5>
655<div class="api_metadata">
656<span>Added in: v9.4.0</span>
657</div>
658<ul>
659<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
660</ul>
661<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been closed, otherwise
662<code>false</code>.</p>
663<h5><code>http2session.connecting</code><span><a class="mark" href="#http2_http2session_connecting" id="http2_http2session_connecting">#</a></span></h5>
664<div class="api_metadata">
665<span>Added in: v10.0.0</span>
666</div>
667<ul>
668<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
669</ul>
670<p>Will be <code>true</code> if this <code>Http2Session</code> instance is still connecting, will be set
671to <code>false</code> before emitting <code>connect</code> event and/or calling the <code>http2.connect</code>
672callback.</p>
673<h5><code>http2session.destroy([error][, code])</code><span><a class="mark" href="#http2_http2session_destroy_error_code" id="http2_http2session_destroy_error_code">#</a></span></h5>
674<div class="api_metadata">
675<span>Added in: v8.4.0</span>
676</div>
677<ul>
678<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> An <code>Error</code> object if the <code>Http2Session</code> is being destroyed
679due to an error.</li>
680<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The HTTP/2 error code to send in the final <code>GOAWAY</code> frame.
681If unspecified, and <code>error</code> is not undefined, the default is <code>INTERNAL_ERROR</code>,
682otherwise defaults to <code>NO_ERROR</code>.</li>
683</ul>
684<p>Immediately terminates the <code>Http2Session</code> and the associated <code>net.Socket</code> or
685<code>tls.TLSSocket</code>.</p>
686<p>Once destroyed, the <code>Http2Session</code> will emit the <code>'close'</code> event. If <code>error</code>
687is not undefined, an <code>'error'</code> event will be emitted immediately before the
688<code>'close'</code> event.</p>
689<p>If there are any remaining open <code>Http2Streams</code> associated with the
690<code>Http2Session</code>, those will also be destroyed.</p>
691<h5><code>http2session.destroyed</code><span><a class="mark" href="#http2_http2session_destroyed" id="http2_http2session_destroyed">#</a></span></h5>
692<div class="api_metadata">
693<span>Added in: v8.4.0</span>
694</div>
695<ul>
696<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
697</ul>
698<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been destroyed and must no
699longer be used, otherwise <code>false</code>.</p>
700<h5><code>http2session.encrypted</code><span><a class="mark" href="#http2_http2session_encrypted" id="http2_http2session_encrypted">#</a></span></h5>
701<div class="api_metadata">
702<span>Added in: v9.4.0</span>
703</div>
704<ul>
705<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
706</ul>
707<p>Value is <code>undefined</code> if the <code>Http2Session</code> session socket has not yet been
708connected, <code>true</code> if the <code>Http2Session</code> is connected with a <code>TLSSocket</code>,
709and <code>false</code> if the <code>Http2Session</code> is connected to any other kind of socket
710or stream.</p>
711<h5><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code><span><a class="mark" href="#http2_http2session_goaway_code_laststreamid_opaquedata" id="http2_http2session_goaway_code_laststreamid_opaquedata">#</a></span></h5>
712<div class="api_metadata">
713<span>Added in: v9.4.0</span>
714</div>
715<ul>
716<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An HTTP/2 error code</li>
717<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric ID of the last processed <code>Http2Stream</code></li>
718<li><code>opaqueData</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A <code>TypedArray</code> or <code>DataView</code>
719instance containing additional data to be carried within the <code>GOAWAY</code> frame.</li>
720</ul>
721<p>Transmits a <code>GOAWAY</code> frame to the connected peer <em>without</em> shutting down the
722<code>Http2Session</code>.</p>
723<h5><code>http2session.localSettings</code><span><a class="mark" href="#http2_http2session_localsettings" id="http2_http2session_localsettings">#</a></span></h5>
724<div class="api_metadata">
725<span>Added in: v8.4.0</span>
726</div>
727<ul>
728<li><a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
729</ul>
730<p>A prototype-less object describing the current local settings of this
731<code>Http2Session</code>. The local settings are local to <em>this</em> <code>Http2Session</code> instance.</p>
732<h5><code>http2session.originSet</code><span><a class="mark" href="#http2_http2session_originset" id="http2_http2session_originset">#</a></span></h5>
733<div class="api_metadata">
734<span>Added in: v9.4.0</span>
735</div>
736<ul>
737<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
738</ul>
739<p>If the <code>Http2Session</code> is connected to a <code>TLSSocket</code>, the <code>originSet</code> property
740will return an <code>Array</code> of origins for which the <code>Http2Session</code> may be
741considered authoritative.</p>
742<p>The <code>originSet</code> property is only available when using a secure TLS connection.</p>
743<h5><code>http2session.pendingSettingsAck</code><span><a class="mark" href="#http2_http2session_pendingsettingsack" id="http2_http2session_pendingsettingsack">#</a></span></h5>
744<div class="api_metadata">
745<span>Added in: v8.4.0</span>
746</div>
747<ul>
748<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
749</ul>
750<p>Indicates whether the <code>Http2Session</code> is currently waiting for acknowledgment of
751a sent <code>SETTINGS</code> frame. Will be <code>true</code> after calling the
752<code>http2session.settings()</code> method. Will be <code>false</code> once all sent <code>SETTINGS</code>
753frames have been acknowledged.</p>
754<h5><code>http2session.ping([payload, ]callback)</code><span><a class="mark" href="#http2_http2session_ping_payload_callback" id="http2_http2session_ping_payload_callback">#</a></span></h5>
755<div class="api_metadata">
756<span>Added in: v8.9.3</span>
757</div>
758<ul>
759<li><code>payload</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Optional ping payload.</li>
760<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
761<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
762</ul>
763<p>Sends a <code>PING</code> frame to the connected HTTP/2 peer. A <code>callback</code> function must
764be provided. The method will return <code>true</code> if the <code>PING</code> was sent, <code>false</code>
765otherwise.</p>
766<p>The maximum number of outstanding (unacknowledged) pings is determined by the
767<code>maxOutstandingPings</code> configuration option. The default maximum is 10.</p>
768<p>If provided, the <code>payload</code> must be a <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>
769containing 8 bytes of data that will be transmitted with the <code>PING</code> and
770returned with the ping acknowledgment.</p>
771<p>The callback will be invoked with three arguments: an error argument that will
772be <code>null</code> if the <code>PING</code> was successfully acknowledged, a <code>duration</code> argument
773that reports the number of milliseconds elapsed since the ping was sent and the
774acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
775payload.</p>
776<pre><code class="language-js">session.<span class="hljs-title function_">ping</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdefgh'</span>), <span class="hljs-function">(<span class="hljs-params">err, duration, payload</span>) =></span> {
777  <span class="hljs-keyword">if</span> (!err) {
778    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Ping acknowledged in <span class="hljs-subst">${duration}</span> milliseconds`</span>);
779    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`With payload '<span class="hljs-subst">${payload.toString()}</span>'`</span>);
780  }
781});</code></pre>
782<p>If the <code>payload</code> argument is not specified, the default payload will be the
78364-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
784<h5><code>http2session.ref()</code><span><a class="mark" href="#http2_http2session_ref" id="http2_http2session_ref">#</a></span></h5>
785<div class="api_metadata">
786<span>Added in: v9.4.0</span>
787</div>
788<p>Calls <a href="net.html#net_socket_ref"><code>ref()</code></a> on this <code>Http2Session</code>
789instance's underlying <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p>
790<h5><code>http2session.remoteSettings</code><span><a class="mark" href="#http2_http2session_remotesettings" id="http2_http2session_remotesettings">#</a></span></h5>
791<div class="api_metadata">
792<span>Added in: v8.4.0</span>
793</div>
794<ul>
795<li><a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
796</ul>
797<p>A prototype-less object describing the current remote settings of this
798<code>Http2Session</code>. The remote settings are set by the <em>connected</em> HTTP/2 peer.</p>
799<h5><code>http2session.setLocalWindowSize(windowSize)</code><span><a class="mark" href="#http2_http2session_setlocalwindowsize_windowsize" id="http2_http2session_setlocalwindowsize_windowsize">#</a></span></h5>
800<div class="api_metadata">
801<span>Added in: v14.18.0</span>
802</div>
803<ul>
804<li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
805</ul>
806<p>Sets the local endpoint's window size.
807The <code>windowSize</code> is the total window size to set, not
808the delta.</p>
809<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
810
811<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
812<span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** <span class="hljs-number">20</span>;
813server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
814
815  <span class="hljs-comment">// Set local window size to be 2 ** 20</span>
816  session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
817});</code></pre>
818<h5><code>http2session.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2_http2session_settimeout_msecs_callback" id="http2_http2session_settimeout_msecs_callback">#</a></span></h5>
819<div class="api_metadata">
820<span>Added in: v8.4.0</span>
821</div>
822<ul>
823<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
824<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
825</ul>
826<p>Used to set a callback function that is called when there is no activity on
827the <code>Http2Session</code> after <code>msecs</code> milliseconds. The given <code>callback</code> is
828registered as a listener on the <code>'timeout'</code> event.</p>
829<h5><code>http2session.socket</code><span><a class="mark" href="#http2_http2session_socket" id="http2_http2session_socket">#</a></span></h5>
830<div class="api_metadata">
831<span>Added in: v8.4.0</span>
832</div>
833<ul>
834<li><a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
835</ul>
836<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
837limits available methods to ones safe to use with HTTP/2.</p>
838<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw
839an error with code <code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See
840<a href="#http2_http2session_and_sockets"><code>Http2Session</code> and Sockets</a> for more information.</p>
841<p><code>setTimeout</code> method will be called on this <code>Http2Session</code>.</p>
842<p>All other interactions will be routed directly to the socket.</p>
843<h5><code>http2session.state</code><span><a class="mark" href="#http2_http2session_state" id="http2_http2session_state">#</a></span></h5>
844<div class="api_metadata">
845<span>Added in: v8.4.0</span>
846</div>
847<p>Provides miscellaneous information about the current state of the
848<code>Http2Session</code>.</p>
849<ul>
850<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
851<ul>
852<li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current local (receive)
853flow control window size for the <code>Http2Session</code>.</li>
854<li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current number of bytes
855that have been received since the last flow control <code>WINDOW_UPDATE</code>.</li>
856<li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric identifier to be used the
857next time a new <code>Http2Stream</code> is created by this <code>Http2Session</code>.</li>
858<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes that the remote peer can
859send without receiving a <code>WINDOW_UPDATE</code>.</li>
860<li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric id of the <code>Http2Stream</code>
861for which a <code>HEADERS</code> or <code>DATA</code> frame was most recently received.</li>
862<li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes that this <code>Http2Session</code>
863may send without receiving a <code>WINDOW_UPDATE</code>.</li>
864<li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of frames currently within the
865outbound queue for this <code>Http2Session</code>.</li>
866<li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current size in bytes of the
867outbound header compression state table.</li>
868<li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current size in bytes of the
869inbound header compression state table.</li>
870</ul>
871</li>
872</ul>
873<p>An object describing the current status of this <code>Http2Session</code>.</p>
874<h5><code>http2session.settings([settings][, callback])</code><span><a class="mark" href="#http2_http2session_settings_settings_callback" id="http2_http2session_settings_settings_callback">#</a></span></h5>
875<div class="api_metadata">
876<span>Added in: v8.4.0</span>
877</div>
878<ul>
879<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
880<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback that is called once the session is connected or
881right away if the session is already connected.
882<ul>
883<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
884<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> The updated <code>settings</code> object.</li>
885<li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
886</ul>
887</li>
888</ul>
889<p>Updates the current local settings for this <code>Http2Session</code> and sends a new
890<code>SETTINGS</code> frame to the connected HTTP/2 peer.</p>
891<p>Once called, the <code>http2session.pendingSettingsAck</code> property will be <code>true</code>
892while the session is waiting for the remote peer to acknowledge the new
893settings.</p>
894<p>The new settings will not become effective until the <code>SETTINGS</code> acknowledgment
895is received and the <code>'localSettings'</code> event is emitted. It is possible to send
896multiple <code>SETTINGS</code> frames while acknowledgment is still pending.</p>
897<h5><code>http2session.type</code><span><a class="mark" href="#http2_http2session_type" id="http2_http2session_type">#</a></span></h5>
898<div class="api_metadata">
899<span>Added in: v8.4.0</span>
900</div>
901<ul>
902<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
903</ul>
904<p>The <code>http2session.type</code> will be equal to
905<code>http2.constants.NGHTTP2_SESSION_SERVER</code> if this <code>Http2Session</code> instance is a
906server, and <code>http2.constants.NGHTTP2_SESSION_CLIENT</code> if the instance is a
907client.</p>
908<h5><code>http2session.unref()</code><span><a class="mark" href="#http2_http2session_unref" id="http2_http2session_unref">#</a></span></h5>
909<div class="api_metadata">
910<span>Added in: v9.4.0</span>
911</div>
912<p>Calls <a href="net.html#net_socket_unref"><code>unref()</code></a> on this <code>Http2Session</code>
913instance's underlying <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p>
914<h4>Class: <code>ServerHttp2Session</code><span><a class="mark" href="#http2_class_serverhttp2session" id="http2_class_serverhttp2session">#</a></span></h4>
915<div class="api_metadata">
916<span>Added in: v8.4.0</span>
917</div>
918<ul>
919<li>Extends: <a href="http2.html#http2_class_http2session" class="type">&#x3C;Http2Session></a></li>
920</ul>
921<h5><code>serverhttp2session.altsvc(alt, originOrStream)</code><span><a class="mark" href="#http2_serverhttp2session_altsvc_alt_originorstream" id="http2_serverhttp2session_altsvc_alt_originorstream">#</a></span></h5>
922<div class="api_metadata">
923<span>Added in: v9.4.0</span>
924</div>
925<ul>
926<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A description of the alternative service configuration as
927defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>.</li>
928<li><code>originOrStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Either a URL string specifying
929the origin (or an <code>Object</code> with an <code>origin</code> property) or the numeric
930identifier of an active <code>Http2Stream</code> as given by the <code>http2stream.id</code>
931property.</li>
932</ul>
933<p>Submits an <code>ALTSVC</code> frame (as defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>) to the connected client.</p>
934<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
935
936<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
937server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
938  <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
939  session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>);
940});
941
942server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
943  <span class="hljs-comment">// Set altsvc for a specific stream</span>
944  stream.<span class="hljs-property">session</span>.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, stream.<span class="hljs-property">id</span>);
945});</code></pre>
946<p>Sending an <code>ALTSVC</code> frame with a specific stream ID indicates that the alternate
947service is associated with the origin of the given <code>Http2Stream</code>.</p>
948<p>The <code>alt</code> and origin string <em>must</em> contain only ASCII bytes and are
949strictly interpreted as a sequence of ASCII bytes. The special value <code>'clear'</code>
950may be passed to clear any previously set alternative service for a given
951domain.</p>
952<p>When a string is passed for the <code>originOrStream</code> argument, it will be parsed as
953a URL and the origin will be derived. For instance, the origin for the
954HTTP URL <code>'https://example.org/foo/bar'</code> is the ASCII string
955<code>'https://example.org'</code>. An error will be thrown if either the given string
956cannot be parsed as a URL or if a valid origin cannot be derived.</p>
957<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
958<code>originOrStream</code>, in which case the value of the <code>origin</code> property will be
959used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
960ASCII origin.</p>
961<h5>Specifying alternative services<span><a class="mark" href="#http2_specifying_alternative_services" id="http2_specifying_alternative_services">#</a></span></h5>
962<p>The format of the <code>alt</code> parameter is strictly defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a> as an
963ASCII string containing a comma-delimited list of "alternative" protocols
964associated with a specific host and port.</p>
965<p>For example, the value <code>'h2="example.org:81"'</code> indicates that the HTTP/2
966protocol is available on the host <code>'example.org'</code> on TCP/IP port 81. The
967host and port <em>must</em> be contained within the quote (<code>"</code>) characters.</p>
968<p>Multiple alternatives may be specified, for instance: <code>'h2="example.org:81", h2=":82"'</code>.</p>
969<p>The protocol identifier (<code>'h2'</code> in the examples) may be any valid
970<a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">ALPN Protocol ID</a>.</p>
971<p>The syntax of these values is not validated by the Node.js implementation and
972are passed through as provided by the user or received from the peer.</p>
973<h5><code>serverhttp2session.origin(...origins)</code><span><a class="mark" href="#http2_serverhttp2session_origin_origins" id="http2_serverhttp2session_origin_origins">#</a></span></h5>
974<div class="api_metadata">
975<span>Added in: v10.12.0</span>
976</div>
977<ul>
978<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> One or more URL Strings passed as
979separate arguments.</li>
980</ul>
981<p>Submits an <code>ORIGIN</code> frame (as defined by <a href="https://tools.ietf.org/html/rfc8336">RFC 8336</a>) to the connected client
982to advertise the set of origins for which the server is capable of providing
983authoritative responses.</p>
984<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
985<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
986<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
987server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
988  stream.<span class="hljs-title function_">respond</span>();
989  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
990});
991server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
992  session.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>);
993});</code></pre>
994<p>When a string is passed as an <code>origin</code>, it will be parsed as a URL and the
995origin will be derived. For instance, the origin for the HTTP URL
996<code>'https://example.org/foo/bar'</code> is the ASCII string
997<code>'https://example.org'</code>. An error will be thrown if either the given string
998cannot be parsed as a URL or if a valid origin cannot be derived.</p>
999<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
1000an <code>origin</code>, in which case the value of the <code>origin</code> property will be
1001used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
1002ASCII origin.</p>
1003<p>Alternatively, the <code>origins</code> option may be used when creating a new HTTP/2
1004server using the <code>http2.createSecureServer()</code> method:</p>
1005<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1006<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
1007options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>];
1008<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
1009server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1010  stream.<span class="hljs-title function_">respond</span>();
1011  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
1012});</code></pre>
1013<h4>Class: <code>ClientHttp2Session</code><span><a class="mark" href="#http2_class_clienthttp2session" id="http2_class_clienthttp2session">#</a></span></h4>
1014<div class="api_metadata">
1015<span>Added in: v8.4.0</span>
1016</div>
1017<ul>
1018<li>Extends: <a href="http2.html#http2_class_http2session" class="type">&#x3C;Http2Session></a></li>
1019</ul>
1020<h5>Event: <code>'altsvc'</code><span><a class="mark" href="#http2_event_altsvc" id="http2_event_altsvc">#</a></span></h5>
1021<div class="api_metadata">
1022<span>Added in: v9.4.0</span>
1023</div>
1024<ul>
1025<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1026<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1027<li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1028</ul>
1029<p>The <code>'altsvc'</code> event is emitted whenever an <code>ALTSVC</code> frame is received by
1030the client. The event is emitted with the <code>ALTSVC</code> value, origin, and stream
1031ID. If no <code>origin</code> is provided in the <code>ALTSVC</code> frame, <code>origin</code> will
1032be an empty string.</p>
1033<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1034<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
1035
1036client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'altsvc'</span>, <span class="hljs-function">(<span class="hljs-params">alt, origin, streamId</span>) =></span> {
1037  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(alt);
1038  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origin);
1039  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(streamId);
1040});</code></pre>
1041<h5>Event: <code>'origin'</code><span><a class="mark" href="#http2_event_origin" id="http2_event_origin">#</a></span></h5>
1042<div class="api_metadata">
1043<span>Added in: v10.12.0</span>
1044</div>
1045<ul>
1046<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1047</ul>
1048<p>The <code>'origin'</code> event is emitted whenever an <code>ORIGIN</code> frame is received by
1049the client. The event is emitted with an array of <code>origin</code> strings. The
1050<code>http2session.originSet</code> will be updated to include the received
1051origins.</p>
1052<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1053<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
1054
1055client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span class="hljs-function">(<span class="hljs-params">origins</span>) =></span> {
1056  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n &#x3C; origins.<span class="hljs-property">length</span>; n++)
1057    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origins[n]);
1058});</code></pre>
1059<p>The <code>'origin'</code> event is only emitted when using a secure TLS connection.</p>
1060<h5><code>clienthttp2session.request(headers[, options])</code><span><a class="mark" href="#http2_clienthttp2session_request_headers_options" id="http2_clienthttp2session_request_headers_options">#</a></span></h5>
1061<div class="api_metadata">
1062<span>Added in: v8.4.0</span>
1063</div>
1064<ul>
1065<li>
1066<p><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></p>
1067</li>
1068<li>
1069<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1070<ul>
1071<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the <code>Http2Stream</code> <em>writable</em> side should
1072be closed initially, such as when sending a <code>GET</code> request that should not
1073expect a payload body.</li>
1074<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
1075the created stream is made the sole direct dependency of the parent, with
1076all other existing dependents made a dependent of the newly created stream.
1077<strong>Default:</strong> <code>false</code>.</li>
1078<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream the newly
1079created stream is dependent on.</li>
1080<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the relative dependency of a stream in relation
1081to other streams with the same <code>parent</code>. The value is a number between <code>1</code>
1082and <code>256</code> (inclusive).</li>
1083<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
1084<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
1085<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> An AbortSignal that may be used to abort an ongoing
1086request.</li>
1087</ul>
1088</li>
1089<li>
1090<p>Returns: <a href="http2.html#http2_class_clienthttp2stream" class="type">&#x3C;ClientHttp2Stream></a></p>
1091</li>
1092</ul>
1093<p>For HTTP/2 Client <code>Http2Session</code> instances only, the <code>http2session.request()</code>
1094creates and returns an <code>Http2Stream</code> instance that can be used to send an
1095HTTP/2 request to the connected server.</p>
1096<p>This method is only available if <code>http2session.type</code> is equal to
1097<code>http2.constants.NGHTTP2_SESSION_CLIENT</code>.</p>
1098<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1099<span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
1100<span class="hljs-keyword">const</span> {
1101  HTTP2_HEADER_PATH,
1102  HTTP2_HEADER_STATUS
1103} = http2.<span class="hljs-property">constants</span>;
1104
1105<span class="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</span>({ [HTTP2_HEADER_PATH]: <span class="hljs-string">'/'</span> });
1106req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
1107  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[HTTP2_HEADER_STATUS]);
1108  req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { <span class="hljs-comment">/* .. */</span> });
1109  req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });
1110});</code></pre>
1111<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
1112is emitted immediately after queuing the last chunk of payload data to be sent.
1113The <code>http2stream.sendTrailers()</code> method can then be called to send trailing
1114headers to the peer.</p>
1115<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
1116close when the final <code>DATA</code> frame is transmitted. User code must call either
1117<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
1118<code>Http2Stream</code>.</p>
1119<p>When <code>options.signal</code> is set with an <code>AbortSignal</code> and then <code>abort</code> on the
1120corresponding <code>AbortController</code> is called, the request will emit an <code>'error'</code>
1121event with an <code>AbortError</code> error.</p>
1122<p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>headers</code>,
1123they respectively default to:</p>
1124<ul>
1125<li><code>:method</code> = <code>'GET'</code></li>
1126<li><code>:path</code> = <code>/</code></li>
1127</ul>
1128<h4>Class: <code>Http2Stream</code><span><a class="mark" href="#http2_class_http2stream" id="http2_class_http2stream">#</a></span></h4>
1129<div class="api_metadata">
1130<span>Added in: v8.4.0</span>
1131</div>
1132<ul>
1133<li>Extends: <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1134</ul>
1135<p>Each instance of the <code>Http2Stream</code> class represents a bidirectional HTTP/2
1136communications stream over an <code>Http2Session</code> instance. Any single <code>Http2Session</code>
1137may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
1138<p>User code will not construct <code>Http2Stream</code> instances directly. Rather, these
1139are created, managed, and provided to user code through the <code>Http2Session</code>
1140instance. On the server, <code>Http2Stream</code> instances are created either in response
1141to an incoming HTTP request (and handed off to user code via the <code>'stream'</code>
1142event), or in response to a call to the <code>http2stream.pushStream()</code> method.
1143On the client, <code>Http2Stream</code> instances are created and returned when either the
1144<code>http2session.request()</code> method is called, or in response to an incoming
1145<code>'push'</code> event.</p>
1146<p>The <code>Http2Stream</code> class is a base for the <a href="#http2_class_serverhttp2stream"><code>ServerHttp2Stream</code></a> and
1147<a href="#http2_class_clienthttp2stream"><code>ClientHttp2Stream</code></a> classes, each of which is used specifically by either
1148the Server or Client side, respectively.</p>
1149<p>All <code>Http2Stream</code> instances are <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> streams. The <code>Writable</code> side of the
1150<code>Duplex</code> is used to send data to the connected peer, while the <code>Readable</code> side
1151is used to receive data sent by the connected peer.</p>
1152<p>The default text character encoding for all <code>Http2Stream</code>s is UTF-8. As a best
1153practice, it is recommended that when using an <code>Http2Stream</code> to send text,
1154the <code>'content-type'</code> header should be set and should identify the character
1155encoding used.</p>
1156<pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
1157  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
1158  <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
1159});</code></pre>
1160<h5><code>Http2Stream</code> Lifecycle<span><a class="mark" href="#http2_http2stream_lifecycle" id="http2_http2stream_lifecycle">#</a></span></h5>
1161<h6>Creation<span><a class="mark" href="#http2_creation" id="http2_creation">#</a></span></h6>
1162<p>On the server side, instances of <a href="#http2_class_serverhttp2stream"><code>ServerHttp2Stream</code></a> are created either
1163when:</p>
1164<ul>
1165<li>A new HTTP/2 <code>HEADERS</code> frame with a previously unused stream ID is received;</li>
1166<li>The <code>http2stream.pushStream()</code> method is called.</li>
1167</ul>
1168<p>On the client side, instances of <a href="#http2_class_clienthttp2stream"><code>ClientHttp2Stream</code></a> are created when the
1169<code>http2session.request()</code> method is called.</p>
1170<p>On the client, the <code>Http2Stream</code> instance returned by <code>http2session.request()</code>
1171may not be immediately ready for use if the parent <code>Http2Session</code> has not yet
1172been fully established. In such cases, operations called on the <code>Http2Stream</code>
1173will be buffered until the <code>'ready'</code> event is emitted. User code should rarely,
1174if ever, need to handle the <code>'ready'</code> event directly. The ready status of an
1175<code>Http2Stream</code> can be determined by checking the value of <code>http2stream.id</code>. If
1176the value is <code>undefined</code>, the stream is not yet ready for use.</p>
1177<h6>Destruction<span><a class="mark" href="#http2_destruction" id="http2_destruction">#</a></span></h6>
1178<p>All <a href="#http2_class_http2stream"><code>Http2Stream</code></a> instances are destroyed either when:</p>
1179<ul>
1180<li>An <code>RST_STREAM</code> frame for the stream is received by the connected peer,
1181and (for client streams only) pending data has been read.</li>
1182<li>The <code>http2stream.close()</code> method is called, and (for client streams only)
1183pending data has been read.</li>
1184<li>The <code>http2stream.destroy()</code> or <code>http2session.destroy()</code> methods are called.</li>
1185</ul>
1186<p>When an <code>Http2Stream</code> instance is destroyed, an attempt will be made to send an
1187<code>RST_STREAM</code> frame to the connected peer.</p>
1188<p>When the <code>Http2Stream</code> instance is destroyed, the <code>'close'</code> event will
1189be emitted. Because <code>Http2Stream</code> is an instance of <code>stream.Duplex</code>, the
1190<code>'end'</code> event will also be emitted if the stream data is currently flowing.
1191The <code>'error'</code> event may also be emitted if <code>http2stream.destroy()</code> was called
1192with an <code>Error</code> passed as the first argument.</p>
1193<p>After the <code>Http2Stream</code> has been destroyed, the <code>http2stream.destroyed</code>
1194property will be <code>true</code> and the <code>http2stream.rstCode</code> property will specify the
1195<code>RST_STREAM</code> error code. The <code>Http2Stream</code> instance is no longer usable once
1196destroyed.</p>
1197<h5>Event: <code>'aborted'</code><span><a class="mark" href="#http2_event_aborted" id="http2_event_aborted">#</a></span></h5>
1198<div class="api_metadata">
1199<span>Added in: v8.4.0</span>
1200</div>
1201<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2Stream</code> instance is
1202abnormally aborted in mid-communication.
1203Its listener does not expect any arguments.</p>
1204<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2Stream</code> writable side
1205has not been ended.</p>
1206<h5>Event: <code>'close'</code><span><a class="mark" href="#http2_event_close_1" id="http2_event_close_1">#</a></span></h5>
1207<div class="api_metadata">
1208<span>Added in: v8.4.0</span>
1209</div>
1210<p>The <code>'close'</code> event is emitted when the <code>Http2Stream</code> is destroyed. Once
1211this event is emitted, the <code>Http2Stream</code> instance is no longer usable.</p>
1212<p>The HTTP/2 error code used when closing the stream can be retrieved using
1213the <code>http2stream.rstCode</code> property. If the code is any value other than
1214<code>NGHTTP2_NO_ERROR</code> (<code>0</code>), an <code>'error'</code> event will have also been emitted.</p>
1215<h5>Event: <code>'error'</code><span><a class="mark" href="#http2_event_error_1" id="http2_event_error_1">#</a></span></h5>
1216<div class="api_metadata">
1217<span>Added in: v8.4.0</span>
1218</div>
1219<ul>
1220<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
1221</ul>
1222<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
1223an <code>Http2Stream</code>.</p>
1224<h5>Event: <code>'frameError'</code><span><a class="mark" href="#http2_event_frameerror_1" id="http2_event_frameerror_1">#</a></span></h5>
1225<div class="api_metadata">
1226<span>Added in: v8.4.0</span>
1227</div>
1228<ul>
1229<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The frame type.</li>
1230<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The error code.</li>
1231<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The stream id (or <code>0</code> if the frame isn't associated with a
1232stream).</li>
1233</ul>
1234<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
1235send a frame. When invoked, the handler function will receive an integer
1236argument identifying the frame type, and an integer argument identifying the
1237error code. The <code>Http2Stream</code> instance will be destroyed immediately after the
1238<code>'frameError'</code> event is emitted.</p>
1239<h5>Event: <code>'ready'</code><span><a class="mark" href="#http2_event_ready" id="http2_event_ready">#</a></span></h5>
1240<div class="api_metadata">
1241<span>Added in: v8.4.0</span>
1242</div>
1243<p>The <code>'ready'</code> event is emitted when the <code>Http2Stream</code> has been opened, has
1244been assigned an <code>id</code>, and can be used. The listener does not expect any
1245arguments.</p>
1246<h5>Event: <code>'timeout'</code><span><a class="mark" href="#http2_event_timeout_1" id="http2_event_timeout_1">#</a></span></h5>
1247<div class="api_metadata">
1248<span>Added in: v8.4.0</span>
1249</div>
1250<p>The <code>'timeout'</code> event is emitted after no activity is received for this
1251<code>Http2Stream</code> within the number of milliseconds set using
1252<code>http2stream.setTimeout()</code>.
1253Its listener does not expect any arguments.</p>
1254<h5>Event: <code>'trailers'</code><span><a class="mark" href="#http2_event_trailers" id="http2_event_trailers">#</a></span></h5>
1255<div class="api_metadata">
1256<span>Added in: v8.4.0</span>
1257</div>
1258<ul>
1259<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
1260<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
1261</ul>
1262<p>The <code>'trailers'</code> event is emitted when a block of headers associated with
1263trailing header fields is received. The listener callback is passed the
1264<a href="#http2_headers_object">HTTP/2 Headers Object</a> and flags associated with the headers.</p>
1265<p>This event might not be emitted if <code>http2stream.end()</code> is called
1266before trailers are received and the incoming data is not being read or
1267listened for.</p>
1268<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'trailers'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> {
1269  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
1270});</code></pre>
1271<h5>Event: <code>'wantTrailers'</code><span><a class="mark" href="#http2_event_wanttrailers" id="http2_event_wanttrailers">#</a></span></h5>
1272<div class="api_metadata">
1273<span>Added in: v10.0.0</span>
1274</div>
1275<p>The <code>'wantTrailers'</code> event is emitted when the <code>Http2Stream</code> has queued the
1276final <code>DATA</code> frame to be sent on a frame and the <code>Http2Stream</code> is ready to send
1277trailing headers. When initiating a request or response, the <code>waitForTrailers</code>
1278option must be set for this event to be emitted.</p>
1279<h5><code>http2stream.aborted</code><span><a class="mark" href="#http2_http2stream_aborted" id="http2_http2stream_aborted">#</a></span></h5>
1280<div class="api_metadata">
1281<span>Added in: v8.4.0</span>
1282</div>
1283<ul>
1284<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1285</ul>
1286<p>Set to <code>true</code> if the <code>Http2Stream</code> instance was aborted abnormally. When set,
1287the <code>'aborted'</code> event will have been emitted.</p>
1288<h5><code>http2stream.bufferSize</code><span><a class="mark" href="#http2_http2stream_buffersize" id="http2_http2stream_buffersize">#</a></span></h5>
1289<div class="api_metadata">
1290<span>Added in: v11.2.0, v10.16.0</span>
1291</div>
1292<ul>
1293<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1294</ul>
1295<p>This property shows the number of characters currently buffered to be written.
1296See <a href="net.html#net_socket_buffersize"><code>net.Socket.bufferSize</code></a> for details.</p>
1297<h5><code>http2stream.close(code[, callback])</code><span><a class="mark" href="#http2_http2stream_close_code_callback" id="http2_http2stream_close_code_callback">#</a></span></h5>
1298<div class="api_metadata">
1299<span>Added in: v8.4.0</span>
1300</div>
1301<ul>
1302<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Unsigned 32-bit integer identifying the error code.
1303<strong>Default:</strong> <code>http2.constants.NGHTTP2_NO_ERROR</code> (<code>0x00</code>).</li>
1304<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional function registered to listen for the
1305<code>'close'</code> event.</li>
1306</ul>
1307<p>Closes the <code>Http2Stream</code> instance by sending an <code>RST_STREAM</code> frame to the
1308connected HTTP/2 peer.</p>
1309<h5><code>http2stream.closed</code><span><a class="mark" href="#http2_http2stream_closed" id="http2_http2stream_closed">#</a></span></h5>
1310<div class="api_metadata">
1311<span>Added in: v9.4.0</span>
1312</div>
1313<ul>
1314<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1315</ul>
1316<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been closed.</p>
1317<h5><code>http2stream.destroyed</code><span><a class="mark" href="#http2_http2stream_destroyed" id="http2_http2stream_destroyed">#</a></span></h5>
1318<div class="api_metadata">
1319<span>Added in: v8.4.0</span>
1320</div>
1321<ul>
1322<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1323</ul>
1324<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been destroyed and is no longer
1325usable.</p>
1326<h5><code>http2stream.endAfterHeaders</code><span><a class="mark" href="#http2_http2stream_endafterheaders" id="http2_http2stream_endafterheaders">#</a></span></h5>
1327<div class="api_metadata">
1328<span>Added in: v10.11.0</span>
1329</div>
1330<ul>
1331<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1332</ul>
1333<p>Set the <code>true</code> if the <code>END_STREAM</code> flag was set in the request or response
1334HEADERS frame received, indicating that no additional data should be received
1335and the readable side of the <code>Http2Stream</code> will be closed.</p>
1336<h5><code>http2stream.id</code><span><a class="mark" href="#http2_http2stream_id" id="http2_http2stream_id">#</a></span></h5>
1337<div class="api_metadata">
1338<span>Added in: v8.4.0</span>
1339</div>
1340<ul>
1341<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
1342</ul>
1343<p>The numeric stream identifier of this <code>Http2Stream</code> instance. Set to <code>undefined</code>
1344if the stream identifier has not yet been assigned.</p>
1345<h5><code>http2stream.pending</code><span><a class="mark" href="#http2_http2stream_pending" id="http2_http2stream_pending">#</a></span></h5>
1346<div class="api_metadata">
1347<span>Added in: v9.4.0</span>
1348</div>
1349<ul>
1350<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1351</ul>
1352<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has not yet been assigned a
1353numeric stream identifier.</p>
1354<h5><code>http2stream.priority(options)</code><span><a class="mark" href="#http2_http2stream_priority_options" id="http2_http2stream_priority_options">#</a></span></h5>
1355<div class="api_metadata">
1356<span>Added in: v8.4.0</span>
1357</div>
1358<ul>
1359<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1360<ul>
1361<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
1362this stream is made the sole direct dependency of the parent, with
1363all other existing dependents made a dependent of this stream. <strong>Default:</strong>
1364<code>false</code>.</li>
1365<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream this stream
1366is dependent on.</li>
1367<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the relative dependency of a stream in relation
1368to other streams with the same <code>parent</code>. The value is a number between <code>1</code>
1369and <code>256</code> (inclusive).</li>
1370<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, changes the priority locally without
1371sending a <code>PRIORITY</code> frame to the connected peer.</li>
1372</ul>
1373</li>
1374</ul>
1375<p>Updates the priority for this <code>Http2Stream</code> instance.</p>
1376<h5><code>http2stream.rstCode</code><span><a class="mark" href="#http2_http2stream_rstcode" id="http2_http2stream_rstcode">#</a></span></h5>
1377<div class="api_metadata">
1378<span>Added in: v8.4.0</span>
1379</div>
1380<ul>
1381<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1382</ul>
1383<p>Set to the <code>RST_STREAM</code> <a href="#http2_error_codes_for_rst_stream_and_goaway">error code</a> reported when the <code>Http2Stream</code> is
1384destroyed after either receiving an <code>RST_STREAM</code> frame from the connected peer,
1385calling <code>http2stream.close()</code>, or <code>http2stream.destroy()</code>. Will be
1386<code>undefined</code> if the <code>Http2Stream</code> has not been closed.</p>
1387<h5><code>http2stream.sentHeaders</code><span><a class="mark" href="#http2_http2stream_sentheaders" id="http2_http2stream_sentheaders">#</a></span></h5>
1388<div class="api_metadata">
1389<span>Added in: v9.5.0</span>
1390</div>
1391<ul>
1392<li><a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1393</ul>
1394<p>An object containing the outbound headers sent for this <code>Http2Stream</code>.</p>
1395<h5><code>http2stream.sentInfoHeaders</code><span><a class="mark" href="#http2_http2stream_sentinfoheaders" id="http2_http2stream_sentinfoheaders">#</a></span></h5>
1396<div class="api_metadata">
1397<span>Added in: v9.5.0</span>
1398</div>
1399<ul>
1400<li><a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object[]></a></li>
1401</ul>
1402<p>An array of objects containing the outbound informational (additional) headers
1403sent for this <code>Http2Stream</code>.</p>
1404<h5><code>http2stream.sentTrailers</code><span><a class="mark" href="#http2_http2stream_senttrailers" id="http2_http2stream_senttrailers">#</a></span></h5>
1405<div class="api_metadata">
1406<span>Added in: v9.5.0</span>
1407</div>
1408<ul>
1409<li><a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1410</ul>
1411<p>An object containing the outbound trailers sent for this <code>HttpStream</code>.</p>
1412<h5><code>http2stream.session</code><span><a class="mark" href="#http2_http2stream_session" id="http2_http2stream_session">#</a></span></h5>
1413<div class="api_metadata">
1414<span>Added in: v8.4.0</span>
1415</div>
1416<ul>
1417<li><a href="http2.html#http2_class_http2session" class="type">&#x3C;Http2Session></a></li>
1418</ul>
1419<p>A reference to the <code>Http2Session</code> instance that owns this <code>Http2Stream</code>. The
1420value will be <code>undefined</code> after the <code>Http2Stream</code> instance is destroyed.</p>
1421<h5><code>http2stream.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2_http2stream_settimeout_msecs_callback" id="http2_http2stream_settimeout_msecs_callback">#</a></span></h5>
1422<div class="api_metadata">
1423<span>Added in: v8.4.0</span>
1424</div>
1425<ul>
1426<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1427<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1428</ul>
1429<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1430<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://example.org:8000'</span>);
1431<span class="hljs-keyword">const</span> { NGHTTP2_CANCEL } = http2.<span class="hljs-property">constants</span>;
1432<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });
1433
1434<span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
1435req.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">5000</span>, <span class="hljs-function">() =></span> req.<span class="hljs-title function_">close</span>(NGHTTP2_CANCEL));</code></pre>
1436<h5><code>http2stream.state</code><span><a class="mark" href="#http2_http2stream_state" id="http2_http2stream_state">#</a></span></h5>
1437<div class="api_metadata">
1438<span>Added in: v8.4.0</span>
1439</div>
1440<p>Provides miscellaneous information about the current state of the
1441<code>Http2Stream</code>.</p>
1442<ul>
1443<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1444<ul>
1445<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes the connected peer may send
1446for this <code>Http2Stream</code> without receiving a <code>WINDOW_UPDATE</code>.</li>
1447<li><code>state</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A flag indicating the low-level current state of the
1448<code>Http2Stream</code> as determined by <code>nghttp2</code>.</li>
1449<li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <code>1</code> if this <code>Http2Stream</code> has been closed locally.</li>
1450<li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <code>1</code> if this <code>Http2Stream</code> has been closed
1451remotely.</li>
1452<li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The sum weight of all <code>Http2Stream</code>
1453instances that depend on this <code>Http2Stream</code> as specified using
1454<code>PRIORITY</code> frames.</li>
1455<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The priority weight of this <code>Http2Stream</code>.</li>
1456</ul>
1457</li>
1458</ul>
1459<p>A current state of this <code>Http2Stream</code>.</p>
1460<h5><code>http2stream.sendTrailers(headers)</code><span><a class="mark" href="#http2_http2stream_sendtrailers_headers" id="http2_http2stream_sendtrailers_headers">#</a></span></h5>
1461<div class="api_metadata">
1462<span>Added in: v10.0.0</span>
1463</div>
1464<ul>
1465<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1466</ul>
1467<p>Sends a trailing <code>HEADERS</code> frame to the connected HTTP/2 peer. This method
1468will cause the <code>Http2Stream</code> to be immediately closed and must only be
1469called after the <code>'wantTrailers'</code> event has been emitted. When sending a
1470request or sending a response, the <code>options.waitForTrailers</code> option must be set
1471in order to keep the <code>Http2Stream</code> open after the final <code>DATA</code> frame so that
1472trailers can be sent.</p>
1473<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1474<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1475server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1476  stream.<span class="hljs-title function_">respond</span>(<span class="hljs-literal">undefined</span>, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
1477  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
1478    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>: <span class="hljs-string">'abc'</span> });
1479  });
1480  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span>);
1481});</code></pre>
1482<p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
1483fields (e.g. <code>':method'</code>, <code>':path'</code>, etc).</p>
1484<h4>Class: <code>ClientHttp2Stream</code><span><a class="mark" href="#http2_class_clienthttp2stream" id="http2_class_clienthttp2stream">#</a></span></h4>
1485<div class="api_metadata">
1486<span>Added in: v8.4.0</span>
1487</div>
1488<ul>
1489<li>Extends <a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a></li>
1490</ul>
1491<p>The <code>ClientHttp2Stream</code> class is an extension of <code>Http2Stream</code> that is
1492used exclusively on HTTP/2 Clients. <code>Http2Stream</code> instances on the client
1493provide events such as <code>'response'</code> and <code>'push'</code> that are only relevant on
1494the client.</p>
1495<h5>Event: <code>'continue'</code><span><a class="mark" href="#http2_event_continue" id="http2_event_continue">#</a></span></h5>
1496<div class="api_metadata">
1497<span>Added in: v8.5.0</span>
1498</div>
1499<p>Emitted when the server sends a <code>100 Continue</code> status, usually because
1500the request contained <code>Expect: 100-continue</code>. This is an instruction that
1501the client should send the request body.</p>
1502<h5>Event: <code>'headers'</code><span><a class="mark" href="#http2_event_headers" id="http2_event_headers">#</a></span></h5>
1503<div class="api_metadata">
1504<span>Added in: v8.4.0</span>
1505</div>
1506<p>The <code>'headers'</code> event is emitted when an additional block of headers is received
1507for a stream, such as when a block of <code>1xx</code> informational headers is received.
1508The listener callback is passed the <a href="#http2_headers_object">HTTP/2 Headers Object</a> and flags
1509associated with the headers.</p>
1510<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'headers'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> {
1511  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
1512});</code></pre>
1513<h5>Event: <code>'push'</code><span><a class="mark" href="#http2_event_push" id="http2_event_push">#</a></span></h5>
1514<div class="api_metadata">
1515<span>Added in: v8.4.0</span>
1516</div>
1517<p>The <code>'push'</code> event is emitted when response headers for a Server Push stream
1518are received. The listener callback is passed the <a href="#http2_headers_object">HTTP/2 Headers Object</a> and
1519flags associated with the headers.</p>
1520<pre><code class="language-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> {
1521  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
1522});</code></pre>
1523<h5>Event: <code>'response'</code><span><a class="mark" href="#http2_event_response" id="http2_event_response">#</a></span></h5>
1524<div class="api_metadata">
1525<span>Added in: v8.4.0</span>
1526</div>
1527<p>The <code>'response'</code> event is emitted when a response <code>HEADERS</code> frame has been
1528received for this stream from the connected HTTP/2 server. The listener is
1529invoked with two arguments: an <code>Object</code> containing the received
1530<a href="#http2_headers_object">HTTP/2 Headers Object</a>, and flags associated with the headers.</p>
1531<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1532<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost'</span>);
1533<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });
1534req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers, flags</span>) =></span> {
1535  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':status'</span>]);
1536});</code></pre>
1537<h4>Class: <code>ServerHttp2Stream</code><span><a class="mark" href="#http2_class_serverhttp2stream" id="http2_class_serverhttp2stream">#</a></span></h4>
1538<div class="api_metadata">
1539<span>Added in: v8.4.0</span>
1540</div>
1541<ul>
1542<li>Extends: <a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a></li>
1543</ul>
1544<p>The <code>ServerHttp2Stream</code> class is an extension of <a href="#http2_class_http2stream"><code>Http2Stream</code></a> that is
1545used exclusively on HTTP/2 Servers. <code>Http2Stream</code> instances on the server
1546provide additional methods such as <code>http2stream.pushStream()</code> and
1547<code>http2stream.respond()</code> that are only relevant on the server.</p>
1548<h5><code>http2stream.additionalHeaders(headers)</code><span><a class="mark" href="#http2_http2stream_additionalheaders_headers" id="http2_http2stream_additionalheaders_headers">#</a></span></h5>
1549<div class="api_metadata">
1550<span>Added in: v8.4.0</span>
1551</div>
1552<ul>
1553<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1554</ul>
1555<p>Sends an additional informational <code>HEADERS</code> frame to the connected HTTP/2 peer.</p>
1556<h5><code>http2stream.headersSent</code><span><a class="mark" href="#http2_http2stream_headerssent" id="http2_http2stream_headerssent">#</a></span></h5>
1557<div class="api_metadata">
1558<span>Added in: v8.4.0</span>
1559</div>
1560<ul>
1561<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1562</ul>
1563<p>True if headers were sent, false otherwise (read-only).</p>
1564<h5><code>http2stream.pushAllowed</code><span><a class="mark" href="#http2_http2stream_pushallowed" id="http2_http2stream_pushallowed">#</a></span></h5>
1565<div class="api_metadata">
1566<span>Added in: v8.4.0</span>
1567</div>
1568<ul>
1569<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1570</ul>
1571<p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
1572client's most recent <code>SETTINGS</code> frame. Will be <code>true</code> if the remote peer
1573accepts push streams, <code>false</code> otherwise. Settings are the same for every
1574<code>Http2Stream</code> in the same <code>Http2Session</code>.</p>
1575<h5><code>http2stream.pushStream(headers[, options], callback)</code><span><a class="mark" href="#http2_http2stream_pushstream_headers_options_callback" id="http2_http2stream_pushstream_headers_options_callback">#</a></span></h5>
1576<div class="api_metadata">
1577<span>Added in: v8.4.0</span>
1578</div>
1579<ul>
1580<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1581<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1582<ul>
1583<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
1584the created stream is made the sole direct dependency of the parent, with
1585all other existing dependents made a dependent of the newly created stream.
1586<strong>Default:</strong> <code>false</code>.</li>
1587<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream the newly
1588created stream is dependent on.</li>
1589</ul>
1590</li>
1591<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback that is called once the push stream has been
1592initiated.
1593<ul>
1594<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
1595<li><code>pushStream</code> <a href="http2.html#http2_class_serverhttp2stream" class="type">&#x3C;ServerHttp2Stream></a> The returned <code>pushStream</code> object.</li>
1596<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> Headers object the <code>pushStream</code> was
1597initiated with.</li>
1598</ul>
1599</li>
1600</ul>
1601<p>Initiates a push stream. The callback is invoked with the new <code>Http2Stream</code>
1602instance created for the push stream passed as the second argument, or an
1603<code>Error</code> passed as the first argument.</p>
1604<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1605<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1606server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1607  stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
1608  stream.<span class="hljs-title function_">pushStream</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> }, <span class="hljs-function">(<span class="hljs-params">err, pushStream, headers</span>) =></span> {
1609    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
1610    pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
1611    pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed data'</span>);
1612  });
1613  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
1614});</code></pre>
1615<p>Setting the weight of a push stream is not allowed in the <code>HEADERS</code> frame. Pass
1616a <code>weight</code> value to <code>http2stream.priority</code> with the <code>silent</code> option set to
1617<code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
1618<p>Calling <code>http2stream.pushStream()</code> from within a pushed stream is not permitted
1619and will throw an error.</p>
1620<h5><code>http2stream.respond([headers[, options]])</code><span><a class="mark" href="#http2_http2stream_respond_headers_options" id="http2_http2stream_respond_headers_options">#</a></span></h5>
1621<div class="api_metadata">
1622<details class="changelog"><summary>History</summary>
1623<table>
1624<tbody><tr><th>Version</th><th>Changes</th></tr>
1625<tr><td>v14.5.0</td>
1626<td><p>Allow explicitly setting date headers.</p></td></tr>
1627<tr><td>v8.4.0</td>
1628<td><p><span>Added in: v8.4.0</span></p></td></tr>
1629</tbody></table>
1630</details>
1631</div>
1632<ul>
1633<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1634<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1635<ul>
1636<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>true</code> to indicate that the response will not
1637include payload data.</li>
1638<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
1639<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
1640</ul>
1641</li>
1642</ul>
1643<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1644<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1645server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1646  stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
1647  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
1648});</code></pre>
1649<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
1650will be emitted immediately after queuing the last chunk of payload data to be
1651sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
1652header fields to the peer.</p>
1653<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
1654close when the final <code>DATA</code> frame is transmitted. User code must call either
1655<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
1656<code>Http2Stream</code>.</p>
1657<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1658<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1659server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1660  stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> }, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
1661  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
1662    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
1663  });
1664  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
1665});</code></pre>
1666<h5><code>http2stream.respondWithFD(fd[, headers[, options]])</code><span><a class="mark" href="#http2_http2stream_respondwithfd_fd_headers_options" id="http2_http2stream_respondwithfd_fd_headers_options">#</a></span></h5>
1667<div class="api_metadata">
1668<details class="changelog"><summary>History</summary>
1669<table>
1670<tbody><tr><th>Version</th><th>Changes</th></tr>
1671<tr><td>v14.5.0</td>
1672<td><p>Allow explicitly setting date headers.</p></td></tr>
1673<tr><td>v12.12.0</td>
1674<td><p>The <code>fd</code> option may now be a <code>FileHandle</code>.</p></td></tr>
1675<tr><td>v10.0.0</td>
1676<td><p>Any readable file descriptor, not necessarily for a regular file, is supported now.</p></td></tr>
1677<tr><td>v8.4.0</td>
1678<td><p><span>Added in: v8.4.0</span></p></td></tr>
1679</tbody></table>
1680</details>
1681</div>
1682<ul>
1683<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="fs.html#fs_class_filehandle" class="type">&#x3C;FileHandle></a> A readable file descriptor.</li>
1684<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1685<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1686<ul>
1687<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1688<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
1689<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
1690<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The offset position at which to begin reading.</li>
1691<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The amount of data from the fd to send.</li>
1692</ul>
1693</li>
1694</ul>
1695<p>Initiates a response whose data is read from the given file descriptor. No
1696validation is performed on the given file descriptor. If an error occurs while
1697attempting to read data using the file descriptor, the <code>Http2Stream</code> will be
1698closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code> code.</p>
1699<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
1700automatically.</p>
1701<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1702<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1703
1704<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1705server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1706  <span class="hljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>);
1707
1708  <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
1709  <span class="hljs-keyword">const</span> headers = {
1710    <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
1711    <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
1712    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>
1713  };
1714  stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
1715  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> fs.<span class="hljs-title function_">closeSync</span>(fd));
1716});</code></pre>
1717<p>The optional <code>options.statCheck</code> function may be specified to give user code
1718an opportunity to set additional content headers based on the <code>fs.Stat</code> details
1719of the given fd. If the <code>statCheck</code> function is provided, the
1720<code>http2stream.respondWithFD()</code> method will perform an <code>fs.fstat()</code> call to
1721collect details on the provided file descriptor.</p>
1722<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
1723specific range subset. This can be used, for instance, to support HTTP Range
1724requests.</p>
1725<p>The file descriptor or <code>FileHandle</code> is not closed when the stream is closed,
1726so it will need to be closed manually once it is no longer needed.
1727Using the same file descriptor concurrently for multiple streams
1728is not supported and may result in data loss. Re-using a file descriptor
1729after a stream has finished is supported.</p>
1730<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
1731will be emitted immediately after queuing the last chunk of payload data to be
1732sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
1733header fields to the peer.</p>
1734<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
1735close when the final <code>DATA</code> frame is transmitted. User code <em>must</em> call either
1736<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
1737<code>Http2Stream</code>.</p>
1738<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1739<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
1740
1741<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1742server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1743  <span class="hljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'/some/file'</span>, <span class="hljs-string">'r'</span>);
1744
1745  <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
1746  <span class="hljs-keyword">const</span> headers = {
1747    <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
1748    <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
1749    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>
1750  };
1751  stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
1752  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
1753    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
1754  });
1755
1756  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> fs.<span class="hljs-title function_">closeSync</span>(fd));
1757});</code></pre>
1758<h5><code>http2stream.respondWithFile(path[, headers[, options]])</code><span><a class="mark" href="#http2_http2stream_respondwithfile_path_headers_options" id="http2_http2stream_respondwithfile_path_headers_options">#</a></span></h5>
1759<div class="api_metadata">
1760<details class="changelog"><summary>History</summary>
1761<table>
1762<tbody><tr><th>Version</th><th>Changes</th></tr>
1763<tr><td>v14.5.0</td>
1764<td><p>Allow explicitly setting date headers.</p></td></tr>
1765<tr><td>v10.0.0</td>
1766<td><p>Any readable file, not necessarily a regular file, is supported now.</p></td></tr>
1767<tr><td>v8.4.0</td>
1768<td><p><span>Added in: v8.4.0</span></p></td></tr>
1769</tbody></table>
1770</details>
1771</div>
1772<ul>
1773<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a></li>
1774<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
1775<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1776<ul>
1777<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1778<li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback function invoked in the case of an
1779error before send.</li>
1780<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
1781<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
1782<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The offset position at which to begin reading.</li>
1783<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The amount of data from the fd to send.</li>
1784</ul>
1785</li>
1786</ul>
1787<p>Sends a regular file as the response. The <code>path</code> must specify a regular file
1788or an <code>'error'</code> event will be emitted on the <code>Http2Stream</code> object.</p>
1789<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
1790automatically.</p>
1791<p>The optional <code>options.statCheck</code> function may be specified to give user code
1792an opportunity to set additional content headers based on the <code>fs.Stat</code> details
1793of the given file:</p>
1794<p>If an error occurs while attempting to read the file data, the <code>Http2Stream</code>
1795will be closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code>
1796code. If the <code>onError</code> callback is defined, then it will be called. Otherwise
1797the stream will be destroyed.</p>
1798<p>Example using a file path:</p>
1799<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1800<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1801server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1802  <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
1803    headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>();
1804  }
1805
1806  <span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span class="hljs-params">err</span>) {
1807    <span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
1808    <span class="hljs-comment">// the other side.</span>
1809    <span class="hljs-keyword">try</span> {
1810      <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
1811        stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">404</span> });
1812      } <span class="hljs-keyword">else</span> {
1813        stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">500</span> });
1814      }
1815    } <span class="hljs-keyword">catch</span> (err) {
1816      <span class="hljs-comment">// Perform actual error handling.</span>
1817      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(err);
1818    }
1819    stream.<span class="hljs-title function_">end</span>();
1820  }
1821
1822  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
1823                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
1824                         { statCheck, onError });
1825});</code></pre>
1826<p>The <code>options.statCheck</code> function may also be used to cancel the send operation
1827by returning <code>false</code>. For instance, a conditional request may check the stat
1828results to determine if the file has been modified to return an appropriate
1829<code>304</code> response:</p>
1830<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1831<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1832server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1833  <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
1834    <span class="hljs-comment">// Check the stat here...</span>
1835    stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">304</span> });
1836    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Cancel the send operation</span>
1837  }
1838  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
1839                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
1840                         { statCheck });
1841});</code></pre>
1842<p>The <code>content-length</code> header field will be automatically set.</p>
1843<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
1844specific range subset. This can be used, for instance, to support HTTP Range
1845requests.</p>
1846<p>The <code>options.onError</code> function may also be used to handle all the errors
1847that could happen before the delivery of the file is initiated. The
1848default behavior is to destroy the stream.</p>
1849<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
1850will be emitted immediately after queuing the last chunk of payload data to be
1851sent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
1852header fields to the peer.</p>
1853<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
1854close when the final <code>DATA</code> frame is transmitted. User code must call either
1855<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
1856<code>Http2Stream</code>.</p>
1857<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1858<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1859server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
1860  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
1861                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
1862                         { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
1863  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
1864    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
1865  });
1866});</code></pre>
1867<h4>Class: <code>Http2Server</code><span><a class="mark" href="#http2_class_http2server" id="http2_class_http2server">#</a></span></h4>
1868<div class="api_metadata">
1869<span>Added in: v8.4.0</span>
1870</div>
1871<ul>
1872<li>Extends: <a href="net.html#net_class_net_server" class="type">&#x3C;net.Server></a></li>
1873</ul>
1874<p>Instances of <code>Http2Server</code> are created using the <code>http2.createServer()</code>
1875function. The <code>Http2Server</code> class is not exported directly by the <code>http2</code>
1876module.</p>
1877<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#http2_event_checkcontinue" id="http2_event_checkcontinue">#</a></span></h5>
1878<div class="api_metadata">
1879<span>Added in: v8.5.0</span>
1880</div>
1881<ul>
1882<li><code>request</code> <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
1883<li><code>response</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
1884</ul>
1885<p>If a <a href="#http2_event_request"><code>'request'</code></a> listener is registered or <a href="#http2_http2_createserver_options_onrequesthandler"><code>http2.createServer()</code></a> is
1886supplied a callback function, the <code>'checkContinue'</code> event is emitted each time
1887a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
1888not listened for, the server will automatically respond with a status
1889<code>100 Continue</code> as appropriate.</p>
1890<p>Handling this event involves calling <a href="#http2_response_writecontinue"><code>response.writeContinue()</code></a> if the
1891client should continue to send the request body, or generating an appropriate
1892HTTP response (e.g. 400 Bad Request) if the client should not continue to send
1893the request body.</p>
1894<p>When this event is emitted and handled, the <a href="#http2_event_request"><code>'request'</code></a> event will
1895not be emitted.</p>
1896<h5>Event: <code>'connection'</code><span><a class="mark" href="#http2_event_connection" id="http2_event_connection">#</a></span></h5>
1897<div class="api_metadata">
1898<span>Added in: v8.4.0</span>
1899</div>
1900<ul>
1901<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
1902</ul>
1903<p>This event is emitted when a new TCP stream is established. <code>socket</code> is
1904typically an object of type <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>. Usually users will not want to
1905access this event.</p>
1906<p>This event can also be explicitly emitted by users to inject connections
1907into the HTTP server. In that case, any <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream can be passed.</p>
1908<h5>Event: <code>'request'</code><span><a class="mark" href="#http2_event_request" id="http2_event_request">#</a></span></h5>
1909<div class="api_metadata">
1910<span>Added in: v8.4.0</span>
1911</div>
1912<ul>
1913<li><code>request</code> <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
1914<li><code>response</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
1915</ul>
1916<p>Emitted each time there is a request. There may be multiple requests
1917per session. See the <a href="#http2_compatibility_api">Compatibility API</a>.</p>
1918<h5>Event: <code>'session'</code><span><a class="mark" href="#http2_event_session" id="http2_event_session">#</a></span></h5>
1919<div class="api_metadata">
1920<span>Added in: v8.4.0</span>
1921</div>
1922<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
1923<code>Http2Server</code>.</p>
1924<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#http2_event_sessionerror" id="http2_event_sessionerror">#</a></span></h5>
1925<div class="api_metadata">
1926<span>Added in: v8.4.0</span>
1927</div>
1928<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
1929an <code>Http2Session</code> object associated with the <code>Http2Server</code>.</p>
1930<h5>Event: <code>'stream'</code><span><a class="mark" href="#http2_event_stream_1" id="http2_event_stream_1">#</a></span></h5>
1931<div class="api_metadata">
1932<span>Added in: v8.4.0</span>
1933</div>
1934<ul>
1935<li><code>stream</code> <a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
1936<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
1937<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
1938<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
1939their respective values.</li>
1940</ul>
1941<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
1942an <code>Http2Session</code> associated with the server.</p>
1943<p>See also <a href="#http2_event_stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
1944<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
1945<span class="hljs-keyword">const</span> {
1946  HTTP2_HEADER_METHOD,
1947  HTTP2_HEADER_PATH,
1948  HTTP2_HEADER_STATUS,
1949  HTTP2_HEADER_CONTENT_TYPE
1950} = http2.<span class="hljs-property">constants</span>;
1951
1952<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
1953server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> {
1954  <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
1955  <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
1956  <span class="hljs-comment">// ...</span>
1957  stream.<span class="hljs-title function_">respond</span>({
1958    [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
1959    [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
1960  });
1961  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
1962  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
1963});</code></pre>
1964<h5>Event: <code>'timeout'</code><span><a class="mark" href="#http2_event_timeout_2" id="http2_event_timeout_2">#</a></span></h5>
1965<div class="api_metadata">
1966<details class="changelog"><summary>History</summary>
1967<table>
1968<tbody><tr><th>Version</th><th>Changes</th></tr>
1969<tr><td>v13.0.0</td>
1970<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
1971<tr><td>v8.4.0</td>
1972<td><p><span>Added in: v8.4.0</span></p></td></tr>
1973</tbody></table>
1974</details>
1975</div>
1976<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
1977a given number of milliseconds set using <code>http2server.setTimeout()</code>.
1978<strong>Default:</strong> 0 (no timeout)</p>
1979<h5><code>server.close([callback])</code><span><a class="mark" href="#http2_server_close_callback" id="http2_server_close_callback">#</a></span></h5>
1980<div class="api_metadata">
1981<span>Added in: v8.4.0</span>
1982</div>
1983<ul>
1984<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
1985</ul>
1986<p>Stops the server from establishing new sessions. This does not prevent new
1987request streams from being created due to the persistent nature of HTTP/2
1988sessions. To gracefully shut down the server, call <a href="#http2_http2session_close_callback"><code>http2session.close()</code></a> on
1989all active sessions.</p>
1990<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
1991closed, although the server has already stopped allowing new sessions. See
1992<a href="net.html#net_server_close_callback"><code>net.Server.close()</code></a> for more details.</p>
1993<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#http2_server_settimeout_msecs_callback" id="http2_server_settimeout_msecs_callback">#</a></span></h5>
1994<div class="api_metadata">
1995<details class="changelog"><summary>History</summary>
1996<table>
1997<tbody><tr><th>Version</th><th>Changes</th></tr>
1998<tr><td>v13.0.0</td>
1999<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
2000<tr><td>v8.4.0</td>
2001<td><p><span>Added in: v8.4.0</span></p></td></tr>
2002</tbody></table>
2003</details>
2004</div>
2005<ul>
2006<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> 0 (no timeout)</li>
2007<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
2008<li>Returns: <a href="http2.html#http2_class_http2server" class="type">&#x3C;Http2Server></a></li>
2009</ul>
2010<p>Used to set the timeout value for http2 server requests,
2011and sets a callback function that is called when there is no activity
2012on the <code>Http2Server</code> after <code>msecs</code> milliseconds.</p>
2013<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
2014<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_CALLBACK</code>
2015error will be thrown.</p>
2016<h5><code>server.timeout</code><span><a class="mark" href="#http2_server_timeout" id="http2_server_timeout">#</a></span></h5>
2017<div class="api_metadata">
2018<details class="changelog"><summary>History</summary>
2019<table>
2020<tbody><tr><th>Version</th><th>Changes</th></tr>
2021<tr><td>v13.0.0</td>
2022<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
2023<tr><td>v8.4.0</td>
2024<td><p><span>Added in: v8.4.0</span></p></td></tr>
2025</tbody></table>
2026</details>
2027</div>
2028<ul>
2029<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li>
2030</ul>
2031<p>The number of milliseconds of inactivity before a socket is presumed
2032to have timed out.</p>
2033<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
2034<p>The socket timeout logic is set up on connection, so changing this
2035value only affects new connections to the server, not any existing connections.</p>
2036<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#http2_server_updatesettings_settings" id="http2_server_updatesettings_settings">#</a></span></h5>
2037<div class="api_metadata">
2038<span>Added in: v14.17.0</span>
2039</div>
2040<ul>
2041<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
2042</ul>
2043<p>Used to update the server with the provided settings.</p>
2044<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
2045<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
2046<h4>Class: <code>Http2SecureServer</code><span><a class="mark" href="#http2_class_http2secureserver" id="http2_class_http2secureserver">#</a></span></h4>
2047<div class="api_metadata">
2048<span>Added in: v8.4.0</span>
2049</div>
2050<ul>
2051<li>Extends: <a href="tls.html#tls_class_tls_server" class="type">&#x3C;tls.Server></a></li>
2052</ul>
2053<p>Instances of <code>Http2SecureServer</code> are created using the
2054<code>http2.createSecureServer()</code> function. The <code>Http2SecureServer</code> class is not
2055exported directly by the <code>http2</code> module.</p>
2056<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#http2_event_checkcontinue_1" id="http2_event_checkcontinue_1">#</a></span></h5>
2057<div class="api_metadata">
2058<span>Added in: v8.5.0</span>
2059</div>
2060<ul>
2061<li><code>request</code> <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
2062<li><code>response</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
2063</ul>
2064<p>If a <a href="#http2_event_request"><code>'request'</code></a> listener is registered or <a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer()</code></a>
2065is supplied a callback function, the <code>'checkContinue'</code> event is emitted each
2066time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
2067is not listened for, the server will automatically respond with a status
2068<code>100 Continue</code> as appropriate.</p>
2069<p>Handling this event involves calling <a href="#http2_response_writecontinue"><code>response.writeContinue()</code></a> if the
2070client should continue to send the request body, or generating an appropriate
2071HTTP response (e.g. 400 Bad Request) if the client should not continue to send
2072the request body.</p>
2073<p>When this event is emitted and handled, the <a href="#http2_event_request"><code>'request'</code></a> event will
2074not be emitted.</p>
2075<h5>Event: <code>'connection'</code><span><a class="mark" href="#http2_event_connection_1" id="http2_event_connection_1">#</a></span></h5>
2076<div class="api_metadata">
2077<span>Added in: v8.4.0</span>
2078</div>
2079<ul>
2080<li><code>socket</code> <a href="stream.html#stream_class_stream_duplex" class="type">&#x3C;stream.Duplex></a></li>
2081</ul>
2082<p>This event is emitted when a new TCP stream is established, before the TLS
2083handshake begins. <code>socket</code> is typically an object of type <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.
2084Usually users will not want to access this event.</p>
2085<p>This event can also be explicitly emitted by users to inject connections
2086into the HTTP server. In that case, any <a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream can be passed.</p>
2087<h5>Event: <code>'request'</code><span><a class="mark" href="#http2_event_request_1" id="http2_event_request_1">#</a></span></h5>
2088<div class="api_metadata">
2089<span>Added in: v8.4.0</span>
2090</div>
2091<ul>
2092<li><code>request</code> <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
2093<li><code>response</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
2094</ul>
2095<p>Emitted each time there is a request. There may be multiple requests
2096per session. See the <a href="#http2_compatibility_api">Compatibility API</a>.</p>
2097<h5>Event: <code>'session'</code><span><a class="mark" href="#http2_event_session_1" id="http2_event_session_1">#</a></span></h5>
2098<div class="api_metadata">
2099<span>Added in: v8.4.0</span>
2100</div>
2101<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
2102<code>Http2SecureServer</code>.</p>
2103<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#http2_event_sessionerror_1" id="http2_event_sessionerror_1">#</a></span></h5>
2104<div class="api_metadata">
2105<span>Added in: v8.4.0</span>
2106</div>
2107<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
2108an <code>Http2Session</code> object associated with the <code>Http2SecureServer</code>.</p>
2109<h5>Event: <code>'stream'</code><span><a class="mark" href="#http2_event_stream_2" id="http2_event_stream_2">#</a></span></h5>
2110<div class="api_metadata">
2111<span>Added in: v8.4.0</span>
2112</div>
2113<ul>
2114<li><code>stream</code> <a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
2115<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
2116<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
2117<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
2118their respective values.</li>
2119</ul>
2120<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
2121an <code>Http2Session</code> associated with the server.</p>
2122<p>See also <a href="#http2_event_stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
2123<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2124<span class="hljs-keyword">const</span> {
2125  HTTP2_HEADER_METHOD,
2126  HTTP2_HEADER_PATH,
2127  HTTP2_HEADER_STATUS,
2128  HTTP2_HEADER_CONTENT_TYPE
2129} = http2.<span class="hljs-property">constants</span>;
2130
2131<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSomehow</span>();
2132
2133<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
2134server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers, flags</span>) =></span> {
2135  <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
2136  <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
2137  <span class="hljs-comment">// ...</span>
2138  stream.<span class="hljs-title function_">respond</span>({
2139    [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
2140    [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
2141  });
2142  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
2143  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
2144});</code></pre>
2145<h5>Event: <code>'timeout'</code><span><a class="mark" href="#http2_event_timeout_3" id="http2_event_timeout_3">#</a></span></h5>
2146<div class="api_metadata">
2147<span>Added in: v8.4.0</span>
2148</div>
2149<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
2150a given number of milliseconds set using <code>http2secureServer.setTimeout()</code>.
2151<strong>Default:</strong> 2 minutes.</p>
2152<h5>Event: <code>'unknownProtocol'</code><span><a class="mark" href="#http2_event_unknownprotocol" id="http2_event_unknownprotocol">#</a></span></h5>
2153<div class="api_metadata">
2154<span>Added in: v8.4.0</span>
2155</div>
2156<p>The <code>'unknownProtocol'</code> event is emitted when a connecting client fails to
2157negotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler
2158receives the socket for handling. If no listener is registered for this event,
2159the connection is terminated. A timeout may be specified using the
2160<code>'unknownProtocolTimeout'</code> option passed to <a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer()</code></a>.
2161See the <a href="#http2_compatibility_api">Compatibility API</a>.</p>
2162<h5><code>server.close([callback])</code><span><a class="mark" href="#http2_server_close_callback_1" id="http2_server_close_callback_1">#</a></span></h5>
2163<div class="api_metadata">
2164<span>Added in: v8.4.0</span>
2165</div>
2166<ul>
2167<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
2168</ul>
2169<p>Stops the server from establishing new sessions. This does not prevent new
2170request streams from being created due to the persistent nature of HTTP/2
2171sessions. To gracefully shut down the server, call <a href="#http2_http2session_close_callback"><code>http2session.close()</code></a> on
2172all active sessions.</p>
2173<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
2174closed, although the server has already stopped allowing new sessions. See
2175<a href="tls.html#tls_server_close_callback"><code>tls.Server.close()</code></a> for more details.</p>
2176<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#http2_server_settimeout_msecs_callback_1" id="http2_server_settimeout_msecs_callback_1">#</a></span></h5>
2177<div class="api_metadata">
2178<span>Added in: v8.4.0</span>
2179</div>
2180<ul>
2181<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>120000</code> (2 minutes)</li>
2182<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
2183<li>Returns: <a href="http2.html#http2_class_http2secureserver" class="type">&#x3C;Http2SecureServer></a></li>
2184</ul>
2185<p>Used to set the timeout value for http2 secure server requests,
2186and sets a callback function that is called when there is no activity
2187on the <code>Http2SecureServer</code> after <code>msecs</code> milliseconds.</p>
2188<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
2189<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_CALLBACK</code>
2190error will be thrown.</p>
2191<h5><code>server.timeout</code><span><a class="mark" href="#http2_server_timeout_1" id="http2_server_timeout_1">#</a></span></h5>
2192<div class="api_metadata">
2193<details class="changelog"><summary>History</summary>
2194<table>
2195<tbody><tr><th>Version</th><th>Changes</th></tr>
2196<tr><td>v13.0.0</td>
2197<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
2198<tr><td>v8.4.0</td>
2199<td><p><span>Added in: v8.4.0</span></p></td></tr>
2200</tbody></table>
2201</details>
2202</div>
2203<ul>
2204<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li>
2205</ul>
2206<p>The number of milliseconds of inactivity before a socket is presumed
2207to have timed out.</p>
2208<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
2209<p>The socket timeout logic is set up on connection, so changing this
2210value only affects new connections to the server, not any existing connections.</p>
2211<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#http2_server_updatesettings_settings_1" id="http2_server_updatesettings_settings_1">#</a></span></h5>
2212<div class="api_metadata">
2213<span>Added in: v14.17.0</span>
2214</div>
2215<ul>
2216<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
2217</ul>
2218<p>Used to update the server with the provided settings.</p>
2219<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
2220<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
2221<h4><code>http2.createServer(options[, onRequestHandler])</code><span><a class="mark" href="#http2_http2_createserver_options_onrequesthandler" id="http2_http2_createserver_options_onrequesthandler">#</a></span></h4>
2222<div class="api_metadata">
2223<details class="changelog"><summary>History</summary>
2224<table>
2225<tbody><tr><th>Version</th><th>Changes</th></tr>
2226<tr><td>v14.16.0</td>
2227<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
2228<tr><td>v13.0.0</td>
2229<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr>
2230<tr><td>v13.3.0, v12.16.0</td>
2231<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
2232<tr><td>v13.3.0, v12.16.0</td>
2233<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
2234<tr><td>v12.4.0</td>
2235<td><p>The <code>options</code> parameter now supports <code>net.createServer()</code> options.</p></td></tr>
2236<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
2237<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
2238<tr><td>v9.6.0</td>
2239<td><p>Added the <code>Http1IncomingMessage</code> and <code>Http1ServerResponse</code> option.</p></td></tr>
2240<tr><td>v8.9.3</td>
2241<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
2242<tr><td>v8.9.3</td>
2243<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
2244<tr><td>v8.4.0</td>
2245<td><p><span>Added in: v8.4.0</span></p></td></tr>
2246</tbody></table>
2247</details>
2248</div>
2249<ul>
2250<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2251<ul>
2252<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
2253for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
2254<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
2255<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
2256<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
2257is permitted to use. The value is expressed in terms of number of megabytes,
2258e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
2259This is a credit based limit, existing <code>Http2Stream</code>s may cause this
2260limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
2261while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
2262the current memory use of the header compression tables, current data
2263queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
2264counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
2265<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
2266This is similar to <a href="http.html#http_server_maxheaderscount"><code>http.Server#maxHeadersCount</code></a> or
2267<a href="http.html#http_request_maxheaderscount"><code>http.ClientRequest#maxHeadersCount</code></a>. The minimum value is <code>4</code>.
2268<strong>Default:</strong> <code>128</code>.</li>
2269<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
2270unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
2271<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
2272serialized, compressed block of headers. Attempts to send headers that
2273exceed this limit will result in a <code>'frameError'</code> event being emitted
2274and the stream being closed and destroyed.</li>
2275<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The strategy used for determining the amount of
2276padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
2277<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
2278<ul>
2279<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
2280<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
2281determined by the internal implementation, is applied.</li>
2282<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
2283padding to ensure that the total frame length, including the 9-byte
2284header, is a multiple of 8. For each frame, there is a maximum allowed
2285number of padding bytes that is determined by current flow control state
2286and settings. If this maximum is less than the calculated amount needed to
2287ensure alignment, the maximum is used and the total frame length is not
2288necessarily aligned at 8 bytes.</li>
2289</ul>
2290</li>
2291<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
2292streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
2293be overridden if the remote peer sets its own value for
2294<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
2295<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of invalid
2296frames that will be tolerated before the session is closed.
2297<strong>Default:</strong> <code>1000</code>.</li>
2298<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of rejected
2299upon creation streams that will be tolerated before the session is closed.
2300Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
2301error that should tell the peer to not open any more streams, continuing
2302to open streams is therefore regarded as a sign of a misbehaving peer.
2303<strong>Default:</strong> <code>100</code>.</li>
2304<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
2305remote peer upon connection.</li>
2306<li><code>Http1IncomingMessage</code> <a href="http.html#http_class_http_incomingmessage" class="type">&#x3C;http.IncomingMessage></a> Specifies the
2307<code>IncomingMessage</code> class to used for HTTP/1 fallback. Useful for extending
2308the original <code>http.IncomingMessage</code>. <strong>Default:</strong> <code>http.IncomingMessage</code>.</li>
2309<li><code>Http1ServerResponse</code> <a href="http.html#http_class_http_serverresponse" class="type">&#x3C;http.ServerResponse></a> Specifies the <code>ServerResponse</code>
2310class to used for HTTP/1 fallback. Useful for extending the original
2311<code>http.ServerResponse</code>. <strong>Default:</strong> <code>http.ServerResponse</code>.</li>
2312<li><code>Http2ServerRequest</code> <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a> Specifies the
2313<code>Http2ServerRequest</code> class to use.
2314Useful for extending the original <code>Http2ServerRequest</code>.
2315<strong>Default:</strong> <code>Http2ServerRequest</code>.</li>
2316<li><code>Http2ServerResponse</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> Specifies the
2317<code>Http2ServerResponse</code> class to use.
2318Useful for extending the original <code>Http2ServerResponse</code>.
2319<strong>Default:</strong> <code>Http2ServerResponse</code>.</li>
2320<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
2321a server should wait when an <a href="#http2_event_unknownprotocol"><code>'unknownProtocol'</code></a> is emitted. If the
2322socket has not been destroyed by that time the server will destroy it.
2323<strong>Default:</strong> <code>10000</code>.</li>
2324<li>...: Any <a href="net.html#net_net_createserver_options_connectionlistener"><code>net.createServer()</code></a> option can be provided.</li>
2325</ul>
2326</li>
2327<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#http2_compatibility_api">Compatibility API</a></li>
2328<li>Returns: <a href="http2.html#http2_class_http2server" class="type">&#x3C;Http2Server></a></li>
2329</ul>
2330<p>Returns a <code>net.Server</code> instance that creates and manages <code>Http2Session</code>
2331instances.</p>
2332<p>Since there are no browsers known that support
2333<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
2334<a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
2335with browser clients.</p>
2336<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2337
2338<span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
2339<span class="hljs-comment">// Since there are no browsers known that support</span>
2340<span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
2341<span class="hljs-comment">// is necessary when communicating with browser clients.</span>
2342<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
2343
2344server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
2345  stream.<span class="hljs-title function_">respond</span>({
2346    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
2347    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
2348  });
2349  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
2350});
2351
2352server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code></pre>
2353<h4><code>http2.createSecureServer(options[, onRequestHandler])</code><span><a class="mark" href="#http2_http2_createsecureserver_options_onrequesthandler" id="http2_http2_createsecureserver_options_onrequesthandler">#</a></span></h4>
2354<div class="api_metadata">
2355<details class="changelog"><summary>History</summary>
2356<table>
2357<tbody><tr><th>Version</th><th>Changes</th></tr>
2358<tr><td>v14.16.0</td>
2359<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
2360<tr><td>v13.0.0</td>
2361<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr>
2362<tr><td>v13.3.0, v12.16.0</td>
2363<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
2364<tr><td>v13.3.0, v12.16.0</td>
2365<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
2366<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
2367<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
2368<tr><td>v10.12.0</td>
2369<td><p>Added the <code>origins</code> option to automatically send an <code>ORIGIN</code> frame on <code>Http2Session</code> startup.</p></td></tr>
2370<tr><td>v8.9.3</td>
2371<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
2372<tr><td>v8.9.3</td>
2373<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
2374<tr><td>v8.4.0</td>
2375<td><p><span>Added in: v8.4.0</span></p></td></tr>
2376</tbody></table>
2377</details>
2378</div>
2379<ul>
2380<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2381<ul>
2382<li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Incoming client connections that do not support
2383HTTP/2 will be downgraded to HTTP/1.x when set to <code>true</code>.
2384See the <a href="#http2_event_unknownprotocol"><code>'unknownProtocol'</code></a> event. See <a href="#http2_alpn_negotiation">ALPN negotiation</a>.
2385<strong>Default:</strong> <code>false</code>.</li>
2386<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
2387for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
2388<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
2389<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
2390<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
2391is permitted to use. The value is expressed in terms of number of megabytes,
2392e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. This is a
2393credit based limit, existing <code>Http2Stream</code>s may cause this
2394limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
2395while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
2396the current memory use of the header compression tables, current data
2397queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
2398counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
2399<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
2400This is similar to <a href="http.html#http_server_maxheaderscount"><code>http.Server#maxHeadersCount</code></a> or
2401<a href="http.html#http_request_maxheaderscount"><code>http.ClientRequest#maxHeadersCount</code></a>. The minimum value is <code>4</code>.
2402<strong>Default:</strong> <code>128</code>.</li>
2403<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
2404unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
2405<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
2406serialized, compressed block of headers. Attempts to send headers that
2407exceed this limit will result in a <code>'frameError'</code> event being emitted
2408and the stream being closed and destroyed.</li>
2409<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Strategy used for determining the amount of
2410padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
2411<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
2412<ul>
2413<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
2414<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
2415determined by the internal implementation, is applied.</li>
2416<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
2417padding to ensure that the total frame length, including the
24189-byte header, is a multiple of 8. For each frame, there is a maximum
2419allowed number of padding bytes that is determined by current flow control
2420state and settings. If this maximum is less than the calculated amount
2421needed to ensure alignment, the maximum is used and the total frame length
2422is not necessarily aligned at 8 bytes.</li>
2423</ul>
2424</li>
2425<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
2426streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
2427be overridden if the remote peer sets its own value for
2428<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
2429<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of invalid
2430frames that will be tolerated before the session is closed.
2431<strong>Default:</strong> <code>1000</code>.</li>
2432<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of rejected
2433upon creation streams that will be tolerated before the session is closed.
2434Each rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
2435error that should tell the peer to not open any more streams, continuing
2436to open streams is therefore regarded as a sign of a misbehaving peer.
2437<strong>Default:</strong> <code>100</code>.</li>
2438<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
2439remote peer upon connection.</li>
2440<li>...: Any <a href="tls.html#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a> options can be provided. For
2441servers, the identity options (<code>pfx</code> or <code>key</code>/<code>cert</code>) are usually required.</li>
2442<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array of origin strings to send within an <code>ORIGIN</code>
2443frame immediately following creation of a new server <code>Http2Session</code>.</li>
2444<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
2445a server should wait when an <a href="#http2_event_unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
2446the socket has not been destroyed by that time the server will destroy it.
2447<strong>Default:</strong> <code>10000</code>.</li>
2448</ul>
2449</li>
2450<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#http2_compatibility_api">Compatibility API</a></li>
2451<li>Returns: <a href="http2.html#http2_class_http2secureserver" class="type">&#x3C;Http2SecureServer></a></li>
2452</ul>
2453<p>Returns a <code>tls.Server</code> instance that creates and manages <code>Http2Session</code>
2454instances.</p>
2455<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2456<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
2457
2458<span class="hljs-keyword">const</span> options = {
2459  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>),
2460  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>)
2461};
2462
2463<span class="hljs-comment">// Create a secure HTTP/2 server</span>
2464<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
2465
2466server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
2467  stream.<span class="hljs-title function_">respond</span>({
2468    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
2469    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
2470  });
2471  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
2472});
2473
2474server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code></pre>
2475<h4><code>http2.connect(authority[, options][, listener])</code><span><a class="mark" href="#http2_http2_connect_authority_options_listener" id="http2_http2_connect_authority_options_listener">#</a></span></h4>
2476<div class="api_metadata">
2477<details class="changelog"><summary>History</summary>
2478<table>
2479<tbody><tr><th>Version</th><th>Changes</th></tr>
2480<tr><td>v14.16.0</td>
2481<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
2482<tr><td>v13.0.0</td>
2483<td><p>The <code>PADDING_STRATEGY_CALLBACK</code> has been made equivalent to providing <code>PADDING_STRATEGY_ALIGNED</code> and <code>selectPadding</code> has been removed.</p></td></tr>
2484<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
2485<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
2486<tr><td>v8.9.3</td>
2487<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
2488<tr><td>v8.9.3</td>
2489<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
2490<tr><td>v8.4.0</td>
2491<td><p><span>Added in: v8.4.0</span></p></td></tr>
2492</tbody></table>
2493</details>
2494</div>
2495<ul>
2496<li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#url_the_whatwg_url_api" class="type">&#x3C;URL></a> The remote HTTP/2 server to connect to. This must
2497be in the form of a minimal, valid URL with the <code>http://</code> or <code>https://</code>
2498prefix, host name, and IP port (if a non-default port is used). Userinfo
2499(user ID and password), path, querystring, and fragment details in the
2500URL will be ignored.</li>
2501<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
2502<ul>
2503<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
2504for deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
2505<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
2506<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
2507<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
2508is permitted to use. The value is expressed in terms of number of megabytes,
2509e.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
2510This is a credit based limit, existing <code>Http2Stream</code>s may cause this
2511limit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
2512while this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
2513the current memory use of the header compression tables, current data
2514queued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
2515counted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
2516<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
2517This is similar to <a href="http.html#http_server_maxheaderscount"><code>http.Server#maxHeadersCount</code></a> or
2518<a href="http.html#http_request_maxheaderscount"><code>http.ClientRequest#maxHeadersCount</code></a>. The minimum value is <code>1</code>.
2519<strong>Default:</strong> <code>128</code>.</li>
2520<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
2521unacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
2522<li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of reserved push
2523streams the client will accept at any given time. Once the current number of
2524currently reserved push streams exceeds reaches this limit, new push streams
2525sent by the server will be automatically rejected. The minimum allowed value
2526is 0. The maximum allowed value is 22<sup>32</sup>-1. A negative value sets
2527this option to the maximum allowed value. <strong>Default:</strong> <code>200</code>.</li>
2528<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
2529serialized, compressed block of headers. Attempts to send headers that
2530exceed this limit will result in a <code>'frameError'</code> event being emitted
2531and the stream being closed and destroyed.</li>
2532<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Strategy used for determining the amount of
2533padding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
2534<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
2535<ul>
2536<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
2537<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
2538determined by the internal implementation, is applied.</li>
2539<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
2540padding to ensure that the total frame length, including the
25419-byte header, is a multiple of 8. For each frame, there is a maximum
2542allowed number of padding bytes that is determined by current flow control
2543state and settings. If this maximum is less than the calculated amount
2544needed to ensure alignment, the maximum is used and the total frame length
2545is not necessarily aligned at 8 bytes.</li>
2546</ul>
2547</li>
2548<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
2549streams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
2550be overridden if the remote peer sets its own value for
2551<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
2552<li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The protocol to connect with, if not set in the
2553<code>authority</code>. Value may be either <code>'http:'</code> or <code>'https:'</code>. <strong>Default:</strong>
2554<code>'https:'</code></li>
2555<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
2556remote peer upon connection.</li>
2557<li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional callback that receives the <code>URL</code>
2558instance passed to <code>connect</code> and the <code>options</code> object, and returns any
2559<a href="stream.html#stream_class_stream_duplex"><code>Duplex</code></a> stream that is to be used as the connection for this session.</li>
2560<li>...: Any <a href="net.html#net_net_connect"><code>net.connect()</code></a> or <a href="tls.html#tls_tls_connect_options_callback"><code>tls.connect()</code></a> options can be provided.</li>
2561<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
2562a server should wait when an <a href="#http2_event_unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
2563the socket has not been destroyed by that time the server will destroy it.
2564<strong>Default:</strong> <code>10000</code>.</li>
2565</ul>
2566</li>
2567<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Will be registered as a one-time listener of the
2568<a href="#http2_event_connect"><code>'connect'</code></a> event.</li>
2569<li>Returns: <a href="http2.html#http2_class_clienthttp2session" class="type">&#x3C;ClientHttp2Session></a></li>
2570</ul>
2571<p>Returns a <code>ClientHttp2Session</code> instance.</p>
2572<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2573<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
2574
2575<span class="hljs-comment">/* Use the client */</span>
2576
2577client.<span class="hljs-title function_">close</span>();</code></pre>
2578<h4><code>http2.constants</code><span><a class="mark" href="#http2_http2_constants" id="http2_http2_constants">#</a></span></h4>
2579<div class="api_metadata">
2580<span>Added in: v8.4.0</span>
2581</div>
2582<h5>Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code><span><a class="mark" href="#http2_error_codes_for_rst_stream_and_goaway" id="http2_error_codes_for_rst_stream_and_goaway">#</a></span></h5>
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663<table><thead><tr><th>Value</th><th>Name</th><th>Constant</th></tr></thead><tbody><tr><td><code>0x00</code></td><td>No Error</td><td><code>http2.constants.NGHTTP2_NO_ERROR</code></td></tr><tr><td><code>0x01</code></td><td>Protocol Error</td><td><code>http2.constants.NGHTTP2_PROTOCOL_ERROR</code></td></tr><tr><td><code>0x02</code></td><td>Internal Error</td><td><code>http2.constants.NGHTTP2_INTERNAL_ERROR</code></td></tr><tr><td><code>0x03</code></td><td>Flow Control Error</td><td><code>http2.constants.NGHTTP2_FLOW_CONTROL_ERROR</code></td></tr><tr><td><code>0x04</code></td><td>Settings Timeout</td><td><code>http2.constants.NGHTTP2_SETTINGS_TIMEOUT</code></td></tr><tr><td><code>0x05</code></td><td>Stream Closed</td><td><code>http2.constants.NGHTTP2_STREAM_CLOSED</code></td></tr><tr><td><code>0x06</code></td><td>Frame Size Error</td><td><code>http2.constants.NGHTTP2_FRAME_SIZE_ERROR</code></td></tr><tr><td><code>0x07</code></td><td>Refused Stream</td><td><code>http2.constants.NGHTTP2_REFUSED_STREAM</code></td></tr><tr><td><code>0x08</code></td><td>Cancel</td><td><code>http2.constants.NGHTTP2_CANCEL</code></td></tr><tr><td><code>0x09</code></td><td>Compression Error</td><td><code>http2.constants.NGHTTP2_COMPRESSION_ERROR</code></td></tr><tr><td><code>0x0a</code></td><td>Connect Error</td><td><code>http2.constants.NGHTTP2_CONNECT_ERROR</code></td></tr><tr><td><code>0x0b</code></td><td>Enhance Your Calm</td><td><code>http2.constants.NGHTTP2_ENHANCE_YOUR_CALM</code></td></tr><tr><td><code>0x0c</code></td><td>Inadequate Security</td><td><code>http2.constants.NGHTTP2_INADEQUATE_SECURITY</code></td></tr><tr><td><code>0x0d</code></td><td>HTTP/1.1 Required</td><td><code>http2.constants.NGHTTP2_HTTP_1_1_REQUIRED</code></td></tr></tbody></table>
2664<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
2665a given number of milliseconds set using <code>http2server.setTimeout()</code>.</p>
2666<h4><code>http2.getDefaultSettings()</code><span><a class="mark" href="#http2_http2_getdefaultsettings" id="http2_http2_getdefaultsettings">#</a></span></h4>
2667<div class="api_metadata">
2668<span>Added in: v8.4.0</span>
2669</div>
2670<ul>
2671<li>Returns: <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
2672</ul>
2673<p>Returns an object containing the default settings for an <code>Http2Session</code>
2674instance. This method returns a new object instance every time it is called
2675so instances returned may be safely modified for use.</p>
2676<h4><code>http2.getPackedSettings([settings])</code><span><a class="mark" href="#http2_http2_getpackedsettings_settings" id="http2_http2_getpackedsettings_settings">#</a></span></h4>
2677<div class="api_metadata">
2678<span>Added in: v8.4.0</span>
2679</div>
2680<ul>
2681<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
2682<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2683</ul>
2684<p>Returns a <code>Buffer</code> instance containing serialized representation of the given
2685HTTP/2 settings as specified in the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> specification. This is intended
2686for use with the <code>HTTP2-Settings</code> header field.</p>
2687<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2688
2689<span class="hljs-keyword">const</span> packed = http2.<span class="hljs-title function_">getPackedSettings</span>({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> });
2690
2691<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(packed.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>));
2692<span class="hljs-comment">// Prints: AAIAAAAA</span></code></pre>
2693<h4><code>http2.getUnpackedSettings(buf)</code><span><a class="mark" href="#http2_http2_getunpackedsettings_buf" id="http2_http2_getunpackedsettings_buf">#</a></span></h4>
2694<div class="api_metadata">
2695<span>Added in: v8.4.0</span>
2696</div>
2697<ul>
2698<li><code>buf</code> <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> The packed settings.</li>
2699<li>Returns: <a href="http2.html#http2_settings_object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
2700</ul>
2701<p>Returns a <a href="#http2_settings_object">HTTP/2 Settings Object</a> containing the deserialized settings from
2702the given <code>Buffer</code> as generated by <code>http2.getPackedSettings()</code>.</p>
2703<h4><code>http2.sensitiveHeaders</code><span><a class="mark" href="#http2_http2_sensitiveheaders" id="http2_http2_sensitiveheaders">#</a></span></h4>
2704<div class="api_metadata">
2705<span>Added in: v14.18.0</span>
2706</div>
2707<ul>
2708<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
2709</ul>
2710<p>This symbol can be set as a property on the HTTP/2 headers object with an array
2711value in order to provide a list of headers considered sensitive.
2712See <a href="#http2_sensitive_headers">Sensitive headers</a> for more details.</p>
2713<h4>Headers object<span><a class="mark" href="#http2_headers_object" id="http2_headers_object">#</a></span></h4>
2714<p>Headers are represented as own-properties on JavaScript objects. The property
2715keys will be serialized to lower-case. Property values should be strings (if
2716they are not they will be coerced to strings) or an <code>Array</code> of strings (in order
2717to send more than one value per header field).</p>
2718<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
2719  <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
2720  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
2721  <span class="hljs-string">'ABC'</span>: [<span class="hljs-string">'has'</span>, <span class="hljs-string">'more'</span>, <span class="hljs-string">'than'</span>, <span class="hljs-string">'one'</span>, <span class="hljs-string">'value'</span>]
2722};
2723
2724stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
2725<p>Header objects passed to callback functions will have a <code>null</code> prototype. This
2726means that normal JavaScript object methods such as
2727<code>Object.prototype.toString()</code> and <code>Object.prototype.hasOwnProperty()</code> will
2728not work.</p>
2729<p>For incoming headers:</p>
2730<ul>
2731<li>The <code>:status</code> header is converted to <code>number</code>.</li>
2732<li>Duplicates of <code>:status</code>, <code>:method</code>, <code>:authority</code>, <code>:scheme</code>, <code>:path</code>,
2733<code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-credentials</code>,
2734<code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-encoding</code>,
2735<code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>content-md5</code>,
2736<code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>etag</code>, <code>expires</code>, <code>from</code>,
2737<code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</code>,
2738<code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>,
2739<code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>, <code>tk</code>,
2740<code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</code> are
2741discarded.</li>
2742<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
2743<li>For duplicate <code>cookie</code> headers, the values are joined together with '; '.</li>
2744<li>For all other headers, the values are joined together with ', '.</li>
2745</ul>
2746<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2747<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
2748server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
2749  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':path'</span>]);
2750  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers.<span class="hljs-property">ABC</span>);
2751});</code></pre>
2752<h5>Sensitive headers<span><a class="mark" href="#http2_sensitive_headers" id="http2_sensitive_headers">#</a></span></h5>
2753<p>HTTP2 headers can be marked as sensitive, which means that the HTTP/2
2754header compression algorithm will never index them. This can make sense for
2755header values with low entropy and that may be considered valuable to an
2756attacker, for example <code>Cookie</code> or <code>Authorization</code>. To achieve this, add
2757the header name to the <code>[http2.sensitiveHeaders]</code> property as an array:</p>
2758<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
2759  <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
2760  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
2761  <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
2762  <span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret data'</span>,
2763  [http2.<span class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <span class="hljs-string">'other-sensitive-header'</span>]
2764};
2765
2766stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
2767<p>For some headers, such as <code>Authorization</code> and short <code>Cookie</code> headers,
2768this flag is set automatically.</p>
2769<p>This property is also set for received headers. It will contain the names of
2770all headers marked as sensitive, including ones marked that way automatically.</p>
2771<h4>Settings object<span><a class="mark" href="#http2_settings_object" id="http2_settings_object">#</a></span></h4>
2772<div class="api_metadata">
2773<details class="changelog"><summary>History</summary>
2774<table>
2775<tbody><tr><th>Version</th><th>Changes</th></tr>
2776<tr><td>v12.12.0</td>
2777<td><p>The <code>maxConcurrentStreams</code> setting is stricter.</p></td></tr>
2778<tr><td>v8.9.3</td>
2779<td><p>The <code>maxHeaderListSize</code> setting is now strictly enforced.</p></td></tr>
2780<tr><td>v8.4.0</td>
2781<td><p><span>Added in: v8.4.0</span></p></td></tr>
2782</tbody></table>
2783</details>
2784</div>
2785<p>The <code>http2.getDefaultSettings()</code>, <code>http2.getPackedSettings()</code>,
2786<code>http2.createServer()</code>, <code>http2.createSecureServer()</code>,
2787<code>http2session.settings()</code>, <code>http2session.localSettings</code>, and
2788<code>http2session.remoteSettings</code> APIs either return or receive as input an
2789object that defines configuration settings for an <code>Http2Session</code> object.
2790These objects are ordinary JavaScript objects containing the following
2791properties.</p>
2792<ul>
2793<li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum number of bytes used for
2794header compression. The minimum allowed value is 0. The maximum allowed value
2795is 22<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
2796<li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Specifies <code>true</code> if HTTP/2 Push Streams are to be
2797permitted on the <code>Http2Session</code> instances. <strong>Default:</strong> <code>true</code>.</li>
2798<li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the <em>sender's</em> initial window size in
2799bytes for stream-level flow control. The minimum allowed value is 0. The
2800maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
2801<li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the size in bytes of the largest frame
2802payload. The minimum allowed value is 16,384. The maximum allowed value is
280322<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
2804<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum number of concurrent
2805streams permitted on an <code>Http2Session</code>. There is no default value which
2806implies, at least theoretically, 2<sup>32</sup>-1 streams may be open
2807concurrently at any given time in an <code>Http2Session</code>. The minimum value
2808is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
2809<code>4294967295</code>.</li>
2810<li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum size (uncompressed octets)
2811of header list that will be accepted. The minimum allowed value is 0. The
2812maximum allowed value is 22<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
2813<li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>maxHeaderListSize</code>.</li>
2814<li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Specifies <code>true</code> if the "Extended Connect
2815Protocol" defined by <a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> is to be enabled. This setting is only
2816meaningful if sent by the server. Once the <code>enableConnectProtocol</code> setting
2817has been enabled for a given <code>Http2Session</code>, it cannot be disabled.
2818<strong>Default:</strong> <code>false</code>.</li>
2819</ul>
2820<p>All additional properties on the settings object are ignored.</p>
2821<h4>Error handling<span><a class="mark" href="#http2_error_handling" id="http2_error_handling">#</a></span></h4>
2822<p>There are several types of error conditions that may arise when using the
2823<code>http2</code> module:</p>
2824<p>Validation errors occur when an incorrect argument, option, or setting value is
2825passed in. These will always be reported by a synchronous <code>throw</code>.</p>
2826<p>State errors occur when an action is attempted at an incorrect time (for
2827instance, attempting to send data on a stream after it has closed). These will
2828be reported using either a synchronous <code>throw</code> or via an <code>'error'</code> event on
2829the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending on where
2830and when the error occurs.</p>
2831<p>Internal errors occur when an HTTP/2 session fails unexpectedly. These will be
2832reported via an <code>'error'</code> event on the <code>Http2Session</code> or HTTP/2 Server objects.</p>
2833<p>Protocol errors occur when various HTTP/2 protocol constraints are violated.
2834These will be reported using either a synchronous <code>throw</code> or via an <code>'error'</code>
2835event on the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending
2836on where and when the error occurs.</p>
2837<h4>Invalid character handling in header names and values<span><a class="mark" href="#http2_invalid_character_handling_in_header_names_and_values" id="http2_invalid_character_handling_in_header_names_and_values">#</a></span></h4>
2838<p>The HTTP/2 implementation applies stricter handling of invalid characters in
2839HTTP header names and values than the HTTP/1 implementation.</p>
2840<p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
2841strictly as lower-case strings. The API provided by Node.js allows header
2842names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
2843those to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
2844<p>Header field-names <em>must only</em> contain one or more of the following ASCII
2845characters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</code>, <code>!</code>, <code>#</code>, <code>$</code>, <code>%</code>, <code>&#x26;</code>, <code>'</code>, <code>*</code>, <code>+</code>,
2846<code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|</code>, and <code>~</code>.</p>
2847<p>Using invalid characters within an HTTP header field name will cause the
2848stream to be closed with a protocol error being reported.</p>
2849<p>Header field values are handled with more leniency but <em>should</em> not contain
2850new-line or carriage return characters and <em>should</em> be limited to US-ASCII
2851characters, per the requirements of the HTTP specification.</p>
2852<h4>Push streams on the client<span><a class="mark" href="#http2_push_streams_on_the_client" id="http2_push_streams_on_the_client">#</a></span></h4>
2853<p>To receive pushed streams on the client, set a listener for the <code>'stream'</code>
2854event on the <code>ClientHttp2Session</code>:</p>
2855<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2856
2857<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost'</span>);
2858
2859client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">pushedStream, requestHeaders</span>) =></span> {
2860  pushedStream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class="hljs-function">(<span class="hljs-params">responseHeaders</span>) =></span> {
2861    <span class="hljs-comment">// Process response headers</span>
2862  });
2863  pushedStream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> { <span class="hljs-comment">/* handle pushed data */</span> });
2864});
2865
2866<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });</code></pre>
2867<h4>Supporting the <code>CONNECT</code> method<span><a class="mark" href="#http2_supporting_the_connect_method" id="http2_supporting_the_connect_method">#</a></span></h4>
2868<p>The <code>CONNECT</code> method is used to allow an HTTP/2 server to be used as a proxy
2869for TCP/IP connections.</p>
2870<p>A simple TCP Server:</p>
2871<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);
2872
2873<span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
2874  <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
2875  socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
2876  socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> name += chunk);
2877  socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">`hello <span class="hljs-subst">${name}</span>`</span>));
2878});
2879
2880server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre>
2881<p>An HTTP/2 CONNECT proxy:</p>
2882<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2883<span class="hljs-keyword">const</span> { NGHTTP2_REFUSED_STREAM } = http2.<span class="hljs-property">constants</span>;
2884<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>);
2885
2886<span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServer</span>();
2887proxy.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream, headers</span>) =></span> {
2888  <span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <span class="hljs-string">'CONNECT'</span>) {
2889    <span class="hljs-comment">// Only accept CONNECT requests</span>
2890    stream.<span class="hljs-title function_">close</span>(NGHTTP2_REFUSED_STREAM);
2891    <span class="hljs-keyword">return</span>;
2892  }
2893  <span class="hljs-keyword">const</span> auth = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">`tcp://<span class="hljs-subst">${headers[<span class="hljs-string">':authority'</span>]}</span>`</span>);
2894  <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
2895  <span class="hljs-comment">// things this proxy should be connecting to.</span>
2896  <span class="hljs-keyword">const</span> socket = net.<span class="hljs-title function_">connect</span>(auth.<span class="hljs-property">port</span>, auth.<span class="hljs-property">hostname</span>, <span class="hljs-function">() =></span> {
2897    stream.<span class="hljs-title function_">respond</span>();
2898    socket.<span class="hljs-title function_">pipe</span>(stream);
2899    stream.<span class="hljs-title function_">pipe</span>(socket);
2900  });
2901  socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> {
2902    stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants</span>.<span class="hljs-property">NGHTTP2_CONNECT_ERROR</span>);
2903  });
2904});
2905
2906proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</code></pre>
2907<p>An HTTP/2 CONNECT client:</p>
2908<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2909
2910<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8001'</span>);
2911
2912<span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
2913<span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
2914<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({
2915  <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>,
2916  <span class="hljs-string">':authority'</span>: <span class="hljs-string">`localhost:<span class="hljs-subst">${port}</span>`</span>
2917});
2918
2919req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
2920  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[http2.<span class="hljs-property">constants</span>.<span class="hljs-property">HTTP2_HEADER_STATUS</span>]);
2921});
2922<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
2923req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
2924req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> data += chunk);
2925req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
2926  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The server says: <span class="hljs-subst">${data}</span>`</span>);
2927  client.<span class="hljs-title function_">close</span>();
2928});
2929req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code></pre>
2930<h4>The extended <code>CONNECT</code> protocol<span><a class="mark" href="#http2_the_extended_connect_protocol" id="http2_the_extended_connect_protocol">#</a></span></h4>
2931<p><a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> defines an "Extended CONNECT Protocol" extension to HTTP/2 that
2932may be used to bootstrap the use of an <code>Http2Stream</code> using the <code>CONNECT</code>
2933method as a tunnel for other communication protocols (such as WebSockets).</p>
2934<p>The use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using
2935the <code>enableConnectProtocol</code> setting:</p>
2936<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2937<span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol</span>: <span class="hljs-literal">true</span> };
2938<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>({ settings });</code></pre>
2939<p>Once the client receives the <code>SETTINGS</code> frame from the server indicating that
2940the extended CONNECT may be used, it may send <code>CONNECT</code> requests that use the
2941<code>':protocol'</code> HTTP/2 pseudo-header:</p>
2942<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2943<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8080'</span>);
2944client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
2945  <span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</span>) {
2946    <span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>, <span class="hljs-string">':protocol'</span>: <span class="hljs-string">'foo'</span> });
2947    <span class="hljs-comment">// ...</span>
2948  }
2949});</code></pre>
2950</section><section><h3>Compatibility API<span><a class="mark" href="#http2_compatibility_api" id="http2_compatibility_api">#</a></span></h3>
2951<p>The Compatibility API has the goal of providing a similar developer experience
2952of HTTP/1 when using HTTP/2, making it possible to develop applications
2953that support both <a href="http.html">HTTP/1</a> and HTTP/2. This API targets only the
2954<strong>public API</strong> of the <a href="http.html">HTTP/1</a>. However many modules use internal
2955methods or state, and those <em>are not supported</em> as it is a completely
2956different implementation.</p>
2957<p>The following example creates an HTTP/2 server using the compatibility
2958API:</p>
2959<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2960<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
2961  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
2962  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
2963  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> });
2964  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
2965});</code></pre>
2966<p>In order to create a mixed <a href="https.html">HTTPS</a> and HTTP/2 server, refer to the
2967<a href="#http2_alpn_negotiation">ALPN negotiation</a> section.
2968Upgrading from non-tls HTTP/1 servers is not supported.</p>
2969<p>The HTTP/2 compatibility API is composed of <a href="#http2_class_http2_http2serverrequest"><code>Http2ServerRequest</code></a> and
2970<a href="#http2_class_http2_http2serverresponse"><code>Http2ServerResponse</code></a>. They aim at API compatibility with HTTP/1, but
2971they do not hide the differences between the protocols. As an example,
2972the status message for HTTP codes is ignored.</p>
2973<h4>ALPN negotiation<span><a class="mark" href="#http2_alpn_negotiation" id="http2_alpn_negotiation">#</a></span></h4>
2974<p>ALPN negotiation allows supporting both <a href="https.html">HTTPS</a> and HTTP/2 over
2975the same socket. The <code>req</code> and <code>res</code> objects can be either HTTP/1 or
2976HTTP/2, and an application <strong>must</strong> restrict itself to the public API of
2977<a href="http.html">HTTP/1</a>, and detect if it is possible to use the more advanced
2978features of HTTP/2.</p>
2979<p>The following example creates a server that supports both protocols:</p>
2980<pre><code class="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
2981<span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
2982
2983<span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./cert.pem'</span>);
2984<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./key.pem'</span>);
2985
2986<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(
2987  { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
2988  onRequest
2989).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
2990
2991<span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<span class="hljs-params">req, res</span>) {
2992  <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
2993  <span class="hljs-keyword">const</span> { <span class="hljs-attr">socket</span>: { alpnProtocol } } = req.<span class="hljs-property">httpVersion</span> === <span class="hljs-string">'2.0'</span> ?
2994    req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
2995  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'application/json'</span> });
2996  res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>({
2997    alpnProtocol,
2998    <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>
2999  }));
3000}</code></pre>
3001<p>The <code>'request'</code> event works identically on both <a href="https.html">HTTPS</a> and
3002HTTP/2.</p>
3003<h4>Class: <code>http2.Http2ServerRequest</code><span><a class="mark" href="#http2_class_http2_http2serverrequest" id="http2_class_http2_http2serverrequest">#</a></span></h4>
3004<div class="api_metadata">
3005<span>Added in: v8.4.0</span>
3006</div>
3007<ul>
3008<li>Extends: <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a></li>
3009</ul>
3010<p>A <code>Http2ServerRequest</code> object is created by <a href="#http2_class_http2server"><code>http2.Server</code></a> or
3011<a href="#http2_class_http2secureserver"><code>http2.SecureServer</code></a> and passed as the first argument to the
3012<a href="#http2_event_request"><code>'request'</code></a> event. It may be used to access a request status, headers, and
3013data.</p>
3014<h5>Event: <code>'aborted'</code><span><a class="mark" href="#http2_event_aborted_1" id="http2_event_aborted_1">#</a></span></h5>
3015<div class="api_metadata">
3016<span>Added in: v8.4.0</span>
3017</div>
3018<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2ServerRequest</code> instance is
3019abnormally aborted in mid-communication.</p>
3020<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2ServerRequest</code> writable
3021side has not been ended.</p>
3022<h5>Event: <code>'close'</code><span><a class="mark" href="#http2_event_close_2" id="http2_event_close_2">#</a></span></h5>
3023<div class="api_metadata">
3024<span>Added in: v8.4.0</span>
3025</div>
3026<p>Indicates that the underlying <a href="#http2_class_http2stream"><code>Http2Stream</code></a> was closed.
3027Just like <code>'end'</code>, this event occurs only once per response.</p>
3028<h5><code>request.aborted</code><span><a class="mark" href="#http2_request_aborted" id="http2_request_aborted">#</a></span></h5>
3029<div class="api_metadata">
3030<span>Added in: v10.1.0</span>
3031</div>
3032<ul>
3033<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3034</ul>
3035<p>The <code>request.aborted</code> property will be <code>true</code> if the request has
3036been aborted.</p>
3037<h5><code>request.authority</code><span><a class="mark" href="#http2_request_authority" id="http2_request_authority">#</a></span></h5>
3038<div class="api_metadata">
3039<span>Added in: v8.4.0</span>
3040</div>
3041<ul>
3042<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3043</ul>
3044<p>The request authority pseudo header field. It can also be accessed via
3045<code>req.headers[':authority']</code>.</p>
3046<h5><code>request.complete</code><span><a class="mark" href="#http2_request_complete" id="http2_request_complete">#</a></span></h5>
3047<div class="api_metadata">
3048<span>Added in: v12.10.0</span>
3049</div>
3050<ul>
3051<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3052</ul>
3053<p>The <code>request.complete</code> property will be <code>true</code> if the request has
3054been completed, aborted, or destroyed.</p>
3055<h5><code>request.connection</code><span><a class="mark" href="#http2_request_connection" id="http2_request_connection">#</a></span></h5>
3056<div class="api_metadata">
3057<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
3058</div>
3059<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http2_request_socket"><code>request.socket</code></a>.</div><p></p>
3060<ul>
3061<li><a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
3062</ul>
3063<p>See <a href="#http2_request_socket"><code>request.socket</code></a>.</p>
3064<h5><code>request.destroy([error])</code><span><a class="mark" href="#http2_request_destroy_error" id="http2_request_destroy_error">#</a></span></h5>
3065<div class="api_metadata">
3066<span>Added in: v8.4.0</span>
3067</div>
3068<ul>
3069<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
3070</ul>
3071<p>Calls <code>destroy()</code> on the <a href="#http2_class_http2stream"><code>Http2Stream</code></a> that received
3072the <a href="#http2_class_http2_http2serverrequest"><code>Http2ServerRequest</code></a>. If <code>error</code> is provided, an <code>'error'</code> event
3073is emitted and <code>error</code> is passed as an argument to any listeners on the event.</p>
3074<p>It does nothing if the stream was already destroyed.</p>
3075<h5><code>request.headers</code><span><a class="mark" href="#http2_request_headers" id="http2_request_headers">#</a></span></h5>
3076<div class="api_metadata">
3077<span>Added in: v8.4.0</span>
3078</div>
3079<ul>
3080<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3081</ul>
3082<p>The request/response headers object.</p>
3083<p>Key-value pairs of header names and values. Header names are lower-cased.</p>
3084<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
3085<span class="hljs-comment">//</span>
3086<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
3087<span class="hljs-comment">//   host: '127.0.0.1:8000',</span>
3088<span class="hljs-comment">//   accept: '*/*' }</span>
3089<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">headers</span>);</code></pre>
3090<p>See <a href="#http2_headers_object">HTTP/2 Headers Object</a>.</p>
3091<p>In HTTP/2, the request path, host name, protocol, and method are represented as
3092special headers prefixed with the <code>:</code> character (e.g. <code>':path'</code>). These special
3093headers will be included in the <code>request.headers</code> object. Care must be taken not
3094to inadvertently modify these special headers or errors may occur. For instance,
3095removing all headers from the request will cause errors to occur:</p>
3096<pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<span class="hljs-property">headers</span>);
3097<span class="hljs-title function_">assert</span>(request.<span class="hljs-property">url</span>);   <span class="hljs-comment">// Fails because the :path header has been removed</span></code></pre>
3098<h5><code>request.httpVersion</code><span><a class="mark" href="#http2_request_httpversion" id="http2_request_httpversion">#</a></span></h5>
3099<div class="api_metadata">
3100<span>Added in: v8.4.0</span>
3101</div>
3102<ul>
3103<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3104</ul>
3105<p>In case of server request, the HTTP version sent by the client. In the case of
3106client response, the HTTP version of the connected-to server. Returns
3107<code>'2.0'</code>.</p>
3108<p>Also <code>message.httpVersionMajor</code> is the first integer and
3109<code>message.httpVersionMinor</code> is the second.</p>
3110<h5><code>request.method</code><span><a class="mark" href="#http2_request_method" id="http2_request_method">#</a></span></h5>
3111<div class="api_metadata">
3112<span>Added in: v8.4.0</span>
3113</div>
3114<ul>
3115<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3116</ul>
3117<p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p>
3118<h5><code>request.rawHeaders</code><span><a class="mark" href="#http2_request_rawheaders" id="http2_request_rawheaders">#</a></span></h5>
3119<div class="api_metadata">
3120<span>Added in: v8.4.0</span>
3121</div>
3122<ul>
3123<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
3124</ul>
3125<p>The raw request/response headers list exactly as they were received.</p>
3126<p>The keys and values are in the same list. It is <em>not</em> a
3127list of tuples. So, the even-numbered offsets are key values, and the
3128odd-numbered offsets are the associated values.</p>
3129<p>Header names are not lowercased, and duplicates are not merged.</p>
3130<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
3131<span class="hljs-comment">//</span>
3132<span class="hljs-comment">// [ 'user-agent',</span>
3133<span class="hljs-comment">//   'this is invalid because there can be only one',</span>
3134<span class="hljs-comment">//   'User-Agent',</span>
3135<span class="hljs-comment">//   'curl/7.22.0',</span>
3136<span class="hljs-comment">//   'Host',</span>
3137<span class="hljs-comment">//   '127.0.0.1:8000',</span>
3138<span class="hljs-comment">//   'ACCEPT',</span>
3139<span class="hljs-comment">//   '*/*' ]</span>
3140<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">rawHeaders</span>);</code></pre>
3141<h5><code>request.rawTrailers</code><span><a class="mark" href="#http2_request_rawtrailers" id="http2_request_rawtrailers">#</a></span></h5>
3142<div class="api_metadata">
3143<span>Added in: v8.4.0</span>
3144</div>
3145<ul>
3146<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
3147</ul>
3148<p>The raw request/response trailer keys and values exactly as they were
3149received. Only populated at the <code>'end'</code> event.</p>
3150<h5><code>request.scheme</code><span><a class="mark" href="#http2_request_scheme" id="http2_request_scheme">#</a></span></h5>
3151<div class="api_metadata">
3152<span>Added in: v8.4.0</span>
3153</div>
3154<ul>
3155<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3156</ul>
3157<p>The request scheme pseudo header field indicating the scheme
3158portion of the target URL.</p>
3159<h5><code>request.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2_request_settimeout_msecs_callback" id="http2_request_settimeout_msecs_callback">#</a></span></h5>
3160<div class="api_metadata">
3161<span>Added in: v8.4.0</span>
3162</div>
3163<ul>
3164<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
3165<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
3166<li>Returns: <a href="http2.html#http2_class_http2_http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
3167</ul>
3168<p>Sets the <a href="#http2_class_http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
3169provided, then it is added as a listener on the <code>'timeout'</code> event on
3170the response object.</p>
3171<p>If no <code>'timeout'</code> listener is added to the request, the response, or
3172the server, then <a href="#http2_class_http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
3173handler is assigned to the request, the response, or the server's <code>'timeout'</code>
3174events, timed out sockets must be handled explicitly.</p>
3175<h5><code>request.socket</code><span><a class="mark" href="#http2_request_socket" id="http2_request_socket">#</a></span></h5>
3176<div class="api_metadata">
3177<span>Added in: v8.4.0</span>
3178</div>
3179<ul>
3180<li><a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
3181</ul>
3182<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
3183applies getters, setters, and methods based on HTTP/2 logic.</p>
3184<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
3185set on <code>request.stream</code>.</p>
3186<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
3187<code>request.stream</code>.</p>
3188<p><code>setTimeout</code> method will be called on <code>request.stream.session</code>.</p>
3189<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
3190<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2_http2session_and_sockets"><code>Http2Session</code> and Sockets</a> for
3191more information.</p>
3192<p>All other interactions will be routed directly to the socket. With TLS support,
3193use <a href="tls.html#tls_tlssocket_getpeercertificate_detailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the client's
3194authentication details.</p>
3195<h5><code>request.stream</code><span><a class="mark" href="#http2_request_stream" id="http2_request_stream">#</a></span></h5>
3196<div class="api_metadata">
3197<span>Added in: v8.4.0</span>
3198</div>
3199<ul>
3200<li><a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a></li>
3201</ul>
3202<p>The <a href="#http2_class_http2stream"><code>Http2Stream</code></a> object backing the request.</p>
3203<h5><code>request.trailers</code><span><a class="mark" href="#http2_request_trailers" id="http2_request_trailers">#</a></span></h5>
3204<div class="api_metadata">
3205<span>Added in: v8.4.0</span>
3206</div>
3207<ul>
3208<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3209</ul>
3210<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p>
3211<h5><code>request.url</code><span><a class="mark" href="#http2_request_url" id="http2_request_url">#</a></span></h5>
3212<div class="api_metadata">
3213<span>Added in: v8.4.0</span>
3214</div>
3215<ul>
3216<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3217</ul>
3218<p>Request URL string. This contains only the URL that is present in the actual
3219HTTP request. If the request is:</p>
3220<pre><code class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?name=ryan</span> <span class="hljs-meta">HTTP/1.1</span>
3221<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code></pre>
3222<p>Then <code>request.url</code> will be:</p>
3223<!-- eslint-disable semi -->
3224<pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code></pre>
3225<p>To parse the url into its parts, <code>new URL()</code> can be used:</p>
3226<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node</span>
3227<span class="hljs-meta">> </span><span class="language-bash">new URL(<span class="hljs-string">'/status?name=ryan'</span>, <span class="hljs-string">'http://example.com'</span>)</span>
3228URL {
3229  href: 'http://example.com/status?name=ryan',
3230  origin: 'http://example.com',
3231  protocol: 'http:',
3232  username: '',
3233  password: '',
3234  host: 'example.com',
3235  hostname: 'example.com',
3236  port: '',
3237  pathname: '/status',
3238  search: '?name=ryan',
3239  searchParams: URLSearchParams { 'name' => 'ryan' },
3240  hash: ''
3241}</code></pre>
3242<h4>Class: <code>http2.Http2ServerResponse</code><span><a class="mark" href="#http2_class_http2_http2serverresponse" id="http2_class_http2_http2serverresponse">#</a></span></h4>
3243<div class="api_metadata">
3244<span>Added in: v8.4.0</span>
3245</div>
3246<ul>
3247<li>Extends: <a href="stream.html#stream_stream" class="type">&#x3C;Stream></a></li>
3248</ul>
3249<p>This object is created internally by an HTTP server, not by the user. It is
3250passed as the second parameter to the <a href="#http2_event_request"><code>'request'</code></a> event.</p>
3251<h5>Event: <code>'close'</code><span><a class="mark" href="#http2_event_close_3" id="http2_event_close_3">#</a></span></h5>
3252<div class="api_metadata">
3253<span>Added in: v8.4.0</span>
3254</div>
3255<p>Indicates that the underlying <a href="#http2_class_http2stream"><code>Http2Stream</code></a> was terminated before
3256<a href="#http2_response_end_data_encoding_callback"><code>response.end()</code></a> was called or able to flush.</p>
3257<h5>Event: <code>'finish'</code><span><a class="mark" href="#http2_event_finish" id="http2_event_finish">#</a></span></h5>
3258<div class="api_metadata">
3259<span>Added in: v8.4.0</span>
3260</div>
3261<p>Emitted when the response has been sent. More specifically, this event is
3262emitted when the last segment of the response headers and body have been
3263handed off to the HTTP/2 multiplexing for transmission over the network. It
3264does not imply that the client has received anything yet.</p>
3265<p>After this event, no more events will be emitted on the response object.</p>
3266<h5><code>response.addTrailers(headers)</code><span><a class="mark" href="#http2_response_addtrailers_headers" id="http2_response_addtrailers_headers">#</a></span></h5>
3267<div class="api_metadata">
3268<span>Added in: v8.4.0</span>
3269</div>
3270<ul>
3271<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3272</ul>
3273<p>This method adds HTTP trailing headers (a header but at the end of the
3274message) to the response.</p>
3275<p>Attempting to set a header field name or value that contains invalid characters
3276will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
3277<h5><code>response.connection</code><span><a class="mark" href="#http2_response_connection" id="http2_response_connection">#</a></span></h5>
3278<div class="api_metadata">
3279<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
3280</div>
3281<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http2_response_socket"><code>response.socket</code></a>.</div><p></p>
3282<ul>
3283<li><a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
3284</ul>
3285<p>See <a href="#http2_response_socket"><code>response.socket</code></a>.</p>
3286<h5><code>response.createPushResponse(headers, callback)</code><span><a class="mark" href="#http2_response_createpushresponse_headers_callback" id="http2_response_createpushresponse_headers_callback">#</a></span></h5>
3287<div class="api_metadata">
3288<span>Added in: v8.4.0</span>
3289</div>
3290<ul>
3291<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
3292<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called once <code>http2stream.pushStream()</code> is finished,
3293or either when the attempt to create the pushed <code>Http2Stream</code> has failed or
3294has been rejected, or the state of <code>Http2ServerRequest</code> is closed prior to
3295calling the <code>http2stream.pushStream()</code> method
3296<ul>
3297<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
3298<li><code>res</code> <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> The newly-created <code>Http2ServerResponse</code>
3299object</li>
3300</ul>
3301</li>
3302</ul>
3303<p>Call <a href="#http2_http2stream_pushstream_headers_options_callback"><code>http2stream.pushStream()</code></a> with the given headers, and wrap the
3304given <a href="#http2_class_http2stream"><code>Http2Stream</code></a> on a newly created <code>Http2ServerResponse</code> as the callback
3305parameter if successful. When <code>Http2ServerRequest</code> is closed, the callback is
3306called with an error <code>ERR_HTTP2_INVALID_STREAM</code>.</p>
3307<h5><code>response.end([data[, encoding]][, callback])</code><span><a class="mark" href="#http2_response_end_data_encoding_callback" id="http2_response_end_data_encoding_callback">#</a></span></h5>
3308<div class="api_metadata">
3309<details class="changelog"><summary>History</summary>
3310<table>
3311<tbody><tr><th>Version</th><th>Changes</th></tr>
3312<tr><td>v10.0.0</td>
3313<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr>
3314<tr><td>v8.4.0</td>
3315<td><p><span>Added in: v8.4.0</span></p></td></tr>
3316</tbody></table>
3317</details>
3318</div>
3319<ul>
3320<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
3321<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3322<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
3323<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
3324</ul>
3325<p>This method signals to the server that all of the response headers and body
3326have been sent; that server should consider this message complete.
3327The method, <code>response.end()</code>, MUST be called on each response.</p>
3328<p>If <code>data</code> is specified, it is equivalent to calling
3329<a href="http.html#http_response_write_chunk_encoding_callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p>
3330<p>If <code>callback</code> is specified, it will be called when the response stream
3331is finished.</p>
3332<h5><code>response.finished</code><span><a class="mark" href="#http2_response_finished" id="http2_response_finished">#</a></span></h5>
3333<div class="api_metadata">
3334<span>Added in: v8.4.0</span><span>Deprecated since: v13.4.0, v12.16.0</span>
3335</div>
3336<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a href="#http2_response_writableended"><code>response.writableEnded</code></a>.</div><p></p>
3337<ul>
3338<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3339</ul>
3340<p>Boolean value that indicates whether the response has completed. Starts
3341as <code>false</code>. After <a href="#http2_response_end_data_encoding_callback"><code>response.end()</code></a> executes, the value will be <code>true</code>.</p>
3342<h5><code>response.getHeader(name)</code><span><a class="mark" href="#http2_response_getheader_name" id="http2_response_getheader_name">#</a></span></h5>
3343<div class="api_metadata">
3344<span>Added in: v8.4.0</span>
3345</div>
3346<ul>
3347<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3348<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3349</ul>
3350<p>Reads out a header that has already been queued but not sent to the client.
3351The name is case-insensitive.</p>
3352<pre><code class="language-js"><span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHeader</span>(<span class="hljs-string">'content-type'</span>);</code></pre>
3353<h5><code>response.getHeaderNames()</code><span><a class="mark" href="#http2_response_getheadernames" id="http2_response_getheadernames">#</a></span></h5>
3354<div class="api_metadata">
3355<span>Added in: v8.4.0</span>
3356</div>
3357<ul>
3358<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
3359</ul>
3360<p>Returns an array containing the unique names of the current outgoing headers.
3361All header names are lowercase.</p>
3362<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
3363response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]);
3364
3365<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>();
3366<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
3367<h5><code>response.getHeaders()</code><span><a class="mark" href="#http2_response_getheaders" id="http2_response_getheaders">#</a></span></h5>
3368<div class="api_metadata">
3369<span>Added in: v8.4.0</span>
3370</div>
3371<ul>
3372<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3373</ul>
3374<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy
3375is used, array values may be mutated without additional calls to various
3376header-related http module methods. The keys of the returned object are the
3377header names and the values are the respective header values. All header names
3378are lowercase.</p>
3379<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em>
3380prototypically inherit from the JavaScript <code>Object</code>. This means that typical
3381<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others
3382are not defined and <em>will not work</em>.</p>
3383<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Foo'</span>, <span class="hljs-string">'bar'</span>);
3384response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'foo=bar'</span>, <span class="hljs-string">'bar=baz'</span>]);
3385
3386<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>();
3387<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code></pre>
3388<h5><code>response.hasHeader(name)</code><span><a class="mark" href="#http2_response_hasheader_name" id="http2_response_hasheader_name">#</a></span></h5>
3389<div class="api_metadata">
3390<span>Added in: v8.4.0</span>
3391</div>
3392<ul>
3393<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3394<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3395</ul>
3396<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the
3397outgoing headers. The header name matching is case-insensitive.</p>
3398<pre><code class="language-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title function_">hasHeader</span>(<span class="hljs-string">'content-type'</span>);</code></pre>
3399<h5><code>response.headersSent</code><span><a class="mark" href="#http2_response_headerssent" id="http2_response_headerssent">#</a></span></h5>
3400<div class="api_metadata">
3401<span>Added in: v8.4.0</span>
3402</div>
3403<ul>
3404<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3405</ul>
3406<p>True if headers were sent, false otherwise (read-only).</p>
3407<h5><code>response.removeHeader(name)</code><span><a class="mark" href="#http2_response_removeheader_name" id="http2_response_removeheader_name">#</a></span></h5>
3408<div class="api_metadata">
3409<span>Added in: v8.4.0</span>
3410</div>
3411<ul>
3412<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3413</ul>
3414<p>Removes a header that has been queued for implicit sending.</p>
3415<pre><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code></pre>
3416<h5><code>response.sendDate</code><span><a class="mark" href="#http2_response_senddate" id="http2_response_senddate">#</a></span></h5>
3417<div class="api_metadata">
3418<span>Added in: v8.4.0</span>
3419</div>
3420<ul>
3421<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3422</ul>
3423<p>When true, the Date header will be automatically generated and sent in
3424the response if it is not already present in the headers. Defaults to true.</p>
3425<p>This should only be disabled for testing; HTTP requires the Date header
3426in responses.</p>
3427<h5><code>response.setHeader(name, value)</code><span><a class="mark" href="#http2_response_setheader_name_value" id="http2_response_setheader_name_value">#</a></span></h5>
3428<div class="api_metadata">
3429<span>Added in: v8.4.0</span>
3430</div>
3431<ul>
3432<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3433<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
3434</ul>
3435<p>Sets a single header value for implicit headers. If this header already exists
3436in the to-be-sent headers, its value will be replaced. Use an array of strings
3437here to send multiple headers with the same name.</p>
3438<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>);</code></pre>
3439<p>or</p>
3440<pre><code class="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="hljs-string">'type=ninja'</span>, <span class="hljs-string">'language=javascript'</span>]);</code></pre>
3441<p>Attempting to set a header field name or value that contains invalid characters
3442will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
3443<p>When headers have been set with <a href="#http2_response_setheader_name_value"><code>response.setHeader()</code></a>, they will be merged
3444with any headers passed to <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a>, with the headers passed
3445to <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> given precedence.</p>
3446<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
3447<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
3448  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>);
3449  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
3450  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> });
3451  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
3452});</code></pre>
3453<h5><code>response.setTimeout(msecs[, callback])</code><span><a class="mark" href="#http2_response_settimeout_msecs_callback" id="http2_response_settimeout_msecs_callback">#</a></span></h5>
3454<div class="api_metadata">
3455<span>Added in: v8.4.0</span>
3456</div>
3457<ul>
3458<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
3459<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
3460<li>Returns: <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
3461</ul>
3462<p>Sets the <a href="#http2_class_http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
3463provided, then it is added as a listener on the <code>'timeout'</code> event on
3464the response object.</p>
3465<p>If no <code>'timeout'</code> listener is added to the request, the response, or
3466the server, then <a href="#http2_class_http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
3467handler is assigned to the request, the response, or the server's <code>'timeout'</code>
3468events, timed out sockets must be handled explicitly.</p>
3469<h5><code>response.socket</code><span><a class="mark" href="#http2_response_socket" id="http2_response_socket">#</a></span></h5>
3470<div class="api_metadata">
3471<span>Added in: v8.4.0</span>
3472</div>
3473<ul>
3474<li><a href="net.html#net_class_net_socket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
3475</ul>
3476<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
3477applies getters, setters, and methods based on HTTP/2 logic.</p>
3478<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
3479set on <code>response.stream</code>.</p>
3480<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
3481<code>response.stream</code>.</p>
3482<p><code>setTimeout</code> method will be called on <code>response.stream.session</code>.</p>
3483<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
3484<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2_http2session_and_sockets"><code>Http2Session</code> and Sockets</a> for
3485more information.</p>
3486<p>All other interactions will be routed directly to the socket.</p>
3487<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
3488<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
3489  <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
3490  <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>;
3491  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst">${ip}</span> and your source port is <span class="hljs-subst">${port}</span>.`</span>);
3492}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code></pre>
3493<h5><code>response.statusCode</code><span><a class="mark" href="#http2_response_statuscode" id="http2_response_statuscode">#</a></span></h5>
3494<div class="api_metadata">
3495<span>Added in: v8.4.0</span>
3496</div>
3497<ul>
3498<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
3499</ul>
3500<p>When using implicit headers (not calling <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> explicitly),
3501this property controls the status code that will be sent to the client when
3502the headers get flushed.</p>
3503<pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class="hljs-number">404</span>;</code></pre>
3504<p>After response header was sent to the client, this property indicates the
3505status code which was sent out.</p>
3506<h5><code>response.statusMessage</code><span><a class="mark" href="#http2_response_statusmessage" id="http2_response_statusmessage">#</a></span></h5>
3507<div class="api_metadata">
3508<span>Added in: v8.4.0</span>
3509</div>
3510<ul>
3511<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3512</ul>
3513<p>Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns
3514an empty string.</p>
3515<h5><code>response.stream</code><span><a class="mark" href="#http2_response_stream" id="http2_response_stream">#</a></span></h5>
3516<div class="api_metadata">
3517<span>Added in: v8.4.0</span>
3518</div>
3519<ul>
3520<li><a href="http2.html#http2_class_http2stream" class="type">&#x3C;Http2Stream></a></li>
3521</ul>
3522<p>The <a href="#http2_class_http2stream"><code>Http2Stream</code></a> object backing the response.</p>
3523<h5><code>response.writableEnded</code><span><a class="mark" href="#http2_response_writableended" id="http2_response_writableended">#</a></span></h5>
3524<div class="api_metadata">
3525<span>Added in: v12.9.0</span>
3526</div>
3527<ul>
3528<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3529</ul>
3530<p>Is <code>true</code> after <a href="#http2_response_end_data_encoding_callback"><code>response.end()</code></a> has been called. This property
3531does not indicate whether the data has been flushed, for this use
3532<a href="stream.html#stream_writable_writablefinished"><code>writable.writableFinished</code></a> instead.</p>
3533<h5><code>response.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#http2_response_write_chunk_encoding_callback" id="http2_response_write_chunk_encoding_callback">#</a></span></h5>
3534<div class="api_metadata">
3535<span>Added in: v8.4.0</span>
3536</div>
3537<ul>
3538<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
3539<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3540<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
3541<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
3542</ul>
3543<p>If this method is called and <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> has not been called,
3544it will switch to implicit header mode and flush the implicit headers.</p>
3545<p>This sends a chunk of the response body. This method may
3546be called multiple times to provide successive parts of the body.</p>
3547<p>In the <code>http</code> module, the response body is omitted when the
3548request is a HEAD request. Similarly, the <code>204</code> and <code>304</code> responses
3549<em>must not</em> include a message body.</p>
3550<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string,
3551the second parameter specifies how to encode it into a byte stream.
3552By default the <code>encoding</code> is <code>'utf8'</code>. <code>callback</code> will be called when this chunk
3553of data is flushed.</p>
3554<p>This is the raw HTTP body and has nothing to do with higher-level multi-part
3555body encodings that may be used.</p>
3556<p>The first time <a href="#http2_response_write_chunk_encoding_callback"><code>response.write()</code></a> is called, it will send the buffered
3557header information and the first chunk of the body to the client. The second
3558time <a href="#http2_response_write_chunk_encoding_callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed,
3559and sends the new data separately. That is, the response is buffered up to the
3560first chunk of the body.</p>
3561<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
3562buffer. Returns <code>false</code> if all or part of the data was queued in user memory.
3563<code>'drain'</code> will be emitted when the buffer is free again.</p>
3564<h5><code>response.writeContinue()</code><span><a class="mark" href="#http2_response_writecontinue" id="http2_response_writecontinue">#</a></span></h5>
3565<div class="api_metadata">
3566<span>Added in: v8.4.0</span>
3567</div>
3568<p>Sends a status <code>100 Continue</code> to the client, indicating that the request body
3569should be sent. See the <a href="#http2_event_checkcontinue"><code>'checkContinue'</code></a> event on <code>Http2Server</code> and
3570<code>Http2SecureServer</code>.</p>
3571<h5><code>response.writeHead(statusCode[, statusMessage][, headers])</code><span><a class="mark" href="#http2_response_writehead_statuscode_statusmessage_headers" id="http2_response_writehead_statuscode_statusmessage_headers">#</a></span></h5>
3572<div class="api_metadata">
3573<details class="changelog"><summary>History</summary>
3574<table>
3575<tbody><tr><th>Version</th><th>Changes</th></tr>
3576<tr><td>v11.10.0, v10.17.0</td>
3577<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr>
3578<tr><td>v8.4.0</td>
3579<td><p><span>Added in: v8.4.0</span></p></td></tr>
3580</tbody></table>
3581</details>
3582</div>
3583<ul>
3584<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
3585<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3586<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3587<li>Returns: <a href="http2.html#http2_class_http2_http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
3588</ul>
3589<p>Sends a response header to the request. The status code is a 3-digit HTTP
3590status code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers.</p>
3591<p>Returns a reference to the <code>Http2ServerResponse</code>, so that calls can be chained.</p>
3592<p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</code> may be
3593passed as the second argument. However, because the <code>statusMessage</code> has no
3594meaning within HTTP/2, the argument will have no effect and a process warning
3595will be emitted.</p>
3596<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>;
3597response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
3598  <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body),
3599  <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
3600});</code></pre>
3601<p><code>Content-Length</code> is given in bytes not characters. The
3602<code>Buffer.byteLength()</code> API may be used to determine the number of bytes in a
3603given encoding. On outbound messages, Node.js does not check if Content-Length
3604and the length of the body being transmitted are equal or not. However, when
3605receiving messages, Node.js will automatically reject messages when the
3606<code>Content-Length</code> does not match the actual payload size.</p>
3607<p>This method may be called at most one time on a message before
3608<a href="#http2_response_end_data_encoding_callback"><code>response.end()</code></a> is called.</p>
3609<p>If <a href="#http2_response_write_chunk_encoding_callback"><code>response.write()</code></a> or <a href="#http2_response_end_data_encoding_callback"><code>response.end()</code></a> are called before calling
3610this, the implicit/mutable headers will be calculated and call this function.</p>
3611<p>When headers have been set with <a href="#http2_response_setheader_name_value"><code>response.setHeader()</code></a>, they will be merged
3612with any headers passed to <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a>, with the headers passed
3613to <a href="#http2_response_writehead_statuscode_statusmessage_headers"><code>response.writeHead()</code></a> given precedence.</p>
3614<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
3615<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> {
3616  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html; charset=utf-8'</span>);
3617  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
3618  res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> });
3619  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
3620});</code></pre>
3621<p>Attempting to set a header field name or value that contains invalid characters
3622will result in a <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> being thrown.</p>
3623</section><section><h3>Collecting HTTP/2 performance metrics<span><a class="mark" href="#http2_collecting_http_2_performance_metrics" id="http2_collecting_http_2_performance_metrics">#</a></span></h3>
3624<p>The <a href="perf_hooks.html">Performance Observer</a> API can be used to collect basic performance
3625metrics for each <code>Http2Session</code> and <code>Http2Stream</code> instance.</p>
3626<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
3627
3628<span class="hljs-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_">PerformanceObserver</span>(<span class="hljs-function">(<span class="hljs-params">items</span>) =></span> {
3629  <span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>];
3630  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span class="hljs-property">entryType</span>);  <span class="hljs-comment">// prints 'http2'</span>
3631  <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Session'</span>) {
3632    <span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
3633  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Stream'</span>) {
3634    <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
3635  }
3636});
3637obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'http2'</span>] });</code></pre>
3638<p>The <code>entryType</code> property of the <code>PerformanceEntry</code> will be equal to <code>'http2'</code>.</p>
3639<p>The <code>name</code> property of the <code>PerformanceEntry</code> will be equal to either
3640<code>'Http2Stream'</code> or <code>'Http2Session'</code>.</p>
3641<p>If <code>name</code> is equal to <code>Http2Stream</code>, the <code>PerformanceEntry</code> will contain the
3642following additional properties:</p>
3643<ul>
3644<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>DATA</code> frame bytes received for this
3645<code>Http2Stream</code>.</li>
3646<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>DATA</code> frame bytes sent for this
3647<code>Http2Stream</code>.</li>
3648<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The identifier of the associated <code>Http2Stream</code></li>
3649<li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between the
3650<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first <code>DATA</code> frame.</li>
3651<li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between
3652the <code>PerformanceEntry</code> <code>startTime</code> and sending of the first <code>DATA</code> frame.</li>
3653<li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between the
3654<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first header.</li>
3655</ul>
3656<p>If <code>name</code> is equal to <code>Http2Session</code>, the <code>PerformanceEntry</code> will contain the
3657following additional properties:</p>
3658<ul>
3659<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes received for this <code>Http2Session</code>.</li>
3660<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes sent for this <code>Http2Session</code>.</li>
3661<li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of HTTP/2 frames received by the
3662<code>Http2Session</code>.</li>
3663<li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of HTTP/2 frames sent by the <code>Http2Session</code>.</li>
3664<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum number of streams concurrently
3665open during the lifetime of the <code>Http2Session</code>.</li>
3666<li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed since the transmission
3667of a <code>PING</code> frame and the reception of its acknowledgment. Only present if
3668a <code>PING</code> frame has been sent on the <code>Http2Session</code>.</li>
3669<li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The average duration (in milliseconds) for
3670all <code>Http2Stream</code> instances.</li>
3671<li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>Http2Stream</code> instances processed by
3672the <code>Http2Session</code>.</li>
3673<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'server'</code> or <code>'client'</code> to identify the type of
3674<code>Http2Session</code>.</li>
3675</ul></section>
3676        <!-- API END -->
3677      </div>
3678    </div>
3679  </div>
3680  <script>
3681    'use strict';
3682    {
3683      const kCustomPreference = 'customDarkTheme';
3684      const userSettings = sessionStorage.getItem(kCustomPreference);
3685      const themeToggleButton = document.getElementById('theme-toggle-btn');
3686      if (userSettings === null && window.matchMedia) {
3687        const mq = window.matchMedia('(prefers-color-scheme: dark)');
3688        if ('onchange' in mq) {
3689          function mqChangeListener(e) {
3690            document.body.classList.toggle('dark-mode', e.matches);
3691          }
3692          mq.addEventListener('change', mqChangeListener);
3693          if (themeToggleButton) {
3694            themeToggleButton.addEventListener('click', function() {
3695              mq.removeEventListener('change', mqChangeListener);
3696            }, { once: true });
3697          }
3698        }
3699        if (mq.matches) {
3700          document.body.classList.add('dark-mode');
3701        }
3702      } else if (userSettings === 'true') {
3703        document.body.classList.add('dark-mode');
3704      }
3705      if (themeToggleButton) {
3706        themeToggleButton.hidden = false;
3707        themeToggleButton.addEventListener('click', function() {
3708          sessionStorage.setItem(
3709            kCustomPreference,
3710            document.body.classList.toggle('dark-mode')
3711          );
3712        });
3713      }
3714    }
3715  </script>
3716</body>
3717</html>
3718