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