UNPKG

10.3 kBTypeScriptView Raw
1declare module "http" {
2 import * as events from "events";
3 import * as stream from "stream";
4 import { URL } from "url";
5 import { Socket, Server as NetServer } from "net";
6
7 // incoming headers will never contain number
8 interface IncomingHttpHeaders {
9 'accept'?: string;
10 'accept-language'?: string;
11 'accept-patch'?: string;
12 'accept-ranges'?: string;
13 'access-control-allow-credentials'?: string;
14 'access-control-allow-headers'?: string;
15 'access-control-allow-methods'?: string;
16 'access-control-allow-origin'?: string;
17 'access-control-expose-headers'?: string;
18 'access-control-max-age'?: string;
19 'age'?: string;
20 'allow'?: string;
21 'alt-svc'?: string;
22 'authorization'?: string;
23 'cache-control'?: string;
24 'connection'?: string;
25 'content-disposition'?: string;
26 'content-encoding'?: string;
27 'content-language'?: string;
28 'content-length'?: string;
29 'content-location'?: string;
30 'content-range'?: string;
31 'content-type'?: string;
32 'cookie'?: string;
33 'date'?: string;
34 'expect'?: string;
35 'expires'?: string;
36 'forwarded'?: string;
37 'from'?: string;
38 'host'?: string;
39 'if-match'?: string;
40 'if-modified-since'?: string;
41 'if-none-match'?: string;
42 'if-unmodified-since'?: string;
43 'last-modified'?: string;
44 'location'?: string;
45 'pragma'?: string;
46 'proxy-authenticate'?: string;
47 'proxy-authorization'?: string;
48 'public-key-pins'?: string;
49 'range'?: string;
50 'referer'?: string;
51 'retry-after'?: string;
52 'set-cookie'?: string[];
53 'strict-transport-security'?: string;
54 'tk'?: string;
55 'trailer'?: string;
56 'transfer-encoding'?: string;
57 'upgrade'?: string;
58 'user-agent'?: string;
59 'vary'?: string;
60 'via'?: string;
61 'warning'?: string;
62 'www-authenticate'?: string;
63 [header: string]: string | string[] | undefined;
64 }
65
66 // outgoing headers allows numbers (as they are converted internally to strings)
67 interface OutgoingHttpHeaders {
68 [header: string]: number | string | string[] | undefined;
69 }
70
71 interface ClientRequestArgs {
72 protocol?: string;
73 host?: string;
74 hostname?: string;
75 family?: number;
76 port?: number | string;
77 defaultPort?: number | string;
78 localAddress?: string;
79 socketPath?: string;
80 method?: string;
81 path?: string;
82 headers?: OutgoingHttpHeaders;
83 auth?: string;
84 agent?: Agent | boolean;
85 _defaultAgent?: Agent;
86 timeout?: number;
87 setHost?: boolean;
88 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278
89 createConnection?: (options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket;
90 }
91
92 interface ServerOptions {
93 IncomingMessage?: typeof IncomingMessage;
94 ServerResponse?: typeof ServerResponse;
95 }
96
97 type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
98
99 class Server extends NetServer {
100 constructor(requestListener?: RequestListener);
101 constructor(options: ServerOptions, requestListener?: RequestListener);
102
103 setTimeout(msecs?: number, callback?: () => void): this;
104 setTimeout(callback: () => void): this;
105 /**
106 * Limits maximum incoming headers count. If set to 0, no limit will be applied.
107 * @default 2000
108 * {@link https://nodejs.org/api/http.html#http_server_maxheaderscount}
109 */
110 maxHeadersCount: number | null;
111 timeout: number;
112 /**
113 * Limit the amount of time the parser will wait to receive the complete HTTP headers.
114 * @default 40000
115 * {@link https://nodejs.org/api/http.html#http_server_headerstimeout}
116 */
117 headersTimeout: number;
118 keepAliveTimeout: number;
119 }
120
121 // https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js
122 class OutgoingMessage extends stream.Writable {
123 upgrading: boolean;
124 chunkedEncoding: boolean;
125 shouldKeepAlive: boolean;
126 useChunkedEncodingByDefault: boolean;
127 sendDate: boolean;
128 finished: boolean;
129 headersSent: boolean;
130 connection: Socket;
131
132 constructor();
133
134 setTimeout(msecs: number, callback?: () => void): this;
135 setHeader(name: string, value: number | string | string[]): void;
136 getHeader(name: string): number | string | string[] | undefined;
137 getHeaders(): OutgoingHttpHeaders;
138 getHeaderNames(): string[];
139 hasHeader(name: string): boolean;
140 removeHeader(name: string): void;
141 addTrailers(headers: OutgoingHttpHeaders | Array<[string, string]>): void;
142 flushHeaders(): void;
143 }
144
145 // https://github.com/nodejs/node/blob/master/lib/_http_server.js#L108-L256
146 class ServerResponse extends OutgoingMessage {
147 statusCode: number;
148 statusMessage: string;
149
150 constructor(req: IncomingMessage);
151
152 assignSocket(socket: Socket): void;
153 detachSocket(socket: Socket): void;
154 // https://github.com/nodejs/node/blob/master/test/parallel/test-http-write-callbacks.js#L53
155 // no args in writeContinue callback
156 writeContinue(callback?: () => void): void;
157 writeHead(statusCode: number, reasonPhrase?: string, headers?: OutgoingHttpHeaders): this;
158 writeHead(statusCode: number, headers?: OutgoingHttpHeaders): this;
159 }
160
161 // https://github.com/nodejs/node/blob/master/lib/_http_client.js#L77
162 class ClientRequest extends OutgoingMessage {
163 connection: Socket;
164 socket: Socket;
165 aborted: number;
166
167 constructor(url: string | URL | ClientRequestArgs, cb?: (res: IncomingMessage) => void);
168
169 readonly path: string;
170 abort(): void;
171 onSocket(socket: Socket): void;
172 setTimeout(timeout: number, callback?: () => void): this;
173 setNoDelay(noDelay?: boolean): void;
174 setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
175 }
176
177 class IncomingMessage extends stream.Readable {
178 constructor(socket: Socket);
179
180 httpVersion: string;
181 httpVersionMajor: number;
182 httpVersionMinor: number;
183 complete: boolean;
184 connection: Socket;
185 headers: IncomingHttpHeaders;
186 rawHeaders: string[];
187 trailers: { [key: string]: string | undefined };
188 rawTrailers: string[];
189 setTimeout(msecs: number, callback: () => void): this;
190 /**
191 * Only valid for request obtained from http.Server.
192 */
193 method?: string;
194 /**
195 * Only valid for request obtained from http.Server.
196 */
197 url?: string;
198 /**
199 * Only valid for response obtained from http.ClientRequest.
200 */
201 statusCode?: number;
202 /**
203 * Only valid for response obtained from http.ClientRequest.
204 */
205 statusMessage?: string;
206 socket: Socket;
207 destroy(error?: Error): void;
208 }
209
210 interface AgentOptions {
211 /**
212 * Keep sockets around in a pool to be used by other requests in the future. Default = false
213 */
214 keepAlive?: boolean;
215 /**
216 * When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
217 * Only relevant if keepAlive is set to true.
218 */
219 keepAliveMsecs?: number;
220 /**
221 * Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
222 */
223 maxSockets?: number;
224 /**
225 * Maximum number of sockets to leave open in a free state. Only relevant if keepAlive is set to true. Default = 256.
226 */
227 maxFreeSockets?: number;
228 /**
229 * Socket timeout in milliseconds. This will set the timeout after the socket is connected.
230 */
231 timeout?: number;
232 }
233
234 class Agent {
235 maxFreeSockets: number;
236 maxSockets: number;
237 readonly sockets: {
238 readonly [key: string]: Socket[];
239 };
240 readonly requests: {
241 readonly [key: string]: IncomingMessage[];
242 };
243
244 constructor(opts?: AgentOptions);
245
246 /**
247 * Destroy any sockets that are currently in use by the agent.
248 * It is usually not necessary to do this. However, if you are using an agent with KeepAlive enabled,
249 * then it is best to explicitly shut down the agent when you know that it will no longer be used. Otherwise,
250 * sockets may hang open for quite a long time before the server terminates them.
251 */
252 destroy(): void;
253 }
254
255 const METHODS: string[];
256
257 const STATUS_CODES: {
258 [errorCode: number]: string | undefined;
259 [errorCode: string]: string | undefined;
260 };
261
262 function createServer(requestListener?: RequestListener): Server;
263 function createServer(options: ServerOptions, requestListener?: RequestListener): Server;
264
265 // although RequestOptions are passed as ClientRequestArgs to ClientRequest directly,
266 // create interface RequestOptions would make the naming more clear to developers
267 interface RequestOptions extends ClientRequestArgs { }
268 function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
269 function request(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
270 function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
271 function get(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
272 let globalAgent: Agent;
273
274 /**
275 * Read-only property specifying the maximum allowed size of HTTP headers in bytes.
276 * Defaults to 8KB. Configurable using the [`--max-http-header-size`][] CLI option.
277 */
278 const maxHeaderSize: number;
279}