@types/node 16.4.1 → 16.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- node/README.md +2 -2
- node/assert/strict.d.ts +0 -1
- node/assert.d.ts +1352 -40
- node/async_hooks.d.ts +359 -90
- node/buffer.d.ts +1503 -78
- node/child_process.d.ts +1054 -233
- node/cluster.d.ts +320 -100
- node/console.d.ts +305 -32
- node/crypto.d.ts +3115 -739
- node/dgram.d.ts +446 -55
- node/diagnostics_channel.d.ts +85 -12
- node/dns/promises.d.ts +292 -36
- node/dns.d.ts +410 -97
- node/domain.d.ts +154 -10
- node/events.d.ts +377 -31
- node/fs/promises.d.ts +697 -273
- node/fs.d.ts +2257 -858
- node/http.d.ts +889 -81
- node/http2.d.ts +1520 -459
- node/https.d.ts +261 -11
- node/index.d.ts +25 -0
- node/inspector.d.ts +354 -661
- node/module.d.ts +49 -11
- node/net.d.ts +548 -140
- node/os.d.ts +236 -26
- node/package.json +7 -2
- node/path.d.ts +9 -5
- node/perf_hooks.d.ts +288 -90
- node/process.d.ts +1092 -153
- node/punycode.d.ts +65 -26
- node/querystring.d.ts +107 -8
- node/readline.d.ts +425 -79
- node/repl.d.ts +135 -110
- node/stream/promises.d.ts +15 -44
- node/stream.d.ts +927 -225
- node/string_decoder.d.ts +57 -1
- node/timers/promises.d.ts +97 -9
- node/timers.d.ts +29 -10
- node/tls.d.ts +444 -221
- node/trace_events.d.ts +107 -11
- node/tty.d.ts +163 -23
- node/url.d.ts +739 -29
- node/util.d.ts +1361 -73
- node/v8.d.ts +254 -78
- node/vm.d.ts +381 -33
- node/wasi.d.ts +107 -24
- node/worker_threads.d.ts +494 -131
- node/zlib.d.ts +215 -63
node/http.d.ts
CHANGED
|
@@ -1,11 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* To use the HTTP server and client one must `require('http')`.
|
|
3
|
+
*
|
|
4
|
+
* The HTTP interfaces in Node.js are designed to support many features
|
|
5
|
+
* of the protocol which have been traditionally difficult to use.
|
|
6
|
+
* In particular, large, possibly chunk-encoded, messages. The interface is
|
|
7
|
+
* careful to never buffer entire requests or responses, so the
|
|
8
|
+
* user is able to stream data.
|
|
9
|
+
*
|
|
10
|
+
* HTTP message headers are represented by an object like this:
|
|
11
|
+
*
|
|
12
|
+
* ```js
|
|
13
|
+
* { 'content-length': '123',
|
|
14
|
+
* 'content-type': 'text/plain',
|
|
15
|
+
* 'connection': 'keep-alive',
|
|
16
|
+
* 'host': 'mysite.com',
|
|
17
|
+
* 'accept': '*' }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Keys are lowercased. Values are not modified.
|
|
21
|
+
*
|
|
22
|
+
* In order to support the full spectrum of possible HTTP applications, the Node.js
|
|
23
|
+
* HTTP API is very low-level. It deals with stream handling and message
|
|
24
|
+
* parsing only. It parses a message into headers and body but it does not
|
|
25
|
+
* parse the actual headers or the body.
|
|
26
|
+
*
|
|
27
|
+
* See `message.headers` for details on how duplicate headers are handled.
|
|
28
|
+
*
|
|
29
|
+
* The raw headers as they were received are retained in the `rawHeaders`property, which is an array of `[key, value, key2, value2, ...]`. For
|
|
30
|
+
* example, the previous message header object might have a `rawHeaders`list like the following:
|
|
31
|
+
*
|
|
32
|
+
* ```js
|
|
33
|
+
* [ 'ConTent-Length', '123456',
|
|
34
|
+
* 'content-LENGTH', '123',
|
|
35
|
+
* 'content-type', 'text/plain',
|
|
36
|
+
* 'CONNECTION', 'keep-alive',
|
|
37
|
+
* 'Host', 'mysite.com',
|
|
38
|
+
* 'accepT', '*' ]
|
|
39
|
+
* ```
|
|
40
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/http.js)
|
|
41
|
+
*/
|
|
1
42
|
declare module 'http' {
|
|
2
43
|
import * as stream from 'node:stream';
|
|
3
44
|
import { URL } from 'node:url';
|
|
4
45
|
import { Socket, Server as NetServer } from 'node:net';
|
|
5
|
-
|
|
6
46
|
// incoming headers will never contain number
|
|
7
47
|
interface IncomingHttpHeaders extends NodeJS.Dict<string | string[]> {
|
|
8
|
-
|
|
48
|
+
accept?: string | undefined;
|
|
9
49
|
'accept-language'?: string | undefined;
|
|
10
50
|
'accept-patch'?: string | undefined;
|
|
11
51
|
'accept-ranges'?: string | undefined;
|
|
@@ -17,12 +57,12 @@ declare module 'http' {
|
|
|
17
57
|
'access-control-max-age'?: string | undefined;
|
|
18
58
|
'access-control-request-headers'?: string | undefined;
|
|
19
59
|
'access-control-request-method'?: string | undefined;
|
|
20
|
-
|
|
21
|
-
|
|
60
|
+
age?: string | undefined;
|
|
61
|
+
allow?: string | undefined;
|
|
22
62
|
'alt-svc'?: string | undefined;
|
|
23
|
-
|
|
63
|
+
authorization?: string | undefined;
|
|
24
64
|
'cache-control'?: string | undefined;
|
|
25
|
-
|
|
65
|
+
connection?: string | undefined;
|
|
26
66
|
'content-disposition'?: string | undefined;
|
|
27
67
|
'content-encoding'?: string | undefined;
|
|
28
68
|
'content-language'?: string | undefined;
|
|
@@ -30,27 +70,27 @@ declare module 'http' {
|
|
|
30
70
|
'content-location'?: string | undefined;
|
|
31
71
|
'content-range'?: string | undefined;
|
|
32
72
|
'content-type'?: string | undefined;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
73
|
+
cookie?: string | undefined;
|
|
74
|
+
date?: string | undefined;
|
|
75
|
+
etag?: string | undefined;
|
|
76
|
+
expect?: string | undefined;
|
|
77
|
+
expires?: string | undefined;
|
|
78
|
+
forwarded?: string | undefined;
|
|
79
|
+
from?: string | undefined;
|
|
80
|
+
host?: string | undefined;
|
|
41
81
|
'if-match'?: string | undefined;
|
|
42
82
|
'if-modified-since'?: string | undefined;
|
|
43
83
|
'if-none-match'?: string | undefined;
|
|
44
84
|
'if-unmodified-since'?: string | undefined;
|
|
45
85
|
'last-modified'?: string | undefined;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
86
|
+
location?: string | undefined;
|
|
87
|
+
origin?: string | undefined;
|
|
88
|
+
pragma?: string | undefined;
|
|
49
89
|
'proxy-authenticate'?: string | undefined;
|
|
50
90
|
'proxy-authorization'?: string | undefined;
|
|
51
91
|
'public-key-pins'?: string | undefined;
|
|
52
|
-
|
|
53
|
-
|
|
92
|
+
range?: string | undefined;
|
|
93
|
+
referer?: string | undefined;
|
|
54
94
|
'retry-after'?: string | undefined;
|
|
55
95
|
'sec-websocket-accept'?: string | undefined;
|
|
56
96
|
'sec-websocket-extensions'?: string | undefined;
|
|
@@ -59,23 +99,19 @@ declare module 'http' {
|
|
|
59
99
|
'sec-websocket-version'?: string | undefined;
|
|
60
100
|
'set-cookie'?: string[] | undefined;
|
|
61
101
|
'strict-transport-security'?: string | undefined;
|
|
62
|
-
|
|
63
|
-
|
|
102
|
+
tk?: string | undefined;
|
|
103
|
+
trailer?: string | undefined;
|
|
64
104
|
'transfer-encoding'?: string | undefined;
|
|
65
|
-
|
|
105
|
+
upgrade?: string | undefined;
|
|
66
106
|
'user-agent'?: string | undefined;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
107
|
+
vary?: string | undefined;
|
|
108
|
+
via?: string | undefined;
|
|
109
|
+
warning?: string | undefined;
|
|
70
110
|
'www-authenticate'?: string | undefined;
|
|
71
111
|
}
|
|
72
|
-
|
|
73
112
|
// outgoing headers allows numbers (as they are converted internally to strings)
|
|
74
113
|
type OutgoingHttpHeader = number | string | string[];
|
|
75
|
-
|
|
76
|
-
interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {
|
|
77
|
-
}
|
|
78
|
-
|
|
114
|
+
interface OutgoingHttpHeaders extends NodeJS.Dict<OutgoingHttpHeader> {}
|
|
79
115
|
interface ClientRequestArgs {
|
|
80
116
|
abort?: AbortSignal | undefined;
|
|
81
117
|
protocol?: string | null | undefined;
|
|
@@ -101,7 +137,6 @@ declare module 'http' {
|
|
|
101
137
|
// https://github.com/nodejs/node/blob/master/lib/_http_client.js#L278
|
|
102
138
|
createConnection?: ((options: ClientRequestArgs, oncreate: (err: Error, socket: Socket) => void) => Socket) | undefined;
|
|
103
139
|
}
|
|
104
|
-
|
|
105
140
|
interface ServerOptions {
|
|
106
141
|
IncomingMessage?: typeof IncomingMessage | undefined;
|
|
107
142
|
ServerResponse?: typeof ServerResponse | undefined;
|
|
@@ -120,9 +155,7 @@ declare module 'http' {
|
|
|
120
155
|
*/
|
|
121
156
|
insecureHTTPParser?: boolean | undefined;
|
|
122
157
|
}
|
|
123
|
-
|
|
124
158
|
type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
|
|
125
|
-
|
|
126
159
|
interface HttpBase {
|
|
127
160
|
setTimeout(msecs?: number, callback?: () => void): this;
|
|
128
161
|
setTimeout(callback: () => void): this;
|
|
@@ -147,17 +180,22 @@ declare module 'http' {
|
|
|
147
180
|
*/
|
|
148
181
|
requestTimeout: number;
|
|
149
182
|
}
|
|
150
|
-
|
|
151
183
|
interface Server extends HttpBase {}
|
|
184
|
+
/**
|
|
185
|
+
* * Extends: `<net.Server>`
|
|
186
|
+
* @since v0.1.17
|
|
187
|
+
*/
|
|
152
188
|
class Server extends NetServer {
|
|
153
189
|
constructor(requestListener?: RequestListener);
|
|
154
190
|
constructor(options: ServerOptions, requestListener?: RequestListener);
|
|
155
191
|
}
|
|
156
|
-
|
|
157
|
-
|
|
192
|
+
/**
|
|
193
|
+
* This class serves as the parent class of {@link ClientRequest} and {@link ServerResponse}. It is an abstract of outgoing message from
|
|
194
|
+
* the perspective of the participants of HTTP transaction.
|
|
195
|
+
* @since v0.1.17
|
|
196
|
+
*/
|
|
158
197
|
class OutgoingMessage extends stream.Writable {
|
|
159
198
|
readonly req: IncomingMessage;
|
|
160
|
-
|
|
161
199
|
chunkedEncoding: boolean;
|
|
162
200
|
shouldKeepAlive: boolean;
|
|
163
201
|
useChunkedEncodingByDefault: boolean;
|
|
@@ -166,43 +204,241 @@ declare module 'http' {
|
|
|
166
204
|
* @deprecated Use `writableEnded` instead.
|
|
167
205
|
*/
|
|
168
206
|
finished: boolean;
|
|
207
|
+
/**
|
|
208
|
+
* Read-only. `true` if the headers were sent, otherwise `false`.
|
|
209
|
+
* @since v0.9.3
|
|
210
|
+
*/
|
|
169
211
|
readonly headersSent: boolean;
|
|
170
212
|
/**
|
|
171
|
-
*
|
|
213
|
+
* Aliases of `outgoingMessage.socket`
|
|
214
|
+
* @since v0.3.0
|
|
215
|
+
* @deprecated Since v15.12.0 - Use `socket` instead.
|
|
172
216
|
*/
|
|
173
217
|
readonly connection: Socket | null;
|
|
218
|
+
/**
|
|
219
|
+
* Reference to the underlying socket. Usually, users will not want to access
|
|
220
|
+
* this property.
|
|
221
|
+
*
|
|
222
|
+
* After calling `outgoingMessage.end()`, this property will be nulled.
|
|
223
|
+
* @since v0.3.0
|
|
224
|
+
*/
|
|
174
225
|
readonly socket: Socket | null;
|
|
175
|
-
|
|
176
226
|
constructor();
|
|
177
|
-
|
|
227
|
+
/**
|
|
228
|
+
* occurs, Same as binding to the `timeout` event.
|
|
229
|
+
*
|
|
230
|
+
* Once a socket is associated with the message and is connected,`socket.setTimeout()` will be called with `msecs` as the first parameter.
|
|
231
|
+
* @since v0.9.12
|
|
232
|
+
* @param callback Optional function to be called when a timeout
|
|
233
|
+
*/
|
|
178
234
|
setTimeout(msecs: number, callback?: () => void): this;
|
|
235
|
+
/**
|
|
236
|
+
* Sets a single header value for the header object.
|
|
237
|
+
* @since v0.4.0
|
|
238
|
+
* @param name Header name
|
|
239
|
+
* @param value Header value
|
|
240
|
+
*/
|
|
179
241
|
setHeader(name: string, value: number | string | ReadonlyArray<string>): this;
|
|
242
|
+
/**
|
|
243
|
+
* Gets the value of HTTP header with the given name. If such a name doesn't
|
|
244
|
+
* exist in message, it will be `undefined`.
|
|
245
|
+
* @since v0.4.0
|
|
246
|
+
* @param name Name of header
|
|
247
|
+
*/
|
|
180
248
|
getHeader(name: string): number | string | string[] | undefined;
|
|
249
|
+
/**
|
|
250
|
+
* Returns a shallow copy of the current outgoing headers. Since a shallow
|
|
251
|
+
* copy is used, array values may be mutated without additional calls to
|
|
252
|
+
* various header-related HTTP module methods. The keys of the returned
|
|
253
|
+
* object are the header names and the values are the respective header
|
|
254
|
+
* values. All header names are lowercase.
|
|
255
|
+
*
|
|
256
|
+
* The object returned by the `outgoingMessage.getHeaders()` method does
|
|
257
|
+
* not prototypically inherit from the JavaScript Object. This means that
|
|
258
|
+
* typical Object methods such as `obj.toString()`, `obj.hasOwnProperty()`,
|
|
259
|
+
* and others are not defined and will not work.
|
|
260
|
+
*
|
|
261
|
+
* ```js
|
|
262
|
+
* outgoingMessage.setHeader('Foo', 'bar');
|
|
263
|
+
* outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
264
|
+
*
|
|
265
|
+
* const headers = outgoingMessage.getHeaders();
|
|
266
|
+
* // headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
|
|
267
|
+
* ```
|
|
268
|
+
* @since v8.0.0
|
|
269
|
+
*/
|
|
181
270
|
getHeaders(): OutgoingHttpHeaders;
|
|
271
|
+
/**
|
|
272
|
+
* Returns an array of names of headers of the outgoing outgoingMessage. All
|
|
273
|
+
* names are lowercase.
|
|
274
|
+
* @since v8.0.0
|
|
275
|
+
*/
|
|
182
276
|
getHeaderNames(): string[];
|
|
277
|
+
/**
|
|
278
|
+
* Returns `true` if the header identified by `name` is currently set in the
|
|
279
|
+
* outgoing headers. The header name is case-insensitive.
|
|
280
|
+
*
|
|
281
|
+
* ```js
|
|
282
|
+
* const hasContentType = outgoingMessage.hasHeader('content-type');
|
|
283
|
+
* ```
|
|
284
|
+
* @since v8.0.0
|
|
285
|
+
*/
|
|
183
286
|
hasHeader(name: string): boolean;
|
|
287
|
+
/**
|
|
288
|
+
* Removes a header that is queued for implicit sending.
|
|
289
|
+
*
|
|
290
|
+
* ```js
|
|
291
|
+
* outgoingMessage.removeHeader('Content-Encoding');
|
|
292
|
+
* ```
|
|
293
|
+
* @since v0.4.0
|
|
294
|
+
*/
|
|
184
295
|
removeHeader(name: string): void;
|
|
296
|
+
/**
|
|
297
|
+
* Adds HTTP trailers (headers but at the end of the message) to the message.
|
|
298
|
+
*
|
|
299
|
+
* Trailers are **only** be emitted if the message is chunked encoded. If not,
|
|
300
|
+
* the trailer will be silently discarded.
|
|
301
|
+
*
|
|
302
|
+
* HTTP requires the `Trailer` header to be sent to emit trailers,
|
|
303
|
+
* with a list of header fields in its value, e.g.
|
|
304
|
+
*
|
|
305
|
+
* ```js
|
|
306
|
+
* message.writeHead(200, { 'Content-Type': 'text/plain',
|
|
307
|
+
* 'Trailer': 'Content-MD5' });
|
|
308
|
+
* message.write(fileData);
|
|
309
|
+
* message.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
|
|
310
|
+
* message.end();
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* Attempting to set a header field name or value that contains invalid characters
|
|
314
|
+
* will result in a `TypeError` being thrown.
|
|
315
|
+
* @since v0.3.0
|
|
316
|
+
*/
|
|
185
317
|
addTrailers(headers: OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void;
|
|
318
|
+
/**
|
|
319
|
+
* Compulsorily flushes the message headers
|
|
320
|
+
*
|
|
321
|
+
* For efficiency reason, Node.js normally buffers the message headers
|
|
322
|
+
* until `outgoingMessage.end()` is called or the first chunk of message data
|
|
323
|
+
* is written. It then tries to pack the headers and data into a single TCP
|
|
324
|
+
* packet.
|
|
325
|
+
*
|
|
326
|
+
* It is usually desired (it saves a TCP round-trip), but not when the first
|
|
327
|
+
* data is not sent until possibly much later. `outgoingMessage.flushHeaders()`bypasses the optimization and kickstarts the request.
|
|
328
|
+
* @since v1.6.0
|
|
329
|
+
*/
|
|
186
330
|
flushHeaders(): void;
|
|
187
331
|
}
|
|
188
|
-
|
|
189
|
-
|
|
332
|
+
/**
|
|
333
|
+
* This object is created internally by an HTTP server, not by the user. It is
|
|
334
|
+
* passed as the second parameter to the `'request'` event.
|
|
335
|
+
* @since v0.1.17
|
|
336
|
+
*/
|
|
190
337
|
class ServerResponse extends OutgoingMessage {
|
|
338
|
+
/**
|
|
339
|
+
* When using implicit headers (not calling `response.writeHead()` explicitly),
|
|
340
|
+
* this property controls the status code that will be sent to the client when
|
|
341
|
+
* the headers get flushed.
|
|
342
|
+
*
|
|
343
|
+
* ```js
|
|
344
|
+
* response.statusCode = 404;
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* After response header was sent to the client, this property indicates the
|
|
348
|
+
* status code which was sent out.
|
|
349
|
+
* @since v0.4.0
|
|
350
|
+
*/
|
|
191
351
|
statusCode: number;
|
|
352
|
+
/**
|
|
353
|
+
* When using implicit headers (not calling `response.writeHead()` explicitly),
|
|
354
|
+
* this property controls the status message that will be sent to the client when
|
|
355
|
+
* the headers get flushed. If this is left as `undefined` then the standard
|
|
356
|
+
* message for the status code will be used.
|
|
357
|
+
*
|
|
358
|
+
* ```js
|
|
359
|
+
* response.statusMessage = 'Not found';
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* After response header was sent to the client, this property indicates the
|
|
363
|
+
* status message which was sent out.
|
|
364
|
+
* @since v0.11.8
|
|
365
|
+
*/
|
|
192
366
|
statusMessage: string;
|
|
193
|
-
|
|
194
367
|
constructor(req: IncomingMessage);
|
|
195
|
-
|
|
196
368
|
assignSocket(socket: Socket): void;
|
|
197
369
|
detachSocket(socket: Socket): void;
|
|
198
|
-
|
|
199
|
-
|
|
370
|
+
/**
|
|
371
|
+
* Sends a HTTP/1.1 100 Continue message to the client, indicating that
|
|
372
|
+
* the request body should be sent. See the `'checkContinue'` event on`Server`.
|
|
373
|
+
* @since v0.3.0
|
|
374
|
+
*/
|
|
200
375
|
writeContinue(callback?: () => void): void;
|
|
201
|
-
|
|
376
|
+
/**
|
|
377
|
+
* Sends a response header to the request. The status code is a 3-digit HTTP
|
|
378
|
+
* status code, like `404`. The last argument, `headers`, are the response headers.
|
|
379
|
+
* Optionally one can give a human-readable `statusMessage` as the second
|
|
380
|
+
* argument.
|
|
381
|
+
*
|
|
382
|
+
* `headers` may be an `Array` where the keys and values are in the same list.
|
|
383
|
+
* It is _not_ a list of tuples. So, the even-numbered offsets are key values,
|
|
384
|
+
* and the odd-numbered offsets are the associated values. The array is in the same
|
|
385
|
+
* format as `request.rawHeaders`.
|
|
386
|
+
*
|
|
387
|
+
* Returns a reference to the `ServerResponse`, so that calls can be chained.
|
|
388
|
+
*
|
|
389
|
+
* ```js
|
|
390
|
+
* const body = 'hello world';
|
|
391
|
+
* response
|
|
392
|
+
* .writeHead(200, {
|
|
393
|
+
* 'Content-Length': Buffer.byteLength(body),
|
|
394
|
+
* 'Content-Type': 'text/plain'
|
|
395
|
+
* })
|
|
396
|
+
* .end(body);
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* This method must only be called once on a message and it must
|
|
400
|
+
* be called before `response.end()` is called.
|
|
401
|
+
*
|
|
402
|
+
* If `response.write()` or `response.end()` are called before calling
|
|
403
|
+
* this, the implicit/mutable headers will be calculated and call this function.
|
|
404
|
+
*
|
|
405
|
+
* When headers have been set with `response.setHeader()`, they will be merged
|
|
406
|
+
* with any headers passed to `response.writeHead()`, with the headers passed
|
|
407
|
+
* to `response.writeHead()` given precedence.
|
|
408
|
+
*
|
|
409
|
+
* If this method is called and `response.setHeader()` has not been called,
|
|
410
|
+
* it will directly write the supplied header values onto the network channel
|
|
411
|
+
* without caching internally, and the `response.getHeader()` on the header
|
|
412
|
+
* will not yield the expected result. If progressive population of headers is
|
|
413
|
+
* desired with potential future retrieval and modification, use `response.setHeader()` instead.
|
|
414
|
+
*
|
|
415
|
+
* ```js
|
|
416
|
+
* // Returns content-type = text/plain
|
|
417
|
+
* const server = http.createServer((req, res) => {
|
|
418
|
+
* res.setHeader('Content-Type', 'text/html');
|
|
419
|
+
* res.setHeader('X-Foo', 'bar');
|
|
420
|
+
* res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
421
|
+
* res.end('ok');
|
|
422
|
+
* });
|
|
423
|
+
* ```
|
|
424
|
+
*
|
|
425
|
+
* `Content-Length` is given in bytes, not characters. Use `Buffer.byteLength()` to determine the length of the body in bytes. Node.js
|
|
426
|
+
* does not check whether `Content-Length` and the length of the body which has
|
|
427
|
+
* been transmitted are equal or not.
|
|
428
|
+
*
|
|
429
|
+
* Attempting to set a header field name or value that contains invalid characters
|
|
430
|
+
* will result in a `TypeError` being thrown.
|
|
431
|
+
* @since v0.1.30
|
|
432
|
+
*/
|
|
433
|
+
writeHead(statusCode: number, statusMessage?: string, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
|
|
202
434
|
writeHead(statusCode: number, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
|
|
435
|
+
/**
|
|
436
|
+
* Sends a HTTP/1.1 102 Processing message to the client, indicating that
|
|
437
|
+
* the request body should be sent.
|
|
438
|
+
* @since v10.0.0
|
|
439
|
+
*/
|
|
203
440
|
writeProcessing(): void;
|
|
204
441
|
}
|
|
205
|
-
|
|
206
442
|
interface InformationEvent {
|
|
207
443
|
statusCode: number;
|
|
208
444
|
statusMessage: string;
|
|
@@ -212,29 +448,101 @@ declare module 'http' {
|
|
|
212
448
|
headers: IncomingHttpHeaders;
|
|
213
449
|
rawHeaders: string[];
|
|
214
450
|
}
|
|
215
|
-
|
|
216
|
-
|
|
451
|
+
/**
|
|
452
|
+
* This object is created internally and returned from {@link request}. It
|
|
453
|
+
* represents an _in-progress_ request whose header has already been queued. The
|
|
454
|
+
* header is still mutable using the `setHeader(name, value)`,`getHeader(name)`, `removeHeader(name)` API. The actual header will
|
|
455
|
+
* be sent along with the first data chunk or when calling `request.end()`.
|
|
456
|
+
*
|
|
457
|
+
* To get the response, add a listener for `'response'` to the request object.`'response'` will be emitted from the request object when the response
|
|
458
|
+
* headers have been received. The `'response'` event is executed with one
|
|
459
|
+
* argument which is an instance of {@link IncomingMessage}.
|
|
460
|
+
*
|
|
461
|
+
* During the `'response'` event, one can add listeners to the
|
|
462
|
+
* response object; particularly to listen for the `'data'` event.
|
|
463
|
+
*
|
|
464
|
+
* If no `'response'` handler is added, then the response will be
|
|
465
|
+
* entirely discarded. However, if a `'response'` event handler is added,
|
|
466
|
+
* then the data from the response object **must** be consumed, either by
|
|
467
|
+
* calling `response.read()` whenever there is a `'readable'` event, or
|
|
468
|
+
* by adding a `'data'` handler, or by calling the `.resume()` method.
|
|
469
|
+
* Until the data is consumed, the `'end'` event will not fire. Also, until
|
|
470
|
+
* the data is read it will consume memory that can eventually lead to a
|
|
471
|
+
* 'process out of memory' error.
|
|
472
|
+
*
|
|
473
|
+
* For backward compatibility, `res` will only emit `'error'` if there is an`'error'` listener registered.
|
|
474
|
+
*
|
|
475
|
+
* Node.js does not check whether Content-Length and the length of the
|
|
476
|
+
* body which has been transmitted are equal or not.
|
|
477
|
+
* @since v0.1.17
|
|
478
|
+
*/
|
|
217
479
|
class ClientRequest extends OutgoingMessage {
|
|
480
|
+
/**
|
|
481
|
+
* The `request.aborted` property will be `true` if the request has
|
|
482
|
+
* been aborted.
|
|
483
|
+
* @since v0.11.14
|
|
484
|
+
*/
|
|
218
485
|
aborted: boolean;
|
|
486
|
+
/**
|
|
487
|
+
* The request host.
|
|
488
|
+
* @since v14.5.0, v12.19.0
|
|
489
|
+
*/
|
|
219
490
|
host: string;
|
|
491
|
+
/**
|
|
492
|
+
* The request protocol.
|
|
493
|
+
* @since v14.5.0, v12.19.0
|
|
494
|
+
*/
|
|
220
495
|
protocol: string;
|
|
221
|
-
|
|
222
496
|
constructor(url: string | URL | ClientRequestArgs, cb?: (res: IncomingMessage) => void);
|
|
223
|
-
|
|
497
|
+
/**
|
|
498
|
+
* The request method.
|
|
499
|
+
* @since v0.1.97
|
|
500
|
+
*/
|
|
224
501
|
method: string;
|
|
502
|
+
/**
|
|
503
|
+
* The request path.
|
|
504
|
+
* @since v0.4.0
|
|
505
|
+
*/
|
|
225
506
|
path: string;
|
|
226
|
-
/**
|
|
507
|
+
/**
|
|
508
|
+
* Marks the request as aborting. Calling this will cause remaining data
|
|
509
|
+
* in the response to be dropped and the socket to be destroyed.
|
|
510
|
+
* @since v0.3.8
|
|
511
|
+
* @deprecated Since v14.1.0,v13.14.0 - Use `destroy` instead.
|
|
512
|
+
*/
|
|
227
513
|
abort(): void;
|
|
228
514
|
onSocket(socket: Socket): void;
|
|
515
|
+
/**
|
|
516
|
+
* Once a socket is assigned to this request and is connected `socket.setTimeout()` will be called.
|
|
517
|
+
* @since v0.5.9
|
|
518
|
+
* @param timeout Milliseconds before a request times out.
|
|
519
|
+
* @param callback Optional function to be called when a timeout occurs. Same as binding to the `'timeout'` event.
|
|
520
|
+
*/
|
|
229
521
|
setTimeout(timeout: number, callback?: () => void): this;
|
|
522
|
+
/**
|
|
523
|
+
* Once a socket is assigned to this request and is connected `socket.setNoDelay()` will be called.
|
|
524
|
+
* @since v0.5.9
|
|
525
|
+
*/
|
|
230
526
|
setNoDelay(noDelay?: boolean): void;
|
|
527
|
+
/**
|
|
528
|
+
* Once a socket is assigned to this request and is connected `socket.setKeepAlive()` will be called.
|
|
529
|
+
* @since v0.5.9
|
|
530
|
+
*/
|
|
231
531
|
setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
|
|
232
532
|
/**
|
|
233
|
-
* Returns an array containing the unique names of the current outgoing raw
|
|
234
|
-
* Header names are returned with their exact casing being set.
|
|
533
|
+
* Returns an array containing the unique names of the current outgoing raw
|
|
534
|
+
* headers. Header names are returned with their exact casing being set.
|
|
535
|
+
*
|
|
536
|
+
* ```js
|
|
537
|
+
* request.setHeader('Foo', 'bar');
|
|
538
|
+
* request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
539
|
+
*
|
|
540
|
+
* const headerNames = request.getRawHeaderNames();
|
|
541
|
+
* // headerNames === ['Foo', 'Set-Cookie']
|
|
542
|
+
* ```
|
|
543
|
+
* @since v15.13.0
|
|
235
544
|
*/
|
|
236
545
|
getRawHeaderNames(): string[];
|
|
237
|
-
|
|
238
546
|
addListener(event: 'abort', listener: () => void): this;
|
|
239
547
|
addListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
|
|
240
548
|
addListener(event: 'continue', listener: () => void): this;
|
|
@@ -250,7 +558,6 @@ declare module 'http' {
|
|
|
250
558
|
addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
|
251
559
|
addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
|
252
560
|
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
253
|
-
|
|
254
561
|
on(event: 'abort', listener: () => void): this;
|
|
255
562
|
on(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
|
|
256
563
|
on(event: 'continue', listener: () => void): this;
|
|
@@ -266,7 +573,6 @@ declare module 'http' {
|
|
|
266
573
|
on(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
|
267
574
|
on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
|
268
575
|
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
269
|
-
|
|
270
576
|
once(event: 'abort', listener: () => void): this;
|
|
271
577
|
once(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
|
|
272
578
|
once(event: 'continue', listener: () => void): this;
|
|
@@ -282,7 +588,6 @@ declare module 'http' {
|
|
|
282
588
|
once(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
|
283
589
|
once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
|
284
590
|
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
285
|
-
|
|
286
591
|
prependListener(event: 'abort', listener: () => void): this;
|
|
287
592
|
prependListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
|
|
288
593
|
prependListener(event: 'continue', listener: () => void): this;
|
|
@@ -298,7 +603,6 @@ declare module 'http' {
|
|
|
298
603
|
prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
|
299
604
|
prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
|
300
605
|
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
301
|
-
|
|
302
606
|
prependOnceListener(event: 'abort', listener: () => void): this;
|
|
303
607
|
prependOnceListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
|
|
304
608
|
prependOnceListener(event: 'continue', listener: () => void): this;
|
|
@@ -315,44 +619,213 @@ declare module 'http' {
|
|
|
315
619
|
prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
|
316
620
|
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
317
621
|
}
|
|
318
|
-
|
|
622
|
+
/**
|
|
623
|
+
* * Extends: `<stream.Readable>`
|
|
624
|
+
*
|
|
625
|
+
* An `IncomingMessage` object is created by {@link Server} or {@link ClientRequest} and passed as the first argument to the `'request'` and `'response'` event respectively. It may be used to
|
|
626
|
+
* access response
|
|
627
|
+
* status, headers and data.
|
|
628
|
+
*
|
|
629
|
+
* Different from its `socket` value which is a subclass of `<stream.Duplex>`, the`IncomingMessage` itself extends `<stream.Readable>` and is created separately to
|
|
630
|
+
* parse and emit the incoming HTTP headers and payload, as the underlying socket
|
|
631
|
+
* may be reused multiple times in case of keep-alive.
|
|
632
|
+
* @since v0.1.17
|
|
633
|
+
*/
|
|
319
634
|
class IncomingMessage extends stream.Readable {
|
|
320
635
|
constructor(socket: Socket);
|
|
321
|
-
|
|
636
|
+
/**
|
|
637
|
+
* The `message.aborted` property will be `true` if the request has
|
|
638
|
+
* been aborted.
|
|
639
|
+
* @since v10.1.0
|
|
640
|
+
*/
|
|
322
641
|
aborted: boolean;
|
|
642
|
+
/**
|
|
643
|
+
* In case of server request, the HTTP version sent by the client. In the case of
|
|
644
|
+
* client response, the HTTP version of the connected-to server.
|
|
645
|
+
* Probably either `'1.1'` or `'1.0'`.
|
|
646
|
+
*
|
|
647
|
+
* Also `message.httpVersionMajor` is the first integer and`message.httpVersionMinor` is the second.
|
|
648
|
+
* @since v0.1.1
|
|
649
|
+
*/
|
|
323
650
|
httpVersion: string;
|
|
324
651
|
httpVersionMajor: number;
|
|
325
652
|
httpVersionMinor: number;
|
|
653
|
+
/**
|
|
654
|
+
* The `message.complete` property will be `true` if a complete HTTP message has
|
|
655
|
+
* been received and successfully parsed.
|
|
656
|
+
*
|
|
657
|
+
* This property is particularly useful as a means of determining if a client or
|
|
658
|
+
* server fully transmitted a message before a connection was terminated:
|
|
659
|
+
*
|
|
660
|
+
* ```js
|
|
661
|
+
* const req = http.request({
|
|
662
|
+
* host: '127.0.0.1',
|
|
663
|
+
* port: 8080,
|
|
664
|
+
* method: 'POST'
|
|
665
|
+
* }, (res) => {
|
|
666
|
+
* res.resume();
|
|
667
|
+
* res.on('end', () => {
|
|
668
|
+
* if (!res.complete)
|
|
669
|
+
* console.error(
|
|
670
|
+
* 'The connection was terminated while the message was still being sent');
|
|
671
|
+
* });
|
|
672
|
+
* });
|
|
673
|
+
* ```
|
|
674
|
+
* @since v0.3.0
|
|
675
|
+
*/
|
|
326
676
|
complete: boolean;
|
|
327
677
|
/**
|
|
328
|
-
*
|
|
678
|
+
* Alias for `message.socket`.
|
|
679
|
+
* @since v0.1.90
|
|
680
|
+
* @deprecated Since v16.0.0 - Deprecated. Use `socket`.
|
|
329
681
|
*/
|
|
330
682
|
connection: Socket;
|
|
683
|
+
/**
|
|
684
|
+
* The `net.Socket` object associated with the connection.
|
|
685
|
+
*
|
|
686
|
+
* With HTTPS support, use `request.socket.getPeerCertificate()` to obtain the
|
|
687
|
+
* client's authentication details.
|
|
688
|
+
*
|
|
689
|
+
* This property is guaranteed to be an instance of the `<net.Socket>` class,
|
|
690
|
+
* a subclass of `<stream.Duplex>`, unless the user specified a socket
|
|
691
|
+
* type other than `<net.Socket>`.
|
|
692
|
+
* @since v0.3.0
|
|
693
|
+
*/
|
|
331
694
|
socket: Socket;
|
|
695
|
+
/**
|
|
696
|
+
* The request/response headers object.
|
|
697
|
+
*
|
|
698
|
+
* Key-value pairs of header names and values. Header names are lower-cased.
|
|
699
|
+
*
|
|
700
|
+
* ```js
|
|
701
|
+
* // Prints something like:
|
|
702
|
+
* //
|
|
703
|
+
* // { 'user-agent': 'curl/7.22.0',
|
|
704
|
+
* // host: '127.0.0.1:8000',
|
|
705
|
+
* // accept: '*' }
|
|
706
|
+
* console.log(request.headers);
|
|
707
|
+
* ```
|
|
708
|
+
*
|
|
709
|
+
* Duplicates in raw headers are handled in the following ways, depending on the
|
|
710
|
+
* header name:
|
|
711
|
+
*
|
|
712
|
+
* * Duplicates of `age`, `authorization`, `content-length`, `content-type`,`etag`, `expires`, `from`, `host`, `if-modified-since`, `if-unmodified-since`,`last-modified`, `location`,
|
|
713
|
+
* `max-forwards`, `proxy-authorization`, `referer`,`retry-after`, `server`, or `user-agent` are discarded.
|
|
714
|
+
* * `set-cookie` is always an array. Duplicates are added to the array.
|
|
715
|
+
* * For duplicate `cookie` headers, the values are joined together with '; '.
|
|
716
|
+
* * For all other headers, the values are joined together with ', '.
|
|
717
|
+
* @since v0.1.5
|
|
718
|
+
*/
|
|
332
719
|
headers: IncomingHttpHeaders;
|
|
720
|
+
/**
|
|
721
|
+
* The raw request/response headers list exactly as they were received.
|
|
722
|
+
*
|
|
723
|
+
* The keys and values are in the same list. It is _not_ a
|
|
724
|
+
* list of tuples. So, the even-numbered offsets are key values, and the
|
|
725
|
+
* odd-numbered offsets are the associated values.
|
|
726
|
+
*
|
|
727
|
+
* Header names are not lowercased, and duplicates are not merged.
|
|
728
|
+
*
|
|
729
|
+
* ```js
|
|
730
|
+
* // Prints something like:
|
|
731
|
+
* //
|
|
732
|
+
* // [ 'user-agent',
|
|
733
|
+
* // 'this is invalid because there can be only one',
|
|
734
|
+
* // 'User-Agent',
|
|
735
|
+
* // 'curl/7.22.0',
|
|
736
|
+
* // 'Host',
|
|
737
|
+
* // '127.0.0.1:8000',
|
|
738
|
+
* // 'ACCEPT',
|
|
739
|
+
* // '*' ]
|
|
740
|
+
* console.log(request.rawHeaders);
|
|
741
|
+
* ```
|
|
742
|
+
* @since v0.11.6
|
|
743
|
+
*/
|
|
333
744
|
rawHeaders: string[];
|
|
745
|
+
/**
|
|
746
|
+
* The request/response trailers object. Only populated at the `'end'` event.
|
|
747
|
+
* @since v0.3.0
|
|
748
|
+
*/
|
|
334
749
|
trailers: NodeJS.Dict<string>;
|
|
750
|
+
/**
|
|
751
|
+
* The raw request/response trailer keys and values exactly as they were
|
|
752
|
+
* received. Only populated at the `'end'` event.
|
|
753
|
+
* @since v0.11.6
|
|
754
|
+
*/
|
|
335
755
|
rawTrailers: string[];
|
|
756
|
+
/**
|
|
757
|
+
* Calls `message.socket.setTimeout(msecs, callback)`.
|
|
758
|
+
* @since v0.5.9
|
|
759
|
+
*/
|
|
336
760
|
setTimeout(msecs: number, callback?: () => void): this;
|
|
337
761
|
/**
|
|
338
|
-
* Only valid for request obtained from
|
|
762
|
+
* **Only valid for request obtained from {@link Server}.**
|
|
763
|
+
*
|
|
764
|
+
* The request method as a string. Read only. Examples: `'GET'`, `'DELETE'`.
|
|
765
|
+
* @since v0.1.1
|
|
339
766
|
*/
|
|
340
767
|
method?: string | undefined;
|
|
341
768
|
/**
|
|
342
|
-
* Only valid for request obtained from
|
|
769
|
+
* **Only valid for request obtained from {@link Server}.**
|
|
770
|
+
*
|
|
771
|
+
* Request URL string. This contains only the URL that is present in the actual
|
|
772
|
+
* HTTP request. Take the following request:
|
|
773
|
+
*
|
|
774
|
+
* ```http
|
|
775
|
+
* GET /status?name=ryan HTTP/1.1
|
|
776
|
+
* Accept: text/plain
|
|
777
|
+
* ```
|
|
778
|
+
*
|
|
779
|
+
* To parse the URL into its parts:
|
|
780
|
+
*
|
|
781
|
+
* ```js
|
|
782
|
+
* new URL(request.url, `http://${request.headers.host}`);
|
|
783
|
+
* ```
|
|
784
|
+
*
|
|
785
|
+
* When `request.url` is `'/status?name=ryan'` and`request.headers.host` is `'localhost:3000'`:
|
|
786
|
+
*
|
|
787
|
+
* ```console
|
|
788
|
+
* $ node
|
|
789
|
+
* > new URL(request.url, `http://${request.headers.host}`)
|
|
790
|
+
* URL {
|
|
791
|
+
* href: 'http://localhost:3000/status?name=ryan',
|
|
792
|
+
* origin: 'http://localhost:3000',
|
|
793
|
+
* protocol: 'http:',
|
|
794
|
+
* username: '',
|
|
795
|
+
* password: '',
|
|
796
|
+
* host: 'localhost:3000',
|
|
797
|
+
* hostname: 'localhost',
|
|
798
|
+
* port: '3000',
|
|
799
|
+
* pathname: '/status',
|
|
800
|
+
* search: '?name=ryan',
|
|
801
|
+
* searchParams: URLSearchParams { 'name' => 'ryan' },
|
|
802
|
+
* hash: ''
|
|
803
|
+
* }
|
|
804
|
+
* ```
|
|
805
|
+
* @since v0.1.90
|
|
343
806
|
*/
|
|
344
807
|
url?: string | undefined;
|
|
345
808
|
/**
|
|
346
|
-
* Only valid for response obtained from
|
|
809
|
+
* **Only valid for response obtained from {@link ClientRequest}.**
|
|
810
|
+
*
|
|
811
|
+
* The 3-digit HTTP response status code. E.G. `404`.
|
|
812
|
+
* @since v0.1.1
|
|
347
813
|
*/
|
|
348
814
|
statusCode?: number | undefined;
|
|
349
815
|
/**
|
|
350
|
-
* Only valid for response obtained from
|
|
816
|
+
* **Only valid for response obtained from {@link ClientRequest}.**
|
|
817
|
+
*
|
|
818
|
+
* The HTTP response status message (reason phrase). E.G. `OK` or `Internal Server Error`.
|
|
819
|
+
* @since v0.11.10
|
|
351
820
|
*/
|
|
352
821
|
statusMessage?: string | undefined;
|
|
822
|
+
/**
|
|
823
|
+
* Calls `destroy()` on the socket that received the `IncomingMessage`. If `error`is provided, an `'error'` event is emitted on the socket and `error` is passed
|
|
824
|
+
* as an argument to any listeners on the event.
|
|
825
|
+
* @since v0.3.0
|
|
826
|
+
*/
|
|
353
827
|
destroy(error?: Error): void;
|
|
354
828
|
}
|
|
355
|
-
|
|
356
829
|
interface AgentOptions {
|
|
357
830
|
/**
|
|
358
831
|
* Keep sockets around in a pool to be used by other requests in the future. Default = false
|
|
@@ -385,52 +858,387 @@ declare module 'http' {
|
|
|
385
858
|
*/
|
|
386
859
|
scheduling?: 'fifo' | 'lifo' | undefined;
|
|
387
860
|
}
|
|
388
|
-
|
|
861
|
+
/**
|
|
862
|
+
* An `Agent` is responsible for managing connection persistence
|
|
863
|
+
* and reuse for HTTP clients. It maintains a queue of pending requests
|
|
864
|
+
* for a given host and port, reusing a single socket connection for each
|
|
865
|
+
* until the queue is empty, at which time the socket is either destroyed
|
|
866
|
+
* or put into a pool where it is kept to be used again for requests to the
|
|
867
|
+
* same host and port. Whether it is destroyed or pooled depends on the`keepAlive` `option`.
|
|
868
|
+
*
|
|
869
|
+
* Pooled connections have TCP Keep-Alive enabled for them, but servers may
|
|
870
|
+
* still close idle connections, in which case they will be removed from the
|
|
871
|
+
* pool and a new connection will be made when a new HTTP request is made for
|
|
872
|
+
* that host and port. Servers may also refuse to allow multiple requests
|
|
873
|
+
* over the same connection, in which case the connection will have to be
|
|
874
|
+
* remade for every request and cannot be pooled. The `Agent` will still make
|
|
875
|
+
* the requests to that server, but each one will occur over a new connection.
|
|
876
|
+
*
|
|
877
|
+
* When a connection is closed by the client or the server, it is removed
|
|
878
|
+
* from the pool. Any unused sockets in the pool will be unrefed so as not
|
|
879
|
+
* to keep the Node.js process running when there are no outstanding requests.
|
|
880
|
+
* (see `socket.unref()`).
|
|
881
|
+
*
|
|
882
|
+
* It is good practice, to `destroy()` an `Agent` instance when it is no
|
|
883
|
+
* longer in use, because unused sockets consume OS resources.
|
|
884
|
+
*
|
|
885
|
+
* Sockets are removed from an agent when the socket emits either
|
|
886
|
+
* a `'close'` event or an `'agentRemove'` event. When intending to keep one
|
|
887
|
+
* HTTP request open for a long time without keeping it in the agent, something
|
|
888
|
+
* like the following may be done:
|
|
889
|
+
*
|
|
890
|
+
* ```js
|
|
891
|
+
* http.get(options, (res) => {
|
|
892
|
+
* // Do stuff
|
|
893
|
+
* }).on('socket', (socket) => {
|
|
894
|
+
* socket.emit('agentRemove');
|
|
895
|
+
* });
|
|
896
|
+
* ```
|
|
897
|
+
*
|
|
898
|
+
* An agent may also be used for an individual request. By providing`{agent: false}` as an option to the `http.get()` or `http.request()`functions, a one-time use `Agent` with default options
|
|
899
|
+
* will be used
|
|
900
|
+
* for the client connection.
|
|
901
|
+
*
|
|
902
|
+
* `agent:false`:
|
|
903
|
+
*
|
|
904
|
+
* ```js
|
|
905
|
+
* http.get({
|
|
906
|
+
* hostname: 'localhost',
|
|
907
|
+
* port: 80,
|
|
908
|
+
* path: '/',
|
|
909
|
+
* agent: false // Create a new agent just for this one request
|
|
910
|
+
* }, (res) => {
|
|
911
|
+
* // Do stuff with response
|
|
912
|
+
* });
|
|
913
|
+
* ```
|
|
914
|
+
* @since v0.3.4
|
|
915
|
+
*/
|
|
389
916
|
class Agent {
|
|
917
|
+
/**
|
|
918
|
+
* By default set to 256\. For agents with `keepAlive` enabled, this
|
|
919
|
+
* sets the maximum number of sockets that will be left open in the free
|
|
920
|
+
* state.
|
|
921
|
+
* @since v0.11.7
|
|
922
|
+
*/
|
|
390
923
|
maxFreeSockets: number;
|
|
924
|
+
/**
|
|
925
|
+
* By default set to `Infinity`. Determines how many concurrent sockets the agent
|
|
926
|
+
* can have open per origin. Origin is the returned value of `agent.getName()`.
|
|
927
|
+
* @since v0.3.6
|
|
928
|
+
*/
|
|
391
929
|
maxSockets: number;
|
|
930
|
+
/**
|
|
931
|
+
* By default set to `Infinity`. Determines how many concurrent sockets the agent
|
|
932
|
+
* can have open. Unlike `maxSockets`, this parameter applies across all origins.
|
|
933
|
+
* @since v14.5.0, v12.19.0
|
|
934
|
+
*/
|
|
392
935
|
maxTotalSockets: number;
|
|
936
|
+
/**
|
|
937
|
+
* An object which contains arrays of sockets currently awaiting use by
|
|
938
|
+
* the agent when `keepAlive` is enabled. Do not modify.
|
|
939
|
+
*
|
|
940
|
+
* Sockets in the `freeSockets` list will be automatically destroyed and
|
|
941
|
+
* removed from the array on `'timeout'`.
|
|
942
|
+
* @since v0.11.4
|
|
943
|
+
*/
|
|
393
944
|
readonly freeSockets: NodeJS.ReadOnlyDict<Socket[]>;
|
|
945
|
+
/**
|
|
946
|
+
* An object which contains arrays of sockets currently in use by the
|
|
947
|
+
* agent. Do not modify.
|
|
948
|
+
* @since v0.3.6
|
|
949
|
+
*/
|
|
394
950
|
readonly sockets: NodeJS.ReadOnlyDict<Socket[]>;
|
|
951
|
+
/**
|
|
952
|
+
* An object which contains queues of requests that have not yet been assigned to
|
|
953
|
+
* sockets. Do not modify.
|
|
954
|
+
* @since v0.5.9
|
|
955
|
+
*/
|
|
395
956
|
readonly requests: NodeJS.ReadOnlyDict<IncomingMessage[]>;
|
|
396
|
-
|
|
397
957
|
constructor(opts?: AgentOptions);
|
|
398
|
-
|
|
399
958
|
/**
|
|
400
959
|
* Destroy any sockets that are currently in use by the agent.
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
960
|
+
*
|
|
961
|
+
* It is usually not necessary to do this. However, if using an
|
|
962
|
+
* agent with `keepAlive` enabled, then it is best to explicitly shut down
|
|
963
|
+
* the agent when it is no longer needed. Otherwise,
|
|
964
|
+
* sockets might stay open for quite a long time before the server
|
|
965
|
+
* terminates them.
|
|
966
|
+
* @since v0.11.4
|
|
404
967
|
*/
|
|
405
968
|
destroy(): void;
|
|
406
969
|
}
|
|
407
|
-
|
|
408
970
|
const METHODS: string[];
|
|
409
|
-
|
|
410
971
|
const STATUS_CODES: {
|
|
411
972
|
[errorCode: number]: string | undefined;
|
|
412
973
|
[errorCode: string]: string | undefined;
|
|
413
974
|
};
|
|
414
|
-
|
|
975
|
+
/**
|
|
976
|
+
* Returns a new instance of {@link Server}.
|
|
977
|
+
*
|
|
978
|
+
* The `requestListener` is a function which is automatically
|
|
979
|
+
* added to the `'request'` event.
|
|
980
|
+
* @since v0.1.13
|
|
981
|
+
*/
|
|
415
982
|
function createServer(requestListener?: RequestListener): Server;
|
|
416
983
|
function createServer(options: ServerOptions, requestListener?: RequestListener): Server;
|
|
417
|
-
|
|
418
984
|
// although RequestOptions are passed as ClientRequestArgs to ClientRequest directly,
|
|
419
985
|
// create interface RequestOptions would make the naming more clear to developers
|
|
420
|
-
interface RequestOptions extends ClientRequestArgs {
|
|
986
|
+
interface RequestOptions extends ClientRequestArgs {}
|
|
987
|
+
/**
|
|
988
|
+
* Node.js maintains several connections per server to make HTTP requests.
|
|
989
|
+
* This function allows one to transparently issue requests.
|
|
990
|
+
*
|
|
991
|
+
* `url` can be a string or a `URL` object. If `url` is a
|
|
992
|
+
* string, it is automatically parsed with `new URL()`. If it is a `URL` object, it will be automatically converted to an ordinary `options` object.
|
|
993
|
+
*
|
|
994
|
+
* If both `url` and `options` are specified, the objects are merged, with the`options` properties taking precedence.
|
|
995
|
+
*
|
|
996
|
+
* The optional `callback` parameter will be added as a one-time listener for
|
|
997
|
+
* the `'response'` event.
|
|
998
|
+
*
|
|
999
|
+
* `http.request()` returns an instance of the {@link ClientRequest} class. The `ClientRequest` instance is a writable stream. If one needs to
|
|
1000
|
+
* upload a file with a POST request, then write to the `ClientRequest` object.
|
|
1001
|
+
*
|
|
1002
|
+
* ```js
|
|
1003
|
+
* const http = require('http');
|
|
1004
|
+
*
|
|
1005
|
+
* const postData = JSON.stringify({
|
|
1006
|
+
* 'msg': 'Hello World!'
|
|
1007
|
+
* });
|
|
1008
|
+
*
|
|
1009
|
+
* const options = {
|
|
1010
|
+
* hostname: 'www.google.com',
|
|
1011
|
+
* port: 80,
|
|
1012
|
+
* path: '/upload',
|
|
1013
|
+
* method: 'POST',
|
|
1014
|
+
* headers: {
|
|
1015
|
+
* 'Content-Type': 'application/json',
|
|
1016
|
+
* 'Content-Length': Buffer.byteLength(postData)
|
|
1017
|
+
* }
|
|
1018
|
+
* };
|
|
1019
|
+
*
|
|
1020
|
+
* const req = http.request(options, (res) => {
|
|
1021
|
+
* console.log(`STATUS: ${res.statusCode}`);
|
|
1022
|
+
* console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
|
|
1023
|
+
* res.setEncoding('utf8');
|
|
1024
|
+
* res.on('data', (chunk) => {
|
|
1025
|
+
* console.log(`BODY: ${chunk}`);
|
|
1026
|
+
* });
|
|
1027
|
+
* res.on('end', () => {
|
|
1028
|
+
* console.log('No more data in response.');
|
|
1029
|
+
* });
|
|
1030
|
+
* });
|
|
1031
|
+
*
|
|
1032
|
+
* req.on('error', (e) => {
|
|
1033
|
+
* console.error(`problem with request: ${e.message}`);
|
|
1034
|
+
* });
|
|
1035
|
+
*
|
|
1036
|
+
* // Write data to request body
|
|
1037
|
+
* req.write(postData);
|
|
1038
|
+
* req.end();
|
|
1039
|
+
* ```
|
|
1040
|
+
*
|
|
1041
|
+
* In the example `req.end()` was called. With `http.request()` one
|
|
1042
|
+
* must always call `req.end()` to signify the end of the request -
|
|
1043
|
+
* even if there is no data being written to the request body.
|
|
1044
|
+
*
|
|
1045
|
+
* If any error is encountered during the request (be that with DNS resolution,
|
|
1046
|
+
* TCP level errors, or actual HTTP parse errors) an `'error'` event is emitted
|
|
1047
|
+
* on the returned request object. As with all `'error'` events, if no listeners
|
|
1048
|
+
* are registered the error will be thrown.
|
|
1049
|
+
*
|
|
1050
|
+
* There are a few special headers that should be noted.
|
|
1051
|
+
*
|
|
1052
|
+
* * Sending a 'Connection: keep-alive' will notify Node.js that the connection to
|
|
1053
|
+
* the server should be persisted until the next request.
|
|
1054
|
+
* * Sending a 'Content-Length' header will disable the default chunked encoding.
|
|
1055
|
+
* * Sending an 'Expect' header will immediately send the request headers.
|
|
1056
|
+
* Usually, when sending 'Expect: 100-continue', both a timeout and a listener
|
|
1057
|
+
* for the `'continue'` event should be set. See RFC 2616 Section 8.2.3 for more
|
|
1058
|
+
* information.
|
|
1059
|
+
* * Sending an Authorization header will override using the `auth` option
|
|
1060
|
+
* to compute basic authentication.
|
|
1061
|
+
*
|
|
1062
|
+
* Example using a `URL` as `options`:
|
|
1063
|
+
*
|
|
1064
|
+
* ```js
|
|
1065
|
+
* const options = new URL('http://abc:xyz@example.com');
|
|
1066
|
+
*
|
|
1067
|
+
* const req = http.request(options, (res) => {
|
|
1068
|
+
* // ...
|
|
1069
|
+
* });
|
|
1070
|
+
* ```
|
|
1071
|
+
*
|
|
1072
|
+
* In a successful request, the following events will be emitted in the following
|
|
1073
|
+
* order:
|
|
1074
|
+
*
|
|
1075
|
+
* * `'socket'`
|
|
1076
|
+
* * `'response'`
|
|
1077
|
+
* * `'data'` any number of times, on the `res` object
|
|
1078
|
+
* (`'data'` will not be emitted at all if the response body is empty, for
|
|
1079
|
+
* instance, in most redirects)
|
|
1080
|
+
* * `'end'` on the `res` object
|
|
1081
|
+
* * `'close'`
|
|
1082
|
+
*
|
|
1083
|
+
* In the case of a connection error, the following events will be emitted:
|
|
1084
|
+
*
|
|
1085
|
+
* * `'socket'`
|
|
1086
|
+
* * `'error'`
|
|
1087
|
+
* * `'close'`
|
|
1088
|
+
*
|
|
1089
|
+
* In the case of a premature connection close before the response is received,
|
|
1090
|
+
* the following events will be emitted in the following order:
|
|
1091
|
+
*
|
|
1092
|
+
* * `'socket'`
|
|
1093
|
+
* * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
|
|
1094
|
+
* * `'close'`
|
|
1095
|
+
*
|
|
1096
|
+
* In the case of a premature connection close after the response is received,
|
|
1097
|
+
* the following events will be emitted in the following order:
|
|
1098
|
+
*
|
|
1099
|
+
* * `'socket'`
|
|
1100
|
+
* * `'response'`
|
|
1101
|
+
* * `'data'` any number of times, on the `res` object
|
|
1102
|
+
* * (connection closed here)
|
|
1103
|
+
* * `'aborted'` on the `res` object
|
|
1104
|
+
* * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
|
|
1105
|
+
* * `'close'`
|
|
1106
|
+
* * `'close'` on the `res` object
|
|
1107
|
+
*
|
|
1108
|
+
* If `req.destroy()` is called before a socket is assigned, the following
|
|
1109
|
+
* events will be emitted in the following order:
|
|
1110
|
+
*
|
|
1111
|
+
* * (`req.destroy()` called here)
|
|
1112
|
+
* * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
|
|
1113
|
+
* * `'close'`
|
|
1114
|
+
*
|
|
1115
|
+
* If `req.destroy()` is called before the connection succeeds, the following
|
|
1116
|
+
* events will be emitted in the following order:
|
|
1117
|
+
*
|
|
1118
|
+
* * `'socket'`
|
|
1119
|
+
* * (`req.destroy()` called here)
|
|
1120
|
+
* * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
|
|
1121
|
+
* * `'close'`
|
|
1122
|
+
*
|
|
1123
|
+
* If `req.destroy()` is called after the response is received, the following
|
|
1124
|
+
* events will be emitted in the following order:
|
|
1125
|
+
*
|
|
1126
|
+
* * `'socket'`
|
|
1127
|
+
* * `'response'`
|
|
1128
|
+
* * `'data'` any number of times, on the `res` object
|
|
1129
|
+
* * (`req.destroy()` called here)
|
|
1130
|
+
* * `'aborted'` on the `res` object
|
|
1131
|
+
* * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
|
|
1132
|
+
* * `'close'`
|
|
1133
|
+
* * `'close'` on the `res` object
|
|
1134
|
+
*
|
|
1135
|
+
* If `req.abort()` is called before a socket is assigned, the following
|
|
1136
|
+
* events will be emitted in the following order:
|
|
1137
|
+
*
|
|
1138
|
+
* * (`req.abort()` called here)
|
|
1139
|
+
* * `'abort'`
|
|
1140
|
+
* * `'close'`
|
|
1141
|
+
*
|
|
1142
|
+
* If `req.abort()` is called before the connection succeeds, the following
|
|
1143
|
+
* events will be emitted in the following order:
|
|
1144
|
+
*
|
|
1145
|
+
* * `'socket'`
|
|
1146
|
+
* * (`req.abort()` called here)
|
|
1147
|
+
* * `'abort'`
|
|
1148
|
+
* * `'error'` with an error with message `'Error: socket hang up'` and code`'ECONNRESET'`
|
|
1149
|
+
* * `'close'`
|
|
1150
|
+
*
|
|
1151
|
+
* If `req.abort()` is called after the response is received, the following
|
|
1152
|
+
* events will be emitted in the following order:
|
|
1153
|
+
*
|
|
1154
|
+
* * `'socket'`
|
|
1155
|
+
* * `'response'`
|
|
1156
|
+
* * `'data'` any number of times, on the `res` object
|
|
1157
|
+
* * (`req.abort()` called here)
|
|
1158
|
+
* * `'abort'`
|
|
1159
|
+
* * `'aborted'` on the `res` object
|
|
1160
|
+
* * `'error'` on the `res` object with an error with message`'Error: aborted'` and code `'ECONNRESET'`.
|
|
1161
|
+
* * `'close'`
|
|
1162
|
+
* * `'close'` on the `res` object
|
|
1163
|
+
*
|
|
1164
|
+
* Setting the `timeout` option or using the `setTimeout()` function will
|
|
1165
|
+
* not abort the request or do anything besides add a `'timeout'` event.
|
|
1166
|
+
*
|
|
1167
|
+
* Passing an `AbortSignal` and then calling `abort` on the corresponding`AbortController` will behave the same way as calling `.destroy()` on the
|
|
1168
|
+
* request itself.
|
|
1169
|
+
* @since v0.3.6
|
|
1170
|
+
*/
|
|
421
1171
|
function request(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
|
|
422
1172
|
function request(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
|
|
1173
|
+
/**
|
|
1174
|
+
* Since most requests are GET requests without bodies, Node.js provides this
|
|
1175
|
+
* convenience method. The only difference between this method and {@link request} is that it sets the method to GET and calls `req.end()`automatically. The callback must take care to consume the
|
|
1176
|
+
* response
|
|
1177
|
+
* data for reasons stated in {@link ClientRequest} section.
|
|
1178
|
+
*
|
|
1179
|
+
* The `callback` is invoked with a single argument that is an instance of {@link IncomingMessage}.
|
|
1180
|
+
*
|
|
1181
|
+
* JSON fetching example:
|
|
1182
|
+
*
|
|
1183
|
+
* ```js
|
|
1184
|
+
* http.get('http://localhost:8000/', (res) => {
|
|
1185
|
+
* const { statusCode } = res;
|
|
1186
|
+
* const contentType = res.headers['content-type'];
|
|
1187
|
+
*
|
|
1188
|
+
* let error;
|
|
1189
|
+
* // Any 2xx status code signals a successful response but
|
|
1190
|
+
* // here we're only checking for 200.
|
|
1191
|
+
* if (statusCode !== 200) {
|
|
1192
|
+
* error = new Error('Request Failed.\n' +
|
|
1193
|
+
* `Status Code: ${statusCode}`);
|
|
1194
|
+
* } else if (!/^application\/json/.test(contentType)) {
|
|
1195
|
+
* error = new Error('Invalid content-type.\n' +
|
|
1196
|
+
* `Expected application/json but received ${contentType}`);
|
|
1197
|
+
* }
|
|
1198
|
+
* if (error) {
|
|
1199
|
+
* console.error(error.message);
|
|
1200
|
+
* // Consume response data to free up memory
|
|
1201
|
+
* res.resume();
|
|
1202
|
+
* return;
|
|
1203
|
+
* }
|
|
1204
|
+
*
|
|
1205
|
+
* res.setEncoding('utf8');
|
|
1206
|
+
* let rawData = '';
|
|
1207
|
+
* res.on('data', (chunk) => { rawData += chunk; });
|
|
1208
|
+
* res.on('end', () => {
|
|
1209
|
+
* try {
|
|
1210
|
+
* const parsedData = JSON.parse(rawData);
|
|
1211
|
+
* console.log(parsedData);
|
|
1212
|
+
* } catch (e) {
|
|
1213
|
+
* console.error(e.message);
|
|
1214
|
+
* }
|
|
1215
|
+
* });
|
|
1216
|
+
* }).on('error', (e) => {
|
|
1217
|
+
* console.error(`Got error: ${e.message}`);
|
|
1218
|
+
* });
|
|
1219
|
+
*
|
|
1220
|
+
* // Create a local server to receive data from
|
|
1221
|
+
* const server = http.createServer((req, res) => {
|
|
1222
|
+
* res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
1223
|
+
* res.end(JSON.stringify({
|
|
1224
|
+
* data: 'Hello World!'
|
|
1225
|
+
* }));
|
|
1226
|
+
* });
|
|
1227
|
+
*
|
|
1228
|
+
* server.listen(8000);
|
|
1229
|
+
* ```
|
|
1230
|
+
* @since v0.3.6
|
|
1231
|
+
* @param options Accepts the same `options` as {@link request}, with the `method` always set to `GET`. Properties that are inherited from the prototype are ignored.
|
|
1232
|
+
*/
|
|
423
1233
|
function get(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest;
|
|
424
1234
|
function get(url: string | URL, options: RequestOptions, callback?: (res: IncomingMessage) => void): ClientRequest;
|
|
425
1235
|
let globalAgent: Agent;
|
|
426
|
-
|
|
427
1236
|
/**
|
|
428
1237
|
* Read-only property specifying the maximum allowed size of HTTP headers in bytes.
|
|
429
1238
|
* Defaults to 16KB. Configurable using the `--max-http-header-size` CLI option.
|
|
430
1239
|
*/
|
|
431
1240
|
const maxHeaderSize: number;
|
|
432
1241
|
}
|
|
433
|
-
|
|
434
1242
|
declare module 'node:http' {
|
|
435
1243
|
export * from 'http';
|
|
436
1244
|
}
|