UNPKG

19.4 kBTypeScriptView Raw
1declare module 'http' {
2 import * as stream from 'stream';
3 import { URL } from 'url';
4 import { Socket, Server as NetServer } from 'net';
5
6 // incoming headers will never contain number
7 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;
71 }
72
73 // outgoing headers allows numbers (as they are converted internally to strings)
74 type OutgoingHttpHeader = number | string | string[];
75
76 interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {
77 }
78
79 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;
101 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278
102 createConnection?: (options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket;
103 }
104
105 interface ServerOptions {
106 IncomingMessage?: typeof IncomingMessage;
107 ServerResponse?: typeof ServerResponse;
108 /**
109 * Optionally overrides the value of
110 * `--max-http-header-size` for requests received by this server, i.e.
111 * the maximum length of request headers in bytes.
112 * @default 8192
113 */
114 maxHeaderSize?: number;
115 /**
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.
119 * @default false
120 */
121 insecureHTTPParser?: boolean;
122 }
123
124 type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
125
126 interface HttpBase {
127 setTimeout(msecs?: number, callback?: () => void): this;
128 setTimeout(callback: () => void): this;
129 /**
130 * 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}
133 */
134 maxHeadersCount: number | null;
135 timeout: number;
136 /**
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}
140 */
141 headersTimeout: number;
142 keepAliveTimeout: number;
143 /**
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}
147 */
148 requestTimeout: number;
149 }
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
161 chunkedEncoding: boolean;
162 shouldKeepAlive: boolean;
163 useChunkedEncodingByDefault: boolean;
164 sendDate: boolean;
165 /**
166 * @deprecated Use `writableEnded` instead.
167 */
168 finished: boolean;
169 readonly headersSent: boolean;
170 /**
171 * @deprecated Use `socket` instead.
172 */
173 readonly connection: Socket | null;
174 readonly socket: Socket | null;
175
176 constructor();
177
178 setTimeout(msecs: number, callback?: () => void): this;
179 setHeader(name: string, value: number | string | ReadonlyArray<string>): this;
180 getHeader(name: string): number | string | string[] | undefined;
181 getHeaders(): OutgoingHttpHeaders;
182 getHeaderNames(): string[];
183 hasHeader(name: string): boolean;
184 removeHeader(name: string): void;
185 addTrailers(headers: OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void;
186 flushHeaders(): void;
187 }
188
189 // https://github.com/nodejs/node/blob/master/lib/_http_server.js#L108-L256
190 class ServerResponse extends OutgoingMessage {
191 statusCode: number;
192 statusMessage: string;
193
194 constructor(req: IncomingMessage);
195
196 assignSocket(socket: Socket): void;
197 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
200 writeContinue(callback?: () => void): void;
201 writeHead(statusCode: number, reasonPhrase?: string, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
202 writeHead(statusCode: number, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
203 writeProcessing(): void;
204 }
205
206 interface InformationEvent {
207 statusCode: number;
208 statusMessage: string;
209 httpVersion: string;
210 httpVersionMajor: number;
211 httpVersionMinor: number;
212 headers: IncomingHttpHeaders;
213 rawHeaders: string[];
214 }
215
216 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
217 class ClientRequest extends OutgoingMessage {
218 aborted: boolean;
219 host: string;
220 protocol: string;
221
222 constructor(url: string | URL | ClientRequestArgs, cb?: (res: IncomingMessage) => void);
223
224 method: string;
225 path: string;
226 /** @deprecated since v14.1.0 Use `request.destroy()` instead. */
227 abort(): void;
228 onSocket(socket: Socket): void;
229 setTimeout(timeout: number, callback?: () => void): this;
230 setNoDelay(noDelay?: boolean): void;
231 setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
232 /**
233 * Returns an array containing the unique names of the current outgoing raw headers.
234 * Header names are returned with their exact casing being set.
235 */
236 getRawHeaderNames(): string[];
237
238 addListener(event: 'abort', listener: () => void): this;
239 addListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
240 addListener(event: 'continue', listener: () => void): this;
241 addListener(event: 'information', listener: (info: InformationEvent) => void): this;
242 addListener(event: 'response', listener: (response: IncomingMessage) => void): this;
243 addListener(event: 'socket', listener: (socket: Socket) => void): this;
244 addListener(event: 'timeout', listener: () => void): this;
245 addListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
246 addListener(event: 'close', listener: () => void): this;
247 addListener(event: 'drain', listener: () => void): this;
248 addListener(event: 'error', listener: (err: Error) => void): this;
249 addListener(event: 'finish', listener: () => void): this;
250 addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
251 addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
252 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
253
254 on(event: 'abort', listener: () => void): this;
255 on(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
256 on(event: 'continue', listener: () => void): this;
257 on(event: 'information', listener: (info: InformationEvent) => void): this;
258 on(event: 'response', listener: (response: IncomingMessage) => void): this;
259 on(event: 'socket', listener: (socket: Socket) => void): this;
260 on(event: 'timeout', listener: () => void): this;
261 on(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
262 on(event: 'close', listener: () => void): this;
263 on(event: 'drain', listener: () => void): this;
264 on(event: 'error', listener: (err: Error) => void): this;
265 on(event: 'finish', listener: () => void): this;
266 on(event: 'pipe', listener: (src: stream.Readable) => void): this;
267 on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
268 on(event: string | symbol, listener: (...args: any[]) => void): this;
269
270 once(event: 'abort', listener: () => void): this;
271 once(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
272 once(event: 'continue', listener: () => void): this;
273 once(event: 'information', listener: (info: InformationEvent) => void): this;
274 once(event: 'response', listener: (response: IncomingMessage) => void): this;
275 once(event: 'socket', listener: (socket: Socket) => void): this;
276 once(event: 'timeout', listener: () => void): this;
277 once(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
278 once(event: 'close', listener: () => void): this;
279 once(event: 'drain', listener: () => void): this;
280 once(event: 'error', listener: (err: Error) => void): this;
281 once(event: 'finish', listener: () => void): this;
282 once(event: 'pipe', listener: (src: stream.Readable) => void): this;
283 once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
284 once(event: string | symbol, listener: (...args: any[]) => void): this;
285
286 prependListener(event: 'abort', listener: () => void): this;
287 prependListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
288 prependListener(event: 'continue', listener: () => void): this;
289 prependListener(event: 'information', listener: (info: InformationEvent) => void): this;
290 prependListener(event: 'response', listener: (response: IncomingMessage) => void): this;
291 prependListener(event: 'socket', listener: (socket: Socket) => void): this;
292 prependListener(event: 'timeout', listener: () => void): this;
293 prependListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
294 prependListener(event: 'close', listener: () => void): this;
295 prependListener(event: 'drain', listener: () => void): this;
296 prependListener(event: 'error', listener: (err: Error) => void): this;
297 prependListener(event: 'finish', listener: () => void): this;
298 prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
299 prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
300 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
301
302 prependOnceListener(event: 'abort', listener: () => void): this;
303 prependOnceListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
304 prependOnceListener(event: 'continue', listener: () => void): this;
305 prependOnceListener(event: 'information', listener: (info: InformationEvent) => void): this;
306 prependOnceListener(event: 'response', listener: (response: IncomingMessage) => void): this;
307 prependOnceListener(event: 'socket', listener: (socket: Socket) => void): this;
308 prependOnceListener(event: 'timeout', listener: () => void): this;
309 prependOnceListener(event: 'upgrade', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
310 prependOnceListener(event: 'close', listener: () => void): this;
311 prependOnceListener(event: 'drain', listener: () => void): this;
312 prependOnceListener(event: 'error', listener: (err: Error) => void): this;
313 prependOnceListener(event: 'finish', listener: () => void): this;
314 prependOnceListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
315 prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
316 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
317 }
318
319 class IncomingMessage extends stream.Readable {
320 constructor(socket: Socket);
321
322 aborted: boolean;
323 httpVersion: string;
324 httpVersionMajor: number;
325 httpVersionMinor: number;
326 complete: boolean;
327 /**
328 * @deprecated since v13.0.0 - Use `socket` instead.
329 */
330 connection: Socket;
331 socket: Socket;
332 headers: IncomingHttpHeaders;
333 rawHeaders: string[];
334 trailers: NodeJS.Dict<string>;
335 rawTrailers: string[];
336 setTimeout(msecs: number, callback?: () => void): this;
337 /**
338 * Only valid for request obtained from http.Server.
339 */
340 method?: string;
341 /**
342 * Only valid for request obtained from http.Server.
343 */
344 url?: string;
345 /**
346 * Only valid for response obtained from http.ClientRequest.
347 */
348 statusCode?: number;
349 /**
350 * Only valid for response obtained from http.ClientRequest.
351 */
352 statusMessage?: string;
353 destroy(error?: Error): void;
354 }
355
356 interface AgentOptions {
357 /**
358 * Keep sockets around in a pool to be used by other requests in the future. Default = false
359 */
360 keepAlive?: boolean;
361 /**
362 * When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
363 * Only relevant if keepAlive is set to true.
364 */
365 keepAliveMsecs?: number;
366 /**
367 * Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
368 */
369 maxSockets?: number;
370 /**
371 * Maximum number of sockets allowed for all hosts in total. Each request will use a new socket until the maximum is reached. Default: Infinity.
372 */
373 maxTotalSockets?: number;
374 /**
375 * Maximum number of sockets to leave open in a free state. Only relevant if keepAlive is set to true. Default = 256.
376 */
377 maxFreeSockets?: number;
378 /**
379 * Socket timeout in milliseconds. This will set the timeout after the socket is connected.
380 */
381 timeout?: number;
382 /**
383 * Scheduling strategy to apply when picking the next free socket to use.
384 * @default `lifo`
385 */
386 scheduling?: 'fifo' | 'lifo';
387 }
388
389 class Agent {
390 maxFreeSockets: number;
391 maxSockets: number;
392 maxTotalSockets: number;
393 readonly freeSockets: NodeJS.ReadOnlyDict<Socket[]>;
394 readonly sockets: NodeJS.ReadOnlyDict<Socket[]>;
395 readonly requests: NodeJS.ReadOnlyDict<IncomingMessage[]>;
396
397 constructor(opts?: AgentOptions);
398
399 /**
400 * Destroy any sockets that are currently in use by the agent.
401 * It is usually not necessary to do this. However, if you are using an agent with KeepAlive enabled,
402 * then it is best to explicitly shut down the agent when you know that it will no longer be used. Otherwise,
403 * sockets may hang open for quite a long time before the server terminates them.
404 */
405 destroy(): void;
406 }
407
408 const METHODS: string[];
409
410 const STATUS_CODES: {
411 [errorCode: number]: string | undefined;
412 [errorCode: string]: string | undefined;
413 };
414
415 function createServer(requestListener?: RequestListener): Server;
416 function createServer(options: ServerOptions, requestListener?: RequestListener): Server;
417
418 // although RequestOptions are passed as ClientRequestArgs to ClientRequest directly,
419 // create interface RequestOptions would make the naming more clear to developers
420 interface RequestOptions extends ClientRequestArgs { }
421 function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
422 function request(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
423 function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
424 function get(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
425 let globalAgent: Agent;
426
427 /**
428 * Read-only property specifying the maximum allowed size of HTTP headers in bytes.
429 * Defaults to 16KB. Configurable using the `--max-http-header-size` CLI option.
430 */
431 const maxHeaderSize: number;
432
433 /**
434 *
435 * This utility function converts a URL object into an ordinary options object as
436 * expected by the `http.request()` and `https.request()` APIs.
437 */
438 function urlToHttpOptions(url: URL): ClientRequestArgs;
439}