xref: /third_party/node/doc/api/http2.html (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>HTTP/2 | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/http2.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-http2">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="http2" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#http2">HTTP/2</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
1261cb0ef41Sopenharmony_ci<li><a href="#core-api">Core API</a>
1271cb0ef41Sopenharmony_ci<ul>
1281cb0ef41Sopenharmony_ci<li><a href="#server-side-example">Server-side example</a></li>
1291cb0ef41Sopenharmony_ci<li><a href="#client-side-example">Client-side example</a></li>
1301cb0ef41Sopenharmony_ci<li><a href="#class-http2session">Class: <code>Http2Session</code></a>
1311cb0ef41Sopenharmony_ci<ul>
1321cb0ef41Sopenharmony_ci<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li>
1331cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
1341cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
1351cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
1361cb0ef41Sopenharmony_ci<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li>
1371cb0ef41Sopenharmony_ci<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li>
1381cb0ef41Sopenharmony_ci<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li>
1391cb0ef41Sopenharmony_ci<li><a href="#event-ping">Event: <code>'ping'</code></a></li>
1401cb0ef41Sopenharmony_ci<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li>
1411cb0ef41Sopenharmony_ci<li><a href="#event-stream">Event: <code>'stream'</code></a></li>
1421cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
1431cb0ef41Sopenharmony_ci<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li>
1441cb0ef41Sopenharmony_ci<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li>
1451cb0ef41Sopenharmony_ci<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li>
1461cb0ef41Sopenharmony_ci<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li>
1471cb0ef41Sopenharmony_ci<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li>
1481cb0ef41Sopenharmony_ci<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li>
1491cb0ef41Sopenharmony_ci<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li>
1511cb0ef41Sopenharmony_ci<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li>
1521cb0ef41Sopenharmony_ci<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li>
1541cb0ef41Sopenharmony_ci<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li>
1551cb0ef41Sopenharmony_ci<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li>
1561cb0ef41Sopenharmony_ci<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li>
1571cb0ef41Sopenharmony_ci<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li>
1581cb0ef41Sopenharmony_ci<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li>
1591cb0ef41Sopenharmony_ci<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li>
1601cb0ef41Sopenharmony_ci<li><a href="#http2sessionstate"><code>http2session.state</code></a></li>
1611cb0ef41Sopenharmony_ci<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li>
1621cb0ef41Sopenharmony_ci<li><a href="#http2sessiontype"><code>http2session.type</code></a></li>
1631cb0ef41Sopenharmony_ci<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li>
1641cb0ef41Sopenharmony_ci</ul>
1651cb0ef41Sopenharmony_ci</li>
1661cb0ef41Sopenharmony_ci<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a>
1671cb0ef41Sopenharmony_ci<ul>
1681cb0ef41Sopenharmony_ci<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li>
1691cb0ef41Sopenharmony_ci<li><a href="#specifying-alternative-services">Specifying alternative services</a></li>
1701cb0ef41Sopenharmony_ci<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li>
1711cb0ef41Sopenharmony_ci</ul>
1721cb0ef41Sopenharmony_ci</li>
1731cb0ef41Sopenharmony_ci<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a>
1741cb0ef41Sopenharmony_ci<ul>
1751cb0ef41Sopenharmony_ci<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li>
1761cb0ef41Sopenharmony_ci<li><a href="#event-origin">Event: <code>'origin'</code></a></li>
1771cb0ef41Sopenharmony_ci<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li>
1781cb0ef41Sopenharmony_ci</ul>
1791cb0ef41Sopenharmony_ci</li>
1801cb0ef41Sopenharmony_ci<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a>
1811cb0ef41Sopenharmony_ci<ul>
1821cb0ef41Sopenharmony_ci<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a>
1831cb0ef41Sopenharmony_ci<ul>
1841cb0ef41Sopenharmony_ci<li><a href="#creation">Creation</a></li>
1851cb0ef41Sopenharmony_ci<li><a href="#destruction">Destruction</a></li>
1861cb0ef41Sopenharmony_ci</ul>
1871cb0ef41Sopenharmony_ci</li>
1881cb0ef41Sopenharmony_ci<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li>
1891cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
1901cb0ef41Sopenharmony_ci<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
1911cb0ef41Sopenharmony_ci<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li>
1921cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
1931cb0ef41Sopenharmony_ci<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li>
1941cb0ef41Sopenharmony_ci<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li>
1951cb0ef41Sopenharmony_ci<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li>
1961cb0ef41Sopenharmony_ci<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li>
1971cb0ef41Sopenharmony_ci<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li>
1981cb0ef41Sopenharmony_ci<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li>
1991cb0ef41Sopenharmony_ci<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li>
2001cb0ef41Sopenharmony_ci<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li>
2011cb0ef41Sopenharmony_ci<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li>
2021cb0ef41Sopenharmony_ci<li><a href="#http2streamid"><code>http2stream.id</code></a></li>
2031cb0ef41Sopenharmony_ci<li><a href="#http2streampending"><code>http2stream.pending</code></a></li>
2041cb0ef41Sopenharmony_ci<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li>
2051cb0ef41Sopenharmony_ci<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li>
2061cb0ef41Sopenharmony_ci<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li>
2071cb0ef41Sopenharmony_ci<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li>
2081cb0ef41Sopenharmony_ci<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li>
2091cb0ef41Sopenharmony_ci<li><a href="#http2streamsession"><code>http2stream.session</code></a></li>
2101cb0ef41Sopenharmony_ci<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li>
2111cb0ef41Sopenharmony_ci<li><a href="#http2streamstate"><code>http2stream.state</code></a></li>
2121cb0ef41Sopenharmony_ci<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li>
2131cb0ef41Sopenharmony_ci</ul>
2141cb0ef41Sopenharmony_ci</li>
2151cb0ef41Sopenharmony_ci<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a>
2161cb0ef41Sopenharmony_ci<ul>
2171cb0ef41Sopenharmony_ci<li><a href="#event-continue">Event: <code>'continue'</code></a></li>
2181cb0ef41Sopenharmony_ci<li><a href="#event-headers">Event: <code>'headers'</code></a></li>
2191cb0ef41Sopenharmony_ci<li><a href="#event-push">Event: <code>'push'</code></a></li>
2201cb0ef41Sopenharmony_ci<li><a href="#event-response">Event: <code>'response'</code></a></li>
2211cb0ef41Sopenharmony_ci</ul>
2221cb0ef41Sopenharmony_ci</li>
2231cb0ef41Sopenharmony_ci<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a>
2241cb0ef41Sopenharmony_ci<ul>
2251cb0ef41Sopenharmony_ci<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li>
2261cb0ef41Sopenharmony_ci<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li>
2271cb0ef41Sopenharmony_ci<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li>
2281cb0ef41Sopenharmony_ci<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li>
2291cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li>
2301cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li>
2311cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li>
2321cb0ef41Sopenharmony_ci</ul>
2331cb0ef41Sopenharmony_ci</li>
2341cb0ef41Sopenharmony_ci<li><a href="#class-http2server">Class: <code>Http2Server</code></a>
2351cb0ef41Sopenharmony_ci<ul>
2361cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
2371cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
2381cb0ef41Sopenharmony_ci<li><a href="#event-request">Event: <code>'request'</code></a></li>
2391cb0ef41Sopenharmony_ci<li><a href="#event-session">Event: <code>'session'</code></a></li>
2401cb0ef41Sopenharmony_ci<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li>
2411cb0ef41Sopenharmony_ci<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li>
2421cb0ef41Sopenharmony_ci<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li>
2431cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
2441cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
2451cb0ef41Sopenharmony_ci<li><a href="#servertimeout"><code>server.timeout</code></a></li>
2461cb0ef41Sopenharmony_ci<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li>
2471cb0ef41Sopenharmony_ci</ul>
2481cb0ef41Sopenharmony_ci</li>
2491cb0ef41Sopenharmony_ci<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a>
2501cb0ef41Sopenharmony_ci<ul>
2511cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li>
2521cb0ef41Sopenharmony_ci<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li>
2531cb0ef41Sopenharmony_ci<li><a href="#event-request_1">Event: <code>'request'</code></a></li>
2541cb0ef41Sopenharmony_ci<li><a href="#event-session_1">Event: <code>'session'</code></a></li>
2551cb0ef41Sopenharmony_ci<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li>
2561cb0ef41Sopenharmony_ci<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li>
2571cb0ef41Sopenharmony_ci<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li>
2581cb0ef41Sopenharmony_ci<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li>
2591cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li>
2601cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li>
2611cb0ef41Sopenharmony_ci<li><a href="#servertimeout_1"><code>server.timeout</code></a></li>
2621cb0ef41Sopenharmony_ci<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li>
2631cb0ef41Sopenharmony_ci</ul>
2641cb0ef41Sopenharmony_ci</li>
2651cb0ef41Sopenharmony_ci<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li>
2661cb0ef41Sopenharmony_ci<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li>
2671cb0ef41Sopenharmony_ci<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li>
2681cb0ef41Sopenharmony_ci<li><a href="#http2constants"><code>http2.constants</code></a>
2691cb0ef41Sopenharmony_ci<ul>
2701cb0ef41Sopenharmony_ci<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li>
2711cb0ef41Sopenharmony_ci</ul>
2721cb0ef41Sopenharmony_ci</li>
2731cb0ef41Sopenharmony_ci<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li>
2741cb0ef41Sopenharmony_ci<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li>
2751cb0ef41Sopenharmony_ci<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li>
2761cb0ef41Sopenharmony_ci<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li>
2771cb0ef41Sopenharmony_ci<li><a href="#headers-object">Headers object</a>
2781cb0ef41Sopenharmony_ci<ul>
2791cb0ef41Sopenharmony_ci<li><a href="#sensitive-headers">Sensitive headers</a></li>
2801cb0ef41Sopenharmony_ci</ul>
2811cb0ef41Sopenharmony_ci</li>
2821cb0ef41Sopenharmony_ci<li><a href="#settings-object">Settings object</a></li>
2831cb0ef41Sopenharmony_ci<li><a href="#error-handling">Error handling</a></li>
2841cb0ef41Sopenharmony_ci<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li>
2851cb0ef41Sopenharmony_ci<li><a href="#push-streams-on-the-client">Push streams on the client</a></li>
2861cb0ef41Sopenharmony_ci<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li>
2871cb0ef41Sopenharmony_ci<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li>
2881cb0ef41Sopenharmony_ci</ul>
2891cb0ef41Sopenharmony_ci</li>
2901cb0ef41Sopenharmony_ci<li><a href="#compatibility-api">Compatibility API</a>
2911cb0ef41Sopenharmony_ci<ul>
2921cb0ef41Sopenharmony_ci<li><a href="#alpn-negotiation">ALPN negotiation</a></li>
2931cb0ef41Sopenharmony_ci<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a>
2941cb0ef41Sopenharmony_ci<ul>
2951cb0ef41Sopenharmony_ci<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li>
2961cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
2971cb0ef41Sopenharmony_ci<li><a href="#requestaborted"><code>request.aborted</code></a></li>
2981cb0ef41Sopenharmony_ci<li><a href="#requestauthority"><code>request.authority</code></a></li>
2991cb0ef41Sopenharmony_ci<li><a href="#requestcomplete"><code>request.complete</code></a></li>
3001cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
3011cb0ef41Sopenharmony_ci<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li>
3021cb0ef41Sopenharmony_ci<li><a href="#requestheaders"><code>request.headers</code></a></li>
3031cb0ef41Sopenharmony_ci<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li>
3041cb0ef41Sopenharmony_ci<li><a href="#requestmethod"><code>request.method</code></a></li>
3051cb0ef41Sopenharmony_ci<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li>
3061cb0ef41Sopenharmony_ci<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li>
3071cb0ef41Sopenharmony_ci<li><a href="#requestscheme"><code>request.scheme</code></a></li>
3081cb0ef41Sopenharmony_ci<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li>
3091cb0ef41Sopenharmony_ci<li><a href="#requestsocket"><code>request.socket</code></a></li>
3101cb0ef41Sopenharmony_ci<li><a href="#requeststream"><code>request.stream</code></a></li>
3111cb0ef41Sopenharmony_ci<li><a href="#requesttrailers"><code>request.trailers</code></a></li>
3121cb0ef41Sopenharmony_ci<li><a href="#requesturl"><code>request.url</code></a></li>
3131cb0ef41Sopenharmony_ci</ul>
3141cb0ef41Sopenharmony_ci</li>
3151cb0ef41Sopenharmony_ci<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a>
3161cb0ef41Sopenharmony_ci<ul>
3171cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
3181cb0ef41Sopenharmony_ci<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
3191cb0ef41Sopenharmony_ci<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
3201cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
3211cb0ef41Sopenharmony_ci<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li>
3221cb0ef41Sopenharmony_ci<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
3231cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li>
3241cb0ef41Sopenharmony_ci<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li>
3251cb0ef41Sopenharmony_ci<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li>
3261cb0ef41Sopenharmony_ci<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li>
3271cb0ef41Sopenharmony_ci<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li>
3281cb0ef41Sopenharmony_ci<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li>
3291cb0ef41Sopenharmony_ci<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li>
3301cb0ef41Sopenharmony_ci<li><a href="#responsereq"><code>response.req</code></a></li>
3311cb0ef41Sopenharmony_ci<li><a href="#responsesenddate"><code>response.sendDate</code></a></li>
3321cb0ef41Sopenharmony_ci<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li>
3331cb0ef41Sopenharmony_ci<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
3341cb0ef41Sopenharmony_ci<li><a href="#responsesocket"><code>response.socket</code></a></li>
3351cb0ef41Sopenharmony_ci<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li>
3361cb0ef41Sopenharmony_ci<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li>
3371cb0ef41Sopenharmony_ci<li><a href="#responsestream"><code>response.stream</code></a></li>
3381cb0ef41Sopenharmony_ci<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
3391cb0ef41Sopenharmony_ci<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
3401cb0ef41Sopenharmony_ci<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li>
3411cb0ef41Sopenharmony_ci<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li>
3421cb0ef41Sopenharmony_ci<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
3431cb0ef41Sopenharmony_ci</ul>
3441cb0ef41Sopenharmony_ci</li>
3451cb0ef41Sopenharmony_ci</ul>
3461cb0ef41Sopenharmony_ci</li>
3471cb0ef41Sopenharmony_ci<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li>
3481cb0ef41Sopenharmony_ci<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li>
3491cb0ef41Sopenharmony_ci</ul>
3501cb0ef41Sopenharmony_ci</li>
3511cb0ef41Sopenharmony_ci</ul></div></div>
3521cb0ef41Sopenharmony_ci    </li>
3531cb0ef41Sopenharmony_ci  
3541cb0ef41Sopenharmony_ci            
3551cb0ef41Sopenharmony_ci    <li class="picker-header">
3561cb0ef41Sopenharmony_ci      <a href="#">
3571cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
3581cb0ef41Sopenharmony_ci        Index
3591cb0ef41Sopenharmony_ci      </a>
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_ci      <div class="picker"><ul>
3621cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
3631cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci      <li>
3661cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
3671cb0ef41Sopenharmony_ci      </li>
3681cb0ef41Sopenharmony_ci    </ul>
3691cb0ef41Sopenharmony_ci  
3701cb0ef41Sopenharmony_ci<hr class="line">
3711cb0ef41Sopenharmony_ci<ul>
3721cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
3731cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
3741cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
3751cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
3761cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
3771cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
3781cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
3791cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
3801cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
3811cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
3821cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
3831cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
3841cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
3851cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
3861cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
3871cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
3881cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
3891cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
3901cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
3911cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
3921cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
3931cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
3941cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
3951cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
3961cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
3971cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
3981cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
3991cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
4001cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
4011cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
4021cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
4031cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
4041cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
4051cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
4061cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
4071cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
4081cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
4091cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
4101cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
4111cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
4121cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li>
4131cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
4141cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
4151cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
4161cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
4171cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
4181cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
4191cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
4201cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
4211cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
4221cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
4231cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
4241cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
4251cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
4261cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
4271cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
4281cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
4291cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
4301cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
4311cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
4321cb0ef41Sopenharmony_ci</ul>
4331cb0ef41Sopenharmony_ci<hr class="line">
4341cb0ef41Sopenharmony_ci<ul>
4351cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
4361cb0ef41Sopenharmony_ci</ul></div>
4371cb0ef41Sopenharmony_ci    </li>
4381cb0ef41Sopenharmony_ci  
4391cb0ef41Sopenharmony_ci            
4401cb0ef41Sopenharmony_ci    <li class="picker-header">
4411cb0ef41Sopenharmony_ci      <a href="#">
4421cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
4431cb0ef41Sopenharmony_ci        Other versions
4441cb0ef41Sopenharmony_ci      </a>
4451cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/http2.html">21.x</a></li>
4461cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/http2.html">20.x <b>LTS</b></a></li>
4471cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/http2.html">19.x</a></li>
4481cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/http2.html">18.x <b>LTS</b></a></li>
4491cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/http2.html">17.x</a></li>
4501cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/http2.html">16.x</a></li>
4511cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/http2.html">15.x</a></li>
4521cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/http2.html">14.x</a></li>
4531cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/http2.html">13.x</a></li>
4541cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/http2.html">12.x</a></li>
4551cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/http2.html">11.x</a></li>
4561cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/http2.html">10.x</a></li>
4571cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/http2.html">9.x</a></li>
4581cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/http2.html">8.x</a></li></ol></div>
4591cb0ef41Sopenharmony_ci    </li>
4601cb0ef41Sopenharmony_ci  
4611cb0ef41Sopenharmony_ci            <li class="picker-header">
4621cb0ef41Sopenharmony_ci              <a href="#">
4631cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
4641cb0ef41Sopenharmony_ci                Options
4651cb0ef41Sopenharmony_ci              </a>
4661cb0ef41Sopenharmony_ci        
4671cb0ef41Sopenharmony_ci              <div class="picker">
4681cb0ef41Sopenharmony_ci                <ul>
4691cb0ef41Sopenharmony_ci                  <li>
4701cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
4711cb0ef41Sopenharmony_ci                  </li>
4721cb0ef41Sopenharmony_ci                  <li>
4731cb0ef41Sopenharmony_ci                    <a href="http2.json">View as JSON</a>
4741cb0ef41Sopenharmony_ci                  </li>
4751cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/http2.md">Edit on GitHub</a></li>    
4761cb0ef41Sopenharmony_ci                </ul>
4771cb0ef41Sopenharmony_ci              </div>
4781cb0ef41Sopenharmony_ci            </li>
4791cb0ef41Sopenharmony_ci          </ul>
4801cb0ef41Sopenharmony_ci        </div>
4811cb0ef41Sopenharmony_ci        <hr>
4821cb0ef41Sopenharmony_ci      </header>
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
4851cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#http2">HTTP/2</a></span>
4861cb0ef41Sopenharmony_ci<ul>
4871cb0ef41Sopenharmony_ci<li><a href="#determining-if-crypto-support-is-unavailable">Determining if crypto support is unavailable</a></li>
4881cb0ef41Sopenharmony_ci<li><a href="#core-api">Core API</a>
4891cb0ef41Sopenharmony_ci<ul>
4901cb0ef41Sopenharmony_ci<li><a href="#server-side-example">Server-side example</a></li>
4911cb0ef41Sopenharmony_ci<li><a href="#client-side-example">Client-side example</a></li>
4921cb0ef41Sopenharmony_ci<li><a href="#class-http2session">Class: <code>Http2Session</code></a>
4931cb0ef41Sopenharmony_ci<ul>
4941cb0ef41Sopenharmony_ci<li><a href="#http2session-and-sockets"><code>Http2Session</code> and sockets</a></li>
4951cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li>
4961cb0ef41Sopenharmony_ci<li><a href="#event-connect">Event: <code>'connect'</code></a></li>
4971cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li>
4981cb0ef41Sopenharmony_ci<li><a href="#event-frameerror">Event: <code>'frameError'</code></a></li>
4991cb0ef41Sopenharmony_ci<li><a href="#event-goaway">Event: <code>'goaway'</code></a></li>
5001cb0ef41Sopenharmony_ci<li><a href="#event-localsettings">Event: <code>'localSettings'</code></a></li>
5011cb0ef41Sopenharmony_ci<li><a href="#event-ping">Event: <code>'ping'</code></a></li>
5021cb0ef41Sopenharmony_ci<li><a href="#event-remotesettings">Event: <code>'remoteSettings'</code></a></li>
5031cb0ef41Sopenharmony_ci<li><a href="#event-stream">Event: <code>'stream'</code></a></li>
5041cb0ef41Sopenharmony_ci<li><a href="#event-timeout">Event: <code>'timeout'</code></a></li>
5051cb0ef41Sopenharmony_ci<li><a href="#http2sessionalpnprotocol"><code>http2session.alpnProtocol</code></a></li>
5061cb0ef41Sopenharmony_ci<li><a href="#http2sessionclosecallback"><code>http2session.close([callback])</code></a></li>
5071cb0ef41Sopenharmony_ci<li><a href="#http2sessionclosed"><code>http2session.closed</code></a></li>
5081cb0ef41Sopenharmony_ci<li><a href="#http2sessionconnecting"><code>http2session.connecting</code></a></li>
5091cb0ef41Sopenharmony_ci<li><a href="#http2sessiondestroyerror-code"><code>http2session.destroy([error][, code])</code></a></li>
5101cb0ef41Sopenharmony_ci<li><a href="#http2sessiondestroyed"><code>http2session.destroyed</code></a></li>
5111cb0ef41Sopenharmony_ci<li><a href="#http2sessionencrypted"><code>http2session.encrypted</code></a></li>
5121cb0ef41Sopenharmony_ci<li><a href="#http2sessiongoawaycode-laststreamid-opaquedata"><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code></a></li>
5131cb0ef41Sopenharmony_ci<li><a href="#http2sessionlocalsettings"><code>http2session.localSettings</code></a></li>
5141cb0ef41Sopenharmony_ci<li><a href="#http2sessionoriginset"><code>http2session.originSet</code></a></li>
5151cb0ef41Sopenharmony_ci<li><a href="#http2sessionpendingsettingsack"><code>http2session.pendingSettingsAck</code></a></li>
5161cb0ef41Sopenharmony_ci<li><a href="#http2sessionpingpayload-callback"><code>http2session.ping([payload, ]callback)</code></a></li>
5171cb0ef41Sopenharmony_ci<li><a href="#http2sessionref"><code>http2session.ref()</code></a></li>
5181cb0ef41Sopenharmony_ci<li><a href="#http2sessionremotesettings"><code>http2session.remoteSettings</code></a></li>
5191cb0ef41Sopenharmony_ci<li><a href="#http2sessionsetlocalwindowsizewindowsize"><code>http2session.setLocalWindowSize(windowSize)</code></a></li>
5201cb0ef41Sopenharmony_ci<li><a href="#http2sessionsettimeoutmsecs-callback"><code>http2session.setTimeout(msecs, callback)</code></a></li>
5211cb0ef41Sopenharmony_ci<li><a href="#http2sessionsocket"><code>http2session.socket</code></a></li>
5221cb0ef41Sopenharmony_ci<li><a href="#http2sessionstate"><code>http2session.state</code></a></li>
5231cb0ef41Sopenharmony_ci<li><a href="#http2sessionsettingssettings-callback"><code>http2session.settings([settings][, callback])</code></a></li>
5241cb0ef41Sopenharmony_ci<li><a href="#http2sessiontype"><code>http2session.type</code></a></li>
5251cb0ef41Sopenharmony_ci<li><a href="#http2sessionunref"><code>http2session.unref()</code></a></li>
5261cb0ef41Sopenharmony_ci</ul>
5271cb0ef41Sopenharmony_ci</li>
5281cb0ef41Sopenharmony_ci<li><a href="#class-serverhttp2session">Class: <code>ServerHttp2Session</code></a>
5291cb0ef41Sopenharmony_ci<ul>
5301cb0ef41Sopenharmony_ci<li><a href="#serverhttp2sessionaltsvcalt-originorstream"><code>serverhttp2session.altsvc(alt, originOrStream)</code></a></li>
5311cb0ef41Sopenharmony_ci<li><a href="#specifying-alternative-services">Specifying alternative services</a></li>
5321cb0ef41Sopenharmony_ci<li><a href="#serverhttp2sessionoriginorigins"><code>serverhttp2session.origin(...origins)</code></a></li>
5331cb0ef41Sopenharmony_ci</ul>
5341cb0ef41Sopenharmony_ci</li>
5351cb0ef41Sopenharmony_ci<li><a href="#class-clienthttp2session">Class: <code>ClientHttp2Session</code></a>
5361cb0ef41Sopenharmony_ci<ul>
5371cb0ef41Sopenharmony_ci<li><a href="#event-altsvc">Event: <code>'altsvc'</code></a></li>
5381cb0ef41Sopenharmony_ci<li><a href="#event-origin">Event: <code>'origin'</code></a></li>
5391cb0ef41Sopenharmony_ci<li><a href="#clienthttp2sessionrequestheaders-options"><code>clienthttp2session.request(headers[, options])</code></a></li>
5401cb0ef41Sopenharmony_ci</ul>
5411cb0ef41Sopenharmony_ci</li>
5421cb0ef41Sopenharmony_ci<li><a href="#class-http2stream">Class: <code>Http2Stream</code></a>
5431cb0ef41Sopenharmony_ci<ul>
5441cb0ef41Sopenharmony_ci<li><a href="#http2stream-lifecycle"><code>Http2Stream</code> Lifecycle</a>
5451cb0ef41Sopenharmony_ci<ul>
5461cb0ef41Sopenharmony_ci<li><a href="#creation">Creation</a></li>
5471cb0ef41Sopenharmony_ci<li><a href="#destruction">Destruction</a></li>
5481cb0ef41Sopenharmony_ci</ul>
5491cb0ef41Sopenharmony_ci</li>
5501cb0ef41Sopenharmony_ci<li><a href="#event-aborted">Event: <code>'aborted'</code></a></li>
5511cb0ef41Sopenharmony_ci<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
5521cb0ef41Sopenharmony_ci<li><a href="#event-error_1">Event: <code>'error'</code></a></li>
5531cb0ef41Sopenharmony_ci<li><a href="#event-frameerror_1">Event: <code>'frameError'</code></a></li>
5541cb0ef41Sopenharmony_ci<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
5551cb0ef41Sopenharmony_ci<li><a href="#event-timeout_1">Event: <code>'timeout'</code></a></li>
5561cb0ef41Sopenharmony_ci<li><a href="#event-trailers">Event: <code>'trailers'</code></a></li>
5571cb0ef41Sopenharmony_ci<li><a href="#event-wanttrailers">Event: <code>'wantTrailers'</code></a></li>
5581cb0ef41Sopenharmony_ci<li><a href="#http2streamaborted"><code>http2stream.aborted</code></a></li>
5591cb0ef41Sopenharmony_ci<li><a href="#http2streambuffersize"><code>http2stream.bufferSize</code></a></li>
5601cb0ef41Sopenharmony_ci<li><a href="#http2streamclosecode-callback"><code>http2stream.close(code[, callback])</code></a></li>
5611cb0ef41Sopenharmony_ci<li><a href="#http2streamclosed"><code>http2stream.closed</code></a></li>
5621cb0ef41Sopenharmony_ci<li><a href="#http2streamdestroyed"><code>http2stream.destroyed</code></a></li>
5631cb0ef41Sopenharmony_ci<li><a href="#http2streamendafterheaders"><code>http2stream.endAfterHeaders</code></a></li>
5641cb0ef41Sopenharmony_ci<li><a href="#http2streamid"><code>http2stream.id</code></a></li>
5651cb0ef41Sopenharmony_ci<li><a href="#http2streampending"><code>http2stream.pending</code></a></li>
5661cb0ef41Sopenharmony_ci<li><a href="#http2streampriorityoptions"><code>http2stream.priority(options)</code></a></li>
5671cb0ef41Sopenharmony_ci<li><a href="#http2streamrstcode"><code>http2stream.rstCode</code></a></li>
5681cb0ef41Sopenharmony_ci<li><a href="#http2streamsentheaders"><code>http2stream.sentHeaders</code></a></li>
5691cb0ef41Sopenharmony_ci<li><a href="#http2streamsentinfoheaders"><code>http2stream.sentInfoHeaders</code></a></li>
5701cb0ef41Sopenharmony_ci<li><a href="#http2streamsenttrailers"><code>http2stream.sentTrailers</code></a></li>
5711cb0ef41Sopenharmony_ci<li><a href="#http2streamsession"><code>http2stream.session</code></a></li>
5721cb0ef41Sopenharmony_ci<li><a href="#http2streamsettimeoutmsecs-callback"><code>http2stream.setTimeout(msecs, callback)</code></a></li>
5731cb0ef41Sopenharmony_ci<li><a href="#http2streamstate"><code>http2stream.state</code></a></li>
5741cb0ef41Sopenharmony_ci<li><a href="#http2streamsendtrailersheaders"><code>http2stream.sendTrailers(headers)</code></a></li>
5751cb0ef41Sopenharmony_ci</ul>
5761cb0ef41Sopenharmony_ci</li>
5771cb0ef41Sopenharmony_ci<li><a href="#class-clienthttp2stream">Class: <code>ClientHttp2Stream</code></a>
5781cb0ef41Sopenharmony_ci<ul>
5791cb0ef41Sopenharmony_ci<li><a href="#event-continue">Event: <code>'continue'</code></a></li>
5801cb0ef41Sopenharmony_ci<li><a href="#event-headers">Event: <code>'headers'</code></a></li>
5811cb0ef41Sopenharmony_ci<li><a href="#event-push">Event: <code>'push'</code></a></li>
5821cb0ef41Sopenharmony_ci<li><a href="#event-response">Event: <code>'response'</code></a></li>
5831cb0ef41Sopenharmony_ci</ul>
5841cb0ef41Sopenharmony_ci</li>
5851cb0ef41Sopenharmony_ci<li><a href="#class-serverhttp2stream">Class: <code>ServerHttp2Stream</code></a>
5861cb0ef41Sopenharmony_ci<ul>
5871cb0ef41Sopenharmony_ci<li><a href="#http2streamadditionalheadersheaders"><code>http2stream.additionalHeaders(headers)</code></a></li>
5881cb0ef41Sopenharmony_ci<li><a href="#http2streamheaderssent"><code>http2stream.headersSent</code></a></li>
5891cb0ef41Sopenharmony_ci<li><a href="#http2streampushallowed"><code>http2stream.pushAllowed</code></a></li>
5901cb0ef41Sopenharmony_ci<li><a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream(headers[, options], callback)</code></a></li>
5911cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondheaders-options"><code>http2stream.respond([headers[, options]])</code></a></li>
5921cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondwithfdfd-headers-options"><code>http2stream.respondWithFD(fd[, headers[, options]])</code></a></li>
5931cb0ef41Sopenharmony_ci<li><a href="#http2streamrespondwithfilepath-headers-options"><code>http2stream.respondWithFile(path[, headers[, options]])</code></a></li>
5941cb0ef41Sopenharmony_ci</ul>
5951cb0ef41Sopenharmony_ci</li>
5961cb0ef41Sopenharmony_ci<li><a href="#class-http2server">Class: <code>Http2Server</code></a>
5971cb0ef41Sopenharmony_ci<ul>
5981cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue">Event: <code>'checkContinue'</code></a></li>
5991cb0ef41Sopenharmony_ci<li><a href="#event-connection">Event: <code>'connection'</code></a></li>
6001cb0ef41Sopenharmony_ci<li><a href="#event-request">Event: <code>'request'</code></a></li>
6011cb0ef41Sopenharmony_ci<li><a href="#event-session">Event: <code>'session'</code></a></li>
6021cb0ef41Sopenharmony_ci<li><a href="#event-sessionerror">Event: <code>'sessionError'</code></a></li>
6031cb0ef41Sopenharmony_ci<li><a href="#event-stream_1">Event: <code>'stream'</code></a></li>
6041cb0ef41Sopenharmony_ci<li><a href="#event-timeout_2">Event: <code>'timeout'</code></a></li>
6051cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback"><code>server.close([callback])</code></a></li>
6061cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback"><code>server.setTimeout([msecs][, callback])</code></a></li>
6071cb0ef41Sopenharmony_ci<li><a href="#servertimeout"><code>server.timeout</code></a></li>
6081cb0ef41Sopenharmony_ci<li><a href="#serverupdatesettingssettings"><code>server.updateSettings([settings])</code></a></li>
6091cb0ef41Sopenharmony_ci</ul>
6101cb0ef41Sopenharmony_ci</li>
6111cb0ef41Sopenharmony_ci<li><a href="#class-http2secureserver">Class: <code>Http2SecureServer</code></a>
6121cb0ef41Sopenharmony_ci<ul>
6131cb0ef41Sopenharmony_ci<li><a href="#event-checkcontinue_1">Event: <code>'checkContinue'</code></a></li>
6141cb0ef41Sopenharmony_ci<li><a href="#event-connection_1">Event: <code>'connection'</code></a></li>
6151cb0ef41Sopenharmony_ci<li><a href="#event-request_1">Event: <code>'request'</code></a></li>
6161cb0ef41Sopenharmony_ci<li><a href="#event-session_1">Event: <code>'session'</code></a></li>
6171cb0ef41Sopenharmony_ci<li><a href="#event-sessionerror_1">Event: <code>'sessionError'</code></a></li>
6181cb0ef41Sopenharmony_ci<li><a href="#event-stream_2">Event: <code>'stream'</code></a></li>
6191cb0ef41Sopenharmony_ci<li><a href="#event-timeout_3">Event: <code>'timeout'</code></a></li>
6201cb0ef41Sopenharmony_ci<li><a href="#event-unknownprotocol">Event: <code>'unknownProtocol'</code></a></li>
6211cb0ef41Sopenharmony_ci<li><a href="#serverclosecallback_1"><code>server.close([callback])</code></a></li>
6221cb0ef41Sopenharmony_ci<li><a href="#serversettimeoutmsecs-callback_1"><code>server.setTimeout([msecs][, callback])</code></a></li>
6231cb0ef41Sopenharmony_ci<li><a href="#servertimeout_1"><code>server.timeout</code></a></li>
6241cb0ef41Sopenharmony_ci<li><a href="#serverupdatesettingssettings_1"><code>server.updateSettings([settings])</code></a></li>
6251cb0ef41Sopenharmony_ci</ul>
6261cb0ef41Sopenharmony_ci</li>
6271cb0ef41Sopenharmony_ci<li><a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer([options][, onRequestHandler])</code></a></li>
6281cb0ef41Sopenharmony_ci<li><a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer(options[, onRequestHandler])</code></a></li>
6291cb0ef41Sopenharmony_ci<li><a href="#http2connectauthority-options-listener"><code>http2.connect(authority[, options][, listener])</code></a></li>
6301cb0ef41Sopenharmony_ci<li><a href="#http2constants"><code>http2.constants</code></a>
6311cb0ef41Sopenharmony_ci<ul>
6321cb0ef41Sopenharmony_ci<li><a href="#error-codes-for-rst_stream-and-goaway">Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code></a></li>
6331cb0ef41Sopenharmony_ci</ul>
6341cb0ef41Sopenharmony_ci</li>
6351cb0ef41Sopenharmony_ci<li><a href="#http2getdefaultsettings"><code>http2.getDefaultSettings()</code></a></li>
6361cb0ef41Sopenharmony_ci<li><a href="#http2getpackedsettingssettings"><code>http2.getPackedSettings([settings])</code></a></li>
6371cb0ef41Sopenharmony_ci<li><a href="#http2getunpackedsettingsbuf"><code>http2.getUnpackedSettings(buf)</code></a></li>
6381cb0ef41Sopenharmony_ci<li><a href="#http2sensitiveheaders"><code>http2.sensitiveHeaders</code></a></li>
6391cb0ef41Sopenharmony_ci<li><a href="#headers-object">Headers object</a>
6401cb0ef41Sopenharmony_ci<ul>
6411cb0ef41Sopenharmony_ci<li><a href="#sensitive-headers">Sensitive headers</a></li>
6421cb0ef41Sopenharmony_ci</ul>
6431cb0ef41Sopenharmony_ci</li>
6441cb0ef41Sopenharmony_ci<li><a href="#settings-object">Settings object</a></li>
6451cb0ef41Sopenharmony_ci<li><a href="#error-handling">Error handling</a></li>
6461cb0ef41Sopenharmony_ci<li><a href="#invalid-character-handling-in-header-names-and-values">Invalid character handling in header names and values</a></li>
6471cb0ef41Sopenharmony_ci<li><a href="#push-streams-on-the-client">Push streams on the client</a></li>
6481cb0ef41Sopenharmony_ci<li><a href="#supporting-the-connect-method">Supporting the <code>CONNECT</code> method</a></li>
6491cb0ef41Sopenharmony_ci<li><a href="#the-extended-connect-protocol">The extended <code>CONNECT</code> protocol</a></li>
6501cb0ef41Sopenharmony_ci</ul>
6511cb0ef41Sopenharmony_ci</li>
6521cb0ef41Sopenharmony_ci<li><a href="#compatibility-api">Compatibility API</a>
6531cb0ef41Sopenharmony_ci<ul>
6541cb0ef41Sopenharmony_ci<li><a href="#alpn-negotiation">ALPN negotiation</a></li>
6551cb0ef41Sopenharmony_ci<li><a href="#class-http2http2serverrequest">Class: <code>http2.Http2ServerRequest</code></a>
6561cb0ef41Sopenharmony_ci<ul>
6571cb0ef41Sopenharmony_ci<li><a href="#event-aborted_1">Event: <code>'aborted'</code></a></li>
6581cb0ef41Sopenharmony_ci<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
6591cb0ef41Sopenharmony_ci<li><a href="#requestaborted"><code>request.aborted</code></a></li>
6601cb0ef41Sopenharmony_ci<li><a href="#requestauthority"><code>request.authority</code></a></li>
6611cb0ef41Sopenharmony_ci<li><a href="#requestcomplete"><code>request.complete</code></a></li>
6621cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#requestconnection"><code>request.connection</code></a></span></li>
6631cb0ef41Sopenharmony_ci<li><a href="#requestdestroyerror"><code>request.destroy([error])</code></a></li>
6641cb0ef41Sopenharmony_ci<li><a href="#requestheaders"><code>request.headers</code></a></li>
6651cb0ef41Sopenharmony_ci<li><a href="#requesthttpversion"><code>request.httpVersion</code></a></li>
6661cb0ef41Sopenharmony_ci<li><a href="#requestmethod"><code>request.method</code></a></li>
6671cb0ef41Sopenharmony_ci<li><a href="#requestrawheaders"><code>request.rawHeaders</code></a></li>
6681cb0ef41Sopenharmony_ci<li><a href="#requestrawtrailers"><code>request.rawTrailers</code></a></li>
6691cb0ef41Sopenharmony_ci<li><a href="#requestscheme"><code>request.scheme</code></a></li>
6701cb0ef41Sopenharmony_ci<li><a href="#requestsettimeoutmsecs-callback"><code>request.setTimeout(msecs, callback)</code></a></li>
6711cb0ef41Sopenharmony_ci<li><a href="#requestsocket"><code>request.socket</code></a></li>
6721cb0ef41Sopenharmony_ci<li><a href="#requeststream"><code>request.stream</code></a></li>
6731cb0ef41Sopenharmony_ci<li><a href="#requesttrailers"><code>request.trailers</code></a></li>
6741cb0ef41Sopenharmony_ci<li><a href="#requesturl"><code>request.url</code></a></li>
6751cb0ef41Sopenharmony_ci</ul>
6761cb0ef41Sopenharmony_ci</li>
6771cb0ef41Sopenharmony_ci<li><a href="#class-http2http2serverresponse">Class: <code>http2.Http2ServerResponse</code></a>
6781cb0ef41Sopenharmony_ci<ul>
6791cb0ef41Sopenharmony_ci<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
6801cb0ef41Sopenharmony_ci<li><a href="#event-finish">Event: <code>'finish'</code></a></li>
6811cb0ef41Sopenharmony_ci<li><a href="#responseaddtrailersheaders"><code>response.addTrailers(headers)</code></a></li>
6821cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responseconnection"><code>response.connection</code></a></span></li>
6831cb0ef41Sopenharmony_ci<li><a href="#responsecreatepushresponseheaders-callback"><code>response.createPushResponse(headers, callback)</code></a></li>
6841cb0ef41Sopenharmony_ci<li><a href="#responseenddata-encoding-callback"><code>response.end([data[, encoding]][, callback])</code></a></li>
6851cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#responsefinished"><code>response.finished</code></a></span></li>
6861cb0ef41Sopenharmony_ci<li><a href="#responsegetheadername"><code>response.getHeader(name)</code></a></li>
6871cb0ef41Sopenharmony_ci<li><a href="#responsegetheadernames"><code>response.getHeaderNames()</code></a></li>
6881cb0ef41Sopenharmony_ci<li><a href="#responsegetheaders"><code>response.getHeaders()</code></a></li>
6891cb0ef41Sopenharmony_ci<li><a href="#responsehasheadername"><code>response.hasHeader(name)</code></a></li>
6901cb0ef41Sopenharmony_ci<li><a href="#responseheaderssent"><code>response.headersSent</code></a></li>
6911cb0ef41Sopenharmony_ci<li><a href="#responseremoveheadername"><code>response.removeHeader(name)</code></a></li>
6921cb0ef41Sopenharmony_ci<li><a href="#responsereq"><code>response.req</code></a></li>
6931cb0ef41Sopenharmony_ci<li><a href="#responsesenddate"><code>response.sendDate</code></a></li>
6941cb0ef41Sopenharmony_ci<li><a href="#responsesetheadername-value"><code>response.setHeader(name, value)</code></a></li>
6951cb0ef41Sopenharmony_ci<li><a href="#responsesettimeoutmsecs-callback"><code>response.setTimeout(msecs[, callback])</code></a></li>
6961cb0ef41Sopenharmony_ci<li><a href="#responsesocket"><code>response.socket</code></a></li>
6971cb0ef41Sopenharmony_ci<li><a href="#responsestatuscode"><code>response.statusCode</code></a></li>
6981cb0ef41Sopenharmony_ci<li><a href="#responsestatusmessage"><code>response.statusMessage</code></a></li>
6991cb0ef41Sopenharmony_ci<li><a href="#responsestream"><code>response.stream</code></a></li>
7001cb0ef41Sopenharmony_ci<li><a href="#responsewritableended"><code>response.writableEnded</code></a></li>
7011cb0ef41Sopenharmony_ci<li><a href="#responsewritechunk-encoding-callback"><code>response.write(chunk[, encoding][, callback])</code></a></li>
7021cb0ef41Sopenharmony_ci<li><a href="#responsewritecontinue"><code>response.writeContinue()</code></a></li>
7031cb0ef41Sopenharmony_ci<li><a href="#responsewriteearlyhintshints"><code>response.writeEarlyHints(hints)</code></a></li>
7041cb0ef41Sopenharmony_ci<li><a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead(statusCode[, statusMessage][, headers])</code></a></li>
7051cb0ef41Sopenharmony_ci</ul>
7061cb0ef41Sopenharmony_ci</li>
7071cb0ef41Sopenharmony_ci</ul>
7081cb0ef41Sopenharmony_ci</li>
7091cb0ef41Sopenharmony_ci<li><a href="#collecting-http2-performance-metrics">Collecting HTTP/2 performance metrics</a></li>
7101cb0ef41Sopenharmony_ci<li><a href="#note-on-authority-and-host">Note on <code>:authority</code> and <code>host</code></a></li>
7111cb0ef41Sopenharmony_ci</ul>
7121cb0ef41Sopenharmony_ci</li>
7131cb0ef41Sopenharmony_ci</ul></details>
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ci      <div id="apicontent">
7161cb0ef41Sopenharmony_ci        <h2>HTTP/2<span><a class="mark" href="#http2" id="http2">#</a></span><a aria-hidden="true" class="legacy" id="http2_http_2"></a></h2>
7171cb0ef41Sopenharmony_ci<div class="api_metadata">
7181cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
7191cb0ef41Sopenharmony_ci<table>
7201cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
7211cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td>
7221cb0ef41Sopenharmony_ci<td><p>Requests with the <code>host</code> header (with or without <code>:authority</code>) can now be sent/received.</p></td></tr>
7231cb0ef41Sopenharmony_ci<tr><td>v15.3.0, v14.17.0</td>
7241cb0ef41Sopenharmony_ci<td><p>It is possible to abort a request with an AbortSignal.</p></td></tr>
7251cb0ef41Sopenharmony_ci<tr><td>v10.10.0</td>
7261cb0ef41Sopenharmony_ci<td><p>HTTP/2 is now Stable. Previously, it had been Experimental.</p></td></tr>
7271cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
7281cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
7291cb0ef41Sopenharmony_ci</tbody></table>
7301cb0ef41Sopenharmony_ci</details>
7311cb0ef41Sopenharmony_ci</div>
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
7341cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/http2.js">lib/http2.js</a></p>
7351cb0ef41Sopenharmony_ci<p>The <code>node:http2</code> module provides an implementation of the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> protocol.
7361cb0ef41Sopenharmony_ciIt can be accessed using:</p>
7371cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);</code> <button class="copy-button">copy</button></pre>
7381cb0ef41Sopenharmony_ci<section><h3>Determining if crypto support is unavailable<span><a class="mark" href="#determining-if-crypto-support-is-unavailable" id="determining-if-crypto-support-is-unavailable">#</a></span><a aria-hidden="true" class="legacy" id="http2_determining_if_crypto_support_is_unavailable"></a></h3>
7391cb0ef41Sopenharmony_ci<p>It is possible for Node.js to be built without including support for the
7401cb0ef41Sopenharmony_ci<code>node:crypto</code> module. In such cases, attempting to <code>import</code> from <code>node:http2</code> or
7411cb0ef41Sopenharmony_cicalling <code>require('node:http2')</code> will result in an error being thrown.</p>
7421cb0ef41Sopenharmony_ci<p>When using CommonJS, the error thrown can be caught using try/catch:</p>
7431cb0ef41Sopenharmony_ci<pre><code class="language-js cjs"><span class="hljs-keyword">let</span> http2;
7441cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
7451cb0ef41Sopenharmony_ci  http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
7461cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
7471cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>);
7481cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
7491cb0ef41Sopenharmony_ci<p>When using the lexical ESM <code>import</code> keyword, the error can only be
7501cb0ef41Sopenharmony_cicaught if a handler for <code>process.on('uncaughtException')</code> is registered
7511cb0ef41Sopenharmony_ci<em>before</em> any attempt to load the module is made (using, for instance,
7521cb0ef41Sopenharmony_cia preload module).</p>
7531cb0ef41Sopenharmony_ci<p>When using ESM, if there is a chance that the code may be run on a build
7541cb0ef41Sopenharmony_ciof Node.js where crypto support is not enabled, consider using the
7551cb0ef41Sopenharmony_ci<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"><code>import()</code></a> function instead of the lexical <code>import</code> keyword:</p>
7561cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">let</span> http2;
7571cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> {
7581cb0ef41Sopenharmony_ci  http2 = <span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>(<span class="hljs-string">'node:http2'</span>);
7591cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) {
7601cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'http2 support is disabled!'</span>);
7611cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
7621cb0ef41Sopenharmony_ci</section><section><h3>Core API<span><a class="mark" href="#core-api" id="core-api">#</a></span><a aria-hidden="true" class="legacy" id="http2_core_api"></a></h3>
7631cb0ef41Sopenharmony_ci<p>The Core API provides a low-level interface designed specifically around
7641cb0ef41Sopenharmony_cisupport for HTTP/2 protocol features. It is specifically <em>not</em> designed for
7651cb0ef41Sopenharmony_cicompatibility with the existing <a href="http.html">HTTP/1</a> module API. However,
7661cb0ef41Sopenharmony_cithe <a href="#compatibility-api">Compatibility API</a> is.</p>
7671cb0ef41Sopenharmony_ci<p>The <code>http2</code> Core API is much more symmetric between client and server than the
7681cb0ef41Sopenharmony_ci<code>http</code> API. For instance, most events, like <code>'error'</code>, <code>'connect'</code> and
7691cb0ef41Sopenharmony_ci<code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
7701cb0ef41Sopenharmony_ci<h4>Server-side example<span><a class="mark" href="#server-side-example" id="server-side-example">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_side_example"></a></h4>
7711cb0ef41Sopenharmony_ci<p>The following illustrates a simple HTTP/2 server using the Core API.
7721cb0ef41Sopenharmony_ciSince there are no browsers known that support
7731cb0ef41Sopenharmony_ci<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
7741cb0ef41Sopenharmony_ci<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
7751cb0ef41Sopenharmony_ciwith browser clients.</p>
7761cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
7771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
7781cb0ef41Sopenharmony_ci
7791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>({
7801cb0ef41Sopenharmony_ci  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-privkey.pem'</span>),
7811cb0ef41Sopenharmony_ci  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
7821cb0ef41Sopenharmony_ci});
7831cb0ef41Sopenharmony_ciserver.<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));
7841cb0ef41Sopenharmony_ci
7851cb0ef41Sopenharmony_ciserver.<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> {
7861cb0ef41Sopenharmony_ci  <span class="hljs-comment">// stream is a Duplex</span>
7871cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
7881cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
7891cb0ef41Sopenharmony_ci    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
7901cb0ef41Sopenharmony_ci  });
7911cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
7921cb0ef41Sopenharmony_ci});
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code> <button class="copy-button">copy</button></pre>
7951cb0ef41Sopenharmony_ci<p>To generate the certificate and key for this example, run:</p>
7961cb0ef41Sopenharmony_ci<pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span class="hljs-string">'/CN=localhost'</span> \
7971cb0ef41Sopenharmony_ci  -keyout localhost-privkey.pem -out localhost-cert.pem</code> <button class="copy-button">copy</button></pre>
7981cb0ef41Sopenharmony_ci<h4>Client-side example<span><a class="mark" href="#client-side-example" id="client-side-example">#</a></span><a aria-hidden="true" class="legacy" id="http2_client_side_example"></a></h4>
7991cb0ef41Sopenharmony_ci<p>The following illustrates an HTTP/2 client:</p>
8001cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
8011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
8021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:8443'</span>, {
8031cb0ef41Sopenharmony_ci  <span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'localhost-cert.pem'</span>),
8041cb0ef41Sopenharmony_ci});
8051cb0ef41Sopenharmony_ciclient.<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));
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ci<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> });
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_cireq.<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> {
8101cb0ef41Sopenharmony_ci  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) {
8111cb0ef41Sopenharmony_ci    <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>);
8121cb0ef41Sopenharmony_ci  }
8131cb0ef41Sopenharmony_ci});
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
8161cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
8171cb0ef41Sopenharmony_cireq.<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; });
8181cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
8191cb0ef41Sopenharmony_ci  <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>);
8201cb0ef41Sopenharmony_ci  client.<span class="hljs-title function_">close</span>();
8211cb0ef41Sopenharmony_ci});
8221cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>();</code> <button class="copy-button">copy</button></pre>
8231cb0ef41Sopenharmony_ci<h4>Class: <code>Http2Session</code><span><a class="mark" href="#class-http2session" id="class-http2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2session"></a></h4>
8241cb0ef41Sopenharmony_ci<div class="api_metadata">
8251cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
8261cb0ef41Sopenharmony_ci</div>
8271cb0ef41Sopenharmony_ci<ul>
8281cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
8291cb0ef41Sopenharmony_ci</ul>
8301cb0ef41Sopenharmony_ci<p>Instances of the <code>http2.Http2Session</code> class represent an active communications
8311cb0ef41Sopenharmony_cisession between an HTTP/2 client and server. Instances of this class are <em>not</em>
8321cb0ef41Sopenharmony_ciintended to be constructed directly by user code.</p>
8331cb0ef41Sopenharmony_ci<p>Each <code>Http2Session</code> instance will exhibit slightly different behaviors
8341cb0ef41Sopenharmony_cidepending on whether it is operating as a server or a client. The
8351cb0ef41Sopenharmony_ci<code>http2session.type</code> property can be used to determine the mode in which an
8361cb0ef41Sopenharmony_ci<code>Http2Session</code> is operating. On the server side, user code should rarely
8371cb0ef41Sopenharmony_cihave occasion to work with the <code>Http2Session</code> object directly, with most
8381cb0ef41Sopenharmony_ciactions typically taken through interactions with either the <code>Http2Server</code> or
8391cb0ef41Sopenharmony_ci<code>Http2Stream</code> objects.</p>
8401cb0ef41Sopenharmony_ci<p>User code will not create <code>Http2Session</code> instances directly. Server-side
8411cb0ef41Sopenharmony_ci<code>Http2Session</code> instances are created by the <code>Http2Server</code> instance when a
8421cb0ef41Sopenharmony_cinew HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
8431cb0ef41Sopenharmony_cicreated using the <code>http2.connect()</code> method.</p>
8441cb0ef41Sopenharmony_ci<h5><code>Http2Session</code> and sockets<span><a class="mark" href="#http2session-and-sockets" id="http2session-and-sockets">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_and_sockets"></a></h5>
8451cb0ef41Sopenharmony_ci<p>Every <code>Http2Session</code> instance is associated with exactly one <a href="net.html#class-netsocket"><code>net.Socket</code></a> or
8461cb0ef41Sopenharmony_ci<a href="tls.html#class-tlstlssocket"><code>tls.TLSSocket</code></a> when it is created. When either the <code>Socket</code> or the
8471cb0ef41Sopenharmony_ci<code>Http2Session</code> are destroyed, both will be destroyed.</p>
8481cb0ef41Sopenharmony_ci<p>Because of the specific serialization and processing requirements imposed
8491cb0ef41Sopenharmony_ciby the HTTP/2 protocol, it is not recommended for user code to read data from
8501cb0ef41Sopenharmony_cior write data to a <code>Socket</code> instance bound to a <code>Http2Session</code>. Doing so can
8511cb0ef41Sopenharmony_ciput the HTTP/2 session into an indeterminate state causing the session and
8521cb0ef41Sopenharmony_cithe socket to become unusable.</p>
8531cb0ef41Sopenharmony_ci<p>Once a <code>Socket</code> has been bound to an <code>Http2Session</code>, user code should rely
8541cb0ef41Sopenharmony_cisolely on the API of the <code>Http2Session</code>.</p>
8551cb0ef41Sopenharmony_ci<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close"></a></h5>
8561cb0ef41Sopenharmony_ci<div class="api_metadata">
8571cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
8581cb0ef41Sopenharmony_ci</div>
8591cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted once the <code>Http2Session</code> has been destroyed. Its
8601cb0ef41Sopenharmony_cilistener does not expect any arguments.</p>
8611cb0ef41Sopenharmony_ci<h5>Event: <code>'connect'</code><span><a class="mark" href="#event-connect" id="event-connect">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connect"></a></h5>
8621cb0ef41Sopenharmony_ci<div class="api_metadata">
8631cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
8641cb0ef41Sopenharmony_ci</div>
8651cb0ef41Sopenharmony_ci<ul>
8661cb0ef41Sopenharmony_ci<li><code>session</code> <a href="http2.html#class-http2session" class="type">&#x3C;Http2Session></a></li>
8671cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a></li>
8681cb0ef41Sopenharmony_ci</ul>
8691cb0ef41Sopenharmony_ci<p>The <code>'connect'</code> event is emitted once the <code>Http2Session</code> has been successfully
8701cb0ef41Sopenharmony_ciconnected to the remote peer and communication may begin.</p>
8711cb0ef41Sopenharmony_ci<p>User code will typically not listen for this event directly.</p>
8721cb0ef41Sopenharmony_ci<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_error"></a></h5>
8731cb0ef41Sopenharmony_ci<div class="api_metadata">
8741cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
8751cb0ef41Sopenharmony_ci</div>
8761cb0ef41Sopenharmony_ci<ul>
8771cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
8781cb0ef41Sopenharmony_ci</ul>
8791cb0ef41Sopenharmony_ci<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
8801cb0ef41Sopenharmony_cian <code>Http2Session</code>.</p>
8811cb0ef41Sopenharmony_ci<h5>Event: <code>'frameError'</code><span><a class="mark" href="#event-frameerror" id="event-frameerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_frameerror"></a></h5>
8821cb0ef41Sopenharmony_ci<div class="api_metadata">
8831cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
8841cb0ef41Sopenharmony_ci</div>
8851cb0ef41Sopenharmony_ci<ul>
8861cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The frame type.</li>
8871cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The error code.</li>
8881cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The stream id (or <code>0</code> if the frame isn't associated with a
8891cb0ef41Sopenharmony_cistream).</li>
8901cb0ef41Sopenharmony_ci</ul>
8911cb0ef41Sopenharmony_ci<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
8921cb0ef41Sopenharmony_cisend a frame on the session. If the frame that could not be sent is associated
8931cb0ef41Sopenharmony_ciwith a specific <code>Http2Stream</code>, an attempt to emit a <code>'frameError'</code> event on the
8941cb0ef41Sopenharmony_ci<code>Http2Stream</code> is made.</p>
8951cb0ef41Sopenharmony_ci<p>If the <code>'frameError'</code> event is associated with a stream, the stream will be
8961cb0ef41Sopenharmony_ciclosed and destroyed immediately following the <code>'frameError'</code> event. If the
8971cb0ef41Sopenharmony_cievent is not associated with a stream, the <code>Http2Session</code> will be shut down
8981cb0ef41Sopenharmony_ciimmediately following the <code>'frameError'</code> event.</p>
8991cb0ef41Sopenharmony_ci<h5>Event: <code>'goaway'</code><span><a class="mark" href="#event-goaway" id="event-goaway">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_goaway"></a></h5>
9001cb0ef41Sopenharmony_ci<div class="api_metadata">
9011cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
9021cb0ef41Sopenharmony_ci</div>
9031cb0ef41Sopenharmony_ci<ul>
9041cb0ef41Sopenharmony_ci<li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The HTTP/2 error code specified in the <code>GOAWAY</code> frame.</li>
9051cb0ef41Sopenharmony_ci<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The ID of the last stream the remote peer successfully
9061cb0ef41Sopenharmony_ciprocessed (or <code>0</code> if no ID is specified).</li>
9071cb0ef41Sopenharmony_ci<li><code>opaqueData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> If additional opaque data was included in the <code>GOAWAY</code>
9081cb0ef41Sopenharmony_ciframe, a <code>Buffer</code> instance will be passed containing that data.</li>
9091cb0ef41Sopenharmony_ci</ul>
9101cb0ef41Sopenharmony_ci<p>The <code>'goaway'</code> event is emitted when a <code>GOAWAY</code> frame is received.</p>
9111cb0ef41Sopenharmony_ci<p>The <code>Http2Session</code> instance will be shut down automatically when the <code>'goaway'</code>
9121cb0ef41Sopenharmony_cievent is emitted.</p>
9131cb0ef41Sopenharmony_ci<h5>Event: <code>'localSettings'</code><span><a class="mark" href="#event-localsettings" id="event-localsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_localsettings"></a></h5>
9141cb0ef41Sopenharmony_ci<div class="api_metadata">
9151cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
9161cb0ef41Sopenharmony_ci</div>
9171cb0ef41Sopenharmony_ci<ul>
9181cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
9191cb0ef41Sopenharmony_ci</ul>
9201cb0ef41Sopenharmony_ci<p>The <code>'localSettings'</code> event is emitted when an acknowledgment <code>SETTINGS</code> frame
9211cb0ef41Sopenharmony_cihas been received.</p>
9221cb0ef41Sopenharmony_ci<p>When using <code>http2session.settings()</code> to submit new settings, the modified
9231cb0ef41Sopenharmony_cisettings do not take effect until the <code>'localSettings'</code> event is emitted.</p>
9241cb0ef41Sopenharmony_ci<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> });
9251cb0ef41Sopenharmony_ci
9261cb0ef41Sopenharmony_cisession.<span class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
9271cb0ef41Sopenharmony_ci  <span class="hljs-comment">/* Use the new settings */</span>
9281cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9291cb0ef41Sopenharmony_ci<h5>Event: <code>'ping'</code><span><a class="mark" href="#event-ping" id="event-ping">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_ping"></a></h5>
9301cb0ef41Sopenharmony_ci<div class="api_metadata">
9311cb0ef41Sopenharmony_ci<span>Added in: v10.12.0</span>
9321cb0ef41Sopenharmony_ci</div>
9331cb0ef41Sopenharmony_ci<ul>
9341cb0ef41Sopenharmony_ci<li><code>payload</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> The <code>PING</code> frame 8-byte payload</li>
9351cb0ef41Sopenharmony_ci</ul>
9361cb0ef41Sopenharmony_ci<p>The <code>'ping'</code> event is emitted whenever a <code>PING</code> frame is received from the
9371cb0ef41Sopenharmony_ciconnected peer.</p>
9381cb0ef41Sopenharmony_ci<h5>Event: <code>'remoteSettings'</code><span><a class="mark" href="#event-remotesettings" id="event-remotesettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_remotesettings"></a></h5>
9391cb0ef41Sopenharmony_ci<div class="api_metadata">
9401cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
9411cb0ef41Sopenharmony_ci</div>
9421cb0ef41Sopenharmony_ci<ul>
9431cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
9441cb0ef41Sopenharmony_ci</ul>
9451cb0ef41Sopenharmony_ci<p>The <code>'remoteSettings'</code> event is emitted when a new <code>SETTINGS</code> frame is received
9461cb0ef41Sopenharmony_cifrom the connected peer.</p>
9471cb0ef41Sopenharmony_ci<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> {
9481cb0ef41Sopenharmony_ci  <span class="hljs-comment">/* Use the new settings */</span>
9491cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9501cb0ef41Sopenharmony_ci<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream" id="event-stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream"></a></h5>
9511cb0ef41Sopenharmony_ci<div class="api_metadata">
9521cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
9531cb0ef41Sopenharmony_ci</div>
9541cb0ef41Sopenharmony_ci<ul>
9551cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
9561cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
9571cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
9581cb0ef41Sopenharmony_ci<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
9591cb0ef41Sopenharmony_citheir respective values.</li>
9601cb0ef41Sopenharmony_ci</ul>
9611cb0ef41Sopenharmony_ci<p>The <code>'stream'</code> event is emitted when a new <code>Http2Stream</code> is created.</p>
9621cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
9631cb0ef41Sopenharmony_cisession.<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> {
9641cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span>];
9651cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
9661cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
9671cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
9681cb0ef41Sopenharmony_ci    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
9691cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
9701cb0ef41Sopenharmony_ci  });
9711cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
9721cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
9731cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
9741cb0ef41Sopenharmony_ci<p>On the server side, user code will typically not listen for this event directly,
9751cb0ef41Sopenharmony_ciand would instead register a handler for the <code>'stream'</code> event emitted by the
9761cb0ef41Sopenharmony_ci<code>net.Server</code> or <code>tls.Server</code> instances returned by <code>http2.createServer()</code> and
9771cb0ef41Sopenharmony_ci<code>http2.createSecureServer()</code>, respectively, as in the example below:</p>
9781cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
9791cb0ef41Sopenharmony_ci
9801cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
9811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ciserver.<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> {
9841cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
9851cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
9861cb0ef41Sopenharmony_ci    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
9871cb0ef41Sopenharmony_ci  });
9881cb0ef41Sopenharmony_ci  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));
9891cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
9901cb0ef41Sopenharmony_ci});
9911cb0ef41Sopenharmony_ci
9921cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
9931cb0ef41Sopenharmony_ci<p>Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence,
9941cb0ef41Sopenharmony_cia network error will destroy each individual stream and must be handled on the
9951cb0ef41Sopenharmony_cistream level, as shown above.</p>
9961cb0ef41Sopenharmony_ci<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout" id="event-timeout">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout"></a></h5>
9971cb0ef41Sopenharmony_ci<div class="api_metadata">
9981cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
9991cb0ef41Sopenharmony_ci</div>
10001cb0ef41Sopenharmony_ci<p>After the <code>http2session.setTimeout()</code> method is used to set the timeout period
10011cb0ef41Sopenharmony_cifor this <code>Http2Session</code>, the <code>'timeout'</code> event is emitted if there is no
10021cb0ef41Sopenharmony_ciactivity on the <code>Http2Session</code> after the configured number of milliseconds.
10031cb0ef41Sopenharmony_ciIts listener does not expect any arguments.</p>
10041cb0ef41Sopenharmony_ci<pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">2000</span>);
10051cb0ef41Sopenharmony_cisession.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });</code> <button class="copy-button">copy</button></pre>
10061cb0ef41Sopenharmony_ci<h5><code>http2session.alpnProtocol</code><span><a class="mark" href="#http2sessionalpnprotocol" id="http2sessionalpnprotocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_alpnprotocol"></a></h5>
10071cb0ef41Sopenharmony_ci<div class="api_metadata">
10081cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
10091cb0ef41Sopenharmony_ci</div>
10101cb0ef41Sopenharmony_ci<ul>
10111cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
10121cb0ef41Sopenharmony_ci</ul>
10131cb0ef41Sopenharmony_ci<p>Value will be <code>undefined</code> if the <code>Http2Session</code> is not yet connected to a
10141cb0ef41Sopenharmony_cisocket, <code>h2c</code> if the <code>Http2Session</code> is not connected to a <code>TLSSocket</code>, or
10151cb0ef41Sopenharmony_ciwill return the value of the connected <code>TLSSocket</code>'s own <code>alpnProtocol</code>
10161cb0ef41Sopenharmony_ciproperty.</p>
10171cb0ef41Sopenharmony_ci<h5><code>http2session.close([callback])</code><span><a class="mark" href="#http2sessionclosecallback" id="http2sessionclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_close_callback"></a></h5>
10181cb0ef41Sopenharmony_ci<div class="api_metadata">
10191cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
10201cb0ef41Sopenharmony_ci</div>
10211cb0ef41Sopenharmony_ci<ul>
10221cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
10231cb0ef41Sopenharmony_ci</ul>
10241cb0ef41Sopenharmony_ci<p>Gracefully closes the <code>Http2Session</code>, allowing any existing streams to
10251cb0ef41Sopenharmony_cicomplete on their own and preventing new <code>Http2Stream</code> instances from being
10261cb0ef41Sopenharmony_cicreated. Once closed, <code>http2session.destroy()</code> <em>might</em> be called if there
10271cb0ef41Sopenharmony_ciare no open <code>Http2Stream</code> instances.</p>
10281cb0ef41Sopenharmony_ci<p>If specified, the <code>callback</code> function is registered as a handler for the
10291cb0ef41Sopenharmony_ci<code>'close'</code> event.</p>
10301cb0ef41Sopenharmony_ci<h5><code>http2session.closed</code><span><a class="mark" href="#http2sessionclosed" id="http2sessionclosed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_closed"></a></h5>
10311cb0ef41Sopenharmony_ci<div class="api_metadata">
10321cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
10331cb0ef41Sopenharmony_ci</div>
10341cb0ef41Sopenharmony_ci<ul>
10351cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
10361cb0ef41Sopenharmony_ci</ul>
10371cb0ef41Sopenharmony_ci<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been closed, otherwise
10381cb0ef41Sopenharmony_ci<code>false</code>.</p>
10391cb0ef41Sopenharmony_ci<h5><code>http2session.connecting</code><span><a class="mark" href="#http2sessionconnecting" id="http2sessionconnecting">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_connecting"></a></h5>
10401cb0ef41Sopenharmony_ci<div class="api_metadata">
10411cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
10421cb0ef41Sopenharmony_ci</div>
10431cb0ef41Sopenharmony_ci<ul>
10441cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
10451cb0ef41Sopenharmony_ci</ul>
10461cb0ef41Sopenharmony_ci<p>Will be <code>true</code> if this <code>Http2Session</code> instance is still connecting, will be set
10471cb0ef41Sopenharmony_cito <code>false</code> before emitting <code>connect</code> event and/or calling the <code>http2.connect</code>
10481cb0ef41Sopenharmony_cicallback.</p>
10491cb0ef41Sopenharmony_ci<h5><code>http2session.destroy([error][, code])</code><span><a class="mark" href="#http2sessiondestroyerror-code" id="http2sessiondestroyerror-code">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_destroy_error_code"></a></h5>
10501cb0ef41Sopenharmony_ci<div class="api_metadata">
10511cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
10521cb0ef41Sopenharmony_ci</div>
10531cb0ef41Sopenharmony_ci<ul>
10541cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> An <code>Error</code> object if the <code>Http2Session</code> is being destroyed
10551cb0ef41Sopenharmony_cidue to an error.</li>
10561cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The HTTP/2 error code to send in the final <code>GOAWAY</code> frame.
10571cb0ef41Sopenharmony_ciIf unspecified, and <code>error</code> is not undefined, the default is <code>INTERNAL_ERROR</code>,
10581cb0ef41Sopenharmony_ciotherwise defaults to <code>NO_ERROR</code>.</li>
10591cb0ef41Sopenharmony_ci</ul>
10601cb0ef41Sopenharmony_ci<p>Immediately terminates the <code>Http2Session</code> and the associated <code>net.Socket</code> or
10611cb0ef41Sopenharmony_ci<code>tls.TLSSocket</code>.</p>
10621cb0ef41Sopenharmony_ci<p>Once destroyed, the <code>Http2Session</code> will emit the <code>'close'</code> event. If <code>error</code>
10631cb0ef41Sopenharmony_ciis not undefined, an <code>'error'</code> event will be emitted immediately before the
10641cb0ef41Sopenharmony_ci<code>'close'</code> event.</p>
10651cb0ef41Sopenharmony_ci<p>If there are any remaining open <code>Http2Streams</code> associated with the
10661cb0ef41Sopenharmony_ci<code>Http2Session</code>, those will also be destroyed.</p>
10671cb0ef41Sopenharmony_ci<h5><code>http2session.destroyed</code><span><a class="mark" href="#http2sessiondestroyed" id="http2sessiondestroyed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_destroyed"></a></h5>
10681cb0ef41Sopenharmony_ci<div class="api_metadata">
10691cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
10701cb0ef41Sopenharmony_ci</div>
10711cb0ef41Sopenharmony_ci<ul>
10721cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
10731cb0ef41Sopenharmony_ci</ul>
10741cb0ef41Sopenharmony_ci<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been destroyed and must no
10751cb0ef41Sopenharmony_cilonger be used, otherwise <code>false</code>.</p>
10761cb0ef41Sopenharmony_ci<h5><code>http2session.encrypted</code><span><a class="mark" href="#http2sessionencrypted" id="http2sessionencrypted">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_encrypted"></a></h5>
10771cb0ef41Sopenharmony_ci<div class="api_metadata">
10781cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
10791cb0ef41Sopenharmony_ci</div>
10801cb0ef41Sopenharmony_ci<ul>
10811cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
10821cb0ef41Sopenharmony_ci</ul>
10831cb0ef41Sopenharmony_ci<p>Value is <code>undefined</code> if the <code>Http2Session</code> session socket has not yet been
10841cb0ef41Sopenharmony_ciconnected, <code>true</code> if the <code>Http2Session</code> is connected with a <code>TLSSocket</code>,
10851cb0ef41Sopenharmony_ciand <code>false</code> if the <code>Http2Session</code> is connected to any other kind of socket
10861cb0ef41Sopenharmony_cior stream.</p>
10871cb0ef41Sopenharmony_ci<h5><code>http2session.goaway([code[, lastStreamID[, opaqueData]]])</code><span><a class="mark" href="#http2sessiongoawaycode-laststreamid-opaquedata" id="http2sessiongoawaycode-laststreamid-opaquedata">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_goaway_code_laststreamid_opaquedata"></a></h5>
10881cb0ef41Sopenharmony_ci<div class="api_metadata">
10891cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
10901cb0ef41Sopenharmony_ci</div>
10911cb0ef41Sopenharmony_ci<ul>
10921cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An HTTP/2 error code</li>
10931cb0ef41Sopenharmony_ci<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric ID of the last processed <code>Http2Stream</code></li>
10941cb0ef41Sopenharmony_ci<li><code>opaqueData</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> A <code>TypedArray</code> or <code>DataView</code>
10951cb0ef41Sopenharmony_ciinstance containing additional data to be carried within the <code>GOAWAY</code> frame.</li>
10961cb0ef41Sopenharmony_ci</ul>
10971cb0ef41Sopenharmony_ci<p>Transmits a <code>GOAWAY</code> frame to the connected peer <em>without</em> shutting down the
10981cb0ef41Sopenharmony_ci<code>Http2Session</code>.</p>
10991cb0ef41Sopenharmony_ci<h5><code>http2session.localSettings</code><span><a class="mark" href="#http2sessionlocalsettings" id="http2sessionlocalsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_localsettings"></a></h5>
11001cb0ef41Sopenharmony_ci<div class="api_metadata">
11011cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
11021cb0ef41Sopenharmony_ci</div>
11031cb0ef41Sopenharmony_ci<ul>
11041cb0ef41Sopenharmony_ci<li><a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
11051cb0ef41Sopenharmony_ci</ul>
11061cb0ef41Sopenharmony_ci<p>A prototype-less object describing the current local settings of this
11071cb0ef41Sopenharmony_ci<code>Http2Session</code>. The local settings are local to <em>this</em> <code>Http2Session</code> instance.</p>
11081cb0ef41Sopenharmony_ci<h5><code>http2session.originSet</code><span><a class="mark" href="#http2sessionoriginset" id="http2sessionoriginset">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_originset"></a></h5>
11091cb0ef41Sopenharmony_ci<div class="api_metadata">
11101cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
11111cb0ef41Sopenharmony_ci</div>
11121cb0ef41Sopenharmony_ci<ul>
11131cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
11141cb0ef41Sopenharmony_ci</ul>
11151cb0ef41Sopenharmony_ci<p>If the <code>Http2Session</code> is connected to a <code>TLSSocket</code>, the <code>originSet</code> property
11161cb0ef41Sopenharmony_ciwill return an <code>Array</code> of origins for which the <code>Http2Session</code> may be
11171cb0ef41Sopenharmony_ciconsidered authoritative.</p>
11181cb0ef41Sopenharmony_ci<p>The <code>originSet</code> property is only available when using a secure TLS connection.</p>
11191cb0ef41Sopenharmony_ci<h5><code>http2session.pendingSettingsAck</code><span><a class="mark" href="#http2sessionpendingsettingsack" id="http2sessionpendingsettingsack">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_pendingsettingsack"></a></h5>
11201cb0ef41Sopenharmony_ci<div class="api_metadata">
11211cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
11221cb0ef41Sopenharmony_ci</div>
11231cb0ef41Sopenharmony_ci<ul>
11241cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
11251cb0ef41Sopenharmony_ci</ul>
11261cb0ef41Sopenharmony_ci<p>Indicates whether the <code>Http2Session</code> is currently waiting for acknowledgment of
11271cb0ef41Sopenharmony_cia sent <code>SETTINGS</code> frame. Will be <code>true</code> after calling the
11281cb0ef41Sopenharmony_ci<code>http2session.settings()</code> method. Will be <code>false</code> once all sent <code>SETTINGS</code>
11291cb0ef41Sopenharmony_ciframes have been acknowledged.</p>
11301cb0ef41Sopenharmony_ci<h5><code>http2session.ping([payload, ]callback)</code><span><a class="mark" href="#http2sessionpingpayload-callback" id="http2sessionpingpayload-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_ping_payload_callback"></a></h5>
11311cb0ef41Sopenharmony_ci<div class="api_metadata">
11321cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
11331cb0ef41Sopenharmony_ci<table>
11341cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
11351cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
11361cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
11371cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
11381cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.9.3</span></p></td></tr>
11391cb0ef41Sopenharmony_ci</tbody></table>
11401cb0ef41Sopenharmony_ci</details>
11411cb0ef41Sopenharmony_ci</div>
11421cb0ef41Sopenharmony_ci<ul>
11431cb0ef41Sopenharmony_ci<li><code>payload</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> Optional ping payload.</li>
11441cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
11451cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
11461cb0ef41Sopenharmony_ci</ul>
11471cb0ef41Sopenharmony_ci<p>Sends a <code>PING</code> frame to the connected HTTP/2 peer. A <code>callback</code> function must
11481cb0ef41Sopenharmony_cibe provided. The method will return <code>true</code> if the <code>PING</code> was sent, <code>false</code>
11491cb0ef41Sopenharmony_ciotherwise.</p>
11501cb0ef41Sopenharmony_ci<p>The maximum number of outstanding (unacknowledged) pings is determined by the
11511cb0ef41Sopenharmony_ci<code>maxOutstandingPings</code> configuration option. The default maximum is 10.</p>
11521cb0ef41Sopenharmony_ci<p>If provided, the <code>payload</code> must be a <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>
11531cb0ef41Sopenharmony_cicontaining 8 bytes of data that will be transmitted with the <code>PING</code> and
11541cb0ef41Sopenharmony_cireturned with the ping acknowledgment.</p>
11551cb0ef41Sopenharmony_ci<p>The callback will be invoked with three arguments: an error argument that will
11561cb0ef41Sopenharmony_cibe <code>null</code> if the <code>PING</code> was successfully acknowledged, a <code>duration</code> argument
11571cb0ef41Sopenharmony_cithat reports the number of milliseconds elapsed since the ping was sent and the
11581cb0ef41Sopenharmony_ciacknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
11591cb0ef41Sopenharmony_cipayload.</p>
11601cb0ef41Sopenharmony_ci<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> {
11611cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (!err) {
11621cb0ef41Sopenharmony_ci    <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>);
11631cb0ef41Sopenharmony_ci    <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>);
11641cb0ef41Sopenharmony_ci  }
11651cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
11661cb0ef41Sopenharmony_ci<p>If the <code>payload</code> argument is not specified, the default payload will be the
11671cb0ef41Sopenharmony_ci64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
11681cb0ef41Sopenharmony_ci<h5><code>http2session.ref()</code><span><a class="mark" href="#http2sessionref" id="http2sessionref">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_ref"></a></h5>
11691cb0ef41Sopenharmony_ci<div class="api_metadata">
11701cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
11711cb0ef41Sopenharmony_ci</div>
11721cb0ef41Sopenharmony_ci<p>Calls <a href="net.html#socketref"><code>ref()</code></a> on this <code>Http2Session</code>
11731cb0ef41Sopenharmony_ciinstance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p>
11741cb0ef41Sopenharmony_ci<h5><code>http2session.remoteSettings</code><span><a class="mark" href="#http2sessionremotesettings" id="http2sessionremotesettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_remotesettings"></a></h5>
11751cb0ef41Sopenharmony_ci<div class="api_metadata">
11761cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
11771cb0ef41Sopenharmony_ci</div>
11781cb0ef41Sopenharmony_ci<ul>
11791cb0ef41Sopenharmony_ci<li><a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
11801cb0ef41Sopenharmony_ci</ul>
11811cb0ef41Sopenharmony_ci<p>A prototype-less object describing the current remote settings of this
11821cb0ef41Sopenharmony_ci<code>Http2Session</code>. The remote settings are set by the <em>connected</em> HTTP/2 peer.</p>
11831cb0ef41Sopenharmony_ci<h5><code>http2session.setLocalWindowSize(windowSize)</code><span><a class="mark" href="#http2sessionsetlocalwindowsizewindowsize" id="http2sessionsetlocalwindowsizewindowsize">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_setlocalwindowsize_windowsize"></a></h5>
11841cb0ef41Sopenharmony_ci<div class="api_metadata">
11851cb0ef41Sopenharmony_ci<span>Added in: v15.3.0, v14.18.0</span>
11861cb0ef41Sopenharmony_ci</div>
11871cb0ef41Sopenharmony_ci<ul>
11881cb0ef41Sopenharmony_ci<li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
11891cb0ef41Sopenharmony_ci</ul>
11901cb0ef41Sopenharmony_ci<p>Sets the local endpoint's window size.
11911cb0ef41Sopenharmony_ciThe <code>windowSize</code> is the total window size to set, not
11921cb0ef41Sopenharmony_cithe delta.</p>
11931cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
11941cb0ef41Sopenharmony_ci
11951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
11961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** <span class="hljs-number">20</span>;
11971cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
11981cb0ef41Sopenharmony_ci
11991cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Set local window size to be 2 ** 20</span>
12001cb0ef41Sopenharmony_ci  session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
12011cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
12021cb0ef41Sopenharmony_ci<h5><code>http2session.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2sessionsettimeoutmsecs-callback" id="http2sessionsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_settimeout_msecs_callback"></a></h5>
12031cb0ef41Sopenharmony_ci<div class="api_metadata">
12041cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
12051cb0ef41Sopenharmony_ci<table>
12061cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
12071cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
12081cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
12091cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
12101cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
12111cb0ef41Sopenharmony_ci</tbody></table>
12121cb0ef41Sopenharmony_ci</details>
12131cb0ef41Sopenharmony_ci</div>
12141cb0ef41Sopenharmony_ci<ul>
12151cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
12161cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
12171cb0ef41Sopenharmony_ci</ul>
12181cb0ef41Sopenharmony_ci<p>Used to set a callback function that is called when there is no activity on
12191cb0ef41Sopenharmony_cithe <code>Http2Session</code> after <code>msecs</code> milliseconds. The given <code>callback</code> is
12201cb0ef41Sopenharmony_ciregistered as a listener on the <code>'timeout'</code> event.</p>
12211cb0ef41Sopenharmony_ci<h5><code>http2session.socket</code><span><a class="mark" href="#http2sessionsocket" id="http2sessionsocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_socket"></a></h5>
12221cb0ef41Sopenharmony_ci<div class="api_metadata">
12231cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
12241cb0ef41Sopenharmony_ci</div>
12251cb0ef41Sopenharmony_ci<ul>
12261cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
12271cb0ef41Sopenharmony_ci</ul>
12281cb0ef41Sopenharmony_ci<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
12291cb0ef41Sopenharmony_cilimits available methods to ones safe to use with HTTP/2.</p>
12301cb0ef41Sopenharmony_ci<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
12311cb0ef41Sopenharmony_cian error with code <code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See
12321cb0ef41Sopenharmony_ci<a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for more information.</p>
12331cb0ef41Sopenharmony_ci<p><code>setTimeout</code> method will be called on this <code>Http2Session</code>.</p>
12341cb0ef41Sopenharmony_ci<p>All other interactions will be routed directly to the socket.</p>
12351cb0ef41Sopenharmony_ci<h5><code>http2session.state</code><span><a class="mark" href="#http2sessionstate" id="http2sessionstate">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_state"></a></h5>
12361cb0ef41Sopenharmony_ci<div class="api_metadata">
12371cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
12381cb0ef41Sopenharmony_ci</div>
12391cb0ef41Sopenharmony_ci<p>Provides miscellaneous information about the current state of the
12401cb0ef41Sopenharmony_ci<code>Http2Session</code>.</p>
12411cb0ef41Sopenharmony_ci<ul>
12421cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
12431cb0ef41Sopenharmony_ci<ul>
12441cb0ef41Sopenharmony_ci<li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current local (receive)
12451cb0ef41Sopenharmony_ciflow control window size for the <code>Http2Session</code>.</li>
12461cb0ef41Sopenharmony_ci<li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current number of bytes
12471cb0ef41Sopenharmony_cithat have been received since the last flow control <code>WINDOW_UPDATE</code>.</li>
12481cb0ef41Sopenharmony_ci<li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric identifier to be used the
12491cb0ef41Sopenharmony_cinext time a new <code>Http2Stream</code> is created by this <code>Http2Session</code>.</li>
12501cb0ef41Sopenharmony_ci<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes that the remote peer can
12511cb0ef41Sopenharmony_cisend without receiving a <code>WINDOW_UPDATE</code>.</li>
12521cb0ef41Sopenharmony_ci<li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The numeric id of the <code>Http2Stream</code>
12531cb0ef41Sopenharmony_cifor which a <code>HEADERS</code> or <code>DATA</code> frame was most recently received.</li>
12541cb0ef41Sopenharmony_ci<li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes that this <code>Http2Session</code>
12551cb0ef41Sopenharmony_cimay send without receiving a <code>WINDOW_UPDATE</code>.</li>
12561cb0ef41Sopenharmony_ci<li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of frames currently within the
12571cb0ef41Sopenharmony_cioutbound queue for this <code>Http2Session</code>.</li>
12581cb0ef41Sopenharmony_ci<li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current size in bytes of the
12591cb0ef41Sopenharmony_cioutbound header compression state table.</li>
12601cb0ef41Sopenharmony_ci<li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The current size in bytes of the
12611cb0ef41Sopenharmony_ciinbound header compression state table.</li>
12621cb0ef41Sopenharmony_ci</ul>
12631cb0ef41Sopenharmony_ci</li>
12641cb0ef41Sopenharmony_ci</ul>
12651cb0ef41Sopenharmony_ci<p>An object describing the current status of this <code>Http2Session</code>.</p>
12661cb0ef41Sopenharmony_ci<h5><code>http2session.settings([settings][, callback])</code><span><a class="mark" href="#http2sessionsettingssettings-callback" id="http2sessionsettingssettings-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_settings_settings_callback"></a></h5>
12671cb0ef41Sopenharmony_ci<div class="api_metadata">
12681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
12691cb0ef41Sopenharmony_ci<table>
12701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
12711cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
12721cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
12731cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
12741cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
12751cb0ef41Sopenharmony_ci</tbody></table>
12761cb0ef41Sopenharmony_ci</details>
12771cb0ef41Sopenharmony_ci</div>
12781cb0ef41Sopenharmony_ci<ul>
12791cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
12801cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback that is called once the session is connected or
12811cb0ef41Sopenharmony_ciright away if the session is already connected.
12821cb0ef41Sopenharmony_ci<ul>
12831cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a></li>
12841cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> The updated <code>settings</code> object.</li>
12851cb0ef41Sopenharmony_ci<li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
12861cb0ef41Sopenharmony_ci</ul>
12871cb0ef41Sopenharmony_ci</li>
12881cb0ef41Sopenharmony_ci</ul>
12891cb0ef41Sopenharmony_ci<p>Updates the current local settings for this <code>Http2Session</code> and sends a new
12901cb0ef41Sopenharmony_ci<code>SETTINGS</code> frame to the connected HTTP/2 peer.</p>
12911cb0ef41Sopenharmony_ci<p>Once called, the <code>http2session.pendingSettingsAck</code> property will be <code>true</code>
12921cb0ef41Sopenharmony_ciwhile the session is waiting for the remote peer to acknowledge the new
12931cb0ef41Sopenharmony_cisettings.</p>
12941cb0ef41Sopenharmony_ci<p>The new settings will not become effective until the <code>SETTINGS</code> acknowledgment
12951cb0ef41Sopenharmony_ciis received and the <code>'localSettings'</code> event is emitted. It is possible to send
12961cb0ef41Sopenharmony_cimultiple <code>SETTINGS</code> frames while acknowledgment is still pending.</p>
12971cb0ef41Sopenharmony_ci<h5><code>http2session.type</code><span><a class="mark" href="#http2sessiontype" id="http2sessiontype">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_type"></a></h5>
12981cb0ef41Sopenharmony_ci<div class="api_metadata">
12991cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
13001cb0ef41Sopenharmony_ci</div>
13011cb0ef41Sopenharmony_ci<ul>
13021cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
13031cb0ef41Sopenharmony_ci</ul>
13041cb0ef41Sopenharmony_ci<p>The <code>http2session.type</code> will be equal to
13051cb0ef41Sopenharmony_ci<code>http2.constants.NGHTTP2_SESSION_SERVER</code> if this <code>Http2Session</code> instance is a
13061cb0ef41Sopenharmony_ciserver, and <code>http2.constants.NGHTTP2_SESSION_CLIENT</code> if the instance is a
13071cb0ef41Sopenharmony_ciclient.</p>
13081cb0ef41Sopenharmony_ci<h5><code>http2session.unref()</code><span><a class="mark" href="#http2sessionunref" id="http2sessionunref">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2session_unref"></a></h5>
13091cb0ef41Sopenharmony_ci<div class="api_metadata">
13101cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
13111cb0ef41Sopenharmony_ci</div>
13121cb0ef41Sopenharmony_ci<p>Calls <a href="net.html#socketunref"><code>unref()</code></a> on this <code>Http2Session</code>
13131cb0ef41Sopenharmony_ciinstance's underlying <a href="net.html#class-netsocket"><code>net.Socket</code></a>.</p>
13141cb0ef41Sopenharmony_ci<h4>Class: <code>ServerHttp2Session</code><span><a class="mark" href="#class-serverhttp2session" id="class-serverhttp2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_serverhttp2session"></a></h4>
13151cb0ef41Sopenharmony_ci<div class="api_metadata">
13161cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
13171cb0ef41Sopenharmony_ci</div>
13181cb0ef41Sopenharmony_ci<ul>
13191cb0ef41Sopenharmony_ci<li>Extends: <a href="http2.html#class-http2session" class="type">&#x3C;Http2Session></a></li>
13201cb0ef41Sopenharmony_ci</ul>
13211cb0ef41Sopenharmony_ci<h5><code>serverhttp2session.altsvc(alt, originOrStream)</code><span><a class="mark" href="#serverhttp2sessionaltsvcalt-originorstream" id="serverhttp2sessionaltsvcalt-originorstream">#</a></span><a aria-hidden="true" class="legacy" id="http2_serverhttp2session_altsvc_alt_originorstream"></a></h5>
13221cb0ef41Sopenharmony_ci<div class="api_metadata">
13231cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
13241cb0ef41Sopenharmony_ci</div>
13251cb0ef41Sopenharmony_ci<ul>
13261cb0ef41Sopenharmony_ci<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A description of the alternative service configuration as
13271cb0ef41Sopenharmony_cidefined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>.</li>
13281cb0ef41Sopenharmony_ci<li><code>originOrStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> Either a URL string specifying
13291cb0ef41Sopenharmony_cithe origin (or an <code>Object</code> with an <code>origin</code> property) or the numeric
13301cb0ef41Sopenharmony_ciidentifier of an active <code>Http2Stream</code> as given by the <code>http2stream.id</code>
13311cb0ef41Sopenharmony_ciproperty.</li>
13321cb0ef41Sopenharmony_ci</ul>
13331cb0ef41Sopenharmony_ci<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>
13341cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
13351cb0ef41Sopenharmony_ci
13361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
13371cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
13381cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
13391cb0ef41Sopenharmony_ci  session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>);
13401cb0ef41Sopenharmony_ci});
13411cb0ef41Sopenharmony_ci
13421cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
13431cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Set altsvc for a specific stream</span>
13441cb0ef41Sopenharmony_ci  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>);
13451cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
13461cb0ef41Sopenharmony_ci<p>Sending an <code>ALTSVC</code> frame with a specific stream ID indicates that the alternate
13471cb0ef41Sopenharmony_ciservice is associated with the origin of the given <code>Http2Stream</code>.</p>
13481cb0ef41Sopenharmony_ci<p>The <code>alt</code> and origin string <em>must</em> contain only ASCII bytes and are
13491cb0ef41Sopenharmony_cistrictly interpreted as a sequence of ASCII bytes. The special value <code>'clear'</code>
13501cb0ef41Sopenharmony_cimay be passed to clear any previously set alternative service for a given
13511cb0ef41Sopenharmony_cidomain.</p>
13521cb0ef41Sopenharmony_ci<p>When a string is passed for the <code>originOrStream</code> argument, it will be parsed as
13531cb0ef41Sopenharmony_cia URL and the origin will be derived. For instance, the origin for the
13541cb0ef41Sopenharmony_ciHTTP URL <code>'https://example.org/foo/bar'</code> is the ASCII string
13551cb0ef41Sopenharmony_ci<code>'https://example.org'</code>. An error will be thrown if either the given string
13561cb0ef41Sopenharmony_cicannot be parsed as a URL or if a valid origin cannot be derived.</p>
13571cb0ef41Sopenharmony_ci<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
13581cb0ef41Sopenharmony_ci<code>originOrStream</code>, in which case the value of the <code>origin</code> property will be
13591cb0ef41Sopenharmony_ciused. The value of the <code>origin</code> property <em>must</em> be a properly serialized
13601cb0ef41Sopenharmony_ciASCII origin.</p>
13611cb0ef41Sopenharmony_ci<h5>Specifying alternative services<span><a class="mark" href="#specifying-alternative-services" id="specifying-alternative-services">#</a></span><a aria-hidden="true" class="legacy" id="http2_specifying_alternative_services"></a></h5>
13621cb0ef41Sopenharmony_ci<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
13631cb0ef41Sopenharmony_ciASCII string containing a comma-delimited list of "alternative" protocols
13641cb0ef41Sopenharmony_ciassociated with a specific host and port.</p>
13651cb0ef41Sopenharmony_ci<p>For example, the value <code>'h2="example.org:81"'</code> indicates that the HTTP/2
13661cb0ef41Sopenharmony_ciprotocol is available on the host <code>'example.org'</code> on TCP/IP port 81. The
13671cb0ef41Sopenharmony_cihost and port <em>must</em> be contained within the quote (<code>"</code>) characters.</p>
13681cb0ef41Sopenharmony_ci<p>Multiple alternatives may be specified, for instance: <code>'h2="example.org:81", h2=":82"'</code>.</p>
13691cb0ef41Sopenharmony_ci<p>The protocol identifier (<code>'h2'</code> in the examples) may be any valid
13701cb0ef41Sopenharmony_ci<a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">ALPN Protocol ID</a>.</p>
13711cb0ef41Sopenharmony_ci<p>The syntax of these values is not validated by the Node.js implementation and
13721cb0ef41Sopenharmony_ciare passed through as provided by the user or received from the peer.</p>
13731cb0ef41Sopenharmony_ci<h5><code>serverhttp2session.origin(...origins)</code><span><a class="mark" href="#serverhttp2sessionoriginorigins" id="serverhttp2sessionoriginorigins">#</a></span><a aria-hidden="true" class="legacy" id="http2_serverhttp2session_origin_origins"></a></h5>
13741cb0ef41Sopenharmony_ci<div class="api_metadata">
13751cb0ef41Sopenharmony_ci<span>Added in: v10.12.0</span>
13761cb0ef41Sopenharmony_ci</div>
13771cb0ef41Sopenharmony_ci<ul>
13781cb0ef41Sopenharmony_ci<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> One or more URL Strings passed as
13791cb0ef41Sopenharmony_ciseparate arguments.</li>
13801cb0ef41Sopenharmony_ci</ul>
13811cb0ef41Sopenharmony_ci<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
13821cb0ef41Sopenharmony_cito advertise the set of origins for which the server is capable of providing
13831cb0ef41Sopenharmony_ciauthoritative responses.</p>
13841cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
13851cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
13861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
13871cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
13881cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>();
13891cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
13901cb0ef41Sopenharmony_ci});
13911cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span class="hljs-function">(<span class="hljs-params">session</span>) =></span> {
13921cb0ef41Sopenharmony_ci  session.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>);
13931cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
13941cb0ef41Sopenharmony_ci<p>When a string is passed as an <code>origin</code>, it will be parsed as a URL and the
13951cb0ef41Sopenharmony_ciorigin will be derived. For instance, the origin for the HTTP URL
13961cb0ef41Sopenharmony_ci<code>'https://example.org/foo/bar'</code> is the ASCII string
13971cb0ef41Sopenharmony_ci<code>'https://example.org'</code>. An error will be thrown if either the given string
13981cb0ef41Sopenharmony_cicannot be parsed as a URL or if a valid origin cannot be derived.</p>
13991cb0ef41Sopenharmony_ci<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
14001cb0ef41Sopenharmony_cian <code>origin</code>, in which case the value of the <code>origin</code> property will be
14011cb0ef41Sopenharmony_ciused. The value of the <code>origin</code> property <em>must</em> be a properly serialized
14021cb0ef41Sopenharmony_ciASCII origin.</p>
14031cb0ef41Sopenharmony_ci<p>Alternatively, the <code>origins</code> option may be used when creating a new HTTP/2
14041cb0ef41Sopenharmony_ciserver using the <code>http2.createSecureServer()</code> method:</p>
14051cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
14061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptionsSomehow</span>();
14071cb0ef41Sopenharmony_cioptions.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>];
14081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
14091cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
14101cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>();
14111cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
14121cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
14131cb0ef41Sopenharmony_ci<h4>Class: <code>ClientHttp2Session</code><span><a class="mark" href="#class-clienthttp2session" id="class-clienthttp2session">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_clienthttp2session"></a></h4>
14141cb0ef41Sopenharmony_ci<div class="api_metadata">
14151cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
14161cb0ef41Sopenharmony_ci</div>
14171cb0ef41Sopenharmony_ci<ul>
14181cb0ef41Sopenharmony_ci<li>Extends: <a href="http2.html#class-http2session" class="type">&#x3C;Http2Session></a></li>
14191cb0ef41Sopenharmony_ci</ul>
14201cb0ef41Sopenharmony_ci<h5>Event: <code>'altsvc'</code><span><a class="mark" href="#event-altsvc" id="event-altsvc">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_altsvc"></a></h5>
14211cb0ef41Sopenharmony_ci<div class="api_metadata">
14221cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
14231cb0ef41Sopenharmony_ci</div>
14241cb0ef41Sopenharmony_ci<ul>
14251cb0ef41Sopenharmony_ci<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
14261cb0ef41Sopenharmony_ci<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
14271cb0ef41Sopenharmony_ci<li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
14281cb0ef41Sopenharmony_ci</ul>
14291cb0ef41Sopenharmony_ci<p>The <code>'altsvc'</code> event is emitted whenever an <code>ALTSVC</code> frame is received by
14301cb0ef41Sopenharmony_cithe client. The event is emitted with the <code>ALTSVC</code> value, origin, and stream
14311cb0ef41Sopenharmony_ciID. If no <code>origin</code> is provided in the <code>ALTSVC</code> frame, <code>origin</code> will
14321cb0ef41Sopenharmony_cibe an empty string.</p>
14331cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
14341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
14351cb0ef41Sopenharmony_ci
14361cb0ef41Sopenharmony_ciclient.<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> {
14371cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(alt);
14381cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origin);
14391cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(streamId);
14401cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
14411cb0ef41Sopenharmony_ci<h5>Event: <code>'origin'</code><span><a class="mark" href="#event-origin" id="event-origin">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_origin"></a></h5>
14421cb0ef41Sopenharmony_ci<div class="api_metadata">
14431cb0ef41Sopenharmony_ci<span>Added in: v10.12.0</span>
14441cb0ef41Sopenharmony_ci</div>
14451cb0ef41Sopenharmony_ci<ul>
14461cb0ef41Sopenharmony_ci<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
14471cb0ef41Sopenharmony_ci</ul>
14481cb0ef41Sopenharmony_ci<p>The <code>'origin'</code> event is emitted whenever an <code>ORIGIN</code> frame is received by
14491cb0ef41Sopenharmony_cithe client. The event is emitted with an array of <code>origin</code> strings. The
14501cb0ef41Sopenharmony_ci<code>http2session.originSet</code> will be updated to include the received
14511cb0ef41Sopenharmony_ciorigins.</p>
14521cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
14531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://example.org'</span>);
14541cb0ef41Sopenharmony_ci
14551cb0ef41Sopenharmony_ciclient.<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span class="hljs-function">(<span class="hljs-params">origins</span>) =></span> {
14561cb0ef41Sopenharmony_ci  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n &#x3C; origins.<span class="hljs-property">length</span>; n++)
14571cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(origins[n]);
14581cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
14591cb0ef41Sopenharmony_ci<p>The <code>'origin'</code> event is only emitted when using a secure TLS connection.</p>
14601cb0ef41Sopenharmony_ci<h5><code>clienthttp2session.request(headers[, options])</code><span><a class="mark" href="#clienthttp2sessionrequestheaders-options" id="clienthttp2sessionrequestheaders-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_clienthttp2session_request_headers_options"></a></h5>
14611cb0ef41Sopenharmony_ci<div class="api_metadata">
14621cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
14631cb0ef41Sopenharmony_ci</div>
14641cb0ef41Sopenharmony_ci<ul>
14651cb0ef41Sopenharmony_ci<li>
14661cb0ef41Sopenharmony_ci<p><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></p>
14671cb0ef41Sopenharmony_ci</li>
14681cb0ef41Sopenharmony_ci<li>
14691cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
14701cb0ef41Sopenharmony_ci<ul>
14711cb0ef41Sopenharmony_ci<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the <code>Http2Stream</code> <em>writable</em> side should
14721cb0ef41Sopenharmony_cibe closed initially, such as when sending a <code>GET</code> request that should not
14731cb0ef41Sopenharmony_ciexpect a payload body.</li>
14741cb0ef41Sopenharmony_ci<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
14751cb0ef41Sopenharmony_cithe created stream is made the sole direct dependency of the parent, with
14761cb0ef41Sopenharmony_ciall other existing dependents made a dependent of the newly created stream.
14771cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
14781cb0ef41Sopenharmony_ci<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream the newly
14791cb0ef41Sopenharmony_cicreated stream is dependent on.</li>
14801cb0ef41Sopenharmony_ci<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the relative dependency of a stream in relation
14811cb0ef41Sopenharmony_cito other streams with the same <code>parent</code>. The value is a number between <code>1</code>
14821cb0ef41Sopenharmony_ciand <code>256</code> (inclusive).</li>
14831cb0ef41Sopenharmony_ci<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
14841cb0ef41Sopenharmony_ci<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
14851cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An AbortSignal that may be used to abort an ongoing
14861cb0ef41Sopenharmony_cirequest.</li>
14871cb0ef41Sopenharmony_ci</ul>
14881cb0ef41Sopenharmony_ci</li>
14891cb0ef41Sopenharmony_ci<li>
14901cb0ef41Sopenharmony_ci<p>Returns: <a href="http2.html#class-clienthttp2stream" class="type">&#x3C;ClientHttp2Stream></a></p>
14911cb0ef41Sopenharmony_ci</li>
14921cb0ef41Sopenharmony_ci</ul>
14931cb0ef41Sopenharmony_ci<p>For HTTP/2 Client <code>Http2Session</code> instances only, the <code>http2session.request()</code>
14941cb0ef41Sopenharmony_cicreates and returns an <code>Http2Stream</code> instance that can be used to send an
14951cb0ef41Sopenharmony_ciHTTP/2 request to the connected server.</p>
14961cb0ef41Sopenharmony_ci<p>When a <code>ClientHttp2Session</code> is first created, the socket may not yet be
14971cb0ef41Sopenharmony_ciconnected. if <code>clienthttp2session.request()</code> is called during this time, the
14981cb0ef41Sopenharmony_ciactual request will be deferred until the socket is ready to go.
14991cb0ef41Sopenharmony_ciIf the <code>session</code> is closed before the actual request be executed, an
15001cb0ef41Sopenharmony_ci<code>ERR_HTTP2_GOAWAY_SESSION</code> is thrown.</p>
15011cb0ef41Sopenharmony_ci<p>This method is only available if <code>http2session.type</code> is equal to
15021cb0ef41Sopenharmony_ci<code>http2.constants.NGHTTP2_SESSION_CLIENT</code>.</p>
15031cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
15041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
15051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
15061cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
15071cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
15081cb0ef41Sopenharmony_ci} = http2.<span class="hljs-property">constants</span>;
15091cb0ef41Sopenharmony_ci
15101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</span>({ [<span class="hljs-title class_">HTTP2</span>_HEADER_PATH]: <span class="hljs-string">'/'</span> });
15111cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
15121cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]);
15131cb0ef41Sopenharmony_ci  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> });
15141cb0ef41Sopenharmony_ci  req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> { <span class="hljs-comment">/* .. */</span> });
15151cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
15161cb0ef41Sopenharmony_ci<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
15171cb0ef41Sopenharmony_ciis emitted immediately after queuing the last chunk of payload data to be sent.
15181cb0ef41Sopenharmony_ciThe <code>http2stream.sendTrailers()</code> method can then be called to send trailing
15191cb0ef41Sopenharmony_ciheaders to the peer.</p>
15201cb0ef41Sopenharmony_ci<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
15211cb0ef41Sopenharmony_ciclose when the final <code>DATA</code> frame is transmitted. User code must call either
15221cb0ef41Sopenharmony_ci<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
15231cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</p>
15241cb0ef41Sopenharmony_ci<p>When <code>options.signal</code> is set with an <code>AbortSignal</code> and then <code>abort</code> on the
15251cb0ef41Sopenharmony_cicorresponding <code>AbortController</code> is called, the request will emit an <code>'error'</code>
15261cb0ef41Sopenharmony_cievent with an <code>AbortError</code> error.</p>
15271cb0ef41Sopenharmony_ci<p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>headers</code>,
15281cb0ef41Sopenharmony_cithey respectively default to:</p>
15291cb0ef41Sopenharmony_ci<ul>
15301cb0ef41Sopenharmony_ci<li><code>:method</code> = <code>'GET'</code></li>
15311cb0ef41Sopenharmony_ci<li><code>:path</code> = <code>/</code></li>
15321cb0ef41Sopenharmony_ci</ul>
15331cb0ef41Sopenharmony_ci<h4>Class: <code>Http2Stream</code><span><a class="mark" href="#class-http2stream" id="class-http2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2stream"></a></h4>
15341cb0ef41Sopenharmony_ci<div class="api_metadata">
15351cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
15361cb0ef41Sopenharmony_ci</div>
15371cb0ef41Sopenharmony_ci<ul>
15381cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamduplex" class="type">&#x3C;stream.Duplex></a></li>
15391cb0ef41Sopenharmony_ci</ul>
15401cb0ef41Sopenharmony_ci<p>Each instance of the <code>Http2Stream</code> class represents a bidirectional HTTP/2
15411cb0ef41Sopenharmony_cicommunications stream over an <code>Http2Session</code> instance. Any single <code>Http2Session</code>
15421cb0ef41Sopenharmony_cimay have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
15431cb0ef41Sopenharmony_ci<p>User code will not construct <code>Http2Stream</code> instances directly. Rather, these
15441cb0ef41Sopenharmony_ciare created, managed, and provided to user code through the <code>Http2Session</code>
15451cb0ef41Sopenharmony_ciinstance. On the server, <code>Http2Stream</code> instances are created either in response
15461cb0ef41Sopenharmony_cito an incoming HTTP request (and handed off to user code via the <code>'stream'</code>
15471cb0ef41Sopenharmony_cievent), or in response to a call to the <code>http2stream.pushStream()</code> method.
15481cb0ef41Sopenharmony_ciOn the client, <code>Http2Stream</code> instances are created and returned when either the
15491cb0ef41Sopenharmony_ci<code>http2session.request()</code> method is called, or in response to an incoming
15501cb0ef41Sopenharmony_ci<code>'push'</code> event.</p>
15511cb0ef41Sopenharmony_ci<p>The <code>Http2Stream</code> class is a base for the <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> and
15521cb0ef41Sopenharmony_ci<a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> classes, each of which is used specifically by either
15531cb0ef41Sopenharmony_cithe Server or Client side, respectively.</p>
15541cb0ef41Sopenharmony_ci<p>All <code>Http2Stream</code> instances are <a href="stream.html#class-streamduplex"><code>Duplex</code></a> streams. The <code>Writable</code> side of the
15551cb0ef41Sopenharmony_ci<code>Duplex</code> is used to send data to the connected peer, while the <code>Readable</code> side
15561cb0ef41Sopenharmony_ciis used to receive data sent by the connected peer.</p>
15571cb0ef41Sopenharmony_ci<p>The default text character encoding for an <code>Http2Stream</code> is UTF-8. When using an
15581cb0ef41Sopenharmony_ci<code>Http2Stream</code> to send text, use the <code>'content-type'</code> header to set the character
15591cb0ef41Sopenharmony_ciencoding.</p>
15601cb0ef41Sopenharmony_ci<pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
15611cb0ef41Sopenharmony_ci  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
15621cb0ef41Sopenharmony_ci  <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
15631cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
15641cb0ef41Sopenharmony_ci<h5><code>Http2Stream</code> Lifecycle<span><a class="mark" href="#http2stream-lifecycle" id="http2stream-lifecycle">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_lifecycle"></a></h5>
15651cb0ef41Sopenharmony_ci<h6>Creation<span><a class="mark" href="#creation" id="creation">#</a></span><a aria-hidden="true" class="legacy" id="http2_creation"></a></h6>
15661cb0ef41Sopenharmony_ci<p>On the server side, instances of <a href="#class-serverhttp2stream"><code>ServerHttp2Stream</code></a> are created either
15671cb0ef41Sopenharmony_ciwhen:</p>
15681cb0ef41Sopenharmony_ci<ul>
15691cb0ef41Sopenharmony_ci<li>A new HTTP/2 <code>HEADERS</code> frame with a previously unused stream ID is received;</li>
15701cb0ef41Sopenharmony_ci<li>The <code>http2stream.pushStream()</code> method is called.</li>
15711cb0ef41Sopenharmony_ci</ul>
15721cb0ef41Sopenharmony_ci<p>On the client side, instances of <a href="#class-clienthttp2stream"><code>ClientHttp2Stream</code></a> are created when the
15731cb0ef41Sopenharmony_ci<code>http2session.request()</code> method is called.</p>
15741cb0ef41Sopenharmony_ci<p>On the client, the <code>Http2Stream</code> instance returned by <code>http2session.request()</code>
15751cb0ef41Sopenharmony_cimay not be immediately ready for use if the parent <code>Http2Session</code> has not yet
15761cb0ef41Sopenharmony_cibeen fully established. In such cases, operations called on the <code>Http2Stream</code>
15771cb0ef41Sopenharmony_ciwill be buffered until the <code>'ready'</code> event is emitted. User code should rarely,
15781cb0ef41Sopenharmony_ciif ever, need to handle the <code>'ready'</code> event directly. The ready status of an
15791cb0ef41Sopenharmony_ci<code>Http2Stream</code> can be determined by checking the value of <code>http2stream.id</code>. If
15801cb0ef41Sopenharmony_cithe value is <code>undefined</code>, the stream is not yet ready for use.</p>
15811cb0ef41Sopenharmony_ci<h6>Destruction<span><a class="mark" href="#destruction" id="destruction">#</a></span><a aria-hidden="true" class="legacy" id="http2_destruction"></a></h6>
15821cb0ef41Sopenharmony_ci<p>All <a href="#class-http2stream"><code>Http2Stream</code></a> instances are destroyed either when:</p>
15831cb0ef41Sopenharmony_ci<ul>
15841cb0ef41Sopenharmony_ci<li>An <code>RST_STREAM</code> frame for the stream is received by the connected peer,
15851cb0ef41Sopenharmony_ciand (for client streams only) pending data has been read.</li>
15861cb0ef41Sopenharmony_ci<li>The <code>http2stream.close()</code> method is called, and (for client streams only)
15871cb0ef41Sopenharmony_cipending data has been read.</li>
15881cb0ef41Sopenharmony_ci<li>The <code>http2stream.destroy()</code> or <code>http2session.destroy()</code> methods are called.</li>
15891cb0ef41Sopenharmony_ci</ul>
15901cb0ef41Sopenharmony_ci<p>When an <code>Http2Stream</code> instance is destroyed, an attempt will be made to send an
15911cb0ef41Sopenharmony_ci<code>RST_STREAM</code> frame to the connected peer.</p>
15921cb0ef41Sopenharmony_ci<p>When the <code>Http2Stream</code> instance is destroyed, the <code>'close'</code> event will
15931cb0ef41Sopenharmony_cibe emitted. Because <code>Http2Stream</code> is an instance of <code>stream.Duplex</code>, the
15941cb0ef41Sopenharmony_ci<code>'end'</code> event will also be emitted if the stream data is currently flowing.
15951cb0ef41Sopenharmony_ciThe <code>'error'</code> event may also be emitted if <code>http2stream.destroy()</code> was called
15961cb0ef41Sopenharmony_ciwith an <code>Error</code> passed as the first argument.</p>
15971cb0ef41Sopenharmony_ci<p>After the <code>Http2Stream</code> has been destroyed, the <code>http2stream.destroyed</code>
15981cb0ef41Sopenharmony_ciproperty will be <code>true</code> and the <code>http2stream.rstCode</code> property will specify the
15991cb0ef41Sopenharmony_ci<code>RST_STREAM</code> error code. The <code>Http2Stream</code> instance is no longer usable once
16001cb0ef41Sopenharmony_cidestroyed.</p>
16011cb0ef41Sopenharmony_ci<h5>Event: <code>'aborted'</code><span><a class="mark" href="#event-aborted" id="event-aborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_aborted"></a></h5>
16021cb0ef41Sopenharmony_ci<div class="api_metadata">
16031cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16041cb0ef41Sopenharmony_ci</div>
16051cb0ef41Sopenharmony_ci<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2Stream</code> instance is
16061cb0ef41Sopenharmony_ciabnormally aborted in mid-communication.
16071cb0ef41Sopenharmony_ciIts listener does not expect any arguments.</p>
16081cb0ef41Sopenharmony_ci<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2Stream</code> writable side
16091cb0ef41Sopenharmony_cihas not been ended.</p>
16101cb0ef41Sopenharmony_ci<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_1" id="event-close_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_1"></a></h5>
16111cb0ef41Sopenharmony_ci<div class="api_metadata">
16121cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16131cb0ef41Sopenharmony_ci</div>
16141cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted when the <code>Http2Stream</code> is destroyed. Once
16151cb0ef41Sopenharmony_cithis event is emitted, the <code>Http2Stream</code> instance is no longer usable.</p>
16161cb0ef41Sopenharmony_ci<p>The HTTP/2 error code used when closing the stream can be retrieved using
16171cb0ef41Sopenharmony_cithe <code>http2stream.rstCode</code> property. If the code is any value other than
16181cb0ef41Sopenharmony_ci<code>NGHTTP2_NO_ERROR</code> (<code>0</code>), an <code>'error'</code> event will have also been emitted.</p>
16191cb0ef41Sopenharmony_ci<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error_1" id="event-error_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_error_1"></a></h5>
16201cb0ef41Sopenharmony_ci<div class="api_metadata">
16211cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16221cb0ef41Sopenharmony_ci</div>
16231cb0ef41Sopenharmony_ci<ul>
16241cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
16251cb0ef41Sopenharmony_ci</ul>
16261cb0ef41Sopenharmony_ci<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
16271cb0ef41Sopenharmony_cian <code>Http2Stream</code>.</p>
16281cb0ef41Sopenharmony_ci<h5>Event: <code>'frameError'</code><span><a class="mark" href="#event-frameerror_1" id="event-frameerror_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_frameerror_1"></a></h5>
16291cb0ef41Sopenharmony_ci<div class="api_metadata">
16301cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16311cb0ef41Sopenharmony_ci</div>
16321cb0ef41Sopenharmony_ci<ul>
16331cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The frame type.</li>
16341cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The error code.</li>
16351cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The stream id (or <code>0</code> if the frame isn't associated with a
16361cb0ef41Sopenharmony_cistream).</li>
16371cb0ef41Sopenharmony_ci</ul>
16381cb0ef41Sopenharmony_ci<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
16391cb0ef41Sopenharmony_cisend a frame. When invoked, the handler function will receive an integer
16401cb0ef41Sopenharmony_ciargument identifying the frame type, and an integer argument identifying the
16411cb0ef41Sopenharmony_cierror code. The <code>Http2Stream</code> instance will be destroyed immediately after the
16421cb0ef41Sopenharmony_ci<code>'frameError'</code> event is emitted.</p>
16431cb0ef41Sopenharmony_ci<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready" id="event-ready">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_ready"></a></h5>
16441cb0ef41Sopenharmony_ci<div class="api_metadata">
16451cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16461cb0ef41Sopenharmony_ci</div>
16471cb0ef41Sopenharmony_ci<p>The <code>'ready'</code> event is emitted when the <code>Http2Stream</code> has been opened, has
16481cb0ef41Sopenharmony_cibeen assigned an <code>id</code>, and can be used. The listener does not expect any
16491cb0ef41Sopenharmony_ciarguments.</p>
16501cb0ef41Sopenharmony_ci<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_1" id="event-timeout_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_1"></a></h5>
16511cb0ef41Sopenharmony_ci<div class="api_metadata">
16521cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16531cb0ef41Sopenharmony_ci</div>
16541cb0ef41Sopenharmony_ci<p>The <code>'timeout'</code> event is emitted after no activity is received for this
16551cb0ef41Sopenharmony_ci<code>Http2Stream</code> within the number of milliseconds set using
16561cb0ef41Sopenharmony_ci<code>http2stream.setTimeout()</code>.
16571cb0ef41Sopenharmony_ciIts listener does not expect any arguments.</p>
16581cb0ef41Sopenharmony_ci<h5>Event: <code>'trailers'</code><span><a class="mark" href="#event-trailers" id="event-trailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_trailers"></a></h5>
16591cb0ef41Sopenharmony_ci<div class="api_metadata">
16601cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16611cb0ef41Sopenharmony_ci</div>
16621cb0ef41Sopenharmony_ci<ul>
16631cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
16641cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
16651cb0ef41Sopenharmony_ci</ul>
16661cb0ef41Sopenharmony_ci<p>The <code>'trailers'</code> event is emitted when a block of headers associated with
16671cb0ef41Sopenharmony_citrailing header fields is received. The listener callback is passed the
16681cb0ef41Sopenharmony_ci<a href="#headers-object">HTTP/2 Headers Object</a> and flags associated with the headers.</p>
16691cb0ef41Sopenharmony_ci<p>This event might not be emitted if <code>http2stream.end()</code> is called
16701cb0ef41Sopenharmony_cibefore trailers are received and the incoming data is not being read or
16711cb0ef41Sopenharmony_cilistened for.</p>
16721cb0ef41Sopenharmony_ci<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> {
16731cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
16741cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
16751cb0ef41Sopenharmony_ci<h5>Event: <code>'wantTrailers'</code><span><a class="mark" href="#event-wanttrailers" id="event-wanttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_wanttrailers"></a></h5>
16761cb0ef41Sopenharmony_ci<div class="api_metadata">
16771cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
16781cb0ef41Sopenharmony_ci</div>
16791cb0ef41Sopenharmony_ci<p>The <code>'wantTrailers'</code> event is emitted when the <code>Http2Stream</code> has queued the
16801cb0ef41Sopenharmony_cifinal <code>DATA</code> frame to be sent on a frame and the <code>Http2Stream</code> is ready to send
16811cb0ef41Sopenharmony_citrailing headers. When initiating a request or response, the <code>waitForTrailers</code>
16821cb0ef41Sopenharmony_cioption must be set for this event to be emitted.</p>
16831cb0ef41Sopenharmony_ci<h5><code>http2stream.aborted</code><span><a class="mark" href="#http2streamaborted" id="http2streamaborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_aborted"></a></h5>
16841cb0ef41Sopenharmony_ci<div class="api_metadata">
16851cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
16861cb0ef41Sopenharmony_ci</div>
16871cb0ef41Sopenharmony_ci<ul>
16881cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
16891cb0ef41Sopenharmony_ci</ul>
16901cb0ef41Sopenharmony_ci<p>Set to <code>true</code> if the <code>Http2Stream</code> instance was aborted abnormally. When set,
16911cb0ef41Sopenharmony_cithe <code>'aborted'</code> event will have been emitted.</p>
16921cb0ef41Sopenharmony_ci<h5><code>http2stream.bufferSize</code><span><a class="mark" href="#http2streambuffersize" id="http2streambuffersize">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_buffersize"></a></h5>
16931cb0ef41Sopenharmony_ci<div class="api_metadata">
16941cb0ef41Sopenharmony_ci<span>Added in: v11.2.0, v10.16.0</span>
16951cb0ef41Sopenharmony_ci</div>
16961cb0ef41Sopenharmony_ci<ul>
16971cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
16981cb0ef41Sopenharmony_ci</ul>
16991cb0ef41Sopenharmony_ci<p>This property shows the number of characters currently buffered to be written.
17001cb0ef41Sopenharmony_ciSee <a href="net.html#socketbuffersize"><code>net.Socket.bufferSize</code></a> for details.</p>
17011cb0ef41Sopenharmony_ci<h5><code>http2stream.close(code[, callback])</code><span><a class="mark" href="#http2streamclosecode-callback" id="http2streamclosecode-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_close_code_callback"></a></h5>
17021cb0ef41Sopenharmony_ci<div class="api_metadata">
17031cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
17041cb0ef41Sopenharmony_ci<table>
17051cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
17061cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
17071cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
17081cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
17091cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
17101cb0ef41Sopenharmony_ci</tbody></table>
17111cb0ef41Sopenharmony_ci</details>
17121cb0ef41Sopenharmony_ci</div>
17131cb0ef41Sopenharmony_ci<ul>
17141cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Unsigned 32-bit integer identifying the error code.
17151cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>http2.constants.NGHTTP2_NO_ERROR</code> (<code>0x00</code>).</li>
17161cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional function registered to listen for the
17171cb0ef41Sopenharmony_ci<code>'close'</code> event.</li>
17181cb0ef41Sopenharmony_ci</ul>
17191cb0ef41Sopenharmony_ci<p>Closes the <code>Http2Stream</code> instance by sending an <code>RST_STREAM</code> frame to the
17201cb0ef41Sopenharmony_ciconnected HTTP/2 peer.</p>
17211cb0ef41Sopenharmony_ci<h5><code>http2stream.closed</code><span><a class="mark" href="#http2streamclosed" id="http2streamclosed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_closed"></a></h5>
17221cb0ef41Sopenharmony_ci<div class="api_metadata">
17231cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
17241cb0ef41Sopenharmony_ci</div>
17251cb0ef41Sopenharmony_ci<ul>
17261cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
17271cb0ef41Sopenharmony_ci</ul>
17281cb0ef41Sopenharmony_ci<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been closed.</p>
17291cb0ef41Sopenharmony_ci<h5><code>http2stream.destroyed</code><span><a class="mark" href="#http2streamdestroyed" id="http2streamdestroyed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_destroyed"></a></h5>
17301cb0ef41Sopenharmony_ci<div class="api_metadata">
17311cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
17321cb0ef41Sopenharmony_ci</div>
17331cb0ef41Sopenharmony_ci<ul>
17341cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
17351cb0ef41Sopenharmony_ci</ul>
17361cb0ef41Sopenharmony_ci<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has been destroyed and is no longer
17371cb0ef41Sopenharmony_ciusable.</p>
17381cb0ef41Sopenharmony_ci<h5><code>http2stream.endAfterHeaders</code><span><a class="mark" href="#http2streamendafterheaders" id="http2streamendafterheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_endafterheaders"></a></h5>
17391cb0ef41Sopenharmony_ci<div class="api_metadata">
17401cb0ef41Sopenharmony_ci<span>Added in: v10.11.0</span>
17411cb0ef41Sopenharmony_ci</div>
17421cb0ef41Sopenharmony_ci<ul>
17431cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
17441cb0ef41Sopenharmony_ci</ul>
17451cb0ef41Sopenharmony_ci<p>Set to <code>true</code> if the <code>END_STREAM</code> flag was set in the request or response
17461cb0ef41Sopenharmony_ciHEADERS frame received, indicating that no additional data should be received
17471cb0ef41Sopenharmony_ciand the readable side of the <code>Http2Stream</code> will be closed.</p>
17481cb0ef41Sopenharmony_ci<h5><code>http2stream.id</code><span><a class="mark" href="#http2streamid" id="http2streamid">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_id"></a></h5>
17491cb0ef41Sopenharmony_ci<div class="api_metadata">
17501cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
17511cb0ef41Sopenharmony_ci</div>
17521cb0ef41Sopenharmony_ci<ul>
17531cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
17541cb0ef41Sopenharmony_ci</ul>
17551cb0ef41Sopenharmony_ci<p>The numeric stream identifier of this <code>Http2Stream</code> instance. Set to <code>undefined</code>
17561cb0ef41Sopenharmony_ciif the stream identifier has not yet been assigned.</p>
17571cb0ef41Sopenharmony_ci<h5><code>http2stream.pending</code><span><a class="mark" href="#http2streampending" id="http2streampending">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pending"></a></h5>
17581cb0ef41Sopenharmony_ci<div class="api_metadata">
17591cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span>
17601cb0ef41Sopenharmony_ci</div>
17611cb0ef41Sopenharmony_ci<ul>
17621cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
17631cb0ef41Sopenharmony_ci</ul>
17641cb0ef41Sopenharmony_ci<p>Set to <code>true</code> if the <code>Http2Stream</code> instance has not yet been assigned a
17651cb0ef41Sopenharmony_cinumeric stream identifier.</p>
17661cb0ef41Sopenharmony_ci<h5><code>http2stream.priority(options)</code><span><a class="mark" href="#http2streampriorityoptions" id="http2streampriorityoptions">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_priority_options"></a></h5>
17671cb0ef41Sopenharmony_ci<div class="api_metadata">
17681cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
17691cb0ef41Sopenharmony_ci</div>
17701cb0ef41Sopenharmony_ci<ul>
17711cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
17721cb0ef41Sopenharmony_ci<ul>
17731cb0ef41Sopenharmony_ci<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
17741cb0ef41Sopenharmony_cithis stream is made the sole direct dependency of the parent, with
17751cb0ef41Sopenharmony_ciall other existing dependents made a dependent of this stream. <strong>Default:</strong>
17761cb0ef41Sopenharmony_ci<code>false</code>.</li>
17771cb0ef41Sopenharmony_ci<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream this stream
17781cb0ef41Sopenharmony_ciis dependent on.</li>
17791cb0ef41Sopenharmony_ci<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the relative dependency of a stream in relation
17801cb0ef41Sopenharmony_cito other streams with the same <code>parent</code>. The value is a number between <code>1</code>
17811cb0ef41Sopenharmony_ciand <code>256</code> (inclusive).</li>
17821cb0ef41Sopenharmony_ci<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, changes the priority locally without
17831cb0ef41Sopenharmony_cisending a <code>PRIORITY</code> frame to the connected peer.</li>
17841cb0ef41Sopenharmony_ci</ul>
17851cb0ef41Sopenharmony_ci</li>
17861cb0ef41Sopenharmony_ci</ul>
17871cb0ef41Sopenharmony_ci<p>Updates the priority for this <code>Http2Stream</code> instance.</p>
17881cb0ef41Sopenharmony_ci<h5><code>http2stream.rstCode</code><span><a class="mark" href="#http2streamrstcode" id="http2streamrstcode">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_rstcode"></a></h5>
17891cb0ef41Sopenharmony_ci<div class="api_metadata">
17901cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
17911cb0ef41Sopenharmony_ci</div>
17921cb0ef41Sopenharmony_ci<ul>
17931cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
17941cb0ef41Sopenharmony_ci</ul>
17951cb0ef41Sopenharmony_ci<p>Set to the <code>RST_STREAM</code> <a href="#error-codes-for-rst_stream-and-goaway">error code</a> reported when the <code>Http2Stream</code> is
17961cb0ef41Sopenharmony_cidestroyed after either receiving an <code>RST_STREAM</code> frame from the connected peer,
17971cb0ef41Sopenharmony_cicalling <code>http2stream.close()</code>, or <code>http2stream.destroy()</code>. Will be
17981cb0ef41Sopenharmony_ci<code>undefined</code> if the <code>Http2Stream</code> has not been closed.</p>
17991cb0ef41Sopenharmony_ci<h5><code>http2stream.sentHeaders</code><span><a class="mark" href="#http2streamsentheaders" id="http2streamsentheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sentheaders"></a></h5>
18001cb0ef41Sopenharmony_ci<div class="api_metadata">
18011cb0ef41Sopenharmony_ci<span>Added in: v9.5.0</span>
18021cb0ef41Sopenharmony_ci</div>
18031cb0ef41Sopenharmony_ci<ul>
18041cb0ef41Sopenharmony_ci<li><a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
18051cb0ef41Sopenharmony_ci</ul>
18061cb0ef41Sopenharmony_ci<p>An object containing the outbound headers sent for this <code>Http2Stream</code>.</p>
18071cb0ef41Sopenharmony_ci<h5><code>http2stream.sentInfoHeaders</code><span><a class="mark" href="#http2streamsentinfoheaders" id="http2streamsentinfoheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sentinfoheaders"></a></h5>
18081cb0ef41Sopenharmony_ci<div class="api_metadata">
18091cb0ef41Sopenharmony_ci<span>Added in: v9.5.0</span>
18101cb0ef41Sopenharmony_ci</div>
18111cb0ef41Sopenharmony_ci<ul>
18121cb0ef41Sopenharmony_ci<li><a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object[]></a></li>
18131cb0ef41Sopenharmony_ci</ul>
18141cb0ef41Sopenharmony_ci<p>An array of objects containing the outbound informational (additional) headers
18151cb0ef41Sopenharmony_cisent for this <code>Http2Stream</code>.</p>
18161cb0ef41Sopenharmony_ci<h5><code>http2stream.sentTrailers</code><span><a class="mark" href="#http2streamsenttrailers" id="http2streamsenttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_senttrailers"></a></h5>
18171cb0ef41Sopenharmony_ci<div class="api_metadata">
18181cb0ef41Sopenharmony_ci<span>Added in: v9.5.0</span>
18191cb0ef41Sopenharmony_ci</div>
18201cb0ef41Sopenharmony_ci<ul>
18211cb0ef41Sopenharmony_ci<li><a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
18221cb0ef41Sopenharmony_ci</ul>
18231cb0ef41Sopenharmony_ci<p>An object containing the outbound trailers sent for this <code>HttpStream</code>.</p>
18241cb0ef41Sopenharmony_ci<h5><code>http2stream.session</code><span><a class="mark" href="#http2streamsession" id="http2streamsession">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_session"></a></h5>
18251cb0ef41Sopenharmony_ci<div class="api_metadata">
18261cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
18271cb0ef41Sopenharmony_ci</div>
18281cb0ef41Sopenharmony_ci<ul>
18291cb0ef41Sopenharmony_ci<li><a href="http2.html#class-http2session" class="type">&#x3C;Http2Session></a></li>
18301cb0ef41Sopenharmony_ci</ul>
18311cb0ef41Sopenharmony_ci<p>A reference to the <code>Http2Session</code> instance that owns this <code>Http2Stream</code>. The
18321cb0ef41Sopenharmony_civalue will be <code>undefined</code> after the <code>Http2Stream</code> instance is destroyed.</p>
18331cb0ef41Sopenharmony_ci<h5><code>http2stream.setTimeout(msecs, callback)</code><span><a class="mark" href="#http2streamsettimeoutmsecs-callback" id="http2streamsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_settimeout_msecs_callback"></a></h5>
18341cb0ef41Sopenharmony_ci<div class="api_metadata">
18351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
18361cb0ef41Sopenharmony_ci<table>
18371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
18381cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
18391cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
18401cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
18411cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
18421cb0ef41Sopenharmony_ci</tbody></table>
18431cb0ef41Sopenharmony_ci</details>
18441cb0ef41Sopenharmony_ci</div>
18451cb0ef41Sopenharmony_ci<ul>
18461cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
18471cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
18481cb0ef41Sopenharmony_ci</ul>
18491cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
18501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://example.org:8000'</span>);
18511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_CANCEL } = http2.<span class="hljs-property">constants</span>;
18521cb0ef41Sopenharmony_ci<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> });
18531cb0ef41Sopenharmony_ci
18541cb0ef41Sopenharmony_ci<span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
18551cb0ef41Sopenharmony_cireq.<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>(<span class="hljs-title class_">NGHTTP2</span>_CANCEL));</code> <button class="copy-button">copy</button></pre>
18561cb0ef41Sopenharmony_ci<h5><code>http2stream.state</code><span><a class="mark" href="#http2streamstate" id="http2streamstate">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_state"></a></h5>
18571cb0ef41Sopenharmony_ci<div class="api_metadata">
18581cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
18591cb0ef41Sopenharmony_ci</div>
18601cb0ef41Sopenharmony_ci<p>Provides miscellaneous information about the current state of the
18611cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</p>
18621cb0ef41Sopenharmony_ci<ul>
18631cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
18641cb0ef41Sopenharmony_ci<ul>
18651cb0ef41Sopenharmony_ci<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes the connected peer may send
18661cb0ef41Sopenharmony_cifor this <code>Http2Stream</code> without receiving a <code>WINDOW_UPDATE</code>.</li>
18671cb0ef41Sopenharmony_ci<li><code>state</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A flag indicating the low-level current state of the
18681cb0ef41Sopenharmony_ci<code>Http2Stream</code> as determined by <code>nghttp2</code>.</li>
18691cb0ef41Sopenharmony_ci<li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <code>1</code> if this <code>Http2Stream</code> has been closed locally.</li>
18701cb0ef41Sopenharmony_ci<li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <code>1</code> if this <code>Http2Stream</code> has been closed
18711cb0ef41Sopenharmony_ciremotely.</li>
18721cb0ef41Sopenharmony_ci<li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The sum weight of all <code>Http2Stream</code>
18731cb0ef41Sopenharmony_ciinstances that depend on this <code>Http2Stream</code> as specified using
18741cb0ef41Sopenharmony_ci<code>PRIORITY</code> frames.</li>
18751cb0ef41Sopenharmony_ci<li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The priority weight of this <code>Http2Stream</code>.</li>
18761cb0ef41Sopenharmony_ci</ul>
18771cb0ef41Sopenharmony_ci</li>
18781cb0ef41Sopenharmony_ci</ul>
18791cb0ef41Sopenharmony_ci<p>A current state of this <code>Http2Stream</code>.</p>
18801cb0ef41Sopenharmony_ci<h5><code>http2stream.sendTrailers(headers)</code><span><a class="mark" href="#http2streamsendtrailersheaders" id="http2streamsendtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_sendtrailers_headers"></a></h5>
18811cb0ef41Sopenharmony_ci<div class="api_metadata">
18821cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span>
18831cb0ef41Sopenharmony_ci</div>
18841cb0ef41Sopenharmony_ci<ul>
18851cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
18861cb0ef41Sopenharmony_ci</ul>
18871cb0ef41Sopenharmony_ci<p>Sends a trailing <code>HEADERS</code> frame to the connected HTTP/2 peer. This method
18881cb0ef41Sopenharmony_ciwill cause the <code>Http2Stream</code> to be immediately closed and must only be
18891cb0ef41Sopenharmony_cicalled after the <code>'wantTrailers'</code> event has been emitted. When sending a
18901cb0ef41Sopenharmony_cirequest or sending a response, the <code>options.waitForTrailers</code> option must be set
18911cb0ef41Sopenharmony_ciin order to keep the <code>Http2Stream</code> open after the final <code>DATA</code> frame so that
18921cb0ef41Sopenharmony_citrailers can be sent.</p>
18931cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
18941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
18951cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
18961cb0ef41Sopenharmony_ci  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> });
18971cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
18981cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>: <span class="hljs-string">'abc'</span> });
18991cb0ef41Sopenharmony_ci  });
19001cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span>);
19011cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
19021cb0ef41Sopenharmony_ci<p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
19031cb0ef41Sopenharmony_cifields (e.g. <code>':method'</code>, <code>':path'</code>, etc).</p>
19041cb0ef41Sopenharmony_ci<h4>Class: <code>ClientHttp2Stream</code><span><a class="mark" href="#class-clienthttp2stream" id="class-clienthttp2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_clienthttp2stream"></a></h4>
19051cb0ef41Sopenharmony_ci<div class="api_metadata">
19061cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19071cb0ef41Sopenharmony_ci</div>
19081cb0ef41Sopenharmony_ci<ul>
19091cb0ef41Sopenharmony_ci<li>Extends <a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a></li>
19101cb0ef41Sopenharmony_ci</ul>
19111cb0ef41Sopenharmony_ci<p>The <code>ClientHttp2Stream</code> class is an extension of <code>Http2Stream</code> that is
19121cb0ef41Sopenharmony_ciused exclusively on HTTP/2 Clients. <code>Http2Stream</code> instances on the client
19131cb0ef41Sopenharmony_ciprovide events such as <code>'response'</code> and <code>'push'</code> that are only relevant on
19141cb0ef41Sopenharmony_cithe client.</p>
19151cb0ef41Sopenharmony_ci<h5>Event: <code>'continue'</code><span><a class="mark" href="#event-continue" id="event-continue">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_continue"></a></h5>
19161cb0ef41Sopenharmony_ci<div class="api_metadata">
19171cb0ef41Sopenharmony_ci<span>Added in: v8.5.0</span>
19181cb0ef41Sopenharmony_ci</div>
19191cb0ef41Sopenharmony_ci<p>Emitted when the server sends a <code>100 Continue</code> status, usually because
19201cb0ef41Sopenharmony_cithe request contained <code>Expect: 100-continue</code>. This is an instruction that
19211cb0ef41Sopenharmony_cithe client should send the request body.</p>
19221cb0ef41Sopenharmony_ci<h5>Event: <code>'headers'</code><span><a class="mark" href="#event-headers" id="event-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_headers"></a></h5>
19231cb0ef41Sopenharmony_ci<div class="api_metadata">
19241cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19251cb0ef41Sopenharmony_ci</div>
19261cb0ef41Sopenharmony_ci<ul>
19271cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
19281cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
19291cb0ef41Sopenharmony_ci</ul>
19301cb0ef41Sopenharmony_ci<p>The <code>'headers'</code> event is emitted when an additional block of headers is received
19311cb0ef41Sopenharmony_cifor a stream, such as when a block of <code>1xx</code> informational headers is received.
19321cb0ef41Sopenharmony_ciThe listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and flags
19331cb0ef41Sopenharmony_ciassociated with the headers.</p>
19341cb0ef41Sopenharmony_ci<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> {
19351cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
19361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
19371cb0ef41Sopenharmony_ci<h5>Event: <code>'push'</code><span><a class="mark" href="#event-push" id="event-push">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_push"></a></h5>
19381cb0ef41Sopenharmony_ci<div class="api_metadata">
19391cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19401cb0ef41Sopenharmony_ci</div>
19411cb0ef41Sopenharmony_ci<ul>
19421cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
19431cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
19441cb0ef41Sopenharmony_ci</ul>
19451cb0ef41Sopenharmony_ci<p>The <code>'push'</code> event is emitted when response headers for a Server Push stream
19461cb0ef41Sopenharmony_ciare received. The listener callback is passed the <a href="#headers-object">HTTP/2 Headers Object</a> and
19471cb0ef41Sopenharmony_ciflags associated with the headers.</p>
19481cb0ef41Sopenharmony_ci<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> {
19491cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers);
19501cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
19511cb0ef41Sopenharmony_ci<h5>Event: <code>'response'</code><span><a class="mark" href="#event-response" id="event-response">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_response"></a></h5>
19521cb0ef41Sopenharmony_ci<div class="api_metadata">
19531cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19541cb0ef41Sopenharmony_ci</div>
19551cb0ef41Sopenharmony_ci<ul>
19561cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
19571cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
19581cb0ef41Sopenharmony_ci</ul>
19591cb0ef41Sopenharmony_ci<p>The <code>'response'</code> event is emitted when a response <code>HEADERS</code> frame has been
19601cb0ef41Sopenharmony_cireceived for this stream from the connected HTTP/2 server. The listener is
19611cb0ef41Sopenharmony_ciinvoked with two arguments: an <code>Object</code> containing the received
19621cb0ef41Sopenharmony_ci<a href="#headers-object">HTTP/2 Headers Object</a>, and flags associated with the headers.</p>
19631cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
19641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost'</span>);
19651cb0ef41Sopenharmony_ci<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> });
19661cb0ef41Sopenharmony_cireq.<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> {
19671cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':status'</span>]);
19681cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
19691cb0ef41Sopenharmony_ci<h4>Class: <code>ServerHttp2Stream</code><span><a class="mark" href="#class-serverhttp2stream" id="class-serverhttp2stream">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_serverhttp2stream"></a></h4>
19701cb0ef41Sopenharmony_ci<div class="api_metadata">
19711cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19721cb0ef41Sopenharmony_ci</div>
19731cb0ef41Sopenharmony_ci<ul>
19741cb0ef41Sopenharmony_ci<li>Extends: <a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a></li>
19751cb0ef41Sopenharmony_ci</ul>
19761cb0ef41Sopenharmony_ci<p>The <code>ServerHttp2Stream</code> class is an extension of <a href="#class-http2stream"><code>Http2Stream</code></a> that is
19771cb0ef41Sopenharmony_ciused exclusively on HTTP/2 Servers. <code>Http2Stream</code> instances on the server
19781cb0ef41Sopenharmony_ciprovide additional methods such as <code>http2stream.pushStream()</code> and
19791cb0ef41Sopenharmony_ci<code>http2stream.respond()</code> that are only relevant on the server.</p>
19801cb0ef41Sopenharmony_ci<h5><code>http2stream.additionalHeaders(headers)</code><span><a class="mark" href="#http2streamadditionalheadersheaders" id="http2streamadditionalheadersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_additionalheaders_headers"></a></h5>
19811cb0ef41Sopenharmony_ci<div class="api_metadata">
19821cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19831cb0ef41Sopenharmony_ci</div>
19841cb0ef41Sopenharmony_ci<ul>
19851cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
19861cb0ef41Sopenharmony_ci</ul>
19871cb0ef41Sopenharmony_ci<p>Sends an additional informational <code>HEADERS</code> frame to the connected HTTP/2 peer.</p>
19881cb0ef41Sopenharmony_ci<h5><code>http2stream.headersSent</code><span><a class="mark" href="#http2streamheaderssent" id="http2streamheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_headerssent"></a></h5>
19891cb0ef41Sopenharmony_ci<div class="api_metadata">
19901cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19911cb0ef41Sopenharmony_ci</div>
19921cb0ef41Sopenharmony_ci<ul>
19931cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
19941cb0ef41Sopenharmony_ci</ul>
19951cb0ef41Sopenharmony_ci<p>True if headers were sent, false otherwise (read-only).</p>
19961cb0ef41Sopenharmony_ci<h5><code>http2stream.pushAllowed</code><span><a class="mark" href="#http2streampushallowed" id="http2streampushallowed">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pushallowed"></a></h5>
19971cb0ef41Sopenharmony_ci<div class="api_metadata">
19981cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
19991cb0ef41Sopenharmony_ci</div>
20001cb0ef41Sopenharmony_ci<ul>
20011cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
20021cb0ef41Sopenharmony_ci</ul>
20031cb0ef41Sopenharmony_ci<p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
20041cb0ef41Sopenharmony_ciclient's most recent <code>SETTINGS</code> frame. Will be <code>true</code> if the remote peer
20051cb0ef41Sopenharmony_ciaccepts push streams, <code>false</code> otherwise. Settings are the same for every
20061cb0ef41Sopenharmony_ci<code>Http2Stream</code> in the same <code>Http2Session</code>.</p>
20071cb0ef41Sopenharmony_ci<h5><code>http2stream.pushStream(headers[, options], callback)</code><span><a class="mark" href="#http2streampushstreamheaders-options-callback" id="http2streampushstreamheaders-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_pushstream_headers_options_callback"></a></h5>
20081cb0ef41Sopenharmony_ci<div class="api_metadata">
20091cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
20101cb0ef41Sopenharmony_ci<table>
20111cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
20121cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
20131cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
20141cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
20151cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
20161cb0ef41Sopenharmony_ci</tbody></table>
20171cb0ef41Sopenharmony_ci</details>
20181cb0ef41Sopenharmony_ci</div>
20191cb0ef41Sopenharmony_ci<ul>
20201cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
20211cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
20221cb0ef41Sopenharmony_ci<ul>
20231cb0ef41Sopenharmony_ci<li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code> and <code>parent</code> identifies a parent Stream,
20241cb0ef41Sopenharmony_cithe created stream is made the sole direct dependency of the parent, with
20251cb0ef41Sopenharmony_ciall other existing dependents made a dependent of the newly created stream.
20261cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
20271cb0ef41Sopenharmony_ci<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the numeric identifier of a stream the newly
20281cb0ef41Sopenharmony_cicreated stream is dependent on.</li>
20291cb0ef41Sopenharmony_ci</ul>
20301cb0ef41Sopenharmony_ci</li>
20311cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback that is called once the push stream has been
20321cb0ef41Sopenharmony_ciinitiated.
20331cb0ef41Sopenharmony_ci<ul>
20341cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
20351cb0ef41Sopenharmony_ci<li><code>pushStream</code> <a href="http2.html#class-serverhttp2stream" class="type">&#x3C;ServerHttp2Stream></a> The returned <code>pushStream</code> object.</li>
20361cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> Headers object the <code>pushStream</code> was
20371cb0ef41Sopenharmony_ciinitiated with.</li>
20381cb0ef41Sopenharmony_ci</ul>
20391cb0ef41Sopenharmony_ci</li>
20401cb0ef41Sopenharmony_ci</ul>
20411cb0ef41Sopenharmony_ci<p>Initiates a push stream. The callback is invoked with the new <code>Http2Stream</code>
20421cb0ef41Sopenharmony_ciinstance created for the push stream passed as the second argument, or an
20431cb0ef41Sopenharmony_ci<code>Error</code> passed as the first argument.</p>
20441cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
20451cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
20461cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
20471cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
20481cb0ef41Sopenharmony_ci  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> {
20491cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
20501cb0ef41Sopenharmony_ci    pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
20511cb0ef41Sopenharmony_ci    pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed data'</span>);
20521cb0ef41Sopenharmony_ci  });
20531cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
20541cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
20551cb0ef41Sopenharmony_ci<p>Setting the weight of a push stream is not allowed in the <code>HEADERS</code> frame. Pass
20561cb0ef41Sopenharmony_cia <code>weight</code> value to <code>http2stream.priority</code> with the <code>silent</code> option set to
20571cb0ef41Sopenharmony_ci<code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
20581cb0ef41Sopenharmony_ci<p>Calling <code>http2stream.pushStream()</code> from within a pushed stream is not permitted
20591cb0ef41Sopenharmony_ciand will throw an error.</p>
20601cb0ef41Sopenharmony_ci<h5><code>http2stream.respond([headers[, options]])</code><span><a class="mark" href="#http2streamrespondheaders-options" id="http2streamrespondheaders-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respond_headers_options"></a></h5>
20611cb0ef41Sopenharmony_ci<div class="api_metadata">
20621cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
20631cb0ef41Sopenharmony_ci<table>
20641cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
20651cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td>
20661cb0ef41Sopenharmony_ci<td><p>Allow explicitly setting date headers.</p></td></tr>
20671cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
20681cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
20691cb0ef41Sopenharmony_ci</tbody></table>
20701cb0ef41Sopenharmony_ci</details>
20711cb0ef41Sopenharmony_ci</div>
20721cb0ef41Sopenharmony_ci<ul>
20731cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
20741cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
20751cb0ef41Sopenharmony_ci<ul>
20761cb0ef41Sopenharmony_ci<li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>true</code> to indicate that the response will not
20771cb0ef41Sopenharmony_ciinclude payload data.</li>
20781cb0ef41Sopenharmony_ci<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
20791cb0ef41Sopenharmony_ci<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
20801cb0ef41Sopenharmony_ci</ul>
20811cb0ef41Sopenharmony_ci</li>
20821cb0ef41Sopenharmony_ci</ul>
20831cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
20841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
20851cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
20861cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span> });
20871cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
20881cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
20891cb0ef41Sopenharmony_ci<p>Initiates a response. When the <code>options.waitForTrailers</code> option is set, the
20901cb0ef41Sopenharmony_ci<code>'wantTrailers'</code> event will be emitted immediately after queuing the last chunk
20911cb0ef41Sopenharmony_ciof payload data to be sent. The <code>http2stream.sendTrailers()</code> method can then be
20921cb0ef41Sopenharmony_ciused to sent trailing header fields to the peer.</p>
20931cb0ef41Sopenharmony_ci<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
20941cb0ef41Sopenharmony_ciclose when the final <code>DATA</code> frame is transmitted. User code must call either
20951cb0ef41Sopenharmony_ci<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
20961cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</p>
20971cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
20981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
20991cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
21001cb0ef41Sopenharmony_ci  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> });
21011cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
21021cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
21031cb0ef41Sopenharmony_ci  });
21041cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
21051cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
21061cb0ef41Sopenharmony_ci<h5><code>http2stream.respondWithFD(fd[, headers[, options]])</code><span><a class="mark" href="#http2streamrespondwithfdfd-headers-options" id="http2streamrespondwithfdfd-headers-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respondwithfd_fd_headers_options"></a></h5>
21071cb0ef41Sopenharmony_ci<div class="api_metadata">
21081cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
21091cb0ef41Sopenharmony_ci<table>
21101cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
21111cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td>
21121cb0ef41Sopenharmony_ci<td><p>Allow explicitly setting date headers.</p></td></tr>
21131cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
21141cb0ef41Sopenharmony_ci<td><p>The <code>fd</code> option may now be a <code>FileHandle</code>.</p></td></tr>
21151cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
21161cb0ef41Sopenharmony_ci<td><p>Any readable file descriptor, not necessarily for a regular file, is supported now.</p></td></tr>
21171cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
21181cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
21191cb0ef41Sopenharmony_ci</tbody></table>
21201cb0ef41Sopenharmony_ci</details>
21211cb0ef41Sopenharmony_ci</div>
21221cb0ef41Sopenharmony_ci<ul>
21231cb0ef41Sopenharmony_ci<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="fs.html#class-filehandle" class="type">&#x3C;FileHandle></a> A readable file descriptor.</li>
21241cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
21251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
21261cb0ef41Sopenharmony_ci<ul>
21271cb0ef41Sopenharmony_ci<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
21281cb0ef41Sopenharmony_ci<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
21291cb0ef41Sopenharmony_ci<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
21301cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The offset position at which to begin reading.</li>
21311cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The amount of data from the fd to send.</li>
21321cb0ef41Sopenharmony_ci</ul>
21331cb0ef41Sopenharmony_ci</li>
21341cb0ef41Sopenharmony_ci</ul>
21351cb0ef41Sopenharmony_ci<p>Initiates a response whose data is read from the given file descriptor. No
21361cb0ef41Sopenharmony_civalidation is performed on the given file descriptor. If an error occurs while
21371cb0ef41Sopenharmony_ciattempting to read data using the file descriptor, the <code>Http2Stream</code> will be
21381cb0ef41Sopenharmony_ciclosed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code> code.</p>
21391cb0ef41Sopenharmony_ci<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
21401cb0ef41Sopenharmony_ciautomatically.</p>
21411cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
21421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
21431cb0ef41Sopenharmony_ci
21441cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
21451cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
21461cb0ef41Sopenharmony_ci  <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>);
21471cb0ef41Sopenharmony_ci
21481cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
21491cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> headers = {
21501cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
21511cb0ef41Sopenharmony_ci    <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
21521cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
21531cb0ef41Sopenharmony_ci  };
21541cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
21551cb0ef41Sopenharmony_ci  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));
21561cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
21571cb0ef41Sopenharmony_ci<p>The optional <code>options.statCheck</code> function may be specified to give user code
21581cb0ef41Sopenharmony_cian opportunity to set additional content headers based on the <code>fs.Stat</code> details
21591cb0ef41Sopenharmony_ciof the given fd. If the <code>statCheck</code> function is provided, the
21601cb0ef41Sopenharmony_ci<code>http2stream.respondWithFD()</code> method will perform an <code>fs.fstat()</code> call to
21611cb0ef41Sopenharmony_cicollect details on the provided file descriptor.</p>
21621cb0ef41Sopenharmony_ci<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
21631cb0ef41Sopenharmony_cispecific range subset. This can be used, for instance, to support HTTP Range
21641cb0ef41Sopenharmony_cirequests.</p>
21651cb0ef41Sopenharmony_ci<p>The file descriptor or <code>FileHandle</code> is not closed when the stream is closed,
21661cb0ef41Sopenharmony_ciso it will need to be closed manually once it is no longer needed.
21671cb0ef41Sopenharmony_ciUsing the same file descriptor concurrently for multiple streams
21681cb0ef41Sopenharmony_ciis not supported and may result in data loss. Re-using a file descriptor
21691cb0ef41Sopenharmony_ciafter a stream has finished is supported.</p>
21701cb0ef41Sopenharmony_ci<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
21711cb0ef41Sopenharmony_ciwill be emitted immediately after queuing the last chunk of payload data to be
21721cb0ef41Sopenharmony_cisent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
21731cb0ef41Sopenharmony_ciheader fields to the peer.</p>
21741cb0ef41Sopenharmony_ci<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
21751cb0ef41Sopenharmony_ciclose when the final <code>DATA</code> frame is transmitted. User code <em>must</em> call either
21761cb0ef41Sopenharmony_ci<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
21771cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</p>
21781cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
21791cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
21801cb0ef41Sopenharmony_ci
21811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
21821cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
21831cb0ef41Sopenharmony_ci  <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>);
21841cb0ef41Sopenharmony_ci
21851cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</span>(fd);
21861cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> headers = {
21871cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
21881cb0ef41Sopenharmony_ci    <span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>(),
21891cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
21901cb0ef41Sopenharmony_ci  };
21911cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
21921cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
21931cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
21941cb0ef41Sopenharmony_ci  });
21951cb0ef41Sopenharmony_ci
21961cb0ef41Sopenharmony_ci  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));
21971cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
21981cb0ef41Sopenharmony_ci<h5><code>http2stream.respondWithFile(path[, headers[, options]])</code><span><a class="mark" href="#http2streamrespondwithfilepath-headers-options" id="http2streamrespondwithfilepath-headers-options">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2stream_respondwithfile_path_headers_options"></a></h5>
21991cb0ef41Sopenharmony_ci<div class="api_metadata">
22001cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
22011cb0ef41Sopenharmony_ci<table>
22021cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
22031cb0ef41Sopenharmony_ci<tr><td>v14.5.0, v12.19.0</td>
22041cb0ef41Sopenharmony_ci<td><p>Allow explicitly setting date headers.</p></td></tr>
22051cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
22061cb0ef41Sopenharmony_ci<td><p>Any readable file, not necessarily a regular file, is supported now.</p></td></tr>
22071cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
22081cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
22091cb0ef41Sopenharmony_ci</tbody></table>
22101cb0ef41Sopenharmony_ci</details>
22111cb0ef41Sopenharmony_ci</div>
22121cb0ef41Sopenharmony_ci<ul>
22131cb0ef41Sopenharmony_ci<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a></li>
22141cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a></li>
22151cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
22161cb0ef41Sopenharmony_ci<ul>
22171cb0ef41Sopenharmony_ci<li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
22181cb0ef41Sopenharmony_ci<li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Callback function invoked in the case of an
22191cb0ef41Sopenharmony_cierror before send.</li>
22201cb0ef41Sopenharmony_ci<li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the <code>Http2Stream</code> will emit the
22211cb0ef41Sopenharmony_ci<code>'wantTrailers'</code> event after the final <code>DATA</code> frame has been sent.</li>
22221cb0ef41Sopenharmony_ci<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The offset position at which to begin reading.</li>
22231cb0ef41Sopenharmony_ci<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The amount of data from the fd to send.</li>
22241cb0ef41Sopenharmony_ci</ul>
22251cb0ef41Sopenharmony_ci</li>
22261cb0ef41Sopenharmony_ci</ul>
22271cb0ef41Sopenharmony_ci<p>Sends a regular file as the response. The <code>path</code> must specify a regular file
22281cb0ef41Sopenharmony_cior an <code>'error'</code> event will be emitted on the <code>Http2Stream</code> object.</p>
22291cb0ef41Sopenharmony_ci<p>When used, the <code>Http2Stream</code> object's <code>Duplex</code> interface will be closed
22301cb0ef41Sopenharmony_ciautomatically.</p>
22311cb0ef41Sopenharmony_ci<p>The optional <code>options.statCheck</code> function may be specified to give user code
22321cb0ef41Sopenharmony_cian opportunity to set additional content headers based on the <code>fs.Stat</code> details
22331cb0ef41Sopenharmony_ciof the given file:</p>
22341cb0ef41Sopenharmony_ci<p>If an error occurs while attempting to read the file data, the <code>Http2Stream</code>
22351cb0ef41Sopenharmony_ciwill be closed using an <code>RST_STREAM</code> frame using the standard <code>INTERNAL_ERROR</code>
22361cb0ef41Sopenharmony_cicode. If the <code>onError</code> callback is defined, then it will be called. Otherwise
22371cb0ef41Sopenharmony_cithe stream will be destroyed.</p>
22381cb0ef41Sopenharmony_ci<p>Example using a file path:</p>
22391cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
22401cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
22411cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
22421cb0ef41Sopenharmony_ci  <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
22431cb0ef41Sopenharmony_ci    headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime</span>.<span class="hljs-title function_">toUTCString</span>();
22441cb0ef41Sopenharmony_ci  }
22451cb0ef41Sopenharmony_ci
22461cb0ef41Sopenharmony_ci  <span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span class="hljs-params">err</span>) {
22471cb0ef41Sopenharmony_ci    <span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
22481cb0ef41Sopenharmony_ci    <span class="hljs-comment">// the other side.</span>
22491cb0ef41Sopenharmony_ci    <span class="hljs-keyword">try</span> {
22501cb0ef41Sopenharmony_ci      <span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
22511cb0ef41Sopenharmony_ci        stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">404</span> });
22521cb0ef41Sopenharmony_ci      } <span class="hljs-keyword">else</span> {
22531cb0ef41Sopenharmony_ci        stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">500</span> });
22541cb0ef41Sopenharmony_ci      }
22551cb0ef41Sopenharmony_ci    } <span class="hljs-keyword">catch</span> (err) {
22561cb0ef41Sopenharmony_ci      <span class="hljs-comment">// Perform actual error handling.</span>
22571cb0ef41Sopenharmony_ci      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
22581cb0ef41Sopenharmony_ci    }
22591cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">end</span>();
22601cb0ef41Sopenharmony_ci  }
22611cb0ef41Sopenharmony_ci
22621cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
22631cb0ef41Sopenharmony_ci                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
22641cb0ef41Sopenharmony_ci                         { statCheck, onError });
22651cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
22661cb0ef41Sopenharmony_ci<p>The <code>options.statCheck</code> function may also be used to cancel the send operation
22671cb0ef41Sopenharmony_ciby returning <code>false</code>. For instance, a conditional request may check the stat
22681cb0ef41Sopenharmony_ciresults to determine if the file has been modified to return an appropriate
22691cb0ef41Sopenharmony_ci<code>304</code> response:</p>
22701cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
22711cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
22721cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
22731cb0ef41Sopenharmony_ci  <span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<span class="hljs-params">stat, headers</span>) {
22741cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Check the stat here...</span>
22751cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-number">304</span> });
22761cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// Cancel the send operation</span>
22771cb0ef41Sopenharmony_ci  }
22781cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
22791cb0ef41Sopenharmony_ci                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
22801cb0ef41Sopenharmony_ci                         { statCheck });
22811cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
22821cb0ef41Sopenharmony_ci<p>The <code>content-length</code> header field will be automatically set.</p>
22831cb0ef41Sopenharmony_ci<p>The <code>offset</code> and <code>length</code> options may be used to limit the response to a
22841cb0ef41Sopenharmony_cispecific range subset. This can be used, for instance, to support HTTP Range
22851cb0ef41Sopenharmony_cirequests.</p>
22861cb0ef41Sopenharmony_ci<p>The <code>options.onError</code> function may also be used to handle all the errors
22871cb0ef41Sopenharmony_cithat could happen before the delivery of the file is initiated. The
22881cb0ef41Sopenharmony_cidefault behavior is to destroy the stream.</p>
22891cb0ef41Sopenharmony_ci<p>When the <code>options.waitForTrailers</code> option is set, the <code>'wantTrailers'</code> event
22901cb0ef41Sopenharmony_ciwill be emitted immediately after queuing the last chunk of payload data to be
22911cb0ef41Sopenharmony_cisent. The <code>http2stream.sendTrailers()</code> method can then be used to sent trailing
22921cb0ef41Sopenharmony_ciheader fields to the peer.</p>
22931cb0ef41Sopenharmony_ci<p>When <code>options.waitForTrailers</code> is set, the <code>Http2Stream</code> will not automatically
22941cb0ef41Sopenharmony_ciclose when the final <code>DATA</code> frame is transmitted. User code must call either
22951cb0ef41Sopenharmony_ci<code>http2stream.sendTrailers()</code> or <code>http2stream.close()</code> to close the
22961cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</p>
22971cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
22981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
22991cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
23001cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/file'</span>,
23011cb0ef41Sopenharmony_ci                         { <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span> },
23021cb0ef41Sopenharmony_ci                         { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
23031cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span>, <span class="hljs-function">() =></span> {
23041cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>: <span class="hljs-string">'some value to send'</span> });
23051cb0ef41Sopenharmony_ci  });
23061cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
23071cb0ef41Sopenharmony_ci<h4>Class: <code>Http2Server</code><span><a class="mark" href="#class-http2server" id="class-http2server">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2server"></a></h4>
23081cb0ef41Sopenharmony_ci<div class="api_metadata">
23091cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23101cb0ef41Sopenharmony_ci</div>
23111cb0ef41Sopenharmony_ci<ul>
23121cb0ef41Sopenharmony_ci<li>Extends: <a href="net.html#class-netserver" class="type">&#x3C;net.Server></a></li>
23131cb0ef41Sopenharmony_ci</ul>
23141cb0ef41Sopenharmony_ci<p>Instances of <code>Http2Server</code> are created using the <code>http2.createServer()</code>
23151cb0ef41Sopenharmony_cifunction. The <code>Http2Server</code> class is not exported directly by the
23161cb0ef41Sopenharmony_ci<code>node:http2</code> module.</p>
23171cb0ef41Sopenharmony_ci<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#event-checkcontinue" id="event-checkcontinue">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_checkcontinue"></a></h5>
23181cb0ef41Sopenharmony_ci<div class="api_metadata">
23191cb0ef41Sopenharmony_ci<span>Added in: v8.5.0</span>
23201cb0ef41Sopenharmony_ci</div>
23211cb0ef41Sopenharmony_ci<ul>
23221cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
23231cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
23241cb0ef41Sopenharmony_ci</ul>
23251cb0ef41Sopenharmony_ci<p>If a <a href="#event-request"><code>'request'</code></a> listener is registered or <a href="#http2createserveroptions-onrequesthandler"><code>http2.createServer()</code></a> is
23261cb0ef41Sopenharmony_cisupplied a callback function, the <code>'checkContinue'</code> event is emitted each time
23271cb0ef41Sopenharmony_cia request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
23281cb0ef41Sopenharmony_cinot listened for, the server will automatically respond with a status
23291cb0ef41Sopenharmony_ci<code>100 Continue</code> as appropriate.</p>
23301cb0ef41Sopenharmony_ci<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the
23311cb0ef41Sopenharmony_ciclient should continue to send the request body, or generating an appropriate
23321cb0ef41Sopenharmony_ciHTTP response (e.g. 400 Bad Request) if the client should not continue to send
23331cb0ef41Sopenharmony_cithe request body.</p>
23341cb0ef41Sopenharmony_ci<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will
23351cb0ef41Sopenharmony_cinot be emitted.</p>
23361cb0ef41Sopenharmony_ci<h5>Event: <code>'connection'</code><span><a class="mark" href="#event-connection" id="event-connection">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connection"></a></h5>
23371cb0ef41Sopenharmony_ci<div class="api_metadata">
23381cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23391cb0ef41Sopenharmony_ci</div>
23401cb0ef41Sopenharmony_ci<ul>
23411cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type">&#x3C;stream.Duplex></a></li>
23421cb0ef41Sopenharmony_ci</ul>
23431cb0ef41Sopenharmony_ci<p>This event is emitted when a new TCP stream is established. <code>socket</code> is
23441cb0ef41Sopenharmony_citypically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>. Usually users will not want to
23451cb0ef41Sopenharmony_ciaccess this event.</p>
23461cb0ef41Sopenharmony_ci<p>This event can also be explicitly emitted by users to inject connections
23471cb0ef41Sopenharmony_ciinto the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p>
23481cb0ef41Sopenharmony_ci<h5>Event: <code>'request'</code><span><a class="mark" href="#event-request" id="event-request">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_request"></a></h5>
23491cb0ef41Sopenharmony_ci<div class="api_metadata">
23501cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23511cb0ef41Sopenharmony_ci</div>
23521cb0ef41Sopenharmony_ci<ul>
23531cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
23541cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
23551cb0ef41Sopenharmony_ci</ul>
23561cb0ef41Sopenharmony_ci<p>Emitted each time there is a request. There may be multiple requests
23571cb0ef41Sopenharmony_ciper session. See the <a href="#compatibility-api">Compatibility API</a>.</p>
23581cb0ef41Sopenharmony_ci<h5>Event: <code>'session'</code><span><a class="mark" href="#event-session" id="event-session">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_session"></a></h5>
23591cb0ef41Sopenharmony_ci<div class="api_metadata">
23601cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23611cb0ef41Sopenharmony_ci</div>
23621cb0ef41Sopenharmony_ci<ul>
23631cb0ef41Sopenharmony_ci<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type">&#x3C;ServerHttp2Session></a></li>
23641cb0ef41Sopenharmony_ci</ul>
23651cb0ef41Sopenharmony_ci<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
23661cb0ef41Sopenharmony_ci<code>Http2Server</code>.</p>
23671cb0ef41Sopenharmony_ci<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#event-sessionerror" id="event-sessionerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_sessionerror"></a></h5>
23681cb0ef41Sopenharmony_ci<div class="api_metadata">
23691cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23701cb0ef41Sopenharmony_ci</div>
23711cb0ef41Sopenharmony_ci<ul>
23721cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
23731cb0ef41Sopenharmony_ci<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type">&#x3C;ServerHttp2Session></a></li>
23741cb0ef41Sopenharmony_ci</ul>
23751cb0ef41Sopenharmony_ci<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
23761cb0ef41Sopenharmony_cian <code>Http2Session</code> object associated with the <code>Http2Server</code>.</p>
23771cb0ef41Sopenharmony_ci<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream_1" id="event-stream_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream_1"></a></h5>
23781cb0ef41Sopenharmony_ci<div class="api_metadata">
23791cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
23801cb0ef41Sopenharmony_ci</div>
23811cb0ef41Sopenharmony_ci<ul>
23821cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
23831cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
23841cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
23851cb0ef41Sopenharmony_ci<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
23861cb0ef41Sopenharmony_citheir respective values.</li>
23871cb0ef41Sopenharmony_ci</ul>
23881cb0ef41Sopenharmony_ci<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
23891cb0ef41Sopenharmony_cian <code>Http2Session</code> associated with the server.</p>
23901cb0ef41Sopenharmony_ci<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
23911cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
23921cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
23931cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
23941cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
23951cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
23961cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
23971cb0ef41Sopenharmony_ci} = http2.<span class="hljs-property">constants</span>;
23981cb0ef41Sopenharmony_ci
23991cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
24001cb0ef41Sopenharmony_ciserver.<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> {
24011cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
24021cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
24031cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
24041cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
24051cb0ef41Sopenharmony_ci    [<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
24061cb0ef41Sopenharmony_ci    [<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
24071cb0ef41Sopenharmony_ci  });
24081cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
24091cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
24101cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
24111cb0ef41Sopenharmony_ci<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_2" id="event-timeout_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_2"></a></h5>
24121cb0ef41Sopenharmony_ci<div class="api_metadata">
24131cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
24141cb0ef41Sopenharmony_ci<table>
24151cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
24161cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
24171cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
24181cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
24191cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
24201cb0ef41Sopenharmony_ci</tbody></table>
24211cb0ef41Sopenharmony_ci</details>
24221cb0ef41Sopenharmony_ci</div>
24231cb0ef41Sopenharmony_ci<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
24241cb0ef41Sopenharmony_cia given number of milliseconds set using <code>http2server.setTimeout()</code>.
24251cb0ef41Sopenharmony_ci<strong>Default:</strong> 0 (no timeout)</p>
24261cb0ef41Sopenharmony_ci<h5><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback" id="serverclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_close_callback"></a></h5>
24271cb0ef41Sopenharmony_ci<div class="api_metadata">
24281cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
24291cb0ef41Sopenharmony_ci</div>
24301cb0ef41Sopenharmony_ci<ul>
24311cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
24321cb0ef41Sopenharmony_ci</ul>
24331cb0ef41Sopenharmony_ci<p>Stops the server from establishing new sessions. This does not prevent new
24341cb0ef41Sopenharmony_cirequest streams from being created due to the persistent nature of HTTP/2
24351cb0ef41Sopenharmony_cisessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on
24361cb0ef41Sopenharmony_ciall active sessions.</p>
24371cb0ef41Sopenharmony_ci<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
24381cb0ef41Sopenharmony_ciclosed, although the server has already stopped allowing new sessions. See
24391cb0ef41Sopenharmony_ci<a href="net.html#serverclosecallback"><code>net.Server.close()</code></a> for more details.</p>
24401cb0ef41Sopenharmony_ci<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#serversettimeoutmsecs-callback" id="serversettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_settimeout_msecs_callback"></a></h5>
24411cb0ef41Sopenharmony_ci<div class="api_metadata">
24421cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
24431cb0ef41Sopenharmony_ci<table>
24441cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
24451cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
24461cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
24471cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
24481cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
24491cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
24501cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
24511cb0ef41Sopenharmony_ci</tbody></table>
24521cb0ef41Sopenharmony_ci</details>
24531cb0ef41Sopenharmony_ci</div>
24541cb0ef41Sopenharmony_ci<ul>
24551cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> 0 (no timeout)</li>
24561cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
24571cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2server" class="type">&#x3C;Http2Server></a></li>
24581cb0ef41Sopenharmony_ci</ul>
24591cb0ef41Sopenharmony_ci<p>Used to set the timeout value for http2 server requests,
24601cb0ef41Sopenharmony_ciand sets a callback function that is called when there is no activity
24611cb0ef41Sopenharmony_cion the <code>Http2Server</code> after <code>msecs</code> milliseconds.</p>
24621cb0ef41Sopenharmony_ci<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
24631cb0ef41Sopenharmony_ci<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code>
24641cb0ef41Sopenharmony_cierror will be thrown.</p>
24651cb0ef41Sopenharmony_ci<h5><code>server.timeout</code><span><a class="mark" href="#servertimeout" id="servertimeout">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_timeout"></a></h5>
24661cb0ef41Sopenharmony_ci<div class="api_metadata">
24671cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
24681cb0ef41Sopenharmony_ci<table>
24691cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
24701cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
24711cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
24721cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
24731cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
24741cb0ef41Sopenharmony_ci</tbody></table>
24751cb0ef41Sopenharmony_ci</details>
24761cb0ef41Sopenharmony_ci</div>
24771cb0ef41Sopenharmony_ci<ul>
24781cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li>
24791cb0ef41Sopenharmony_ci</ul>
24801cb0ef41Sopenharmony_ci<p>The number of milliseconds of inactivity before a socket is presumed
24811cb0ef41Sopenharmony_cito have timed out.</p>
24821cb0ef41Sopenharmony_ci<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
24831cb0ef41Sopenharmony_ci<p>The socket timeout logic is set up on connection, so changing this
24841cb0ef41Sopenharmony_civalue only affects new connections to the server, not any existing connections.</p>
24851cb0ef41Sopenharmony_ci<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#serverupdatesettingssettings" id="serverupdatesettingssettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_updatesettings_settings"></a></h5>
24861cb0ef41Sopenharmony_ci<div class="api_metadata">
24871cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.17.0</span>
24881cb0ef41Sopenharmony_ci</div>
24891cb0ef41Sopenharmony_ci<ul>
24901cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
24911cb0ef41Sopenharmony_ci</ul>
24921cb0ef41Sopenharmony_ci<p>Used to update the server with the provided settings.</p>
24931cb0ef41Sopenharmony_ci<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
24941cb0ef41Sopenharmony_ci<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
24951cb0ef41Sopenharmony_ci<h4>Class: <code>Http2SecureServer</code><span><a class="mark" href="#class-http2secureserver" id="class-http2secureserver">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2secureserver"></a></h4>
24961cb0ef41Sopenharmony_ci<div class="api_metadata">
24971cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
24981cb0ef41Sopenharmony_ci</div>
24991cb0ef41Sopenharmony_ci<ul>
25001cb0ef41Sopenharmony_ci<li>Extends: <a href="tls.html#class-tlsserver" class="type">&#x3C;tls.Server></a></li>
25011cb0ef41Sopenharmony_ci</ul>
25021cb0ef41Sopenharmony_ci<p>Instances of <code>Http2SecureServer</code> are created using the
25031cb0ef41Sopenharmony_ci<code>http2.createSecureServer()</code> function. The <code>Http2SecureServer</code> class is not
25041cb0ef41Sopenharmony_ciexported directly by the <code>node:http2</code> module.</p>
25051cb0ef41Sopenharmony_ci<h5>Event: <code>'checkContinue'</code><span><a class="mark" href="#event-checkcontinue_1" id="event-checkcontinue_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_checkcontinue_1"></a></h5>
25061cb0ef41Sopenharmony_ci<div class="api_metadata">
25071cb0ef41Sopenharmony_ci<span>Added in: v8.5.0</span>
25081cb0ef41Sopenharmony_ci</div>
25091cb0ef41Sopenharmony_ci<ul>
25101cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
25111cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
25121cb0ef41Sopenharmony_ci</ul>
25131cb0ef41Sopenharmony_ci<p>If a <a href="#event-request"><code>'request'</code></a> listener is registered or <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a>
25141cb0ef41Sopenharmony_ciis supplied a callback function, the <code>'checkContinue'</code> event is emitted each
25151cb0ef41Sopenharmony_citime a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
25161cb0ef41Sopenharmony_ciis not listened for, the server will automatically respond with a status
25171cb0ef41Sopenharmony_ci<code>100 Continue</code> as appropriate.</p>
25181cb0ef41Sopenharmony_ci<p>Handling this event involves calling <a href="#responsewritecontinue"><code>response.writeContinue()</code></a> if the
25191cb0ef41Sopenharmony_ciclient should continue to send the request body, or generating an appropriate
25201cb0ef41Sopenharmony_ciHTTP response (e.g. 400 Bad Request) if the client should not continue to send
25211cb0ef41Sopenharmony_cithe request body.</p>
25221cb0ef41Sopenharmony_ci<p>When this event is emitted and handled, the <a href="#event-request"><code>'request'</code></a> event will
25231cb0ef41Sopenharmony_cinot be emitted.</p>
25241cb0ef41Sopenharmony_ci<h5>Event: <code>'connection'</code><span><a class="mark" href="#event-connection_1" id="event-connection_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_connection_1"></a></h5>
25251cb0ef41Sopenharmony_ci<div class="api_metadata">
25261cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
25271cb0ef41Sopenharmony_ci</div>
25281cb0ef41Sopenharmony_ci<ul>
25291cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type">&#x3C;stream.Duplex></a></li>
25301cb0ef41Sopenharmony_ci</ul>
25311cb0ef41Sopenharmony_ci<p>This event is emitted when a new TCP stream is established, before the TLS
25321cb0ef41Sopenharmony_cihandshake begins. <code>socket</code> is typically an object of type <a href="net.html#class-netsocket"><code>net.Socket</code></a>.
25331cb0ef41Sopenharmony_ciUsually users will not want to access this event.</p>
25341cb0ef41Sopenharmony_ci<p>This event can also be explicitly emitted by users to inject connections
25351cb0ef41Sopenharmony_ciinto the HTTP server. In that case, any <a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream can be passed.</p>
25361cb0ef41Sopenharmony_ci<h5>Event: <code>'request'</code><span><a class="mark" href="#event-request_1" id="event-request_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_request_1"></a></h5>
25371cb0ef41Sopenharmony_ci<div class="api_metadata">
25381cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
25391cb0ef41Sopenharmony_ci</div>
25401cb0ef41Sopenharmony_ci<ul>
25411cb0ef41Sopenharmony_ci<li><code>request</code> <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
25421cb0ef41Sopenharmony_ci<li><code>response</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
25431cb0ef41Sopenharmony_ci</ul>
25441cb0ef41Sopenharmony_ci<p>Emitted each time there is a request. There may be multiple requests
25451cb0ef41Sopenharmony_ciper session. See the <a href="#compatibility-api">Compatibility API</a>.</p>
25461cb0ef41Sopenharmony_ci<h5>Event: <code>'session'</code><span><a class="mark" href="#event-session_1" id="event-session_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_session_1"></a></h5>
25471cb0ef41Sopenharmony_ci<div class="api_metadata">
25481cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
25491cb0ef41Sopenharmony_ci</div>
25501cb0ef41Sopenharmony_ci<ul>
25511cb0ef41Sopenharmony_ci<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type">&#x3C;ServerHttp2Session></a></li>
25521cb0ef41Sopenharmony_ci</ul>
25531cb0ef41Sopenharmony_ci<p>The <code>'session'</code> event is emitted when a new <code>Http2Session</code> is created by the
25541cb0ef41Sopenharmony_ci<code>Http2SecureServer</code>.</p>
25551cb0ef41Sopenharmony_ci<h5>Event: <code>'sessionError'</code><span><a class="mark" href="#event-sessionerror_1" id="event-sessionerror_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_sessionerror_1"></a></h5>
25561cb0ef41Sopenharmony_ci<div class="api_metadata">
25571cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
25581cb0ef41Sopenharmony_ci</div>
25591cb0ef41Sopenharmony_ci<ul>
25601cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
25611cb0ef41Sopenharmony_ci<li><code>session</code> <a href="http2.html#class-serverhttp2session" class="type">&#x3C;ServerHttp2Session></a></li>
25621cb0ef41Sopenharmony_ci</ul>
25631cb0ef41Sopenharmony_ci<p>The <code>'sessionError'</code> event is emitted when an <code>'error'</code> event is emitted by
25641cb0ef41Sopenharmony_cian <code>Http2Session</code> object associated with the <code>Http2SecureServer</code>.</p>
25651cb0ef41Sopenharmony_ci<h5>Event: <code>'stream'</code><span><a class="mark" href="#event-stream_2" id="event-stream_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_stream_2"></a></h5>
25661cb0ef41Sopenharmony_ci<div class="api_metadata">
25671cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
25681cb0ef41Sopenharmony_ci</div>
25691cb0ef41Sopenharmony_ci<ul>
25701cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a> A reference to the stream</li>
25711cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
25721cb0ef41Sopenharmony_ci<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The associated numeric flags</li>
25731cb0ef41Sopenharmony_ci<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> An array containing the raw header names followed by
25741cb0ef41Sopenharmony_citheir respective values.</li>
25751cb0ef41Sopenharmony_ci</ul>
25761cb0ef41Sopenharmony_ci<p>The <code>'stream'</code> event is emitted when a <code>'stream'</code> event has been emitted by
25771cb0ef41Sopenharmony_cian <code>Http2Session</code> associated with the server.</p>
25781cb0ef41Sopenharmony_ci<p>See also <a href="#event-stream"><code>Http2Session</code>'s <code>'stream'</code> event</a>.</p>
25791cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
25801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> {
25811cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_METHOD,
25821cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_PATH,
25831cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_STATUS,
25841cb0ef41Sopenharmony_ci  <span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE,
25851cb0ef41Sopenharmony_ci} = http2.<span class="hljs-property">constants</span>;
25861cb0ef41Sopenharmony_ci
25871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSomehow</span>();
25881cb0ef41Sopenharmony_ci
25891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
25901cb0ef41Sopenharmony_ciserver.<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> {
25911cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> method = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_METHOD];
25921cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> path = headers[<span class="hljs-title class_">HTTP2</span>_HEADER_PATH];
25931cb0ef41Sopenharmony_ci  <span class="hljs-comment">// ...</span>
25941cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
25951cb0ef41Sopenharmony_ci    [<span class="hljs-title class_">HTTP2</span>_HEADER_STATUS]: <span class="hljs-number">200</span>,
25961cb0ef41Sopenharmony_ci    [<span class="hljs-title class_">HTTP2</span>_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
25971cb0ef41Sopenharmony_ci  });
25981cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
25991cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
26001cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
26011cb0ef41Sopenharmony_ci<h5>Event: <code>'timeout'</code><span><a class="mark" href="#event-timeout_3" id="event-timeout_3">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_timeout_3"></a></h5>
26021cb0ef41Sopenharmony_ci<div class="api_metadata">
26031cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
26041cb0ef41Sopenharmony_ci</div>
26051cb0ef41Sopenharmony_ci<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
26061cb0ef41Sopenharmony_cia given number of milliseconds set using <code>http2secureServer.setTimeout()</code>.
26071cb0ef41Sopenharmony_ci<strong>Default:</strong> 2 minutes.</p>
26081cb0ef41Sopenharmony_ci<h5>Event: <code>'unknownProtocol'</code><span><a class="mark" href="#event-unknownprotocol" id="event-unknownprotocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_unknownprotocol"></a></h5>
26091cb0ef41Sopenharmony_ci<div class="api_metadata">
26101cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
26111cb0ef41Sopenharmony_ci</div>
26121cb0ef41Sopenharmony_ci<ul>
26131cb0ef41Sopenharmony_ci<li><code>socket</code> <a href="stream.html#class-streamduplex" class="type">&#x3C;stream.Duplex></a></li>
26141cb0ef41Sopenharmony_ci</ul>
26151cb0ef41Sopenharmony_ci<p>The <code>'unknownProtocol'</code> event is emitted when a connecting client fails to
26161cb0ef41Sopenharmony_cinegotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler
26171cb0ef41Sopenharmony_cireceives the socket for handling. If no listener is registered for this event,
26181cb0ef41Sopenharmony_cithe connection is terminated. A timeout may be specified using the
26191cb0ef41Sopenharmony_ci<code>'unknownProtocolTimeout'</code> option passed to <a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a>.
26201cb0ef41Sopenharmony_ciSee the <a href="#compatibility-api">Compatibility API</a>.</p>
26211cb0ef41Sopenharmony_ci<h5><code>server.close([callback])</code><span><a class="mark" href="#serverclosecallback_1" id="serverclosecallback_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_close_callback_1"></a></h5>
26221cb0ef41Sopenharmony_ci<div class="api_metadata">
26231cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
26241cb0ef41Sopenharmony_ci</div>
26251cb0ef41Sopenharmony_ci<ul>
26261cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
26271cb0ef41Sopenharmony_ci</ul>
26281cb0ef41Sopenharmony_ci<p>Stops the server from establishing new sessions. This does not prevent new
26291cb0ef41Sopenharmony_cirequest streams from being created due to the persistent nature of HTTP/2
26301cb0ef41Sopenharmony_cisessions. To gracefully shut down the server, call <a href="#http2sessionclosecallback"><code>http2session.close()</code></a> on
26311cb0ef41Sopenharmony_ciall active sessions.</p>
26321cb0ef41Sopenharmony_ci<p>If <code>callback</code> is provided, it is not invoked until all active sessions have been
26331cb0ef41Sopenharmony_ciclosed, although the server has already stopped allowing new sessions. See
26341cb0ef41Sopenharmony_ci<a href="tls.html#serverclosecallback"><code>tls.Server.close()</code></a> for more details.</p>
26351cb0ef41Sopenharmony_ci<h5><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#serversettimeoutmsecs-callback_1" id="serversettimeoutmsecs-callback_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_settimeout_msecs_callback_1"></a></h5>
26361cb0ef41Sopenharmony_ci<div class="api_metadata">
26371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
26381cb0ef41Sopenharmony_ci<table>
26391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
26401cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
26411cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
26421cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
26431cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
26441cb0ef41Sopenharmony_ci</tbody></table>
26451cb0ef41Sopenharmony_ci</details>
26461cb0ef41Sopenharmony_ci</div>
26471cb0ef41Sopenharmony_ci<ul>
26481cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <strong>Default:</strong> <code>120000</code> (2 minutes)</li>
26491cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
26501cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2secureserver" class="type">&#x3C;Http2SecureServer></a></li>
26511cb0ef41Sopenharmony_ci</ul>
26521cb0ef41Sopenharmony_ci<p>Used to set the timeout value for http2 secure server requests,
26531cb0ef41Sopenharmony_ciand sets a callback function that is called when there is no activity
26541cb0ef41Sopenharmony_cion the <code>Http2SecureServer</code> after <code>msecs</code> milliseconds.</p>
26551cb0ef41Sopenharmony_ci<p>The given callback is registered as a listener on the <code>'timeout'</code> event.</p>
26561cb0ef41Sopenharmony_ci<p>In case if <code>callback</code> is not a function, a new <code>ERR_INVALID_ARG_TYPE</code>
26571cb0ef41Sopenharmony_cierror will be thrown.</p>
26581cb0ef41Sopenharmony_ci<h5><code>server.timeout</code><span><a class="mark" href="#servertimeout_1" id="servertimeout_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_timeout_1"></a></h5>
26591cb0ef41Sopenharmony_ci<div class="api_metadata">
26601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
26611cb0ef41Sopenharmony_ci<table>
26621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
26631cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
26641cb0ef41Sopenharmony_ci<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr>
26651cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
26661cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
26671cb0ef41Sopenharmony_ci</tbody></table>
26681cb0ef41Sopenharmony_ci</details>
26691cb0ef41Sopenharmony_ci</div>
26701cb0ef41Sopenharmony_ci<ul>
26711cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Timeout in milliseconds. <strong>Default:</strong> 0 (no timeout)</li>
26721cb0ef41Sopenharmony_ci</ul>
26731cb0ef41Sopenharmony_ci<p>The number of milliseconds of inactivity before a socket is presumed
26741cb0ef41Sopenharmony_cito have timed out.</p>
26751cb0ef41Sopenharmony_ci<p>A value of <code>0</code> will disable the timeout behavior on incoming connections.</p>
26761cb0ef41Sopenharmony_ci<p>The socket timeout logic is set up on connection, so changing this
26771cb0ef41Sopenharmony_civalue only affects new connections to the server, not any existing connections.</p>
26781cb0ef41Sopenharmony_ci<h5><code>server.updateSettings([settings])</code><span><a class="mark" href="#serverupdatesettingssettings_1" id="serverupdatesettingssettings_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_server_updatesettings_settings_1"></a></h5>
26791cb0ef41Sopenharmony_ci<div class="api_metadata">
26801cb0ef41Sopenharmony_ci<span>Added in: v15.1.0, v14.17.0</span>
26811cb0ef41Sopenharmony_ci</div>
26821cb0ef41Sopenharmony_ci<ul>
26831cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
26841cb0ef41Sopenharmony_ci</ul>
26851cb0ef41Sopenharmony_ci<p>Used to update the server with the provided settings.</p>
26861cb0ef41Sopenharmony_ci<p>Throws <code>ERR_HTTP2_INVALID_SETTING_VALUE</code> for invalid <code>settings</code> values.</p>
26871cb0ef41Sopenharmony_ci<p>Throws <code>ERR_INVALID_ARG_TYPE</code> for invalid <code>settings</code> argument.</p>
26881cb0ef41Sopenharmony_ci<h4><code>http2.createServer([options][, onRequestHandler])</code><span><a class="mark" href="#http2createserveroptions-onrequesthandler" id="http2createserveroptions-onrequesthandler">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_createserver_options_onrequesthandler"></a></h4>
26891cb0ef41Sopenharmony_ci<div class="api_metadata">
26901cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
26911cb0ef41Sopenharmony_ci<table>
26921cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
26931cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
26941cb0ef41Sopenharmony_ci<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>
26951cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
26961cb0ef41Sopenharmony_ci<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
26971cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
26981cb0ef41Sopenharmony_ci<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
26991cb0ef41Sopenharmony_ci<tr><td>v12.4.0</td>
27001cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter now supports <code>net.createServer()</code> options.</p></td></tr>
27011cb0ef41Sopenharmony_ci<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
27021cb0ef41Sopenharmony_ci<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
27031cb0ef41Sopenharmony_ci<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
27041cb0ef41Sopenharmony_ci<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
27051cb0ef41Sopenharmony_ci<tr><td>v9.6.0</td>
27061cb0ef41Sopenharmony_ci<td><p>Added the <code>Http1IncomingMessage</code> and <code>Http1ServerResponse</code> option.</p></td></tr>
27071cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
27081cb0ef41Sopenharmony_ci<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
27091cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
27101cb0ef41Sopenharmony_ci<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
27111cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
27121cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
27131cb0ef41Sopenharmony_ci</tbody></table>
27141cb0ef41Sopenharmony_ci</details>
27151cb0ef41Sopenharmony_ci</div>
27161cb0ef41Sopenharmony_ci<ul>
27171cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
27181cb0ef41Sopenharmony_ci<ul>
27191cb0ef41Sopenharmony_ci<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
27201cb0ef41Sopenharmony_cifor deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
27211cb0ef41Sopenharmony_ci<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
27221cb0ef41Sopenharmony_ci<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
27231cb0ef41Sopenharmony_ci<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
27241cb0ef41Sopenharmony_ciis permitted to use. The value is expressed in terms of number of megabytes,
27251cb0ef41Sopenharmony_cie.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
27261cb0ef41Sopenharmony_ciThis is a credit based limit, existing <code>Http2Stream</code>s may cause this
27271cb0ef41Sopenharmony_cilimit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
27281cb0ef41Sopenharmony_ciwhile this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
27291cb0ef41Sopenharmony_cithe current memory use of the header compression tables, current data
27301cb0ef41Sopenharmony_ciqueued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
27311cb0ef41Sopenharmony_cicounted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
27321cb0ef41Sopenharmony_ci<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
27331cb0ef41Sopenharmony_ciThis is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
27341cb0ef41Sopenharmony_ci<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
27351cb0ef41Sopenharmony_ciis <code>4</code>. <strong>Default:</strong> <code>128</code>.</li>
27361cb0ef41Sopenharmony_ci<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
27371cb0ef41Sopenharmony_ciunacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
27381cb0ef41Sopenharmony_ci<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
27391cb0ef41Sopenharmony_ciserialized, compressed block of headers. Attempts to send headers that
27401cb0ef41Sopenharmony_ciexceed this limit will result in a <code>'frameError'</code> event being emitted
27411cb0ef41Sopenharmony_ciand the stream being closed and destroyed.
27421cb0ef41Sopenharmony_ciWhile this sets the maximum allowed size to the entire block of headers,
27431cb0ef41Sopenharmony_ci<code>nghttp2</code> (the internal http2 library) has a limit of <code>65536</code>
27441cb0ef41Sopenharmony_cifor each decompressed key/value pair.</li>
27451cb0ef41Sopenharmony_ci<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The strategy used for determining the amount of
27461cb0ef41Sopenharmony_cipadding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
27471cb0ef41Sopenharmony_ci<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
27481cb0ef41Sopenharmony_ci<ul>
27491cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
27501cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
27511cb0ef41Sopenharmony_cidetermined by the internal implementation, is applied.</li>
27521cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
27531cb0ef41Sopenharmony_cipadding to ensure that the total frame length, including the 9-byte
27541cb0ef41Sopenharmony_ciheader, is a multiple of 8. For each frame, there is a maximum allowed
27551cb0ef41Sopenharmony_cinumber of padding bytes that is determined by current flow control state
27561cb0ef41Sopenharmony_ciand settings. If this maximum is less than the calculated amount needed to
27571cb0ef41Sopenharmony_ciensure alignment, the maximum is used and the total frame length is not
27581cb0ef41Sopenharmony_cinecessarily aligned at 8 bytes.</li>
27591cb0ef41Sopenharmony_ci</ul>
27601cb0ef41Sopenharmony_ci</li>
27611cb0ef41Sopenharmony_ci<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
27621cb0ef41Sopenharmony_cistreams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
27631cb0ef41Sopenharmony_cibe overridden if the remote peer sets its own value for
27641cb0ef41Sopenharmony_ci<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
27651cb0ef41Sopenharmony_ci<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of invalid
27661cb0ef41Sopenharmony_ciframes that will be tolerated before the session is closed.
27671cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1000</code>.</li>
27681cb0ef41Sopenharmony_ci<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of rejected
27691cb0ef41Sopenharmony_ciupon creation streams that will be tolerated before the session is closed.
27701cb0ef41Sopenharmony_ciEach rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
27711cb0ef41Sopenharmony_cierror that should tell the peer to not open any more streams, continuing
27721cb0ef41Sopenharmony_cito open streams is therefore regarded as a sign of a misbehaving peer.
27731cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
27741cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
27751cb0ef41Sopenharmony_ciremote peer upon connection.</li>
27761cb0ef41Sopenharmony_ci<li><code>Http1IncomingMessage</code> <a href="http.html#class-httpincomingmessage" class="type">&#x3C;http.IncomingMessage></a> Specifies the
27771cb0ef41Sopenharmony_ci<code>IncomingMessage</code> class to used for HTTP/1 fallback. Useful for extending
27781cb0ef41Sopenharmony_cithe original <code>http.IncomingMessage</code>. <strong>Default:</strong> <code>http.IncomingMessage</code>.</li>
27791cb0ef41Sopenharmony_ci<li><code>Http1ServerResponse</code> <a href="http.html#class-httpserverresponse" class="type">&#x3C;http.ServerResponse></a> Specifies the <code>ServerResponse</code>
27801cb0ef41Sopenharmony_ciclass to used for HTTP/1 fallback. Useful for extending the original
27811cb0ef41Sopenharmony_ci<code>http.ServerResponse</code>. <strong>Default:</strong> <code>http.ServerResponse</code>.</li>
27821cb0ef41Sopenharmony_ci<li><code>Http2ServerRequest</code> <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a> Specifies the
27831cb0ef41Sopenharmony_ci<code>Http2ServerRequest</code> class to use.
27841cb0ef41Sopenharmony_ciUseful for extending the original <code>Http2ServerRequest</code>.
27851cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Http2ServerRequest</code>.</li>
27861cb0ef41Sopenharmony_ci<li><code>Http2ServerResponse</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> Specifies the
27871cb0ef41Sopenharmony_ci<code>Http2ServerResponse</code> class to use.
27881cb0ef41Sopenharmony_ciUseful for extending the original <code>Http2ServerResponse</code>.
27891cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>Http2ServerResponse</code>.</li>
27901cb0ef41Sopenharmony_ci<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
27911cb0ef41Sopenharmony_cia server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> is emitted. If the
27921cb0ef41Sopenharmony_cisocket has not been destroyed by that time the server will destroy it.
27931cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>10000</code>.</li>
27941cb0ef41Sopenharmony_ci<li>...: Any <a href="net.html#netcreateserveroptions-connectionlistener"><code>net.createServer()</code></a> option can be provided.</li>
27951cb0ef41Sopenharmony_ci</ul>
27961cb0ef41Sopenharmony_ci</li>
27971cb0ef41Sopenharmony_ci<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#compatibility-api">Compatibility API</a></li>
27981cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2server" class="type">&#x3C;Http2Server></a></li>
27991cb0ef41Sopenharmony_ci</ul>
28001cb0ef41Sopenharmony_ci<p>Returns a <code>net.Server</code> instance that creates and manages <code>Http2Session</code>
28011cb0ef41Sopenharmony_ciinstances.</p>
28021cb0ef41Sopenharmony_ci<p>Since there are no browsers known that support
28031cb0ef41Sopenharmony_ci<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
28041cb0ef41Sopenharmony_ci<a href="#http2createsecureserveroptions-onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
28051cb0ef41Sopenharmony_ciwith browser clients.</p>
28061cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
28071cb0ef41Sopenharmony_ci
28081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
28091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Since there are no browsers known that support</span>
28101cb0ef41Sopenharmony_ci<span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
28111cb0ef41Sopenharmony_ci<span class="hljs-comment">// is necessary when communicating with browser clients.</span>
28121cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
28131cb0ef41Sopenharmony_ci
28141cb0ef41Sopenharmony_ciserver.<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> {
28151cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
28161cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
28171cb0ef41Sopenharmony_ci    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
28181cb0ef41Sopenharmony_ci  });
28191cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
28201cb0ef41Sopenharmony_ci});
28211cb0ef41Sopenharmony_ci
28221cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
28231cb0ef41Sopenharmony_ci<h4><code>http2.createSecureServer(options[, onRequestHandler])</code><span><a class="mark" href="#http2createsecureserveroptions-onrequesthandler" id="http2createsecureserveroptions-onrequesthandler">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_createsecureserver_options_onrequesthandler"></a></h4>
28241cb0ef41Sopenharmony_ci<div class="api_metadata">
28251cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
28261cb0ef41Sopenharmony_ci<table>
28271cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
28281cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
28291cb0ef41Sopenharmony_ci<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>
28301cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
28311cb0ef41Sopenharmony_ci<td><p>Added <code>maxSessionRejectedStreams</code> option with a default of 100.</p></td></tr>
28321cb0ef41Sopenharmony_ci<tr><td>v13.3.0, v12.16.0</td>
28331cb0ef41Sopenharmony_ci<td><p>Added <code>maxSessionInvalidFrames</code> option with a default of 1000.</p></td></tr>
28341cb0ef41Sopenharmony_ci<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
28351cb0ef41Sopenharmony_ci<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
28361cb0ef41Sopenharmony_ci<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
28371cb0ef41Sopenharmony_ci<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
28381cb0ef41Sopenharmony_ci<tr><td>v10.12.0</td>
28391cb0ef41Sopenharmony_ci<td><p>Added the <code>origins</code> option to automatically send an <code>ORIGIN</code> frame on <code>Http2Session</code> startup.</p></td></tr>
28401cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
28411cb0ef41Sopenharmony_ci<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
28421cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
28431cb0ef41Sopenharmony_ci<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
28441cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
28451cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
28461cb0ef41Sopenharmony_ci</tbody></table>
28471cb0ef41Sopenharmony_ci</details>
28481cb0ef41Sopenharmony_ci</div>
28491cb0ef41Sopenharmony_ci<ul>
28501cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
28511cb0ef41Sopenharmony_ci<ul>
28521cb0ef41Sopenharmony_ci<li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Incoming client connections that do not support
28531cb0ef41Sopenharmony_ciHTTP/2 will be downgraded to HTTP/1.x when set to <code>true</code>.
28541cb0ef41Sopenharmony_ciSee the <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event. See <a href="#alpn-negotiation">ALPN negotiation</a>.
28551cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
28561cb0ef41Sopenharmony_ci<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
28571cb0ef41Sopenharmony_cifor deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
28581cb0ef41Sopenharmony_ci<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
28591cb0ef41Sopenharmony_ci<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
28601cb0ef41Sopenharmony_ci<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
28611cb0ef41Sopenharmony_ciis permitted to use. The value is expressed in terms of number of megabytes,
28621cb0ef41Sopenharmony_cie.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>. This is a
28631cb0ef41Sopenharmony_cicredit based limit, existing <code>Http2Stream</code>s may cause this
28641cb0ef41Sopenharmony_cilimit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
28651cb0ef41Sopenharmony_ciwhile this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
28661cb0ef41Sopenharmony_cithe current memory use of the header compression tables, current data
28671cb0ef41Sopenharmony_ciqueued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
28681cb0ef41Sopenharmony_cicounted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
28691cb0ef41Sopenharmony_ci<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
28701cb0ef41Sopenharmony_ciThis is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
28711cb0ef41Sopenharmony_ci<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
28721cb0ef41Sopenharmony_ciis <code>4</code>. <strong>Default:</strong> <code>128</code>.</li>
28731cb0ef41Sopenharmony_ci<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
28741cb0ef41Sopenharmony_ciunacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
28751cb0ef41Sopenharmony_ci<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
28761cb0ef41Sopenharmony_ciserialized, compressed block of headers. Attempts to send headers that
28771cb0ef41Sopenharmony_ciexceed this limit will result in a <code>'frameError'</code> event being emitted
28781cb0ef41Sopenharmony_ciand the stream being closed and destroyed.</li>
28791cb0ef41Sopenharmony_ci<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Strategy used for determining the amount of
28801cb0ef41Sopenharmony_cipadding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
28811cb0ef41Sopenharmony_ci<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
28821cb0ef41Sopenharmony_ci<ul>
28831cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
28841cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
28851cb0ef41Sopenharmony_cidetermined by the internal implementation, is applied.</li>
28861cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
28871cb0ef41Sopenharmony_cipadding to ensure that the total frame length, including the
28881cb0ef41Sopenharmony_ci9-byte header, is a multiple of 8. For each frame, there is a maximum
28891cb0ef41Sopenharmony_ciallowed number of padding bytes that is determined by current flow control
28901cb0ef41Sopenharmony_cistate and settings. If this maximum is less than the calculated amount
28911cb0ef41Sopenharmony_cineeded to ensure alignment, the maximum is used and the total frame length
28921cb0ef41Sopenharmony_ciis not necessarily aligned at 8 bytes.</li>
28931cb0ef41Sopenharmony_ci</ul>
28941cb0ef41Sopenharmony_ci</li>
28951cb0ef41Sopenharmony_ci<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
28961cb0ef41Sopenharmony_cistreams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
28971cb0ef41Sopenharmony_cibe overridden if the remote peer sets its own value for
28981cb0ef41Sopenharmony_ci<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
28991cb0ef41Sopenharmony_ci<li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of invalid
29001cb0ef41Sopenharmony_ciframes that will be tolerated before the session is closed.
29011cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1000</code>.</li>
29021cb0ef41Sopenharmony_ci<li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Sets the maximum number of rejected
29031cb0ef41Sopenharmony_ciupon creation streams that will be tolerated before the session is closed.
29041cb0ef41Sopenharmony_ciEach rejection is associated with an <code>NGHTTP2_ENHANCE_YOUR_CALM</code>
29051cb0ef41Sopenharmony_cierror that should tell the peer to not open any more streams, continuing
29061cb0ef41Sopenharmony_cito open streams is therefore regarded as a sign of a misbehaving peer.
29071cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>100</code>.</li>
29081cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
29091cb0ef41Sopenharmony_ciremote peer upon connection.</li>
29101cb0ef41Sopenharmony_ci<li>...: Any <a href="tls.html#tlscreateserveroptions-secureconnectionlistener"><code>tls.createServer()</code></a> options can be provided. For
29111cb0ef41Sopenharmony_ciservers, the identity options (<code>pfx</code> or <code>key</code>/<code>cert</code>) are usually required.</li>
29121cb0ef41Sopenharmony_ci<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> An array of origin strings to send within an <code>ORIGIN</code>
29131cb0ef41Sopenharmony_ciframe immediately following creation of a new server <code>Http2Session</code>.</li>
29141cb0ef41Sopenharmony_ci<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
29151cb0ef41Sopenharmony_cia server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
29161cb0ef41Sopenharmony_cithe socket has not been destroyed by that time the server will destroy it.
29171cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>10000</code>.</li>
29181cb0ef41Sopenharmony_ci</ul>
29191cb0ef41Sopenharmony_ci</li>
29201cb0ef41Sopenharmony_ci<li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> See <a href="#compatibility-api">Compatibility API</a></li>
29211cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2secureserver" class="type">&#x3C;Http2SecureServer></a></li>
29221cb0ef41Sopenharmony_ci</ul>
29231cb0ef41Sopenharmony_ci<p>Returns a <code>tls.Server</code> instance that creates and manages <code>Http2Session</code>
29241cb0ef41Sopenharmony_ciinstances.</p>
29251cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
29261cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
29271cb0ef41Sopenharmony_ci
29281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = {
29291cb0ef41Sopenharmony_ci  <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-key.pem'</span>),
29301cb0ef41Sopenharmony_ci  <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'server-cert.pem'</span>),
29311cb0ef41Sopenharmony_ci};
29321cb0ef41Sopenharmony_ci
29331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Create a secure HTTP/2 server</span>
29341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSecureServer</span>(options);
29351cb0ef41Sopenharmony_ci
29361cb0ef41Sopenharmony_ciserver.<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> {
29371cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">respond</span>({
29381cb0ef41Sopenharmony_ci    <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf-8'</span>,
29391cb0ef41Sopenharmony_ci    <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
29401cb0ef41Sopenharmony_ci  });
29411cb0ef41Sopenharmony_ci  stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello World&#x3C;/h1>'</span>);
29421cb0ef41Sopenharmony_ci});
29431cb0ef41Sopenharmony_ci
29441cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</code> <button class="copy-button">copy</button></pre>
29451cb0ef41Sopenharmony_ci<h4><code>http2.connect(authority[, options][, listener])</code><span><a class="mark" href="#http2connectauthority-options-listener" id="http2connectauthority-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_connect_authority_options_listener"></a></h4>
29461cb0ef41Sopenharmony_ci<div class="api_metadata">
29471cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
29481cb0ef41Sopenharmony_ci<table>
29491cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
29501cb0ef41Sopenharmony_ci<tr><td>v13.0.0</td>
29511cb0ef41Sopenharmony_ci<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>
29521cb0ef41Sopenharmony_ci<tr><td>v15.10.0, v14.16.0, v12.21.0, v10.24.0</td>
29531cb0ef41Sopenharmony_ci<td><p>Added <code>unknownProtocolTimeout</code> option with a default of 10000.</p></td></tr>
29541cb0ef41Sopenharmony_ci<tr><td>v14.4.0, v12.18.0, v10.21.0</td>
29551cb0ef41Sopenharmony_ci<td><p>Added <code>maxSettings</code> option with a default of 32.</p></td></tr>
29561cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
29571cb0ef41Sopenharmony_ci<td><p>Added the <code>maxOutstandingPings</code> option with a default limit of 10.</p></td></tr>
29581cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
29591cb0ef41Sopenharmony_ci<td><p>Added the <code>maxHeaderListPairs</code> option with a default limit of 128 header pairs.</p></td></tr>
29601cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
29611cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
29621cb0ef41Sopenharmony_ci</tbody></table>
29631cb0ef41Sopenharmony_ci</details>
29641cb0ef41Sopenharmony_ci</div>
29651cb0ef41Sopenharmony_ci<ul>
29661cb0ef41Sopenharmony_ci<li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="url.html#the-whatwg-url-api" class="type">&#x3C;URL></a> The remote HTTP/2 server to connect to. This must
29671cb0ef41Sopenharmony_cibe in the form of a minimal, valid URL with the <code>http://</code> or <code>https://</code>
29681cb0ef41Sopenharmony_ciprefix, host name, and IP port (if a non-default port is used). Userinfo
29691cb0ef41Sopenharmony_ci(user ID and password), path, querystring, and fragment details in the
29701cb0ef41Sopenharmony_ciURL will be ignored.</li>
29711cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
29721cb0ef41Sopenharmony_ci<ul>
29731cb0ef41Sopenharmony_ci<li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum dynamic table size
29741cb0ef41Sopenharmony_cifor deflating header fields. <strong>Default:</strong> <code>4Kib</code>.</li>
29751cb0ef41Sopenharmony_ci<li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of settings entries per
29761cb0ef41Sopenharmony_ci<code>SETTINGS</code> frame. The minimum value allowed is <code>1</code>. <strong>Default:</strong> <code>32</code>.</li>
29771cb0ef41Sopenharmony_ci<li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum memory that the <code>Http2Session</code>
29781cb0ef41Sopenharmony_ciis permitted to use. The value is expressed in terms of number of megabytes,
29791cb0ef41Sopenharmony_cie.g. <code>1</code> equal 1 megabyte. The minimum value allowed is <code>1</code>.
29801cb0ef41Sopenharmony_ciThis is a credit based limit, existing <code>Http2Stream</code>s may cause this
29811cb0ef41Sopenharmony_cilimit to be exceeded, but new <code>Http2Stream</code> instances will be rejected
29821cb0ef41Sopenharmony_ciwhile this limit is exceeded. The current number of <code>Http2Stream</code> sessions,
29831cb0ef41Sopenharmony_cithe current memory use of the header compression tables, current data
29841cb0ef41Sopenharmony_ciqueued to be sent, and unacknowledged <code>PING</code> and <code>SETTINGS</code> frames are all
29851cb0ef41Sopenharmony_cicounted towards the current limit. <strong>Default:</strong> <code>10</code>.</li>
29861cb0ef41Sopenharmony_ci<li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of header entries.
29871cb0ef41Sopenharmony_ciThis is similar to <a href="http.html#servermaxheaderscount"><code>server.maxHeadersCount</code></a> or
29881cb0ef41Sopenharmony_ci<a href="http.html#requestmaxheaderscount"><code>request.maxHeadersCount</code></a> in the <code>node:http</code> module. The minimum value
29891cb0ef41Sopenharmony_ciis <code>1</code>. <strong>Default:</strong> <code>128</code>.</li>
29901cb0ef41Sopenharmony_ci<li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of outstanding,
29911cb0ef41Sopenharmony_ciunacknowledged pings. <strong>Default:</strong> <code>10</code>.</li>
29921cb0ef41Sopenharmony_ci<li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of reserved push
29931cb0ef41Sopenharmony_cistreams the client will accept at any given time. Once the current number of
29941cb0ef41Sopenharmony_cicurrently reserved push streams exceeds reaches this limit, new push streams
29951cb0ef41Sopenharmony_cisent by the server will be automatically rejected. The minimum allowed value
29961cb0ef41Sopenharmony_ciis 0. The maximum allowed value is 2<sup>32</sup>-1. A negative value sets
29971cb0ef41Sopenharmony_cithis option to the maximum allowed value. <strong>Default:</strong> <code>200</code>.</li>
29981cb0ef41Sopenharmony_ci<li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum allowed size for a
29991cb0ef41Sopenharmony_ciserialized, compressed block of headers. Attempts to send headers that
30001cb0ef41Sopenharmony_ciexceed this limit will result in a <code>'frameError'</code> event being emitted
30011cb0ef41Sopenharmony_ciand the stream being closed and destroyed.</li>
30021cb0ef41Sopenharmony_ci<li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Strategy used for determining the amount of
30031cb0ef41Sopenharmony_cipadding to use for <code>HEADERS</code> and <code>DATA</code> frames. <strong>Default:</strong>
30041cb0ef41Sopenharmony_ci<code>http2.constants.PADDING_STRATEGY_NONE</code>. Value may be one of:
30051cb0ef41Sopenharmony_ci<ul>
30061cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_NONE</code>: No padding is applied.</li>
30071cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_MAX</code>: The maximum amount of padding,
30081cb0ef41Sopenharmony_cidetermined by the internal implementation, is applied.</li>
30091cb0ef41Sopenharmony_ci<li><code>http2.constants.PADDING_STRATEGY_ALIGNED</code>: Attempts to apply enough
30101cb0ef41Sopenharmony_cipadding to ensure that the total frame length, including the
30111cb0ef41Sopenharmony_ci9-byte header, is a multiple of 8. For each frame, there is a maximum
30121cb0ef41Sopenharmony_ciallowed number of padding bytes that is determined by current flow control
30131cb0ef41Sopenharmony_cistate and settings. If this maximum is less than the calculated amount
30141cb0ef41Sopenharmony_cineeded to ensure alignment, the maximum is used and the total frame length
30151cb0ef41Sopenharmony_ciis not necessarily aligned at 8 bytes.</li>
30161cb0ef41Sopenharmony_ci</ul>
30171cb0ef41Sopenharmony_ci</li>
30181cb0ef41Sopenharmony_ci<li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the maximum number of concurrent
30191cb0ef41Sopenharmony_cistreams for the remote peer as if a <code>SETTINGS</code> frame had been received. Will
30201cb0ef41Sopenharmony_cibe overridden if the remote peer sets its own value for
30211cb0ef41Sopenharmony_ci<code>maxConcurrentStreams</code>. <strong>Default:</strong> <code>100</code>.</li>
30221cb0ef41Sopenharmony_ci<li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The protocol to connect with, if not set in the
30231cb0ef41Sopenharmony_ci<code>authority</code>. Value may be either <code>'http:'</code> or <code>'https:'</code>. <strong>Default:</strong>
30241cb0ef41Sopenharmony_ci<code>'https:'</code></li>
30251cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a> The initial settings to send to the
30261cb0ef41Sopenharmony_ciremote peer upon connection.</li>
30271cb0ef41Sopenharmony_ci<li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional callback that receives the <code>URL</code>
30281cb0ef41Sopenharmony_ciinstance passed to <code>connect</code> and the <code>options</code> object, and returns any
30291cb0ef41Sopenharmony_ci<a href="stream.html#class-streamduplex"><code>Duplex</code></a> stream that is to be used as the connection for this session.</li>
30301cb0ef41Sopenharmony_ci<li>...: Any <a href="net.html#netconnect"><code>net.connect()</code></a> or <a href="tls.html#tlsconnectoptions-callback"><code>tls.connect()</code></a> options can be provided.</li>
30311cb0ef41Sopenharmony_ci<li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies a timeout in milliseconds that
30321cb0ef41Sopenharmony_cia server should wait when an <a href="#event-unknownprotocol"><code>'unknownProtocol'</code></a> event is emitted. If
30331cb0ef41Sopenharmony_cithe socket has not been destroyed by that time the server will destroy it.
30341cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>10000</code>.</li>
30351cb0ef41Sopenharmony_ci</ul>
30361cb0ef41Sopenharmony_ci</li>
30371cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Will be registered as a one-time listener of the
30381cb0ef41Sopenharmony_ci<a href="#event-connect"><code>'connect'</code></a> event.</li>
30391cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-clienthttp2session" class="type">&#x3C;ClientHttp2Session></a></li>
30401cb0ef41Sopenharmony_ci</ul>
30411cb0ef41Sopenharmony_ci<p>Returns a <code>ClientHttp2Session</code> instance.</p>
30421cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
30431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'https://localhost:1234'</span>);
30441cb0ef41Sopenharmony_ci
30451cb0ef41Sopenharmony_ci<span class="hljs-comment">/* Use the client */</span>
30461cb0ef41Sopenharmony_ci
30471cb0ef41Sopenharmony_ciclient.<span class="hljs-title function_">close</span>();</code> <button class="copy-button">copy</button></pre>
30481cb0ef41Sopenharmony_ci<h4><code>http2.constants</code><span><a class="mark" href="#http2constants" id="http2constants">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_constants"></a></h4>
30491cb0ef41Sopenharmony_ci<div class="api_metadata">
30501cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
30511cb0ef41Sopenharmony_ci</div>
30521cb0ef41Sopenharmony_ci<h5>Error codes for <code>RST_STREAM</code> and <code>GOAWAY</code><span><a class="mark" href="#error-codes-for-rst_stream-and-goaway" id="error-codes-for-rst_stream-and-goaway">#</a></span><a aria-hidden="true" class="legacy" id="http2_error_codes_for_rst_stream_and_goaway"></a></h5>
30531cb0ef41Sopenharmony_ci
30541cb0ef41Sopenharmony_ci
30551cb0ef41Sopenharmony_ci
30561cb0ef41Sopenharmony_ci
30571cb0ef41Sopenharmony_ci
30581cb0ef41Sopenharmony_ci
30591cb0ef41Sopenharmony_ci
30601cb0ef41Sopenharmony_ci
30611cb0ef41Sopenharmony_ci
30621cb0ef41Sopenharmony_ci
30631cb0ef41Sopenharmony_ci
30641cb0ef41Sopenharmony_ci
30651cb0ef41Sopenharmony_ci
30661cb0ef41Sopenharmony_ci
30671cb0ef41Sopenharmony_ci
30681cb0ef41Sopenharmony_ci
30691cb0ef41Sopenharmony_ci
30701cb0ef41Sopenharmony_ci
30711cb0ef41Sopenharmony_ci
30721cb0ef41Sopenharmony_ci
30731cb0ef41Sopenharmony_ci
30741cb0ef41Sopenharmony_ci
30751cb0ef41Sopenharmony_ci
30761cb0ef41Sopenharmony_ci
30771cb0ef41Sopenharmony_ci
30781cb0ef41Sopenharmony_ci
30791cb0ef41Sopenharmony_ci
30801cb0ef41Sopenharmony_ci
30811cb0ef41Sopenharmony_ci
30821cb0ef41Sopenharmony_ci
30831cb0ef41Sopenharmony_ci
30841cb0ef41Sopenharmony_ci
30851cb0ef41Sopenharmony_ci
30861cb0ef41Sopenharmony_ci
30871cb0ef41Sopenharmony_ci
30881cb0ef41Sopenharmony_ci
30891cb0ef41Sopenharmony_ci
30901cb0ef41Sopenharmony_ci
30911cb0ef41Sopenharmony_ci
30921cb0ef41Sopenharmony_ci
30931cb0ef41Sopenharmony_ci
30941cb0ef41Sopenharmony_ci
30951cb0ef41Sopenharmony_ci
30961cb0ef41Sopenharmony_ci
30971cb0ef41Sopenharmony_ci
30981cb0ef41Sopenharmony_ci
30991cb0ef41Sopenharmony_ci
31001cb0ef41Sopenharmony_ci
31011cb0ef41Sopenharmony_ci
31021cb0ef41Sopenharmony_ci
31031cb0ef41Sopenharmony_ci
31041cb0ef41Sopenharmony_ci
31051cb0ef41Sopenharmony_ci
31061cb0ef41Sopenharmony_ci
31071cb0ef41Sopenharmony_ci
31081cb0ef41Sopenharmony_ci
31091cb0ef41Sopenharmony_ci
31101cb0ef41Sopenharmony_ci
31111cb0ef41Sopenharmony_ci
31121cb0ef41Sopenharmony_ci
31131cb0ef41Sopenharmony_ci
31141cb0ef41Sopenharmony_ci
31151cb0ef41Sopenharmony_ci
31161cb0ef41Sopenharmony_ci
31171cb0ef41Sopenharmony_ci
31181cb0ef41Sopenharmony_ci
31191cb0ef41Sopenharmony_ci
31201cb0ef41Sopenharmony_ci
31211cb0ef41Sopenharmony_ci
31221cb0ef41Sopenharmony_ci
31231cb0ef41Sopenharmony_ci
31241cb0ef41Sopenharmony_ci
31251cb0ef41Sopenharmony_ci
31261cb0ef41Sopenharmony_ci
31271cb0ef41Sopenharmony_ci
31281cb0ef41Sopenharmony_ci
31291cb0ef41Sopenharmony_ci
31301cb0ef41Sopenharmony_ci
31311cb0ef41Sopenharmony_ci
31321cb0ef41Sopenharmony_ci
31331cb0ef41Sopenharmony_ci<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>
31341cb0ef41Sopenharmony_ci<p>The <code>'timeout'</code> event is emitted when there is no activity on the Server for
31351cb0ef41Sopenharmony_cia given number of milliseconds set using <code>http2server.setTimeout()</code>.</p>
31361cb0ef41Sopenharmony_ci<h4><code>http2.getDefaultSettings()</code><span><a class="mark" href="#http2getdefaultsettings" id="http2getdefaultsettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getdefaultsettings"></a></h4>
31371cb0ef41Sopenharmony_ci<div class="api_metadata">
31381cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
31391cb0ef41Sopenharmony_ci</div>
31401cb0ef41Sopenharmony_ci<ul>
31411cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
31421cb0ef41Sopenharmony_ci</ul>
31431cb0ef41Sopenharmony_ci<p>Returns an object containing the default settings for an <code>Http2Session</code>
31441cb0ef41Sopenharmony_ciinstance. This method returns a new object instance every time it is called
31451cb0ef41Sopenharmony_ciso instances returned may be safely modified for use.</p>
31461cb0ef41Sopenharmony_ci<h4><code>http2.getPackedSettings([settings])</code><span><a class="mark" href="#http2getpackedsettingssettings" id="http2getpackedsettingssettings">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getpackedsettings_settings"></a></h4>
31471cb0ef41Sopenharmony_ci<div class="api_metadata">
31481cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
31491cb0ef41Sopenharmony_ci</div>
31501cb0ef41Sopenharmony_ci<ul>
31511cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
31521cb0ef41Sopenharmony_ci<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
31531cb0ef41Sopenharmony_ci</ul>
31541cb0ef41Sopenharmony_ci<p>Returns a <code>Buffer</code> instance containing serialized representation of the given
31551cb0ef41Sopenharmony_ciHTTP/2 settings as specified in the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> specification. This is intended
31561cb0ef41Sopenharmony_cifor use with the <code>HTTP2-Settings</code> header field.</p>
31571cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
31581cb0ef41Sopenharmony_ci
31591cb0ef41Sopenharmony_ci<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> });
31601cb0ef41Sopenharmony_ci
31611cb0ef41Sopenharmony_ci<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>));
31621cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: AAIAAAAA</span></code> <button class="copy-button">copy</button></pre>
31631cb0ef41Sopenharmony_ci<h4><code>http2.getUnpackedSettings(buf)</code><span><a class="mark" href="#http2getunpackedsettingsbuf" id="http2getunpackedsettingsbuf">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_getunpackedsettings_buf"></a></h4>
31641cb0ef41Sopenharmony_ci<div class="api_metadata">
31651cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
31661cb0ef41Sopenharmony_ci</div>
31671cb0ef41Sopenharmony_ci<ul>
31681cb0ef41Sopenharmony_ci<li><code>buf</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> The packed settings.</li>
31691cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#settings-object" class="type">&#x3C;HTTP/2 Settings Object></a></li>
31701cb0ef41Sopenharmony_ci</ul>
31711cb0ef41Sopenharmony_ci<p>Returns a <a href="#settings-object">HTTP/2 Settings Object</a> containing the deserialized settings from
31721cb0ef41Sopenharmony_cithe given <code>Buffer</code> as generated by <code>http2.getPackedSettings()</code>.</p>
31731cb0ef41Sopenharmony_ci<h4><code>http2.sensitiveHeaders</code><span><a class="mark" href="#http2sensitiveheaders" id="http2sensitiveheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_http2_sensitiveheaders"></a></h4>
31741cb0ef41Sopenharmony_ci<div class="api_metadata">
31751cb0ef41Sopenharmony_ci<span>Added in: v15.0.0, v14.18.0</span>
31761cb0ef41Sopenharmony_ci</div>
31771cb0ef41Sopenharmony_ci<ul>
31781cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
31791cb0ef41Sopenharmony_ci</ul>
31801cb0ef41Sopenharmony_ci<p>This symbol can be set as a property on the HTTP/2 headers object with an array
31811cb0ef41Sopenharmony_civalue in order to provide a list of headers considered sensitive.
31821cb0ef41Sopenharmony_ciSee <a href="#sensitive-headers">Sensitive headers</a> for more details.</p>
31831cb0ef41Sopenharmony_ci<h4>Headers object<span><a class="mark" href="#headers-object" id="headers-object">#</a></span><a aria-hidden="true" class="legacy" id="http2_headers_object"></a></h4>
31841cb0ef41Sopenharmony_ci<p>Headers are represented as own-properties on JavaScript objects. The property
31851cb0ef41Sopenharmony_cikeys will be serialized to lower-case. Property values should be strings (if
31861cb0ef41Sopenharmony_cithey are not they will be coerced to strings) or an <code>Array</code> of strings (in order
31871cb0ef41Sopenharmony_cito send more than one value per header field).</p>
31881cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
31891cb0ef41Sopenharmony_ci  <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
31901cb0ef41Sopenharmony_ci  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
31911cb0ef41Sopenharmony_ci  <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>],
31921cb0ef41Sopenharmony_ci};
31931cb0ef41Sopenharmony_ci
31941cb0ef41Sopenharmony_cistream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre>
31951cb0ef41Sopenharmony_ci<p>Header objects passed to callback functions will have a <code>null</code> prototype. This
31961cb0ef41Sopenharmony_cimeans that normal JavaScript object methods such as
31971cb0ef41Sopenharmony_ci<code>Object.prototype.toString()</code> and <code>Object.prototype.hasOwnProperty()</code> will
31981cb0ef41Sopenharmony_cinot work.</p>
31991cb0ef41Sopenharmony_ci<p>For incoming headers:</p>
32001cb0ef41Sopenharmony_ci<ul>
32011cb0ef41Sopenharmony_ci<li>The <code>:status</code> header is converted to <code>number</code>.</li>
32021cb0ef41Sopenharmony_ci<li>Duplicates of <code>:status</code>, <code>:method</code>, <code>:authority</code>, <code>:scheme</code>, <code>:path</code>,
32031cb0ef41Sopenharmony_ci<code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-credentials</code>,
32041cb0ef41Sopenharmony_ci<code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-encoding</code>,
32051cb0ef41Sopenharmony_ci<code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>content-md5</code>,
32061cb0ef41Sopenharmony_ci<code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>etag</code>, <code>expires</code>, <code>from</code>,
32071cb0ef41Sopenharmony_ci<code>host</code>, <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</code>,
32081cb0ef41Sopenharmony_ci<code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>,
32091cb0ef41Sopenharmony_ci<code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>, <code>tk</code>,
32101cb0ef41Sopenharmony_ci<code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</code> are
32111cb0ef41Sopenharmony_cidiscarded.</li>
32121cb0ef41Sopenharmony_ci<li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
32131cb0ef41Sopenharmony_ci<li>For duplicate <code>cookie</code> headers, the values are joined together with '; '.</li>
32141cb0ef41Sopenharmony_ci<li>For all other headers, the values are joined together with ', '.</li>
32151cb0ef41Sopenharmony_ci</ul>
32161cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
32171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>();
32181cb0ef41Sopenharmony_ciserver.<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> {
32191cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[<span class="hljs-string">':path'</span>]);
32201cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers.<span class="hljs-property">ABC</span>);
32211cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
32221cb0ef41Sopenharmony_ci<h5>Sensitive headers<span><a class="mark" href="#sensitive-headers" id="sensitive-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_sensitive_headers"></a></h5>
32231cb0ef41Sopenharmony_ci<p>HTTP2 headers can be marked as sensitive, which means that the HTTP/2
32241cb0ef41Sopenharmony_ciheader compression algorithm will never index them. This can make sense for
32251cb0ef41Sopenharmony_ciheader values with low entropy and that may be considered valuable to an
32261cb0ef41Sopenharmony_ciattacker, for example <code>Cookie</code> or <code>Authorization</code>. To achieve this, add
32271cb0ef41Sopenharmony_cithe header name to the <code>[http2.sensitiveHeaders]</code> property as an array:</p>
32281cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
32291cb0ef41Sopenharmony_ci  <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
32301cb0ef41Sopenharmony_ci  <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
32311cb0ef41Sopenharmony_ci  <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
32321cb0ef41Sopenharmony_ci  <span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret data'</span>,
32331cb0ef41Sopenharmony_ci  [http2.<span class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <span class="hljs-string">'other-sensitive-header'</span>],
32341cb0ef41Sopenharmony_ci};
32351cb0ef41Sopenharmony_ci
32361cb0ef41Sopenharmony_cistream.<span class="hljs-title function_">respond</span>(headers);</code> <button class="copy-button">copy</button></pre>
32371cb0ef41Sopenharmony_ci<p>For some headers, such as <code>Authorization</code> and short <code>Cookie</code> headers,
32381cb0ef41Sopenharmony_cithis flag is set automatically.</p>
32391cb0ef41Sopenharmony_ci<p>This property is also set for received headers. It will contain the names of
32401cb0ef41Sopenharmony_ciall headers marked as sensitive, including ones marked that way automatically.</p>
32411cb0ef41Sopenharmony_ci<h4>Settings object<span><a class="mark" href="#settings-object" id="settings-object">#</a></span><a aria-hidden="true" class="legacy" id="http2_settings_object"></a></h4>
32421cb0ef41Sopenharmony_ci<div class="api_metadata">
32431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
32441cb0ef41Sopenharmony_ci<table>
32451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
32461cb0ef41Sopenharmony_ci<tr><td>v12.12.0</td>
32471cb0ef41Sopenharmony_ci<td><p>The <code>maxConcurrentStreams</code> setting is stricter.</p></td></tr>
32481cb0ef41Sopenharmony_ci<tr><td>v8.9.3</td>
32491cb0ef41Sopenharmony_ci<td><p>The <code>maxHeaderListSize</code> setting is now strictly enforced.</p></td></tr>
32501cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
32511cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
32521cb0ef41Sopenharmony_ci</tbody></table>
32531cb0ef41Sopenharmony_ci</details>
32541cb0ef41Sopenharmony_ci</div>
32551cb0ef41Sopenharmony_ci<p>The <code>http2.getDefaultSettings()</code>, <code>http2.getPackedSettings()</code>,
32561cb0ef41Sopenharmony_ci<code>http2.createServer()</code>, <code>http2.createSecureServer()</code>,
32571cb0ef41Sopenharmony_ci<code>http2session.settings()</code>, <code>http2session.localSettings</code>, and
32581cb0ef41Sopenharmony_ci<code>http2session.remoteSettings</code> APIs either return or receive as input an
32591cb0ef41Sopenharmony_ciobject that defines configuration settings for an <code>Http2Session</code> object.
32601cb0ef41Sopenharmony_ciThese objects are ordinary JavaScript objects containing the following
32611cb0ef41Sopenharmony_ciproperties.</p>
32621cb0ef41Sopenharmony_ci<ul>
32631cb0ef41Sopenharmony_ci<li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum number of bytes used for
32641cb0ef41Sopenharmony_ciheader compression. The minimum allowed value is 0. The maximum allowed value
32651cb0ef41Sopenharmony_ciis 2<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
32661cb0ef41Sopenharmony_ci<li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Specifies <code>true</code> if HTTP/2 Push Streams are to be
32671cb0ef41Sopenharmony_cipermitted on the <code>Http2Session</code> instances. <strong>Default:</strong> <code>true</code>.</li>
32681cb0ef41Sopenharmony_ci<li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the <em>sender's</em> initial window size in
32691cb0ef41Sopenharmony_cibytes for stream-level flow control. The minimum allowed value is 0. The
32701cb0ef41Sopenharmony_cimaximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
32711cb0ef41Sopenharmony_ci<li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the size in bytes of the largest frame
32721cb0ef41Sopenharmony_cipayload. The minimum allowed value is 16,384. The maximum allowed value is
32731cb0ef41Sopenharmony_ci2<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
32741cb0ef41Sopenharmony_ci<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum number of concurrent
32751cb0ef41Sopenharmony_cistreams permitted on an <code>Http2Session</code>. There is no default value which
32761cb0ef41Sopenharmony_ciimplies, at least theoretically, 2<sup>32</sup>-1 streams may be open
32771cb0ef41Sopenharmony_ciconcurrently at any given time in an <code>Http2Session</code>. The minimum value
32781cb0ef41Sopenharmony_ciis 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
32791cb0ef41Sopenharmony_ci<code>4294967295</code>.</li>
32801cb0ef41Sopenharmony_ci<li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Specifies the maximum size (uncompressed octets)
32811cb0ef41Sopenharmony_ciof header list that will be accepted. The minimum allowed value is 0. The
32821cb0ef41Sopenharmony_cimaximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
32831cb0ef41Sopenharmony_ci<li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Alias for <code>maxHeaderListSize</code>.</li>
32841cb0ef41Sopenharmony_ci<li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Specifies <code>true</code> if the "Extended Connect
32851cb0ef41Sopenharmony_ciProtocol" defined by <a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> is to be enabled. This setting is only
32861cb0ef41Sopenharmony_cimeaningful if sent by the server. Once the <code>enableConnectProtocol</code> setting
32871cb0ef41Sopenharmony_cihas been enabled for a given <code>Http2Session</code>, it cannot be disabled.
32881cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li>
32891cb0ef41Sopenharmony_ci</ul>
32901cb0ef41Sopenharmony_ci<p>All additional properties on the settings object are ignored.</p>
32911cb0ef41Sopenharmony_ci<h4>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a aria-hidden="true" class="legacy" id="http2_error_handling"></a></h4>
32921cb0ef41Sopenharmony_ci<p>There are several types of error conditions that may arise when using the
32931cb0ef41Sopenharmony_ci<code>node:http2</code> module:</p>
32941cb0ef41Sopenharmony_ci<p>Validation errors occur when an incorrect argument, option, or setting value is
32951cb0ef41Sopenharmony_cipassed in. These will always be reported by a synchronous <code>throw</code>.</p>
32961cb0ef41Sopenharmony_ci<p>State errors occur when an action is attempted at an incorrect time (for
32971cb0ef41Sopenharmony_ciinstance, attempting to send data on a stream after it has closed). These will
32981cb0ef41Sopenharmony_cibe reported using either a synchronous <code>throw</code> or via an <code>'error'</code> event on
32991cb0ef41Sopenharmony_cithe <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending on where
33001cb0ef41Sopenharmony_ciand when the error occurs.</p>
33011cb0ef41Sopenharmony_ci<p>Internal errors occur when an HTTP/2 session fails unexpectedly. These will be
33021cb0ef41Sopenharmony_cireported via an <code>'error'</code> event on the <code>Http2Session</code> or HTTP/2 Server objects.</p>
33031cb0ef41Sopenharmony_ci<p>Protocol errors occur when various HTTP/2 protocol constraints are violated.
33041cb0ef41Sopenharmony_ciThese will be reported using either a synchronous <code>throw</code> or via an <code>'error'</code>
33051cb0ef41Sopenharmony_cievent on the <code>Http2Stream</code>, <code>Http2Session</code> or HTTP/2 Server objects, depending
33061cb0ef41Sopenharmony_cion where and when the error occurs.</p>
33071cb0ef41Sopenharmony_ci<h4>Invalid character handling in header names and values<span><a class="mark" href="#invalid-character-handling-in-header-names-and-values" id="invalid-character-handling-in-header-names-and-values">#</a></span><a aria-hidden="true" class="legacy" id="http2_invalid_character_handling_in_header_names_and_values"></a></h4>
33081cb0ef41Sopenharmony_ci<p>The HTTP/2 implementation applies stricter handling of invalid characters in
33091cb0ef41Sopenharmony_ciHTTP header names and values than the HTTP/1 implementation.</p>
33101cb0ef41Sopenharmony_ci<p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
33111cb0ef41Sopenharmony_cistrictly as lower-case strings. The API provided by Node.js allows header
33121cb0ef41Sopenharmony_cinames to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
33131cb0ef41Sopenharmony_cithose to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
33141cb0ef41Sopenharmony_ci<p>Header field-names <em>must only</em> contain one or more of the following ASCII
33151cb0ef41Sopenharmony_cicharacters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</code>, <code>!</code>, <code>#</code>, <code>$</code>, <code>%</code>, <code>&#x26;</code>, <code>'</code>, <code>*</code>, <code>+</code>,
33161cb0ef41Sopenharmony_ci<code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|</code>, and <code>~</code>.</p>
33171cb0ef41Sopenharmony_ci<p>Using invalid characters within an HTTP header field name will cause the
33181cb0ef41Sopenharmony_cistream to be closed with a protocol error being reported.</p>
33191cb0ef41Sopenharmony_ci<p>Header field values are handled with more leniency but <em>should</em> not contain
33201cb0ef41Sopenharmony_cinew-line or carriage return characters and <em>should</em> be limited to US-ASCII
33211cb0ef41Sopenharmony_cicharacters, per the requirements of the HTTP specification.</p>
33221cb0ef41Sopenharmony_ci<h4>Push streams on the client<span><a class="mark" href="#push-streams-on-the-client" id="push-streams-on-the-client">#</a></span><a aria-hidden="true" class="legacy" id="http2_push_streams_on_the_client"></a></h4>
33231cb0ef41Sopenharmony_ci<p>To receive pushed streams on the client, set a listener for the <code>'stream'</code>
33241cb0ef41Sopenharmony_cievent on the <code>ClientHttp2Session</code>:</p>
33251cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
33261cb0ef41Sopenharmony_ci
33271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost'</span>);
33281cb0ef41Sopenharmony_ci
33291cb0ef41Sopenharmony_ciclient.<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> {
33301cb0ef41Sopenharmony_ci  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> {
33311cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Process response headers</span>
33321cb0ef41Sopenharmony_ci  });
33331cb0ef41Sopenharmony_ci  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> });
33341cb0ef41Sopenharmony_ci});
33351cb0ef41Sopenharmony_ci
33361cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
33371cb0ef41Sopenharmony_ci<h4>Supporting the <code>CONNECT</code> method<span><a class="mark" href="#supporting-the-connect-method" id="supporting-the-connect-method">#</a></span><a aria-hidden="true" class="legacy" id="http2_supporting_the_connect_method"></a></h4>
33381cb0ef41Sopenharmony_ci<p>The <code>CONNECT</code> method is used to allow an HTTP/2 server to be used as a proxy
33391cb0ef41Sopenharmony_cifor TCP/IP connections.</p>
33401cb0ef41Sopenharmony_ci<p>A simple TCP Server:</p>
33411cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
33421cb0ef41Sopenharmony_ci
33431cb0ef41Sopenharmony_ci<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> {
33441cb0ef41Sopenharmony_ci  <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
33451cb0ef41Sopenharmony_ci  socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
33461cb0ef41Sopenharmony_ci  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);
33471cb0ef41Sopenharmony_ci  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>));
33481cb0ef41Sopenharmony_ci});
33491cb0ef41Sopenharmony_ci
33501cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code> <button class="copy-button">copy</button></pre>
33511cb0ef41Sopenharmony_ci<p>An HTTP/2 CONNECT proxy:</p>
33521cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
33531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { <span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM } = http2.<span class="hljs-property">constants</span>;
33541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>);
33551cb0ef41Sopenharmony_ci
33561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServer</span>();
33571cb0ef41Sopenharmony_ciproxy.<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> {
33581cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <span class="hljs-string">'CONNECT'</span>) {
33591cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Only accept CONNECT requests</span>
33601cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">close</span>(<span class="hljs-title class_">NGHTTP2</span>_REFUSED_STREAM);
33611cb0ef41Sopenharmony_ci    <span class="hljs-keyword">return</span>;
33621cb0ef41Sopenharmony_ci  }
33631cb0ef41Sopenharmony_ci  <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>);
33641cb0ef41Sopenharmony_ci  <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
33651cb0ef41Sopenharmony_ci  <span class="hljs-comment">// things this proxy should be connecting to.</span>
33661cb0ef41Sopenharmony_ci  <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> {
33671cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">respond</span>();
33681cb0ef41Sopenharmony_ci    socket.<span class="hljs-title function_">pipe</span>(stream);
33691cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">pipe</span>(socket);
33701cb0ef41Sopenharmony_ci  });
33711cb0ef41Sopenharmony_ci  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> {
33721cb0ef41Sopenharmony_ci    stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants</span>.<span class="hljs-property">NGHTTP2_CONNECT_ERROR</span>);
33731cb0ef41Sopenharmony_ci  });
33741cb0ef41Sopenharmony_ci});
33751cb0ef41Sopenharmony_ci
33761cb0ef41Sopenharmony_ciproxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</code> <button class="copy-button">copy</button></pre>
33771cb0ef41Sopenharmony_ci<p>An HTTP/2 CONNECT client:</p>
33781cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
33791cb0ef41Sopenharmony_ci
33801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8001'</span>);
33811cb0ef41Sopenharmony_ci
33821cb0ef41Sopenharmony_ci<span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
33831cb0ef41Sopenharmony_ci<span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
33841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({
33851cb0ef41Sopenharmony_ci  <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>,
33861cb0ef41Sopenharmony_ci  <span class="hljs-string">':authority'</span>: <span class="hljs-string">'localhost:8000'</span>,
33871cb0ef41Sopenharmony_ci});
33881cb0ef41Sopenharmony_ci
33891cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span class="hljs-function">(<span class="hljs-params">headers</span>) =></span> {
33901cb0ef41Sopenharmony_ci  <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>]);
33911cb0ef41Sopenharmony_ci});
33921cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
33931cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
33941cb0ef41Sopenharmony_cireq.<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);
33951cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">() =></span> {
33961cb0ef41Sopenharmony_ci  <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>);
33971cb0ef41Sopenharmony_ci  client.<span class="hljs-title function_">close</span>();
33981cb0ef41Sopenharmony_ci});
33991cb0ef41Sopenharmony_cireq.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code> <button class="copy-button">copy</button></pre>
34001cb0ef41Sopenharmony_ci<h4>The extended <code>CONNECT</code> protocol<span><a class="mark" href="#the-extended-connect-protocol" id="the-extended-connect-protocol">#</a></span><a aria-hidden="true" class="legacy" id="http2_the_extended_connect_protocol"></a></h4>
34011cb0ef41Sopenharmony_ci<p><a href="https://tools.ietf.org/html/rfc8441">RFC 8441</a> defines an "Extended CONNECT Protocol" extension to HTTP/2 that
34021cb0ef41Sopenharmony_cimay be used to bootstrap the use of an <code>Http2Stream</code> using the <code>CONNECT</code>
34031cb0ef41Sopenharmony_cimethod as a tunnel for other communication protocols (such as WebSockets).</p>
34041cb0ef41Sopenharmony_ci<p>The use of the Extended CONNECT Protocol is enabled by HTTP/2 servers by using
34051cb0ef41Sopenharmony_cithe <code>enableConnectProtocol</code> setting:</p>
34061cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
34071cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol</span>: <span class="hljs-literal">true</span> };
34081cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>({ settings });</code> <button class="copy-button">copy</button></pre>
34091cb0ef41Sopenharmony_ci<p>Once the client receives the <code>SETTINGS</code> frame from the server indicating that
34101cb0ef41Sopenharmony_cithe extended CONNECT may be used, it may send <code>CONNECT</code> requests that use the
34111cb0ef41Sopenharmony_ci<code>':protocol'</code> HTTP/2 pseudo-header:</p>
34121cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
34131cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</span>(<span class="hljs-string">'http://localhost:8080'</span>);
34141cb0ef41Sopenharmony_ciclient.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span class="hljs-function">(<span class="hljs-params">settings</span>) =></span> {
34151cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</span>) {
34161cb0ef41Sopenharmony_ci    <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> });
34171cb0ef41Sopenharmony_ci    <span class="hljs-comment">// ...</span>
34181cb0ef41Sopenharmony_ci  }
34191cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
34201cb0ef41Sopenharmony_ci</section><section><h3>Compatibility API<span><a class="mark" href="#compatibility-api" id="compatibility-api">#</a></span><a aria-hidden="true" class="legacy" id="http2_compatibility_api"></a></h3>
34211cb0ef41Sopenharmony_ci<p>The Compatibility API has the goal of providing a similar developer experience
34221cb0ef41Sopenharmony_ciof HTTP/1 when using HTTP/2, making it possible to develop applications
34231cb0ef41Sopenharmony_cithat support both <a href="http.html">HTTP/1</a> and HTTP/2. This API targets only the
34241cb0ef41Sopenharmony_ci<strong>public API</strong> of the <a href="http.html">HTTP/1</a>. However many modules use internal
34251cb0ef41Sopenharmony_cimethods or state, and those <em>are not supported</em> as it is a completely
34261cb0ef41Sopenharmony_cidifferent implementation.</p>
34271cb0ef41Sopenharmony_ci<p>The following example creates an HTTP/2 server using the compatibility
34281cb0ef41Sopenharmony_ciAPI:</p>
34291cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
34301cb0ef41Sopenharmony_ci<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> {
34311cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'text/html'</span>);
34321cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
34331cb0ef41Sopenharmony_ci  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> });
34341cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
34351cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
34361cb0ef41Sopenharmony_ci<p>In order to create a mixed <a href="https.html">HTTPS</a> and HTTP/2 server, refer to the
34371cb0ef41Sopenharmony_ci<a href="#alpn-negotiation">ALPN negotiation</a> section.
34381cb0ef41Sopenharmony_ciUpgrading from non-tls HTTP/1 servers is not supported.</p>
34391cb0ef41Sopenharmony_ci<p>The HTTP/2 compatibility API is composed of <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a> and
34401cb0ef41Sopenharmony_ci<a href="#class-http2http2serverresponse"><code>Http2ServerResponse</code></a>. They aim at API compatibility with HTTP/1, but
34411cb0ef41Sopenharmony_cithey do not hide the differences between the protocols. As an example,
34421cb0ef41Sopenharmony_cithe status message for HTTP codes is ignored.</p>
34431cb0ef41Sopenharmony_ci<h4>ALPN negotiation<span><a class="mark" href="#alpn-negotiation" id="alpn-negotiation">#</a></span><a aria-hidden="true" class="legacy" id="http2_alpn_negotiation"></a></h4>
34441cb0ef41Sopenharmony_ci<p>ALPN negotiation allows supporting both <a href="https.html">HTTPS</a> and HTTP/2 over
34451cb0ef41Sopenharmony_cithe same socket. The <code>req</code> and <code>res</code> objects can be either HTTP/1 or
34461cb0ef41Sopenharmony_ciHTTP/2, and an application <strong>must</strong> restrict itself to the public API of
34471cb0ef41Sopenharmony_ci<a href="http.html">HTTP/1</a>, and detect if it is possible to use the more advanced
34481cb0ef41Sopenharmony_cifeatures of HTTP/2.</p>
34491cb0ef41Sopenharmony_ci<p>The following example creates a server that supports both protocols:</p>
34501cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
34511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
34521cb0ef41Sopenharmony_ci
34531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./cert.pem'</span>);
34541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'./key.pem'</span>);
34551cb0ef41Sopenharmony_ci
34561cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureServer</span>(
34571cb0ef41Sopenharmony_ci  { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
34581cb0ef41Sopenharmony_ci  onRequest,
34591cb0ef41Sopenharmony_ci).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
34601cb0ef41Sopenharmony_ci
34611cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<span class="hljs-params">req, res</span>) {
34621cb0ef41Sopenharmony_ci  <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
34631cb0ef41Sopenharmony_ci  <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> ?
34641cb0ef41Sopenharmony_ci    req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
34651cb0ef41Sopenharmony_ci  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> });
34661cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
34671cb0ef41Sopenharmony_ci    alpnProtocol,
34681cb0ef41Sopenharmony_ci    <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>,
34691cb0ef41Sopenharmony_ci  }));
34701cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
34711cb0ef41Sopenharmony_ci<p>The <code>'request'</code> event works identically on both <a href="https.html">HTTPS</a> and
34721cb0ef41Sopenharmony_ciHTTP/2.</p>
34731cb0ef41Sopenharmony_ci<h4>Class: <code>http2.Http2ServerRequest</code><span><a class="mark" href="#class-http2http2serverrequest" id="class-http2http2serverrequest">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2_http2serverrequest"></a></h4>
34741cb0ef41Sopenharmony_ci<div class="api_metadata">
34751cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
34761cb0ef41Sopenharmony_ci</div>
34771cb0ef41Sopenharmony_ci<ul>
34781cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a></li>
34791cb0ef41Sopenharmony_ci</ul>
34801cb0ef41Sopenharmony_ci<p>A <code>Http2ServerRequest</code> object is created by <a href="#class-http2server"><code>http2.Server</code></a> or
34811cb0ef41Sopenharmony_ci<a href="#class-http2secureserver"><code>http2.SecureServer</code></a> and passed as the first argument to the
34821cb0ef41Sopenharmony_ci<a href="#event-request"><code>'request'</code></a> event. It may be used to access a request status, headers, and
34831cb0ef41Sopenharmony_cidata.</p>
34841cb0ef41Sopenharmony_ci<h5>Event: <code>'aborted'</code><span><a class="mark" href="#event-aborted_1" id="event-aborted_1">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_aborted_1"></a></h5>
34851cb0ef41Sopenharmony_ci<div class="api_metadata">
34861cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
34871cb0ef41Sopenharmony_ci</div>
34881cb0ef41Sopenharmony_ci<p>The <code>'aborted'</code> event is emitted whenever a <code>Http2ServerRequest</code> instance is
34891cb0ef41Sopenharmony_ciabnormally aborted in mid-communication.</p>
34901cb0ef41Sopenharmony_ci<p>The <code>'aborted'</code> event will only be emitted if the <code>Http2ServerRequest</code> writable
34911cb0ef41Sopenharmony_ciside has not been ended.</p>
34921cb0ef41Sopenharmony_ci<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_2" id="event-close_2">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_2"></a></h5>
34931cb0ef41Sopenharmony_ci<div class="api_metadata">
34941cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
34951cb0ef41Sopenharmony_ci</div>
34961cb0ef41Sopenharmony_ci<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was closed.
34971cb0ef41Sopenharmony_ciJust like <code>'end'</code>, this event occurs only once per response.</p>
34981cb0ef41Sopenharmony_ci<h5><code>request.aborted</code><span><a class="mark" href="#requestaborted" id="requestaborted">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_aborted"></a></h5>
34991cb0ef41Sopenharmony_ci<div class="api_metadata">
35001cb0ef41Sopenharmony_ci<span>Added in: v10.1.0</span>
35011cb0ef41Sopenharmony_ci</div>
35021cb0ef41Sopenharmony_ci<ul>
35031cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
35041cb0ef41Sopenharmony_ci</ul>
35051cb0ef41Sopenharmony_ci<p>The <code>request.aborted</code> property will be <code>true</code> if the request has
35061cb0ef41Sopenharmony_cibeen aborted.</p>
35071cb0ef41Sopenharmony_ci<h5><code>request.authority</code><span><a class="mark" href="#requestauthority" id="requestauthority">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_authority"></a></h5>
35081cb0ef41Sopenharmony_ci<div class="api_metadata">
35091cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35101cb0ef41Sopenharmony_ci</div>
35111cb0ef41Sopenharmony_ci<ul>
35121cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
35131cb0ef41Sopenharmony_ci</ul>
35141cb0ef41Sopenharmony_ci<p>The request authority pseudo header field. Because HTTP/2 allows requests
35151cb0ef41Sopenharmony_cito set either <code>:authority</code> or <code>host</code>, this value is derived from
35161cb0ef41Sopenharmony_ci<code>req.headers[':authority']</code> if present. Otherwise, it is derived from
35171cb0ef41Sopenharmony_ci<code>req.headers['host']</code>.</p>
35181cb0ef41Sopenharmony_ci<h5><code>request.complete</code><span><a class="mark" href="#requestcomplete" id="requestcomplete">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_complete"></a></h5>
35191cb0ef41Sopenharmony_ci<div class="api_metadata">
35201cb0ef41Sopenharmony_ci<span>Added in: v12.10.0</span>
35211cb0ef41Sopenharmony_ci</div>
35221cb0ef41Sopenharmony_ci<ul>
35231cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
35241cb0ef41Sopenharmony_ci</ul>
35251cb0ef41Sopenharmony_ci<p>The <code>request.complete</code> property will be <code>true</code> if the request has
35261cb0ef41Sopenharmony_cibeen completed, aborted, or destroyed.</p>
35271cb0ef41Sopenharmony_ci<h5><code>request.connection</code><span><a class="mark" href="#requestconnection" id="requestconnection">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_connection"></a></h5>
35281cb0ef41Sopenharmony_ci<div class="api_metadata">
35291cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
35301cb0ef41Sopenharmony_ci</div>
35311cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#requestsocket"><code>request.socket</code></a>.</div><p></p>
35321cb0ef41Sopenharmony_ci<ul>
35331cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
35341cb0ef41Sopenharmony_ci</ul>
35351cb0ef41Sopenharmony_ci<p>See <a href="#requestsocket"><code>request.socket</code></a>.</p>
35361cb0ef41Sopenharmony_ci<h5><code>request.destroy([error])</code><span><a class="mark" href="#requestdestroyerror" id="requestdestroyerror">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_destroy_error"></a></h5>
35371cb0ef41Sopenharmony_ci<div class="api_metadata">
35381cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35391cb0ef41Sopenharmony_ci</div>
35401cb0ef41Sopenharmony_ci<ul>
35411cb0ef41Sopenharmony_ci<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
35421cb0ef41Sopenharmony_ci</ul>
35431cb0ef41Sopenharmony_ci<p>Calls <code>destroy()</code> on the <a href="#class-http2stream"><code>Http2Stream</code></a> that received
35441cb0ef41Sopenharmony_cithe <a href="#class-http2http2serverrequest"><code>Http2ServerRequest</code></a>. If <code>error</code> is provided, an <code>'error'</code> event
35451cb0ef41Sopenharmony_ciis emitted and <code>error</code> is passed as an argument to any listeners on the event.</p>
35461cb0ef41Sopenharmony_ci<p>It does nothing if the stream was already destroyed.</p>
35471cb0ef41Sopenharmony_ci<h5><code>request.headers</code><span><a class="mark" href="#requestheaders" id="requestheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_headers"></a></h5>
35481cb0ef41Sopenharmony_ci<div class="api_metadata">
35491cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35501cb0ef41Sopenharmony_ci</div>
35511cb0ef41Sopenharmony_ci<ul>
35521cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
35531cb0ef41Sopenharmony_ci</ul>
35541cb0ef41Sopenharmony_ci<p>The request/response headers object.</p>
35551cb0ef41Sopenharmony_ci<p>Key-value pairs of header names and values. Header names are lower-cased.</p>
35561cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
35571cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
35581cb0ef41Sopenharmony_ci<span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
35591cb0ef41Sopenharmony_ci<span class="hljs-comment">//   host: '127.0.0.1:8000',</span>
35601cb0ef41Sopenharmony_ci<span class="hljs-comment">//   accept: '*/*' }</span>
35611cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">headers</span>);</code> <button class="copy-button">copy</button></pre>
35621cb0ef41Sopenharmony_ci<p>See <a href="#headers-object">HTTP/2 Headers Object</a>.</p>
35631cb0ef41Sopenharmony_ci<p>In HTTP/2, the request path, host name, protocol, and method are represented as
35641cb0ef41Sopenharmony_cispecial headers prefixed with the <code>:</code> character (e.g. <code>':path'</code>). These special
35651cb0ef41Sopenharmony_ciheaders will be included in the <code>request.headers</code> object. Care must be taken not
35661cb0ef41Sopenharmony_cito inadvertently modify these special headers or errors may occur. For instance,
35671cb0ef41Sopenharmony_ciremoving all headers from the request will cause errors to occur:</p>
35681cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<span class="hljs-property">headers</span>);
35691cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
35701cb0ef41Sopenharmony_ci<h5><code>request.httpVersion</code><span><a class="mark" href="#requesthttpversion" id="requesthttpversion">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_httpversion"></a></h5>
35711cb0ef41Sopenharmony_ci<div class="api_metadata">
35721cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35731cb0ef41Sopenharmony_ci</div>
35741cb0ef41Sopenharmony_ci<ul>
35751cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
35761cb0ef41Sopenharmony_ci</ul>
35771cb0ef41Sopenharmony_ci<p>In case of server request, the HTTP version sent by the client. In the case of
35781cb0ef41Sopenharmony_ciclient response, the HTTP version of the connected-to server. Returns
35791cb0ef41Sopenharmony_ci<code>'2.0'</code>.</p>
35801cb0ef41Sopenharmony_ci<p>Also <code>message.httpVersionMajor</code> is the first integer and
35811cb0ef41Sopenharmony_ci<code>message.httpVersionMinor</code> is the second.</p>
35821cb0ef41Sopenharmony_ci<h5><code>request.method</code><span><a class="mark" href="#requestmethod" id="requestmethod">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_method"></a></h5>
35831cb0ef41Sopenharmony_ci<div class="api_metadata">
35841cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35851cb0ef41Sopenharmony_ci</div>
35861cb0ef41Sopenharmony_ci<ul>
35871cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
35881cb0ef41Sopenharmony_ci</ul>
35891cb0ef41Sopenharmony_ci<p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.</p>
35901cb0ef41Sopenharmony_ci<h5><code>request.rawHeaders</code><span><a class="mark" href="#requestrawheaders" id="requestrawheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_rawheaders"></a></h5>
35911cb0ef41Sopenharmony_ci<div class="api_metadata">
35921cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
35931cb0ef41Sopenharmony_ci</div>
35941cb0ef41Sopenharmony_ci<ul>
35951cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
35961cb0ef41Sopenharmony_ci</ul>
35971cb0ef41Sopenharmony_ci<p>The raw request/response headers list exactly as they were received.</p>
35981cb0ef41Sopenharmony_ci<p>The keys and values are in the same list. It is <em>not</em> a
35991cb0ef41Sopenharmony_cilist of tuples. So, the even-numbered offsets are key values, and the
36001cb0ef41Sopenharmony_ciodd-numbered offsets are the associated values.</p>
36011cb0ef41Sopenharmony_ci<p>Header names are not lowercased, and duplicates are not merged.</p>
36021cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
36031cb0ef41Sopenharmony_ci<span class="hljs-comment">//</span>
36041cb0ef41Sopenharmony_ci<span class="hljs-comment">// [ 'user-agent',</span>
36051cb0ef41Sopenharmony_ci<span class="hljs-comment">//   'this is invalid because there can be only one',</span>
36061cb0ef41Sopenharmony_ci<span class="hljs-comment">//   'User-Agent',</span>
36071cb0ef41Sopenharmony_ci<span class="hljs-comment">//   'curl/7.22.0',</span>
36081cb0ef41Sopenharmony_ci<span class="hljs-comment">//   'Host',</span>
36091cb0ef41Sopenharmony_ci<span class="hljs-comment">//   '127.0.0.1:8000',</span>
36101cb0ef41Sopenharmony_ci<span class="hljs-comment">//   'ACCEPT',</span>
36111cb0ef41Sopenharmony_ci<span class="hljs-comment">//   '*/*' ]</span>
36121cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(request.<span class="hljs-property">rawHeaders</span>);</code> <button class="copy-button">copy</button></pre>
36131cb0ef41Sopenharmony_ci<h5><code>request.rawTrailers</code><span><a class="mark" href="#requestrawtrailers" id="requestrawtrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_rawtrailers"></a></h5>
36141cb0ef41Sopenharmony_ci<div class="api_metadata">
36151cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36161cb0ef41Sopenharmony_ci</div>
36171cb0ef41Sopenharmony_ci<ul>
36181cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
36191cb0ef41Sopenharmony_ci</ul>
36201cb0ef41Sopenharmony_ci<p>The raw request/response trailer keys and values exactly as they were
36211cb0ef41Sopenharmony_cireceived. Only populated at the <code>'end'</code> event.</p>
36221cb0ef41Sopenharmony_ci<h5><code>request.scheme</code><span><a class="mark" href="#requestscheme" id="requestscheme">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_scheme"></a></h5>
36231cb0ef41Sopenharmony_ci<div class="api_metadata">
36241cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36251cb0ef41Sopenharmony_ci</div>
36261cb0ef41Sopenharmony_ci<ul>
36271cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
36281cb0ef41Sopenharmony_ci</ul>
36291cb0ef41Sopenharmony_ci<p>The request scheme pseudo header field indicating the scheme
36301cb0ef41Sopenharmony_ciportion of the target URL.</p>
36311cb0ef41Sopenharmony_ci<h5><code>request.setTimeout(msecs, callback)</code><span><a class="mark" href="#requestsettimeoutmsecs-callback" id="requestsettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_settimeout_msecs_callback"></a></h5>
36321cb0ef41Sopenharmony_ci<div class="api_metadata">
36331cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36341cb0ef41Sopenharmony_ci</div>
36351cb0ef41Sopenharmony_ci<ul>
36361cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
36371cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
36381cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
36391cb0ef41Sopenharmony_ci</ul>
36401cb0ef41Sopenharmony_ci<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
36411cb0ef41Sopenharmony_ciprovided, then it is added as a listener on the <code>'timeout'</code> event on
36421cb0ef41Sopenharmony_cithe response object.</p>
36431cb0ef41Sopenharmony_ci<p>If no <code>'timeout'</code> listener is added to the request, the response, or
36441cb0ef41Sopenharmony_cithe server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
36451cb0ef41Sopenharmony_cihandler is assigned to the request, the response, or the server's <code>'timeout'</code>
36461cb0ef41Sopenharmony_cievents, timed out sockets must be handled explicitly.</p>
36471cb0ef41Sopenharmony_ci<h5><code>request.socket</code><span><a class="mark" href="#requestsocket" id="requestsocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_socket"></a></h5>
36481cb0ef41Sopenharmony_ci<div class="api_metadata">
36491cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36501cb0ef41Sopenharmony_ci</div>
36511cb0ef41Sopenharmony_ci<ul>
36521cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
36531cb0ef41Sopenharmony_ci</ul>
36541cb0ef41Sopenharmony_ci<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
36551cb0ef41Sopenharmony_ciapplies getters, setters, and methods based on HTTP/2 logic.</p>
36561cb0ef41Sopenharmony_ci<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
36571cb0ef41Sopenharmony_ciset on <code>request.stream</code>.</p>
36581cb0ef41Sopenharmony_ci<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
36591cb0ef41Sopenharmony_ci<code>request.stream</code>.</p>
36601cb0ef41Sopenharmony_ci<p><code>setTimeout</code> method will be called on <code>request.stream.session</code>.</p>
36611cb0ef41Sopenharmony_ci<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
36621cb0ef41Sopenharmony_ci<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for
36631cb0ef41Sopenharmony_cimore information.</p>
36641cb0ef41Sopenharmony_ci<p>All other interactions will be routed directly to the socket. With TLS support,
36651cb0ef41Sopenharmony_ciuse <a href="tls.html#tlssocketgetpeercertificatedetailed"><code>request.socket.getPeerCertificate()</code></a> to obtain the client's
36661cb0ef41Sopenharmony_ciauthentication details.</p>
36671cb0ef41Sopenharmony_ci<h5><code>request.stream</code><span><a class="mark" href="#requeststream" id="requeststream">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_stream"></a></h5>
36681cb0ef41Sopenharmony_ci<div class="api_metadata">
36691cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36701cb0ef41Sopenharmony_ci</div>
36711cb0ef41Sopenharmony_ci<ul>
36721cb0ef41Sopenharmony_ci<li><a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a></li>
36731cb0ef41Sopenharmony_ci</ul>
36741cb0ef41Sopenharmony_ci<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the request.</p>
36751cb0ef41Sopenharmony_ci<h5><code>request.trailers</code><span><a class="mark" href="#requesttrailers" id="requesttrailers">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_trailers"></a></h5>
36761cb0ef41Sopenharmony_ci<div class="api_metadata">
36771cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36781cb0ef41Sopenharmony_ci</div>
36791cb0ef41Sopenharmony_ci<ul>
36801cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
36811cb0ef41Sopenharmony_ci</ul>
36821cb0ef41Sopenharmony_ci<p>The request/response trailers object. Only populated at the <code>'end'</code> event.</p>
36831cb0ef41Sopenharmony_ci<h5><code>request.url</code><span><a class="mark" href="#requesturl" id="requesturl">#</a></span><a aria-hidden="true" class="legacy" id="http2_request_url"></a></h5>
36841cb0ef41Sopenharmony_ci<div class="api_metadata">
36851cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
36861cb0ef41Sopenharmony_ci</div>
36871cb0ef41Sopenharmony_ci<ul>
36881cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
36891cb0ef41Sopenharmony_ci</ul>
36901cb0ef41Sopenharmony_ci<p>Request URL string. This contains only the URL that is present in the actual
36911cb0ef41Sopenharmony_ciHTTP request. If the request is:</p>
36921cb0ef41Sopenharmony_ci<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>
36931cb0ef41Sopenharmony_ci<span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code> <button class="copy-button">copy</button></pre>
36941cb0ef41Sopenharmony_ci<p>Then <code>request.url</code> will be:</p>
36951cb0ef41Sopenharmony_ci<!-- eslint-disable semi -->
36961cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code> <button class="copy-button">copy</button></pre>
36971cb0ef41Sopenharmony_ci<p>To parse the url into its parts, <code>new URL()</code> can be used:</p>
36981cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span>
36991cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">new URL(<span class="hljs-string">'/status?name=ryan'</span>, <span class="hljs-string">'http://example.com'</span>)</span>
37001cb0ef41Sopenharmony_ciURL {
37011cb0ef41Sopenharmony_ci  href: 'http://example.com/status?name=ryan',
37021cb0ef41Sopenharmony_ci  origin: 'http://example.com',
37031cb0ef41Sopenharmony_ci  protocol: 'http:',
37041cb0ef41Sopenharmony_ci  username: '',
37051cb0ef41Sopenharmony_ci  password: '',
37061cb0ef41Sopenharmony_ci  host: 'example.com',
37071cb0ef41Sopenharmony_ci  hostname: 'example.com',
37081cb0ef41Sopenharmony_ci  port: '',
37091cb0ef41Sopenharmony_ci  pathname: '/status',
37101cb0ef41Sopenharmony_ci  search: '?name=ryan',
37111cb0ef41Sopenharmony_ci  searchParams: URLSearchParams { 'name' => 'ryan' },
37121cb0ef41Sopenharmony_ci  hash: ''
37131cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
37141cb0ef41Sopenharmony_ci<h4>Class: <code>http2.Http2ServerResponse</code><span><a class="mark" href="#class-http2http2serverresponse" id="class-http2http2serverresponse">#</a></span><a aria-hidden="true" class="legacy" id="http2_class_http2_http2serverresponse"></a></h4>
37151cb0ef41Sopenharmony_ci<div class="api_metadata">
37161cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
37171cb0ef41Sopenharmony_ci</div>
37181cb0ef41Sopenharmony_ci<ul>
37191cb0ef41Sopenharmony_ci<li>Extends: <a href="stream.html#stream" class="type">&#x3C;Stream></a></li>
37201cb0ef41Sopenharmony_ci</ul>
37211cb0ef41Sopenharmony_ci<p>This object is created internally by an HTTP server, not by the user. It is
37221cb0ef41Sopenharmony_cipassed as the second parameter to the <a href="#event-request"><code>'request'</code></a> event.</p>
37231cb0ef41Sopenharmony_ci<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_3" id="event-close_3">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_close_3"></a></h5>
37241cb0ef41Sopenharmony_ci<div class="api_metadata">
37251cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
37261cb0ef41Sopenharmony_ci</div>
37271cb0ef41Sopenharmony_ci<p>Indicates that the underlying <a href="#class-http2stream"><code>Http2Stream</code></a> was terminated before
37281cb0ef41Sopenharmony_ci<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> was called or able to flush.</p>
37291cb0ef41Sopenharmony_ci<h5>Event: <code>'finish'</code><span><a class="mark" href="#event-finish" id="event-finish">#</a></span><a aria-hidden="true" class="legacy" id="http2_event_finish"></a></h5>
37301cb0ef41Sopenharmony_ci<div class="api_metadata">
37311cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
37321cb0ef41Sopenharmony_ci</div>
37331cb0ef41Sopenharmony_ci<p>Emitted when the response has been sent. More specifically, this event is
37341cb0ef41Sopenharmony_ciemitted when the last segment of the response headers and body have been
37351cb0ef41Sopenharmony_cihanded off to the HTTP/2 multiplexing for transmission over the network. It
37361cb0ef41Sopenharmony_cidoes not imply that the client has received anything yet.</p>
37371cb0ef41Sopenharmony_ci<p>After this event, no more events will be emitted on the response object.</p>
37381cb0ef41Sopenharmony_ci<h5><code>response.addTrailers(headers)</code><span><a class="mark" href="#responseaddtrailersheaders" id="responseaddtrailersheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_addtrailers_headers"></a></h5>
37391cb0ef41Sopenharmony_ci<div class="api_metadata">
37401cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
37411cb0ef41Sopenharmony_ci</div>
37421cb0ef41Sopenharmony_ci<ul>
37431cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
37441cb0ef41Sopenharmony_ci</ul>
37451cb0ef41Sopenharmony_ci<p>This method adds HTTP trailing headers (a header but at the end of the
37461cb0ef41Sopenharmony_cimessage) to the response.</p>
37471cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters
37481cb0ef41Sopenharmony_ciwill result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
37491cb0ef41Sopenharmony_ci<h5><code>response.connection</code><span><a class="mark" href="#responseconnection" id="responseconnection">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_connection"></a></h5>
37501cb0ef41Sopenharmony_ci<div class="api_metadata">
37511cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span><span>Deprecated since: v13.0.0</span>
37521cb0ef41Sopenharmony_ci</div>
37531cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#responsesocket"><code>response.socket</code></a>.</div><p></p>
37541cb0ef41Sopenharmony_ci<ul>
37551cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
37561cb0ef41Sopenharmony_ci</ul>
37571cb0ef41Sopenharmony_ci<p>See <a href="#responsesocket"><code>response.socket</code></a>.</p>
37581cb0ef41Sopenharmony_ci<h5><code>response.createPushResponse(headers, callback)</code><span><a class="mark" href="#responsecreatepushresponseheaders-callback" id="responsecreatepushresponseheaders-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_createpushresponse_headers_callback"></a></h5>
37591cb0ef41Sopenharmony_ci<div class="api_metadata">
37601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
37611cb0ef41Sopenharmony_ci<table>
37621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
37631cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td>
37641cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
37651cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
37661cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
37671cb0ef41Sopenharmony_ci</tbody></table>
37681cb0ef41Sopenharmony_ci</details>
37691cb0ef41Sopenharmony_ci</div>
37701cb0ef41Sopenharmony_ci<ul>
37711cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="http2.html#headers-object" class="type">&#x3C;HTTP/2 Headers Object></a> An object describing the headers</li>
37721cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Called once <code>http2stream.pushStream()</code> is finished,
37731cb0ef41Sopenharmony_cior either when the attempt to create the pushed <code>Http2Stream</code> has failed or
37741cb0ef41Sopenharmony_cihas been rejected, or the state of <code>Http2ServerRequest</code> is closed prior to
37751cb0ef41Sopenharmony_cicalling the <code>http2stream.pushStream()</code> method
37761cb0ef41Sopenharmony_ci<ul>
37771cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
37781cb0ef41Sopenharmony_ci<li><code>res</code> <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> The newly-created <code>Http2ServerResponse</code>
37791cb0ef41Sopenharmony_ciobject</li>
37801cb0ef41Sopenharmony_ci</ul>
37811cb0ef41Sopenharmony_ci</li>
37821cb0ef41Sopenharmony_ci</ul>
37831cb0ef41Sopenharmony_ci<p>Call <a href="#http2streampushstreamheaders-options-callback"><code>http2stream.pushStream()</code></a> with the given headers, and wrap the
37841cb0ef41Sopenharmony_cigiven <a href="#class-http2stream"><code>Http2Stream</code></a> on a newly created <code>Http2ServerResponse</code> as the callback
37851cb0ef41Sopenharmony_ciparameter if successful. When <code>Http2ServerRequest</code> is closed, the callback is
37861cb0ef41Sopenharmony_cicalled with an error <code>ERR_HTTP2_INVALID_STREAM</code>.</p>
37871cb0ef41Sopenharmony_ci<h5><code>response.end([data[, encoding]][, callback])</code><span><a class="mark" href="#responseenddata-encoding-callback" id="responseenddata-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_end_data_encoding_callback"></a></h5>
37881cb0ef41Sopenharmony_ci<div class="api_metadata">
37891cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
37901cb0ef41Sopenharmony_ci<table>
37911cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
37921cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
37931cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>ServerResponse</code>.</p></td></tr>
37941cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
37951cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
37961cb0ef41Sopenharmony_ci</tbody></table>
37971cb0ef41Sopenharmony_ci</details>
37981cb0ef41Sopenharmony_ci</div>
37991cb0ef41Sopenharmony_ci<ul>
38001cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
38011cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
38021cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
38031cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" class="type">&#x3C;this></a></li>
38041cb0ef41Sopenharmony_ci</ul>
38051cb0ef41Sopenharmony_ci<p>This method signals to the server that all of the response headers and body
38061cb0ef41Sopenharmony_cihave been sent; that server should consider this message complete.
38071cb0ef41Sopenharmony_ciThe method, <code>response.end()</code>, MUST be called on each response.</p>
38081cb0ef41Sopenharmony_ci<p>If <code>data</code> is specified, it is equivalent to calling
38091cb0ef41Sopenharmony_ci<a href="http.html#responsewritechunk-encoding-callback"><code>response.write(data, encoding)</code></a> followed by <code>response.end(callback)</code>.</p>
38101cb0ef41Sopenharmony_ci<p>If <code>callback</code> is specified, it will be called when the response stream
38111cb0ef41Sopenharmony_ciis finished.</p>
38121cb0ef41Sopenharmony_ci<h5><code>response.finished</code><span><a class="mark" href="#responsefinished" id="responsefinished">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_finished"></a></h5>
38131cb0ef41Sopenharmony_ci<div class="api_metadata">
38141cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span><span>Deprecated since: v13.4.0, v12.16.0</span>
38151cb0ef41Sopenharmony_ci</div>
38161cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated. Use <a href="#responsewritableended"><code>response.writableEnded</code></a>.</div><p></p>
38171cb0ef41Sopenharmony_ci<ul>
38181cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
38191cb0ef41Sopenharmony_ci</ul>
38201cb0ef41Sopenharmony_ci<p>Boolean value that indicates whether the response has completed. Starts
38211cb0ef41Sopenharmony_cias <code>false</code>. After <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> executes, the value will be <code>true</code>.</p>
38221cb0ef41Sopenharmony_ci<h5><code>response.getHeader(name)</code><span><a class="mark" href="#responsegetheadername" id="responsegetheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheader_name"></a></h5>
38231cb0ef41Sopenharmony_ci<div class="api_metadata">
38241cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38251cb0ef41Sopenharmony_ci</div>
38261cb0ef41Sopenharmony_ci<ul>
38271cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
38281cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
38291cb0ef41Sopenharmony_ci</ul>
38301cb0ef41Sopenharmony_ci<p>Reads out a header that has already been queued but not sent to the client.
38311cb0ef41Sopenharmony_ciThe name is case-insensitive.</p>
38321cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
38331cb0ef41Sopenharmony_ci<h5><code>response.getHeaderNames()</code><span><a class="mark" href="#responsegetheadernames" id="responsegetheadernames">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheadernames"></a></h5>
38341cb0ef41Sopenharmony_ci<div class="api_metadata">
38351cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38361cb0ef41Sopenharmony_ci</div>
38371cb0ef41Sopenharmony_ci<ul>
38381cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
38391cb0ef41Sopenharmony_ci</ul>
38401cb0ef41Sopenharmony_ci<p>Returns an array containing the unique names of the current outgoing headers.
38411cb0ef41Sopenharmony_ciAll header names are lowercase.</p>
38421cb0ef41Sopenharmony_ci<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>);
38431cb0ef41Sopenharmony_ciresponse.<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>]);
38441cb0ef41Sopenharmony_ci
38451cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">getHeaderNames</span>();
38461cb0ef41Sopenharmony_ci<span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code> <button class="copy-button">copy</button></pre>
38471cb0ef41Sopenharmony_ci<h5><code>response.getHeaders()</code><span><a class="mark" href="#responsegetheaders" id="responsegetheaders">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_getheaders"></a></h5>
38481cb0ef41Sopenharmony_ci<div class="api_metadata">
38491cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38501cb0ef41Sopenharmony_ci</div>
38511cb0ef41Sopenharmony_ci<ul>
38521cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
38531cb0ef41Sopenharmony_ci</ul>
38541cb0ef41Sopenharmony_ci<p>Returns a shallow copy of the current outgoing headers. Since a shallow copy
38551cb0ef41Sopenharmony_ciis used, array values may be mutated without additional calls to various
38561cb0ef41Sopenharmony_ciheader-related http module methods. The keys of the returned object are the
38571cb0ef41Sopenharmony_ciheader names and the values are the respective header values. All header names
38581cb0ef41Sopenharmony_ciare lowercase.</p>
38591cb0ef41Sopenharmony_ci<p>The object returned by the <code>response.getHeaders()</code> method <em>does not</em>
38601cb0ef41Sopenharmony_ciprototypically inherit from the JavaScript <code>Object</code>. This means that typical
38611cb0ef41Sopenharmony_ci<code>Object</code> methods such as <code>obj.toString()</code>, <code>obj.hasOwnProperty()</code>, and others
38621cb0ef41Sopenharmony_ciare not defined and <em>will not work</em>.</p>
38631cb0ef41Sopenharmony_ci<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>);
38641cb0ef41Sopenharmony_ciresponse.<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>]);
38651cb0ef41Sopenharmony_ci
38661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHeaders</span>();
38671cb0ef41Sopenharmony_ci<span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</span></code> <button class="copy-button">copy</button></pre>
38681cb0ef41Sopenharmony_ci<h5><code>response.hasHeader(name)</code><span><a class="mark" href="#responsehasheadername" id="responsehasheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_hasheader_name"></a></h5>
38691cb0ef41Sopenharmony_ci<div class="api_metadata">
38701cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38711cb0ef41Sopenharmony_ci</div>
38721cb0ef41Sopenharmony_ci<ul>
38731cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
38741cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
38751cb0ef41Sopenharmony_ci</ul>
38761cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the header identified by <code>name</code> is currently set in the
38771cb0ef41Sopenharmony_cioutgoing headers. The header name matching is case-insensitive.</p>
38781cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
38791cb0ef41Sopenharmony_ci<h5><code>response.headersSent</code><span><a class="mark" href="#responseheaderssent" id="responseheaderssent">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_headerssent"></a></h5>
38801cb0ef41Sopenharmony_ci<div class="api_metadata">
38811cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38821cb0ef41Sopenharmony_ci</div>
38831cb0ef41Sopenharmony_ci<ul>
38841cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
38851cb0ef41Sopenharmony_ci</ul>
38861cb0ef41Sopenharmony_ci<p>True if headers were sent, false otherwise (read-only).</p>
38871cb0ef41Sopenharmony_ci<h5><code>response.removeHeader(name)</code><span><a class="mark" href="#responseremoveheadername" id="responseremoveheadername">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_removeheader_name"></a></h5>
38881cb0ef41Sopenharmony_ci<div class="api_metadata">
38891cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
38901cb0ef41Sopenharmony_ci</div>
38911cb0ef41Sopenharmony_ci<ul>
38921cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
38931cb0ef41Sopenharmony_ci</ul>
38941cb0ef41Sopenharmony_ci<p>Removes a header that has been queued for implicit sending.</p>
38951cb0ef41Sopenharmony_ci<pre><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span class="hljs-string">'Content-Encoding'</span>);</code> <button class="copy-button">copy</button></pre>
38961cb0ef41Sopenharmony_ci<h5><code>response.req</code><span><a class="mark" href="#responsereq" id="responsereq">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_req"></a></h5>
38971cb0ef41Sopenharmony_ci<div class="api_metadata">
38981cb0ef41Sopenharmony_ci<span>Added in: v15.7.0</span>
38991cb0ef41Sopenharmony_ci</div>
39001cb0ef41Sopenharmony_ci<ul>
39011cb0ef41Sopenharmony_ci<li><a href="http2.html#class-http2http2serverrequest" class="type">&#x3C;http2.Http2ServerRequest></a></li>
39021cb0ef41Sopenharmony_ci</ul>
39031cb0ef41Sopenharmony_ci<p>A reference to the original HTTP2 <code>request</code> object.</p>
39041cb0ef41Sopenharmony_ci<h5><code>response.sendDate</code><span><a class="mark" href="#responsesenddate" id="responsesenddate">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_senddate"></a></h5>
39051cb0ef41Sopenharmony_ci<div class="api_metadata">
39061cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39071cb0ef41Sopenharmony_ci</div>
39081cb0ef41Sopenharmony_ci<ul>
39091cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
39101cb0ef41Sopenharmony_ci</ul>
39111cb0ef41Sopenharmony_ci<p>When true, the Date header will be automatically generated and sent in
39121cb0ef41Sopenharmony_cithe response if it is not already present in the headers. Defaults to true.</p>
39131cb0ef41Sopenharmony_ci<p>This should only be disabled for testing; HTTP requires the Date header
39141cb0ef41Sopenharmony_ciin responses.</p>
39151cb0ef41Sopenharmony_ci<h5><code>response.setHeader(name, value)</code><span><a class="mark" href="#responsesetheadername-value" id="responsesetheadername-value">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_setheader_name_value"></a></h5>
39161cb0ef41Sopenharmony_ci<div class="api_metadata">
39171cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39181cb0ef41Sopenharmony_ci</div>
39191cb0ef41Sopenharmony_ci<ul>
39201cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
39211cb0ef41Sopenharmony_ci<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
39221cb0ef41Sopenharmony_ci</ul>
39231cb0ef41Sopenharmony_ci<p>Sets a single header value for implicit headers. If this header already exists
39241cb0ef41Sopenharmony_ciin the to-be-sent headers, its value will be replaced. Use an array of strings
39251cb0ef41Sopenharmony_cihere to send multiple headers with the same name.</p>
39261cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
39271cb0ef41Sopenharmony_ci<p>or</p>
39281cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
39291cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters
39301cb0ef41Sopenharmony_ciwill result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
39311cb0ef41Sopenharmony_ci<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged
39321cb0ef41Sopenharmony_ciwith any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed
39331cb0ef41Sopenharmony_cito <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p>
39341cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
39351cb0ef41Sopenharmony_ci<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> {
39361cb0ef41Sopenharmony_ci  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>);
39371cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
39381cb0ef41Sopenharmony_ci  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> });
39391cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39401cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
39411cb0ef41Sopenharmony_ci<h5><code>response.setTimeout(msecs[, callback])</code><span><a class="mark" href="#responsesettimeoutmsecs-callback" id="responsesettimeoutmsecs-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_settimeout_msecs_callback"></a></h5>
39421cb0ef41Sopenharmony_ci<div class="api_metadata">
39431cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39441cb0ef41Sopenharmony_ci</div>
39451cb0ef41Sopenharmony_ci<ul>
39461cb0ef41Sopenharmony_ci<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
39471cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
39481cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
39491cb0ef41Sopenharmony_ci</ul>
39501cb0ef41Sopenharmony_ci<p>Sets the <a href="#class-http2stream"><code>Http2Stream</code></a>'s timeout value to <code>msecs</code>. If a callback is
39511cb0ef41Sopenharmony_ciprovided, then it is added as a listener on the <code>'timeout'</code> event on
39521cb0ef41Sopenharmony_cithe response object.</p>
39531cb0ef41Sopenharmony_ci<p>If no <code>'timeout'</code> listener is added to the request, the response, or
39541cb0ef41Sopenharmony_cithe server, then <a href="#class-http2stream"><code>Http2Stream</code></a>s are destroyed when they time out. If a
39551cb0ef41Sopenharmony_cihandler is assigned to the request, the response, or the server's <code>'timeout'</code>
39561cb0ef41Sopenharmony_cievents, timed out sockets must be handled explicitly.</p>
39571cb0ef41Sopenharmony_ci<h5><code>response.socket</code><span><a class="mark" href="#responsesocket" id="responsesocket">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_socket"></a></h5>
39581cb0ef41Sopenharmony_ci<div class="api_metadata">
39591cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39601cb0ef41Sopenharmony_ci</div>
39611cb0ef41Sopenharmony_ci<ul>
39621cb0ef41Sopenharmony_ci<li><a href="net.html#class-netsocket" class="type">&#x3C;net.Socket></a> | <a href="tls.html#class-tlstlssocket" class="type">&#x3C;tls.TLSSocket></a></li>
39631cb0ef41Sopenharmony_ci</ul>
39641cb0ef41Sopenharmony_ci<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
39651cb0ef41Sopenharmony_ciapplies getters, setters, and methods based on HTTP/2 logic.</p>
39661cb0ef41Sopenharmony_ci<p><code>destroyed</code>, <code>readable</code>, and <code>writable</code> properties will be retrieved from and
39671cb0ef41Sopenharmony_ciset on <code>response.stream</code>.</p>
39681cb0ef41Sopenharmony_ci<p><code>destroy</code>, <code>emit</code>, <code>end</code>, <code>on</code> and <code>once</code> methods will be called on
39691cb0ef41Sopenharmony_ci<code>response.stream</code>.</p>
39701cb0ef41Sopenharmony_ci<p><code>setTimeout</code> method will be called on <code>response.stream.session</code>.</p>
39711cb0ef41Sopenharmony_ci<p><code>pause</code>, <code>read</code>, <code>resume</code>, and <code>write</code> will throw an error with code
39721cb0ef41Sopenharmony_ci<code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See <a href="#http2session-and-sockets"><code>Http2Session</code> and Sockets</a> for
39731cb0ef41Sopenharmony_cimore information.</p>
39741cb0ef41Sopenharmony_ci<p>All other interactions will be routed directly to the socket.</p>
39751cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http2'</span>);
39761cb0ef41Sopenharmony_ci<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> {
39771cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remoteAddress</span>;
39781cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span class="hljs-property">remotePort</span>;
39791cb0ef41Sopenharmony_ci  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>);
39801cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code> <button class="copy-button">copy</button></pre>
39811cb0ef41Sopenharmony_ci<h5><code>response.statusCode</code><span><a class="mark" href="#responsestatuscode" id="responsestatuscode">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_statuscode"></a></h5>
39821cb0ef41Sopenharmony_ci<div class="api_metadata">
39831cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39841cb0ef41Sopenharmony_ci</div>
39851cb0ef41Sopenharmony_ci<ul>
39861cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
39871cb0ef41Sopenharmony_ci</ul>
39881cb0ef41Sopenharmony_ci<p>When using implicit headers (not calling <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> explicitly),
39891cb0ef41Sopenharmony_cithis property controls the status code that will be sent to the client when
39901cb0ef41Sopenharmony_cithe headers get flushed.</p>
39911cb0ef41Sopenharmony_ci<pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class="hljs-number">404</span>;</code> <button class="copy-button">copy</button></pre>
39921cb0ef41Sopenharmony_ci<p>After response header was sent to the client, this property indicates the
39931cb0ef41Sopenharmony_cistatus code which was sent out.</p>
39941cb0ef41Sopenharmony_ci<h5><code>response.statusMessage</code><span><a class="mark" href="#responsestatusmessage" id="responsestatusmessage">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_statusmessage"></a></h5>
39951cb0ef41Sopenharmony_ci<div class="api_metadata">
39961cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
39971cb0ef41Sopenharmony_ci</div>
39981cb0ef41Sopenharmony_ci<ul>
39991cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
40001cb0ef41Sopenharmony_ci</ul>
40011cb0ef41Sopenharmony_ci<p>Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns
40021cb0ef41Sopenharmony_cian empty string.</p>
40031cb0ef41Sopenharmony_ci<h5><code>response.stream</code><span><a class="mark" href="#responsestream" id="responsestream">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_stream"></a></h5>
40041cb0ef41Sopenharmony_ci<div class="api_metadata">
40051cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
40061cb0ef41Sopenharmony_ci</div>
40071cb0ef41Sopenharmony_ci<ul>
40081cb0ef41Sopenharmony_ci<li><a href="http2.html#class-http2stream" class="type">&#x3C;Http2Stream></a></li>
40091cb0ef41Sopenharmony_ci</ul>
40101cb0ef41Sopenharmony_ci<p>The <a href="#class-http2stream"><code>Http2Stream</code></a> object backing the response.</p>
40111cb0ef41Sopenharmony_ci<h5><code>response.writableEnded</code><span><a class="mark" href="#responsewritableended" id="responsewritableended">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writableended"></a></h5>
40121cb0ef41Sopenharmony_ci<div class="api_metadata">
40131cb0ef41Sopenharmony_ci<span>Added in: v12.9.0</span>
40141cb0ef41Sopenharmony_ci</div>
40151cb0ef41Sopenharmony_ci<ul>
40161cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
40171cb0ef41Sopenharmony_ci</ul>
40181cb0ef41Sopenharmony_ci<p>Is <code>true</code> after <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> has been called. This property
40191cb0ef41Sopenharmony_cidoes not indicate whether the data has been flushed, for this use
40201cb0ef41Sopenharmony_ci<a href="stream.html#writablewritablefinished"><code>writable.writableFinished</code></a> instead.</p>
40211cb0ef41Sopenharmony_ci<h5><code>response.write(chunk[, encoding][, callback])</code><span><a class="mark" href="#responsewritechunk-encoding-callback" id="responsewritechunk-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_write_chunk_encoding_callback"></a></h5>
40221cb0ef41Sopenharmony_ci<div class="api_metadata">
40231cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
40241cb0ef41Sopenharmony_ci</div>
40251cb0ef41Sopenharmony_ci<ul>
40261cb0ef41Sopenharmony_ci<li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
40271cb0ef41Sopenharmony_ci<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
40281cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
40291cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
40301cb0ef41Sopenharmony_ci</ul>
40311cb0ef41Sopenharmony_ci<p>If this method is called and <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> has not been called,
40321cb0ef41Sopenharmony_ciit will switch to implicit header mode and flush the implicit headers.</p>
40331cb0ef41Sopenharmony_ci<p>This sends a chunk of the response body. This method may
40341cb0ef41Sopenharmony_cibe called multiple times to provide successive parts of the body.</p>
40351cb0ef41Sopenharmony_ci<p>In the <code>node:http</code> module, the response body is omitted when the
40361cb0ef41Sopenharmony_cirequest is a HEAD request. Similarly, the <code>204</code> and <code>304</code> responses
40371cb0ef41Sopenharmony_ci<em>must not</em> include a message body.</p>
40381cb0ef41Sopenharmony_ci<p><code>chunk</code> can be a string or a buffer. If <code>chunk</code> is a string,
40391cb0ef41Sopenharmony_cithe second parameter specifies how to encode it into a byte stream.
40401cb0ef41Sopenharmony_ciBy default the <code>encoding</code> is <code>'utf8'</code>. <code>callback</code> will be called when this chunk
40411cb0ef41Sopenharmony_ciof data is flushed.</p>
40421cb0ef41Sopenharmony_ci<p>This is the raw HTTP body and has nothing to do with higher-level multi-part
40431cb0ef41Sopenharmony_cibody encodings that may be used.</p>
40441cb0ef41Sopenharmony_ci<p>The first time <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, it will send the buffered
40451cb0ef41Sopenharmony_ciheader information and the first chunk of the body to the client. The second
40461cb0ef41Sopenharmony_citime <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> is called, Node.js assumes data will be streamed,
40471cb0ef41Sopenharmony_ciand sends the new data separately. That is, the response is buffered up to the
40481cb0ef41Sopenharmony_cifirst chunk of the body.</p>
40491cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the entire data was flushed successfully to the kernel
40501cb0ef41Sopenharmony_cibuffer. Returns <code>false</code> if all or part of the data was queued in user memory.
40511cb0ef41Sopenharmony_ci<code>'drain'</code> will be emitted when the buffer is free again.</p>
40521cb0ef41Sopenharmony_ci<h5><code>response.writeContinue()</code><span><a class="mark" href="#responsewritecontinue" id="responsewritecontinue">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writecontinue"></a></h5>
40531cb0ef41Sopenharmony_ci<div class="api_metadata">
40541cb0ef41Sopenharmony_ci<span>Added in: v8.4.0</span>
40551cb0ef41Sopenharmony_ci</div>
40561cb0ef41Sopenharmony_ci<p>Sends a status <code>100 Continue</code> to the client, indicating that the request body
40571cb0ef41Sopenharmony_cishould be sent. See the <a href="#event-checkcontinue"><code>'checkContinue'</code></a> event on <code>Http2Server</code> and
40581cb0ef41Sopenharmony_ci<code>Http2SecureServer</code>.</p>
40591cb0ef41Sopenharmony_ci<h5><code>response.writeEarlyHints(hints)</code><span><a class="mark" href="#responsewriteearlyhintshints" id="responsewriteearlyhintshints">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writeearlyhints_hints"></a></h5>
40601cb0ef41Sopenharmony_ci<div class="api_metadata">
40611cb0ef41Sopenharmony_ci<span>Added in: v18.11.0</span>
40621cb0ef41Sopenharmony_ci</div>
40631cb0ef41Sopenharmony_ci<ul>
40641cb0ef41Sopenharmony_ci<li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
40651cb0ef41Sopenharmony_ci</ul>
40661cb0ef41Sopenharmony_ci<p>Sends a status <code>103 Early Hints</code> to the client with a Link header,
40671cb0ef41Sopenharmony_ciindicating that the user agent can preload/preconnect the linked resources.
40681cb0ef41Sopenharmony_ciThe <code>hints</code> is an object containing the values of headers to be sent with
40691cb0ef41Sopenharmony_ciearly hints message.</p>
40701cb0ef41Sopenharmony_ci<p><strong>Example</strong></p>
40711cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> earlyHintsLink = <span class="hljs-string">'&#x3C;/styles.css>; rel=preload; as=style'</span>;
40721cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeEarlyHints</span>({
40731cb0ef41Sopenharmony_ci  <span class="hljs-string">'link'</span>: earlyHintsLink,
40741cb0ef41Sopenharmony_ci});
40751cb0ef41Sopenharmony_ci
40761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> earlyHintsLinks = [
40771cb0ef41Sopenharmony_ci  <span class="hljs-string">'&#x3C;/styles.css>; rel=preload; as=style'</span>,
40781cb0ef41Sopenharmony_ci  <span class="hljs-string">'&#x3C;/scripts.js>; rel=preload; as=script'</span>,
40791cb0ef41Sopenharmony_ci];
40801cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeEarlyHints</span>({
40811cb0ef41Sopenharmony_ci  <span class="hljs-string">'link'</span>: earlyHintsLinks,
40821cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
40831cb0ef41Sopenharmony_ci<h5><code>response.writeHead(statusCode[, statusMessage][, headers])</code><span><a class="mark" href="#responsewriteheadstatuscode-statusmessage-headers" id="responsewriteheadstatuscode-statusmessage-headers">#</a></span><a aria-hidden="true" class="legacy" id="http2_response_writehead_statuscode_statusmessage_headers"></a></h5>
40841cb0ef41Sopenharmony_ci<div class="api_metadata">
40851cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
40861cb0ef41Sopenharmony_ci<table>
40871cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
40881cb0ef41Sopenharmony_ci<tr><td>v11.10.0, v10.17.0</td>
40891cb0ef41Sopenharmony_ci<td><p>Return <code>this</code> from <code>writeHead()</code> to allow chaining with <code>end()</code>.</p></td></tr>
40901cb0ef41Sopenharmony_ci<tr><td>v8.4.0</td>
40911cb0ef41Sopenharmony_ci<td><p><span>Added in: v8.4.0</span></p></td></tr>
40921cb0ef41Sopenharmony_ci</tbody></table>
40931cb0ef41Sopenharmony_ci</details>
40941cb0ef41Sopenharmony_ci</div>
40951cb0ef41Sopenharmony_ci<ul>
40961cb0ef41Sopenharmony_ci<li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
40971cb0ef41Sopenharmony_ci<li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
40981cb0ef41Sopenharmony_ci<li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a></li>
40991cb0ef41Sopenharmony_ci<li>Returns: <a href="http2.html#class-http2http2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a></li>
41001cb0ef41Sopenharmony_ci</ul>
41011cb0ef41Sopenharmony_ci<p>Sends a response header to the request. The status code is a 3-digit HTTP
41021cb0ef41Sopenharmony_cistatus code, like <code>404</code>. The last argument, <code>headers</code>, are the response headers.</p>
41031cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>Http2ServerResponse</code>, so that calls can be chained.</p>
41041cb0ef41Sopenharmony_ci<p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</code> may be
41051cb0ef41Sopenharmony_cipassed as the second argument. However, because the <code>statusMessage</code> has no
41061cb0ef41Sopenharmony_cimeaning within HTTP/2, the argument will have no effect and a process warning
41071cb0ef41Sopenharmony_ciwill be emitted.</p>
41081cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-string">'hello world'</span>;
41091cb0ef41Sopenharmony_ciresponse.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
41101cb0ef41Sopenharmony_ci  <span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">byteLength</span>(body),
41111cb0ef41Sopenharmony_ci  <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=utf-8'</span>,
41121cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
41131cb0ef41Sopenharmony_ci<p><code>Content-Length</code> is given in bytes not characters. The
41141cb0ef41Sopenharmony_ci<code>Buffer.byteLength()</code> API may be used to determine the number of bytes in a
41151cb0ef41Sopenharmony_cigiven encoding. On outbound messages, Node.js does not check if Content-Length
41161cb0ef41Sopenharmony_ciand the length of the body being transmitted are equal or not. However, when
41171cb0ef41Sopenharmony_cireceiving messages, Node.js will automatically reject messages when the
41181cb0ef41Sopenharmony_ci<code>Content-Length</code> does not match the actual payload size.</p>
41191cb0ef41Sopenharmony_ci<p>This method may be called at most one time on a message before
41201cb0ef41Sopenharmony_ci<a href="#responseenddata-encoding-callback"><code>response.end()</code></a> is called.</p>
41211cb0ef41Sopenharmony_ci<p>If <a href="#responsewritechunk-encoding-callback"><code>response.write()</code></a> or <a href="#responseenddata-encoding-callback"><code>response.end()</code></a> are called before calling
41221cb0ef41Sopenharmony_cithis, the implicit/mutable headers will be calculated and call this function.</p>
41231cb0ef41Sopenharmony_ci<p>When headers have been set with <a href="#responsesetheadername-value"><code>response.setHeader()</code></a>, they will be merged
41241cb0ef41Sopenharmony_ciwith any headers passed to <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a>, with the headers passed
41251cb0ef41Sopenharmony_cito <a href="#responsewriteheadstatuscode-statusmessage-headers"><code>response.writeHead()</code></a> given precedence.</p>
41261cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</span>
41271cb0ef41Sopenharmony_ci<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> {
41281cb0ef41Sopenharmony_ci  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>);
41291cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, <span class="hljs-string">'bar'</span>);
41301cb0ef41Sopenharmony_ci  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> });
41311cb0ef41Sopenharmony_ci  res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41321cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
41331cb0ef41Sopenharmony_ci<p>Attempting to set a header field name or value that contains invalid characters
41341cb0ef41Sopenharmony_ciwill result in a <a href="errors.html#class-typeerror"><code>TypeError</code></a> being thrown.</p>
41351cb0ef41Sopenharmony_ci</section><section><h3>Collecting HTTP/2 performance metrics<span><a class="mark" href="#collecting-http2-performance-metrics" id="collecting-http2-performance-metrics">#</a></span><a aria-hidden="true" class="legacy" id="http2_collecting_http_2_performance_metrics"></a></h3>
41361cb0ef41Sopenharmony_ci<p>The <a href="perf_hooks.html">Performance Observer</a> API can be used to collect basic performance
41371cb0ef41Sopenharmony_cimetrics for each <code>Http2Session</code> and <code>Http2Stream</code> instance.</p>
41381cb0ef41Sopenharmony_ci<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">'node:perf_hooks'</span>);
41391cb0ef41Sopenharmony_ci
41401cb0ef41Sopenharmony_ci<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> {
41411cb0ef41Sopenharmony_ci  <span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntries</span>()[<span class="hljs-number">0</span>];
41421cb0ef41Sopenharmony_ci  <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>
41431cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span class="hljs-string">'Http2Session'</span>) {
41441cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
41451cb0ef41Sopenharmony_ci  } <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>) {
41461cb0ef41Sopenharmony_ci    <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
41471cb0ef41Sopenharmony_ci  }
41481cb0ef41Sopenharmony_ci});
41491cb0ef41Sopenharmony_ciobs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-string">'http2'</span>] });</code> <button class="copy-button">copy</button></pre>
41501cb0ef41Sopenharmony_ci<p>The <code>entryType</code> property of the <code>PerformanceEntry</code> will be equal to <code>'http2'</code>.</p>
41511cb0ef41Sopenharmony_ci<p>The <code>name</code> property of the <code>PerformanceEntry</code> will be equal to either
41521cb0ef41Sopenharmony_ci<code>'Http2Stream'</code> or <code>'Http2Session'</code>.</p>
41531cb0ef41Sopenharmony_ci<p>If <code>name</code> is equal to <code>Http2Stream</code>, the <code>PerformanceEntry</code> will contain the
41541cb0ef41Sopenharmony_cifollowing additional properties:</p>
41551cb0ef41Sopenharmony_ci<ul>
41561cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>DATA</code> frame bytes received for this
41571cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</li>
41581cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>DATA</code> frame bytes sent for this
41591cb0ef41Sopenharmony_ci<code>Http2Stream</code>.</li>
41601cb0ef41Sopenharmony_ci<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The identifier of the associated <code>Http2Stream</code></li>
41611cb0ef41Sopenharmony_ci<li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between the
41621cb0ef41Sopenharmony_ci<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first <code>DATA</code> frame.</li>
41631cb0ef41Sopenharmony_ci<li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between
41641cb0ef41Sopenharmony_cithe <code>PerformanceEntry</code> <code>startTime</code> and sending of the first <code>DATA</code> frame.</li>
41651cb0ef41Sopenharmony_ci<li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed between the
41661cb0ef41Sopenharmony_ci<code>PerformanceEntry</code> <code>startTime</code> and the reception of the first header.</li>
41671cb0ef41Sopenharmony_ci</ul>
41681cb0ef41Sopenharmony_ci<p>If <code>name</code> is equal to <code>Http2Session</code>, the <code>PerformanceEntry</code> will contain the
41691cb0ef41Sopenharmony_cifollowing additional properties:</p>
41701cb0ef41Sopenharmony_ci<ul>
41711cb0ef41Sopenharmony_ci<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes received for this <code>Http2Session</code>.</li>
41721cb0ef41Sopenharmony_ci<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of bytes sent for this <code>Http2Session</code>.</li>
41731cb0ef41Sopenharmony_ci<li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of HTTP/2 frames received by the
41741cb0ef41Sopenharmony_ci<code>Http2Session</code>.</li>
41751cb0ef41Sopenharmony_ci<li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of HTTP/2 frames sent by the <code>Http2Session</code>.</li>
41761cb0ef41Sopenharmony_ci<li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The maximum number of streams concurrently
41771cb0ef41Sopenharmony_ciopen during the lifetime of the <code>Http2Session</code>.</li>
41781cb0ef41Sopenharmony_ci<li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds elapsed since the transmission
41791cb0ef41Sopenharmony_ciof a <code>PING</code> frame and the reception of its acknowledgment. Only present if
41801cb0ef41Sopenharmony_cia <code>PING</code> frame has been sent on the <code>Http2Session</code>.</li>
41811cb0ef41Sopenharmony_ci<li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The average duration (in milliseconds) for
41821cb0ef41Sopenharmony_ciall <code>Http2Stream</code> instances.</li>
41831cb0ef41Sopenharmony_ci<li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of <code>Http2Stream</code> instances processed by
41841cb0ef41Sopenharmony_cithe <code>Http2Session</code>.</li>
41851cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Either <code>'server'</code> or <code>'client'</code> to identify the type of
41861cb0ef41Sopenharmony_ci<code>Http2Session</code>.</li>
41871cb0ef41Sopenharmony_ci</ul>
41881cb0ef41Sopenharmony_ci</section><section><h3>Note on <code>:authority</code> and <code>host</code><span><a class="mark" href="#note-on-authority-and-host" id="note-on-authority-and-host">#</a></span><a aria-hidden="true" class="legacy" id="http2_note_on_authority_and_host"></a></h3>
41891cb0ef41Sopenharmony_ci<p>HTTP/2 requires requests to have either the <code>:authority</code> pseudo-header
41901cb0ef41Sopenharmony_cior the <code>host</code> header. Prefer <code>:authority</code> when constructing an HTTP/2
41911cb0ef41Sopenharmony_cirequest directly, and <code>host</code> when converting from HTTP/1 (in proxies,
41921cb0ef41Sopenharmony_cifor instance).</p>
41931cb0ef41Sopenharmony_ci<p>The compatibility API falls back to <code>host</code> if <code>:authority</code> is not
41941cb0ef41Sopenharmony_cipresent. See <a href="#requestauthority"><code>request.authority</code></a> for more information. However,
41951cb0ef41Sopenharmony_ciif you don't use the compatibility API (or use <code>req.headers</code> directly),
41961cb0ef41Sopenharmony_ciyou need to implement any fall-back behavior yourself.</p></section>
41971cb0ef41Sopenharmony_ci        <!-- API END -->
41981cb0ef41Sopenharmony_ci      </div>
41991cb0ef41Sopenharmony_ci    </div>
42001cb0ef41Sopenharmony_ci  </div>
42011cb0ef41Sopenharmony_ci</body>
42021cb0ef41Sopenharmony_ci</html>
4203