430162088b5dcf1d485436f41f47faf8c912daa85319bd3f31671a904d0f988507b654e1ac0cab7d7c6c8cc0ab08de71ed414405fb872c0a83cc510e56a800 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  1. /**
  2. * The `node:http2` module provides an implementation of the [HTTP/2](https://tools.ietf.org/html/rfc7540) protocol.
  3. * It can be accessed using:
  4. *
  5. * ```js
  6. * import http2 from 'node:http2';
  7. * ```
  8. * @since v8.4.0
  9. * @see [source](https://github.com/nodejs/node/blob/v22.x/lib/http2.js)
  10. */
  11. declare module "http2" {
  12. import EventEmitter = require("node:events");
  13. import * as fs from "node:fs";
  14. import * as net from "node:net";
  15. import * as stream from "node:stream";
  16. import * as tls from "node:tls";
  17. import * as url from "node:url";
  18. import {
  19. IncomingHttpHeaders as Http1IncomingHttpHeaders,
  20. IncomingMessage,
  21. OutgoingHttpHeaders,
  22. ServerResponse,
  23. } from "node:http";
  24. export { OutgoingHttpHeaders } from "node:http";
  25. export interface IncomingHttpStatusHeader {
  26. ":status"?: number | undefined;
  27. }
  28. export interface IncomingHttpHeaders extends Http1IncomingHttpHeaders {
  29. ":path"?: string | undefined;
  30. ":method"?: string | undefined;
  31. ":authority"?: string | undefined;
  32. ":scheme"?: string | undefined;
  33. }
  34. // Http2Stream
  35. export interface StreamPriorityOptions {
  36. exclusive?: boolean | undefined;
  37. parent?: number | undefined;
  38. weight?: number | undefined;
  39. silent?: boolean | undefined;
  40. }
  41. export interface StreamState {
  42. localWindowSize?: number | undefined;
  43. state?: number | undefined;
  44. localClose?: number | undefined;
  45. remoteClose?: number | undefined;
  46. sumDependencyWeight?: number | undefined;
  47. weight?: number | undefined;
  48. }
  49. export interface ServerStreamResponseOptions {
  50. endStream?: boolean | undefined;
  51. waitForTrailers?: boolean | undefined;
  52. }
  53. export interface StatOptions {
  54. offset: number;
  55. length: number;
  56. }
  57. export interface ServerStreamFileResponseOptions {
  58. // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
  59. statCheck?(stats: fs.Stats, headers: OutgoingHttpHeaders, statOptions: StatOptions): void | boolean;
  60. waitForTrailers?: boolean | undefined;
  61. offset?: number | undefined;
  62. length?: number | undefined;
  63. }
  64. export interface ServerStreamFileResponseOptionsWithError extends ServerStreamFileResponseOptions {
  65. onError?(err: NodeJS.ErrnoException): void;
  66. }
  67. export interface Http2Stream extends stream.Duplex {
  68. /**
  69. * Set to `true` if the `Http2Stream` instance was aborted abnormally. When set,
  70. * the `'aborted'` event will have been emitted.
  71. * @since v8.4.0
  72. */
  73. readonly aborted: boolean;
  74. /**
  75. * This property shows the number of characters currently buffered to be written.
  76. * See `net.Socket.bufferSize` for details.
  77. * @since v11.2.0, v10.16.0
  78. */
  79. readonly bufferSize: number;
  80. /**
  81. * Set to `true` if the `Http2Stream` instance has been closed.
  82. * @since v9.4.0
  83. */
  84. readonly closed: boolean;
  85. /**
  86. * Set to `true` if the `Http2Stream` instance has been destroyed and is no longer
  87. * usable.
  88. * @since v8.4.0
  89. */
  90. readonly destroyed: boolean;
  91. /**
  92. * Set to `true` if the `END_STREAM` flag was set in the request or response
  93. * HEADERS frame received, indicating that no additional data should be received
  94. * and the readable side of the `Http2Stream` will be closed.
  95. * @since v10.11.0
  96. */
  97. readonly endAfterHeaders: boolean;
  98. /**
  99. * The numeric stream identifier of this `Http2Stream` instance. Set to `undefined` if the stream identifier has not yet been assigned.
  100. * @since v8.4.0
  101. */
  102. readonly id?: number | undefined;
  103. /**
  104. * Set to `true` if the `Http2Stream` instance has not yet been assigned a
  105. * numeric stream identifier.
  106. * @since v9.4.0
  107. */
  108. readonly pending: boolean;
  109. /**
  110. * Set to the `RST_STREAM` `error code` reported when the `Http2Stream` is
  111. * destroyed after either receiving an `RST_STREAM` frame from the connected peer,
  112. * calling `http2stream.close()`, or `http2stream.destroy()`. Will be `undefined` if the `Http2Stream` has not been closed.
  113. * @since v8.4.0
  114. */
  115. readonly rstCode: number;
  116. /**
  117. * An object containing the outbound headers sent for this `Http2Stream`.
  118. * @since v9.5.0
  119. */
  120. readonly sentHeaders: OutgoingHttpHeaders;
  121. /**
  122. * An array of objects containing the outbound informational (additional) headers
  123. * sent for this `Http2Stream`.
  124. * @since v9.5.0
  125. */
  126. readonly sentInfoHeaders?: OutgoingHttpHeaders[] | undefined;
  127. /**
  128. * An object containing the outbound trailers sent for this `HttpStream`.
  129. * @since v9.5.0
  130. */
  131. readonly sentTrailers?: OutgoingHttpHeaders | undefined;
  132. /**
  133. * A reference to the `Http2Session` instance that owns this `Http2Stream`. The
  134. * value will be `undefined` after the `Http2Stream` instance is destroyed.
  135. * @since v8.4.0
  136. */
  137. readonly session: Http2Session | undefined;
  138. /**
  139. * Provides miscellaneous information about the current state of the `Http2Stream`.
  140. *
  141. * A current state of this `Http2Stream`.
  142. * @since v8.4.0
  143. */
  144. readonly state: StreamState;
  145. /**
  146. * Closes the `Http2Stream` instance by sending an `RST_STREAM` frame to the
  147. * connected HTTP/2 peer.
  148. * @since v8.4.0
  149. * @param [code=http2.constants.NGHTTP2_NO_ERROR] Unsigned 32-bit integer identifying the error code.
  150. * @param callback An optional function registered to listen for the `'close'` event.
  151. */
  152. close(code?: number, callback?: () => void): void;
  153. /**
  154. * Updates the priority for this `Http2Stream` instance.
  155. * @since v8.4.0
  156. */
  157. priority(options: StreamPriorityOptions): void;
  158. /**
  159. * ```js
  160. * import http2 from 'node:http2';
  161. * const client = http2.connect('http://example.org:8000');
  162. * const { NGHTTP2_CANCEL } = http2.constants;
  163. * const req = client.request({ ':path': '/' });
  164. *
  165. * // Cancel the stream if there's no activity after 5 seconds
  166. * req.setTimeout(5000, () => req.close(NGHTTP2_CANCEL));
  167. * ```
  168. * @since v8.4.0
  169. */
  170. setTimeout(msecs: number, callback?: () => void): void;
  171. /**
  172. * Sends a trailing `HEADERS` frame to the connected HTTP/2 peer. This method
  173. * will cause the `Http2Stream` to be immediately closed and must only be
  174. * called after the `'wantTrailers'` event has been emitted. When sending a
  175. * request or sending a response, the `options.waitForTrailers` option must be set
  176. * in order to keep the `Http2Stream` open after the final `DATA` frame so that
  177. * trailers can be sent.
  178. *
  179. * ```js
  180. * import http2 from 'node:http2';
  181. * const server = http2.createServer();
  182. * server.on('stream', (stream) => {
  183. * stream.respond(undefined, { waitForTrailers: true });
  184. * stream.on('wantTrailers', () => {
  185. * stream.sendTrailers({ xyz: 'abc' });
  186. * });
  187. * stream.end('Hello World');
  188. * });
  189. * ```
  190. *
  191. * The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
  192. * fields (e.g. `':method'`, `':path'`, etc).
  193. * @since v10.0.0
  194. */
  195. sendTrailers(headers: OutgoingHttpHeaders): void;
  196. addListener(event: "aborted", listener: () => void): this;
  197. addListener(event: "close", listener: () => void): this;
  198. addListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  199. addListener(event: "drain", listener: () => void): this;
  200. addListener(event: "end", listener: () => void): this;
  201. addListener(event: "error", listener: (err: Error) => void): this;
  202. addListener(event: "finish", listener: () => void): this;
  203. addListener(event: "frameError", listener: (frameType: number, errorCode: number) => void): this;
  204. addListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  205. addListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  206. addListener(event: "streamClosed", listener: (code: number) => void): this;
  207. addListener(event: "timeout", listener: () => void): this;
  208. addListener(event: "trailers", listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
  209. addListener(event: "wantTrailers", listener: () => void): this;
  210. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  211. emit(event: "aborted"): boolean;
  212. emit(event: "close"): boolean;
  213. emit(event: "data", chunk: Buffer | string): boolean;
  214. emit(event: "drain"): boolean;
  215. emit(event: "end"): boolean;
  216. emit(event: "error", err: Error): boolean;
  217. emit(event: "finish"): boolean;
  218. emit(event: "frameError", frameType: number, errorCode: number): boolean;
  219. emit(event: "pipe", src: stream.Readable): boolean;
  220. emit(event: "unpipe", src: stream.Readable): boolean;
  221. emit(event: "streamClosed", code: number): boolean;
  222. emit(event: "timeout"): boolean;
  223. emit(event: "trailers", trailers: IncomingHttpHeaders, flags: number): boolean;
  224. emit(event: "wantTrailers"): boolean;
  225. emit(event: string | symbol, ...args: any[]): boolean;
  226. on(event: "aborted", listener: () => void): this;
  227. on(event: "close", listener: () => void): this;
  228. on(event: "data", listener: (chunk: Buffer | string) => void): this;
  229. on(event: "drain", listener: () => void): this;
  230. on(event: "end", listener: () => void): this;
  231. on(event: "error", listener: (err: Error) => void): this;
  232. on(event: "finish", listener: () => void): this;
  233. on(event: "frameError", listener: (frameType: number, errorCode: number) => void): this;
  234. on(event: "pipe", listener: (src: stream.Readable) => void): this;
  235. on(event: "unpipe", listener: (src: stream.Readable) => void): this;
  236. on(event: "streamClosed", listener: (code: number) => void): this;
  237. on(event: "timeout", listener: () => void): this;
  238. on(event: "trailers", listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
  239. on(event: "wantTrailers", listener: () => void): this;
  240. on(event: string | symbol, listener: (...args: any[]) => void): this;
  241. once(event: "aborted", listener: () => void): this;
  242. once(event: "close", listener: () => void): this;
  243. once(event: "data", listener: (chunk: Buffer | string) => void): this;
  244. once(event: "drain", listener: () => void): this;
  245. once(event: "end", listener: () => void): this;
  246. once(event: "error", listener: (err: Error) => void): this;
  247. once(event: "finish", listener: () => void): this;
  248. once(event: "frameError", listener: (frameType: number, errorCode: number) => void): this;
  249. once(event: "pipe", listener: (src: stream.Readable) => void): this;
  250. once(event: "unpipe", listener: (src: stream.Readable) => void): this;
  251. once(event: "streamClosed", listener: (code: number) => void): this;
  252. once(event: "timeout", listener: () => void): this;
  253. once(event: "trailers", listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
  254. once(event: "wantTrailers", listener: () => void): this;
  255. once(event: string | symbol, listener: (...args: any[]) => void): this;
  256. prependListener(event: "aborted", listener: () => void): this;
  257. prependListener(event: "close", listener: () => void): this;
  258. prependListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  259. prependListener(event: "drain", listener: () => void): this;
  260. prependListener(event: "end", listener: () => void): this;
  261. prependListener(event: "error", listener: (err: Error) => void): this;
  262. prependListener(event: "finish", listener: () => void): this;
  263. prependListener(event: "frameError", listener: (frameType: number, errorCode: number) => void): this;
  264. prependListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  265. prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  266. prependListener(event: "streamClosed", listener: (code: number) => void): this;
  267. prependListener(event: "timeout", listener: () => void): this;
  268. prependListener(event: "trailers", listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
  269. prependListener(event: "wantTrailers", listener: () => void): this;
  270. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  271. prependOnceListener(event: "aborted", listener: () => void): this;
  272. prependOnceListener(event: "close", listener: () => void): this;
  273. prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  274. prependOnceListener(event: "drain", listener: () => void): this;
  275. prependOnceListener(event: "end", listener: () => void): this;
  276. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  277. prependOnceListener(event: "finish", listener: () => void): this;
  278. prependOnceListener(event: "frameError", listener: (frameType: number, errorCode: number) => void): this;
  279. prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  280. prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  281. prependOnceListener(event: "streamClosed", listener: (code: number) => void): this;
  282. prependOnceListener(event: "timeout", listener: () => void): this;
  283. prependOnceListener(event: "trailers", listener: (trailers: IncomingHttpHeaders, flags: number) => void): this;
  284. prependOnceListener(event: "wantTrailers", listener: () => void): this;
  285. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  286. }
  287. export interface ClientHttp2Stream extends Http2Stream {
  288. addListener(event: "continue", listener: () => {}): this;
  289. addListener(
  290. event: "headers",
  291. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  292. ): this;
  293. addListener(event: "push", listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
  294. addListener(
  295. event: "response",
  296. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  297. ): this;
  298. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  299. emit(event: "continue"): boolean;
  300. emit(event: "headers", headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number): boolean;
  301. emit(event: "push", headers: IncomingHttpHeaders, flags: number): boolean;
  302. emit(event: "response", headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number): boolean;
  303. emit(event: string | symbol, ...args: any[]): boolean;
  304. on(event: "continue", listener: () => {}): this;
  305. on(
  306. event: "headers",
  307. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  308. ): this;
  309. on(event: "push", listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
  310. on(
  311. event: "response",
  312. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  313. ): this;
  314. on(event: string | symbol, listener: (...args: any[]) => void): this;
  315. once(event: "continue", listener: () => {}): this;
  316. once(
  317. event: "headers",
  318. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  319. ): this;
  320. once(event: "push", listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
  321. once(
  322. event: "response",
  323. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  324. ): this;
  325. once(event: string | symbol, listener: (...args: any[]) => void): this;
  326. prependListener(event: "continue", listener: () => {}): this;
  327. prependListener(
  328. event: "headers",
  329. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  330. ): this;
  331. prependListener(event: "push", listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
  332. prependListener(
  333. event: "response",
  334. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  335. ): this;
  336. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  337. prependOnceListener(event: "continue", listener: () => {}): this;
  338. prependOnceListener(
  339. event: "headers",
  340. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  341. ): this;
  342. prependOnceListener(event: "push", listener: (headers: IncomingHttpHeaders, flags: number) => void): this;
  343. prependOnceListener(
  344. event: "response",
  345. listener: (headers: IncomingHttpHeaders & IncomingHttpStatusHeader, flags: number) => void,
  346. ): this;
  347. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  348. }
  349. export interface ServerHttp2Stream extends Http2Stream {
  350. /**
  351. * True if headers were sent, false otherwise (read-only).
  352. * @since v8.4.0
  353. */
  354. readonly headersSent: boolean;
  355. /**
  356. * Read-only property mapped to the `SETTINGS_ENABLE_PUSH` flag of the remote
  357. * client's most recent `SETTINGS` frame. Will be `true` if the remote peer
  358. * accepts push streams, `false` otherwise. Settings are the same for every `Http2Stream` in the same `Http2Session`.
  359. * @since v8.4.0
  360. */
  361. readonly pushAllowed: boolean;
  362. /**
  363. * Sends an additional informational `HEADERS` frame to the connected HTTP/2 peer.
  364. * @since v8.4.0
  365. */
  366. additionalHeaders(headers: OutgoingHttpHeaders): void;
  367. /**
  368. * 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.
  369. *
  370. * ```js
  371. * import http2 from 'node:http2';
  372. * const server = http2.createServer();
  373. * server.on('stream', (stream) => {
  374. * stream.respond({ ':status': 200 });
  375. * stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => {
  376. * if (err) throw err;
  377. * pushStream.respond({ ':status': 200 });
  378. * pushStream.end('some pushed data');
  379. * });
  380. * stream.end('some data');
  381. * });
  382. * ```
  383. *
  384. * Setting the weight of a push stream is not allowed in the `HEADERS` frame. Pass
  385. * a `weight` value to `http2stream.priority` with the `silent` option set to `true` to enable server-side bandwidth balancing between concurrent streams.
  386. *
  387. * Calling `http2stream.pushStream()` from within a pushed stream is not permitted
  388. * and will throw an error.
  389. * @since v8.4.0
  390. * @param callback Callback that is called once the push stream has been initiated.
  391. */
  392. pushStream(
  393. headers: OutgoingHttpHeaders,
  394. callback?: (err: Error | null, pushStream: ServerHttp2Stream, headers: OutgoingHttpHeaders) => void,
  395. ): void;
  396. pushStream(
  397. headers: OutgoingHttpHeaders,
  398. options?: StreamPriorityOptions,
  399. callback?: (err: Error | null, pushStream: ServerHttp2Stream, headers: OutgoingHttpHeaders) => void,
  400. ): void;
  401. /**
  402. * ```js
  403. * import http2 from 'node:http2';
  404. * const server = http2.createServer();
  405. * server.on('stream', (stream) => {
  406. * stream.respond({ ':status': 200 });
  407. * stream.end('some data');
  408. * });
  409. * ```
  410. *
  411. * Initiates a response. When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
  412. * will be emitted immediately after queuing the last chunk of payload data to be sent.
  413. * The `http2stream.sendTrailers()` method can then be used to send trailing header fields to the peer.
  414. *
  415. * When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
  416. * close when the final `DATA` frame is transmitted. User code must call either `http2stream.sendTrailers()` or `http2stream.close()` to close the `Http2Stream`.
  417. *
  418. * ```js
  419. * import http2 from 'node:http2';
  420. * const server = http2.createServer();
  421. * server.on('stream', (stream) => {
  422. * stream.respond({ ':status': 200 }, { waitForTrailers: true });
  423. * stream.on('wantTrailers', () => {
  424. * stream.sendTrailers({ ABC: 'some value to send' });
  425. * });
  426. * stream.end('some data');
  427. * });
  428. * ```
  429. * @since v8.4.0
  430. */
  431. respond(headers?: OutgoingHttpHeaders, options?: ServerStreamResponseOptions): void;
  432. /**
  433. * Initiates a response whose data is read from the given file descriptor. No
  434. * validation is performed on the given file descriptor. If an error occurs while
  435. * attempting to read data using the file descriptor, the `Http2Stream` will be
  436. * closed using an `RST_STREAM` frame using the standard `INTERNAL_ERROR` code.
  437. *
  438. * When used, the `Http2Stream` object's `Duplex` interface will be closed
  439. * automatically.
  440. *
  441. * ```js
  442. * import http2 from 'node:http2';
  443. * import fs from 'node:fs';
  444. *
  445. * const server = http2.createServer();
  446. * server.on('stream', (stream) => {
  447. * const fd = fs.openSync('/some/file', 'r');
  448. *
  449. * const stat = fs.fstatSync(fd);
  450. * const headers = {
  451. * 'content-length': stat.size,
  452. * 'last-modified': stat.mtime.toUTCString(),
  453. * 'content-type': 'text/plain; charset=utf-8',
  454. * };
  455. * stream.respondWithFD(fd, headers);
  456. * stream.on('close', () => fs.closeSync(fd));
  457. * });
  458. * ```
  459. *
  460. * The optional `options.statCheck` function may be specified to give user code
  461. * an opportunity to set additional content headers based on the `fs.Stat` details
  462. * of the given fd. If the `statCheck` function is provided, the `http2stream.respondWithFD()` method will
  463. * perform an `fs.fstat()` call to collect details on the provided file descriptor.
  464. *
  465. * The `offset` and `length` options may be used to limit the response to a
  466. * specific range subset. This can be used, for instance, to support HTTP Range
  467. * requests.
  468. *
  469. * The file descriptor or `FileHandle` is not closed when the stream is closed,
  470. * so it will need to be closed manually once it is no longer needed.
  471. * Using the same file descriptor concurrently for multiple streams
  472. * is not supported and may result in data loss. Re-using a file descriptor
  473. * after a stream has finished is supported.
  474. *
  475. * When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
  476. * will be emitted immediately after queuing the last chunk of payload data to be
  477. * sent. The `http2stream.sendTrailers()` method can then be used to sent trailing
  478. * header fields to the peer.
  479. *
  480. * When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
  481. * close when the final `DATA` frame is transmitted. User code _must_ call either `http2stream.sendTrailers()`
  482. * or `http2stream.close()` to close the `Http2Stream`.
  483. *
  484. * ```js
  485. * import http2 from 'node:http2';
  486. * import fs from 'node:fs';
  487. *
  488. * const server = http2.createServer();
  489. * server.on('stream', (stream) => {
  490. * const fd = fs.openSync('/some/file', 'r');
  491. *
  492. * const stat = fs.fstatSync(fd);
  493. * const headers = {
  494. * 'content-length': stat.size,
  495. * 'last-modified': stat.mtime.toUTCString(),
  496. * 'content-type': 'text/plain; charset=utf-8',
  497. * };
  498. * stream.respondWithFD(fd, headers, { waitForTrailers: true });
  499. * stream.on('wantTrailers', () => {
  500. * stream.sendTrailers({ ABC: 'some value to send' });
  501. * });
  502. *
  503. * stream.on('close', () => fs.closeSync(fd));
  504. * });
  505. * ```
  506. * @since v8.4.0
  507. * @param fd A readable file descriptor.
  508. */
  509. respondWithFD(
  510. fd: number | fs.promises.FileHandle,
  511. headers?: OutgoingHttpHeaders,
  512. options?: ServerStreamFileResponseOptions,
  513. ): void;
  514. /**
  515. * Sends a regular file as the response. The `path` must specify a regular file
  516. * or an `'error'` event will be emitted on the `Http2Stream` object.
  517. *
  518. * When used, the `Http2Stream` object's `Duplex` interface will be closed
  519. * automatically.
  520. *
  521. * The optional `options.statCheck` function may be specified to give user code
  522. * an opportunity to set additional content headers based on the `fs.Stat` details
  523. * of the given file:
  524. *
  525. * If an error occurs while attempting to read the file data, the `Http2Stream` will be closed using an
  526. * `RST_STREAM` frame using the standard `INTERNAL_ERROR` code.
  527. * If the `onError` callback is defined, then it will be called. Otherwise, the stream will be destroyed.
  528. *
  529. * Example using a file path:
  530. *
  531. * ```js
  532. * import http2 from 'node:http2';
  533. * const server = http2.createServer();
  534. * server.on('stream', (stream) => {
  535. * function statCheck(stat, headers) {
  536. * headers['last-modified'] = stat.mtime.toUTCString();
  537. * }
  538. *
  539. * function onError(err) {
  540. * // stream.respond() can throw if the stream has been destroyed by
  541. * // the other side.
  542. * try {
  543. * if (err.code === 'ENOENT') {
  544. * stream.respond({ ':status': 404 });
  545. * } else {
  546. * stream.respond({ ':status': 500 });
  547. * }
  548. * } catch (err) {
  549. * // Perform actual error handling.
  550. * console.error(err);
  551. * }
  552. * stream.end();
  553. * }
  554. *
  555. * stream.respondWithFile('/some/file',
  556. * { 'content-type': 'text/plain; charset=utf-8' },
  557. * { statCheck, onError });
  558. * });
  559. * ```
  560. *
  561. * The `options.statCheck` function may also be used to cancel the send operation
  562. * by returning `false`. For instance, a conditional request may check the stat
  563. * results to determine if the file has been modified to return an appropriate `304` response:
  564. *
  565. * ```js
  566. * import http2 from 'node:http2';
  567. * const server = http2.createServer();
  568. * server.on('stream', (stream) => {
  569. * function statCheck(stat, headers) {
  570. * // Check the stat here...
  571. * stream.respond({ ':status': 304 });
  572. * return false; // Cancel the send operation
  573. * }
  574. * stream.respondWithFile('/some/file',
  575. * { 'content-type': 'text/plain; charset=utf-8' },
  576. * { statCheck });
  577. * });
  578. * ```
  579. *
  580. * The `content-length` header field will be automatically set.
  581. *
  582. * The `offset` and `length` options may be used to limit the response to a
  583. * specific range subset. This can be used, for instance, to support HTTP Range
  584. * requests.
  585. *
  586. * The `options.onError` function may also be used to handle all the errors
  587. * that could happen before the delivery of the file is initiated. The
  588. * default behavior is to destroy the stream.
  589. *
  590. * When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
  591. * will be emitted immediately after queuing the last chunk of payload data to be
  592. * sent. The `http2stream.sendTrailers()` method can then be used to sent trailing
  593. * header fields to the peer.
  594. *
  595. * When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
  596. * close when the final `DATA` frame is transmitted. User code must call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
  597. *
  598. * ```js
  599. * import http2 from 'node:http2';
  600. * const server = http2.createServer();
  601. * server.on('stream', (stream) => {
  602. * stream.respondWithFile('/some/file',
  603. * { 'content-type': 'text/plain; charset=utf-8' },
  604. * { waitForTrailers: true });
  605. * stream.on('wantTrailers', () => {
  606. * stream.sendTrailers({ ABC: 'some value to send' });
  607. * });
  608. * });
  609. * ```
  610. * @since v8.4.0
  611. */
  612. respondWithFile(
  613. path: string,
  614. headers?: OutgoingHttpHeaders,
  615. options?: ServerStreamFileResponseOptionsWithError,
  616. ): void;
  617. }
  618. // Http2Session
  619. export interface Settings {
  620. headerTableSize?: number | undefined;
  621. enablePush?: boolean | undefined;
  622. initialWindowSize?: number | undefined;
  623. maxFrameSize?: number | undefined;
  624. maxConcurrentStreams?: number | undefined;
  625. maxHeaderListSize?: number | undefined;
  626. enableConnectProtocol?: boolean | undefined;
  627. }
  628. export interface ClientSessionRequestOptions {
  629. endStream?: boolean | undefined;
  630. exclusive?: boolean | undefined;
  631. parent?: number | undefined;
  632. weight?: number | undefined;
  633. waitForTrailers?: boolean | undefined;
  634. signal?: AbortSignal | undefined;
  635. }
  636. export interface SessionState {
  637. effectiveLocalWindowSize?: number | undefined;
  638. effectiveRecvDataLength?: number | undefined;
  639. nextStreamID?: number | undefined;
  640. localWindowSize?: number | undefined;
  641. lastProcStreamID?: number | undefined;
  642. remoteWindowSize?: number | undefined;
  643. outboundQueueSize?: number | undefined;
  644. deflateDynamicTableSize?: number | undefined;
  645. inflateDynamicTableSize?: number | undefined;
  646. }
  647. export interface Http2Session extends EventEmitter {
  648. /**
  649. * Value will be `undefined` if the `Http2Session` is not yet connected to a
  650. * socket, `h2c` if the `Http2Session` is not connected to a `TLSSocket`, or
  651. * will return the value of the connected `TLSSocket`'s own `alpnProtocol` property.
  652. * @since v9.4.0
  653. */
  654. readonly alpnProtocol?: string | undefined;
  655. /**
  656. * Will be `true` if this `Http2Session` instance has been closed, otherwise `false`.
  657. * @since v9.4.0
  658. */
  659. readonly closed: boolean;
  660. /**
  661. * Will be `true` if this `Http2Session` instance is still connecting, will be set
  662. * to `false` before emitting `connect` event and/or calling the `http2.connect` callback.
  663. * @since v10.0.0
  664. */
  665. readonly connecting: boolean;
  666. /**
  667. * Will be `true` if this `Http2Session` instance has been destroyed and must no
  668. * longer be used, otherwise `false`.
  669. * @since v8.4.0
  670. */
  671. readonly destroyed: boolean;
  672. /**
  673. * Value is `undefined` if the `Http2Session` session socket has not yet been
  674. * connected, `true` if the `Http2Session` is connected with a `TLSSocket`,
  675. * and `false` if the `Http2Session` is connected to any other kind of socket
  676. * or stream.
  677. * @since v9.4.0
  678. */
  679. readonly encrypted?: boolean | undefined;
  680. /**
  681. * A prototype-less object describing the current local settings of this `Http2Session`.
  682. * The local settings are local to _this_`Http2Session` instance.
  683. * @since v8.4.0
  684. */
  685. readonly localSettings: Settings;
  686. /**
  687. * If the `Http2Session` is connected to a `TLSSocket`, the `originSet` property
  688. * will return an `Array` of origins for which the `Http2Session` may be
  689. * considered authoritative.
  690. *
  691. * The `originSet` property is only available when using a secure TLS connection.
  692. * @since v9.4.0
  693. */
  694. readonly originSet?: string[] | undefined;
  695. /**
  696. * Indicates whether the `Http2Session` is currently waiting for acknowledgment of
  697. * a sent `SETTINGS` frame. Will be `true` after calling the `http2session.settings()` method.
  698. * Will be `false` once all sent `SETTINGS` frames have been acknowledged.
  699. * @since v8.4.0
  700. */
  701. readonly pendingSettingsAck: boolean;
  702. /**
  703. * A prototype-less object describing the current remote settings of this`Http2Session`.
  704. * The remote settings are set by the _connected_ HTTP/2 peer.
  705. * @since v8.4.0
  706. */
  707. readonly remoteSettings: Settings;
  708. /**
  709. * Returns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but
  710. * limits available methods to ones safe to use with HTTP/2.
  711. *
  712. * `destroy`, `emit`, `end`, `pause`, `read`, `resume`, and `write` will throw
  713. * an error with code `ERR_HTTP2_NO_SOCKET_MANIPULATION`. See `Http2Session and Sockets` for more information.
  714. *
  715. * `setTimeout` method will be called on this `Http2Session`.
  716. *
  717. * All other interactions will be routed directly to the socket.
  718. * @since v8.4.0
  719. */
  720. readonly socket: net.Socket | tls.TLSSocket;
  721. /**
  722. * Provides miscellaneous information about the current state of the`Http2Session`.
  723. *
  724. * An object describing the current status of this `Http2Session`.
  725. * @since v8.4.0
  726. */
  727. readonly state: SessionState;
  728. /**
  729. * The `http2session.type` will be equal to `http2.constants.NGHTTP2_SESSION_SERVER` if this `Http2Session` instance is a
  730. * server, and `http2.constants.NGHTTP2_SESSION_CLIENT` if the instance is a
  731. * client.
  732. * @since v8.4.0
  733. */
  734. readonly type: number;
  735. /**
  736. * Gracefully closes the `Http2Session`, allowing any existing streams to
  737. * complete on their own and preventing new `Http2Stream` instances from being
  738. * created. Once closed, `http2session.destroy()`_might_ be called if there
  739. * are no open `Http2Stream` instances.
  740. *
  741. * If specified, the `callback` function is registered as a handler for the`'close'` event.
  742. * @since v9.4.0
  743. */
  744. close(callback?: () => void): void;
  745. /**
  746. * Immediately terminates the `Http2Session` and the associated `net.Socket` or `tls.TLSSocket`.
  747. *
  748. * 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.
  749. *
  750. * If there are any remaining open `Http2Streams` associated with the `Http2Session`, those will also be destroyed.
  751. * @since v8.4.0
  752. * @param error An `Error` object if the `Http2Session` is being destroyed due to an error.
  753. * @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`.
  754. */
  755. destroy(error?: Error, code?: number): void;
  756. /**
  757. * Transmits a `GOAWAY` frame to the connected peer _without_ shutting down the`Http2Session`.
  758. * @since v9.4.0
  759. * @param code An HTTP/2 error code
  760. * @param lastStreamID The numeric ID of the last processed `Http2Stream`
  761. * @param opaqueData A `TypedArray` or `DataView` instance containing additional data to be carried within the `GOAWAY` frame.
  762. */
  763. goaway(code?: number, lastStreamID?: number, opaqueData?: NodeJS.ArrayBufferView): void;
  764. /**
  765. * Sends a `PING` frame to the connected HTTP/2 peer. A `callback` function must
  766. * be provided. The method will return `true` if the `PING` was sent, `false` otherwise.
  767. *
  768. * The maximum number of outstanding (unacknowledged) pings is determined by the `maxOutstandingPings` configuration option. The default maximum is 10.
  769. *
  770. * If provided, the `payload` must be a `Buffer`, `TypedArray`, or `DataView` containing 8 bytes of data that will be transmitted with the `PING` and
  771. * returned with the ping acknowledgment.
  772. *
  773. * The callback will be invoked with three arguments: an error argument that will
  774. * be `null` if the `PING` was successfully acknowledged, a `duration` argument
  775. * that reports the number of milliseconds elapsed since the ping was sent and the
  776. * acknowledgment was received, and a `Buffer` containing the 8-byte `PING` payload.
  777. *
  778. * ```js
  779. * session.ping(Buffer.from('abcdefgh'), (err, duration, payload) => {
  780. * if (!err) {
  781. * console.log(`Ping acknowledged in ${duration} milliseconds`);
  782. * console.log(`With payload '${payload.toString()}'`);
  783. * }
  784. * });
  785. * ```
  786. *
  787. * If the `payload` argument is not specified, the default payload will be the
  788. * 64-bit timestamp (little endian) marking the start of the `PING` duration.
  789. * @since v8.9.3
  790. * @param payload Optional ping payload.
  791. */
  792. ping(callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean;
  793. ping(
  794. payload: NodeJS.ArrayBufferView,
  795. callback: (err: Error | null, duration: number, payload: Buffer) => void,
  796. ): boolean;
  797. /**
  798. * Calls `ref()` on this `Http2Session` instance's underlying `net.Socket`.
  799. * @since v9.4.0
  800. */
  801. ref(): void;
  802. /**
  803. * Sets the local endpoint's window size.
  804. * The `windowSize` is the total window size to set, not
  805. * the delta.
  806. *
  807. * ```js
  808. * import http2 from 'node:http2';
  809. *
  810. * const server = http2.createServer();
  811. * const expectedWindowSize = 2 ** 20;
  812. * server.on('connect', (session) => {
  813. *
  814. * // Set local window size to be 2 ** 20
  815. * session.setLocalWindowSize(expectedWindowSize);
  816. * });
  817. * ```
  818. * @since v15.3.0, v14.18.0
  819. */
  820. setLocalWindowSize(windowSize: number): void;
  821. /**
  822. * Used to set a callback function that is called when there is no activity on
  823. * the `Http2Session` after `msecs` milliseconds. The given `callback` is
  824. * registered as a listener on the `'timeout'` event.
  825. * @since v8.4.0
  826. */
  827. setTimeout(msecs: number, callback?: () => void): void;
  828. /**
  829. * Updates the current local settings for this `Http2Session` and sends a new `SETTINGS` frame to the connected HTTP/2 peer.
  830. *
  831. * Once called, the `http2session.pendingSettingsAck` property will be `true` while the session is waiting for the remote peer to acknowledge the new
  832. * settings.
  833. *
  834. * The new settings will not become effective until the `SETTINGS` acknowledgment
  835. * is received and the `'localSettings'` event is emitted. It is possible to send
  836. * multiple `SETTINGS` frames while acknowledgment is still pending.
  837. * @since v8.4.0
  838. * @param callback Callback that is called once the session is connected or right away if the session is already connected.
  839. */
  840. settings(
  841. settings: Settings,
  842. callback?: (err: Error | null, settings: Settings, duration: number) => void,
  843. ): void;
  844. /**
  845. * Calls `unref()` on this `Http2Session`instance's underlying `net.Socket`.
  846. * @since v9.4.0
  847. */
  848. unref(): void;
  849. addListener(event: "close", listener: () => void): this;
  850. addListener(event: "error", listener: (err: Error) => void): this;
  851. addListener(
  852. event: "frameError",
  853. listener: (frameType: number, errorCode: number, streamID: number) => void,
  854. ): this;
  855. addListener(
  856. event: "goaway",
  857. listener: (errorCode: number, lastStreamID: number, opaqueData?: Buffer) => void,
  858. ): this;
  859. addListener(event: "localSettings", listener: (settings: Settings) => void): this;
  860. addListener(event: "ping", listener: () => void): this;
  861. addListener(event: "remoteSettings", listener: (settings: Settings) => void): this;
  862. addListener(event: "timeout", listener: () => void): this;
  863. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  864. emit(event: "close"): boolean;
  865. emit(event: "error", err: Error): boolean;
  866. emit(event: "frameError", frameType: number, errorCode: number, streamID: number): boolean;
  867. emit(event: "goaway", errorCode: number, lastStreamID: number, opaqueData?: Buffer): boolean;
  868. emit(event: "localSettings", settings: Settings): boolean;
  869. emit(event: "ping"): boolean;
  870. emit(event: "remoteSettings", settings: Settings): boolean;
  871. emit(event: "timeout"): boolean;
  872. emit(event: string | symbol, ...args: any[]): boolean;
  873. on(event: "close", listener: () => void): this;
  874. on(event: "error", listener: (err: Error) => void): this;
  875. on(event: "frameError", listener: (frameType: number, errorCode: number, streamID: number) => void): this;
  876. on(event: "goaway", listener: (errorCode: number, lastStreamID: number, opaqueData?: Buffer) => void): this;
  877. on(event: "localSettings", listener: (settings: Settings) => void): this;
  878. on(event: "ping", listener: () => void): this;
  879. on(event: "remoteSettings", listener: (settings: Settings) => void): this;
  880. on(event: "timeout", listener: () => void): this;
  881. on(event: string | symbol, listener: (...args: any[]) => void): this;
  882. once(event: "close", listener: () => void): this;
  883. once(event: "error", listener: (err: Error) => void): this;
  884. once(event: "frameError", listener: (frameType: number, errorCode: number, streamID: number) => void): this;
  885. once(event: "goaway", listener: (errorCode: number, lastStreamID: number, opaqueData?: Buffer) => void): this;
  886. once(event: "localSettings", listener: (settings: Settings) => void): this;
  887. once(event: "ping", listener: () => void): this;
  888. once(event: "remoteSettings", listener: (settings: Settings) => void): this;
  889. once(event: "timeout", listener: () => void): this;
  890. once(event: string | symbol, listener: (...args: any[]) => void): this;
  891. prependListener(event: "close", listener: () => void): this;
  892. prependListener(event: "error", listener: (err: Error) => void): this;
  893. prependListener(
  894. event: "frameError",
  895. listener: (frameType: number, errorCode: number, streamID: number) => void,
  896. ): this;
  897. prependListener(
  898. event: "goaway",
  899. listener: (errorCode: number, lastStreamID: number, opaqueData?: Buffer) => void,
  900. ): this;
  901. prependListener(event: "localSettings", listener: (settings: Settings) => void): this;
  902. prependListener(event: "ping", listener: () => void): this;
  903. prependListener(event: "remoteSettings", listener: (settings: Settings) => void): this;
  904. prependListener(event: "timeout", listener: () => void): this;
  905. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  906. prependOnceListener(event: "close", listener: () => void): this;
  907. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  908. prependOnceListener(
  909. event: "frameError",
  910. listener: (frameType: number, errorCode: number, streamID: number) => void,
  911. ): this;
  912. prependOnceListener(
  913. event: "goaway",
  914. listener: (errorCode: number, lastStreamID: number, opaqueData?: Buffer) => void,
  915. ): this;
  916. prependOnceListener(event: "localSettings", listener: (settings: Settings) => void): this;
  917. prependOnceListener(event: "ping", listener: () => void): this;
  918. prependOnceListener(event: "remoteSettings", listener: (settings: Settings) => void): this;
  919. prependOnceListener(event: "timeout", listener: () => void): this;
  920. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  921. }
  922. export interface ClientHttp2Session extends Http2Session {
  923. /**
  924. * For HTTP/2 Client `Http2Session` instances only, the `http2session.request()` creates and returns an `Http2Stream` instance that can be used to send an
  925. * HTTP/2 request to the connected server.
  926. *
  927. * When a `ClientHttp2Session` is first created, the socket may not yet be
  928. * connected. if `clienthttp2session.request()` is called during this time, the
  929. * actual request will be deferred until the socket is ready to go.
  930. * If the `session` is closed before the actual request be executed, an `ERR_HTTP2_GOAWAY_SESSION` is thrown.
  931. *
  932. * This method is only available if `http2session.type` is equal to `http2.constants.NGHTTP2_SESSION_CLIENT`.
  933. *
  934. * ```js
  935. * import http2 from 'node:http2';
  936. * const clientSession = http2.connect('https://localhost:1234');
  937. * const {
  938. * HTTP2_HEADER_PATH,
  939. * HTTP2_HEADER_STATUS,
  940. * } = http2.constants;
  941. *
  942. * const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
  943. * req.on('response', (headers) => {
  944. * console.log(headers[HTTP2_HEADER_STATUS]);
  945. * req.on('data', (chunk) => { // .. });
  946. * req.on('end', () => { // .. });
  947. * });
  948. * ```
  949. *
  950. * When the `options.waitForTrailers` option is set, the `'wantTrailers'` event
  951. * is emitted immediately after queuing the last chunk of payload data to be sent.
  952. * The `http2stream.sendTrailers()` method can then be called to send trailing
  953. * headers to the peer.
  954. *
  955. * When `options.waitForTrailers` is set, the `Http2Stream` will not automatically
  956. * close when the final `DATA` frame is transmitted. User code must call either`http2stream.sendTrailers()` or `http2stream.close()` to close the`Http2Stream`.
  957. *
  958. * When `options.signal` is set with an `AbortSignal` and then `abort` on the
  959. * corresponding `AbortController` is called, the request will emit an `'error'`event with an `AbortError` error.
  960. *
  961. * The `:method` and `:path` pseudo-headers are not specified within `headers`,
  962. * they respectively default to:
  963. *
  964. * * `:method` \= `'GET'`
  965. * * `:path` \= `/`
  966. * @since v8.4.0
  967. */
  968. request(headers?: OutgoingHttpHeaders, options?: ClientSessionRequestOptions): ClientHttp2Stream;
  969. addListener(event: "altsvc", listener: (alt: string, origin: string, stream: number) => void): this;
  970. addListener(event: "origin", listener: (origins: string[]) => void): this;
  971. addListener(
  972. event: "connect",
  973. listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  974. ): this;
  975. addListener(
  976. event: "stream",
  977. listener: (
  978. stream: ClientHttp2Stream,
  979. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  980. flags: number,
  981. ) => void,
  982. ): this;
  983. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  984. emit(event: "altsvc", alt: string, origin: string, stream: number): boolean;
  985. emit(event: "origin", origins: readonly string[]): boolean;
  986. emit(event: "connect", session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket): boolean;
  987. emit(
  988. event: "stream",
  989. stream: ClientHttp2Stream,
  990. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  991. flags: number,
  992. ): boolean;
  993. emit(event: string | symbol, ...args: any[]): boolean;
  994. on(event: "altsvc", listener: (alt: string, origin: string, stream: number) => void): this;
  995. on(event: "origin", listener: (origins: string[]) => void): this;
  996. on(event: "connect", listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): this;
  997. on(
  998. event: "stream",
  999. listener: (
  1000. stream: ClientHttp2Stream,
  1001. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  1002. flags: number,
  1003. ) => void,
  1004. ): this;
  1005. on(event: string | symbol, listener: (...args: any[]) => void): this;
  1006. once(event: "altsvc", listener: (alt: string, origin: string, stream: number) => void): this;
  1007. once(event: "origin", listener: (origins: string[]) => void): this;
  1008. once(
  1009. event: "connect",
  1010. listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  1011. ): this;
  1012. once(
  1013. event: "stream",
  1014. listener: (
  1015. stream: ClientHttp2Stream,
  1016. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  1017. flags: number,
  1018. ) => void,
  1019. ): this;
  1020. once(event: string | symbol, listener: (...args: any[]) => void): this;
  1021. prependListener(event: "altsvc", listener: (alt: string, origin: string, stream: number) => void): this;
  1022. prependListener(event: "origin", listener: (origins: string[]) => void): this;
  1023. prependListener(
  1024. event: "connect",
  1025. listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  1026. ): this;
  1027. prependListener(
  1028. event: "stream",
  1029. listener: (
  1030. stream: ClientHttp2Stream,
  1031. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  1032. flags: number,
  1033. ) => void,
  1034. ): this;
  1035. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1036. prependOnceListener(event: "altsvc", listener: (alt: string, origin: string, stream: number) => void): this;
  1037. prependOnceListener(event: "origin", listener: (origins: string[]) => void): this;
  1038. prependOnceListener(
  1039. event: "connect",
  1040. listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  1041. ): this;
  1042. prependOnceListener(
  1043. event: "stream",
  1044. listener: (
  1045. stream: ClientHttp2Stream,
  1046. headers: IncomingHttpHeaders & IncomingHttpStatusHeader,
  1047. flags: number,
  1048. ) => void,
  1049. ): this;
  1050. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1051. }
  1052. export interface AlternativeServiceOptions {
  1053. origin: number | string | url.URL;
  1054. }
  1055. export interface ServerHttp2Session<
  1056. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1057. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1058. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1059. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1060. > extends Http2Session {
  1061. readonly server:
  1062. | Http2Server<Http1Request, Http1Response, Http2Request, Http2Response>
  1063. | Http2SecureServer<Http1Request, Http1Response, Http2Request, Http2Response>;
  1064. /**
  1065. * Submits an `ALTSVC` frame (as defined by [RFC 7838](https://tools.ietf.org/html/rfc7838)) to the connected client.
  1066. *
  1067. * ```js
  1068. * import http2 from 'node:http2';
  1069. *
  1070. * const server = http2.createServer();
  1071. * server.on('session', (session) => {
  1072. * // Set altsvc for origin https://example.org:80
  1073. * session.altsvc('h2=":8000"', 'https://example.org:80');
  1074. * });
  1075. *
  1076. * server.on('stream', (stream) => {
  1077. * // Set altsvc for a specific stream
  1078. * stream.session.altsvc('h2=":8000"', stream.id);
  1079. * });
  1080. * ```
  1081. *
  1082. * Sending an `ALTSVC` frame with a specific stream ID indicates that the alternate
  1083. * service is associated with the origin of the given `Http2Stream`.
  1084. *
  1085. * The `alt` and origin string _must_ contain only ASCII bytes and are
  1086. * 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
  1087. * domain.
  1088. *
  1089. * When a string is passed for the `originOrStream` argument, it will be parsed as
  1090. * a URL and the origin will be derived. For instance, the origin for the
  1091. * HTTP URL `'https://example.org/foo/bar'` is the ASCII string`'https://example.org'`. An error will be thrown if either the given string
  1092. * cannot be parsed as a URL or if a valid origin cannot be derived.
  1093. *
  1094. * 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
  1095. * used. The value of the `origin` property _must_ be a properly serialized
  1096. * ASCII origin.
  1097. * @since v9.4.0
  1098. * @param alt A description of the alternative service configuration as defined by `RFC 7838`.
  1099. * @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
  1100. * `http2stream.id` property.
  1101. */
  1102. altsvc(alt: string, originOrStream: number | string | url.URL | AlternativeServiceOptions): void;
  1103. /**
  1104. * Submits an `ORIGIN` frame (as defined by [RFC 8336](https://tools.ietf.org/html/rfc8336)) to the connected client
  1105. * to advertise the set of origins for which the server is capable of providing
  1106. * authoritative responses.
  1107. *
  1108. * ```js
  1109. * import http2 from 'node:http2';
  1110. * const options = getSecureOptionsSomehow();
  1111. * const server = http2.createSecureServer(options);
  1112. * server.on('stream', (stream) => {
  1113. * stream.respond();
  1114. * stream.end('ok');
  1115. * });
  1116. * server.on('session', (session) => {
  1117. * session.origin('https://example.com', 'https://example.org');
  1118. * });
  1119. * ```
  1120. *
  1121. * When a string is passed as an `origin`, it will be parsed as a URL and the
  1122. * 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
  1123. * string
  1124. * cannot be parsed as a URL or if a valid origin cannot be derived.
  1125. *
  1126. * A `URL` object, or any object with an `origin` property, may be passed as
  1127. * an `origin`, in which case the value of the `origin` property will be
  1128. * used. The value of the `origin` property _must_ be a properly serialized
  1129. * ASCII origin.
  1130. *
  1131. * Alternatively, the `origins` option may be used when creating a new HTTP/2
  1132. * server using the `http2.createSecureServer()` method:
  1133. *
  1134. * ```js
  1135. * import http2 from 'node:http2';
  1136. * const options = getSecureOptionsSomehow();
  1137. * options.origins = ['https://example.com', 'https://example.org'];
  1138. * const server = http2.createSecureServer(options);
  1139. * server.on('stream', (stream) => {
  1140. * stream.respond();
  1141. * stream.end('ok');
  1142. * });
  1143. * ```
  1144. * @since v10.12.0
  1145. * @param origins One or more URL Strings passed as separate arguments.
  1146. */
  1147. origin(
  1148. ...origins: Array<
  1149. | string
  1150. | url.URL
  1151. | {
  1152. origin: string;
  1153. }
  1154. >
  1155. ): void;
  1156. addListener(
  1157. event: "connect",
  1158. listener: (
  1159. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1160. socket: net.Socket | tls.TLSSocket,
  1161. ) => void,
  1162. ): this;
  1163. addListener(
  1164. event: "stream",
  1165. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1166. ): this;
  1167. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1168. emit(
  1169. event: "connect",
  1170. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1171. socket: net.Socket | tls.TLSSocket,
  1172. ): boolean;
  1173. emit(event: "stream", stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number): boolean;
  1174. emit(event: string | symbol, ...args: any[]): boolean;
  1175. on(
  1176. event: "connect",
  1177. listener: (
  1178. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1179. socket: net.Socket | tls.TLSSocket,
  1180. ) => void,
  1181. ): this;
  1182. on(
  1183. event: "stream",
  1184. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1185. ): this;
  1186. on(event: string | symbol, listener: (...args: any[]) => void): this;
  1187. once(
  1188. event: "connect",
  1189. listener: (
  1190. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1191. socket: net.Socket | tls.TLSSocket,
  1192. ) => void,
  1193. ): this;
  1194. once(
  1195. event: "stream",
  1196. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1197. ): this;
  1198. once(event: string | symbol, listener: (...args: any[]) => void): this;
  1199. prependListener(
  1200. event: "connect",
  1201. listener: (
  1202. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1203. socket: net.Socket | tls.TLSSocket,
  1204. ) => void,
  1205. ): this;
  1206. prependListener(
  1207. event: "stream",
  1208. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1209. ): this;
  1210. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1211. prependOnceListener(
  1212. event: "connect",
  1213. listener: (
  1214. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1215. socket: net.Socket | tls.TLSSocket,
  1216. ) => void,
  1217. ): this;
  1218. prependOnceListener(
  1219. event: "stream",
  1220. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1221. ): this;
  1222. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1223. }
  1224. // Http2Server
  1225. export interface SessionOptions {
  1226. maxDeflateDynamicTableSize?: number | undefined;
  1227. maxSessionMemory?: number | undefined;
  1228. maxHeaderListPairs?: number | undefined;
  1229. maxOutstandingPings?: number | undefined;
  1230. maxSendHeaderBlockLength?: number | undefined;
  1231. paddingStrategy?: number | undefined;
  1232. peerMaxConcurrentStreams?: number | undefined;
  1233. settings?: Settings | undefined;
  1234. remoteCustomSettings?: number[] | undefined;
  1235. /**
  1236. * Specifies a timeout in milliseconds that
  1237. * a server should wait when an [`'unknownProtocol'`][] is emitted. If the
  1238. * socket has not been destroyed by that time the server will destroy it.
  1239. * @default 100000
  1240. */
  1241. unknownProtocolTimeout?: number | undefined;
  1242. selectPadding?(frameLen: number, maxFrameLen: number): number;
  1243. }
  1244. export interface ClientSessionOptions extends SessionOptions {
  1245. maxReservedRemoteStreams?: number | undefined;
  1246. createConnection?: ((authority: url.URL, option: SessionOptions) => stream.Duplex) | undefined;
  1247. protocol?: "http:" | "https:" | undefined;
  1248. }
  1249. export interface ServerSessionOptions<
  1250. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1251. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1252. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1253. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1254. > extends SessionOptions {
  1255. streamResetBurst?: number | undefined;
  1256. streamResetRate?: number | undefined;
  1257. Http1IncomingMessage?: Http1Request | undefined;
  1258. Http1ServerResponse?: Http1Response | undefined;
  1259. Http2ServerRequest?: Http2Request | undefined;
  1260. Http2ServerResponse?: Http2Response | undefined;
  1261. }
  1262. export interface SecureClientSessionOptions extends ClientSessionOptions, tls.ConnectionOptions {}
  1263. export interface SecureServerSessionOptions<
  1264. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1265. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1266. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1267. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1268. > extends ServerSessionOptions<Http1Request, Http1Response, Http2Request, Http2Response>, tls.TlsOptions {}
  1269. export interface ServerOptions<
  1270. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1271. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1272. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1273. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1274. > extends ServerSessionOptions<Http1Request, Http1Response, Http2Request, Http2Response> {}
  1275. export interface SecureServerOptions<
  1276. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1277. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1278. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1279. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1280. > extends SecureServerSessionOptions<Http1Request, Http1Response, Http2Request, Http2Response> {
  1281. allowHTTP1?: boolean | undefined;
  1282. origins?: string[] | undefined;
  1283. }
  1284. interface HTTP2ServerCommon {
  1285. setTimeout(msec?: number, callback?: () => void): this;
  1286. /**
  1287. * Throws ERR_HTTP2_INVALID_SETTING_VALUE for invalid settings values.
  1288. * Throws ERR_INVALID_ARG_TYPE for invalid settings argument.
  1289. */
  1290. updateSettings(settings: Settings): void;
  1291. }
  1292. export interface Http2Server<
  1293. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1294. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1295. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1296. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1297. > extends net.Server, HTTP2ServerCommon {
  1298. addListener(
  1299. event: "checkContinue",
  1300. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1301. ): this;
  1302. addListener(
  1303. event: "request",
  1304. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1305. ): this;
  1306. addListener(
  1307. event: "session",
  1308. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1309. ): this;
  1310. addListener(event: "sessionError", listener: (err: Error) => void): this;
  1311. addListener(
  1312. event: "stream",
  1313. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1314. ): this;
  1315. addListener(event: "timeout", listener: () => void): this;
  1316. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1317. emit(
  1318. event: "checkContinue",
  1319. request: InstanceType<Http2Request>,
  1320. response: InstanceType<Http2Response>,
  1321. ): boolean;
  1322. emit(event: "request", request: InstanceType<Http2Request>, response: InstanceType<Http2Response>): boolean;
  1323. emit(
  1324. event: "session",
  1325. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1326. ): boolean;
  1327. emit(event: "sessionError", err: Error): boolean;
  1328. emit(event: "stream", stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number): boolean;
  1329. emit(event: "timeout"): boolean;
  1330. emit(event: string | symbol, ...args: any[]): boolean;
  1331. on(
  1332. event: "checkContinue",
  1333. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1334. ): this;
  1335. on(
  1336. event: "request",
  1337. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1338. ): this;
  1339. on(
  1340. event: "session",
  1341. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1342. ): this;
  1343. on(event: "sessionError", listener: (err: Error) => void): this;
  1344. on(
  1345. event: "stream",
  1346. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1347. ): this;
  1348. on(event: "timeout", listener: () => void): this;
  1349. on(event: string | symbol, listener: (...args: any[]) => void): this;
  1350. once(
  1351. event: "checkContinue",
  1352. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1353. ): this;
  1354. once(
  1355. event: "request",
  1356. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1357. ): this;
  1358. once(
  1359. event: "session",
  1360. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1361. ): this;
  1362. once(event: "sessionError", listener: (err: Error) => void): this;
  1363. once(
  1364. event: "stream",
  1365. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1366. ): this;
  1367. once(event: "timeout", listener: () => void): this;
  1368. once(event: string | symbol, listener: (...args: any[]) => void): this;
  1369. prependListener(
  1370. event: "checkContinue",
  1371. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1372. ): this;
  1373. prependListener(
  1374. event: "request",
  1375. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1376. ): this;
  1377. prependListener(
  1378. event: "session",
  1379. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1380. ): this;
  1381. prependListener(event: "sessionError", listener: (err: Error) => void): this;
  1382. prependListener(
  1383. event: "stream",
  1384. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1385. ): this;
  1386. prependListener(event: "timeout", listener: () => void): this;
  1387. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1388. prependOnceListener(
  1389. event: "checkContinue",
  1390. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1391. ): this;
  1392. prependOnceListener(
  1393. event: "request",
  1394. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1395. ): this;
  1396. prependOnceListener(
  1397. event: "session",
  1398. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1399. ): this;
  1400. prependOnceListener(event: "sessionError", listener: (err: Error) => void): this;
  1401. prependOnceListener(
  1402. event: "stream",
  1403. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1404. ): this;
  1405. prependOnceListener(event: "timeout", listener: () => void): this;
  1406. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1407. }
  1408. export interface Http2SecureServer<
  1409. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  1410. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  1411. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  1412. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  1413. > extends tls.Server, HTTP2ServerCommon {
  1414. addListener(
  1415. event: "checkContinue",
  1416. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1417. ): this;
  1418. addListener(
  1419. event: "request",
  1420. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1421. ): this;
  1422. addListener(
  1423. event: "session",
  1424. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1425. ): this;
  1426. addListener(event: "sessionError", listener: (err: Error) => void): this;
  1427. addListener(
  1428. event: "stream",
  1429. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1430. ): this;
  1431. addListener(event: "timeout", listener: () => void): this;
  1432. addListener(event: "unknownProtocol", listener: (socket: tls.TLSSocket) => void): this;
  1433. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1434. emit(
  1435. event: "checkContinue",
  1436. request: InstanceType<Http2Request>,
  1437. response: InstanceType<Http2Response>,
  1438. ): boolean;
  1439. emit(event: "request", request: InstanceType<Http2Request>, response: InstanceType<Http2Response>): boolean;
  1440. emit(
  1441. event: "session",
  1442. session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>,
  1443. ): boolean;
  1444. emit(event: "sessionError", err: Error): boolean;
  1445. emit(event: "stream", stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number): boolean;
  1446. emit(event: "timeout"): boolean;
  1447. emit(event: "unknownProtocol", socket: tls.TLSSocket): boolean;
  1448. emit(event: string | symbol, ...args: any[]): boolean;
  1449. on(
  1450. event: "checkContinue",
  1451. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1452. ): this;
  1453. on(
  1454. event: "request",
  1455. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1456. ): this;
  1457. on(
  1458. event: "session",
  1459. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1460. ): this;
  1461. on(event: "sessionError", listener: (err: Error) => void): this;
  1462. on(
  1463. event: "stream",
  1464. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1465. ): this;
  1466. on(event: "timeout", listener: () => void): this;
  1467. on(event: "unknownProtocol", listener: (socket: tls.TLSSocket) => void): this;
  1468. on(event: string | symbol, listener: (...args: any[]) => void): this;
  1469. once(
  1470. event: "checkContinue",
  1471. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1472. ): this;
  1473. once(
  1474. event: "request",
  1475. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1476. ): this;
  1477. once(
  1478. event: "session",
  1479. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1480. ): this;
  1481. once(event: "sessionError", listener: (err: Error) => void): this;
  1482. once(
  1483. event: "stream",
  1484. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1485. ): this;
  1486. once(event: "timeout", listener: () => void): this;
  1487. once(event: "unknownProtocol", listener: (socket: tls.TLSSocket) => void): this;
  1488. once(event: string | symbol, listener: (...args: any[]) => void): this;
  1489. prependListener(
  1490. event: "checkContinue",
  1491. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1492. ): this;
  1493. prependListener(
  1494. event: "request",
  1495. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1496. ): this;
  1497. prependListener(
  1498. event: "session",
  1499. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1500. ): this;
  1501. prependListener(event: "sessionError", listener: (err: Error) => void): this;
  1502. prependListener(
  1503. event: "stream",
  1504. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1505. ): this;
  1506. prependListener(event: "timeout", listener: () => void): this;
  1507. prependListener(event: "unknownProtocol", listener: (socket: tls.TLSSocket) => void): this;
  1508. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1509. prependOnceListener(
  1510. event: "checkContinue",
  1511. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1512. ): this;
  1513. prependOnceListener(
  1514. event: "request",
  1515. listener: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  1516. ): this;
  1517. prependOnceListener(
  1518. event: "session",
  1519. listener: (session: ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>) => void,
  1520. ): this;
  1521. prependOnceListener(event: "sessionError", listener: (err: Error) => void): this;
  1522. prependOnceListener(
  1523. event: "stream",
  1524. listener: (stream: ServerHttp2Stream, headers: IncomingHttpHeaders, flags: number) => void,
  1525. ): this;
  1526. prependOnceListener(event: "timeout", listener: () => void): this;
  1527. prependOnceListener(event: "unknownProtocol", listener: (socket: tls.TLSSocket) => void): this;
  1528. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1529. }
  1530. /**
  1531. * A `Http2ServerRequest` object is created by {@link Server} or {@link SecureServer} and passed as the first argument to the `'request'` event. It may be used to access a request status,
  1532. * headers, and
  1533. * data.
  1534. * @since v8.4.0
  1535. */
  1536. export class Http2ServerRequest extends stream.Readable {
  1537. constructor(
  1538. stream: ServerHttp2Stream,
  1539. headers: IncomingHttpHeaders,
  1540. options: stream.ReadableOptions,
  1541. rawHeaders: readonly string[],
  1542. );
  1543. /**
  1544. * The `request.aborted` property will be `true` if the request has
  1545. * been aborted.
  1546. * @since v10.1.0
  1547. */
  1548. readonly aborted: boolean;
  1549. /**
  1550. * The request authority pseudo header field. Because HTTP/2 allows requests
  1551. * to set either `:authority` or `host`, this value is derived from `req.headers[':authority']` if present. Otherwise, it is derived from `req.headers['host']`.
  1552. * @since v8.4.0
  1553. */
  1554. readonly authority: string;
  1555. /**
  1556. * See `request.socket`.
  1557. * @since v8.4.0
  1558. * @deprecated Since v13.0.0 - Use `socket`.
  1559. */
  1560. readonly connection: net.Socket | tls.TLSSocket;
  1561. /**
  1562. * The `request.complete` property will be `true` if the request has
  1563. * been completed, aborted, or destroyed.
  1564. * @since v12.10.0
  1565. */
  1566. readonly complete: boolean;
  1567. /**
  1568. * The request/response headers object.
  1569. *
  1570. * Key-value pairs of header names and values. Header names are lower-cased.
  1571. *
  1572. * ```js
  1573. * // Prints something like:
  1574. * //
  1575. * // { 'user-agent': 'curl/7.22.0',
  1576. * // host: '127.0.0.1:8000',
  1577. * // accept: '*' }
  1578. * console.log(request.headers);
  1579. * ```
  1580. *
  1581. * See `HTTP/2 Headers Object`.
  1582. *
  1583. * In HTTP/2, the request path, host name, protocol, and method are represented as
  1584. * special headers prefixed with the `:` character (e.g. `':path'`). These special
  1585. * headers will be included in the `request.headers` object. Care must be taken not
  1586. * to inadvertently modify these special headers or errors may occur. For instance,
  1587. * removing all headers from the request will cause errors to occur:
  1588. *
  1589. * ```js
  1590. * removeAllHeaders(request.headers);
  1591. * assert(request.url); // Fails because the :path header has been removed
  1592. * ```
  1593. * @since v8.4.0
  1594. */
  1595. readonly headers: IncomingHttpHeaders;
  1596. /**
  1597. * In case of server request, the HTTP version sent by the client. In the case of
  1598. * client response, the HTTP version of the connected-to server. Returns `'2.0'`.
  1599. *
  1600. * Also `message.httpVersionMajor` is the first integer and `message.httpVersionMinor` is the second.
  1601. * @since v8.4.0
  1602. */
  1603. readonly httpVersion: string;
  1604. readonly httpVersionMinor: number;
  1605. readonly httpVersionMajor: number;
  1606. /**
  1607. * The request method as a string. Read-only. Examples: `'GET'`, `'DELETE'`.
  1608. * @since v8.4.0
  1609. */
  1610. readonly method: string;
  1611. /**
  1612. * The raw request/response headers list exactly as they were received.
  1613. *
  1614. * The keys and values are in the same list. It is _not_ a
  1615. * list of tuples. So, the even-numbered offsets are key values, and the
  1616. * odd-numbered offsets are the associated values.
  1617. *
  1618. * Header names are not lowercased, and duplicates are not merged.
  1619. *
  1620. * ```js
  1621. * // Prints something like:
  1622. * //
  1623. * // [ 'user-agent',
  1624. * // 'this is invalid because there can be only one',
  1625. * // 'User-Agent',
  1626. * // 'curl/7.22.0',
  1627. * // 'Host',
  1628. * // '127.0.0.1:8000',
  1629. * // 'ACCEPT',
  1630. * // '*' ]
  1631. * console.log(request.rawHeaders);
  1632. * ```
  1633. * @since v8.4.0
  1634. */
  1635. readonly rawHeaders: string[];
  1636. /**
  1637. * The raw request/response trailer keys and values exactly as they were
  1638. * received. Only populated at the `'end'` event.
  1639. * @since v8.4.0
  1640. */
  1641. readonly rawTrailers: string[];
  1642. /**
  1643. * The request scheme pseudo header field indicating the scheme
  1644. * portion of the target URL.
  1645. * @since v8.4.0
  1646. */
  1647. readonly scheme: string;
  1648. /**
  1649. * Returns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but
  1650. * applies getters, setters, and methods based on HTTP/2 logic.
  1651. *
  1652. * `destroyed`, `readable`, and `writable` properties will be retrieved from and
  1653. * set on `request.stream`.
  1654. *
  1655. * `destroy`, `emit`, `end`, `on` and `once` methods will be called on `request.stream`.
  1656. *
  1657. * `setTimeout` method will be called on `request.stream.session`.
  1658. *
  1659. * `pause`, `read`, `resume`, and `write` will throw an error with code `ERR_HTTP2_NO_SOCKET_MANIPULATION`. See `Http2Session and Sockets` for
  1660. * more information.
  1661. *
  1662. * All other interactions will be routed directly to the socket. With TLS support,
  1663. * use `request.socket.getPeerCertificate()` to obtain the client's
  1664. * authentication details.
  1665. * @since v8.4.0
  1666. */
  1667. readonly socket: net.Socket | tls.TLSSocket;
  1668. /**
  1669. * The `Http2Stream` object backing the request.
  1670. * @since v8.4.0
  1671. */
  1672. readonly stream: ServerHttp2Stream;
  1673. /**
  1674. * The request/response trailers object. Only populated at the `'end'` event.
  1675. * @since v8.4.0
  1676. */
  1677. readonly trailers: IncomingHttpHeaders;
  1678. /**
  1679. * Request URL string. This contains only the URL that is present in the actual
  1680. * HTTP request. If the request is:
  1681. *
  1682. * ```http
  1683. * GET /status?name=ryan HTTP/1.1
  1684. * Accept: text/plain
  1685. * ```
  1686. *
  1687. * Then `request.url` will be:
  1688. *
  1689. * ```js
  1690. * '/status?name=ryan'
  1691. * ```
  1692. *
  1693. * To parse the url into its parts, `new URL()` can be used:
  1694. *
  1695. * ```console
  1696. * $ node
  1697. * > new URL('/status?name=ryan', 'http://example.com')
  1698. * URL {
  1699. * href: 'http://example.com/status?name=ryan',
  1700. * origin: 'http://example.com',
  1701. * protocol: 'http:',
  1702. * username: '',
  1703. * password: '',
  1704. * host: 'example.com',
  1705. * hostname: 'example.com',
  1706. * port: '',
  1707. * pathname: '/status',
  1708. * search: '?name=ryan',
  1709. * searchParams: URLSearchParams { 'name' => 'ryan' },
  1710. * hash: ''
  1711. * }
  1712. * ```
  1713. * @since v8.4.0
  1714. */
  1715. url: string;
  1716. /**
  1717. * Sets the `Http2Stream`'s timeout value to `msecs`. If a callback is
  1718. * provided, then it is added as a listener on the `'timeout'` event on
  1719. * the response object.
  1720. *
  1721. * If no `'timeout'` listener is added to the request, the response, or
  1722. * the server, then `Http2Stream`s are destroyed when they time out. If a
  1723. * handler is assigned to the request, the response, or the server's `'timeout'`events, timed out sockets must be handled explicitly.
  1724. * @since v8.4.0
  1725. */
  1726. setTimeout(msecs: number, callback?: () => void): void;
  1727. read(size?: number): Buffer | string | null;
  1728. addListener(event: "aborted", listener: (hadError: boolean, code: number) => void): this;
  1729. addListener(event: "close", listener: () => void): this;
  1730. addListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  1731. addListener(event: "end", listener: () => void): this;
  1732. addListener(event: "readable", listener: () => void): this;
  1733. addListener(event: "error", listener: (err: Error) => void): this;
  1734. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1735. emit(event: "aborted", hadError: boolean, code: number): boolean;
  1736. emit(event: "close"): boolean;
  1737. emit(event: "data", chunk: Buffer | string): boolean;
  1738. emit(event: "end"): boolean;
  1739. emit(event: "readable"): boolean;
  1740. emit(event: "error", err: Error): boolean;
  1741. emit(event: string | symbol, ...args: any[]): boolean;
  1742. on(event: "aborted", listener: (hadError: boolean, code: number) => void): this;
  1743. on(event: "close", listener: () => void): this;
  1744. on(event: "data", listener: (chunk: Buffer | string) => void): this;
  1745. on(event: "end", listener: () => void): this;
  1746. on(event: "readable", listener: () => void): this;
  1747. on(event: "error", listener: (err: Error) => void): this;
  1748. on(event: string | symbol, listener: (...args: any[]) => void): this;
  1749. once(event: "aborted", listener: (hadError: boolean, code: number) => void): this;
  1750. once(event: "close", listener: () => void): this;
  1751. once(event: "data", listener: (chunk: Buffer | string) => void): this;
  1752. once(event: "end", listener: () => void): this;
  1753. once(event: "readable", listener: () => void): this;
  1754. once(event: "error", listener: (err: Error) => void): this;
  1755. once(event: string | symbol, listener: (...args: any[]) => void): this;
  1756. prependListener(event: "aborted", listener: (hadError: boolean, code: number) => void): this;
  1757. prependListener(event: "close", listener: () => void): this;
  1758. prependListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  1759. prependListener(event: "end", listener: () => void): this;
  1760. prependListener(event: "readable", listener: () => void): this;
  1761. prependListener(event: "error", listener: (err: Error) => void): this;
  1762. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1763. prependOnceListener(event: "aborted", listener: (hadError: boolean, code: number) => void): this;
  1764. prependOnceListener(event: "close", listener: () => void): this;
  1765. prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this;
  1766. prependOnceListener(event: "end", listener: () => void): this;
  1767. prependOnceListener(event: "readable", listener: () => void): this;
  1768. prependOnceListener(event: "error", listener: (err: Error) => void): this;
  1769. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  1770. }
  1771. /**
  1772. * This object is created internally by an HTTP server, not by the user. It is
  1773. * passed as the second parameter to the `'request'` event.
  1774. * @since v8.4.0
  1775. */
  1776. export class Http2ServerResponse<Request extends Http2ServerRequest = Http2ServerRequest> extends stream.Writable {
  1777. constructor(stream: ServerHttp2Stream);
  1778. /**
  1779. * See `response.socket`.
  1780. * @since v8.4.0
  1781. * @deprecated Since v13.0.0 - Use `socket`.
  1782. */
  1783. readonly connection: net.Socket | tls.TLSSocket;
  1784. /**
  1785. * Append a single header value to the header object.
  1786. *
  1787. * If the value is an array, this is equivalent to calling this method multiple times.
  1788. *
  1789. * If there were no previous values for the header, this is equivalent to calling {@link setHeader}.
  1790. *
  1791. * Attempting to set a header field name or value that contains invalid characters will result in a
  1792. * [TypeError](https://nodejs.org/docs/latest-v22.x/api/errors.html#class-typeerror) being thrown.
  1793. *
  1794. * ```js
  1795. * // Returns headers including "set-cookie: a" and "set-cookie: b"
  1796. * const server = http2.createServer((req, res) => {
  1797. * res.setHeader('set-cookie', 'a');
  1798. * res.appendHeader('set-cookie', 'b');
  1799. * res.writeHead(200);
  1800. * res.end('ok');
  1801. * });
  1802. * ```
  1803. * @since v20.12.0
  1804. */
  1805. appendHeader(name: string, value: string | string[]): void;
  1806. /**
  1807. * Boolean value that indicates whether the response has completed. Starts
  1808. * as `false`. After `response.end()` executes, the value will be `true`.
  1809. * @since v8.4.0
  1810. * @deprecated Since v13.4.0,v12.16.0 - Use `writableEnded`.
  1811. */
  1812. readonly finished: boolean;
  1813. /**
  1814. * True if headers were sent, false otherwise (read-only).
  1815. * @since v8.4.0
  1816. */
  1817. readonly headersSent: boolean;
  1818. /**
  1819. * A reference to the original HTTP2 `request` object.
  1820. * @since v15.7.0
  1821. */
  1822. readonly req: Request;
  1823. /**
  1824. * Returns a `Proxy` object that acts as a `net.Socket` (or `tls.TLSSocket`) but
  1825. * applies getters, setters, and methods based on HTTP/2 logic.
  1826. *
  1827. * `destroyed`, `readable`, and `writable` properties will be retrieved from and
  1828. * set on `response.stream`.
  1829. *
  1830. * `destroy`, `emit`, `end`, `on` and `once` methods will be called on `response.stream`.
  1831. *
  1832. * `setTimeout` method will be called on `response.stream.session`.
  1833. *
  1834. * `pause`, `read`, `resume`, and `write` will throw an error with code `ERR_HTTP2_NO_SOCKET_MANIPULATION`. See `Http2Session and Sockets` for
  1835. * more information.
  1836. *
  1837. * All other interactions will be routed directly to the socket.
  1838. *
  1839. * ```js
  1840. * import http2 from 'node:http2';
  1841. * const server = http2.createServer((req, res) => {
  1842. * const ip = req.socket.remoteAddress;
  1843. * const port = req.socket.remotePort;
  1844. * res.end(`Your IP address is ${ip} and your source port is ${port}.`);
  1845. * }).listen(3000);
  1846. * ```
  1847. * @since v8.4.0
  1848. */
  1849. readonly socket: net.Socket | tls.TLSSocket;
  1850. /**
  1851. * The `Http2Stream` object backing the response.
  1852. * @since v8.4.0
  1853. */
  1854. readonly stream: ServerHttp2Stream;
  1855. /**
  1856. * When true, the Date header will be automatically generated and sent in
  1857. * the response if it is not already present in the headers. Defaults to true.
  1858. *
  1859. * This should only be disabled for testing; HTTP requires the Date header
  1860. * in responses.
  1861. * @since v8.4.0
  1862. */
  1863. sendDate: boolean;
  1864. /**
  1865. * When using implicit headers (not calling `response.writeHead()` explicitly),
  1866. * this property controls the status code that will be sent to the client when
  1867. * the headers get flushed.
  1868. *
  1869. * ```js
  1870. * response.statusCode = 404;
  1871. * ```
  1872. *
  1873. * After response header was sent to the client, this property indicates the
  1874. * status code which was sent out.
  1875. * @since v8.4.0
  1876. */
  1877. statusCode: number;
  1878. /**
  1879. * Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns
  1880. * an empty string.
  1881. * @since v8.4.0
  1882. */
  1883. statusMessage: "";
  1884. /**
  1885. * This method adds HTTP trailing headers (a header but at the end of the
  1886. * message) to the response.
  1887. *
  1888. * Attempting to set a header field name or value that contains invalid characters
  1889. * will result in a `TypeError` being thrown.
  1890. * @since v8.4.0
  1891. */
  1892. addTrailers(trailers: OutgoingHttpHeaders): void;
  1893. /**
  1894. * This method signals to the server that all of the response headers and body
  1895. * have been sent; that server should consider this message complete.
  1896. * The method, `response.end()`, MUST be called on each response.
  1897. *
  1898. * If `data` is specified, it is equivalent to calling `response.write(data, encoding)` followed by `response.end(callback)`.
  1899. *
  1900. * If `callback` is specified, it will be called when the response stream
  1901. * is finished.
  1902. * @since v8.4.0
  1903. */
  1904. end(callback?: () => void): this;
  1905. end(data: string | Uint8Array, callback?: () => void): this;
  1906. end(data: string | Uint8Array, encoding: BufferEncoding, callback?: () => void): this;
  1907. /**
  1908. * Reads out a header that has already been queued but not sent to the client.
  1909. * The name is case-insensitive.
  1910. *
  1911. * ```js
  1912. * const contentType = response.getHeader('content-type');
  1913. * ```
  1914. * @since v8.4.0
  1915. */
  1916. getHeader(name: string): string;
  1917. /**
  1918. * Returns an array containing the unique names of the current outgoing headers.
  1919. * All header names are lowercase.
  1920. *
  1921. * ```js
  1922. * response.setHeader('Foo', 'bar');
  1923. * response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
  1924. *
  1925. * const headerNames = response.getHeaderNames();
  1926. * // headerNames === ['foo', 'set-cookie']
  1927. * ```
  1928. * @since v8.4.0
  1929. */
  1930. getHeaderNames(): string[];
  1931. /**
  1932. * Returns a shallow copy of the current outgoing headers. Since a shallow copy
  1933. * is used, array values may be mutated without additional calls to various
  1934. * header-related http module methods. The keys of the returned object are the
  1935. * header names and the values are the respective header values. All header names
  1936. * are lowercase.
  1937. *
  1938. * The object returned by the `response.getHeaders()` method _does not_ prototypically inherit from the JavaScript `Object`. This means that typical `Object` methods such as `obj.toString()`,
  1939. * `obj.hasOwnProperty()`, and others
  1940. * are not defined and _will not work_.
  1941. *
  1942. * ```js
  1943. * response.setHeader('Foo', 'bar');
  1944. * response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
  1945. *
  1946. * const headers = response.getHeaders();
  1947. * // headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
  1948. * ```
  1949. * @since v8.4.0
  1950. */
  1951. getHeaders(): OutgoingHttpHeaders;
  1952. /**
  1953. * Returns `true` if the header identified by `name` is currently set in the
  1954. * outgoing headers. The header name matching is case-insensitive.
  1955. *
  1956. * ```js
  1957. * const hasContentType = response.hasHeader('content-type');
  1958. * ```
  1959. * @since v8.4.0
  1960. */
  1961. hasHeader(name: string): boolean;
  1962. /**
  1963. * Removes a header that has been queued for implicit sending.
  1964. *
  1965. * ```js
  1966. * response.removeHeader('Content-Encoding');
  1967. * ```
  1968. * @since v8.4.0
  1969. */
  1970. removeHeader(name: string): void;
  1971. /**
  1972. * Sets a single header value for implicit headers. If this header already exists
  1973. * in the to-be-sent headers, its value will be replaced. Use an array of strings
  1974. * here to send multiple headers with the same name.
  1975. *
  1976. * ```js
  1977. * response.setHeader('Content-Type', 'text/html; charset=utf-8');
  1978. * ```
  1979. *
  1980. * or
  1981. *
  1982. * ```js
  1983. * response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
  1984. * ```
  1985. *
  1986. * Attempting to set a header field name or value that contains invalid characters
  1987. * will result in a `TypeError` being thrown.
  1988. *
  1989. * When headers have been set with `response.setHeader()`, they will be merged
  1990. * with any headers passed to `response.writeHead()`, with the headers passed
  1991. * to `response.writeHead()` given precedence.
  1992. *
  1993. * ```js
  1994. * // Returns content-type = text/plain
  1995. * const server = http2.createServer((req, res) => {
  1996. * res.setHeader('Content-Type', 'text/html; charset=utf-8');
  1997. * res.setHeader('X-Foo', 'bar');
  1998. * res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  1999. * res.end('ok');
  2000. * });
  2001. * ```
  2002. * @since v8.4.0
  2003. */
  2004. setHeader(name: string, value: number | string | readonly string[]): void;
  2005. /**
  2006. * Sets the `Http2Stream`'s timeout value to `msecs`. If a callback is
  2007. * provided, then it is added as a listener on the `'timeout'` event on
  2008. * the response object.
  2009. *
  2010. * If no `'timeout'` listener is added to the request, the response, or
  2011. * the server, then `Http2Stream` s are destroyed when they time out. If a
  2012. * handler is assigned to the request, the response, or the server's `'timeout'` events, timed out sockets must be handled explicitly.
  2013. * @since v8.4.0
  2014. */
  2015. setTimeout(msecs: number, callback?: () => void): void;
  2016. /**
  2017. * If this method is called and `response.writeHead()` has not been called,
  2018. * it will switch to implicit header mode and flush the implicit headers.
  2019. *
  2020. * This sends a chunk of the response body. This method may
  2021. * be called multiple times to provide successive parts of the body.
  2022. *
  2023. * In the `node:http` module, the response body is omitted when the
  2024. * request is a HEAD request. Similarly, the `204` and `304` responses _must not_ include a message body.
  2025. *
  2026. * `chunk` can be a string or a buffer. If `chunk` is a string,
  2027. * the second parameter specifies how to encode it into a byte stream.
  2028. * By default the `encoding` is `'utf8'`. `callback` will be called when this chunk
  2029. * of data is flushed.
  2030. *
  2031. * This is the raw HTTP body and has nothing to do with higher-level multi-part
  2032. * body encodings that may be used.
  2033. *
  2034. * The first time `response.write()` is called, it will send the buffered
  2035. * header information and the first chunk of the body to the client. The second
  2036. * time `response.write()` is called, Node.js assumes data will be streamed,
  2037. * and sends the new data separately. That is, the response is buffered up to the
  2038. * first chunk of the body.
  2039. *
  2040. * Returns `true` if the entire data was flushed successfully to the kernel
  2041. * buffer. Returns `false` if all or part of the data was queued in user memory.`'drain'` will be emitted when the buffer is free again.
  2042. * @since v8.4.0
  2043. */
  2044. write(chunk: string | Uint8Array, callback?: (err: Error) => void): boolean;
  2045. write(chunk: string | Uint8Array, encoding: BufferEncoding, callback?: (err: Error) => void): boolean;
  2046. /**
  2047. * Sends a status `100 Continue` to the client, indicating that the request body
  2048. * should be sent. See the `'checkContinue'` event on `Http2Server` and `Http2SecureServer`.
  2049. * @since v8.4.0
  2050. */
  2051. writeContinue(): void;
  2052. /**
  2053. * Sends a status `103 Early Hints` to the client with a Link header,
  2054. * indicating that the user agent can preload/preconnect the linked resources.
  2055. * The `hints` is an object containing the values of headers to be sent with
  2056. * early hints message.
  2057. *
  2058. * **Example**
  2059. *
  2060. * ```js
  2061. * const earlyHintsLink = '</styles.css>; rel=preload; as=style';
  2062. * response.writeEarlyHints({
  2063. * 'link': earlyHintsLink,
  2064. * });
  2065. *
  2066. * const earlyHintsLinks = [
  2067. * '</styles.css>; rel=preload; as=style',
  2068. * '</scripts.js>; rel=preload; as=script',
  2069. * ];
  2070. * response.writeEarlyHints({
  2071. * 'link': earlyHintsLinks,
  2072. * });
  2073. * ```
  2074. * @since v18.11.0
  2075. */
  2076. writeEarlyHints(hints: Record<string, string | string[]>): void;
  2077. /**
  2078. * Sends a response header to the request. The status code is a 3-digit HTTP
  2079. * status code, like `404`. The last argument, `headers`, are the response headers.
  2080. *
  2081. * Returns a reference to the `Http2ServerResponse`, so that calls can be chained.
  2082. *
  2083. * For compatibility with `HTTP/1`, a human-readable `statusMessage` may be
  2084. * passed as the second argument. However, because the `statusMessage` has no
  2085. * meaning within HTTP/2, the argument will have no effect and a process warning
  2086. * will be emitted.
  2087. *
  2088. * ```js
  2089. * const body = 'hello world';
  2090. * response.writeHead(200, {
  2091. * 'Content-Length': Buffer.byteLength(body),
  2092. * 'Content-Type': 'text/plain; charset=utf-8',
  2093. * });
  2094. * ```
  2095. *
  2096. * `Content-Length` is given in bytes not characters. The`Buffer.byteLength()` API may be used to determine the number of bytes in a
  2097. * given encoding. On outbound messages, Node.js does not check if Content-Length
  2098. * and the length of the body being transmitted are equal or not. However, when
  2099. * receiving messages, Node.js will automatically reject messages when the `Content-Length` does not match the actual payload size.
  2100. *
  2101. * This method may be called at most one time on a message before `response.end()` is called.
  2102. *
  2103. * If `response.write()` or `response.end()` are called before calling
  2104. * this, the implicit/mutable headers will be calculated and call this function.
  2105. *
  2106. * When headers have been set with `response.setHeader()`, they will be merged
  2107. * with any headers passed to `response.writeHead()`, with the headers passed
  2108. * to `response.writeHead()` given precedence.
  2109. *
  2110. * ```js
  2111. * // Returns content-type = text/plain
  2112. * const server = http2.createServer((req, res) => {
  2113. * res.setHeader('Content-Type', 'text/html; charset=utf-8');
  2114. * res.setHeader('X-Foo', 'bar');
  2115. * res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  2116. * res.end('ok');
  2117. * });
  2118. * ```
  2119. *
  2120. * Attempting to set a header field name or value that contains invalid characters
  2121. * will result in a `TypeError` being thrown.
  2122. * @since v8.4.0
  2123. */
  2124. writeHead(statusCode: number, headers?: OutgoingHttpHeaders): this;
  2125. writeHead(statusCode: number, statusMessage: string, headers?: OutgoingHttpHeaders): this;
  2126. /**
  2127. * Call `http2stream.pushStream()` with the given headers, and wrap the
  2128. * given `Http2Stream` on a newly created `Http2ServerResponse` as the callback
  2129. * parameter if successful. When `Http2ServerRequest` is closed, the callback is
  2130. * called with an error `ERR_HTTP2_INVALID_STREAM`.
  2131. * @since v8.4.0
  2132. * @param headers An object describing the headers
  2133. * @param callback Called once `http2stream.pushStream()` is finished, or either when the attempt to create the pushed `Http2Stream` has failed or has been rejected, or the state of
  2134. * `Http2ServerRequest` is closed prior to calling the `http2stream.pushStream()` method
  2135. */
  2136. createPushResponse(
  2137. headers: OutgoingHttpHeaders,
  2138. callback: (err: Error | null, res: Http2ServerResponse) => void,
  2139. ): void;
  2140. addListener(event: "close", listener: () => void): this;
  2141. addListener(event: "drain", listener: () => void): this;
  2142. addListener(event: "error", listener: (error: Error) => void): this;
  2143. addListener(event: "finish", listener: () => void): this;
  2144. addListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  2145. addListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  2146. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  2147. emit(event: "close"): boolean;
  2148. emit(event: "drain"): boolean;
  2149. emit(event: "error", error: Error): boolean;
  2150. emit(event: "finish"): boolean;
  2151. emit(event: "pipe", src: stream.Readable): boolean;
  2152. emit(event: "unpipe", src: stream.Readable): boolean;
  2153. emit(event: string | symbol, ...args: any[]): boolean;
  2154. on(event: "close", listener: () => void): this;
  2155. on(event: "drain", listener: () => void): this;
  2156. on(event: "error", listener: (error: Error) => void): this;
  2157. on(event: "finish", listener: () => void): this;
  2158. on(event: "pipe", listener: (src: stream.Readable) => void): this;
  2159. on(event: "unpipe", listener: (src: stream.Readable) => void): this;
  2160. on(event: string | symbol, listener: (...args: any[]) => void): this;
  2161. once(event: "close", listener: () => void): this;
  2162. once(event: "drain", listener: () => void): this;
  2163. once(event: "error", listener: (error: Error) => void): this;
  2164. once(event: "finish", listener: () => void): this;
  2165. once(event: "pipe", listener: (src: stream.Readable) => void): this;
  2166. once(event: "unpipe", listener: (src: stream.Readable) => void): this;
  2167. once(event: string | symbol, listener: (...args: any[]) => void): this;
  2168. prependListener(event: "close", listener: () => void): this;
  2169. prependListener(event: "drain", listener: () => void): this;
  2170. prependListener(event: "error", listener: (error: Error) => void): this;
  2171. prependListener(event: "finish", listener: () => void): this;
  2172. prependListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  2173. prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  2174. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  2175. prependOnceListener(event: "close", listener: () => void): this;
  2176. prependOnceListener(event: "drain", listener: () => void): this;
  2177. prependOnceListener(event: "error", listener: (error: Error) => void): this;
  2178. prependOnceListener(event: "finish", listener: () => void): this;
  2179. prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this;
  2180. prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
  2181. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  2182. }
  2183. export namespace constants {
  2184. const NGHTTP2_SESSION_SERVER: number;
  2185. const NGHTTP2_SESSION_CLIENT: number;
  2186. const NGHTTP2_STREAM_STATE_IDLE: number;
  2187. const NGHTTP2_STREAM_STATE_OPEN: number;
  2188. const NGHTTP2_STREAM_STATE_RESERVED_LOCAL: number;
  2189. const NGHTTP2_STREAM_STATE_RESERVED_REMOTE: number;
  2190. const NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL: number;
  2191. const NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE: number;
  2192. const NGHTTP2_STREAM_STATE_CLOSED: number;
  2193. const NGHTTP2_NO_ERROR: number;
  2194. const NGHTTP2_PROTOCOL_ERROR: number;
  2195. const NGHTTP2_INTERNAL_ERROR: number;
  2196. const NGHTTP2_FLOW_CONTROL_ERROR: number;
  2197. const NGHTTP2_SETTINGS_TIMEOUT: number;
  2198. const NGHTTP2_STREAM_CLOSED: number;
  2199. const NGHTTP2_FRAME_SIZE_ERROR: number;
  2200. const NGHTTP2_REFUSED_STREAM: number;
  2201. const NGHTTP2_CANCEL: number;
  2202. const NGHTTP2_COMPRESSION_ERROR: number;
  2203. const NGHTTP2_CONNECT_ERROR: number;
  2204. const NGHTTP2_ENHANCE_YOUR_CALM: number;
  2205. const NGHTTP2_INADEQUATE_SECURITY: number;
  2206. const NGHTTP2_HTTP_1_1_REQUIRED: number;
  2207. const NGHTTP2_ERR_FRAME_SIZE_ERROR: number;
  2208. const NGHTTP2_FLAG_NONE: number;
  2209. const NGHTTP2_FLAG_END_STREAM: number;
  2210. const NGHTTP2_FLAG_END_HEADERS: number;
  2211. const NGHTTP2_FLAG_ACK: number;
  2212. const NGHTTP2_FLAG_PADDED: number;
  2213. const NGHTTP2_FLAG_PRIORITY: number;
  2214. const DEFAULT_SETTINGS_HEADER_TABLE_SIZE: number;
  2215. const DEFAULT_SETTINGS_ENABLE_PUSH: number;
  2216. const DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE: number;
  2217. const DEFAULT_SETTINGS_MAX_FRAME_SIZE: number;
  2218. const MAX_MAX_FRAME_SIZE: number;
  2219. const MIN_MAX_FRAME_SIZE: number;
  2220. const MAX_INITIAL_WINDOW_SIZE: number;
  2221. const NGHTTP2_DEFAULT_WEIGHT: number;
  2222. const NGHTTP2_SETTINGS_HEADER_TABLE_SIZE: number;
  2223. const NGHTTP2_SETTINGS_ENABLE_PUSH: number;
  2224. const NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS: number;
  2225. const NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE: number;
  2226. const NGHTTP2_SETTINGS_MAX_FRAME_SIZE: number;
  2227. const NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE: number;
  2228. const PADDING_STRATEGY_NONE: number;
  2229. const PADDING_STRATEGY_MAX: number;
  2230. const PADDING_STRATEGY_CALLBACK: number;
  2231. const HTTP2_HEADER_STATUS: string;
  2232. const HTTP2_HEADER_METHOD: string;
  2233. const HTTP2_HEADER_AUTHORITY: string;
  2234. const HTTP2_HEADER_SCHEME: string;
  2235. const HTTP2_HEADER_PATH: string;
  2236. const HTTP2_HEADER_ACCEPT_CHARSET: string;
  2237. const HTTP2_HEADER_ACCEPT_ENCODING: string;
  2238. const HTTP2_HEADER_ACCEPT_LANGUAGE: string;
  2239. const HTTP2_HEADER_ACCEPT_RANGES: string;
  2240. const HTTP2_HEADER_ACCEPT: string;
  2241. const HTTP2_HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS: string;
  2242. const HTTP2_HEADER_ACCESS_CONTROL_ALLOW_HEADERS: string;
  2243. const HTTP2_HEADER_ACCESS_CONTROL_ALLOW_METHODS: string;
  2244. const HTTP2_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN: string;
  2245. const HTTP2_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS: string;
  2246. const HTTP2_HEADER_ACCESS_CONTROL_REQUEST_HEADERS: string;
  2247. const HTTP2_HEADER_ACCESS_CONTROL_REQUEST_METHOD: string;
  2248. const HTTP2_HEADER_AGE: string;
  2249. const HTTP2_HEADER_ALLOW: string;
  2250. const HTTP2_HEADER_AUTHORIZATION: string;
  2251. const HTTP2_HEADER_CACHE_CONTROL: string;
  2252. const HTTP2_HEADER_CONNECTION: string;
  2253. const HTTP2_HEADER_CONTENT_DISPOSITION: string;
  2254. const HTTP2_HEADER_CONTENT_ENCODING: string;
  2255. const HTTP2_HEADER_CONTENT_LANGUAGE: string;
  2256. const HTTP2_HEADER_CONTENT_LENGTH: string;
  2257. const HTTP2_HEADER_CONTENT_LOCATION: string;
  2258. const HTTP2_HEADER_CONTENT_MD5: string;
  2259. const HTTP2_HEADER_CONTENT_RANGE: string;
  2260. const HTTP2_HEADER_CONTENT_TYPE: string;
  2261. const HTTP2_HEADER_COOKIE: string;
  2262. const HTTP2_HEADER_DATE: string;
  2263. const HTTP2_HEADER_ETAG: string;
  2264. const HTTP2_HEADER_EXPECT: string;
  2265. const HTTP2_HEADER_EXPIRES: string;
  2266. const HTTP2_HEADER_FROM: string;
  2267. const HTTP2_HEADER_HOST: string;
  2268. const HTTP2_HEADER_IF_MATCH: string;
  2269. const HTTP2_HEADER_IF_MODIFIED_SINCE: string;
  2270. const HTTP2_HEADER_IF_NONE_MATCH: string;
  2271. const HTTP2_HEADER_IF_RANGE: string;
  2272. const HTTP2_HEADER_IF_UNMODIFIED_SINCE: string;
  2273. const HTTP2_HEADER_LAST_MODIFIED: string;
  2274. const HTTP2_HEADER_LINK: string;
  2275. const HTTP2_HEADER_LOCATION: string;
  2276. const HTTP2_HEADER_MAX_FORWARDS: string;
  2277. const HTTP2_HEADER_PREFER: string;
  2278. const HTTP2_HEADER_PROXY_AUTHENTICATE: string;
  2279. const HTTP2_HEADER_PROXY_AUTHORIZATION: string;
  2280. const HTTP2_HEADER_RANGE: string;
  2281. const HTTP2_HEADER_REFERER: string;
  2282. const HTTP2_HEADER_REFRESH: string;
  2283. const HTTP2_HEADER_RETRY_AFTER: string;
  2284. const HTTP2_HEADER_SERVER: string;
  2285. const HTTP2_HEADER_SET_COOKIE: string;
  2286. const HTTP2_HEADER_STRICT_TRANSPORT_SECURITY: string;
  2287. const HTTP2_HEADER_TRANSFER_ENCODING: string;
  2288. const HTTP2_HEADER_TE: string;
  2289. const HTTP2_HEADER_UPGRADE: string;
  2290. const HTTP2_HEADER_USER_AGENT: string;
  2291. const HTTP2_HEADER_VARY: string;
  2292. const HTTP2_HEADER_VIA: string;
  2293. const HTTP2_HEADER_WWW_AUTHENTICATE: string;
  2294. const HTTP2_HEADER_HTTP2_SETTINGS: string;
  2295. const HTTP2_HEADER_KEEP_ALIVE: string;
  2296. const HTTP2_HEADER_PROXY_CONNECTION: string;
  2297. const HTTP2_METHOD_ACL: string;
  2298. const HTTP2_METHOD_BASELINE_CONTROL: string;
  2299. const HTTP2_METHOD_BIND: string;
  2300. const HTTP2_METHOD_CHECKIN: string;
  2301. const HTTP2_METHOD_CHECKOUT: string;
  2302. const HTTP2_METHOD_CONNECT: string;
  2303. const HTTP2_METHOD_COPY: string;
  2304. const HTTP2_METHOD_DELETE: string;
  2305. const HTTP2_METHOD_GET: string;
  2306. const HTTP2_METHOD_HEAD: string;
  2307. const HTTP2_METHOD_LABEL: string;
  2308. const HTTP2_METHOD_LINK: string;
  2309. const HTTP2_METHOD_LOCK: string;
  2310. const HTTP2_METHOD_MERGE: string;
  2311. const HTTP2_METHOD_MKACTIVITY: string;
  2312. const HTTP2_METHOD_MKCALENDAR: string;
  2313. const HTTP2_METHOD_MKCOL: string;
  2314. const HTTP2_METHOD_MKREDIRECTREF: string;
  2315. const HTTP2_METHOD_MKWORKSPACE: string;
  2316. const HTTP2_METHOD_MOVE: string;
  2317. const HTTP2_METHOD_OPTIONS: string;
  2318. const HTTP2_METHOD_ORDERPATCH: string;
  2319. const HTTP2_METHOD_PATCH: string;
  2320. const HTTP2_METHOD_POST: string;
  2321. const HTTP2_METHOD_PRI: string;
  2322. const HTTP2_METHOD_PROPFIND: string;
  2323. const HTTP2_METHOD_PROPPATCH: string;
  2324. const HTTP2_METHOD_PUT: string;
  2325. const HTTP2_METHOD_REBIND: string;
  2326. const HTTP2_METHOD_REPORT: string;
  2327. const HTTP2_METHOD_SEARCH: string;
  2328. const HTTP2_METHOD_TRACE: string;
  2329. const HTTP2_METHOD_UNBIND: string;
  2330. const HTTP2_METHOD_UNCHECKOUT: string;
  2331. const HTTP2_METHOD_UNLINK: string;
  2332. const HTTP2_METHOD_UNLOCK: string;
  2333. const HTTP2_METHOD_UPDATE: string;
  2334. const HTTP2_METHOD_UPDATEREDIRECTREF: string;
  2335. const HTTP2_METHOD_VERSION_CONTROL: string;
  2336. const HTTP_STATUS_CONTINUE: number;
  2337. const HTTP_STATUS_SWITCHING_PROTOCOLS: number;
  2338. const HTTP_STATUS_PROCESSING: number;
  2339. const HTTP_STATUS_OK: number;
  2340. const HTTP_STATUS_CREATED: number;
  2341. const HTTP_STATUS_ACCEPTED: number;
  2342. const HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION: number;
  2343. const HTTP_STATUS_NO_CONTENT: number;
  2344. const HTTP_STATUS_RESET_CONTENT: number;
  2345. const HTTP_STATUS_PARTIAL_CONTENT: number;
  2346. const HTTP_STATUS_MULTI_STATUS: number;
  2347. const HTTP_STATUS_ALREADY_REPORTED: number;
  2348. const HTTP_STATUS_IM_USED: number;
  2349. const HTTP_STATUS_MULTIPLE_CHOICES: number;
  2350. const HTTP_STATUS_MOVED_PERMANENTLY: number;
  2351. const HTTP_STATUS_FOUND: number;
  2352. const HTTP_STATUS_SEE_OTHER: number;
  2353. const HTTP_STATUS_NOT_MODIFIED: number;
  2354. const HTTP_STATUS_USE_PROXY: number;
  2355. const HTTP_STATUS_TEMPORARY_REDIRECT: number;
  2356. const HTTP_STATUS_PERMANENT_REDIRECT: number;
  2357. const HTTP_STATUS_BAD_REQUEST: number;
  2358. const HTTP_STATUS_UNAUTHORIZED: number;
  2359. const HTTP_STATUS_PAYMENT_REQUIRED: number;
  2360. const HTTP_STATUS_FORBIDDEN: number;
  2361. const HTTP_STATUS_NOT_FOUND: number;
  2362. const HTTP_STATUS_METHOD_NOT_ALLOWED: number;
  2363. const HTTP_STATUS_NOT_ACCEPTABLE: number;
  2364. const HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED: number;
  2365. const HTTP_STATUS_REQUEST_TIMEOUT: number;
  2366. const HTTP_STATUS_CONFLICT: number;
  2367. const HTTP_STATUS_GONE: number;
  2368. const HTTP_STATUS_LENGTH_REQUIRED: number;
  2369. const HTTP_STATUS_PRECONDITION_FAILED: number;
  2370. const HTTP_STATUS_PAYLOAD_TOO_LARGE: number;
  2371. const HTTP_STATUS_URI_TOO_LONG: number;
  2372. const HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE: number;
  2373. const HTTP_STATUS_RANGE_NOT_SATISFIABLE: number;
  2374. const HTTP_STATUS_EXPECTATION_FAILED: number;
  2375. const HTTP_STATUS_TEAPOT: number;
  2376. const HTTP_STATUS_MISDIRECTED_REQUEST: number;
  2377. const HTTP_STATUS_UNPROCESSABLE_ENTITY: number;
  2378. const HTTP_STATUS_LOCKED: number;
  2379. const HTTP_STATUS_FAILED_DEPENDENCY: number;
  2380. const HTTP_STATUS_UNORDERED_COLLECTION: number;
  2381. const HTTP_STATUS_UPGRADE_REQUIRED: number;
  2382. const HTTP_STATUS_PRECONDITION_REQUIRED: number;
  2383. const HTTP_STATUS_TOO_MANY_REQUESTS: number;
  2384. const HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE: number;
  2385. const HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS: number;
  2386. const HTTP_STATUS_INTERNAL_SERVER_ERROR: number;
  2387. const HTTP_STATUS_NOT_IMPLEMENTED: number;
  2388. const HTTP_STATUS_BAD_GATEWAY: number;
  2389. const HTTP_STATUS_SERVICE_UNAVAILABLE: number;
  2390. const HTTP_STATUS_GATEWAY_TIMEOUT: number;
  2391. const HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED: number;
  2392. const HTTP_STATUS_VARIANT_ALSO_NEGOTIATES: number;
  2393. const HTTP_STATUS_INSUFFICIENT_STORAGE: number;
  2394. const HTTP_STATUS_LOOP_DETECTED: number;
  2395. const HTTP_STATUS_BANDWIDTH_LIMIT_EXCEEDED: number;
  2396. const HTTP_STATUS_NOT_EXTENDED: number;
  2397. const HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED: number;
  2398. }
  2399. /**
  2400. * This symbol can be set as a property on the HTTP/2 headers object with
  2401. * an array value in order to provide a list of headers considered sensitive.
  2402. */
  2403. export const sensitiveHeaders: symbol;
  2404. /**
  2405. * Returns an object containing the default settings for an `Http2Session` instance. This method returns a new object instance every time it is called
  2406. * so instances returned may be safely modified for use.
  2407. * @since v8.4.0
  2408. */
  2409. export function getDefaultSettings(): Settings;
  2410. /**
  2411. * Returns a `Buffer` instance containing serialized representation of the given
  2412. * HTTP/2 settings as specified in the [HTTP/2](https://tools.ietf.org/html/rfc7540) specification. This is intended
  2413. * for use with the `HTTP2-Settings` header field.
  2414. *
  2415. * ```js
  2416. * import http2 from 'node:http2';
  2417. *
  2418. * const packed = http2.getPackedSettings({ enablePush: false });
  2419. *
  2420. * console.log(packed.toString('base64'));
  2421. * // Prints: AAIAAAAA
  2422. * ```
  2423. * @since v8.4.0
  2424. */
  2425. export function getPackedSettings(settings: Settings): Buffer;
  2426. /**
  2427. * Returns a `HTTP/2 Settings Object` containing the deserialized settings from
  2428. * the given `Buffer` as generated by `http2.getPackedSettings()`.
  2429. * @since v8.4.0
  2430. * @param buf The packed settings.
  2431. */
  2432. export function getUnpackedSettings(buf: Uint8Array): Settings;
  2433. /**
  2434. * Returns a `net.Server` instance that creates and manages `Http2Session` instances.
  2435. *
  2436. * Since there are no browsers known that support [unencrypted HTTP/2](https://http2.github.io/faq/#does-http2-require-encryption), the use of {@link createSecureServer} is necessary when
  2437. * communicating
  2438. * with browser clients.
  2439. *
  2440. * ```js
  2441. * import http2 from 'node:http2';
  2442. *
  2443. * // Create an unencrypted HTTP/2 server.
  2444. * // Since there are no browsers known that support
  2445. * // unencrypted HTTP/2, the use of `http2.createSecureServer()`
  2446. * // is necessary when communicating with browser clients.
  2447. * const server = http2.createServer();
  2448. *
  2449. * server.on('stream', (stream, headers) => {
  2450. * stream.respond({
  2451. * 'content-type': 'text/html; charset=utf-8',
  2452. * ':status': 200,
  2453. * });
  2454. * stream.end('<h1>Hello World</h1>');
  2455. * });
  2456. *
  2457. * server.listen(8000);
  2458. * ```
  2459. * @since v8.4.0
  2460. * @param onRequestHandler See `Compatibility API`
  2461. */
  2462. export function createServer(
  2463. onRequestHandler?: (request: Http2ServerRequest, response: Http2ServerResponse) => void,
  2464. ): Http2Server;
  2465. export function createServer<
  2466. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  2467. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  2468. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  2469. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  2470. >(
  2471. options: ServerOptions<Http1Request, Http1Response, Http2Request, Http2Response>,
  2472. onRequestHandler?: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  2473. ): Http2Server<Http1Request, Http1Response, Http2Request, Http2Response>;
  2474. /**
  2475. * Returns a `tls.Server` instance that creates and manages `Http2Session` instances.
  2476. *
  2477. * ```js
  2478. * import http2 from 'node:http2';
  2479. * import fs from 'node:fs';
  2480. *
  2481. * const options = {
  2482. * key: fs.readFileSync('server-key.pem'),
  2483. * cert: fs.readFileSync('server-cert.pem'),
  2484. * };
  2485. *
  2486. * // Create a secure HTTP/2 server
  2487. * const server = http2.createSecureServer(options);
  2488. *
  2489. * server.on('stream', (stream, headers) => {
  2490. * stream.respond({
  2491. * 'content-type': 'text/html; charset=utf-8',
  2492. * ':status': 200,
  2493. * });
  2494. * stream.end('<h1>Hello World</h1>');
  2495. * });
  2496. *
  2497. * server.listen(8443);
  2498. * ```
  2499. * @since v8.4.0
  2500. * @param onRequestHandler See `Compatibility API`
  2501. */
  2502. export function createSecureServer(
  2503. onRequestHandler?: (request: Http2ServerRequest, response: Http2ServerResponse) => void,
  2504. ): Http2SecureServer;
  2505. export function createSecureServer<
  2506. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  2507. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  2508. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  2509. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  2510. >(
  2511. options: SecureServerOptions<Http1Request, Http1Response, Http2Request, Http2Response>,
  2512. onRequestHandler?: (request: InstanceType<Http2Request>, response: InstanceType<Http2Response>) => void,
  2513. ): Http2SecureServer<Http1Request, Http1Response, Http2Request, Http2Response>;
  2514. /**
  2515. * Returns a `ClientHttp2Session` instance.
  2516. *
  2517. * ```js
  2518. * import http2 from 'node:http2';
  2519. * const client = http2.connect('https://localhost:1234');
  2520. *
  2521. * // Use the client
  2522. *
  2523. * client.close();
  2524. * ```
  2525. * @since v8.4.0
  2526. * @param authority The remote HTTP/2 server to connect to. This must be in the form of a minimal, valid URL with the `http://` or `https://` prefix, host name, and IP port (if a non-default port
  2527. * is used). Userinfo (user ID and password), path, querystring, and fragment details in the URL will be ignored.
  2528. * @param listener Will be registered as a one-time listener of the {@link 'connect'} event.
  2529. */
  2530. export function connect(
  2531. authority: string | url.URL,
  2532. listener: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  2533. ): ClientHttp2Session;
  2534. export function connect(
  2535. authority: string | url.URL,
  2536. options?: ClientSessionOptions | SecureClientSessionOptions,
  2537. listener?: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void,
  2538. ): ClientHttp2Session;
  2539. /**
  2540. * Create an HTTP/2 server session from an existing socket.
  2541. * @param socket A Duplex Stream
  2542. * @param options Any `{@link createServer}` options can be provided.
  2543. * @since v20.12.0
  2544. */
  2545. export function performServerHandshake<
  2546. Http1Request extends typeof IncomingMessage = typeof IncomingMessage,
  2547. Http1Response extends typeof ServerResponse<InstanceType<Http1Request>> = typeof ServerResponse,
  2548. Http2Request extends typeof Http2ServerRequest = typeof Http2ServerRequest,
  2549. Http2Response extends typeof Http2ServerResponse<InstanceType<Http2Request>> = typeof Http2ServerResponse,
  2550. >(
  2551. socket: stream.Duplex,
  2552. options?: ServerOptions<Http1Request, Http1Response, Http2Request, Http2Response>,
  2553. ): ServerHttp2Session<Http1Request, Http1Response, Http2Request, Http2Response>;
  2554. }
  2555. declare module "node:http2" {
  2556. export * from "http2";
  2557. }