Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

http.d.ts 10KB

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