UNPKG

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