-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp2.d.ts
More file actions
2100 lines (2100 loc) · 110 KB
/
http2.d.ts
File metadata and controls
2100 lines (2100 loc) · 110 KB
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
/**
* The `http2` module provides an implementation of the [HTTP/2](https://tools.ietf.org/html/rfc7540) protocol. It
* can be accessed using:
*
* ```js
* const http2 = require('http2');
* ```
* @since v8.4.0
* @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/http2.js)
*/
declare module 'http2' {
import EventEmitter = require('node:events');
import * as fs from 'node:fs';
import * as net from 'node:net';
import * as stream from 'node:stream';
import * as tls from 'node:tls';
import * as url from 'node:url';
import { IncomingHttpHeaders as Http1IncomingHttpHeaders, OutgoingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
export { OutgoingHttpHeaders } from 'node:http';
export interface IncomingHttpStatusHeader {
':status'?: number | undefined;
}
export interface IncomingHttpHeaders extends Http1IncomingHttpHeaders {
':path'?: string | undefined;
':method'?: string | undefined;
':authority'?: string | undefined;
':scheme'?: string | undefined;
}
// Http2Stream
export interface StreamPriorityOptions {
exclusive?: boolean | undefined;
parent?: number | undefined;
weight?: number | undefined;
silent?: boolean | undefined;
}
export interface StreamState {
localWindowSize?: number | undefined;
state?: number | undefined;
localClose?: number | undefined;
remoteClose?: number | undefined;
sumDependencyWeight?: number | undefined;
weight?: number | undefined;
}
export interface ServerStreamResponseOptions {
endStream?: boolean | undefined;
waitForTrailers?: boolean | undefined;
}
export interface StatOptions {
offset: number;
length: number;
}
export interface ServerStreamFileResponseOptions {
statCheck?(stats: fs.Stats, headers: OutgoingHttpHeaders, statOptions: StatOptions): void | boolean;
waitForTrailers?: boolean | undefined;
offset?: number | undefined;
length?: number | undefined;
}
export interface ServerStreamFileResponseOptionsWithError extends ServerStreamFileResponseOptions {
onError?(err: NodeJS.ErrnoException): void;
}
export interface Http2Stream extends stream.Duplex {
/**
* Set to `true` if the `Http2Stream` instance was aborted abnormally. When set,
* the `'aborted'` event will have been emitted.
* @since v8.4.0
*/
readonly aborted: boolean;
/**
* This property shows the number of characters currently buffered to be written.
* See `net.Socket.bufferSize` for details.
* @since v11.2.0, v10.16.0
*/
readonly bufferSize: number;
/**
* Set to `true` if the `Http2Stream` instance has been closed.
* @since v9.4.0
*/
readonly closed: boolean;
/**
* Set to `true` if the `Http2Stream` instance has been destroyed and is no longer
* usable.
* @since v8.4.0
*/
readonly destroyed: boolean;
/**
* Set the `true` if the `END_STREAM` flag was set in the request or response
* HEADERS frame received, indicating that no additional data should be received
* and the readable side of the `Http2Stream` will be closed.
* @since v10.11.0
*/
readonly endAfterHeaders: boolean;
/**
* The numeric stream identifier of this `Http2Stream` instance. Set to `undefined`if the stream identifier has not yet been assigned.
* @since v8.4.0
*/
readonly id?: number | undefined;
/**
* Set to `true` if the `Http2Stream` instance has not yet been assigned a
* numeric stream identifier.
* @since v9.4.0
*/
readonly pending: boolean;
/**
* Set to the `RST_STREAM` `error code` reported when the `Http2Stream` is
* destroyed after either receiving an `RST_STREAM` frame from the connected peer,
* calling `http2stream.close()`, or `http2stream.destroy()`. Will be`undefined` if the `Http2Stream` has not been closed.
* @since v8.4.0
*/
readonly rstCode: number;
/**
* An object containing the outbound headers sent for this `Http2Stream`.
* @since v9.5.0
*/
readonly sentHeaders: OutgoingHttpHeaders;
/**
* An array of objects containing the outbound informational (additional) headers
* sent for this `Http2Stream`.
* @since v9.5.0
*/
readonly sentInfoHeaders?: OutgoingHttpHeaders[] | undefined;
/**
* An object containing the outbound trailers sent for this `HttpStream`.
* @since v9.5.0
*/
readonly sentTrailers?: OutgoingHttpHeaders | undefined;
/**
* A reference to the `Http2Session` instance that owns this `Http2Stream`. The
* value will be `undefined` after the `Http2Stream` instance is destroyed.
* @since v8.4.0
*/
readonly session: Http2Session;
/**
* Provides miscellaneous information about the current state of the`Http2Stream`.
*
* A current state of this `Http2Stream`.
* @since v8.4.0
*/
readonly state: StreamState;
/**
* Closes the `Http2Stream` instance by sending an `RST_STREAM` frame to the
* connected HTTP/2 peer.
* @since v8.4.0
* @param [code=http2.constants.NGHTTP2_NO_ERROR] Unsigned 32-bit integer identifying the error code.
* @param callback An optional function registered to listen for the `'close'` event.
*/
close(code?: number, callback?: () => void): void;
/**
* Updates the priority for this `Http2Stream` instance.
* @since v8.4.0
*/
priority(options: StreamPriorityOptions): void;
/**
* ```js
* const http2 = require('http2');
* const client = http2.connect('http://example.org:8000');
* const { NGHTTP2_CANCEL } = http2.constants;
* const req = client.request({ ':path': '/' });
*
* // Cancel the stream if there's no activity after 5 seconds
* req.setTimeout(5000, () => req.close(NGHTTP2_CANCEL));
* ```
* @since v8.4.0
*/
setTimeout(msecs: number, callback?: () => void): void;
/**
* Sends a trailing `HEADERS` frame to the connected HTTP/2 peer. This method
* will cause the `Http2Stream` to be immediately closed and must only be
* called after the `'wantTrailers'` event has been emitted. When sending a
* request or sending a response, the `options.waitForTrailers` option must be set
* in order to keep the `Http2Stream` open after the final `DATA` frame so that
* trailers can be sent.
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* stream.respond(undefined, { waitForTrailers: true });
* stream.on('wantTrailers', () => {
* stream.sendTrailers({ xyz: 'abc' });
* });
* stream.end('Hello World');
* });
* ```
*
* The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
* fields (e.g. `':method'`, `':path'`, etc).
* @since v10.0.0
*/
sendTrailers(headers: OutgoingHttpHeaders): void;
addListener(event: 'aborted', listener: () => void): this;
addListener(event: 'close', listener: () => void): this;
addListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
addListener(event: 'drain', listener: () => void): this;
addListener(event: 'end', listener: () => void): this;
addListener(event: 'error', listener: (err: Error) => void): this;
addListener(event: 'finish', listener: () => void): this;
addListener(event: 'frameError', listener: (frameType: number, errorCode: number) => void): this;
addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
addListener(event: 'streamClosed', listener: (code: number) => void): this;
addListener(event: 'timeout', listener: () => void): this;
addListener(event: 'trailers', listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
addListener(event: 'wantTrailers', listener: () => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'aborted'): boolean;
emit(event: 'close'): boolean;
emit(event: 'data', chunk: Buffer | string): boolean;
emit(event: 'drain'): boolean;
emit(event: 'end'): boolean;
emit(event: 'error', err: Error): boolean;
emit(event: 'finish'): boolean;
emit(event: 'frameError', frameType: number, errorCode: number): boolean;
emit(event: 'pipe', src: stream.Readable): boolean;
emit(event: 'unpipe', src: stream.Readable): boolean;
emit(event: 'streamClosed', code: number): boolean;
emit(event: 'timeout'): boolean;
emit(event: 'trailers', trailers: IncomingHttpHeaders, flags: number): boolean;
emit(event: 'wantTrailers'): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: 'aborted', listener: () => void): this;
on(event: 'close', listener: () => void): this;
on(event: 'data', listener: (chunk: Buffer | string) => void): this;
on(event: 'drain', listener: () => void): this;
on(event: 'end', listener: () => void): this;
on(event: 'error', listener: (err: Error) => void): this;
on(event: 'finish', listener: () => void): this;
on(event: 'frameError', listener: (frameType: number, errorCode: number) => void): this;
on(event: 'pipe', listener: (src: stream.Readable) => void): this;
on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
on(event: 'streamClosed', listener: (code: number) => void): this;
on(event: 'timeout', listener: () => void): this;
on(event: 'trailers', listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
on(event: 'wantTrailers', listener: () => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'aborted', listener: () => void): this;
once(event: 'close', listener: () => void): this;
once(event: 'data', listener: (chunk: Buffer | string) => void): this;
once(event: 'drain', listener: () => void): this;
once(event: 'end', listener: () => void): this;
once(event: 'error', listener: (err: Error) => void): this;
once(event: 'finish', listener: () => void): this;
once(event: 'frameError', listener: (frameType: number, errorCode: number) => void): this;
once(event: 'pipe', listener: (src: stream.Readable) => void): this;
once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
once(event: 'streamClosed', listener: (code: number) => void): this;
once(event: 'timeout', listener: () => void): this;
once(event: 'trailers', listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
once(event: 'wantTrailers', listener: () => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: 'aborted', listener: () => void): this;
prependListener(event: 'close', listener: () => void): this;
prependListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
prependListener(event: 'drain', listener: () => void): this;
prependListener(event: 'end', listener: () => void): this;
prependListener(event: 'error', listener: (err: Error) => void): this;
prependListener(event: 'finish', listener: () => void): this;
prependListener(event: 'frameError', listener: (frameType: number, errorCode: number) => void): this;
prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
prependListener(event: 'streamClosed', listener: (code: number) => void): this;
prependListener(event: 'timeout', listener: () => void): this;
prependListener(event: 'trailers', listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
prependListener(event: 'wantTrailers', listener: () => void): this;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'aborted', listener: () => void): this;
prependOnceListener(event: 'close', listener: () => void): this;
prependOnceListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
prependOnceListener(event: 'drain', listener: () => void): this;
prependOnceListener(event: 'end', listener: () => void): this;
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
prependOnceListener(event: 'finish', listener: () => void): this;
prependOnceListener(event: 'frameError', listener: (frameType: number, errorCode: number) => void): this;
prependOnceListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
prependOnceListener(event: 'streamClosed', listener: (code: number) => void): this;
prependOnceListener(event: 'timeout', listener: () => void): this;
prependOnceListener(event: 'trailers', listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
prependOnceListener(event: 'wantTrailers', listener: () => void): this;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
}
export interface ClientHttp2Stream extends Http2Stream {
addListener(event: 'continue', listener: () => {}): this;
addListener(event: 'headers', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
addListener(event: 'push', listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
addListener(event: 'response', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'continue'): boolean;
emit(event: 'headers', headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number): boolean;
emit(event: 'push', headers: IncomingHttpHeaders, flags: number): boolean;
emit(event: 'response', headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: 'continue', listener: () => {}): this;
on(event: 'headers', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
on(event: 'push', listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
on(event: 'response', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'continue', listener: () => {}): this;
once(event: 'headers', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
once(event: 'push', listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
once(event: 'response', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: 'continue', listener: () => {}): this;
prependListener(event: 'headers', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependListener(event: 'push', listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
prependListener(event: 'response', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'continue', listener: () => {}): this;
prependOnceListener(event: 'headers', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependOnceListener(event: 'push', listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
prependOnceListener(event: 'response', listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
}
export interface ServerHttp2Stream extends Http2Stream {
/**
* True if headers were sent, false otherwise (read-only).
* @since v8.4.0
*/
readonly headersSent: boolean;
/**
* Read-only property mapped to the `SETTINGS_ENABLE_PUSH` flag of the remote
* client's most recent `SETTINGS` frame. Will be `true` if the remote peer
* accepts push streams, `false` otherwise. Settings are the same for every`Http2Stream` in the same `Http2Session`.
* @since v8.4.0
*/
readonly pushAllowed: boolean;
/**
* Sends an additional informational `HEADERS` frame to the connected HTTP/2 peer.
* @since v8.4.0
*/
additionalHeaders(headers: OutgoingHttpHeaders): void;
/**
* Initiates a push stream. The callback is invoked with the new `Http2Stream`instance created for the push stream passed as the second argument, or an`Error` passed as the first argument.
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* stream.respond({ ':status': 200 });
* stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => {
* if (err) throw err;
* pushStream.respond({ ':status': 200 });
* pushStream.end('some pushed data');
* });
* stream.end('some data');
* });
* ```
*
* Setting the weight of a push stream is not allowed in the `HEADERS` frame. Pass
* a `weight` value to `http2stream.priority` with the `silent` option set to`true` to enable server-side bandwidth balancing between concurrent streams.
*
* Calling `http2stream.pushStream()` from within a pushed stream is not permitted
* and will throw an error.
* @since v8.4.0
* @param callback Callback that is called once the push stream has been initiated.
*/
pushStream(headers: OutgoingHttpHeaders, callback?: (err: Error | null, pushStream: ServerHttp2Stream, headers: OutgoingHttpHeaders) => void): void;
pushStream(headers: OutgoingHttpHeaders, options?: StreamPriorityOptions, callback?: (err: Error | null, pushStream: ServerHttp2Stream, headers: OutgoingHttpHeaders) => void): void;
/**
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* stream.respond({ ':status': 200 });
* stream.end('some data');
* });
* ```
*
* When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
* will be emitted immediately after queuing the last chunk of payload data to be
* sent. The `http2stream.sendTrailers()` method can then be used to sent trailing
* header fields to the peer.
*
* When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
* close when the final `DATA` frame is transmitted. User code must call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* stream.respond({ ':status': 200 }, { waitForTrailers: true });
* stream.on('wantTrailers', () => {
* stream.sendTrailers({ ABC: 'some value to send' });
* });
* stream.end('some data');
* });
* ```
* @since v8.4.0
*/
respond(headers?: OutgoingHttpHeaders, options?: ServerStreamResponseOptions): void;
/**
* Initiates a response whose data is read from the given file descriptor. No
* validation is performed on the given file descriptor. If an error occurs while
* attempting to read data using the file descriptor, the `Http2Stream` will be
* closed using an `RST_STREAM` frame using the standard `INTERNAL_ERROR` code.
*
* When used, the `Http2Stream` object's `Duplex` interface will be closed
* automatically.
*
* ```js
* const http2 = require('http2');
* const fs = require('fs');
*
* const server = http2.createServer();
* server.on('stream', (stream) => {
* const fd = fs.openSync('/some/file', 'r');
*
* const stat = fs.fstatSync(fd);
* const headers = {
* 'content-length': stat.size,
* 'last-modified': stat.mtime.toUTCString(),
* 'content-type': 'text/plain; charset=utf-8'
* };
* stream.respondWithFD(fd, headers);
* stream.on('close', () => fs.closeSync(fd));
* });
* ```
*
* The optional `options.statCheck` function may be specified to give user code
* an opportunity to set additional content headers based on the `fs.Stat` details
* of the given fd. If the `statCheck` function is provided, the`http2stream.respondWithFD()` method will perform an `fs.fstat()` call to
* collect details on the provided file descriptor.
*
* The `offset` and `length` options may be used to limit the response to a
* specific range subset. This can be used, for instance, to support HTTP Range
* requests.
*
* The file descriptor or `FileHandle` is not closed when the stream is closed,
* so it will need to be closed manually once it is no longer needed.
* Using the same file descriptor concurrently for multiple streams
* is not supported and may result in data loss. Re-using a file descriptor
* after a stream has finished is supported.
*
* When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
* will be emitted immediately after queuing the last chunk of payload data to be
* sent. The `http2stream.sendTrailers()` method can then be used to sent trailing
* header fields to the peer.
*
* When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
* close when the final `DATA` frame is transmitted. User code _must_ call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
*
* ```js
* const http2 = require('http2');
* const fs = require('fs');
*
* const server = http2.createServer();
* server.on('stream', (stream) => {
* const fd = fs.openSync('/some/file', 'r');
*
* const stat = fs.fstatSync(fd);
* const headers = {
* 'content-length': stat.size,
* 'last-modified': stat.mtime.toUTCString(),
* 'content-type': 'text/plain; charset=utf-8'
* };
* stream.respondWithFD(fd, headers, { waitForTrailers: true });
* stream.on('wantTrailers', () => {
* stream.sendTrailers({ ABC: 'some value to send' });
* });
*
* stream.on('close', () => fs.closeSync(fd));
* });
* ```
* @since v8.4.0
* @param fd A readable file descriptor.
*/
respondWithFD(fd: number | fs.promises.FileHandle, headers?: OutgoingHttpHeaders, options?: ServerStreamFileResponseOptions): void;
/**
* Sends a regular file as the response. The `path` must specify a regular file
* or an `'error'` event will be emitted on the `Http2Stream` object.
*
* When used, the `Http2Stream` object's `Duplex` interface will be closed
* automatically.
*
* The optional `options.statCheck` function may be specified to give user code
* an opportunity to set additional content headers based on the `fs.Stat` details
* of the given file:
*
* If an error occurs while attempting to read the file data, the `Http2Stream`will be closed using an `RST_STREAM` frame using the standard `INTERNAL_ERROR`code. If the `onError` callback is
* defined, then it will be called. Otherwise
* the stream will be destroyed.
*
* Example using a file path:
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* function statCheck(stat, headers) {
* headers['last-modified'] = stat.mtime.toUTCString();
* }
*
* function onError(err) {
* // stream.respond() can throw if the stream has been destroyed by
* // the other side.
* try {
* if (err.code === 'ENOENT') {
* stream.respond({ ':status': 404 });
* } else {
* stream.respond({ ':status': 500 });
* }
* } catch (err) {
* // Perform actual error handling.
* console.log(err);
* }
* stream.end();
* }
*
* stream.respondWithFile('/some/file',
* { 'content-type': 'text/plain; charset=utf-8' },
* { statCheck, onError });
* });
* ```
*
* The `options.statCheck` function may also be used to cancel the send operation
* by returning `false`. For instance, a conditional request may check the stat
* results to determine if the file has been modified to return an appropriate`304` response:
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* function statCheck(stat, headers) {
* // Check the stat here...
* stream.respond({ ':status': 304 });
* return false; // Cancel the send operation
* }
* stream.respondWithFile('/some/file',
* { 'content-type': 'text/plain; charset=utf-8' },
* { statCheck });
* });
* ```
*
* The `content-length` header field will be automatically set.
*
* The `offset` and `length` options may be used to limit the response to a
* specific range subset. This can be used, for instance, to support HTTP Range
* requests.
*
* The `options.onError` function may also be used to handle all the errors
* that could happen before the delivery of the file is initiated. The
* default behavior is to destroy the stream.
*
* When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
* will be emitted immediately after queuing the last chunk of payload data to be
* sent. The `http2stream.sendTrailers()` method can then be used to sent trailing
* header fields to the peer.
*
* When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
* close when the final `DATA` frame is transmitted. User code must call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
*
* ```js
* const http2 = require('http2');
* const server = http2.createServer();
* server.on('stream', (stream) => {
* stream.respondWithFile('/some/file',
* { 'content-type': 'text/plain; charset=utf-8' },
* { waitForTrailers: true });
* stream.on('wantTrailers', () => {
* stream.sendTrailers({ ABC: 'some value to send' });
* });
* });
* ```
* @since v8.4.0
*/
respondWithFile(path: string, headers?: OutgoingHttpHeaders, options?: ServerStreamFileResponseOptionsWithError): void;
}
// Http2Session
export interface Settings {
headerTableSize?: number | undefined;
enablePush?: boolean | undefined;
initialWindowSize?: number | undefined;
maxFrameSize?: number | undefined;
maxConcurrentStreams?: number | undefined;
maxHeaderListSize?: number | undefined;
enableConnectProtocol?: boolean | undefined;
}
export interface ClientSessionRequestOptions {
endStream?: boolean | undefined;
exclusive?: boolean | undefined;
parent?: number | undefined;
weight?: number | undefined;
waitForTrailers?: boolean | undefined;
}
export interface SessionState {
effectiveLocalWindowSize?: number | undefined;
effectiveRecvDataLength?: number | undefined;
nextStreamID?: number | undefined;
localWindowSize?: number | undefined;
lastProcStreamID?: number | undefined;
remoteWindowSize?: number | undefined;
outboundQueueSize?: number | undefined;
deflateDynamicTableSize?: number | undefined;
inflateDynamicTableSize?: number | undefined;
}
export interface Http2Session extends EventEmitter {
/**
* Value will be `undefined` if the `Http2Session` is not yet connected to a
* socket, `h2c` if the `Http2Session` is not connected to a `TLSSocket`, or
* will return the value of the connected `TLSSocket`'s own `alpnProtocol`property.
* @since v9.4.0
*/
readonly alpnProtocol?: string | undefined;
/**
* Will be `true` if this `Http2Session` instance has been closed, otherwise`false`.
* @since v9.4.0
*/
readonly closed: boolean;
/**
* Will be `true` if this `Http2Session` instance is still connecting, will be set
* to `false` before emitting `connect` event and/or calling the `http2.connect`callback.
* @since v10.0.0
*/
readonly connecting: boolean;
/**
* Will be `true` if this `Http2Session` instance has been destroyed and must no
* longer be used, otherwise `false`.
* @since v8.4.0
*/
readonly destroyed: boolean;
/**
* Value is `undefined` if the `Http2Session` session socket has not yet been
* connected, `true` if the `Http2Session` is connected with a `TLSSocket`,
* and `false` if the `Http2Session` is connected to any other kind of socket
* or stream.
* @since v9.4.0
*/
readonly encrypted?: boolean | undefined;
/**
* A prototype-less object describing the current local settings of this`Http2Session`. The local settings are local to _this_`Http2Session` instance.
* @since v8.4.0
*/
readonly localSettings: Settings;
/**
* If the `Http2Session` is connected to a `TLSSocket`, the `originSet` property
* will return an `Array` of origins for which the `Http2Session` may be
* considered authoritative.
*
* The `originSet` property is only available when using a secure TLS connection.
* @since v9.4.0
*/
readonly originSet?: string[] | undefined;
/**
* Indicates whether the `Http2Session` is currently waiting for acknowledgment of
* a sent `SETTINGS` frame. Will be `true` after calling the`http2session.settings()` method. Will be `false` once all sent `SETTINGS`frames have been acknowledged.
* @since v8.4.0
*/
readonly pendingSettingsAck: boolean;
/**
* A prototype-less object describing the current remote settings of this`Http2Session`. The remote settings are set by the _connected_ HTTP/2 peer.
* @since v8.4.0
*/
readonly remoteSettings: Settings;
/**
* Returns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but
* limits available methods to ones safe to use with HTTP/2.
*
* `destroy`, `emit`, `end`, `pause`, `read`, `resume`, and `write` will throw
* an error with code `ERR_HTTP2_NO_SOCKET_MANIPULATION`. See `Http2Session and Sockets` for more information.
*
* `setTimeout` method will be called on this `Http2Session`.
*
* All other interactions will be routed directly to the socket.
* @since v8.4.0
*/
readonly socket: net.Socket | tls.TLSSocket;
/**
* Provides miscellaneous information about the current state of the`Http2Session`.
*
* An object describing the current status of this `Http2Session`.
* @since v8.4.0
*/
readonly state: SessionState;
/**
* The `http2session.type` will be equal to`http2.constants.NGHTTP2_SESSION_SERVER` if this `Http2Session` instance is a
* server, and `http2.constants.NGHTTP2_SESSION_CLIENT` if the instance is a
* client.
* @since v8.4.0
*/
readonly type: number;
/**
* Gracefully closes the `Http2Session`, allowing any existing streams to
* complete on their own and preventing new `Http2Stream` instances from being
* created. Once closed, `http2session.destroy()`_might_ be called if there
* are no open `Http2Stream` instances.
*
* If specified, the `callback` function is registered as a handler for the`'close'` event.
* @since v9.4.0
*/
close(callback?: () => void): void;
/**
* Immediately terminates the `Http2Session` and the associated `net.Socket` or`tls.TLSSocket`.
*
* Once destroyed, the `Http2Session` will emit the `'close'` event. If `error`is not undefined, an `'error'` event will be emitted immediately before the`'close'` event.
*
* If there are any remaining open `Http2Streams` associated with the`Http2Session`, those will also be destroyed.
* @since v8.4.0
* @param error An `Error` object if the `Http2Session` is being destroyed due to an error.
* @param code The HTTP/2 error code to send in the final `GOAWAY` frame. If unspecified, and `error` is not undefined, the default is `INTERNAL_ERROR`, otherwise defaults to `NO_ERROR`.
*/
destroy(error?: Error, code?: number): void;
/**
* Transmits a `GOAWAY` frame to the connected peer _without_ shutting down the`Http2Session`.
* @since v9.4.0
* @param code An HTTP/2 error code
* @param lastStreamID The numeric ID of the last processed `Http2Stream`
* @param opaqueData A `TypedArray` or `DataView` instance containing additional data to be carried within the `GOAWAY` frame.
*/
goaway(code?: number, lastStreamID?: number, opaqueData?: NodeJS.ArrayBufferView): void;
/**
* Sends a `PING` frame to the connected HTTP/2 peer. A `callback` function must
* be provided. The method will return `true` if the `PING` was sent, `false`otherwise.
*
* The maximum number of outstanding (unacknowledged) pings is determined by the`maxOutstandingPings` configuration option. The default maximum is 10.
*
* If provided, the `payload` must be a `Buffer`, `TypedArray`, or `DataView`containing 8 bytes of data that will be transmitted with the `PING` and
* returned with the ping acknowledgment.
*
* The callback will be invoked with three arguments: an error argument that will
* be `null` if the `PING` was successfully acknowledged, a `duration` argument
* that reports the number of milliseconds elapsed since the ping was sent and the
* acknowledgment was received, and a `Buffer` containing the 8-byte `PING`payload.
*
* ```js
* session.ping(Buffer.from('abcdefgh'), (err, duration, payload) => {
* if (!err) {
* console.log(`Ping acknowledged in ${duration} milliseconds`);
* console.log(`With payload '${payload.toString()}'`);
* }
* });
* ```
*
* If the `payload` argument is not specified, the default payload will be the
* 64-bit timestamp (little endian) marking the start of the `PING` duration.
* @since v8.9.3
* @param payload Optional ping payload.
*/
ping(callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean;
ping(payload: NodeJS.ArrayBufferView, callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean;
/**
* Calls `ref()` on this `Http2Session`instance's underlying `net.Socket`.
* @since v9.4.0
*/
ref(): void;
/**
* Sets the local endpoint's window size.
* The `windowSize` is the total window size to set, not
* the delta.
*
* ```js
* const http2 = require('http2');
*
* const server = http2.createServer();
* const expectedWindowSize = 2 ** 20;
* server.on('connect', (session) => {
*
* // Set local window size to be 2 ** 20
* session.setLocalWindowSize(expectedWindowSize);
* });
* ```
* @since v15.3.0
*/
setLocalWindowSize(windowSize: number): void;
/**
* Used to set a callback function that is called when there is no activity on
* the `Http2Session` after `msecs` milliseconds. The given `callback` is
* registered as a listener on the `'timeout'` event.
* @since v8.4.0
*/
setTimeout(msecs: number, callback?: () => void): void;
/**
* Updates the current local settings for this `Http2Session` and sends a new`SETTINGS` frame to the connected HTTP/2 peer.
*
* Once called, the `http2session.pendingSettingsAck` property will be `true`while the session is waiting for the remote peer to acknowledge the new
* settings.
*
* The new settings will not become effective until the `SETTINGS` acknowledgment
* is received and the `'localSettings'` event is emitted. It is possible to send
* multiple `SETTINGS` frames while acknowledgment is still pending.
* @since v8.4.0
* @param callback Callback that is called once the session is connected or right away if the session is already connected.
*/
settings(settings: Settings): void;
/**
* Calls `unref()` on this `Http2Session`instance's underlying `net.Socket`.
* @since v9.4.0
*/
unref(): void;
addListener(event: 'close', listener: () => void): this;
addListener(event: 'error', listener: (err: Error) => void): this;
addListener(event: 'frameError', listener: (frameType: number, errorCode: number, streamID: number) => void): this;
addListener(event: 'goaway', listener: (errorCode: number, lastStreamID: number, opaqueData: Buffer) => void): this;
addListener(event: 'localSettings', listener: (settings: Settings) => void): this;
addListener(event: 'ping', listener: () => void): this;
addListener(event: 'remoteSettings', listener: (settings: Settings) => void): this;
addListener(event: 'timeout', listener: () => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'close'): boolean;
emit(event: 'error', err: Error): boolean;
emit(event: 'frameError', frameType: number, errorCode: number, streamID: number): boolean;
emit(event: 'goaway', errorCode: number, lastStreamID: number, opaqueData: Buffer): boolean;
emit(event: 'localSettings', settings: Settings): boolean;
emit(event: 'ping'): boolean;
emit(event: 'remoteSettings', settings: Settings): boolean;
emit(event: 'timeout'): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: 'close', listener: () => void): this;
on(event: 'error', listener: (err: Error) => void): this;
on(event: 'frameError', listener: (frameType: number, errorCode: number, streamID: number) => void): this;
on(event: 'goaway', listener: (errorCode: number, lastStreamID: number, opaqueData: Buffer) => void): this;
on(event: 'localSettings', listener: (settings: Settings) => void): this;
on(event: 'ping', listener: () => void): this;
on(event: 'remoteSettings', listener: (settings: Settings) => void): this;
on(event: 'timeout', listener: () => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'close', listener: () => void): this;
once(event: 'error', listener: (err: Error) => void): this;
once(event: 'frameError', listener: (frameType: number, errorCode: number, streamID: number) => void): this;
once(event: 'goaway', listener: (errorCode: number, lastStreamID: number, opaqueData: Buffer) => void): this;
once(event: 'localSettings', listener: (settings: Settings) => void): this;
once(event: 'ping', listener: () => void): this;
once(event: 'remoteSettings', listener: (settings: Settings) => void): this;
once(event: 'timeout', listener: () => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: 'close', listener: () => void): this;
prependListener(event: 'error', listener: (err: Error) => void): this;
prependListener(event: 'frameError', listener: (frameType: number, errorCode: number, streamID: number) => void): this;
prependListener(event: 'goaway', listener: (errorCode: number, lastStreamID: number, opaqueData: Buffer) => void): this;
prependListener(event: 'localSettings', listener: (settings: Settings) => void): this;
prependListener(event: 'ping', listener: () => void): this;
prependListener(event: 'remoteSettings', listener: (settings: Settings) => void): this;
prependListener(event: 'timeout', listener: () => void): this;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'close', listener: () => void): this;
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
prependOnceListener(event: 'frameError', listener: (frameType: number, errorCode: number, streamID: number) => void): this;
prependOnceListener(event: 'goaway', listener: (errorCode: number, lastStreamID: number, opaqueData: Buffer) => void): this;
prependOnceListener(event: 'localSettings', listener: (settings: Settings) => void): this;
prependOnceListener(event: 'ping', listener: () => void): this;
prependOnceListener(event: 'remoteSettings', listener: (settings: Settings) => void): this;
prependOnceListener(event: 'timeout', listener: () => void): this;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
}
export interface ClientHttp2Session extends Http2Session {
/**
* For HTTP/2 Client `Http2Session` instances only, the `http2session.request()`creates and returns an `Http2Stream` instance that can be used to send an
* HTTP/2 request to the connected server.
*
* This method is only available if `http2session.type` is equal to`http2.constants.NGHTTP2_SESSION_CLIENT`.
*
* ```js
* const http2 = require('http2');
* const clientSession = http2.connect('https://localhost:1234');
* const {
* HTTP2_HEADER_PATH,
* HTTP2_HEADER_STATUS
* } = http2.constants;
*
* const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
* req.on('response', (headers) => {
* console.log(headers[HTTP2_HEADER_STATUS]);
* req.on('data', (chunk) => { // .. });
* req.on('end', () => { // .. });
* });
* ```
*
* When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
* is emitted immediately after queuing the last chunk of payload data to be sent.
* The `http2stream.sendTrailers()` method can then be called to send trailing
* headers to the peer.
*
* When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
* close when the final `DATA` frame is transmitted. User code must call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
*
* When `options.signal` is set with an `AbortSignal` and then `abort` on the
* corresponding `AbortController` is called, the request will emit an `'error'`event with an `AbortError` error.
*
* The `:method` and `:path` pseudo-headers are not specified within `headers`,
* they respectively default to:
*
* * `:method` \= `'GET'`
* * `:path` \= `/`
* @since v8.4.0
*/
request(headers?: OutgoingHttpHeaders, options?: ClientSessionRequestOptions): ClientHttp2Stream;
addListener(event: 'altsvc', listener: (alt: string, origin: string, stream: number) => void): this;
addListener(event: 'origin', listener: (origins: string[]) => void): this;
addListener(event: 'connect', listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
addListener(event: 'stream', listener: (stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'altsvc', alt: string, origin: string, stream: number): boolean;
emit(event: 'origin', origins: ReadonlyArray<string>): boolean;
emit(event: 'connect', session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket): boolean;
emit(event: 'stream', stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: 'altsvc', listener: (alt: string, origin: string, stream: number) => void): this;
on(event: 'origin', listener: (origins: string[]) => void): this;
on(event: 'connect', listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
on(event: 'stream', listener: (stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'altsvc', listener: (alt: string, origin: string, stream: number) => void): this;
once(event: 'origin', listener: (origins: string[]) => void): this;
once(event: 'connect', listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
once(event: 'stream', listener: (stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: 'altsvc', listener: (alt: string, origin: string, stream: number) => void): this;
prependListener(event: 'origin', listener: (origins: string[]) => void): this;
prependListener(event: 'connect', listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
prependListener(event: 'stream', listener: (stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'altsvc', listener: (alt: string, origin: string, stream: number) => void): this;
prependOnceListener(event: 'origin', listener: (origins: string[]) => void): this;
prependOnceListener(event: 'connect', listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
prependOnceListener(event: 'stream', listener: (stream: ClientHttp2Stream, headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void): this;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
}
export interface AlternativeServiceOptions {
origin: number | string | url.URL;
}
export interface ServerHttp2Session extends Http2Session {
readonly server: Http2Server | Http2SecureServer;
/**
* Submits an `ALTSVC` frame (as defined by [RFC 7838](https://tools.ietf.org/html/rfc7838)) to the connected client.
*
* ```js
* const http2 = require('http2');
*
* const server = http2.createServer();
* server.on('session', (session) => {
* // Set altsvc for origin https://example.org:80
* session.altsvc('h2=":8000"', 'https://example.org:80');
* });
*
* server.on('stream', (stream) => {
* // Set altsvc for a specific stream
* stream.session.altsvc('h2=":8000"', stream.id);
* });
* ```
*
* Sending an `ALTSVC` frame with a specific stream ID indicates that the alternate
* service is associated with the origin of the given `Http2Stream`.
*
* The `alt` and origin string _must_ contain only ASCII bytes and are
* strictly interpreted as a sequence of ASCII bytes. The special value `'clear'`may be passed to clear any previously set alternative service for a given
* domain.
*
* When a string is passed for the `originOrStream` argument, it will be parsed as
* a URL and the origin will be derived. For instance, the origin for the
* HTTP URL `'https://example.org/foo/bar'` is the ASCII string`'https://example.org'`. An error will be thrown if either the given string
* cannot be parsed as a URL or if a valid origin cannot be derived.
*
* A `URL` object, or any object with an `origin` property, may be passed as`originOrStream`, in which case the value of the `origin` property will be
* used. The value of the `origin` property _must_ be a properly serialized
* ASCII origin.
* @since v9.4.0
* @param alt A description of the alternative service configuration as defined by `RFC 7838`.
* @param originOrStream Either a URL string specifying the origin (or an `Object` with an `origin` property) or the numeric identifier of an active `Http2Stream` as given by the
* `http2stream.id` property.
*/
altsvc(alt: string, originOrStream: number | string | url.URL | AlternativeServiceOptions): void;
/**
* Submits an `ORIGIN` frame (as defined by [RFC 8336](https://tools.ietf.org/html/rfc8336)) to the connected client
* to advertise the set of origins for which the server is capable of providing
* authoritative responses.
*
* ```js
* const http2 = require('http2');
* const options = getSecureOptionsSomehow();
* const server = http2.createSecureServer(options);
* server.on('stream', (stream) => {
* stream.respond();
* stream.end('ok');
* });
* server.on('session', (session) => {
* session.origin('https://example.com', 'https://example.org');
* });
* ```
*
* When a string is passed as an `origin`, it will be parsed as a URL and the
* origin will be derived. For instance, the origin for the HTTP URL`'https://example.org/foo/bar'` is the ASCII string`'https://example.org'`. An error will be thrown if either the given
* string
* cannot be parsed as a URL or if a valid origin cannot be derived.
*
* A `URL` object, or any object with an `origin` property, may be passed as
* an `origin`, in which case the value of the `origin` property will be
* used. The value of the `origin` property _must_ be a properly serialized
* ASCII origin.
*
* Alternatively, the `origins` option may be used when creating a new HTTP/2
* server using the `http2.createSecureServer()` method:
*
* ```js
* const http2 = require('http2');
* const options = getSecureOptionsSomehow();
* options.origins = ['https://example.com', 'https://example.org'];
* const server = http2.createSecureServer(options);
* server.on('stream', (stream) => {
* stream.respond();
* stream.end('ok');
* });
* ```