UNPKG

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