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