got 11.5.2 → 11.7.0

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.
@@ -32,6 +32,7 @@ declare const kTriggerRead: unique symbol;
32
32
  declare const kBody: unique symbol;
33
33
  declare const kJobs: unique symbol;
34
34
  declare const kOriginalResponse: unique symbol;
35
+ declare const kRetryTimeout: unique symbol;
35
36
  export declare const kIsNormalizedAlready: unique symbol;
36
37
  export interface Agents {
37
38
  http?: http.Agent;
@@ -51,21 +52,133 @@ export interface PromiseCookieJar {
51
52
  getCookieString: (url: string) => Promise<string>;
52
53
  setCookie: (rawCookie: string, url: string) => Promise<unknown>;
53
54
  }
55
+ /**
56
+ All available HTTP request methods provided by Got.
57
+ */
54
58
  export declare type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE' | 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace';
55
59
  declare type Promisable<T> = T | Promise<T>;
56
60
  export declare type InitHook = (options: Options) => void;
57
61
  export declare type BeforeRequestHook = (options: NormalizedOptions) => Promisable<void | Response | ResponseLike>;
58
62
  export declare type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => Promisable<void>;
59
63
  export declare type BeforeErrorHook = (error: RequestError) => Promisable<RequestError>;
64
+ export declare type BeforeRetryHook = (options: NormalizedOptions, error?: RequestError, retryCount?: number) => void | Promise<void>;
60
65
  interface PlainHooks {
66
+ /**
67
+ Called with plain request options, right before their normalization.
68
+ This is especially useful in conjunction with `got.extend()` when the input needs custom handling.
69
+
70
+ __Note #1__: This hook must be synchronous!
71
+
72
+ __Note #2__: Errors in this hook will be converted into an instances of `RequestError`.
73
+
74
+ __Note #3__: The options object may not have a `url` property.
75
+ To modify it, use a `beforeRequest` hook instead.
76
+
77
+ @default []
78
+ */
61
79
  init?: InitHook[];
80
+ /**
81
+ Called with normalized request options.
82
+ Got will make no further changes to the request before it is sent.
83
+ This is especially useful in conjunction with `got.extend()` when you want to create an API client that, for example, uses HMAC-signing.
84
+
85
+ @default []
86
+ */
62
87
  beforeRequest?: BeforeRequestHook[];
88
+ /**
89
+ Called with normalized request options and the redirect response.
90
+ Got will make no further changes to the request.
91
+ This is especially useful when you want to avoid dead sites.
92
+
93
+ @default []
94
+
95
+ @example
96
+ ```
97
+ const got = require('got');
98
+
99
+ got('https://example.com', {
100
+ hooks: {
101
+ beforeRedirect: [
102
+ (options, response) => {
103
+ if (options.hostname === 'deadSite') {
104
+ options.hostname = 'fallbackSite';
105
+ }
106
+ }
107
+ ]
108
+ }
109
+ });
110
+ ```
111
+ */
63
112
  beforeRedirect?: BeforeRedirectHook[];
113
+ /**
114
+ Called with an `Error` instance.
115
+ The error is passed to the hook right before it's thrown.
116
+ This is especially useful when you want to have more detailed errors.
117
+
118
+ __Note__: Errors thrown while normalizing input options are thrown directly and not part of this hook.
119
+
120
+ @default []
121
+
122
+ @example
123
+ ```
124
+ const got = require('got');
125
+
126
+ got('https://api.github.com/some-endpoint', {
127
+ hooks: {
128
+ beforeError: [
129
+ error => {
130
+ const {response} = error;
131
+ if (response && response.body) {
132
+ error.name = 'GitHubError';
133
+ error.message = `${response.body.message} (${response.statusCode})`;
134
+ }
135
+
136
+ return error;
137
+ }
138
+ ]
139
+ }
140
+ });
141
+ ```
142
+ */
64
143
  beforeError?: BeforeErrorHook[];
144
+ /**
145
+ Called with normalized request options, the error and the retry count.
146
+ Got will make no further changes to the request.
147
+ This is especially useful when some extra work is required before the next try.
148
+
149
+ __Note__: When using streams, this hook is ignored.
150
+ __Note__: When retrying in a `afterResponse` hook, all remaining `beforeRetry` hooks will be called without the `error` and `retryCount` arguments.
151
+
152
+ @default []
153
+
154
+ @example
155
+ ```
156
+ const got = require('got');
157
+
158
+ got.post('https://example.com', {
159
+ hooks: {
160
+ beforeRetry: [
161
+ (options, error, retryCount) => {
162
+ if (error.response.statusCode === 413) { // Payload too large
163
+ options.body = getNewBody();
164
+ }
165
+ }
166
+ ]
167
+ }
168
+ });
169
+ ```
170
+ */
171
+ beforeRetry?: BeforeRetryHook[];
65
172
  }
173
+ /**
174
+ All available hook of Got.
175
+ */
66
176
  export interface Hooks extends PromiseOnly.Hooks, PlainHooks {
67
177
  }
68
- declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError';
178
+ declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError' | 'beforeRetry';
179
+ /**
180
+ All hook events acceptable by Got.
181
+ */
69
182
  export declare type HookEvent = PromiseOnly.HookEvent | PlainHookEvent;
70
183
  export declare const knownHookEvents: HookEvent[];
71
184
  declare type AcceptableResponse = IncomingMessageWithTimings | ResponseLike;
@@ -75,47 +188,512 @@ export declare type Headers = Record<string, string | string[] | undefined>;
75
188
  declare type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => Error | void;
76
189
  export declare type ParseJsonFunction = (text: string) => unknown;
77
190
  export declare type StringifyJsonFunction = (object: unknown) => string;
191
+ export interface RetryObject {
192
+ attemptCount: number;
193
+ retryOptions: RequiredRetryOptions;
194
+ error: TimeoutError | RequestError;
195
+ computedValue: number;
196
+ retryAfter?: number;
197
+ }
198
+ export declare type RetryFunction = (retryObject: RetryObject) => number | Promise<number>;
199
+ /**
200
+ An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
201
+
202
+ Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
203
+
204
+ The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
205
+ The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
206
+
207
+ By default, it retries *only* on the specified methods, status codes, and on these network errors:
208
+ - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
209
+ - `ECONNRESET`: Connection was forcibly closed by a peer.
210
+ - `EADDRINUSE`: Could not bind to any free port.
211
+ - `ECONNREFUSED`: Connection was refused by the server.
212
+ - `EPIPE`: The remote side of the stream being written has been closed.
213
+ - `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
214
+ - `ENETUNREACH`: No internet connection.
215
+ - `EAI_AGAIN`: DNS lookup timed out.
216
+
217
+ __Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
218
+ __Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
219
+ */
220
+ export interface RequiredRetryOptions {
221
+ limit: number;
222
+ methods: Method[];
223
+ statusCodes: number[];
224
+ errorCodes: string[];
225
+ calculateDelay: RetryFunction;
226
+ maxRetryAfter?: number;
227
+ }
228
+ export interface CacheOptions {
229
+ shared?: boolean;
230
+ cacheHeuristic?: number;
231
+ immutableMinTimeToLive?: number;
232
+ ignoreCargoCult?: boolean;
233
+ }
78
234
  interface PlainOptions extends URLOptions {
235
+ /**
236
+ Custom request function.
237
+ The main purpose of this is to [support HTTP2 using a wrapper](https://github.com/szmarczak/http2-wrapper).
238
+
239
+ @default http.request | https.request
240
+ */
79
241
  request?: RequestFunction;
242
+ /**
243
+ An object representing `http`, `https` and `http2` keys for [`http.Agent`](https://nodejs.org/api/http.html#http_class_http_agent), [`https.Agent`](https://nodejs.org/api/https.html#https_class_https_agent) and [`http2wrapper.Agent`](https://github.com/szmarczak/http2-wrapper#new-http2agentoptions) instance.
244
+ This is necessary because a request to one protocol might redirect to another.
245
+ In such a scenario, Got will switch over to the right protocol agent for you.
246
+
247
+ If a key is not present, it will default to a global agent.
248
+
249
+ @example
250
+ ```
251
+ const got = require('got');
252
+ const HttpAgent = require('agentkeepalive');
253
+ const {HttpsAgent} = HttpAgent;
254
+
255
+ got('https://sindresorhus.com', {
256
+ agent: {
257
+ http: new HttpAgent(),
258
+ https: new HttpsAgent()
259
+ }
260
+ });
261
+ ```
262
+ */
80
263
  agent?: Agents | false;
264
+ /**
265
+ Decompress the response automatically.
266
+ This will set the `accept-encoding` header to `gzip, deflate, br` on Node.js 11.7.0+ or `gzip, deflate` for older Node.js versions, unless you set it yourself.
267
+
268
+ Brotli (`br`) support requires Node.js 11.7.0 or later.
269
+
270
+ If this is disabled, a compressed response is returned as a `Buffer`.
271
+ This may be useful if you want to handle decompression yourself or stream the raw compressed data.
272
+
273
+ @default true
274
+ */
81
275
  decompress?: boolean;
276
+ /**
277
+ Milliseconds to wait for the server to end the response before aborting the request with `got.TimeoutError` error (a.k.a. `request` property).
278
+ By default, there's no timeout.
279
+
280
+ This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
281
+
282
+ - `lookup` starts when a socket is assigned and ends when the hostname has been resolved.
283
+ Does not apply when using a Unix domain socket.
284
+ - `connect` starts when `lookup` completes (or when the socket is assigned if lookup does not apply to the request) and ends when the socket is connected.
285
+ - `secureConnect` starts when `connect` completes and ends when the handshaking process completes (HTTPS only).
286
+ - `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
287
+ - `response` starts when the request has been written to the socket and ends when the response headers are received.
288
+ - `send` starts when the socket is connected and ends with the request has been written to the socket.
289
+ - `request` starts when the request is initiated and ends when the response's end event fires.
290
+ */
82
291
  timeout?: Delays | number;
292
+ /**
293
+ When specified, `prefixUrl` will be prepended to `url`.
294
+ The prefix can be any valid URL, either relative or absolute.
295
+ A trailing slash `/` is optional - one will be added automatically.
296
+
297
+ __Note__: `prefixUrl` will be ignored if the `url` argument is a URL instance.
298
+
299
+ __Note__: Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion.
300
+ For example, when the prefix URL is `https://example.com/foo` and the input is `/bar`, there's ambiguity whether the resulting URL would become `https://example.com/foo/bar` or `https://example.com/bar`.
301
+ The latter is used by browsers.
302
+
303
+ __Tip__: Useful when used with `got.extend()` to create niche-specific Got instances.
304
+
305
+ __Tip__: You can change `prefixUrl` using hooks as long as the URL still includes the `prefixUrl`.
306
+ If the URL doesn't include it anymore, it will throw.
307
+
308
+ @example
309
+ ```
310
+ const got = require('got');
311
+
312
+ (async () => {
313
+ await got('unicorn', {prefixUrl: 'https://cats.com'});
314
+ //=> 'https://cats.com/unicorn'
315
+
316
+ const instance = got.extend({
317
+ prefixUrl: 'https://google.com'
318
+ });
319
+
320
+ await instance('unicorn', {
321
+ hooks: {
322
+ beforeRequest: [
323
+ options => {
324
+ options.prefixUrl = 'https://cats.com';
325
+ }
326
+ ]
327
+ }
328
+ });
329
+ //=> 'https://cats.com/unicorn'
330
+ })();
331
+ ```
332
+ */
83
333
  prefixUrl?: string | URL;
334
+ /**
335
+ __Note #1__: The `body` option cannot be used with the `json` or `form` option.
336
+
337
+ __Note #2__: If you provide this option, `got.stream()` will be read-only.
338
+
339
+ __Note #3__: If you provide a payload with the `GET` or `HEAD` method, it will throw a `TypeError` unless the method is `GET` and the `allowGetBody` option is set to `true`.
340
+
341
+ __Note #4__: This option is not enumerable and will not be merged with the instance defaults.
342
+
343
+ The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
344
+ */
84
345
  body?: string | Buffer | Readable;
346
+ /**
347
+ The form body is converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
348
+
349
+ If the `Content-Type` header is not present, it will be set to `application/x-www-form-urlencoded`.
350
+
351
+ __Note #1__: If you provide this option, `got.stream()` will be read-only.
352
+
353
+ __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
354
+ */
85
355
  form?: {
86
356
  [key: string]: any;
87
357
  };
358
+ /**
359
+ JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
360
+
361
+ __Note #1__: If you provide this option, `got.stream()` will be read-only.
362
+
363
+ __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
364
+ */
88
365
  json?: {
89
366
  [key: string]: any;
90
367
  };
368
+ /**
369
+ The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
370
+
371
+ Properties from `options` will override properties in the parsed `url`.
372
+
373
+ If no protocol is specified, it will throw a `TypeError`.
374
+
375
+ __Note__: The query string is **not** parsed as search params.
376
+
377
+ @example
378
+ ```
379
+ got('https://example.com/?query=a b'); //=> https://example.com/?query=a%20b
380
+ got('https://example.com/', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
381
+
382
+ // The query string is overridden by `searchParams`
383
+ got('https://example.com/?query=a b', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
384
+ ```
385
+ */
91
386
  url?: string | URL;
387
+ /**
388
+ Cookie support. You don't have to care about parsing or how to store them.
389
+
390
+ __Note__: If you provide this option, `options.headers.cookie` will be overridden.
391
+ */
92
392
  cookieJar?: PromiseCookieJar | ToughCookieJar;
393
+ /**
394
+ Ignore invalid cookies instead of throwing an error.
395
+ Only useful when the `cookieJar` option has been set. Not recommended.
396
+
397
+ @default false
398
+ */
93
399
  ignoreInvalidCookies?: boolean;
400
+ /**
401
+ Query string that will be added to the request URL.
402
+ This will override the query string in `url`.
403
+
404
+ If you need to pass in an array, you can do it using a `URLSearchParams` instance.
405
+
406
+ @example
407
+ ```
408
+ const got = require('got');
409
+
410
+ const searchParams = new URLSearchParams([['key', 'a'], ['key', 'b']]);
411
+
412
+ got('https://example.com', {searchParams});
413
+
414
+ console.log(searchParams.toString());
415
+ //=> 'key=a&key=b'
416
+ ```
417
+ */
94
418
  searchParams?: string | {
95
419
  [key: string]: string | number | boolean | null | undefined;
96
420
  } | URLSearchParams;
421
+ /**
422
+ An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
423
+ Useful when making lots of requests to different *public* hostnames.
424
+
425
+ `CacheableLookup` uses `dns.resolver4(..)` and `dns.resolver6(...)` under the hood and fall backs to `dns.lookup(...)` when the first two fail, which may lead to additional delay.
426
+
427
+ __Note__: This should stay disabled when making requests to internal hostnames such as `localhost`, `database.local` etc.
428
+
429
+ @default false
430
+ */
97
431
  dnsCache?: CacheableLookup | boolean;
432
+ /**
433
+ User data. In contrast to other options, `context` is not enumerable.
434
+
435
+ __Note__: The object is never merged, it's just passed through.
436
+ Got will not modify the object in any way.
437
+
438
+ @example
439
+ ```
440
+ const got = require('got');
441
+
442
+ const instance = got.extend({
443
+ hooks: {
444
+ beforeRequest: [
445
+ options => {
446
+ if (!options.context || !options.context.token) {
447
+ throw new Error('Token required');
448
+ }
449
+
450
+ options.headers.token = options.context.token;
451
+ }
452
+ ]
453
+ }
454
+ });
455
+
456
+ (async () => {
457
+ const context = {
458
+ token: 'secret'
459
+ };
460
+
461
+ const response = await instance('https://httpbin.org/headers', {context});
462
+
463
+ // Let's see the headers
464
+ console.log(response.body);
465
+ })();
466
+ ```
467
+ */
98
468
  context?: Record<string, unknown>;
469
+ /**
470
+ Hooks allow modifications during the request lifecycle.
471
+ Hook functions may be async and are run serially.
472
+ */
99
473
  hooks?: Hooks;
474
+ /**
475
+ Defines if redirect responses should be followed automatically.
476
+
477
+ Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
478
+ This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
479
+
480
+ @default true
481
+ */
100
482
  followRedirect?: boolean;
483
+ /**
484
+ If exceeded, the request will be aborted and a `MaxRedirectsError` will be thrown.
485
+
486
+ @default 10
487
+ */
101
488
  maxRedirects?: number;
489
+ /**
490
+ A cache adapter instance for storing cached response data.
491
+
492
+ @default false
493
+ */
102
494
  cache?: string | CacheableRequest.StorageAdapter | false;
495
+ /**
496
+ Determines if a `got.HTTPError` is thrown for unsuccessful responses.
497
+
498
+ If this is disabled, requests that encounter an error status code will be resolved with the `response` instead of throwing.
499
+ This may be useful if you are checking for resource availability and are expecting error responses.
500
+
501
+ @default true
502
+ */
103
503
  throwHttpErrors?: boolean;
104
504
  username?: string;
105
505
  password?: string;
506
+ /**
507
+ If set to `true`, Got will additionally accept HTTP2 requests.
508
+
509
+ It will choose either HTTP/1.1 or HTTP/2 depending on the ALPN protocol.
510
+
511
+ __Note__: Overriding `options.request` will disable HTTP2 support.
512
+
513
+ __Note__: This option will default to `true` in the next upcoming major release.
514
+
515
+ @default false
516
+
517
+ @example
518
+ ```
519
+ const got = require('got');
520
+
521
+ (async () => {
522
+ const {headers} = await got('https://nghttp2.org/httpbin/anything', {http2: true});
523
+ console.log(headers.via);
524
+ //=> '2 nghttpx'
525
+ })();
526
+ ```
527
+ */
106
528
  http2?: boolean;
529
+ /**
530
+ Set this to `true` to allow sending body for the `GET` method.
531
+ However, the [HTTP/2 specification](https://tools.ietf.org/html/rfc7540#section-8.1.3) says that `An HTTP GET request includes request header fields and no payload body`, therefore when using the HTTP/2 protocol this option will have no effect.
532
+ This option is only meant to interact with non-compliant servers when you have no other choice.
533
+
534
+ __Note__: The [RFC 7321](https://tools.ietf.org/html/rfc7231#section-4.3.1) doesn't specify any particular behavior for the GET method having a payload, therefore __it's considered an [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern)__.
535
+
536
+ @default false
537
+ */
107
538
  allowGetBody?: boolean;
108
539
  lookup?: CacheableLookup['lookup'];
540
+ /**
541
+ Request headers.
542
+
543
+ Existing headers will be overwritten. Headers set to `undefined` will be omitted.
544
+
545
+ @default {}
546
+ */
109
547
  headers?: Headers;
548
+ /**
549
+ By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4).
550
+ For example, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
551
+
552
+ @default true
553
+ */
110
554
  methodRewriting?: boolean;
555
+ /**
556
+ Indicates which DNS record family to use.
557
+
558
+ Values:
559
+ - `auto`: IPv4 (if present) or IPv6
560
+ - `ipv4`: Only IPv4
561
+ - `ipv6`: Only IPv6
562
+
563
+ __Note__: If you are using the undocumented option `family`, `dnsLookupIpVersion` will override it.
564
+
565
+ @default 'auto'
566
+ */
111
567
  dnsLookupIpVersion?: DnsLookupIpVersion;
568
+ /**
569
+ A function used to parse JSON responses.
570
+
571
+ @example
572
+ ```
573
+ const got = require('got');
574
+ const Bourne = require('@hapi/bourne');
575
+
576
+ (async () => {
577
+ const parsed = await got('https://example.com', {
578
+ parseJson: text => Bourne.parse(text)
579
+ }).json();
580
+
581
+ console.log(parsed);
582
+ })();
583
+ ```
584
+ */
112
585
  parseJson?: ParseJsonFunction;
586
+ /**
587
+ A function used to stringify the body of JSON requests.
588
+
589
+ @example
590
+ ```
591
+ const got = require('got');
592
+
593
+ (async () => {
594
+ await got.post('https://example.com', {
595
+ stringifyJson: object => JSON.stringify(object, (key, value) => {
596
+ if (key.startsWith('_')) {
597
+ return;
598
+ }
599
+
600
+ return value;
601
+ }),
602
+ json: {
603
+ some: 'payload',
604
+ _ignoreMe: 1234
605
+ }
606
+ });
607
+ })();
608
+ ```
609
+
610
+ @example
611
+ ```
612
+ const got = require('got');
613
+
614
+ (async () => {
615
+ await got.post('https://example.com', {
616
+ stringifyJson: object => JSON.stringify(object, (key, value) => {
617
+ if (typeof value === 'number') {
618
+ return value.toString();
619
+ }
620
+
621
+ return value;
622
+ }),
623
+ json: {
624
+ some: 'payload',
625
+ number: 1
626
+ }
627
+ });
628
+ })();
629
+ ```
630
+ */
113
631
  stringifyJson?: StringifyJsonFunction;
632
+ /**
633
+ An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
634
+
635
+ Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
636
+
637
+ The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
638
+ The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
639
+
640
+ By default, it retries *only* on the specified methods, status codes, and on these network errors:
641
+
642
+ - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
643
+ - `ECONNRESET`: Connection was forcibly closed by a peer.
644
+ - `EADDRINUSE`: Could not bind to any free port.
645
+ - `ECONNREFUSED`: Connection was refused by the server.
646
+ - `EPIPE`: The remote side of the stream being written has been closed.
647
+ - `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
648
+ - `ENETUNREACH`: No internet connection.
649
+ - `EAI_AGAIN`: DNS lookup timed out.
650
+
651
+ __Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
652
+ __Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
653
+ */
654
+ retry?: Partial<RequiredRetryOptions> | number;
655
+ /**
656
+ The IP address used to send the request from.
657
+ */
114
658
  localAddress?: string;
115
659
  socketPath?: string;
660
+ /**
661
+ The HTTP method used to make the request.
662
+
663
+ @default 'GET'
664
+ */
116
665
  method?: Method;
117
666
  createConnection?: (options: http.RequestOptions, oncreate: (error: Error, socket: Socket) => void) => Socket;
667
+ cacheOptions?: CacheOptions;
668
+ /**
669
+ If set to `false`, all invalid SSL certificates will be ignored and no error will be thrown.
670
+
671
+ If set to `true`, it will throw an error whenever an invalid SSL certificate is detected.
672
+
673
+ We strongly recommend to have this set to `true` for security reasons.
674
+
675
+ @default true
676
+
677
+ @example
678
+ ```
679
+ const got = require('got');
680
+
681
+ (async () => {
682
+ // Correct:
683
+ await got('https://example.com', {rejectUnauthorized: true});
684
+
685
+ // You can disable it when developing an HTTPS app:
686
+ await got('https://localhost', {rejectUnauthorized: false});
687
+
688
+ // Never do this:
689
+ await got('https://example.com', {rejectUnauthorized: false});
690
+ })();
691
+ ```
692
+ */
118
693
  rejectUnauthorized?: boolean;
694
+ /**
695
+ Options for the advanced HTTPS API.
696
+ */
119
697
  https?: HTTPSOptions;
120
698
  }
121
699
  export interface Options extends PromiseOnly.Options, PlainOptions {
@@ -123,10 +701,44 @@ export interface Options extends PromiseOnly.Options, PlainOptions {
123
701
  export interface HTTPSOptions {
124
702
  rejectUnauthorized?: https.RequestOptions['rejectUnauthorized'];
125
703
  checkServerIdentity?: CheckServerIdentityFunction;
704
+ /**
705
+ Override the default Certificate Authorities ([from Mozilla](https://ccadb-public.secure.force.com/mozilla/IncludedCACertificateReport)).
706
+
707
+ @example
708
+ ```
709
+ // Single Certificate Authority
710
+ got('https://example.com', {
711
+ https: {
712
+ certificateAuthority: fs.readFileSync('./my_ca.pem')
713
+ }
714
+ });
715
+ ```
716
+ */
126
717
  certificateAuthority?: SecureContextOptions['ca'];
718
+ /**
719
+ Private keys in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
720
+
721
+ [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) allows the option of private keys being encrypted.
722
+ Encrypted keys will be decrypted with `options.https.passphrase`.
723
+
724
+ Multiple keys with different passphrases can be provided as an array of `{pem: <string | Buffer>, passphrase: <string>}`
725
+ */
127
726
  key?: SecureContextOptions['key'];
727
+ /**
728
+ [Certificate chains](https://en.wikipedia.org/wiki/X.509#Certificate_chains_and_cross-certification) in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
729
+
730
+ One cert chain should be provided per private key (`options.https.key`).
731
+
732
+ When providing multiple cert chains, they do not have to be in the same order as their private keys in `options.https.key`.
733
+
734
+ If the intermediate certificates are not provided, the peer will not be able to validate the certificate, and the handshake will fail.
735
+ */
128
736
  certificate?: SecureContextOptions['cert'];
737
+ /**
738
+ The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
739
+ */
129
740
  passphrase?: SecureContextOptions['passphrase'];
741
+ pfx?: SecureContextOptions['pfx'];
130
742
  }
131
743
  interface NormalizedPlainOptions extends PlainOptions {
132
744
  method: Method;
@@ -154,6 +766,8 @@ interface NormalizedPlainOptions extends PlainOptions {
154
766
  password: string;
155
767
  parseJson: ParseJsonFunction;
156
768
  stringifyJson: StringifyJsonFunction;
769
+ retry: RequiredRetryOptions;
770
+ cacheOptions: CacheOptions;
157
771
  [kRequest]: HttpRequestFunction;
158
772
  [kIsNormalizedAlready]?: boolean;
159
773
  }
@@ -180,12 +794,14 @@ interface PlainDefaults {
180
794
  methodRewriting: boolean;
181
795
  parseJson: ParseJsonFunction;
182
796
  stringifyJson: StringifyJsonFunction;
797
+ retry: RequiredRetryOptions;
183
798
  agent?: Agents | false;
184
799
  request?: RequestFunction;
185
800
  searchParams?: URLSearchParams;
186
801
  lookup?: CacheableLookup['lookup'];
187
802
  localAddress?: string;
188
803
  createConnection?: Options['createConnection'];
804
+ cacheOptions: CacheOptions;
189
805
  }
190
806
  export interface Defaults extends PromiseOnly.Defaults, PlainDefaults {
191
807
  }
@@ -195,24 +811,156 @@ export interface Progress {
195
811
  total?: number;
196
812
  }
197
813
  export interface PlainResponse extends IncomingMessageWithTimings {
814
+ /**
815
+ The original request URL.
816
+ */
198
817
  requestUrl: string;
818
+ /**
819
+ The redirect URLs.
820
+ */
199
821
  redirectUrls: string[];
822
+ /**
823
+ - `options` - The Got options that were set on this request.
824
+
825
+ __Note__: This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest).
826
+ */
200
827
  request: Request;
828
+ /**
829
+ The remote IP address.
830
+
831
+ This is hopefully a temporary limitation, see [lukechilds/cacheable-request#86](https://github.com/lukechilds/cacheable-request/issues/86).
832
+
833
+ __Note__: Not available when the response is cached.
834
+ */
201
835
  ip?: string;
836
+ /**
837
+ Whether the response was retrieved from the cache.
838
+ */
202
839
  isFromCache: boolean;
840
+ /**
841
+ The status code of the response.
842
+ */
203
843
  statusCode: number;
844
+ /**
845
+ The request URL or the final URL after redirects.
846
+ */
204
847
  url: string;
848
+ /**
849
+ The object contains the following properties:
850
+
851
+ - `start` - Time when the request started.
852
+ - `socket` - Time when a socket was assigned to the request.
853
+ - `lookup` - Time when the DNS lookup finished.
854
+ - `connect` - Time when the socket successfully connected.
855
+ - `secureConnect` - Time when the socket securely connected.
856
+ - `upload` - Time when the request finished uploading.
857
+ - `response` - Time when the request fired `response` event.
858
+ - `end` - Time when the response fired `end` event.
859
+ - `error` - Time when the request fired `error` event.
860
+ - `abort` - Time when the request fired `abort` event.
861
+ - `phases`
862
+ - `wait` - `timings.socket - timings.start`
863
+ - `dns` - `timings.lookup - timings.socket`
864
+ - `tcp` - `timings.connect - timings.lookup`
865
+ - `tls` - `timings.secureConnect - timings.connect`
866
+ - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
867
+ - `firstByte` - `timings.response - timings.upload`
868
+ - `download` - `timings.end - timings.response`
869
+ - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
870
+
871
+ If something has not been measured yet, it will be `undefined`.
872
+
873
+ __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
874
+ */
205
875
  timings: Timings;
876
+ /**
877
+ The number of times the request was retried.
878
+ */
879
+ retryCount: number;
880
+ /**
881
+ The raw result of the request.
882
+ */
883
+ rawBody?: Buffer;
884
+ /**
885
+ The result of the request.
886
+ */
887
+ body?: unknown;
206
888
  }
207
889
  export interface Response<T = unknown> extends PlainResponse {
890
+ /**
891
+ The result of the request.
892
+ */
208
893
  body: T;
894
+ /**
895
+ The raw result of the request.
896
+ */
209
897
  rawBody: Buffer;
210
- retryCount: number;
211
898
  }
212
899
  export interface RequestEvents<T> {
213
- on: ((name: 'request', listener: (request: http.ClientRequest) => void) => T) & (<R extends Response>(name: 'response', listener: (response: R) => void) => T) & (<R extends Response, N extends NormalizedOptions>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T) & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T);
900
+ /**
901
+ `request` event to get the request object of the request.
902
+
903
+ __Tip__: You can use `request` event to abort requests.
904
+
905
+ @example
906
+ ```
907
+ got.stream('https://github.com')
908
+ .on('request', request => setTimeout(() => request.destroy(), 50));
909
+ ```
910
+ */
911
+ on: ((name: 'request', listener: (request: http.ClientRequest) => void) => T)
912
+ /**
913
+ The `response` event to get the response object of the final request.
914
+ */
915
+ & (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
916
+ /**
917
+ The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
918
+ */
919
+ & (<R extends Response, N extends NormalizedOptions>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T)
920
+ /**
921
+ Progress events for uploading (sending a request) and downloading (receiving a response).
922
+ The `progress` argument is an object like:
923
+
924
+ ```js
925
+ {
926
+ percent: 0.1,
927
+ transferred: 1024,
928
+ total: 10240
929
+ }
930
+ ```
931
+
932
+ If the `content-length` header is missing, `total` will be `undefined`.
933
+
934
+ @example
935
+ ```js
936
+ (async () => {
937
+ const response = await got('https://sindresorhus.com')
938
+ .on('downloadProgress', progress => {
939
+ // Report download progress
940
+ })
941
+ .on('uploadProgress', progress => {
942
+ // Report upload progress
943
+ });
944
+
945
+ console.log(response);
946
+ })();
947
+ ```
948
+ */
949
+ & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
950
+ /**
951
+ To enable retrying on a Got stream, it is required to have a `retry` handler attached.
952
+
953
+ When this event is emitted, you should reset the stream you were writing to and prepare the body again.
954
+
955
+ See `got.options.retry` for more information.
956
+ */
957
+ & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
214
958
  }
215
959
  export declare const setNonEnumerableProperties: (sources: Array<Options | Defaults | undefined>, to: Options) => void;
960
+ /**
961
+ An error to be thrown when a request fails.
962
+ Contains a `code` property with error class code, like `ECONNREFUSED`.
963
+ */
216
964
  export declare class RequestError extends Error {
217
965
  code?: string;
218
966
  stack: string;
@@ -224,38 +972,63 @@ export declare class RequestError extends Error {
224
972
  code?: string;
225
973
  }>, self: Request | NormalizedOptions);
226
974
  }
975
+ /**
976
+ An error to be thrown when the server redirects you more than ten times.
977
+ Includes a `response` property.
978
+ */
227
979
  export declare class MaxRedirectsError extends RequestError {
228
980
  readonly response: Response;
229
981
  readonly request: Request;
230
982
  readonly timings: Timings;
231
983
  constructor(request: Request);
232
984
  }
985
+ /**
986
+ An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
987
+ Includes a `response` property.
988
+ */
233
989
  export declare class HTTPError extends RequestError {
234
990
  readonly response: Response;
235
991
  readonly request: Request;
236
992
  readonly timings: Timings;
237
993
  constructor(response: Response);
238
994
  }
995
+ /**
996
+ An error to be thrown when a cache method fails.
997
+ For example, if the database goes down or there's a filesystem error.
998
+ */
239
999
  export declare class CacheError extends RequestError {
240
1000
  readonly request: Request;
241
1001
  constructor(error: Error, request: Request);
242
1002
  }
1003
+ /**
1004
+ An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
1005
+ */
243
1006
  export declare class UploadError extends RequestError {
244
1007
  readonly request: Request;
245
1008
  constructor(error: Error, request: Request);
246
1009
  }
1010
+ /**
1011
+ An error to be thrown when the request is aborted due to a timeout.
1012
+ Includes an `event` and `timings` property.
1013
+ */
247
1014
  export declare class TimeoutError extends RequestError {
248
1015
  readonly request: Request;
249
1016
  readonly timings: Timings;
250
1017
  readonly event: string;
251
1018
  constructor(error: TimedOutTimeoutError, timings: Timings, request: Request);
252
1019
  }
1020
+ /**
1021
+ An error to be thrown when reading from response stream fails.
1022
+ */
253
1023
  export declare class ReadError extends RequestError {
254
1024
  readonly request: Request;
255
1025
  readonly response: Response;
256
1026
  readonly timings: Timings;
257
1027
  constructor(error: Error, request: Request);
258
1028
  }
1029
+ /**
1030
+ An error to be thrown when given an unsupported protocol.
1031
+ */
259
1032
  export declare class UnsupportedProtocolError extends RequestError {
260
1033
  constructor(options: NormalizedOptions);
261
1034
  }
@@ -269,6 +1042,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
269
1042
  [kTriggerRead]: boolean;
270
1043
  [kBody]: Options['body'];
271
1044
  [kJobs]: Array<() => void>;
1045
+ [kRetryTimeout]?: NodeJS.Timeout;
272
1046
  [kBodySize]?: number;
273
1047
  [kServerResponsesPiped]: Set<ServerResponse>;
274
1048
  [kIsFromCache]?: boolean;
@@ -284,7 +1058,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
284
1058
  requestUrl: string;
285
1059
  requestInitialized: boolean;
286
1060
  redirects: string[];
287
- constructor(url: string | URL, options?: Options, defaults?: Defaults);
1061
+ retryCount: number;
1062
+ constructor(url: string | URL | undefined, options?: Options, defaults?: Defaults);
288
1063
  static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions;
289
1064
  _lockWrite(): void;
290
1065
  _unlockWrite(): void;
@@ -294,20 +1069,63 @@ export default class Request extends Duplex implements RequestEvents<Request> {
294
1069
  _onRequest(request: ClientRequest): void;
295
1070
  _createCacheableRequest(url: URL, options: RequestOptions): Promise<ClientRequest | ResponseLike>;
296
1071
  _makeRequest(): Promise<void>;
1072
+ _error(error: RequestError): Promise<void>;
297
1073
  _beforeError(error: Error): void;
298
1074
  _read(): void;
299
1075
  _write(chunk: any, encoding: string | undefined, callback: (error?: Error | null) => void): void;
300
1076
  _writeRequest(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
301
1077
  _final(callback: (error?: Error | null) => void): void;
302
1078
  _destroy(error: Error | null, callback: (error: Error | null) => void): void;
1079
+ get _isAboutToError(): boolean;
1080
+ /**
1081
+ The remote IP address.
1082
+ */
303
1083
  get ip(): string | undefined;
1084
+ /**
1085
+ Indicates whether the request has been aborted or not.
1086
+ */
304
1087
  get aborted(): boolean;
305
1088
  get socket(): Socket | undefined;
1089
+ /**
1090
+ Progress event for downloading (receiving a response).
1091
+ */
306
1092
  get downloadProgress(): Progress;
1093
+ /**
1094
+ Progress event for uploading (sending a request).
1095
+ */
307
1096
  get uploadProgress(): Progress;
1097
+ /**
1098
+ The object contains the following properties:
1099
+
1100
+ - `start` - Time when the request started.
1101
+ - `socket` - Time when a socket was assigned to the request.
1102
+ - `lookup` - Time when the DNS lookup finished.
1103
+ - `connect` - Time when the socket successfully connected.
1104
+ - `secureConnect` - Time when the socket securely connected.
1105
+ - `upload` - Time when the request finished uploading.
1106
+ - `response` - Time when the request fired `response` event.
1107
+ - `end` - Time when the response fired `end` event.
1108
+ - `error` - Time when the request fired `error` event.
1109
+ - `abort` - Time when the request fired `abort` event.
1110
+ - `phases`
1111
+ - `wait` - `timings.socket - timings.start`
1112
+ - `dns` - `timings.lookup - timings.socket`
1113
+ - `tcp` - `timings.connect - timings.lookup`
1114
+ - `tls` - `timings.secureConnect - timings.connect`
1115
+ - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
1116
+ - `firstByte` - `timings.response - timings.upload`
1117
+ - `download` - `timings.end - timings.response`
1118
+ - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
1119
+
1120
+ If something has not been measured yet, it will be `undefined`.
1121
+
1122
+ __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
1123
+ */
308
1124
  get timings(): Timings | undefined;
1125
+ /**
1126
+ Whether the response was retrieved from the cache.
1127
+ */
309
1128
  get isFromCache(): boolean | undefined;
310
- get _response(): Response | undefined;
311
1129
  pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
312
1130
  end?: boolean;
313
1131
  }): T;