@aligent/cdk-prerender-proxy 0.2.4 → 0.2.5-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/lib/handlers/cache-control.ts +7 -20
  2. package/lib/handlers/error-response.ts +8 -51
  3. package/lib/handlers/node_modules/.package-lock.json +32 -3
  4. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/index.d.ts +6 -0
  5. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/index.js +14 -0
  6. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/cache-control.d.ts +3 -0
  7. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/cache-control.js +24 -0
  8. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/error-response.d.ts +3 -0
  9. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/error-response.js +50 -0
  10. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender-check.d.ts +3 -0
  11. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender-check.js +28 -0
  12. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender.d.ts +3 -0
  13. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender.js +49 -0
  14. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/redirect.d.ts +3 -0
  15. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/redirect.js +33 -0
  16. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/package.json +27 -0
  17. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/tsconfig.json +3 -0
  18. package/lib/handlers/node_modules/@middy/core/LICENSE +21 -0
  19. package/lib/handlers/node_modules/@middy/core/README.md +65 -0
  20. package/lib/handlers/node_modules/@middy/core/index.cjs +207 -0
  21. package/lib/handlers/node_modules/@middy/core/index.d.ts +91 -0
  22. package/lib/handlers/node_modules/@middy/core/index.js +199 -0
  23. package/lib/handlers/node_modules/@middy/core/package.json +65 -0
  24. package/lib/handlers/node_modules/@types/node/README.md +4 -4
  25. package/lib/handlers/node_modules/@types/node/assert/strict.d.ts +5 -1
  26. package/lib/handlers/node_modules/@types/node/assert.d.ts +904 -67
  27. package/lib/handlers/node_modules/@types/node/async_hooks.d.ts +371 -96
  28. package/lib/handlers/node_modules/@types/node/buffer.d.ts +2199 -25
  29. package/lib/handlers/node_modules/@types/node/child_process.d.ts +1151 -308
  30. package/lib/handlers/node_modules/@types/node/cluster.d.ts +356 -208
  31. package/lib/handlers/node_modules/@types/node/console.d.ts +324 -45
  32. package/lib/handlers/node_modules/@types/node/constants.d.ts +8 -3
  33. package/lib/handlers/node_modules/@types/node/crypto.d.ts +3230 -846
  34. package/lib/handlers/node_modules/@types/node/dgram.d.ts +478 -74
  35. package/lib/handlers/node_modules/@types/node/diagnostics_channel.d.ts +153 -0
  36. package/lib/handlers/node_modules/@types/node/dns/promises.d.ts +308 -35
  37. package/lib/handlers/node_modules/@types/node/dns.d.ts +444 -107
  38. package/lib/handlers/node_modules/@types/node/dom-events.d.ts +126 -0
  39. package/lib/handlers/node_modules/@types/node/domain.d.ts +162 -16
  40. package/lib/handlers/node_modules/@types/node/events.d.ts +624 -39
  41. package/lib/handlers/node_modules/@types/node/fs/promises.d.ts +911 -365
  42. package/lib/handlers/node_modules/@types/node/fs.d.ts +2611 -978
  43. package/lib/handlers/node_modules/@types/node/globals.d.ts +66 -421
  44. package/lib/handlers/node_modules/@types/node/globals.global.d.ts +1 -1
  45. package/lib/handlers/node_modules/@types/node/http.d.ts +1416 -199
  46. package/lib/handlers/node_modules/@types/node/http2.d.ts +1688 -530
  47. package/lib/handlers/node_modules/@types/node/https.d.ts +532 -26
  48. package/lib/handlers/node_modules/@types/node/index.d.ts +92 -16
  49. package/lib/handlers/node_modules/@types/node/inspector.d.ts +511 -811
  50. package/lib/handlers/node_modules/@types/node/module.d.ts +75 -13
  51. package/lib/handlers/node_modules/@types/node/net.d.ts +710 -167
  52. package/lib/handlers/node_modules/@types/node/os.d.ts +252 -25
  53. package/lib/handlers/node_modules/@types/node/package.json +30 -25
  54. package/lib/handlers/node_modules/@types/node/path.d.ts +59 -31
  55. package/lib/handlers/node_modules/@types/node/perf_hooks.d.ts +420 -105
  56. package/lib/handlers/node_modules/@types/node/process.d.ts +1249 -228
  57. package/lib/handlers/node_modules/@types/node/punycode.d.ts +67 -25
  58. package/lib/handlers/node_modules/@types/node/querystring.d.ts +113 -10
  59. package/lib/handlers/node_modules/@types/node/readline/promises.d.ts +143 -0
  60. package/lib/handlers/node_modules/@types/node/readline.d.ts +570 -109
  61. package/lib/handlers/node_modules/@types/node/repl.d.ts +154 -125
  62. package/lib/handlers/node_modules/@types/node/stream/consumers.d.ts +12 -0
  63. package/lib/handlers/node_modules/@types/node/stream/promises.d.ts +18 -43
  64. package/lib/handlers/node_modules/@types/node/stream/web.d.ts +330 -0
  65. package/lib/handlers/node_modules/@types/node/stream.d.ts +1117 -248
  66. package/lib/handlers/node_modules/@types/node/string_decoder.d.ts +60 -0
  67. package/lib/handlers/node_modules/@types/node/test.d.ts +446 -0
  68. package/lib/handlers/node_modules/@types/node/timers/promises.d.ts +81 -9
  69. package/lib/handlers/node_modules/@types/node/timers.d.ts +84 -17
  70. package/lib/handlers/node_modules/@types/node/tls.d.ts +510 -275
  71. package/lib/handlers/node_modules/@types/node/trace_events.d.ts +120 -10
  72. package/lib/handlers/node_modules/@types/node/ts4.8/assert/strict.d.ts +8 -0
  73. package/lib/handlers/node_modules/@types/node/ts4.8/assert.d.ts +961 -0
  74. package/lib/handlers/node_modules/@types/node/ts4.8/async_hooks.d.ts +501 -0
  75. package/lib/handlers/node_modules/@types/node/ts4.8/buffer.d.ts +2259 -0
  76. package/lib/handlers/node_modules/@types/node/ts4.8/child_process.d.ts +1369 -0
  77. package/lib/handlers/node_modules/@types/node/ts4.8/cluster.d.ts +410 -0
  78. package/lib/handlers/node_modules/@types/node/ts4.8/console.d.ts +412 -0
  79. package/lib/handlers/node_modules/@types/node/ts4.8/constants.d.ts +18 -0
  80. package/lib/handlers/node_modules/@types/node/ts4.8/crypto.d.ts +3964 -0
  81. package/lib/handlers/node_modules/@types/node/ts4.8/dgram.d.ts +545 -0
  82. package/lib/handlers/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +153 -0
  83. package/lib/handlers/node_modules/@types/node/ts4.8/dns/promises.d.ts +370 -0
  84. package/lib/handlers/node_modules/@types/node/ts4.8/dns.d.ts +659 -0
  85. package/lib/handlers/node_modules/@types/node/ts4.8/dom-events.d.ts +126 -0
  86. package/lib/handlers/node_modules/@types/node/ts4.8/domain.d.ts +170 -0
  87. package/lib/handlers/node_modules/@types/node/ts4.8/events.d.ts +678 -0
  88. package/lib/handlers/node_modules/@types/node/ts4.8/fs/promises.d.ts +1138 -0
  89. package/lib/handlers/node_modules/@types/node/ts4.8/fs.d.ts +3872 -0
  90. package/lib/handlers/node_modules/@types/node/ts4.8/globals.d.ts +294 -0
  91. package/lib/handlers/node_modules/@types/node/ts4.8/globals.global.d.ts +1 -0
  92. package/lib/handlers/node_modules/@types/node/ts4.8/http.d.ts +1651 -0
  93. package/lib/handlers/node_modules/@types/node/ts4.8/http2.d.ts +2134 -0
  94. package/lib/handlers/node_modules/@types/node/ts4.8/https.d.ts +542 -0
  95. package/lib/handlers/node_modules/@types/node/ts4.8/index.d.ts +88 -0
  96. package/lib/handlers/node_modules/@types/node/ts4.8/inspector.d.ts +2741 -0
  97. package/lib/handlers/node_modules/@types/node/ts4.8/module.d.ts +114 -0
  98. package/lib/handlers/node_modules/@types/node/ts4.8/net.d.ts +869 -0
  99. package/lib/handlers/node_modules/@types/node/ts4.8/os.d.ts +466 -0
  100. package/lib/handlers/node_modules/@types/node/ts4.8/path.d.ts +191 -0
  101. package/lib/handlers/node_modules/@types/node/ts4.8/perf_hooks.d.ts +625 -0
  102. package/lib/handlers/node_modules/@types/node/ts4.8/process.d.ts +1482 -0
  103. package/lib/handlers/node_modules/@types/node/ts4.8/punycode.d.ts +117 -0
  104. package/lib/handlers/node_modules/@types/node/ts4.8/querystring.d.ts +131 -0
  105. package/lib/handlers/node_modules/@types/node/ts4.8/readline/promises.d.ts +143 -0
  106. package/lib/handlers/node_modules/@types/node/ts4.8/readline.d.ts +653 -0
  107. package/lib/handlers/node_modules/@types/node/ts4.8/repl.d.ts +424 -0
  108. package/lib/handlers/node_modules/@types/node/ts4.8/stream/consumers.d.ts +12 -0
  109. package/lib/handlers/node_modules/@types/node/ts4.8/stream/promises.d.ts +42 -0
  110. package/lib/handlers/node_modules/@types/node/ts4.8/stream/web.d.ts +330 -0
  111. package/lib/handlers/node_modules/@types/node/ts4.8/stream.d.ts +1340 -0
  112. package/lib/handlers/node_modules/@types/node/ts4.8/string_decoder.d.ts +67 -0
  113. package/lib/handlers/node_modules/@types/node/ts4.8/test.d.ts +446 -0
  114. package/lib/handlers/node_modules/@types/node/ts4.8/timers/promises.d.ts +93 -0
  115. package/lib/handlers/node_modules/@types/node/ts4.8/timers.d.ts +94 -0
  116. package/lib/handlers/node_modules/@types/node/ts4.8/tls.d.ts +1028 -0
  117. package/lib/handlers/node_modules/@types/node/ts4.8/trace_events.d.ts +171 -0
  118. package/lib/handlers/node_modules/@types/node/ts4.8/tty.d.ts +206 -0
  119. package/lib/handlers/node_modules/@types/node/ts4.8/url.d.ts +897 -0
  120. package/lib/handlers/node_modules/@types/node/ts4.8/util.d.ts +1926 -0
  121. package/lib/handlers/node_modules/@types/node/ts4.8/v8.d.ts +396 -0
  122. package/lib/handlers/node_modules/@types/node/ts4.8/vm.d.ts +509 -0
  123. package/lib/handlers/node_modules/@types/node/ts4.8/wasi.d.ts +158 -0
  124. package/lib/handlers/node_modules/@types/node/ts4.8/worker_threads.d.ts +689 -0
  125. package/lib/handlers/node_modules/@types/node/ts4.8/zlib.d.ts +517 -0
  126. package/lib/handlers/node_modules/@types/node/tty.d.ts +163 -23
  127. package/lib/handlers/node_modules/@types/node/url.d.ts +825 -44
  128. package/lib/handlers/node_modules/@types/node/util.d.ts +1842 -72
  129. package/lib/handlers/node_modules/@types/node/v8.d.ts +276 -78
  130. package/lib/handlers/node_modules/@types/node/vm.d.ts +410 -53
  131. package/lib/handlers/node_modules/@types/node/wasi.d.ts +102 -30
  132. package/lib/handlers/node_modules/@types/node/worker_threads.d.ts +559 -152
  133. package/lib/handlers/node_modules/@types/node/zlib.d.ts +234 -78
  134. package/lib/handlers/node_modules/esbuild/README.md +3 -0
  135. package/lib/handlers/node_modules/esbuild/bin/esbuild +0 -0
  136. package/lib/handlers/node_modules/esbuild/install.js +256 -0
  137. package/lib/handlers/node_modules/esbuild/lib/main.d.ts +397 -0
  138. package/lib/handlers/node_modules/esbuild/lib/main.js +1939 -0
  139. package/lib/handlers/node_modules/esbuild/package.json +15 -0
  140. package/lib/handlers/package-lock.json +62 -6
  141. package/lib/handlers/package.json +6 -1
  142. package/lib/handlers/prerender-check.ts +8 -27
  143. package/lib/handlers/prerender.ts +8 -43
  144. package/package.json +1 -1
  145. package/lib/handlers/node_modules/@types/node/base.d.ts +0 -19
  146. package/lib/handlers/node_modules/@types/node/diagnostic_channel.d.ts +0 -34
  147. package/lib/handlers/node_modules/@types/node/ts3.6/assert.d.ts +0 -98
  148. package/lib/handlers/node_modules/@types/node/ts3.6/base.d.ts +0 -68
  149. package/lib/handlers/node_modules/@types/node/ts3.6/index.d.ts +0 -7
  150. package/lib/handlers/node_modules/@types/node/util/types.d.ts +0 -53
@@ -1,163 +1,441 @@
1
+ /**
2
+ * To use the HTTP server and client one must `require('http')`.
3
+ *
4
+ * The HTTP interfaces in Node.js are designed to support many features
5
+ * of the protocol which have been traditionally difficult to use.
6
+ * In particular, large, possibly chunk-encoded, messages. The interface is
7
+ * careful to never buffer entire requests or responses, so the
8
+ * user is able to stream data.
9
+ *
10
+ * HTTP message headers are represented by an object like this:
11
+ *
12
+ * ```js
13
+ * { 'content-length': '123',
14
+ * 'content-type': 'text/plain',
15
+ * 'connection': 'keep-alive',
16
+ * 'host': 'example.com',
17
+ * 'accept': '*' }
18
+ * ```
19
+ *
20
+ * Keys are lowercased. Values are not modified.
21
+ *
22
+ * In order to support the full spectrum of possible HTTP applications, the Node.js
23
+ * HTTP API is very low-level. It deals with stream handling and message
24
+ * parsing only. It parses a message into headers and body but it does not
25
+ * parse the actual headers or the body.
26
+ *
27
+ * See `message.headers` for details on how duplicate headers are handled.
28
+ *
29
+ * The raw headers as they were received are retained in the `rawHeaders`property, which is an array of `[key, value, key2, value2, ...]`. For
30
+ * example, the previous message header object might have a `rawHeaders`list like the following:
31
+ *
32
+ * ```js
33
+ * [ 'ConTent-Length', '123456',
34
+ * 'content-LENGTH', '123',
35
+ * 'content-type', 'text/plain',
36
+ * 'CONNECTION', 'keep-alive',
37
+ * 'Host', 'example.com',
38
+ * 'accepT', '*' ]
39
+ * ```
40
+ * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/http.js)
41
+ */
1
42
  declare module 'http' {
2
- import * as stream from 'stream';
3
- import { URL } from 'url';
4
- import { Socket, Server as NetServer } from 'net';
5
-
43
+ import * as stream from 'node:stream';
44
+ import { URL } from 'node:url';
45
+ import { TcpSocketConnectOpts, Socket, Server as NetServer, LookupFunction } from 'node:net';
46
+ import { LookupOptions } from 'node:dns';
6
47
  // incoming headers will never contain number
7
48
  interface IncomingHttpHeaders extends NodeJS.Dict<string | string[]> {
8
- 'accept'?: string;
9
- 'accept-language'?: string;
10
- 'accept-patch'?: string;
11
- 'accept-ranges'?: string;
12
- 'access-control-allow-credentials'?: string;
13
- 'access-control-allow-headers'?: string;
14
- 'access-control-allow-methods'?: string;
15
- 'access-control-allow-origin'?: string;
16
- 'access-control-expose-headers'?: string;
17
- 'access-control-max-age'?: string;
18
- 'access-control-request-headers'?: string;
19
- 'access-control-request-method'?: string;
20
- 'age'?: string;
21
- 'allow'?: string;
22
- 'alt-svc'?: string;
23
- 'authorization'?: string;
24
- 'cache-control'?: string;
25
- 'connection'?: string;
26
- 'content-disposition'?: string;
27
- 'content-encoding'?: string;
28
- 'content-language'?: string;
29
- 'content-length'?: string;
30
- 'content-location'?: string;
31
- 'content-range'?: string;
32
- 'content-type'?: string;
33
- 'cookie'?: string;
34
- 'date'?: string;
35
- 'etag'?: string;
36
- 'expect'?: string;
37
- 'expires'?: string;
38
- 'forwarded'?: string;
39
- 'from'?: string;
40
- 'host'?: string;
41
- 'if-match'?: string;
42
- 'if-modified-since'?: string;
43
- 'if-none-match'?: string;
44
- 'if-unmodified-since'?: string;
45
- 'last-modified'?: string;
46
- 'location'?: string;
47
- 'origin'?: string;
48
- 'pragma'?: string;
49
- 'proxy-authenticate'?: string;
50
- 'proxy-authorization'?: string;
51
- 'public-key-pins'?: string;
52
- 'range'?: string;
53
- 'referer'?: string;
54
- 'retry-after'?: string;
55
- 'sec-websocket-accept'?: string;
56
- 'sec-websocket-extensions'?: string;
57
- 'sec-websocket-key'?: string;
58
- 'sec-websocket-protocol'?: string;
59
- 'sec-websocket-version'?: string;
60
- 'set-cookie'?: string[];
61
- 'strict-transport-security'?: string;
62
- 'tk'?: string;
63
- 'trailer'?: string;
64
- 'transfer-encoding'?: string;
65
- 'upgrade'?: string;
66
- 'user-agent'?: string;
67
- 'vary'?: string;
68
- 'via'?: string;
69
- 'warning'?: string;
70
- 'www-authenticate'?: string;
49
+ accept?: string | undefined;
50
+ 'accept-language'?: string | undefined;
51
+ 'accept-patch'?: string | undefined;
52
+ 'accept-ranges'?: string | undefined;
53
+ 'access-control-allow-credentials'?: string | undefined;
54
+ 'access-control-allow-headers'?: string | undefined;
55
+ 'access-control-allow-methods'?: string | undefined;
56
+ 'access-control-allow-origin'?: string | undefined;
57
+ 'access-control-expose-headers'?: string | undefined;
58
+ 'access-control-max-age'?: string | undefined;
59
+ 'access-control-request-headers'?: string | undefined;
60
+ 'access-control-request-method'?: string | undefined;
61
+ age?: string | undefined;
62
+ allow?: string | undefined;
63
+ 'alt-svc'?: string | undefined;
64
+ authorization?: string | undefined;
65
+ 'cache-control'?: string | undefined;
66
+ connection?: string | undefined;
67
+ 'content-disposition'?: string | undefined;
68
+ 'content-encoding'?: string | undefined;
69
+ 'content-language'?: string | undefined;
70
+ 'content-length'?: string | undefined;
71
+ 'content-location'?: string | undefined;
72
+ 'content-range'?: string | undefined;
73
+ 'content-type'?: string | undefined;
74
+ cookie?: string | undefined;
75
+ date?: string | undefined;
76
+ etag?: string | undefined;
77
+ expect?: string | undefined;
78
+ expires?: string | undefined;
79
+ forwarded?: string | undefined;
80
+ from?: string | undefined;
81
+ host?: string | undefined;
82
+ 'if-match'?: string | undefined;
83
+ 'if-modified-since'?: string | undefined;
84
+ 'if-none-match'?: string | undefined;
85
+ 'if-unmodified-since'?: string | undefined;
86
+ 'last-modified'?: string | undefined;
87
+ location?: string | undefined;
88
+ origin?: string | undefined;
89
+ pragma?: string | undefined;
90
+ 'proxy-authenticate'?: string | undefined;
91
+ 'proxy-authorization'?: string | undefined;
92
+ 'public-key-pins'?: string | undefined;
93
+ range?: string | undefined;
94
+ referer?: string | undefined;
95
+ 'retry-after'?: string | undefined;
96
+ 'sec-websocket-accept'?: string | undefined;
97
+ 'sec-websocket-extensions'?: string | undefined;
98
+ 'sec-websocket-key'?: string | undefined;
99
+ 'sec-websocket-protocol'?: string | undefined;
100
+ 'sec-websocket-version'?: string | undefined;
101
+ 'set-cookie'?: string[] | undefined;
102
+ 'strict-transport-security'?: string | undefined;
103
+ tk?: string | undefined;
104
+ trailer?: string | undefined;
105
+ 'transfer-encoding'?: string | undefined;
106
+ upgrade?: string | undefined;
107
+ 'user-agent'?: string | undefined;
108
+ vary?: string | undefined;
109
+ via?: string | undefined;
110
+ warning?: string | undefined;
111
+ 'www-authenticate'?: string | undefined;
71
112
  }
72
-
73
113
  // outgoing headers allows numbers (as they are converted internally to strings)
74
114
  type OutgoingHttpHeader = number | string | string[];
75
-
76
- interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {
77
- }
78
-
115
+ interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {}
79
116
  interface ClientRequestArgs {
80
- abort?: AbortSignal;
81
- protocol?: string | null;
82
- host?: string | null;
83
- hostname?: string | null;
84
- family?: number;
85
- port?: number | string | null;
86
- defaultPort?: number | string;
87
- localAddress?: string;
88
- socketPath?: string;
89
- /**
90
- * @default 8192
91
- */
92
- maxHeaderSize?: number;
93
- method?: string;
94
- path?: string | null;
95
- headers?: OutgoingHttpHeaders;
96
- auth?: string | null;
97
- agent?: Agent | boolean;
98
- _defaultAgent?: Agent;
99
- timeout?: number;
100
- setHost?: boolean;
117
+ _defaultAgent?: Agent | undefined;
118
+ agent?: Agent | boolean | undefined;
119
+ auth?: string | null | undefined;
101
120
  // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278
102
- createConnection?: (options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket;
121
+ createConnection?:
122
+ | ((options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket)
123
+ | undefined;
124
+ defaultPort?: number | string | undefined;
125
+ family?: number | undefined;
126
+ headers?: OutgoingHttpHeaders | undefined;
127
+ hints?: LookupOptions['hints'];
128
+ host?: string | null | undefined;
129
+ hostname?: string | null | undefined;
130
+ insecureHTTPParser?: boolean | undefined;
131
+ localAddress?: string | undefined;
132
+ localPort?: number | undefined;
133
+ lookup?: LookupFunction | undefined;
134
+ /**
135
+ * @default 16384
136
+ */
137
+ maxHeaderSize?: number | undefined;
138
+ method?: string | undefined;
139
+ path?: string | null | undefined;
140
+ port?: number | string | null | undefined;
141
+ protocol?: string | null | undefined;
142
+ setHost?: boolean | undefined;
143
+ signal?: AbortSignal | undefined;
144
+ socketPath?: string | undefined;
145
+ timeout?: number | undefined;
146
+ uniqueHeaders?: Array<string | string[]> | undefined;
103
147
  }
104
-
105
- interface ServerOptions {
106
- IncomingMessage?: typeof IncomingMessage;
107
- ServerResponse?: typeof ServerResponse;
148
+ interface ServerOptions<
149
+ Request extends typeof IncomingMessage = typeof IncomingMessage,
150
+ Response extends typeof ServerResponse = typeof ServerResponse,
151
+ > {
152
+ /**
153
+ * Specifies the `IncomingMessage` class to be used. Useful for extending the original `IncomingMessage`.
154
+ */
155
+ IncomingMessage?: Request | undefined;
156
+ /**
157
+ * Specifies the `ServerResponse` class to be used. Useful for extending the original `ServerResponse`.
158
+ */
159
+ ServerResponse?: Response | undefined;
160
+ /**
161
+ * Sets the timeout value in milliseconds for receiving the entire request from the client.
162
+ * @see Server.requestTimeout for more information.
163
+ * @default 300000
164
+ * @since v18.0.0
165
+ */
166
+ requestTimeout?: number | undefined;
167
+ /**
168
+ * The number of milliseconds of inactivity a server needs to wait for additional incoming data,
169
+ * after it has finished writing the last response, before a socket will be destroyed.
170
+ * @see Server.keepAliveTimeout for more information.
171
+ * @default 5000
172
+ * @since v18.0.0
173
+ */
174
+ keepAliveTimeout?: number | undefined;
175
+ /**
176
+ * Sets the interval value in milliseconds to check for request and headers timeout in incomplete requests.
177
+ * @default 30000
178
+ */
179
+ connectionsCheckingInterval?: number | undefined;
180
+ /**
181
+ * Use an insecure HTTP parser that accepts invalid HTTP headers when `true`.
182
+ * Using the insecure parser should be avoided.
183
+ * See --insecure-http-parser for more information.
184
+ * @default false
185
+ */
186
+ insecureHTTPParser?: boolean | undefined;
108
187
  /**
109
188
  * Optionally overrides the value of
110
189
  * `--max-http-header-size` for requests received by this server, i.e.
111
190
  * the maximum length of request headers in bytes.
112
- * @default 8192
191
+ * @default 16384
192
+ * @since v13.3.0
113
193
  */
114
- maxHeaderSize?: number;
194
+ maxHeaderSize?: number | undefined;
115
195
  /**
116
- * Use an insecure HTTP parser that accepts invalid HTTP headers when true.
117
- * Using the insecure parser should be avoided.
118
- * See --insecure-http-parser for more information.
196
+ * If set to `true`, it disables the use of Nagle's algorithm immediately after a new incoming connection is received.
197
+ * @default true
198
+ * @since v16.5.0
199
+ */
200
+ noDelay?: boolean | undefined;
201
+ /**
202
+ * If set to `true`, it enables keep-alive functionality on the socket immediately after a new incoming connection is received,
203
+ * similarly on what is done in `socket.setKeepAlive([enable][, initialDelay])`.
119
204
  * @default false
205
+ * @since v16.5.0
120
206
  */
121
- insecureHTTPParser?: boolean;
207
+ keepAlive?: boolean | undefined;
208
+ /**
209
+ * If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket.
210
+ * @default 0
211
+ * @since v16.5.0
212
+ */
213
+ keepAliveInitialDelay?: number | undefined;
214
+ /**
215
+ * A list of response headers that should be sent only once.
216
+ * If the header's value is an array, the items will be joined using `; `.
217
+ */
218
+ uniqueHeaders?: Array<string | string[]> | undefined;
122
219
  }
123
-
124
- type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
125
-
126
- interface HttpBase {
220
+ type RequestListener<
221
+ Request extends typeof IncomingMessage = typeof IncomingMessage,
222
+ Response extends typeof ServerResponse = typeof ServerResponse,
223
+ > = (req: InstanceType<Request>, res: InstanceType<Response> & { req: InstanceType<Request> }) => void;
224
+ /**
225
+ * @since v0.1.17
226
+ */
227
+ class Server<
228
+ Request extends typeof IncomingMessage = typeof IncomingMessage,
229
+ Response extends typeof ServerResponse = typeof ServerResponse,
230
+ > extends NetServer {
231
+ constructor(requestListener?: RequestListener<Request, Response>);
232
+ constructor(options: ServerOptions<Request, Response>, requestListener?: RequestListener<Request, Response>);
233
+ /**
234
+ * Sets the timeout value for sockets, and emits a `'timeout'` event on
235
+ * the Server object, passing the socket as an argument, if a timeout
236
+ * occurs.
237
+ *
238
+ * If there is a `'timeout'` event listener on the Server object, then it
239
+ * will be called with the timed-out socket as an argument.
240
+ *
241
+ * By default, the Server does not timeout sockets. However, if a callback
242
+ * is assigned to the Server's `'timeout'` event, timeouts must be handled
243
+ * explicitly.
244
+ * @since v0.9.12
245
+ * @param [msecs=0 (no timeout)]
246
+ */
127
247
  setTimeout(msecs?: number, callback?: () => void): this;
128
248
  setTimeout(callback: () => void): this;
129
249
  /**
130
250
  * Limits maximum incoming headers count. If set to 0, no limit will be applied.
131
- * @default 2000
132
- * {@link https://nodejs.org/api/http.html#http_server_maxheaderscount}
251
+ * @since v0.7.0
133
252
  */
134
253
  maxHeadersCount: number | null;
254
+ /**
255
+ * The maximum number of requests socket can handle
256
+ * before closing keep alive connection.
257
+ *
258
+ * A value of `0` will disable the limit.
259
+ *
260
+ * When the limit is reached it will set the `Connection` header value to `close`,
261
+ * but will not actually close the connection, subsequent requests sent
262
+ * after the limit is reached will get `503 Service Unavailable` as a response.
263
+ * @since v16.10.0
264
+ */
265
+ maxRequestsPerSocket: number | null;
266
+ /**
267
+ * The number of milliseconds of inactivity before a socket is presumed
268
+ * to have timed out.
269
+ *
270
+ * A value of `0` will disable the timeout behavior on incoming connections.
271
+ *
272
+ * The socket timeout logic is set up on connection, so changing this
273
+ * value only affects new connections to the server, not any existing connections.
274
+ * @since v0.9.12
275
+ */
135
276
  timeout: number;
136
277
  /**
137
- * Limit the amount of time the parser will wait to receive the complete HTTP headers.
138
- * @default 60000
139
- * {@link https://nodejs.org/api/http.html#http_server_headerstimeout}
278
+ * Limit the amount of time the parser will wait to receive the complete HTTP
279
+ * headers.
280
+ *
281
+ * If the timeout expires, the server responds with status 408 without
282
+ * forwarding the request to the request listener and then closes the connection.
283
+ *
284
+ * It must be set to a non-zero value (e.g. 120 seconds) to protect against
285
+ * potential Denial-of-Service attacks in case the server is deployed without a
286
+ * reverse proxy in front.
287
+ * @since v11.3.0, v10.14.0
140
288
  */
141
289
  headersTimeout: number;
290
+ /**
291
+ * The number of milliseconds of inactivity a server needs to wait for additional
292
+ * incoming data, after it has finished writing the last response, before a socket
293
+ * will be destroyed. If the server receives new data before the keep-alive
294
+ * timeout has fired, it will reset the regular inactivity timeout, i.e.,`server.timeout`.
295
+ *
296
+ * A value of `0` will disable the keep-alive timeout behavior on incoming
297
+ * connections.
298
+ * A value of `0` makes the http server behave similarly to Node.js versions prior
299
+ * to 8.0.0, which did not have a keep-alive timeout.
300
+ *
301
+ * The socket timeout logic is set up on connection, so changing this value only
302
+ * affects new connections to the server, not any existing connections.
303
+ * @since v8.0.0
304
+ */
142
305
  keepAliveTimeout: number;
143
306
  /**
144
- * Sets the timeout value in milliseconds for receiving the entire request from the client.
145
- * @default 0
146
- * {@link https://nodejs.org/api/http.html#http_server_requesttimeout}
307
+ * Sets the timeout value in milliseconds for receiving the entire request from
308
+ * the client.
309
+ *
310
+ * If the timeout expires, the server responds with status 408 without
311
+ * forwarding the request to the request listener and then closes the connection.
312
+ *
313
+ * It must be set to a non-zero value (e.g. 120 seconds) to protect against
314
+ * potential Denial-of-Service attacks in case the server is deployed without a
315
+ * reverse proxy in front.
316
+ * @since v14.11.0
147
317
  */
148
318
  requestTimeout: number;
319
+ /**
320
+ * Closes all connections connected to this server.
321
+ * @since v18.2.0
322
+ */
323
+ closeAllConnections(): void;
324
+ /**
325
+ * Closes all connections connected to this server which are not sending a request or waiting for a response.
326
+ * @since v18.2.0
327
+ */
328
+ closeIdleConnections(): void;
329
+ addListener(event: string, listener: (...args: any[]) => void): this;
330
+ addListener(event: 'close', listener: () => void): this;
331
+ addListener(event: 'connection', listener: (socket: Socket) => void): this;
332
+ addListener(event: 'error', listener: (err: Error) => void): this;
333
+ addListener(event: 'listening', listener: () => void): this;
334
+ addListener(event: 'checkContinue', listener: RequestListener<Request, Response>): this;
335
+ addListener(event: 'checkExpectation', listener: RequestListener<Request, Response>): this;
336
+ addListener(event: 'clientError', listener: (err: Error, socket: stream.Duplex) => void): this;
337
+ addListener(
338
+ event: 'connect',
339
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
340
+ ): this;
341
+ addListener(event: 'request', listener: RequestListener<Request, Response>): this;
342
+ addListener(
343
+ event: 'upgrade',
344
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
345
+ ): this;
346
+ emit(event: string, ...args: any[]): boolean;
347
+ emit(event: 'close'): boolean;
348
+ emit(event: 'connection', socket: Socket): boolean;
349
+ emit(event: 'error', err: Error): boolean;
350
+ emit(event: 'listening'): boolean;
351
+ emit(
352
+ event: 'checkContinue',
353
+ req: InstanceType<Request>,
354
+ res: InstanceType<Response> & { req: InstanceType<Request> },
355
+ ): boolean;
356
+ emit(
357
+ event: 'checkExpectation',
358
+ req: InstanceType<Request>,
359
+ res: InstanceType<Response> & { req: InstanceType<Request> },
360
+ ): boolean;
361
+ emit(event: 'clientError', err: Error, socket: stream.Duplex): boolean;
362
+ emit(event: 'connect', req: InstanceType<Request>, socket: stream.Duplex, head: Buffer): boolean;
363
+ emit(
364
+ event: 'request',
365
+ req: InstanceType<Request>,
366
+ res: InstanceType<Response> & { req: InstanceType<Request> },
367
+ ): boolean;
368
+ emit(event: 'upgrade', req: InstanceType<Request>, socket: stream.Duplex, head: Buffer): boolean;
369
+ on(event: string, listener: (...args: any[]) => void): this;
370
+ on(event: 'close', listener: () => void): this;
371
+ on(event: 'connection', listener: (socket: Socket) => void): this;
372
+ on(event: 'error', listener: (err: Error) => void): this;
373
+ on(event: 'listening', listener: () => void): this;
374
+ on(event: 'checkContinue', listener: RequestListener<Request, Response>): this;
375
+ on(event: 'checkExpectation', listener: RequestListener<Request, Response>): this;
376
+ on(event: 'clientError', listener: (err: Error, socket: stream.Duplex) => void): this;
377
+ on(event: 'connect', listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void): this;
378
+ on(event: 'request', listener: RequestListener<Request, Response>): this;
379
+ on(event: 'upgrade', listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void): this;
380
+ once(event: string, listener: (...args: any[]) => void): this;
381
+ once(event: 'close', listener: () => void): this;
382
+ once(event: 'connection', listener: (socket: Socket) => void): this;
383
+ once(event: 'error', listener: (err: Error) => void): this;
384
+ once(event: 'listening', listener: () => void): this;
385
+ once(event: 'checkContinue', listener: RequestListener<Request, Response>): this;
386
+ once(event: 'checkExpectation', listener: RequestListener<Request, Response>): this;
387
+ once(event: 'clientError', listener: (err: Error, socket: stream.Duplex) => void): this;
388
+ once(
389
+ event: 'connect',
390
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
391
+ ): this;
392
+ once(event: 'request', listener: RequestListener<Request, Response>): this;
393
+ once(
394
+ event: 'upgrade',
395
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
396
+ ): this;
397
+ prependListener(event: string, listener: (...args: any[]) => void): this;
398
+ prependListener(event: 'close', listener: () => void): this;
399
+ prependListener(event: 'connection', listener: (socket: Socket) => void): this;
400
+ prependListener(event: 'error', listener: (err: Error) => void): this;
401
+ prependListener(event: 'listening', listener: () => void): this;
402
+ prependListener(event: 'checkContinue', listener: RequestListener<Request, Response>): this;
403
+ prependListener(event: 'checkExpectation', listener: RequestListener<Request, Response>): this;
404
+ prependListener(event: 'clientError', listener: (err: Error, socket: stream.Duplex) => void): this;
405
+ prependListener(
406
+ event: 'connect',
407
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
408
+ ): this;
409
+ prependListener(event: 'request', listener: RequestListener<Request, Response>): this;
410
+ prependListener(
411
+ event: 'upgrade',
412
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
413
+ ): this;
414
+ prependOnceListener(event: string, listener: (...args: any[]) => void): this;
415
+ prependOnceListener(event: 'close', listener: () => void): this;
416
+ prependOnceListener(event: 'connection', listener: (socket: Socket) => void): this;
417
+ prependOnceListener(event: 'error', listener: (err: Error) => void): this;
418
+ prependOnceListener(event: 'listening', listener: () => void): this;
419
+ prependOnceListener(event: 'checkContinue', listener: RequestListener<Request, Response>): this;
420
+ prependOnceListener(event: 'checkExpectation', listener: RequestListener<Request, Response>): this;
421
+ prependOnceListener(event: 'clientError', listener: (err: Error, socket: stream.Duplex) => void): this;
422
+ prependOnceListener(
423
+ event: 'connect',
424
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
425
+ ): this;
426
+ prependOnceListener(event: 'request', listener: RequestListener<Request, Response>): this;
427
+ prependOnceListener(
428
+ event: 'upgrade',
429
+ listener: (req: InstanceType<Request>, socket: stream.Duplex, head: Buffer) => void,
430
+ ): this;
149
431
  }
150
-
151
- interface Server extends HttpBase {}
152
- class Server extends NetServer {
153
- constructor(requestListener?: RequestListener);
154
- constructor(options: ServerOptions, requestListener?: RequestListener);
155
- }
156
-
157
- // https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
158
- class OutgoingMessage extends stream.Writable {
159
- readonly req: IncomingMessage;
160
-
432
+ /**
433
+ * This class serves as the parent class of {@link ClientRequest} and {@link ServerResponse}. It is an abstract of outgoing message from
434
+ * the perspective of the participants of HTTP transaction.
435
+ * @since v0.1.17
436
+ */
437
+ class OutgoingMessage<Request extends IncomingMessage = IncomingMessage> extends stream.Writable {
438
+ readonly req: Request;
161
439
  chunkedEncoding: boolean;
162
440
  shouldKeepAlive: boolean;
163
441
  useChunkedEncodingByDefault: boolean;
@@ -166,43 +444,279 @@ declare module 'http' {
166
444
  * @deprecated Use `writableEnded` instead.
167
445
  */
168
446
  finished: boolean;
447
+ /**
448
+ * Read-only. `true` if the headers were sent, otherwise `false`.
449
+ * @since v0.9.3
450
+ */
169
451
  readonly headersSent: boolean;
170
452
  /**
171
- * @deprecated Use `socket` instead.
453
+ * Aliases of `outgoingMessage.socket`
454
+ * @since v0.3.0
455
+ * @deprecated Since v15.12.0,v14.17.1 - Use `socket` instead.
172
456
  */
173
457
  readonly connection: Socket | null;
458
+ /**
459
+ * Reference to the underlying socket. Usually, users will not want to access
460
+ * this property.
461
+ *
462
+ * After calling `outgoingMessage.end()`, this property will be nulled.
463
+ * @since v0.3.0
464
+ */
174
465
  readonly socket: Socket | null;
175
-
176
466
  constructor();
177
-
467
+ /**
468
+ * Once a socket is associated with the message and is connected,`socket.setTimeout()` will be called with `msecs` as the first parameter.
469
+ * @since v0.9.12
470
+ * @param callback Optional function to be called when a timeout occurs. Same as binding to the `timeout` event.
471
+ */
178
472
  setTimeout(msecs: number, callback?: () => void): this;
473
+ /**
474
+ * Sets a single header value for the header object.
475
+ * @since v0.4.0
476
+ * @param name Header name
477
+ * @param value Header value
478
+ */
179
479
  setHeader(name: string, value: number | string | ReadonlyArray<string>): this;
480
+ /**
481
+ * Gets the value of HTTP header with the given name. If such a name doesn't
482
+ * exist in message, it will be `undefined`.
483
+ * @since v0.4.0
484
+ * @param name Name of header
485
+ */
180
486
  getHeader(name: string): number | string | string[] | undefined;
487
+ /**
488
+ * Returns a shallow copy of the current outgoing headers. Since a shallow
489
+ * copy is used, array values may be mutated without additional calls to
490
+ * various header-related HTTP module methods. The keys of the returned
491
+ * object are the header names and the values are the respective header
492
+ * values. All header names are lowercase.
493
+ *
494
+ * The object returned by the `outgoingMessage.getHeaders()` method does
495
+ * not prototypically inherit from the JavaScript Object. This means that
496
+ * typical Object methods such as `obj.toString()`, `obj.hasOwnProperty()`,
497
+ * and others are not defined and will not work.
498
+ *
499
+ * ```js
500
+ * outgoingMessage.setHeader('Foo', 'bar');
501
+ * outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
502
+ *
503
+ * const headers = outgoingMessage.getHeaders();
504
+ * // headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
505
+ * ```
506
+ * @since v7.7.0
507
+ */
181
508
  getHeaders(): OutgoingHttpHeaders;
509
+ /**
510
+ * Returns an array of names of headers of the outgoing outgoingMessage. All
511
+ * names are lowercase.
512
+ * @since v7.7.0
513
+ */
182
514
  getHeaderNames(): string[];
515
+ /**
516
+ * Returns `true` if the header identified by `name` is currently set in the
517
+ * outgoing headers. The header name is case-insensitive.
518
+ *
519
+ * ```js
520
+ * const hasContentType = outgoingMessage.hasHeader('content-type');
521
+ * ```
522
+ * @since v7.7.0
523
+ */
183
524
  hasHeader(name: string): boolean;
525
+ /**
526
+ * Removes a header that is queued for implicit sending.
527
+ *
528
+ * ```js
529
+ * outgoingMessage.removeHeader('Content-Encoding');
530
+ * ```
531
+ * @since v0.4.0
532
+ * @param name Header name
533
+ */
184
534
  removeHeader(name: string): void;
535
+ /**
536
+ * Adds HTTP trailers (headers but at the end of the message) to the message.
537
+ *
538
+ * Trailers are **only** be emitted if the message is chunked encoded. If not,
539
+ * the trailer will be silently discarded.
540
+ *
541
+ * HTTP requires the `Trailer` header to be sent to emit trailers,
542
+ * with a list of header fields in its value, e.g.
543
+ *
544
+ * ```js
545
+ * message.writeHead(200, { 'Content-Type': 'text/plain',
546
+ * 'Trailer': 'Content-MD5' });
547
+ * message.write(fileData);
548
+ * message.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
549
+ * message.end();
550
+ * ```
551
+ *
552
+ * Attempting to set a header field name or value that contains invalid characters
553
+ * will result in a `TypeError` being thrown.
554
+ * @since v0.3.0
555
+ */
185
556
  addTrailers(headers: OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void;
557
+ /**
558
+ * Compulsorily flushes the message headers
559
+ *
560
+ * For efficiency reason, Node.js normally buffers the message headers
561
+ * until `outgoingMessage.end()` is called or the first chunk of message data
562
+ * is written. It then tries to pack the headers and data into a single TCP
563
+ * packet.
564
+ *
565
+ * It is usually desired (it saves a TCP round-trip), but not when the first
566
+ * data is not sent until possibly much later. `outgoingMessage.flushHeaders()`bypasses the optimization and kickstarts the request.
567
+ * @since v1.6.0
568
+ */
186
569
  flushHeaders(): void;
187
570
  }
188
-
189
- // https://github.com/nodejs/node/blob/master/lib/_http_server.js#L108-L256
190
- class ServerResponse extends OutgoingMessage {
571
+ /**
572
+ * This object is created internally by an HTTP server, not by the user. It is
573
+ * passed as the second parameter to the `'request'` event.
574
+ * @since v0.1.17
575
+ */
576
+ class ServerResponse<Request extends IncomingMessage = IncomingMessage> extends OutgoingMessage<Request> {
577
+ /**
578
+ * When using implicit headers (not calling `response.writeHead()` explicitly),
579
+ * this property controls the status code that will be sent to the client when
580
+ * the headers get flushed.
581
+ *
582
+ * ```js
583
+ * response.statusCode = 404;
584
+ * ```
585
+ *
586
+ * After response header was sent to the client, this property indicates the
587
+ * status code which was sent out.
588
+ * @since v0.4.0
589
+ */
191
590
  statusCode: number;
591
+ /**
592
+ * When using implicit headers (not calling `response.writeHead()` explicitly),
593
+ * this property controls the status message that will be sent to the client when
594
+ * the headers get flushed. If this is left as `undefined` then the standard
595
+ * message for the status code will be used.
596
+ *
597
+ * ```js
598
+ * response.statusMessage = 'Not found';
599
+ * ```
600
+ *
601
+ * After response header was sent to the client, this property indicates the
602
+ * status message which was sent out.
603
+ * @since v0.11.8
604
+ */
192
605
  statusMessage: string;
193
-
194
- constructor(req: IncomingMessage);
195
-
606
+ constructor(req: Request);
196
607
  assignSocket(socket: Socket): void;
197
608
  detachSocket(socket: Socket): void;
198
- // https://github.com/nodejs/node/blob/master/test/parallel/test-http-write-callbacks.js#L53
199
- // no args in writeContinue callback
609
+ /**
610
+ * Sends an HTTP/1.1 100 Continue message to the client, indicating that
611
+ * the request body should be sent. See the `'checkContinue'` event on`Server`.
612
+ * @since v0.3.0
613
+ */
200
614
  writeContinue(callback?: () => void): void;
201
- writeHead(statusCode: number, reasonPhrase?: string, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
615
+ /**
616
+ * Sends an HTTP/1.1 103 Early Hints message to the client with a Link header,
617
+ * indicating that the user agent can preload/preconnect the linked resources.
618
+ * The `hints` is an object containing the values of headers to be sent with
619
+ * early hints message. The optional `callback` argument will be called when
620
+ * the response message has been written.
621
+ *
622
+ * Example:
623
+ *
624
+ * ```js
625
+ * const earlyHintsLink = '</styles.css>; rel=preload; as=style';
626
+ * response.writeEarlyHints({
627
+ * 'link': earlyHintsLink,
628
+ * });
629
+ *
630
+ * const earlyHintsLinks = [
631
+ * '</styles.css>; rel=preload; as=style',
632
+ * '</scripts.js>; rel=preload; as=script',
633
+ * ];
634
+ * response.writeEarlyHints({
635
+ * 'link': earlyHintsLinks,
636
+ * 'x-trace-id': 'id for diagnostics'
637
+ * });
638
+ *
639
+ * const earlyHintsCallback = () => console.log('early hints message sent');
640
+ * response.writeEarlyHints({
641
+ * 'link': earlyHintsLinks
642
+ * }, earlyHintsCallback);
643
+ * ```
644
+ *
645
+ * @since v18.11.0
646
+ * @param hints An object containing the values of headers
647
+ * @param callback Will be called when the response message has been written
648
+ */
649
+ writeEarlyHints(hints: Record<string, string | string[]>, callback?: () => void): void;
650
+ /**
651
+ * Sends a response header to the request. The status code is a 3-digit HTTP
652
+ * status code, like `404`. The last argument, `headers`, are the response headers.
653
+ * Optionally one can give a human-readable `statusMessage` as the second
654
+ * argument.
655
+ *
656
+ * `headers` may be an `Array` where the keys and values are in the same list.
657
+ * It is _not_ a list of tuples. So, the even-numbered offsets are key values,
658
+ * and the odd-numbered offsets are the associated values. The array is in the same
659
+ * format as `request.rawHeaders`.
660
+ *
661
+ * Returns a reference to the `ServerResponse`, so that calls can be chained.
662
+ *
663
+ * ```js
664
+ * const body = 'hello world';
665
+ * response
666
+ * .writeHead(200, {
667
+ * 'Content-Length': Buffer.byteLength(body),
668
+ * 'Content-Type': 'text/plain'
669
+ * })
670
+ * .end(body);
671
+ * ```
672
+ *
673
+ * This method must only be called once on a message and it must
674
+ * be called before `response.end()` is called.
675
+ *
676
+ * If `response.write()` or `response.end()` are called before calling
677
+ * this, the implicit/mutable headers will be calculated and call this function.
678
+ *
679
+ * When headers have been set with `response.setHeader()`, they will be merged
680
+ * with any headers passed to `response.writeHead()`, with the headers passed
681
+ * to `response.writeHead()` given precedence.
682
+ *
683
+ * If this method is called and `response.setHeader()` has not been called,
684
+ * it will directly write the supplied header values onto the network channel
685
+ * without caching internally, and the `response.getHeader()` on the header
686
+ * will not yield the expected result. If progressive population of headers is
687
+ * desired with potential future retrieval and modification, use `response.setHeader()` instead.
688
+ *
689
+ * ```js
690
+ * // Returns content-type = text/plain
691
+ * const server = http.createServer((req, res) => {
692
+ * res.setHeader('Content-Type', 'text/html');
693
+ * res.setHeader('X-Foo', 'bar');
694
+ * res.writeHead(200, { 'Content-Type': 'text/plain' });
695
+ * res.end('ok');
696
+ * });
697
+ * ```
698
+ *
699
+ * `Content-Length` is given in bytes, not characters. Use `Buffer.byteLength()` to determine the length of the body in bytes. Node.js
700
+ * does not check whether `Content-Length` and the length of the body which has
701
+ * been transmitted are equal or not.
702
+ *
703
+ * Attempting to set a header field name or value that contains invalid characters
704
+ * will result in a `TypeError` being thrown.
705
+ * @since v0.1.30
706
+ */
707
+ writeHead(
708
+ statusCode: number,
709
+ statusMessage?: string,
710
+ headers?: OutgoingHttpHeaders | OutgoingHttpHeader[],
711
+ ): this;
202
712
  writeHead(statusCode: number, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
713
+ /**
714
+ * Sends an HTTP/1.1 102 Processing message to the client, indicating that
715
+ * the request body should be sent.
716
+ * @since v10.0.0
717
+ */
203
718
  writeProcessing(): void;
204
719
  }
205
-
206
720
  interface InformationEvent {
207
721
  statusCode: number;
208
722
  statusMessage: string;
@@ -212,32 +726,174 @@ declare module 'http' {
212
726
  headers: IncomingHttpHeaders;
213
727
  rawHeaders: string[];
214
728
  }
215
-
216
- // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
729
+ /**
730
+ * This object is created internally and returned from {@link request}. It
731
+ * represents an _in-progress_ request whose header has already been queued. The
732
+ * header is still mutable using the `setHeader(name, value)`,`getHeader(name)`, `removeHeader(name)` API. The actual header will
733
+ * be sent along with the first data chunk or when calling `request.end()`.
734
+ *
735
+ * To get the response, add a listener for `'response'` to the request object.`'response'` will be emitted from the request object when the response
736
+ * headers have been received. The `'response'` event is executed with one
737
+ * argument which is an instance of {@link IncomingMessage}.
738
+ *
739
+ * During the `'response'` event, one can add listeners to the
740
+ * response object; particularly to listen for the `'data'` event.
741
+ *
742
+ * If no `'response'` handler is added, then the response will be
743
+ * entirely discarded. However, if a `'response'` event handler is added,
744
+ * then the data from the response object **must** be consumed, either by
745
+ * calling `response.read()` whenever there is a `'readable'` event, or
746
+ * by adding a `'data'` handler, or by calling the `.resume()` method.
747
+ * Until the data is consumed, the `'end'` event will not fire. Also, until
748
+ * the data is read it will consume memory that can eventually lead to a
749
+ * 'process out of memory' error.
750
+ *
751
+ * For backward compatibility, `res` will only emit `'error'` if there is an`'error'` listener registered.
752
+ *
753
+ * Node.js does not check whether Content-Length and the length of the
754
+ * body which has been transmitted are equal or not.
755
+ * @since v0.1.17
756
+ */
217
757
  class ClientRequest extends OutgoingMessage {
758
+ /**
759
+ * The `request.aborted` property will be `true` if the request has
760
+ * been aborted.
761
+ * @since v0.11.14
762
+ * @deprecated Since v17.0.0,v16.12.0 - Check `destroyed` instead.
763
+ */
218
764
  aborted: boolean;
765
+ /**
766
+ * The request host.
767
+ * @since v14.5.0, v12.19.0
768
+ */
219
769
  host: string;
770
+ /**
771
+ * The request protocol.
772
+ * @since v14.5.0, v12.19.0
773
+ */
220
774
  protocol: string;
221
-
775
+ /**
776
+ * When sending request through a keep-alive enabled agent, the underlying socket
777
+ * might be reused. But if server closes connection at unfortunate time, client
778
+ * may run into a 'ECONNRESET' error.
779
+ *
780
+ * ```js
781
+ * const http = require('http');
782
+ *
783
+ * // Server has a 5 seconds keep-alive timeout by default
784
+ * http
785
+ * .createServer((req, res) => {
786
+ * res.write('hello\n');
787
+ * res.end();
788
+ * })
789
+ * .listen(3000);
790
+ *
791
+ * setInterval(() => {
792
+ * // Adapting a keep-alive agent
793
+ * http.get('http://localhost:3000', { agent }, (res) => {
794
+ * res.on('data', (data) => {
795
+ * // Do nothing
796
+ * });
797
+ * });
798
+ * }, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
799
+ * ```
800
+ *
801
+ * By marking a request whether it reused socket or not, we can do
802
+ * automatic error retry base on it.
803
+ *
804
+ * ```js
805
+ * const http = require('http');
806
+ * const agent = new http.Agent({ keepAlive: true });
807
+ *
808
+ * function retriableRequest() {
809
+ * const req = http
810
+ * .get('http://localhost:3000', { agent }, (res) => {
811
+ * // ...
812
+ * })
813
+ * .on('error', (err) => {
814
+ * // Check if retry is needed
815
+ * if (req.reusedSocket &#x26;&#x26; err.code === 'ECONNRESET') {
816
+ * retriableRequest();
817
+ * }
818
+ * });
819
+ * }
820
+ *
821
+ * retriableRequest();
822
+ * ```
823
+ * @since v13.0.0, v12.16.0
824
+ */
825
+ reusedSocket: boolean;
826
+ /**
827
+ * Limits maximum response headers count. If set to 0, no limit will be applied.
828
+ */
829
+ maxHeadersCount: number;
222
830
  constructor(url: string | URL | ClientRequestArgs, cb?: (res: IncomingMessage) => void);
223
-
831
+ /**
832
+ * The request method.
833
+ * @since v0.1.97
834
+ */
224
835
  method: string;
836
+ /**
837
+ * The request path.
838
+ * @since v0.4.0
839
+ */
225
840
  path: string;
226
- /** @deprecated since v14.1.0 Use `request.destroy()` instead. */
841
+ /**
842
+ * Marks the request as aborting. Calling this will cause remaining data
843
+ * in the response to be dropped and the socket to be destroyed.
844
+ * @since v0.3.8
845
+ * @deprecated Since v14.1.0,v13.14.0 - Use `destroy` instead.
846
+ */
227
847
  abort(): void;
228
848
  onSocket(socket: Socket): void;
849
+ /**
850
+ * Once a socket is assigned to this request and is connected `socket.setTimeout()` will be called.
851
+ * @since v0.5.9
852
+ * @param timeout Milliseconds before a request times out.
853
+ * @param callback Optional function to be called when a timeout occurs. Same as binding to the `'timeout'` event.
854
+ */
229
855
  setTimeout(timeout: number, callback?: () => void): this;
856
+ /**
857
+ * Once a socket is assigned to this request and is connected `socket.setNoDelay()` will be called.
858
+ * @since v0.5.9
859
+ */
230
860
  setNoDelay(noDelay?: boolean): void;
861
+ /**
862
+ * Once a socket is assigned to this request and is connected `socket.setKeepAlive()` will be called.
863
+ * @since v0.5.9
864
+ */
231
865
  setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
232
-
866
+ /**
867
+ * Returns an array containing the unique names of the current outgoing raw
868
+ * headers. Header names are returned with their exact casing being set.
869
+ *
870
+ * ```js
871
+ * request.setHeader('Foo', 'bar');
872
+ * request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
873
+ *
874
+ * const headerNames = request.getRawHeaderNames();
875
+ * // headerNames === ['Foo', 'Set-Cookie']
876
+ * ```
877
+ * @since v15.13.0, v14.17.0
878
+ */
879
+ getRawHeaderNames(): string[];
880
+ /**
881
+ * @deprecated
882
+ */
233
883
  addListener(event: 'abort', listener: () => void): this;
234
- addListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
884
+ addListener(
885
+ event: 'connect',
886
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
887
+ ): this;
235
888
  addListener(event: 'continue', listener: () => void): this;
236
889
  addListener(event: 'information', listener: (info: InformationEvent) => void): this;
237
890
  addListener(event: 'response', listener: (response: IncomingMessage) => void): this;
238
891
  addListener(event: 'socket', listener: (socket: Socket) => void): this;
239
892
  addListener(event: 'timeout', listener: () => void): this;
240
- addListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
893
+ addListener(
894
+ event: 'upgrade',
895
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
896
+ ): this;
241
897
  addListener(event: 'close', listener: () => void): this;
242
898
  addListener(event: 'drain', listener: () => void): this;
243
899
  addListener(event: 'error', listener: (err: Error) => void): this;
@@ -245,7 +901,9 @@ declare module 'http' {
245
901
  addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
246
902
  addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
247
903
  addListener(event: string | symbol, listener: (...args: any[]) => void): this;
248
-
904
+ /**
905
+ * @deprecated
906
+ */
249
907
  on(event: 'abort', listener: () => void): this;
250
908
  on(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
251
909
  on(event: 'continue', listener: () => void): this;
@@ -261,7 +919,9 @@ declare module 'http' {
261
919
  on(event: 'pipe', listener: (src: stream.Readable) => void): this;
262
920
  on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
263
921
  on(event: string | symbol, listener: (...args: any[]) => void): this;
264
-
922
+ /**
923
+ * @deprecated
924
+ */
265
925
  once(event: 'abort', listener: () => void): this;
266
926
  once(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
267
927
  once(event: 'continue', listener: () => void): this;
@@ -277,15 +937,23 @@ declare module 'http' {
277
937
  once(event: 'pipe', listener: (src: stream.Readable) => void): this;
278
938
  once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
279
939
  once(event: string | symbol, listener: (...args: any[]) => void): this;
280
-
940
+ /**
941
+ * @deprecated
942
+ */
281
943
  prependListener(event: 'abort', listener: () => void): this;
282
- prependListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
944
+ prependListener(
945
+ event: 'connect',
946
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
947
+ ): this;
283
948
  prependListener(event: 'continue', listener: () => void): this;
284
949
  prependListener(event: 'information', listener: (info: InformationEvent) => void): this;
285
950
  prependListener(event: 'response', listener: (response: IncomingMessage) => void): this;
286
951
  prependListener(event: 'socket', listener: (socket: Socket) => void): this;
287
952
  prependListener(event: 'timeout', listener: () => void): this;
288
- prependListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
953
+ prependListener(
954
+ event: 'upgrade',
955
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
956
+ ): this;
289
957
  prependListener(event: 'close', listener: () => void): this;
290
958
  prependListener(event: 'drain', listener: () => void): this;
291
959
  prependListener(event: 'error', listener: (err: Error) => void): this;
@@ -293,15 +961,23 @@ declare module 'http' {
293
961
  prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
294
962
  prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
295
963
  prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
296
-
964
+ /**
965
+ * @deprecated
966
+ */
297
967
  prependOnceListener(event: 'abort', listener: () => void): this;
298
- prependOnceListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
968
+ prependOnceListener(
969
+ event: 'connect',
970
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
971
+ ): this;
299
972
  prependOnceListener(event: 'continue', listener: () => void): this;
300
973
  prependOnceListener(event: 'information', listener: (info: InformationEvent) => void): this;
301
974
  prependOnceListener(event: 'response', listener: (response: IncomingMessage) => void): this;
302
975
  prependOnceListener(event: 'socket', listener: (socket: Socket) => void): this;
303
976
  prependOnceListener(event: 'timeout', listener: () => void): this;
304
- prependOnceListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
977
+ prependOnceListener(
978
+ event: 'upgrade',
979
+ listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void,
980
+ ): this;
305
981
  prependOnceListener(event: 'close', listener: () => void): this;
306
982
  prependOnceListener(event: 'drain', listener: () => void): this;
307
983
  prependOnceListener(event: 'error', listener: (err: Error) => void): this;
@@ -310,125 +986,666 @@ declare module 'http' {
310
986
  prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
311
987
  prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
312
988
  }
313
-
989
+ /**
990
+ * An `IncomingMessage` object is created by {@link Server} or {@link ClientRequest} and passed as the first argument to the `'request'` and `'response'` event respectively. It may be used to
991
+ * access response
992
+ * status, headers and data.
993
+ *
994
+ * Different from its `socket` value which is a subclass of `stream.Duplex`, the`IncomingMessage` itself extends `stream.Readable` and is created separately to
995
+ * parse and emit the incoming HTTP headers and payload, as the underlying socket
996
+ * may be reused multiple times in case of keep-alive.
997
+ * @since v0.1.17
998
+ */
314
999
  class IncomingMessage extends stream.Readable {
315
1000
  constructor(socket: Socket);
316
-
1001
+ /**
1002
+ * The `message.aborted` property will be `true` if the request has
1003
+ * been aborted.
1004
+ * @since v10.1.0
1005
+ * @deprecated Since v17.0.0,v16.12.0 - Check `message.destroyed` from <a href="stream.html#class-streamreadable" class="type">stream.Readable</a>.
1006
+ */
317
1007
  aborted: boolean;
1008
+ /**
1009
+ * In case of server request, the HTTP version sent by the client. In the case of
1010
+ * client response, the HTTP version of the connected-to server.
1011
+ * Probably either `'1.1'` or `'1.0'`.
1012
+ *
1013
+ * Also `message.httpVersionMajor` is the first integer and`message.httpVersionMinor` is the second.
1014
+ * @since v0.1.1
1015
+ */
318
1016
  httpVersion: string;
319
1017
  httpVersionMajor: number;
320
1018
  httpVersionMinor: number;
1019
+ /**
1020
+ * The `message.complete` property will be `true` if a complete HTTP message has
1021
+ * been received and successfully parsed.
1022
+ *
1023
+ * This property is particularly useful as a means of determining if a client or
1024
+ * server fully transmitted a message before a connection was terminated:
1025
+ *
1026
+ * ```js
1027
+ * const req = http.request({
1028
+ * host: '127.0.0.1',
1029
+ * port: 8080,
1030
+ * method: 'POST'
1031
+ * }, (res) => {
1032
+ * res.resume();
1033
+ * res.on('end', () => {
1034
+ * if (!res.complete)
1035
+ * console.error(
1036
+ * 'The connection was terminated while the message was still being sent');
1037
+ * });
1038
+ * });
1039
+ * ```
1040
+ * @since v0.3.0
1041
+ */
321
1042
  complete: boolean;
322
1043
  /**
323
- * @deprecated since v13.0.0 - Use `socket` instead.
1044
+ * Alias for `message.socket`.
1045
+ * @since v0.1.90
1046
+ * @deprecated Since v16.0.0 - Use `socket`.
324
1047
  */
325
1048
  connection: Socket;
1049
+ /**
1050
+ * The `net.Socket` object associated with the connection.
1051
+ *
1052
+ * With HTTPS support, use `request.socket.getPeerCertificate()` to obtain the
1053
+ * client's authentication details.
1054
+ *
1055
+ * This property is guaranteed to be an instance of the `net.Socket` class,
1056
+ * a subclass of `stream.Duplex`, unless the user specified a socket
1057
+ * type other than `net.Socket` or internally nulled.
1058
+ * @since v0.3.0
1059
+ */
326
1060
  socket: Socket;
1061
+ /**
1062
+ * The request/response headers object.
1063
+ *
1064
+ * Key-value pairs of header names and values. Header names are lower-cased.
1065
+ *
1066
+ * ```js
1067
+ * // Prints something like:
1068
+ * //
1069
+ * // { 'user-agent': 'curl/7.22.0',
1070
+ * // host: '127.0.0.1:8000',
1071
+ * // accept: '*' }
1072
+ * console.log(request.getHeaders());
1073
+ * ```
1074
+ *
1075
+ * Duplicates in raw headers are handled in the following ways, depending on the
1076
+ * header name:
1077
+ *
1078
+ * * Duplicates of `age`, `authorization`, `content-length`, `content-type`,`etag`, `expires`, `from`, `host`, `if-modified-since`, `if-unmodified-since`,`last-modified`, `location`,
1079
+ * `max-forwards`, `proxy-authorization`, `referer`,`retry-after`, `server`, or `user-agent` are discarded.
1080
+ * * `set-cookie` is always an array. Duplicates are added to the array.
1081
+ * * For duplicate `cookie` headers, the values are joined together with '; '.
1082
+ * * For all other headers, the values are joined together with ', '.
1083
+ * @since v0.1.5
1084
+ */
327
1085
  headers: IncomingHttpHeaders;
1086
+ /**
1087
+ * The raw request/response headers list exactly as they were received.
1088
+ *
1089
+ * The keys and values are in the same list. It is _not_ a
1090
+ * list of tuples. So, the even-numbered offsets are key values, and the
1091
+ * odd-numbered offsets are the associated values.
1092
+ *
1093
+ * Header names are not lowercased, and duplicates are not merged.
1094
+ *
1095
+ * ```js
1096
+ * // Prints something like:
1097
+ * //
1098
+ * // [ 'user-agent',
1099
+ * // 'this is invalid because there can be only one',
1100
+ * // 'User-Agent',
1101
+ * // 'curl/7.22.0',
1102
+ * // 'Host',
1103
+ * // '127.0.0.1:8000',
1104
+ * // 'ACCEPT',
1105
+ * // '*' ]
1106
+ * console.log(request.rawHeaders);
1107
+ * ```
1108
+ * @since v0.11.6
1109
+ */
328
1110
  rawHeaders: string[];
1111
+ /**
1112
+ * The request/response trailers object. Only populated at the `'end'` event.
1113
+ * @since v0.3.0
1114
+ */
329
1115
  trailers: NodeJS.Dict<string>;
1116
+ /**
1117
+ * The raw request/response trailer keys and values exactly as they were
1118
+ * received. Only populated at the `'end'` event.
1119
+ * @since v0.11.6
1120
+ */
330
1121
  rawTrailers: string[];
1122
+ /**
1123
+ * Calls `message.socket.setTimeout(msecs, callback)`.
1124
+ * @since v0.5.9
1125
+ */
331
1126
  setTimeout(msecs: number, callback?: () => void): this;
332
1127
  /**
333
- * Only valid for request obtained from http.Server.
1128
+ * **Only valid for request obtained from {@link Server}.**
1129
+ *
1130
+ * The request method as a string. Read only. Examples: `'GET'`, `'DELETE'`.
1131
+ * @since v0.1.1
334
1132
  */
335
- method?: string;
1133
+ method?: string | undefined;
336
1134
  /**
337
- * Only valid for request obtained from http.Server.
1135
+ * **Only valid for request obtained from {@link Server}.**
1136
+ *
1137
+ * Request URL string. This contains only the URL that is present in the actual
1138
+ * HTTP request. Take the following request:
1139
+ *
1140
+ * ```http
1141
+ * GET /status?name=ryan HTTP/1.1
1142
+ * Accept: text/plain
1143
+ * ```
1144
+ *
1145
+ * To parse the URL into its parts:
1146
+ *
1147
+ * ```js
1148
+ * new URL(request.url, `http://${request.getHeaders().host}`);
1149
+ * ```
1150
+ *
1151
+ * When `request.url` is `'/status?name=ryan'` and`request.getHeaders().host` is `'localhost:3000'`:
1152
+ *
1153
+ * ```console
1154
+ * $ node
1155
+ * > new URL(request.url, `http://${request.getHeaders().host}`)
1156
+ * URL {
1157
+ * href: 'http://localhost:3000/status?name=ryan',
1158
+ * origin: 'http://localhost:3000',
1159
+ * protocol: 'http:',
1160
+ * username: '',
1161
+ * password: '',
1162
+ * host: 'localhost:3000',
1163
+ * hostname: 'localhost',
1164
+ * port: '3000',
1165
+ * pathname: '/status',
1166
+ * search: '?name=ryan',
1167
+ * searchParams: URLSearchParams { 'name' => 'ryan' },
1168
+ * hash: ''
1169
+ * }
1170
+ * ```
1171
+ * @since v0.1.90
338
1172
  */
339
- url?: string;
1173
+ url?: string | undefined;
340
1174
  /**
341
- * Only valid for response obtained from http.ClientRequest.
1175
+ * **Only valid for response obtained from {@link ClientRequest}.**
1176
+ *
1177
+ * The 3-digit HTTP response status code. E.G. `404`.
1178
+ * @since v0.1.1
342
1179
  */
343
- statusCode?: number;
1180
+ statusCode?: number | undefined;
344
1181
  /**
345
- * Only valid for response obtained from http.ClientRequest.
1182
+ * **Only valid for response obtained from {@link ClientRequest}.**
1183
+ *
1184
+ * The HTTP response status message (reason phrase). E.G. `OK` or `Internal Server Error`.
1185
+ * @since v0.11.10
346
1186
  */
347
- statusMessage?: string;
348
- destroy(error?: Error): void;
1187
+ statusMessage?: string | undefined;
1188
+ /**
1189
+ * Calls `destroy()` on the socket that received the `IncomingMessage`. If `error`is provided, an `'error'` event is emitted on the socket and `error` is passed
1190
+ * as an argument to any listeners on the event.
1191
+ * @since v0.3.0
1192
+ */
1193
+ destroy(error?: Error): this;
349
1194
  }
350
-
351
- interface AgentOptions {
1195
+ interface AgentOptions extends Partial<TcpSocketConnectOpts> {
352
1196
  /**
353
1197
  * Keep sockets around in a pool to be used by other requests in the future. Default = false
354
1198
  */
355
- keepAlive?: boolean;
1199
+ keepAlive?: boolean | undefined;
356
1200
  /**
357
1201
  * When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
358
1202
  * Only relevant if keepAlive is set to true.
359
1203
  */
360
- keepAliveMsecs?: number;
1204
+ keepAliveMsecs?: number | undefined;
361
1205
  /**
362
1206
  * Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
363
1207
  */
364
- maxSockets?: number;
1208
+ maxSockets?: number | undefined;
365
1209
  /**
366
1210
  * Maximum number of sockets allowed for all hosts in total. Each request will use a new socket until the maximum is reached. Default: Infinity.
367
1211
  */
368
- maxTotalSockets?: number;
1212
+ maxTotalSockets?: number | undefined;
369
1213
  /**
370
1214
  * Maximum number of sockets to leave open in a free state. Only relevant if keepAlive is set to true. Default = 256.
371
1215
  */
372
- maxFreeSockets?: number;
1216
+ maxFreeSockets?: number | undefined;
373
1217
  /**
374
1218
  * Socket timeout in milliseconds. This will set the timeout after the socket is connected.
375
1219
  */
376
- timeout?: number;
1220
+ timeout?: number | undefined;
377
1221
  /**
378
1222
  * Scheduling strategy to apply when picking the next free socket to use.
379
1223
  * @default `lifo`
380
1224
  */
381
- scheduling?: 'fifo' | 'lifo';
1225
+ scheduling?: 'fifo' | 'lifo' | undefined;
382
1226
  }
383
-
1227
+ /**
1228
+ * An `Agent` is responsible for managing connection persistence
1229
+ * and reuse for HTTP clients. It maintains a queue of pending requests
1230
+ * for a given host and port, reusing a single socket connection for each
1231
+ * until the queue is empty, at which time the socket is either destroyed
1232
+ * or put into a pool where it is kept to be used again for requests to the
1233
+ * same host and port. Whether it is destroyed or pooled depends on the`keepAlive` `option`.
1234
+ *
1235
+ * Pooled connections have TCP Keep-Alive enabled for them, but servers may
1236
+ * still close idle connections, in which case they will be removed from the
1237
+ * pool and a new connection will be made when a new HTTP request is made for
1238
+ * that host and port. Servers may also refuse to allow multiple requests
1239
+ * over the same connection, in which case the connection will have to be
1240
+ * remade for every request and cannot be pooled. The `Agent` will still make
1241
+ * the requests to that server, but each one will occur over a new connection.
1242
+ *
1243
+ * When a connection is closed by the client or the server, it is removed
1244
+ * from the pool. Any unused sockets in the pool will be unrefed so as not
1245
+ * to keep the Node.js process running when there are no outstanding requests.
1246
+ * (see `socket.unref()`).
1247
+ *
1248
+ * It is good practice, to `destroy()` an `Agent` instance when it is no
1249
+ * longer in use, because unused sockets consume OS resources.
1250
+ *
1251
+ * Sockets are removed from an agent when the socket emits either
1252
+ * a `'close'` event or an `'agentRemove'` event. When intending to keep one
1253
+ * HTTP request open for a long time without keeping it in the agent, something
1254
+ * like the following may be done:
1255
+ *
1256
+ * ```js
1257
+ * http.get(options, (res) => {
1258
+ * // Do stuff
1259
+ * }).on('socket', (socket) => {
1260
+ * socket.emit('agentRemove');
1261
+ * });
1262
+ * ```
1263
+ *
1264
+ * An agent may also be used for an individual request. By providing`{agent: false}` as an option to the `http.get()` or `http.request()`functions, a one-time use `Agent` with default options
1265
+ * will be used
1266
+ * for the client connection.
1267
+ *
1268
+ * `agent:false`:
1269
+ *
1270
+ * ```js
1271
+ * http.get({
1272
+ * hostname: 'localhost',
1273
+ * port: 80,
1274
+ * path: '/',
1275
+ * agent: false // Create a new agent just for this one request
1276
+ * }, (res) => {
1277
+ * // Do stuff with response
1278
+ * });
1279
+ * ```
1280
+ * @since v0.3.4
1281
+ */
384
1282
  class Agent {
1283
+ /**
1284
+ * By default set to 256\. For agents with `keepAlive` enabled, this
1285
+ * sets the maximum number of sockets that will be left open in the free
1286
+ * state.
1287
+ * @since v0.11.7
1288
+ */
385
1289
  maxFreeSockets: number;
1290
+ /**
1291
+ * By default set to `Infinity`. Determines how many concurrent sockets the agent
1292
+ * can have open per origin. Origin is the returned value of `agent.getName()`.
1293
+ * @since v0.3.6
1294
+ */
386
1295
  maxSockets: number;
1296
+ /**
1297
+ * By default set to `Infinity`. Determines how many concurrent sockets the agent
1298
+ * can have open. Unlike `maxSockets`, this parameter applies across all origins.
1299
+ * @since v14.5.0, v12.19.0
1300
+ */
387
1301
  maxTotalSockets: number;
1302
+ /**
1303
+ * An object which contains arrays of sockets currently awaiting use by
1304
+ * the agent when `keepAlive` is enabled. Do not modify.
1305
+ *
1306
+ * Sockets in the `freeSockets` list will be automatically destroyed and
1307
+ * removed from the array on `'timeout'`.
1308
+ * @since v0.11.4
1309
+ */
388
1310
  readonly freeSockets: NodeJS.ReadOnlyDict<Socket[]>;
1311
+ /**
1312
+ * An object which contains arrays of sockets currently in use by the
1313
+ * agent. Do not modify.
1314
+ * @since v0.3.6
1315
+ */
389
1316
  readonly sockets: NodeJS.ReadOnlyDict<Socket[]>;
1317
+ /**
1318
+ * An object which contains queues of requests that have not yet been assigned to
1319
+ * sockets. Do not modify.
1320
+ * @since v0.5.9
1321
+ */
390
1322
  readonly requests: NodeJS.ReadOnlyDict<IncomingMessage[]>;
391
-
392
1323
  constructor(opts?: AgentOptions);
393
-
394
1324
  /**
395
1325
  * Destroy any sockets that are currently in use by the agent.
396
- * It is usually not necessary to do this. However, if you are using an agent with KeepAlive enabled,
397
- * then it is best to explicitly shut down the agent when you know that it will no longer be used. Otherwise,
398
- * sockets may hang open for quite a long time before the server terminates them.
1326
+ *
1327
+ * It is usually not necessary to do this. However, if using an
1328
+ * agent with `keepAlive` enabled, then it is best to explicitly shut down
1329
+ * the agent when it is no longer needed. Otherwise,
1330
+ * sockets might stay open for quite a long time before the server
1331
+ * terminates them.
1332
+ * @since v0.11.4
399
1333
  */
400
1334
  destroy(): void;
401
1335
  }
402
-
403
1336
  const METHODS: string[];
404
-
405
1337
  const STATUS_CODES: {
406
1338
  [errorCode: number]: string | undefined;
407
1339
  [errorCode: string]: string | undefined;
408
1340
  };
409
-
410
- function createServer(requestListener?: RequestListener): Server;
411
- function createServer(options: ServerOptions, requestListener?: RequestListener): Server;
412
-
1341
+ /**
1342
+ * Returns a new instance of {@link Server}.
1343
+ *
1344
+ * The `requestListener` is a function which is automatically
1345
+ * added to the `'request'` event.
1346
+ * @since v0.1.13
1347
+ */
1348
+ function createServer<
1349
+ Request extends typeof IncomingMessage = typeof IncomingMessage,
1350
+ Response extends typeof ServerResponse = typeof ServerResponse,
1351
+ >(requestListener?: RequestListener<Request, Response>): Server<Request, Response>;
1352
+ function createServer<
1353
+ Request extends typeof IncomingMessage = typeof IncomingMessage,
1354
+ Response extends typeof ServerResponse = typeof ServerResponse,
1355
+ >(
1356
+ options: ServerOptions<Request, Response>,
1357
+ requestListener?: RequestListener<Request, Response>,
1358
+ ): Server<Request, Response>;
413
1359
  // although RequestOptions are passed as ClientRequestArgs to ClientRequest directly,
414
1360
  // create interface RequestOptions would make the naming more clear to developers
415
- interface RequestOptions extends ClientRequestArgs { }
1361
+ interface RequestOptions extends ClientRequestArgs {}
1362
+ /**
1363
+ * `options` in `socket.connect()` are also supported.
1364
+ *
1365
+ * Node.js maintains several connections per server to make HTTP requests.
1366
+ * This function allows one to transparently issue requests.
1367
+ *
1368
+ * `url` can be a string or a `URL` object. If `url` is a
1369
+ * string, it is automatically parsed with `new URL()`. If it is a `URL` object, it will be automatically converted to an ordinary `options` object.
1370
+ *
1371
+ * If both `url` and `options` are specified, the objects are merged, with the`options` properties taking precedence.
1372
+ *
1373
+ * The optional `callback` parameter will be added as a one-time listener for
1374
+ * the `'response'` event.
1375
+ *
1376
+ * `http.request()` returns an instance of the {@link ClientRequest} class. The `ClientRequest` instance is a writable stream. If one needs to
1377
+ * upload a file with a POST request, then write to the `ClientRequest` object.
1378
+ *
1379
+ * ```js
1380
+ * const http = require('http');
1381
+ *
1382
+ * const postData = JSON.stringify({
1383
+ * 'msg': 'Hello World!'
1384
+ * });
1385
+ *
1386
+ * const options = {
1387
+ * hostname: 'www.google.com',
1388
+ * port: 80,
1389
+ * path: '/upload',
1390
+ * method: 'POST',
1391
+ * headers: {
1392
+ * 'Content-Type': 'application/json',
1393
+ * 'Content-Length': Buffer.byteLength(postData)
1394
+ * }
1395
+ * };
1396
+ *
1397
+ * const req = http.request(options, (res) => {
1398
+ * console.log(`STATUS: ${res.statusCode}`);
1399
+ * console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
1400
+ * res.setEncoding('utf8');
1401
+ * res.on('data', (chunk) => {
1402
+ * console.log(`BODY: ${chunk}`);
1403
+ * });
1404
+ * res.on('end', () => {
1405
+ * console.log('No more data in response.');
1406
+ * });
1407
+ * });
1408
+ *
1409
+ * req.on('error', (e) => {
1410
+ * console.error(`problem with request: ${e.message}`);
1411
+ * });
1412
+ *
1413
+ * // Write data to request body
1414
+ * req.write(postData);
1415
+ * req.end();
1416
+ * ```
1417
+ *
1418
+ * In the example `req.end()` was called. With `http.request()` one
1419
+ * must always call `req.end()` to signify the end of the request -
1420
+ * even if there is no data being written to the request body.
1421
+ *
1422
+ * If any error is encountered during the request (be that with DNS resolution,
1423
+ * TCP level errors, or actual HTTP parse errors) an `'error'` event is emitted
1424
+ * on the returned request object. As with all `'error'` events, if no listeners
1425
+ * are registered the error will be thrown.
1426
+ *
1427
+ * There are a few special headers that should be noted.
1428
+ *
1429
+ * * Sending a 'Connection: keep-alive' will notify Node.js that the connection to
1430
+ * the server should be persisted until the next request.
1431
+ * * Sending a 'Content-Length' header will disable the default chunked encoding.
1432
+ * * Sending an 'Expect' header will immediately send the request headers.
1433
+ * Usually, when sending 'Expect: 100-continue', both a timeout and a listener
1434
+ * for the `'continue'` event should be set. See RFC 2616 Section 8.2.3 for more
1435
+ * information.
1436
+ * * Sending an Authorization header will override using the `auth` option
1437
+ * to compute basic authentication.
1438
+ *
1439
+ * Example using a `URL` as `options`:
1440
+ *
1441
+ * ```js
1442
+ * const options = new URL('http://abc:xyz@example.com');
1443
+ *
1444
+ * const req = http.request(options, (res) => {
1445
+ * // ...
1446
+ * });
1447
+ * ```
1448
+ *
1449
+ * In a successful request, the following events will be emitted in the following
1450
+ * order:
1451
+ *
1452
+ * * `'socket'`
1453
+ * * `'response'`
1454
+ * * `'data'` any number of times, on the `res` object
1455
+ * (`'data'` will not be emitted at all if the response body is empty, for
1456
+ * instance, in most redirects)
1457
+ * * `'end'` on the `res` object
1458
+ * * `'close'`
1459
+ *
1460
+ * In the case of a connection error, the following events will be emitted:
1461
+ *
1462
+ * * `'socket'`
1463
+ * * `'error'`
1464
+ * * `'close'`
1465
+ *
1466
+ * In the case of a premature connection close before the response is received,
1467
+ * the following events will be emitted in the following order:
1468
+ *
1469
+ * * `'socket'`
1470
+ * * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
1471
+ * * `'close'`
1472
+ *
1473
+ * In the case of a premature connection close after the response is received,
1474
+ * the following events will be emitted in the following order:
1475
+ *
1476
+ * * `'socket'`
1477
+ * * `'response'`
1478
+ * * `'data'` any number of times, on the `res` object
1479
+ * * (connection closed here)
1480
+ * * `'aborted'` on the `res` object
1481
+ * * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
1482
+ * * `'close'`
1483
+ * * `'close'` on the `res` object
1484
+ *
1485
+ * If `req.destroy()` is called before a socket is assigned, the following
1486
+ * events will be emitted in the following order:
1487
+ *
1488
+ * * (`req.destroy()` called here)
1489
+ * * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
1490
+ * * `'close'`
1491
+ *
1492
+ * If `req.destroy()` is called before the connection succeeds, the following
1493
+ * events will be emitted in the following order:
1494
+ *
1495
+ * * `'socket'`
1496
+ * * (`req.destroy()` called here)
1497
+ * * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
1498
+ * * `'close'`
1499
+ *
1500
+ * If `req.destroy()` is called after the response is received, the following
1501
+ * events will be emitted in the following order:
1502
+ *
1503
+ * * `'socket'`
1504
+ * * `'response'`
1505
+ * * `'data'` any number of times, on the `res` object
1506
+ * * (`req.destroy()` called here)
1507
+ * * `'aborted'` on the `res` object
1508
+ * * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
1509
+ * * `'close'`
1510
+ * * `'close'` on the `res` object
1511
+ *
1512
+ * If `req.abort()` is called before a socket is assigned, the following
1513
+ * events will be emitted in the following order:
1514
+ *
1515
+ * * (`req.abort()` called here)
1516
+ * * `'abort'`
1517
+ * * `'close'`
1518
+ *
1519
+ * If `req.abort()` is called before the connection succeeds, the following
1520
+ * events will be emitted in the following order:
1521
+ *
1522
+ * * `'socket'`
1523
+ * * (`req.abort()` called here)
1524
+ * * `'abort'`
1525
+ * * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
1526
+ * * `'close'`
1527
+ *
1528
+ * If `req.abort()` is called after the response is received, the following
1529
+ * events will be emitted in the following order:
1530
+ *
1531
+ * * `'socket'`
1532
+ * * `'response'`
1533
+ * * `'data'` any number of times, on the `res` object
1534
+ * * (`req.abort()` called here)
1535
+ * * `'abort'`
1536
+ * * `'aborted'` on the `res` object
1537
+ * * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
1538
+ * * `'close'`
1539
+ * * `'close'` on the `res` object
1540
+ *
1541
+ * Setting the `timeout` option or using the `setTimeout()` function will
1542
+ * not abort the request or do anything besides add a `'timeout'` event.
1543
+ *
1544
+ * Passing an `AbortSignal` and then calling `abort` on the corresponding`AbortController` will behave the same way as calling `.destroy()` on the
1545
+ * request itself.
1546
+ * @since v0.3.6
1547
+ */
416
1548
  function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
417
- function request(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
1549
+ function request(
1550
+ url: string | URL,
1551
+ options: RequestOptions,
1552
+ callback?: (res: IncomingMessage) => void,
1553
+ ): ClientRequest;
1554
+ /**
1555
+ * Since most requests are GET requests without bodies, Node.js provides this
1556
+ * convenience method. The only difference between this method and {@link request} is that it sets the method to GET and calls `req.end()`automatically. The callback must take care to consume the
1557
+ * response
1558
+ * data for reasons stated in {@link ClientRequest} section.
1559
+ *
1560
+ * The `callback` is invoked with a single argument that is an instance of {@link IncomingMessage}.
1561
+ *
1562
+ * JSON fetching example:
1563
+ *
1564
+ * ```js
1565
+ * http.get('http://localhost:8000/', (res) => {
1566
+ * const { statusCode } = res;
1567
+ * const contentType = res.headers['content-type'];
1568
+ *
1569
+ * let error;
1570
+ * // Any 2xx status code signals a successful response but
1571
+ * // here we're only checking for 200.
1572
+ * if (statusCode !== 200) {
1573
+ * error = new Error('Request Failed.\n' +
1574
+ * `Status Code: ${statusCode}`);
1575
+ * } else if (!/^application\/json/.test(contentType)) {
1576
+ * error = new Error('Invalid content-type.\n' +
1577
+ * `Expected application/json but received ${contentType}`);
1578
+ * }
1579
+ * if (error) {
1580
+ * console.error(error.message);
1581
+ * // Consume response data to free up memory
1582
+ * res.resume();
1583
+ * return;
1584
+ * }
1585
+ *
1586
+ * res.setEncoding('utf8');
1587
+ * let rawData = '';
1588
+ * res.on('data', (chunk) => { rawData += chunk; });
1589
+ * res.on('end', () => {
1590
+ * try {
1591
+ * const parsedData = JSON.parse(rawData);
1592
+ * console.log(parsedData);
1593
+ * } catch (e) {
1594
+ * console.error(e.message);
1595
+ * }
1596
+ * });
1597
+ * }).on('error', (e) => {
1598
+ * console.error(`Got error: ${e.message}`);
1599
+ * });
1600
+ *
1601
+ * // Create a local server to receive data from
1602
+ * const server = http.createServer((req, res) => {
1603
+ * res.writeHead(200, { 'Content-Type': 'application/json' });
1604
+ * res.end(JSON.stringify({
1605
+ * data: 'Hello World!'
1606
+ * }));
1607
+ * });
1608
+ *
1609
+ * server.listen(8000);
1610
+ * ```
1611
+ * @since v0.3.6
1612
+ * @param options Accepts the same `options` as {@link request}, with the `method` always set to `GET`. Properties that are inherited from the prototype are ignored.
1613
+ */
418
1614
  function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
419
1615
  function get(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
420
- let globalAgent: Agent;
421
1616
 
422
1617
  /**
423
- * Read-only property specifying the maximum allowed size of HTTP headers in bytes.
424
- * Defaults to 16KB. Configurable using the `--max-http-header-size` CLI option.
1618
+ * Performs the low-level validations on the provided name that are done when `res.setHeader(name, value)` is called.
1619
+ * Passing illegal value as name will result in a TypeError being thrown, identified by `code: 'ERR_INVALID_HTTP_TOKEN'`.
1620
+ * @param name Header name
1621
+ * @since v14.3.0
425
1622
  */
426
- const maxHeaderSize: number;
1623
+ function validateHeaderName(name: string): void;
1624
+ /**
1625
+ * Performs the low-level validations on the provided value that are done when `res.setHeader(name, value)` is called.
1626
+ * Passing illegal value as value will result in a TypeError being thrown.
1627
+ * - Undefined value error is identified by `code: 'ERR_HTTP_INVALID_HEADER_VALUE'`.
1628
+ * - Invalid value character error is identified by `code: 'ERR_INVALID_CHAR'`.
1629
+ * @param name Header name
1630
+ * @param value Header value
1631
+ * @since v14.3.0
1632
+ */
1633
+ function validateHeaderValue(name: string, value: string): void;
427
1634
 
428
1635
  /**
429
- *
430
- * This utility function converts a URL object into an ordinary options object as
431
- * expected by the `http.request()` and `https.request()` APIs.
1636
+ * Set the maximum number of idle HTTP parsers. Default: 1000.
1637
+ * @param count
1638
+ * @since v18.8.0, v16.18.0
432
1639
  */
433
- function urlToHttpOptions(url: URL): ClientRequestArgs;
1640
+ function setMaxIdleHTTPParsers(count: number): void;
1641
+
1642
+ let globalAgent: Agent;
1643
+ /**
1644
+ * Read-only property specifying the maximum allowed size of HTTP headers in bytes.
1645
+ * Defaults to 16KB. Configurable using the `--max-http-header-size` CLI option.
1646
+ */
1647
+ const maxHeaderSize: number;
1648
+ }
1649
+ declare module 'node:http' {
1650
+ export * from 'http';
434
1651
  }