-
Notifications
You must be signed in to change notification settings - Fork 0
/
http2.html
3325 lines (3185 loc) · 226 KB
/
http2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>HTTP/2 | Node.js v12.0.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/http2.html">
</head>
<body class="alt apidoc" id="api-section-http2">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About these Docs</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="assert.html" class="nav-assert">Assertion Testing</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command Line Options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File System</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2 active">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query Strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace Events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li>
<li><a href="zlib.html" class="nav-zlib">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li>
</ul>
</div>
<div id="column1" data-id="http2" class="interior">
<header>
<h1>Node.js v12.0.0 Documentation</h1>
<div id="gtoc">
<ul>
<li>
<a href="index.html" name="toc">Index</a>
</li>
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="http2.json">View as JSON</a>
</li>
<li class="version-picker">
<a href="#">View another version <span>▼</span></a>
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v11.x/api/http2.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/http2.html">10.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/http2.html">9.x</a></li></ol>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/http2.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
</ul>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li>
<p><span class="stability_2"><a href="#http2_http_2">HTTP/2</a></span></p>
<ul>
<li>
<p><a href="#http2_core_api">Core API</a></p>
<ul>
<li><a href="#http2_server_side_example">Server-side example</a></li>
<li><a href="#http2_client_side_example">Client-side example</a></li>
<li>
<p><a href="#http2_class_http2session">Class: Http2Session</a></p>
<ul>
<li><a href="#http2_http2session_and_sockets">Http2Session and Sockets</a></li>
<li><a href="#http2_event_close">Event: 'close'</a></li>
<li><a href="#http2_event_connect">Event: 'connect'</a></li>
<li><a href="#http2_event_error">Event: 'error'</a></li>
<li><a href="#http2_event_frameerror">Event: 'frameError'</a></li>
<li><a href="#http2_event_goaway">Event: 'goaway'</a></li>
<li><a href="#http2_event_localsettings">Event: 'localSettings'</a></li>
<li><a href="#http2_event_ping">Event: 'ping'</a></li>
<li><a href="#http2_event_remotesettings">Event: 'remoteSettings'</a></li>
<li><a href="#http2_event_stream">Event: 'stream'</a></li>
<li><a href="#http2_event_timeout">Event: 'timeout'</a></li>
<li><a href="#http2_http2session_alpnprotocol">http2session.alpnProtocol</a></li>
<li><a href="#http2_http2session_close_callback">http2session.close([callback])</a></li>
<li><a href="#http2_http2session_closed">http2session.closed</a></li>
<li><a href="#http2_http2session_connecting">http2session.connecting</a></li>
<li><a href="#http2_http2session_destroy_error_code">http2session.destroy([error][, code])</a></li>
<li><a href="#http2_http2session_destroyed">http2session.destroyed</a></li>
<li><a href="#http2_http2session_encrypted">http2session.encrypted</a></li>
<li><a href="#http2_http2session_goaway_code_laststreamid_opaquedata">http2session.goaway([code[, lastStreamID[, opaqueData]]])</a></li>
<li><a href="#http2_http2session_localsettings">http2session.localSettings</a></li>
<li><a href="#http2_http2session_originset">http2session.originSet</a></li>
<li><a href="#http2_http2session_pendingsettingsack">http2session.pendingSettingsAck</a></li>
<li><a href="#http2_http2session_ping_payload_callback">http2session.ping([payload, ]callback)</a></li>
<li><a href="#http2_http2session_ref">http2session.ref()</a></li>
<li><a href="#http2_http2session_remotesettings">http2session.remoteSettings</a></li>
<li><a href="#http2_http2session_settimeout_msecs_callback">http2session.setTimeout(msecs, callback)</a></li>
<li><a href="#http2_http2session_socket">http2session.socket</a></li>
<li><a href="#http2_http2session_state">http2session.state</a></li>
<li><a href="#http2_http2session_settings_settings">http2session.settings(settings)</a></li>
<li><a href="#http2_http2session_type">http2session.type</a></li>
<li><a href="#http2_http2session_unref">http2session.unref()</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_serverhttp2session">Class: ServerHttp2Session</a></p>
<ul>
<li><a href="#http2_serverhttp2session_altsvc_alt_originorstream">serverhttp2session.altsvc(alt, originOrStream)</a></li>
<li><a href="#http2_specifying_alternative_services">Specifying alternative services</a></li>
<li><a href="#http2_serverhttp2session_origin_origins">serverhttp2session.origin(...origins)</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_clienthttp2session">Class: ClientHttp2Session</a></p>
<ul>
<li><a href="#http2_event_altsvc">Event: 'altsvc'</a></li>
<li><a href="#http2_event_origin">Event: 'origin'</a></li>
<li><a href="#http2_clienthttp2session_request_headers_options">clienthttp2session.request(headers[, options])</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_http2stream">Class: Http2Stream</a></p>
<ul>
<li>
<p><a href="#http2_http2stream_lifecycle">Http2Stream Lifecycle</a></p>
<ul>
<li><a href="#http2_creation">Creation</a></li>
<li><a href="#http2_destruction">Destruction</a></li>
</ul>
</li>
<li><a href="#http2_event_aborted">Event: 'aborted'</a></li>
<li><a href="#http2_event_close_1">Event: 'close'</a></li>
<li><a href="#http2_event_error_1">Event: 'error'</a></li>
<li><a href="#http2_event_frameerror_1">Event: 'frameError'</a></li>
<li><a href="#http2_event_timeout_1">Event: 'timeout'</a></li>
<li><a href="#http2_event_trailers">Event: 'trailers'</a></li>
<li><a href="#http2_event_wanttrailers">Event: 'wantTrailers'</a></li>
<li><a href="#http2_http2stream_aborted">http2stream.aborted</a></li>
<li><a href="#http2_http2stream_buffersize">http2stream.bufferSize</a></li>
<li><a href="#http2_http2stream_close_code_callback">http2stream.close(code[, callback])</a></li>
<li><a href="#http2_http2stream_closed">http2stream.closed</a></li>
<li><a href="#http2_http2stream_destroyed">http2stream.destroyed</a></li>
<li><a href="#http2_http2stream_endafterheaders">http2stream.endAfterHeaders</a></li>
<li><a href="#http2_http2stream_pending">http2stream.pending</a></li>
<li><a href="#http2_http2stream_priority_options">http2stream.priority(options)</a></li>
<li><a href="#http2_http2stream_rstcode">http2stream.rstCode</a></li>
<li><a href="#http2_http2stream_sentheaders">http2stream.sentHeaders</a></li>
<li><a href="#http2_http2stream_sentinfoheaders">http2stream.sentInfoHeaders</a></li>
<li><a href="#http2_http2stream_senttrailers">http2stream.sentTrailers</a></li>
<li><a href="#http2_http2stream_session">http2stream.session</a></li>
<li><a href="#http2_http2stream_settimeout_msecs_callback">http2stream.setTimeout(msecs, callback)</a></li>
<li><a href="#http2_http2stream_state">http2stream.state</a></li>
<li><a href="#http2_http2stream_sendtrailers_headers">http2stream.sendTrailers(headers)</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_clienthttp2stream">Class: ClientHttp2Stream</a></p>
<ul>
<li><a href="#http2_event_continue">Event: 'continue'</a></li>
<li><a href="#http2_event_headers">Event: 'headers'</a></li>
<li><a href="#http2_event_push">Event: 'push'</a></li>
<li><a href="#http2_event_response">Event: 'response'</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_serverhttp2stream">Class: ServerHttp2Stream</a></p>
<ul>
<li><a href="#http2_http2stream_additionalheaders_headers">http2stream.additionalHeaders(headers)</a></li>
<li><a href="#http2_http2stream_headerssent">http2stream.headersSent</a></li>
<li><a href="#http2_http2stream_pushallowed">http2stream.pushAllowed</a></li>
<li><a href="#http2_http2stream_pushstream_headers_options_callback">http2stream.pushStream(headers[, options], callback)</a></li>
<li><a href="#http2_http2stream_respond_headers_options">http2stream.respond([headers[, options]])</a></li>
<li><a href="#http2_http2stream_respondwithfd_fd_headers_options">http2stream.respondWithFD(fd[, headers[, options]])</a></li>
<li><a href="#http2_http2stream_respondwithfile_path_headers_options">http2stream.respondWithFile(path[, headers[, options]])</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_http2server">Class: Http2Server</a></p>
<ul>
<li><a href="#http2_event_checkcontinue">Event: 'checkContinue'</a></li>
<li><a href="#http2_event_request">Event: 'request'</a></li>
<li><a href="#http2_event_session">Event: 'session'</a></li>
<li><a href="#http2_event_sessionerror">Event: 'sessionError'</a></li>
<li><a href="#http2_event_stream_1">Event: 'stream'</a></li>
<li><a href="#http2_event_timeout_2">Event: 'timeout'</a></li>
<li><a href="#http2_server_close_callback">server.close([callback])</a></li>
<li><a href="#http2_server_settimeout_msecs_callback">server.setTimeout([msecs][, callback])</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_http2secureserver">Class: Http2SecureServer</a></p>
<ul>
<li><a href="#http2_event_checkcontinue_1">Event: 'checkContinue'</a></li>
<li><a href="#http2_event_request_1">Event: 'request'</a></li>
<li><a href="#http2_event_session_1">Event: 'session'</a></li>
<li><a href="#http2_event_sessionerror_1">Event: 'sessionError'</a></li>
<li><a href="#http2_event_stream_2">Event: 'stream'</a></li>
<li><a href="#http2_event_timeout_3">Event: 'timeout'</a></li>
<li><a href="#http2_event_unknownprotocol">Event: 'unknownProtocol'</a></li>
<li><a href="#http2_server_close_callback_1">server.close([callback])</a></li>
<li><a href="#http2_server_settimeout_msecs_callback_1">server.setTimeout([msecs][, callback])</a></li>
</ul>
</li>
<li><a href="#http2_http2_createserver_options_onrequesthandler">http2.createServer(options[, onRequestHandler])</a></li>
<li><a href="#http2_http2_createsecureserver_options_onrequesthandler">http2.createSecureServer(options[, onRequestHandler])</a></li>
<li><a href="#http2_http2_connect_authority_options_listener">http2.connect(authority[, options][, listener])</a></li>
<li>
<p><a href="#http2_http2_constants">http2.constants</a></p>
<ul>
<li><a href="#http2_error_codes_for_rst_stream_and_goaway">Error Codes for RST_STREAM and GOAWAY</a></li>
</ul>
</li>
<li><a href="#http2_http2_getdefaultsettings">http2.getDefaultSettings()</a></li>
<li><a href="#http2_http2_getpackedsettings_settings">http2.getPackedSettings(settings)</a></li>
<li><a href="#http2_http2_getunpackedsettings_buf">http2.getUnpackedSettings(buf)</a></li>
<li><a href="#http2_headers_object">Headers Object</a></li>
<li><a href="#http2_settings_object">Settings Object</a></li>
<li><a href="#http2_using_options_selectpadding">Using <code>options.selectPadding()</code></a></li>
<li><a href="#http2_error_handling">Error Handling</a></li>
<li><a href="#http2_invalid_character_handling_in_header_names_and_values">Invalid character handling in header names and values</a></li>
<li><a href="#http2_push_streams_on_the_client">Push streams on the client</a></li>
<li><a href="#http2_supporting_the_connect_method">Supporting the CONNECT method</a></li>
<li><a href="#http2_the_extended_connect_protocol">The Extended CONNECT Protocol</a></li>
</ul>
</li>
<li>
<p><a href="#http2_compatibility_api">Compatibility API</a></p>
<ul>
<li><a href="#http2_alpn_negotiation">ALPN negotiation</a></li>
<li>
<p><a href="#http2_class_http2_http2serverrequest">Class: http2.Http2ServerRequest</a></p>
<ul>
<li><a href="#http2_event_aborted_1">Event: 'aborted'</a></li>
<li><a href="#http2_event_close_2">Event: 'close'</a></li>
<li><a href="#http2_request_aborted">request.aborted</a></li>
<li><a href="#http2_request_destroy_error">request.destroy([error])</a></li>
<li><a href="#http2_request_headers">request.headers</a></li>
<li><a href="#http2_request_httpversion">request.httpVersion</a></li>
<li><a href="#http2_request_method">request.method</a></li>
<li><a href="#http2_request_rawheaders">request.rawHeaders</a></li>
<li><a href="#http2_request_rawtrailers">request.rawTrailers</a></li>
<li><a href="#http2_request_settimeout_msecs_callback">request.setTimeout(msecs, callback)</a></li>
<li><a href="#http2_request_socket">request.socket</a></li>
<li><a href="#http2_request_stream">request.stream</a></li>
<li><a href="#http2_request_trailers">request.trailers</a></li>
<li><a href="#http2_request_url">request.url</a></li>
</ul>
</li>
<li>
<p><a href="#http2_class_http2_http2serverresponse">Class: http2.Http2ServerResponse</a></p>
<ul>
<li><a href="#http2_event_close_3">Event: 'close'</a></li>
<li><a href="#http2_event_finish">Event: 'finish'</a></li>
<li><a href="#http2_response_addtrailers_headers">response.addTrailers(headers)</a></li>
<li><a href="#http2_response_connection">response.connection</a></li>
<li><a href="#http2_response_end_data_encoding_callback">response.end([data][, encoding][, callback])</a></li>
<li><a href="#http2_response_finished">response.finished</a></li>
<li><a href="#http2_response_getheader_name">response.getHeader(name)</a></li>
<li><a href="#http2_response_getheadernames">response.getHeaderNames()</a></li>
<li><a href="#http2_response_getheaders">response.getHeaders()</a></li>
<li><a href="#http2_response_hasheader_name">response.hasHeader(name)</a></li>
<li><a href="#http2_response_headerssent">response.headersSent</a></li>
<li><a href="#http2_response_removeheader_name">response.removeHeader(name)</a></li>
<li><a href="#http2_response_senddate">response.sendDate</a></li>
<li><a href="#http2_response_setheader_name_value">response.setHeader(name, value)</a></li>
<li><a href="#http2_response_settimeout_msecs_callback">response.setTimeout(msecs[, callback])</a></li>
<li><a href="#http2_response_socket">response.socket</a></li>
<li><a href="#http2_response_statuscode">response.statusCode</a></li>
<li><a href="#http2_response_statusmessage">response.statusMessage</a></li>
<li><a href="#http2_response_stream">response.stream</a></li>
<li><a href="#http2_response_write_chunk_encoding_callback">response.write(chunk[, encoding][, callback])</a></li>
<li><a href="#http2_response_writecontinue">response.writeContinue()</a></li>
<li><a href="#http2_response_writehead_statuscode_statusmessage_headers">response.writeHead(statusCode[, statusMessage][, headers])</a></li>
<li><a href="#http2_response_createpushresponse_headers_callback">response.createPushResponse(headers, callback)</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#http2_collecting_http_2_performance_metrics">Collecting HTTP/2 Performance Metrics</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>HTTP/2<span><a class="mark" href="#http2_http_2" id="http2_http_2">#</a></span></h1>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.10.0</td>
<td><p>HTTP/2 is now Stable. Previously, it had been Experimental.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p><span>Added in: v8.4.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
<p>The <code>http2</code> module provides an implementation of the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2</a> protocol. It
can be accessed using:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);</code></pre>
<h2>Core API<span><a class="mark" href="#http2_core_api" id="http2_core_api">#</a></span></h2>
<p>The Core API provides a low-level interface designed specifically around
support for HTTP/2 protocol features. It is specifically <em>not</em> designed for
compatibility with the existing <a href="http.html">HTTP/1</a> module API. However,
the <a href="#http2_compatibility_api">Compatibility API</a> is.</p>
<p>The <code>http2</code> Core API is much more symmetric between client and server than the
<code>http</code> API. For instance, most events, like <code>'error'</code>, <code>'connect'</code> and
<code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
<h3>Server-side example<span><a class="mark" href="#http2_server_side_example" id="http2_server_side_example">#</a></span></h3>
<p>The following illustrates a simple HTTP/2 server using the Core API.
Since there are no browsers known that support
<a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the use of
<a href="#http2_http2_createsecureserver_options_onrequesthandler"><code>http2.createSecureServer()</code></a> is necessary when communicating
with browser clients.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
<span class="hljs-keyword">const</span> server = http2.createSecureServer({
<span class="hljs-attr">key</span>: fs.readFileSync(<span class="hljs-string">'localhost-privkey.pem'</span>),
<span class="hljs-attr">cert</span>: fs.readFileSync(<span class="hljs-string">'localhost-cert.pem'</span>)
});
server.on(<span class="hljs-string">'error'</span>, (err) => <span class="hljs-built_in">console</span>.error(err));
server.on(<span class="hljs-string">'stream'</span>, (stream, headers) => {
<span class="hljs-comment">// stream is a Duplex</span>
stream.respond({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
});
stream.end(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.listen(<span class="hljs-number">8443</span>);</code></pre>
<p>To generate the certificate and key for this example, run:</p>
<pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span class="hljs-string">'/CN=localhost'</span> \
-keyout localhost-privkey.pem -out localhost-cert.pem</code></pre>
<h3>Client-side example<span><a class="mark" href="#http2_client_side_example" id="http2_client_side_example">#</a></span></h3>
<p>The following illustrates an HTTP/2 client:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
<span class="hljs-keyword">const</span> client = http2.connect(<span class="hljs-string">'https://localhost:8443'</span>, {
<span class="hljs-attr">ca</span>: fs.readFileSync(<span class="hljs-string">'localhost-cert.pem'</span>)
});
client.on(<span class="hljs-string">'error'</span>, (err) => <span class="hljs-built_in">console</span>.error(err));
<span class="hljs-keyword">const</span> req = client.request({ <span class="hljs-string">':path'</span>: <span class="hljs-string">'/'</span> });
req.on(<span class="hljs-string">'response'</span>, (headers, flags) => {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> headers) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${name}</span>: <span class="hljs-subst">${headers[name]}</span>`</span>);
}
});
req.setEncoding(<span class="hljs-string">'utf8'</span>);
<span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
req.on(<span class="hljs-string">'data'</span>, (chunk) => { data += chunk; });
req.on(<span class="hljs-string">'end'</span>, () => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`\n<span class="hljs-subst">${data}</span>`</span>);
client.close();
});
req.end();</code></pre>
<h3>Class: Http2Session<span><a class="mark" href="#http2_class_http2session" id="http2_class_http2session">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>Extends: <a href="events.html#events_class_eventemitter" class="type"><EventEmitter></a></li>
</ul>
<p>Instances of the <code>http2.Http2Session</code> class represent an active communications
session between an HTTP/2 client and server. Instances of this class are <em>not</em>
intended to be constructed directly by user code.</p>
<p>Each <code>Http2Session</code> instance will exhibit slightly different behaviors
depending on whether it is operating as a server or a client. The
<code>http2session.type</code> property can be used to determine the mode in which an
<code>Http2Session</code> is operating. On the server side, user code should rarely
have occasion to work with the <code>Http2Session</code> object directly, with most
actions typically taken through interactions with either the <code>Http2Server</code> or
<code>Http2Stream</code> objects.</p>
<p>User code will not create <code>Http2Session</code> instances directly. Server-side
<code>Http2Session</code> instances are created by the <code>Http2Server</code> instance when a
new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
created using the <code>http2.connect()</code> method.</p>
<h4>Http2Session and Sockets<span><a class="mark" href="#http2_http2session_and_sockets" id="http2_http2session_and_sockets">#</a></span></h4>
<p>Every <code>Http2Session</code> instance is associated with exactly one <a href="net.html#net_class_net_socket"><code>net.Socket</code></a> or
<a href="tls.html#tls_class_tls_tlssocket"><code>tls.TLSSocket</code></a> when it is created. When either the <code>Socket</code> or the
<code>Http2Session</code> are destroyed, both will be destroyed.</p>
<p>Because the of the specific serialization and processing requirements imposed
by the HTTP/2 protocol, it is not recommended for user code to read data from
or write data to a <code>Socket</code> instance bound to a <code>Http2Session</code>. Doing so can
put the HTTP/2 session into an indeterminate state causing the session and
the socket to become unusable.</p>
<p>Once a <code>Socket</code> has been bound to an <code>Http2Session</code>, user code should rely
solely on the API of the <code>Http2Session</code>.</p>
<h4>Event: 'close'<span><a class="mark" href="#http2_event_close" id="http2_event_close">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>The <code>'close'</code> event is emitted once the <code>Http2Session</code> has been destroyed. Its
listener does not expect any arguments.</p>
<h4>Event: 'connect'<span><a class="mark" href="#http2_event_connect" id="http2_event_connect">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>session</code> <a href="http2.html#http2_class_http2session" class="type"><Http2Session></a></li>
<li><code>socket</code> <a href="net.html#net_class_net_socket" class="type"><net.Socket></a></li>
</ul>
<p>The <code>'connect'</code> event is emitted once the <code>Http2Session</code> has been successfully
connected to the remote peer and communication may begin.</p>
<p>User code will typically not listen for this event directly.</p>
<h4>Event: 'error'<span><a class="mark" href="#http2_event_error" id="http2_event_error">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>The <code>'error'</code> event is emitted when an error occurs during the processing of
an <code>Http2Session</code>.</p>
<h4>Event: 'frameError'<span><a class="mark" href="#http2_event_frameerror" id="http2_event_frameerror">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The frame type.</li>
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The error code.</li>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The stream id (or <code>0</code> if the frame isn't associated with a
stream).</li>
</ul>
<p>The <code>'frameError'</code> event is emitted when an error occurs while attempting to
send a frame on the session. If the frame that could not be sent is associated
with a specific <code>Http2Stream</code>, an attempt to emit <code>'frameError'</code> event on the
<code>Http2Stream</code> is made.</p>
<p>If the <code>'frameError'</code> event is associated with a stream, the stream will be
closed and destroyed immediately following the <code>'frameError'</code> event. If the
event is not associated with a stream, the <code>Http2Session</code> will be shut down
immediately following the <code>'frameError'</code> event.</p>
<h4>Event: 'goaway'<span><a class="mark" href="#http2_event_goaway" id="http2_event_goaway">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The HTTP/2 error code specified in the <code>GOAWAY</code> frame.</li>
<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ID of the last stream the remote peer successfully
processed (or <code>0</code> if no ID is specified).</li>
<li><code>opaqueData</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> If additional opaque data was included in the <code>GOAWAY</code>
frame, a <code>Buffer</code> instance will be passed containing that data.</li>
</ul>
<p>The <code>'goaway'</code> event is emitted when a <code>GOAWAY</code> frame is received.</p>
<p>The <code>Http2Session</code> instance will be shut down automatically when the <code>'goaway'</code>
event is emitted.</p>
<h4>Event: 'localSettings'<span><a class="mark" href="#http2_event_localsettings" id="http2_event_localsettings">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type"><HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
</ul>
<p>The <code>'localSettings'</code> event is emitted when an acknowledgment <code>SETTINGS</code> frame
has been received.</p>
<p>When using <code>http2session.settings()</code> to submit new settings, the modified
settings do not take effect until the <code>'localSettings'</code> event is emitted.</p>
<pre><code class="language-js">session.settings({ <span class="hljs-attr">enablePush</span>: <span class="hljs-literal">false</span> });
session.on(<span class="hljs-string">'localSettings'</span>, (settings) => {
<span class="hljs-comment">/* Use the new settings */</span>
});</code></pre>
<h4>Event: 'ping'<span><a class="mark" href="#http2_event_ping" id="http2_event_ping">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<li><code>payload</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> The <code>PING</code> frame 8-byte payload</li>
</ul>
<p>The <code>'ping'</code> event is emitted whenever a <code>PING</code> frame is received from the
connected peer.</p>
<h4>Event: 'remoteSettings'<span><a class="mark" href="#http2_event_remotesettings" id="http2_event_remotesettings">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type"><HTTP/2 Settings Object></a> A copy of the <code>SETTINGS</code> frame received.</li>
</ul>
<p>The <code>'remoteSettings'</code> event is emitted when a new <code>SETTINGS</code> frame is received
from the connected peer.</p>
<pre><code class="language-js">session.on(<span class="hljs-string">'remoteSettings'</span>, (settings) => {
<span class="hljs-comment">/* Use the new settings */</span>
});</code></pre>
<h4>Event: 'stream'<span><a class="mark" href="#http2_event_stream" id="http2_event_stream">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>stream</code> <a href="http2.html#http2_class_http2stream" class="type"><Http2Stream></a> A reference to the stream</li>
<li><code>headers</code> <a href="http2.html#http2_headers_object" class="type"><HTTP/2 Headers Object></a> An object describing the headers</li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The associated numeric flags</li>
<li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> An array containing the raw header names followed by
their respective values.</li>
</ul>
<p>The <code>'stream'</code> event is emitted when a new <code>Http2Stream</code> is created.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
session.on(<span class="hljs-string">'stream'</span>, (stream, headers, flags) => {
<span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span>];
<span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
<span class="hljs-comment">// ...</span>
stream.respond({
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>
});
stream.write(<span class="hljs-string">'hello '</span>);
stream.end(<span class="hljs-string">'world'</span>);
});</code></pre>
<p>On the server side, user code will typically not listen for this event directly,
and would instead register a handler for the <code>'stream'</code> event emitted by the
<code>net.Server</code> or <code>tls.Server</code> instances returned by <code>http2.createServer()</code> and
<code>http2.createSecureServer()</code>, respectively, as in the example below:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
<span class="hljs-keyword">const</span> server = http2.createServer();
server.on(<span class="hljs-string">'stream'</span>, (stream, headers) => {
stream.respond({
<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html'</span>,
<span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
});
stream.on(<span class="hljs-string">'error'</span>, (error) => <span class="hljs-built_in">console</span>.error(error));
stream.end(<span class="hljs-string">'<h1>Hello World</h1>'</span>);
});
server.listen(<span class="hljs-number">80</span>);</code></pre>
<p>Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence,
a network error will destroy each individual stream and must be handled on the
stream level, as shown above.</p>
<h4>Event: 'timeout'<span><a class="mark" href="#http2_event_timeout" id="http2_event_timeout">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>After the <code>http2session.setTimeout()</code> method is used to set the timeout period
for this <code>Http2Session</code>, the <code>'timeout'</code> event is emitted if there is no
activity on the <code>Http2Session</code> after the configured number of milliseconds.</p>
<pre><code class="language-js">session.setTimeout(<span class="hljs-number">2000</span>);
session.on(<span class="hljs-string">'timeout'</span>, () => { <span class="hljs-comment">/* .. */</span> });</code></pre>
<h4>http2session.alpnProtocol<span><a class="mark" href="#http2_http2session_alpnprotocol" id="http2_http2session_alpnprotocol">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
</ul>
<p>Value will be <code>undefined</code> if the <code>Http2Session</code> is not yet connected to a
socket, <code>h2c</code> if the <code>Http2Session</code> is not connected to a <code>TLSSocket</code>, or
will return the value of the connected <code>TLSSocket</code>'s own <code>alpnProtocol</code>
property.</p>
<h4>http2session.close([callback])<span><a class="mark" href="#http2_http2session_close_callback" id="http2_http2session_close_callback">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Gracefully closes the <code>Http2Session</code>, allowing any existing streams to
complete on their own and preventing new <code>Http2Stream</code> instances from being
created. Once closed, <code>http2session.destroy()</code> <em>might</em> be called if there
are no open <code>Http2Stream</code> instances.</p>
<p>If specified, the <code>callback</code> function is registered as a handler for the
<code>'close'</code> event.</p>
<h4>http2session.closed<span><a class="mark" href="#http2_http2session_closed" id="http2_http2session_closed">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been closed, otherwise
<code>false</code>.</p>
<h4>http2session.connecting<span><a class="mark" href="#http2_http2session_connecting" id="http2_http2session_connecting">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance is still connecting, will be set
to <code>false</code> before emitting <code>connect</code> event and/or calling the <code>http2.connect</code>
callback.</p>
<h4>http2session.destroy([error][, code])<span><a class="mark" href="#http2_http2session_destroy_error_code" id="http2_http2session_destroy_error_code">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> An <code>Error</code> object if the <code>Http2Session</code> is being destroyed
due to an error.</li>
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The HTTP/2 error code to send in the final <code>GOAWAY</code> frame.
If unspecified, and <code>error</code> is not undefined, the default is <code>INTERNAL_ERROR</code>,
otherwise defaults to <code>NO_ERROR</code>.</li>
</ul>
<p>Immediately terminates the <code>Http2Session</code> and the associated <code>net.Socket</code> or
<code>tls.TLSSocket</code>.</p>
<p>Once destroyed, the <code>Http2Session</code> will emit the <code>'close'</code> event. If <code>error</code>
is not undefined, an <code>'error'</code> event will be emitted immediately before the
<code>'close'</code> event.</p>
<p>If there are any remaining open <code>Http2Streams</code> associated with the
<code>Http2Session</code>, those will also be destroyed.</p>
<h4>http2session.destroyed<span><a class="mark" href="#http2_http2session_destroyed" id="http2_http2session_destroyed">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Will be <code>true</code> if this <code>Http2Session</code> instance has been destroyed and must no
longer be used, otherwise <code>false</code>.</p>
<h4>http2session.encrypted<span><a class="mark" href="#http2_http2session_encrypted" id="http2_http2session_encrypted">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
</ul>
<p>Value is <code>undefined</code> if the <code>Http2Session</code> session socket has not yet been
connected, <code>true</code> if the <code>Http2Session</code> is connected with a <code>TLSSocket</code>,
and <code>false</code> if the <code>Http2Session</code> is connected to any other kind of socket
or stream.</p>
<h4>http2session.goaway([code[, lastStreamID[, opaqueData]]])<span><a class="mark" href="#http2_http2session_goaway_code_laststreamid_opaquedata" id="http2_http2session_goaway_code_laststreamid_opaquedata">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> An HTTP/2 error code</li>
<li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric ID of the last processed <code>Http2Stream</code></li>
<li><code>opaqueData</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A <code>TypedArray</code> or <code>DataView</code>
instance containing additional data to be carried within the <code>GOAWAY</code> frame.</li>
</ul>
<p>Transmits a <code>GOAWAY</code> frame to the connected peer <em>without</em> shutting down the
<code>Http2Session</code>.</p>
<h4>http2session.localSettings<span><a class="mark" href="#http2_http2session_localsettings" id="http2_http2session_localsettings">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#http2_settings_object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>A prototype-less object describing the current local settings of this
<code>Http2Session</code>. The local settings are local to <em>this</em> <code>Http2Session</code> instance.</p>
<h4>http2session.originSet<span><a class="mark" href="#http2_http2session_originset" id="http2_http2session_originset">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
</ul>
<p>If the <code>Http2Session</code> is connected to a <code>TLSSocket</code>, the <code>originSet</code> property
will return an <code>Array</code> of origins for which the <code>Http2Session</code> may be
considered authoritative.</p>
<p>The <code>originSet</code> property is only available when using a secure TLS connection.</p>
<h4>http2session.pendingSettingsAck<span><a class="mark" href="#http2_http2session_pendingsettingsack" id="http2_http2session_pendingsettingsack">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Indicates whether or not the <code>Http2Session</code> is currently waiting for an
acknowledgment for a sent <code>SETTINGS</code> frame. Will be <code>true</code> after calling the
<code>http2session.settings()</code> method. Will be <code>false</code> once all sent SETTINGS
frames have been acknowledged.</p>
<h4>http2session.ping([payload, ]callback)<span><a class="mark" href="#http2_http2session_ping_payload_callback" id="http2_http2session_ping_payload_callback">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.9.3</span>
</div>
<ul>
<li><code>payload</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> Optional ping payload.</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Sends a <code>PING</code> frame to the connected HTTP/2 peer. A <code>callback</code> function must
be provided. The method will return <code>true</code> if the <code>PING</code> was sent, <code>false</code>
otherwise.</p>
<p>The maximum number of outstanding (unacknowledged) pings is determined by the
<code>maxOutstandingPings</code> configuration option. The default maximum is 10.</p>
<p>If provided, the <code>payload</code> must be a <code>Buffer</code>, <code>TypedArray</code>, or <code>DataView</code>
containing 8 bytes of data that will be transmitted with the <code>PING</code> and
returned with the ping acknowledgment.</p>
<p>The callback will be invoked with three arguments: an error argument that will
be <code>null</code> if the <code>PING</code> was successfully acknowledged, a <code>duration</code> argument
that reports the number of milliseconds elapsed since the ping was sent and the
acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
payload.</p>
<pre><code class="language-js">session.ping(Buffer.from(<span class="hljs-string">'abcdefgh'</span>), (err, duration, payload) => {
<span class="hljs-keyword">if</span> (!err) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Ping acknowledged in <span class="hljs-subst">${duration}</span> milliseconds`</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`With payload '<span class="hljs-subst">${payload.toString()}</span>'`</span>);
}
});</code></pre>
<p>If the <code>payload</code> argument is not specified, the default payload will be the
64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
<h4>http2session.ref()<span><a class="mark" href="#http2_http2session_ref" id="http2_http2session_ref">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<p>Calls <a href="net.html#net_socket_ref"><code>ref()</code></a> on this <code>Http2Session</code>
instance's underlying <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p>
<h4>http2session.remoteSettings<span><a class="mark" href="#http2_http2session_remotesettings" id="http2_http2session_remotesettings">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="http2.html#http2_settings_object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>A prototype-less object describing the current remote settings of this
<code>Http2Session</code>. The remote settings are set by the <em>connected</em> HTTP/2 peer.</p>
<h4>http2session.setTimeout(msecs, callback)<span><a class="mark" href="#http2_http2session_settimeout_msecs_callback" id="http2_http2session_settimeout_msecs_callback">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Used to set a callback function that is called when there is no activity on
the <code>Http2Session</code> after <code>msecs</code> milliseconds. The given <code>callback</code> is
registered as a listener on the <code>'timeout'</code> event.</p>
<h4>http2session.socket<span><a class="mark" href="#http2_http2session_socket" id="http2_http2session_socket">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="net.html#net_class_net_socket" class="type"><net.Socket></a> | <a href="tls.html#tls_class_tls_tlssocket" class="type"><tls.TLSSocket></a></li>
</ul>
<p>Returns a <code>Proxy</code> object that acts as a <code>net.Socket</code> (or <code>tls.TLSSocket</code>) but
limits available methods to ones safe to use with HTTP/2.</p>
<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
an error with code <code>ERR_HTTP2_NO_SOCKET_MANIPULATION</code>. See
<a href="#http2_http2session_and_sockets"><code>Http2Session</code> and Sockets</a> for more information.</p>
<p><code>setTimeout</code> method will be called on this <code>Http2Session</code>.</p>
<p>All other interactions will be routed directly to the socket.</p>
<h4>http2session.state<span><a class="mark" href="#http2_http2session_state" id="http2_http2session_state">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<p>Provides miscellaneous information about the current state of the
<code>Http2Session</code>.</p>
<ul>
<li>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current local (receive)
flow control window size for the <code>Http2Session</code>.</li>
<li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current number of bytes
that have been received since the last flow control <code>WINDOW_UPDATE</code>.</li>
<li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric identifier to be used the
next time a new <code>Http2Stream</code> is created by this <code>Http2Session</code>.</li>
<li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes that the remote peer can
send without receiving a <code>WINDOW_UPDATE</code>.</li>
<li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric id of the <code>Http2Stream</code>
for which a <code>HEADERS</code> or <code>DATA</code> frame was most recently received.</li>
<li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes that this <code>Http2Session</code>
may send without receiving a <code>WINDOW_UPDATE</code>.</li>
<li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of frames currently within the
outbound queue for this <code>Http2Session</code>.</li>
<li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current size in bytes of the
outbound header compression state table.</li>
<li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The current size in bytes of the
inbound header compression state table.</li>
</ul>
</li>
</ul>
<p>An object describing the current status of this <code>Http2Session</code>.</p>
<h4>http2session.settings(settings)<span><a class="mark" href="#http2_http2session_settings_settings" id="http2_http2session_settings_settings">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><code>settings</code> <a href="http2.html#http2_settings_object" class="type"><HTTP/2 Settings Object></a></li>
</ul>
<p>Updates the current local settings for this <code>Http2Session</code> and sends a new
<code>SETTINGS</code> frame to the connected HTTP/2 peer.</p>
<p>Once called, the <code>http2session.pendingSettingsAck</code> property will be <code>true</code>
while the session is waiting for the remote peer to acknowledge the new
settings.</p>
<p>The new settings will not become effective until the <code>SETTINGS</code> acknowledgment
is received and the <code>'localSettings'</code> event is emitted. It is possible to send
multiple <code>SETTINGS</code> frames while acknowledgment is still pending.</p>
<h4>http2session.type<span><a class="mark" href="#http2_http2session_type" id="http2_http2session_type">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>http2session.type</code> will be equal to
<code>http2.constants.NGHTTP2_SESSION_SERVER</code> if this <code>Http2Session</code> instance is a
server, and <code>http2.constants.NGHTTP2_SESSION_CLIENT</code> if the instance is a
client.</p>
<h4>http2session.unref()<span><a class="mark" href="#http2_http2session_unref" id="http2_http2session_unref">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<p>Calls <a href="net.html#net_socket_unref"><code>unref()</code></a> on this <code>Http2Session</code>
instance's underlying <a href="net.html#net_class_net_socket"><code>net.Socket</code></a>.</p>
<h3>Class: ServerHttp2Session<span><a class="mark" href="#http2_class_serverhttp2session" id="http2_class_serverhttp2session">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<h4>serverhttp2session.altsvc(alt, originOrStream)<span><a class="mark" href="#http2_serverhttp2session_altsvc_alt_originorstream" id="http2_serverhttp2session_altsvc_alt_originorstream">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A description of the alternative service configuration as
defined by <a href="https://tools.ietf.org/html/rfc7838">RFC 7838</a>.</li>
<li><code>originOrStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Either a URL string specifying
the origin (or an <code>Object</code> with an <code>origin</code> property) or the numeric
identifier of an active <code>Http2Stream</code> as given by the <code>http2stream.id</code>
property.</li>
</ul>
<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>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> server = http2.createServer();
server.on(<span class="hljs-string">'session'</span>, (session) => {
<span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
session.altsvc(<span class="hljs-string">'h2=":8000"'</span>, <span class="hljs-string">'https://example.org:80'</span>);
});
server.on(<span class="hljs-string">'stream'</span>, (stream) => {
<span class="hljs-comment">// Set altsvc for a specific stream</span>
stream.session.altsvc(<span class="hljs-string">'h2=":8000"'</span>, stream.id);
});</code></pre>
<p>Sending an <code>ALTSVC</code> frame with a specific stream ID indicates that the alternate
service is associated with the origin of the given <code>Http2Stream</code>.</p>
<p>The <code>alt</code> and origin string <em>must</em> contain only ASCII bytes and are
strictly interpreted as a sequence of ASCII bytes. The special value <code>'clear'</code>
may be passed to clear any previously set alternative service for a given
domain.</p>
<p>When a string is passed for the <code>originOrStream</code> argument, it will be parsed as
a URL and the origin will be derived. For instance, the origin for the
HTTP URL <code>'https://example.org/foo/bar'</code> is the ASCII string
<code>'https://example.org'</code>. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.</p>
<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
<code>originOrStream</code>, in which case the value of the <code>origin</code> property will be
used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
ASCII origin.</p>
<h4>Specifying alternative services<span><a class="mark" href="#http2_specifying_alternative_services" id="http2_specifying_alternative_services">#</a></span></h4>
<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
ASCII string containing a comma-delimited list of "alternative" protocols
associated with a specific host and port.</p>
<p>For example, the value <code>'h2="example.org:81"'</code> indicates that the HTTP/2
protocol is available on the host <code>'example.org'</code> on TCP/IP port 81. The
host and port <em>must</em> be contained within the quote (<code>"</code>) characters.</p>
<p>Multiple alternatives may be specified, for instance: <code>'h2="example.org:81", h2=":82"'</code>.</p>
<p>The protocol identifier (<code>'h2'</code> in the examples) may be any valid
<a href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">ALPN Protocol ID</a>.</p>
<p>The syntax of these values is not validated by the Node.js implementation and
are passed through as provided by the user or received from the peer.</p>
<h4>serverhttp2session.origin(...origins)<span><a class="mark" href="#http2_serverhttp2session_origin_origins" id="http2_serverhttp2session_origin_origins">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> One or more URL Strings passed as
separate arguments.</li>
</ul>
<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
to advertise the set of origins for which the server is capable of providing
authoritative responses.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> options = getSecureOptionsSomehow();
<span class="hljs-keyword">const</span> server = http2.createSecureServer(options);
server.on(<span class="hljs-string">'stream'</span>, (stream) => {
stream.respond();
stream.end(<span class="hljs-string">'ok'</span>);
});
server.on(<span class="hljs-string">'session'</span>, (session) => {
session.origin(<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>);
});</code></pre>
<p>When a string is passed as an <code>origin</code>, it will be parsed as a URL and the
origin will be derived. For instance, the origin for the HTTP URL
<code>'https://example.org/foo/bar'</code> is the ASCII string
<code>'https://example.org'</code>. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.</p>
<p>A <code>URL</code> object, or any object with an <code>origin</code> property, may be passed as
an <code>origin</code>, in which case the value of the <code>origin</code> property will be
used. The value of the <code>origin</code> property <em>must</em> be a properly serialized
ASCII origin.</p>
<p>Alternatively, the <code>origins</code> option may be used when creating a new HTTP/2
server using the <code>http2.createSecureServer()</code> method:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> options = getSecureOptionsSomehow();
options.origins = [<span class="hljs-string">'https://example.com'</span>, <span class="hljs-string">'https://example.org'</span>];
<span class="hljs-keyword">const</span> server = http2.createSecureServer(options);
server.on(<span class="hljs-string">'stream'</span>, (stream) => {
stream.respond();
stream.end(<span class="hljs-string">'ok'</span>);
});</code></pre>
<h3>Class: ClientHttp2Session<span><a class="mark" href="#http2_class_clienthttp2session" id="http2_class_clienthttp2session">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<h4>Event: 'altsvc'<span><a class="mark" href="#http2_event_altsvc" id="http2_event_altsvc">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v9.4.0</span>
</div>
<ul>
<li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The <code>'altsvc'</code> event is emitted whenever an <code>ALTSVC</code> frame is received by
the client. The event is emitted with the <code>ALTSVC</code> value, origin, and stream
ID. If no <code>origin</code> is provided in the <code>ALTSVC</code> frame, <code>origin</code> will
be an empty string.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> client = http2.connect(<span class="hljs-string">'https://example.org'</span>);
client.on(<span class="hljs-string">'altsvc'</span>, (alt, origin, streamId) => {
<span class="hljs-built_in">console</span>.log(alt);
<span class="hljs-built_in">console</span>.log(origin);
<span class="hljs-built_in">console</span>.log(streamId);
});</code></pre>
<h4>Event: 'origin'<span><a class="mark" href="#http2_event_origin" id="http2_event_origin">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v10.12.0</span>
</div>
<ul>
<li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
</ul>
<p>The <code>'origin'</code> event is emitted whenever an <code>ORIGIN</code> frame is received by
the client. The event is emitted with an array of <code>origin</code> strings. The
<code>http2session.originSet</code> will be updated to include the received
origins.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http2'</span>);
<span class="hljs-keyword">const</span> client = http2.connect(<span class="hljs-string">'https://example.org'</span>);
client.on(<span class="hljs-string">'origin'</span>, (origins) => {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number">0</span>; n < origins.length; n++)
<span class="hljs-built_in">console</span>.log(origins[n]);
});</code></pre>
<p>The <code>'origin'</code> event is only emitted when using a secure TLS connection.</p>
<h4>clienthttp2session.request(headers[, options])<span><a class="mark" href="#http2_clienthttp2session_request_headers_options" id="http2_clienthttp2session_request_headers_options">#</a></span></h4>
<div class="api_metadata">
<span>Added in: v8.4.0</span>
</div>
<ul>
<li>
<p><code>headers</code> <a href="http2.html#http2_headers_object" class="type"><HTTP/2 Headers Object></a></p>