UNPKG

18.9 kBTypeScriptView Raw
1declare module 'node:http' {
2 export * from 'http';
3}
4
5declare module 'http' {
6 import * as stream from 'node:stream';
7 import { URL } from 'node:url';
8 import { Socket, Server as NetServer } from 'node:net';
9
10 // incoming headers will never contain number
11 interface IncomingHttpHeaders extends NodeJS.Dict<string | string[]> {
12 'accept'?: string;
13 'accept-language'?: string;
14 'accept-patch'?: string;
15 'accept-ranges'?: string;
16 'access-control-allow-credentials'?: string;
17 'access-control-allow-headers'?: string;
18 'access-control-allow-methods'?: string;
19 'access-control-allow-origin'?: string;
20 'access-control-expose-headers'?: string;
21 'access-control-max-age'?: string;
22 'access-control-request-headers'?: string;
23 'access-control-request-method'?: string;
24 'age'?: string;
25 'allow'?: string;
26 'alt-svc'?: string;
27 'authorization'?: string;
28 'cache-control'?: string;
29 'connection'?: string;
30 'content-disposition'?: string;
31 'content-encoding'?: string;
32 'content-language'?: string;
33 'content-length'?: string;
34 'content-location'?: string;
35 'content-range'?: string;
36 'content-type'?: string;
37 'cookie'?: string;
38 'date'?: string;
39 'expect'?: string;
40 'expires'?: string;
41 'forwarded'?: string;
42 'from'?: string;
43 'host'?: string;
44 'if-match'?: string;
45 'if-modified-since'?: string;
46 'if-none-match'?: string;
47 'if-unmodified-since'?: string;
48 'last-modified'?: string;
49 'location'?: string;
50 'origin'?: string;
51 'pragma'?: string;
52 'proxy-authenticate'?: string;
53 'proxy-authorization'?: string;
54 'public-key-pins'?: string;
55 'range'?: string;
56 'referer'?: string;
57 'retry-after'?: string;
58 'sec-websocket-accept'?: string;
59 'sec-websocket-extensions'?: string;
60 'sec-websocket-key'?: string;
61 'sec-websocket-protocol'?: string;
62 'sec-websocket-version'?: string;
63 'set-cookie'?: string[];
64 'strict-transport-security'?: string;
65 'tk'?: string;
66 'trailer'?: string;
67 'transfer-encoding'?: string;
68 'upgrade'?: string;
69 'user-agent'?: string;
70 'vary'?: string;
71 'via'?: string;
72 'warning'?: string;
73 'www-authenticate'?: string;
74 }
75
76 // outgoing headers allows numbers (as they are converted internally to strings)
77 type OutgoingHttpHeader = number | string | string[];
78
79 interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {
80 }
81
82 interface ClientRequestArgs {
83 protocol?: string | null;
84 host?: string | null;
85 hostname?: string | null;
86 family?: number;
87 port?: number | string | null;
88 defaultPort?: number | string;
89 localAddress?: string;
90 socketPath?: string;
91 /**
92 * @default 8192
93 */
94 maxHeaderSize?: number;
95 method?: string;
96 path?: string | null;
97 headers?: OutgoingHttpHeaders;
98 auth?: string | null;
99 agent?: Agent | boolean;
100 _defaultAgent?: Agent;
101 timeout?: number;
102 setHost?: boolean;
103 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278
104 createConnection?: (options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket;
105 }
106
107 interface ServerOptions {
108 IncomingMessage?: typeof IncomingMessage;
109 ServerResponse?: typeof ServerResponse;
110 /**
111 * Optionally overrides the value of
112 * [`--max-http-header-size`][] for requests received by this server, i.e.
113 * the maximum length of request headers in bytes.
114 * @default 8192
115 */
116 maxHeaderSize?: number;
117 /**
118 * Use an insecure HTTP parser that accepts invalid HTTP headers when true.
119 * Using the insecure parser should be avoided.
120 * See --insecure-http-parser for more information.
121 * @default false
122 */
123 insecureHTTPParser?: boolean;
124 }
125
126 type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
127
128 interface HttpBase {
129 setTimeout(msecs?: number, callback?: () => void): this;
130 setTimeout(callback: () => void): this;
131 /**
132 * Limits maximum incoming headers count. If set to 0, no limit will be applied.
133 * @default 2000
134 * {@link https://nodejs.org/api/http.html#http_server_maxheaderscount}
135 */
136 maxHeadersCount: number | null;
137 timeout: number;
138 /**
139 * Limit the amount of time the parser will wait to receive the complete HTTP headers.
140 * @default 60000
141 * {@link https://nodejs.org/api/http.html#http_server_headerstimeout}
142 */
143 headersTimeout: number;
144 keepAliveTimeout: number;
145 /**
146 * Sets the timeout value in milliseconds for receiving the entire request from the client.
147 * @default 0
148 * {@link https://nodejs.org/api/http.html#http_server_requesttimeout}
149 */
150 requestTimeout: number;
151 }
152
153 interface Server extends HttpBase {}
154 class Server extends NetServer {
155 constructor(requestListener?: RequestListener);
156 constructor(options: ServerOptions, requestListener?: RequestListener);
157 }
158
159 // https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
160 class OutgoingMessage extends stream.Writable {
161 upgrading: boolean;
162 chunkedEncoding: boolean;
163 shouldKeepAlive: boolean;
164 useChunkedEncodingByDefault: boolean;
165 sendDate: boolean;
166 /**
167 * @deprecated Use `writableEnded` instead.
168 */
169 finished: boolean;
170 headersSent: boolean;
171 /**
172 * @deprecate Use `socket` instead.
173 */
174 connection: Socket | null;
175 socket: Socket | null;
176
177 constructor();
178
179 setTimeout(msecs: number, callback?: () => void): this;
180 setHeader(name: string, value: number | string | ReadonlyArray<string>): void;
181 getHeader(name: string): number | string | string[] | undefined;
182 getHeaders(): OutgoingHttpHeaders;
183 getHeaderNames(): string[];
184 hasHeader(name: string): boolean;
185 removeHeader(name: string): void;
186 addTrailers(headers: OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void;
187 flushHeaders(): void;
188 }
189
190 // https://github.com/nodejs/node/blob/master/lib/_http_server.js#L108-L256
191 class ServerResponse extends OutgoingMessage {
192 statusCode: number;
193 statusMessage: string;
194
195 constructor(req: IncomingMessage);
196
197 assignSocket(socket: Socket): void;
198 detachSocket(socket: Socket): void;
199 // https://github.com/nodejs/node/blob/master/test/parallel/test-http-write-callbacks.js#L53
200 // no args in writeContinue callback
201 writeContinue(callback?: () => void): void;
202 writeHead(statusCode: number, reasonPhrase?: string, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
203 writeHead(statusCode: number, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
204 writeProcessing(): void;
205 }
206
207 interface InformationEvent {
208 statusCode: number;
209 statusMessage: string;
210 httpVersion: string;
211 httpVersionMajor: number;
212 httpVersionMinor: number;
213 headers: IncomingHttpHeaders;
214 rawHeaders: string[];
215 }
216
217 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
218 class ClientRequest extends OutgoingMessage {
219 aborted: boolean;
220 host: string;
221 protocol: string;
222
223 constructor(url: string | URL | ClientRequestArgs, cb?: (res: IncomingMessage) => void);
224
225 method: string;
226 path: string;
227 /** @deprecated since v14.1.0 Use `request.destroy()` instead. */
228 abort(): void;
229 onSocket(socket: Socket): void;
230 setTimeout(timeout: number, callback?: () => void): this;
231 setNoDelay(noDelay?: boolean): void;
232 setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
233
234 addListener(event: 'abort', listener: () => void): this;
235 addListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
236 addListener(event: 'continue', listener: () => void): this;
237 addListener(event: 'information', listener: (info: InformationEvent) => void): this;
238 addListener(event: 'response', listener: (response: IncomingMessage) => void): this;
239 addListener(event: 'socket', listener: (socket: Socket) => void): this;
240 addListener(event: 'timeout', listener: () => void): this;
241 addListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
242 addListener(event: 'close', listener: () => void): this;
243 addListener(event: 'drain', listener: () => void): this;
244 addListener(event: 'error', listener: (err: Error) => void): this;
245 addListener(event: 'finish', listener: () => void): this;
246 addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
247 addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
248 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
249
250 on(event: 'abort', listener: () => void): this;
251 on(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
252 on(event: 'continue', listener: () => void): this;
253 on(event: 'information', listener: (info: InformationEvent) => void): this;
254 on(event: 'response', listener: (response: IncomingMessage) => void): this;
255 on(event: 'socket', listener: (socket: Socket) => void): this;
256 on(event: 'timeout', listener: () => void): this;
257 on(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
258 on(event: 'close', listener: () => void): this;
259 on(event: 'drain', listener: () => void): this;
260 on(event: 'error', listener: (err: Error) => void): this;
261 on(event: 'finish', listener: () => void): this;
262 on(event: 'pipe', listener: (src: stream.Readable) => void): this;
263 on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
264 on(event: string | symbol, listener: (...args: any[]) => void): this;
265
266 once(event: 'abort', listener: () => void): this;
267 once(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
268 once(event: 'continue', listener: () => void): this;
269 once(event: 'information', listener: (info: InformationEvent) => void): this;
270 once(event: 'response', listener: (response: IncomingMessage) => void): this;
271 once(event: 'socket', listener: (socket: Socket) => void): this;
272 once(event: 'timeout', listener: () => void): this;
273 once(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
274 once(event: 'close', listener: () => void): this;
275 once(event: 'drain', listener: () => void): this;
276 once(event: 'error', listener: (err: Error) => void): this;
277 once(event: 'finish', listener: () => void): this;
278 once(event: 'pipe', listener: (src: stream.Readable) => void): this;
279 once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
280 once(event: string | symbol, listener: (...args: any[]) => void): this;
281
282 prependListener(event: 'abort', listener: () => void): this;
283 prependListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
284 prependListener(event: 'continue', listener: () => void): this;
285 prependListener(event: 'information', listener: (info: InformationEvent) => void): this;
286 prependListener(event: 'response', listener: (response: IncomingMessage) => void): this;
287 prependListener(event: 'socket', listener: (socket: Socket) => void): this;
288 prependListener(event: 'timeout', listener: () => void): this;
289 prependListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
290 prependListener(event: 'close', listener: () => void): this;
291 prependListener(event: 'drain', listener: () => void): this;
292 prependListener(event: 'error', listener: (err: Error) => void): this;
293 prependListener(event: 'finish', listener: () => void): this;
294 prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
295 prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
296 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
297
298 prependOnceListener(event: 'abort', listener: () => void): this;
299 prependOnceListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
300 prependOnceListener(event: 'continue', listener: () => void): this;
301 prependOnceListener(event: 'information', listener: (info: InformationEvent) => void): this;
302 prependOnceListener(event: 'response', listener: (response: IncomingMessage) => void): this;
303 prependOnceListener(event: 'socket', listener: (socket: Socket) => void): this;
304 prependOnceListener(event: 'timeout', listener: () => void): this;
305 prependOnceListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
306 prependOnceListener(event: 'close', listener: () => void): this;
307 prependOnceListener(event: 'drain', listener: () => void): this;
308 prependOnceListener(event: 'error', listener: (err: Error) => void): this;
309 prependOnceListener(event: 'finish', listener: () => void): this;
310 prependOnceListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
311 prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
312 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
313 }
314
315 class IncomingMessage extends stream.Readable {
316 constructor(socket: Socket);
317
318 aborted: boolean;
319 httpVersion: string;
320 httpVersionMajor: number;
321 httpVersionMinor: number;
322 complete: boolean;
323 /**
324 * @deprecated since v13.0.0 - Use `socket` instead.
325 */
326 connection: Socket;
327 socket: Socket;
328 headers: IncomingHttpHeaders;
329 rawHeaders: string[];
330 trailers: NodeJS.Dict<string>;
331 rawTrailers: string[];
332 setTimeout(msecs: number, callback?: () => void): this;
333 /**
334 * Only valid for request obtained from http.Server.
335 */
336 method?: string;
337 /**
338 * Only valid for request obtained from http.Server.
339 */
340 url?: string;
341 /**
342 * Only valid for response obtained from http.ClientRequest.
343 */
344 statusCode?: number;
345 /**
346 * Only valid for response obtained from http.ClientRequest.
347 */
348 statusMessage?: string;
349 destroy(error?: Error): void;
350 }
351
352 interface AgentOptions {
353 /**
354 * Keep sockets around in a pool to be used by other requests in the future. Default = false
355 */
356 keepAlive?: boolean;
357 /**
358 * When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
359 * Only relevant if keepAlive is set to true.
360 */
361 keepAliveMsecs?: number;
362 /**
363 * Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
364 */
365 maxSockets?: number;
366 /**
367 * Maximum number of sockets allowed for all hosts in total. Each request will use a new socket until the maximum is reached. Default: Infinity.
368 */
369 maxTotalSockets?: number;
370 /**
371 * Maximum number of sockets to leave open in a free state. Only relevant if keepAlive is set to true. Default = 256.
372 */
373 maxFreeSockets?: number;
374 /**
375 * Socket timeout in milliseconds. This will set the timeout after the socket is connected.
376 */
377 timeout?: number;
378 /**
379 * Scheduling strategy to apply when picking the next free socket to use. Default: 'fifo'.
380 */
381 scheduling?: 'fifo' | 'lifo';
382 }
383
384 class Agent {
385 maxFreeSockets: number;
386 maxSockets: number;
387 maxTotalSockets: number;
388 readonly freeSockets: NodeJS.ReadOnlyDict<Socket[]>;
389 readonly sockets: NodeJS.ReadOnlyDict<Socket[]>;
390 readonly requests: NodeJS.ReadOnlyDict<IncomingMessage[]>;
391
392 constructor(opts?: AgentOptions);
393
394 /**
395 * 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.
399 */
400 destroy(): void;
401 }
402
403 const METHODS: string[];
404
405 const STATUS_CODES: {
406 [errorCode: number]: string | undefined;
407 [errorCode: string]: string | undefined;
408 };
409
410 function createServer(requestListener?: RequestListener): Server;
411 function createServer(options: ServerOptions, requestListener?: RequestListener): Server;
412
413 // although RequestOptions are passed as ClientRequestArgs to ClientRequest directly,
414 // create interface RequestOptions would make the naming more clear to developers
415 interface RequestOptions extends ClientRequestArgs { }
416 function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
417 function request(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
418 function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
419 function get(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
420 let globalAgent: Agent;
421
422 /**
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.
425 */
426 const maxHeaderSize: number;
427}