got 11.6.1 → 11.8.1

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.
Files changed (49) hide show
  1. package/dist/source/as-promise/create-rejection.d.ts +0 -0
  2. package/dist/source/as-promise/create-rejection.js +0 -0
  3. package/dist/source/as-promise/index.d.ts +0 -0
  4. package/dist/source/as-promise/index.js +3 -2
  5. package/dist/source/as-promise/normalize-arguments.d.ts +0 -0
  6. package/dist/source/as-promise/normalize-arguments.js +0 -0
  7. package/dist/source/as-promise/parse-body.d.ts +0 -0
  8. package/dist/source/as-promise/parse-body.js +1 -1
  9. package/dist/source/as-promise/types.d.ts +0 -0
  10. package/dist/source/as-promise/types.js +0 -0
  11. package/dist/source/core/calculate-retry-delay.d.ts +0 -0
  12. package/dist/source/core/calculate-retry-delay.js +0 -0
  13. package/dist/source/core/index.d.ts +6 -15
  14. package/dist/source/core/index.js +63 -38
  15. package/dist/source/core/utils/dns-ip-version.d.ts +0 -0
  16. package/dist/source/core/utils/dns-ip-version.js +0 -0
  17. package/dist/source/core/utils/get-body-size.d.ts +0 -0
  18. package/dist/source/core/utils/get-body-size.js +3 -0
  19. package/dist/source/core/utils/get-buffer.d.ts +0 -0
  20. package/dist/source/core/utils/get-buffer.js +0 -0
  21. package/dist/source/core/utils/is-form-data.d.ts +0 -0
  22. package/dist/source/core/utils/is-form-data.js +0 -0
  23. package/dist/source/core/utils/is-response-ok.d.ts +0 -0
  24. package/dist/source/core/utils/is-response-ok.js +0 -0
  25. package/dist/source/core/utils/options-to-url.d.ts +0 -0
  26. package/dist/source/core/utils/options-to-url.js +0 -0
  27. package/dist/source/core/utils/proxy-events.d.ts +0 -0
  28. package/dist/source/core/utils/proxy-events.js +0 -0
  29. package/dist/source/core/utils/timed-out.d.ts +0 -0
  30. package/dist/source/core/utils/timed-out.js +0 -0
  31. package/dist/source/core/utils/unhandle.d.ts +0 -0
  32. package/dist/source/core/utils/unhandle.js +0 -0
  33. package/dist/source/core/utils/url-to-options.d.ts +0 -0
  34. package/dist/source/core/utils/url-to-options.js +1 -1
  35. package/dist/source/core/utils/weakable-map.d.ts +0 -0
  36. package/dist/source/core/utils/weakable-map.js +0 -0
  37. package/dist/source/create.d.ts +0 -0
  38. package/dist/source/create.js +4 -4
  39. package/dist/source/index.d.ts +0 -0
  40. package/dist/source/index.js +0 -0
  41. package/dist/source/types.d.ts +0 -0
  42. package/dist/source/types.js +0 -0
  43. package/dist/source/utils/deep-freeze.d.ts +0 -0
  44. package/dist/source/utils/deep-freeze.js +0 -0
  45. package/dist/source/utils/deprecation-warning.d.ts +0 -0
  46. package/dist/source/utils/deprecation-warning.js +0 -0
  47. package/license +0 -0
  48. package/package.json +7 -7
  49. package/readme.md +110 -23
File without changes
File without changes
File without changes
@@ -133,9 +133,10 @@ function asPromise(normalizedOptions) {
133
133
  reject(error);
134
134
  };
135
135
  request.once('error', onError);
136
+ const previousBody = request.options.body;
136
137
  request.once('retry', (newRetryCount, error) => {
137
- var _a;
138
- if (is_1.default.nodeStream((_a = error.request) === null || _a === void 0 ? void 0 : _a.options.body)) {
138
+ var _a, _b;
139
+ if (previousBody === ((_a = error.request) === null || _a === void 0 ? void 0 : _a.options.body) && is_1.default.nodeStream((_b = error.request) === null || _b === void 0 ? void 0 : _b.options.body)) {
139
140
  onError(error);
140
141
  return;
141
142
  }
File without changes
File without changes
File without changes
@@ -11,7 +11,7 @@ const parseBody = (response, responseType, parseJson, encoding) => {
11
11
  return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
12
12
  }
13
13
  if (responseType === 'buffer') {
14
- return Buffer.from(rawBody);
14
+ return rawBody;
15
15
  }
16
16
  throw new types_1.ParseError({
17
17
  message: `Unknown body type '${responseType}'`,
File without changes
File without changes
File without changes
File without changes
@@ -41,12 +41,8 @@ export interface Agents {
41
41
  }
42
42
  export declare const withoutBody: ReadonlySet<string>;
43
43
  export interface ToughCookieJar {
44
- getCookieString: ((currentUrl: string, options: {
45
- [key: string]: unknown;
46
- }, cb: (err: Error | null, cookies: string) => void) => void) & ((url: string, callback: (error: Error | null, cookieHeader: string) => void) => void);
47
- setCookie: ((cookieOrString: unknown, currentUrl: string, options: {
48
- [key: string]: unknown;
49
- }, cb: (err: Error | null, cookie: unknown) => void) => void) & ((rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void) => void);
44
+ getCookieString: ((currentUrl: string, options: Record<string, unknown>, cb: (err: Error | null, cookies: string) => void) => void) & ((url: string, callback: (error: Error | null, cookieHeader: string) => void) => void);
45
+ setCookie: ((cookieOrString: unknown, currentUrl: string, options: Record<string, unknown>, cb: (err: Error | null, cookie: unknown) => void) => void) & ((rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void) => void);
50
46
  }
51
47
  export interface PromiseCookieJar {
52
48
  getCookieString: (url: string) => Promise<string>;
@@ -352,9 +348,7 @@ interface PlainOptions extends URLOptions {
352
348
 
353
349
  __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
354
350
  */
355
- form?: {
356
- [key: string]: any;
357
- };
351
+ form?: Record<string, any>;
358
352
  /**
359
353
  JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
360
354
 
@@ -362,9 +356,7 @@ interface PlainOptions extends URLOptions {
362
356
 
363
357
  __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
364
358
  */
365
- json?: {
366
- [key: string]: any;
367
- };
359
+ json?: Record<string, any>;
368
360
  /**
369
361
  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
362
 
@@ -415,9 +407,7 @@ interface PlainOptions extends URLOptions {
415
407
  //=> 'key=a&key=b'
416
408
  ```
417
409
  */
418
- searchParams?: string | {
419
- [key: string]: string | number | boolean | null | undefined;
420
- } | URLSearchParams;
410
+ searchParams?: string | Record<string, string | number | boolean | null | undefined> | URLSearchParams;
421
411
  /**
422
412
  An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
423
413
  Useful when making lots of requests to different *public* hostnames.
@@ -738,6 +728,7 @@ export interface HTTPSOptions {
738
728
  The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
739
729
  */
740
730
  passphrase?: SecureContextOptions['passphrase'];
731
+ pfx?: SecureContextOptions['pfx'];
741
732
  }
742
733
  interface NormalizedPlainOptions extends PlainOptions {
743
734
  method: Method;
@@ -152,7 +152,7 @@ class RequestError extends Error {
152
152
  }
153
153
  this.timings = (_a = this.request) === null || _a === void 0 ? void 0 : _a.timings;
154
154
  // Recover the original stacktrace
155
- if (!is_1.default.undefined(error.stack)) {
155
+ if (is_1.default.string(error.stack) && is_1.default.string(this.stack)) {
156
156
  const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
157
157
  const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
158
158
  const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message) + error.message.length).split('\n').reverse();
@@ -295,18 +295,28 @@ class Request extends stream_1.Duplex {
295
295
  if (json || body || form) {
296
296
  this._lockWrite();
297
297
  }
298
- (async (nonNormalizedOptions) => {
299
- var _a;
298
+ if (exports.kIsNormalizedAlready in options) {
299
+ this.options = options;
300
+ }
301
+ else {
300
302
  try {
301
- if (nonNormalizedOptions.body instanceof fs_1.ReadStream) {
302
- await waitForOpenFile(nonNormalizedOptions.body);
303
- }
304
- if (exports.kIsNormalizedAlready in nonNormalizedOptions) {
305
- this.options = nonNormalizedOptions;
303
+ // @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
304
+ this.options = this.constructor.normalizeArguments(url, options, defaults);
305
+ }
306
+ catch (error) {
307
+ // TODO: Move this to `_destroy()`
308
+ if (is_1.default.nodeStream(options.body)) {
309
+ options.body.destroy();
306
310
  }
307
- else {
308
- // @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
309
- this.options = this.constructor.normalizeArguments(url, nonNormalizedOptions, defaults);
311
+ this.destroy(error);
312
+ return;
313
+ }
314
+ }
315
+ (async () => {
316
+ var _a;
317
+ try {
318
+ if (this.options.body instanceof fs_1.ReadStream) {
319
+ await waitForOpenFile(this.options.body);
310
320
  }
311
321
  const { url: normalizedURL } = this.options;
312
322
  if (!normalizedURL) {
@@ -338,10 +348,10 @@ class Request extends stream_1.Duplex {
338
348
  this.destroy(error);
339
349
  }
340
350
  }
341
- })(options);
351
+ })();
342
352
  }
343
353
  static normalizeArguments(url, options, defaults) {
344
- var _a, _b, _c, _d;
354
+ var _a, _b, _c, _d, _e;
345
355
  const rawOptions = options;
346
356
  if (is_1.default.object(url) && !is_1.default.urlInstance(url)) {
347
357
  options = { ...defaults, ...url, ...options };
@@ -394,6 +404,7 @@ class Request extends stream_1.Duplex {
394
404
  is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.key);
395
405
  is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificate);
396
406
  is_1.assert.any([is_1.default.string, is_1.default.undefined], options.https.passphrase);
407
+ is_1.assert.any([is_1.default.string, is_1.default.buffer, is_1.default.array, is_1.default.undefined], options.https.pfx);
397
408
  }
398
409
  is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cacheOptions);
399
410
  // `options.method`
@@ -453,15 +464,15 @@ class Request extends stream_1.Duplex {
453
464
  options.username = (_b = options.username) !== null && _b !== void 0 ? _b : '';
454
465
  options.password = (_c = options.password) !== null && _c !== void 0 ? _c : '';
455
466
  // `options.prefixUrl` & `options.url`
456
- if (options.prefixUrl) {
467
+ if (is_1.default.undefined(options.prefixUrl)) {
468
+ options.prefixUrl = (_d = defaults === null || defaults === void 0 ? void 0 : defaults.prefixUrl) !== null && _d !== void 0 ? _d : '';
469
+ }
470
+ else {
457
471
  options.prefixUrl = options.prefixUrl.toString();
458
472
  if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) {
459
473
  options.prefixUrl += '/';
460
474
  }
461
475
  }
462
- else {
463
- options.prefixUrl = '';
464
- }
465
476
  if (is_1.default.string(options.url)) {
466
477
  if (options.url.startsWith('/')) {
467
478
  throw new Error('`input` must not start with a slash when using `prefixUrl`');
@@ -472,6 +483,9 @@ class Request extends stream_1.Duplex {
472
483
  options.url = options_to_url_1.default(options.prefixUrl, options);
473
484
  }
474
485
  if (options.url) {
486
+ if ('port' in options) {
487
+ delete options.port;
488
+ }
475
489
  // Make it possible to change `options.prefixUrl`
476
490
  let { prefixUrl } = options;
477
491
  Object.defineProperty(options, 'prefixUrl', {
@@ -614,7 +628,7 @@ class Request extends stream_1.Duplex {
614
628
  if (defaults && !areHooksDefault) {
615
629
  for (const event of exports.knownHookEvents) {
616
630
  const defaultHooks = defaults.hooks[event];
617
- if (defaultHooks.length !== 0) {
631
+ if (defaultHooks.length > 0) {
618
632
  // See https://github.com/microsoft/TypeScript/issues/31445#issuecomment-576929044
619
633
  options.hooks[event] = [
620
634
  ...defaults.hooks[event],
@@ -649,6 +663,9 @@ class Request extends stream_1.Duplex {
649
663
  if ('passphrase' in options) {
650
664
  deprecation_warning_1.default('"options.passphrase" was never documented, please use "options.https.passphrase"');
651
665
  }
666
+ if ('pfx' in options) {
667
+ deprecation_warning_1.default('"options.pfx" was never documented, please use "options.https.pfx"');
668
+ }
652
669
  // Other options
653
670
  if ('followRedirects' in options) {
654
671
  throw new TypeError('The `followRedirects` option does not exist. Use `followRedirect` instead.');
@@ -660,7 +677,7 @@ class Request extends stream_1.Duplex {
660
677
  }
661
678
  }
662
679
  }
663
- options.maxRedirects = (_d = options.maxRedirects) !== null && _d !== void 0 ? _d : 0;
680
+ options.maxRedirects = (_e = options.maxRedirects) !== null && _e !== void 0 ? _e : 0;
664
681
  // Set non-enumerable properties
665
682
  exports.setNonEnumerableProperties([defaults, rawOptions], options);
666
683
  return normalize_arguments_1.default(options, defaults);
@@ -826,6 +843,8 @@ class Request extends stream_1.Duplex {
826
843
  if ('form' in options) {
827
844
  delete options.form;
828
845
  }
846
+ this[kBody] = undefined;
847
+ delete options.headers['content-length'];
829
848
  }
830
849
  if (this.redirects.length >= options.maxRedirects) {
831
850
  this._beforeError(new MaxRedirectsError(this));
@@ -850,16 +869,14 @@ class Request extends stream_1.Duplex {
850
869
  delete options.headers.authorization;
851
870
  }
852
871
  if (options.username || options.password) {
853
- // TODO: Fix this ignore.
854
- // @ts-expect-error
855
- delete options.username;
856
- // @ts-expect-error
857
- delete options.password;
858
- }
859
- if ('port' in options) {
860
- delete options.port;
872
+ options.username = '';
873
+ options.password = '';
861
874
  }
862
875
  }
876
+ else {
877
+ redirectUrl.username = options.username;
878
+ redirectUrl.password = options.password;
879
+ }
863
880
  this.redirects.push(redirectString);
864
881
  options.url = redirectUrl;
865
882
  for (const hook of options.hooks.beforeRedirect) {
@@ -933,12 +950,7 @@ class Request extends stream_1.Duplex {
933
950
  request.destroy();
934
951
  // Node.js <= 12.18.2 mistakenly emits the response `end` first.
935
952
  (_a = request.res) === null || _a === void 0 ? void 0 : _a.removeAllListeners('end');
936
- if (error instanceof timed_out_1.TimeoutError) {
937
- error = new TimeoutError(error, this.timings, this);
938
- }
939
- else {
940
- error = new RequestError(error.message, error, this);
941
- }
953
+ error = error instanceof timed_out_1.TimeoutError ? new TimeoutError(error, this.timings, this) : new RequestError(error.message, error, this);
942
954
  this._beforeError(error);
943
955
  });
944
956
  this[kUnproxyEvents] = proxy_events_1.default(request, this, proxiedRequestEvents);
@@ -1026,6 +1038,9 @@ class Request extends stream_1.Duplex {
1026
1038
  break;
1027
1039
  }
1028
1040
  }
1041
+ if (options.body && this[kBody] !== options.body) {
1042
+ this[kBody] = options.body;
1043
+ }
1029
1044
  const { agent, request, timeout, url } = options;
1030
1045
  if (options.dnsCache && !('lookup' in options)) {
1031
1046
  options.lookup = options.dnsCache.lookup;
@@ -1098,6 +1113,9 @@ class Request extends stream_1.Duplex {
1098
1113
  if (options.https.passphrase) {
1099
1114
  requestOptions.passphrase = options.https.passphrase;
1100
1115
  }
1116
+ if (options.https.pfx) {
1117
+ requestOptions.pfx = options.https.pfx;
1118
+ }
1101
1119
  }
1102
1120
  try {
1103
1121
  let requestOrResponse = await fn(url, requestOptions);
@@ -1129,6 +1147,9 @@ class Request extends stream_1.Duplex {
1129
1147
  if (options.https.passphrase) {
1130
1148
  delete requestOptions.passphrase;
1131
1149
  }
1150
+ if (options.https.pfx) {
1151
+ delete requestOptions.pfx;
1152
+ }
1132
1153
  }
1133
1154
  if (isClientRequest(requestOrResponse)) {
1134
1155
  this._onRequest(requestOrResponse);
@@ -1182,9 +1203,9 @@ class Request extends stream_1.Duplex {
1182
1203
  response.setEncoding(this._readableState.encoding);
1183
1204
  try {
1184
1205
  response.rawBody = await get_buffer_1.default(response);
1206
+ response.body = response.rawBody.toString();
1185
1207
  }
1186
1208
  catch (_a) { }
1187
- response.body = response.rawBody.toString();
1188
1209
  }
1189
1210
  if (this.listenerCount('retry') !== 0) {
1190
1211
  let backoff;
@@ -1280,6 +1301,10 @@ class Request extends stream_1.Duplex {
1280
1301
  }
1281
1302
  }
1282
1303
  _writeRequest(chunk, encoding, callback) {
1304
+ if (this[kRequest].destroyed) {
1305
+ // Probably the `ClientRequest` instance will throw
1306
+ return;
1307
+ }
1283
1308
  this._progressCallbacks.push(() => {
1284
1309
  this[kUploadedSize] += Buffer.byteLength(chunk, encoding);
1285
1310
  const progress = this.uploadProgress;
@@ -1289,7 +1314,7 @@ class Request extends stream_1.Duplex {
1289
1314
  });
1290
1315
  // TODO: What happens if it's from cache? Then this[kRequest] won't be defined.
1291
1316
  this[kRequest].write(chunk, encoding, (error) => {
1292
- if (!error && this._progressCallbacks.length !== 0) {
1317
+ if (!error && this._progressCallbacks.length > 0) {
1293
1318
  this._progressCallbacks.shift()();
1294
1319
  }
1295
1320
  callback(error);
@@ -1353,7 +1378,7 @@ class Request extends stream_1.Duplex {
1353
1378
  */
1354
1379
  get ip() {
1355
1380
  var _a;
1356
- return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket.remoteAddress;
1381
+ return (_a = this.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress;
1357
1382
  }
1358
1383
  /**
1359
1384
  Indicates whether the request has been aborted or not.
@@ -1363,8 +1388,8 @@ class Request extends stream_1.Duplex {
1363
1388
  return ((_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.destroyed) !== null && _b !== void 0 ? _b : this.destroyed) && !((_c = this[kOriginalResponse]) === null || _c === void 0 ? void 0 : _c.complete);
1364
1389
  }
1365
1390
  get socket() {
1366
- var _a;
1367
- return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket;
1391
+ var _a, _b;
1392
+ return (_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket) !== null && _b !== void 0 ? _b : undefined;
1368
1393
  }
1369
1394
  /**
1370
1395
  Progress event for downloading (receiving a response).
File without changes
File without changes
File without changes
@@ -23,6 +23,9 @@ exports.default = async (body, headers) => {
23
23
  }
24
24
  if (body instanceof fs_1.ReadStream) {
25
25
  const { size } = await statAsync(body.path);
26
+ if (size === 0) {
27
+ return undefined;
28
+ }
26
29
  return size;
27
30
  }
28
31
  return undefined;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -14,7 +14,7 @@ exports.default = (url) => {
14
14
  href: url.href,
15
15
  path: `${url.pathname || ''}${url.search || ''}`
16
16
  };
17
- if (is_1.default.string(url.port) && url.port.length !== 0) {
17
+ if (is_1.default.string(url.port) && url.port.length > 0) {
18
18
  options.port = Number(url.port);
19
19
  }
20
20
  if (url.username || url.password) {
File without changes
File without changes
File without changes
@@ -82,7 +82,7 @@ const create = (defaults) => {
82
82
  return result;
83
83
  }));
84
84
  // Got interface
85
- const got = ((url, options, _defaults) => {
85
+ const got = ((url, options = {}, _defaults) => {
86
86
  var _a, _b;
87
87
  let iteration = 0;
88
88
  const iterateHandlers = (newOptions) => {
@@ -103,7 +103,7 @@ const create = (defaults) => {
103
103
  let initHookError;
104
104
  try {
105
105
  callInitHooks(defaults.options.hooks.init, options);
106
- callInitHooks((_a = options === null || options === void 0 ? void 0 : options.hooks) === null || _a === void 0 ? void 0 : _a.init, options);
106
+ callInitHooks((_a = options.hooks) === null || _a === void 0 ? void 0 : _a.init, options);
107
107
  }
108
108
  catch (error) {
109
109
  initHookError = error;
@@ -117,11 +117,11 @@ const create = (defaults) => {
117
117
  return iterateHandlers(normalizedOptions);
118
118
  }
119
119
  catch (error) {
120
- if (options === null || options === void 0 ? void 0 : options.isStream) {
120
+ if (options.isStream) {
121
121
  throw error;
122
122
  }
123
123
  else {
124
- return create_rejection_1.default(error, defaults.options.hooks.beforeError, (_b = options === null || options === void 0 ? void 0 : options.hooks) === null || _b === void 0 ? void 0 : _b.beforeError);
124
+ return create_rejection_1.default(error, defaults.options.hooks.beforeError, (_b = options.hooks) === null || _b === void 0 ? void 0 : _b.beforeError);
125
125
  }
126
126
  }
127
127
  });
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/license CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "11.6.1",
3
+ "version": "11.8.1",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -43,7 +43,7 @@
43
43
  "ky"
44
44
  ],
45
45
  "dependencies": {
46
- "@sindresorhus/is": "^3.1.1",
46
+ "@sindresorhus/is": "^4.0.0",
47
47
  "@szmarczak/http-timer": "^4.0.5",
48
48
  "@types/cacheable-request": "^6.0.1",
49
49
  "@types/responselike": "^1.0.0",
@@ -61,7 +61,7 @@
61
61
  "@sinonjs/fake-timers": "^6.0.1",
62
62
  "@types/benchmark": "^1.0.33",
63
63
  "@types/express": "^4.17.7",
64
- "@types/node": "^14.6.0",
64
+ "@types/node": "^14.14.0",
65
65
  "@types/node-fetch": "^2.5.7",
66
66
  "@types/pem": "^1.9.5",
67
67
  "@types/pify": "^3.0.2",
@@ -87,11 +87,11 @@
87
87
  "pify": "^5.0.0",
88
88
  "sinon": "^9.0.3",
89
89
  "slow-stream": "0.0.4",
90
- "tempy": "^0.6.0",
90
+ "tempy": "^1.0.0",
91
91
  "to-readable-stream": "^2.1.0",
92
92
  "tough-cookie": "^4.0.0",
93
- "typescript": "^4.0.2",
94
- "xo": "^0.33.0"
93
+ "typescript": "4.0.3",
94
+ "xo": "^0.34.1"
95
95
  },
96
96
  "types": "dist/source",
97
97
  "sideEffects": false,
@@ -123,7 +123,7 @@
123
123
  "node/prefer-global/url": "off",
124
124
  "node/prefer-global/url-search-params": "off",
125
125
  "import/no-anonymous-default-export": "off",
126
- "@typescript-eslint/no-invalid-void-type": "off"
126
+ "@typescript-eslint/no-implicit-any-catch": "off"
127
127
  }
128
128
  },
129
129
  "runkitExampleFilename": "./documentation/examples/runkit-example.js"
package/readme.md CHANGED
@@ -107,16 +107,16 @@ const got = require('got');
107
107
  const pipeline = promisify(stream.pipeline);
108
108
 
109
109
  (async () => {
110
- await pipeline(
111
- got.stream('https://sindresorhus.com'),
112
- fs.createWriteStream('index.html')
113
- );
114
-
115
- // For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
116
- await pipeline(
117
- fs.createReadStream('index.html'),
118
- got.stream.post('https://sindresorhus.com')
119
- );
110
+ await pipeline(
111
+ got.stream('https://sindresorhus.com'),
112
+ fs.createWriteStream('index.html')
113
+ );
114
+
115
+ // For POST, PUT, PATCH, and DELETE methods, `got.stream` returns a `stream.Writable`.
116
+ await pipeline(
117
+ fs.createReadStream('index.html'),
118
+ got.stream.post('https://sindresorhus.com')
119
+ );
120
120
  })();
121
121
  ```
122
122
 
@@ -146,7 +146,7 @@ If no protocol is specified, it will throw a `TypeError`.
146
146
 
147
147
  **Note:** The query string is **not** parsed as search params. Example:
148
148
 
149
- ```
149
+ ```js
150
150
  got('https://example.com/?query=a b'); //=> https://example.com/?query=a%20b
151
151
  got('https://example.com/', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
152
152
 
@@ -306,7 +306,7 @@ Example:
306
306
  const bufferPromise = responsePromise.buffer();
307
307
  const jsonPromise = responsePromise.json();
308
308
 
309
- const [response, buffer, json] = Promise.all([responsePromise, bufferPromise, jsonPromise]);
309
+ const [response, buffer, json] = await Promise.all([responsePromise, bufferPromise, jsonPromise]);
310
310
  // `response` is an instance of Got Response
311
311
  // `buffer` is an instance of Buffer
312
312
  // `json` is an object
@@ -321,6 +321,8 @@ const body = await got(url).json();
321
321
  const body = await got(url, {responseType: 'json', resolveBodyOnly: true});
322
322
  ```
323
323
 
324
+ **Note:** `buffer` will return the raw body buffer. Modifying it will also alter the result of `promise.text()` and `promise.json()`. Before overwritting the buffer, please copy it first via `Buffer.from(buffer)`. See https://github.com/nodejs/node/issues/27080
325
+
324
326
  ###### parseJson
325
327
 
326
328
  Type: `(text: string) => unknown`\
@@ -519,6 +521,8 @@ Delays between retries counts with function `1000 * Math.pow(2, retry - 1) + Mat
519
521
 
520
522
  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. The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
521
523
 
524
+ **Note:** The `calculateDelay` function is responsible for the entire cache mechanism, including the `limit` property. To support it, you need to check whether `computedValue` is different than `0`.
525
+
522
526
  By default, it retries *only* on the specified methods, status codes, and on these network errors:
523
527
  - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
524
528
  - `ECONNRESET`: Connection was forcibly closed by a peer.
@@ -651,6 +655,15 @@ await got('https://api6.ipify.org', {
651
655
  });
652
656
  ```
653
657
 
658
+ ###### lookup
659
+
660
+ Type: `Function`\
661
+ Default: [`dns.lookup`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback)
662
+
663
+ Custom DNS resolution logic.
664
+
665
+ The function signature is the same as [`dns.lookup`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback).
666
+
654
667
  ###### request
655
668
 
656
669
  Type: `Function`\
@@ -726,7 +739,7 @@ Called with plain [request options](#options), right before their normalization.
726
739
  See the [Request migration guide](documentation/migration-guides.md#breaking-changes) for an example.
727
740
 
728
741
  **Note #1:** This hook must be synchronous!\
729
- **Note #2:** Errors in this hook will be converted into an instances of [`RequestError`](#got.requesterror).\
742
+ **Note #2:** Errors in this hook will be converted into an instances of [`RequestError`](#gotrequesterror).\
730
743
  **Note #3:** The options object may not have a `url` property. To modify it, use a `beforeRequest` hook instead.
731
744
 
732
745
  ###### hooks.beforeRequest
@@ -736,6 +749,24 @@ Default: `[]`
736
749
 
737
750
  Called with [normalized](source/core/index.ts) [request options](#options). Got will make no further changes to the request before it is sent. This is especially useful in conjunction with [`got.extend()`](#instances) when you want to create an API client that, for example, uses HMAC-signing.
738
751
 
752
+ **Note:** Changing `options.json` or `options.form` has no effect on the request, you should change `options.body` instead. If needed, update the `options.headers` accordingly. Example:
753
+
754
+ ```js
755
+ const got = require('got');
756
+
757
+ got.post({
758
+ json: {payload: 'old'},
759
+ hooks: {
760
+ beforeRequest: [
761
+ options => {
762
+ options.body = JSON.stringify({payload: 'new'});
763
+ options.headers['content-length'] = options.body.length.toString();
764
+ }
765
+ ]
766
+ }
767
+ });
768
+ ```
769
+
739
770
  **Tip:** You can override the `request` function by returning a [`ClientRequest`-like](https://nodejs.org/api/http.html#http_class_http_clientrequest) instance or a [`IncomingMessage`-like](https://nodejs.org/api/http.html#http_class_http_incomingmessage) instance. This is very useful when creating a custom cache mechanism.
740
771
 
741
772
  ###### hooks.beforeRedirect
@@ -851,12 +882,12 @@ got('https://api.github.com/some-endpoint', {
851
882
  beforeError: [
852
883
  error => {
853
884
  const {response} = error;
854
- if (response && response.body) {
885
+ if (response && response.body) {
855
886
  error.name = 'GitHubError';
856
887
  error.message = `${response.body.message} (${response.statusCode})`;
857
888
  }
858
889
 
859
- return error;
890
+ return error;
860
891
  }
861
892
  ]
862
893
  }
@@ -1020,7 +1051,24 @@ Type: `string`
1020
1051
 
1021
1052
  The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
1022
1053
 
1023
- ##### Examples for `https.key`, `https.certificate` and `https.passphrase`
1054
+ ##### https.pfx
1055
+
1056
+ Type: `string | Buffer | Array<string | Buffer | object>`
1057
+
1058
+ [PFX or PKCS12](https://en.wikipedia.org/wiki/PKCS_12) encoded private key and certificate chain. Using `options.https.pfx` is an alternative to providing `options.https.key` and `options.https.certificate` individually. A PFX is usually encrypted, and if it is, `options.https.passphrase` will be used to decrypt it.
1059
+
1060
+ Multiple PFX's can be be provided as an array of unencrypted buffers or an array of objects like:
1061
+
1062
+ ```ts
1063
+ {
1064
+ buffer: string | Buffer,
1065
+ passphrase?: string
1066
+ }
1067
+ ```
1068
+
1069
+ This object form can only occur in an array. If the provided buffers are encrypted, `object.passphrase` can be used to decrypt them. If `object.passphrase` is not provided, `options.https.passphrase` will be used for decryption.
1070
+
1071
+ ##### Examples for `https.key`, `https.certificate`, `https.passphrase`, and `https.pfx`
1024
1072
 
1025
1073
  ```js
1026
1074
  // Single key with certificate
@@ -1067,6 +1115,45 @@ got('https://example.com', {
1067
1115
  ]
1068
1116
  }
1069
1117
  });
1118
+
1119
+ // Single encrypted PFX with passphrase
1120
+ got('https://example.com', {
1121
+ https: {
1122
+ pfx: fs.readFileSync('./fake.pfx'),
1123
+ passphrase: 'passphrase'
1124
+ }
1125
+ });
1126
+
1127
+ // Multiple encrypted PFX's with different passphrases
1128
+ got('https://example.com', {
1129
+ https: {
1130
+ pfx: [
1131
+ {
1132
+ buffer: fs.readFileSync('./key1.pfx'),
1133
+ passphrase: 'passphrase1'
1134
+ },
1135
+ {
1136
+ buffer: fs.readFileSync('./key2.pfx'),
1137
+ passphrase: 'passphrase2'
1138
+ }
1139
+ ]
1140
+ }
1141
+ });
1142
+
1143
+ // Multiple encrypted PFX's with single passphrase
1144
+ got('https://example.com', {
1145
+ https: {
1146
+ passphrase: 'passphrase',
1147
+ pfx: [
1148
+ {
1149
+ buffer: fs.readFileSync('./key1.pfx')
1150
+ },
1151
+ {
1152
+ buffer: fs.readFileSync('./key2.pfx')
1153
+ }
1154
+ ]
1155
+ }
1156
+ });
1070
1157
  ```
1071
1158
 
1072
1159
  ##### https.rejectUnauthorized
@@ -1320,7 +1407,7 @@ The same as `response.socket`.
1320
1407
 
1321
1408
  ##### .on('error', error)
1322
1409
 
1323
- The emitted `error` is an instance of [`RequestError`](#got.requesterror).
1410
+ The emitted `error` is an instance of [`RequestError`](#gotrequesterror).
1324
1411
 
1325
1412
  #### Pagination
1326
1413
 
@@ -2194,12 +2281,12 @@ The Electron `net` module is not consistent with the Node.js `http` module. See
2194
2281
  [ab]: https://badgen.net/travis/axios/axios?label
2195
2282
  [sb]: https://badgen.net/travis/visionmedia/superagent?label
2196
2283
 
2197
- [g5]: https://travis-ci.org/sindresorhus/got
2198
- [k5]: https://travis-ci.org/sindresorhus/ky
2199
- [r5]: https://travis-ci.org/request/request
2200
- [n5]: https://travis-ci.org/bitinn/node-fetch
2201
- [a5]: https://travis-ci.org/axios/axios
2202
- [s5]: https://travis-ci.org/visionmedia/superagent
2284
+ [g5]: https://travis-ci.com/github/sindresorhus/got
2285
+ [k5]: https://travis-ci.com/github/sindresorhus/ky
2286
+ [r5]: https://travis-ci.org/github/request/request
2287
+ [n5]: https://travis-ci.org/github/bitinn/node-fetch
2288
+ [a5]: https://travis-ci.org/github/axios/axios
2289
+ [s5]: https://travis-ci.org/github/visionmedia/superagent
2203
2290
 
2204
2291
  <!-- BUGS -->
2205
2292
  [gbg]: https://badgen.net/github/label-issues/sindresorhus/got/bug/open?label