got 11.6.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.
@@ -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}'`,
@@ -738,6 +738,7 @@ export interface HTTPSOptions {
738
738
  The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
739
739
  */
740
740
  passphrase?: SecureContextOptions['passphrase'];
741
+ pfx?: SecureContextOptions['pfx'];
741
742
  }
742
743
  interface NormalizedPlainOptions extends PlainOptions {
743
744
  method: Method;
@@ -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,7 +348,7 @@ 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
354
  var _a, _b, _c, _d, _e;
@@ -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`
@@ -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', {
@@ -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.');
@@ -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) {
@@ -1026,6 +1043,9 @@ class Request extends stream_1.Duplex {
1026
1043
  break;
1027
1044
  }
1028
1045
  }
1046
+ if (options.body && this[kBody] !== options.body) {
1047
+ this[kBody] = options.body;
1048
+ }
1029
1049
  const { agent, request, timeout, url } = options;
1030
1050
  if (options.dnsCache && !('lookup' in options)) {
1031
1051
  options.lookup = options.dnsCache.lookup;
@@ -1098,6 +1118,9 @@ class Request extends stream_1.Duplex {
1098
1118
  if (options.https.passphrase) {
1099
1119
  requestOptions.passphrase = options.https.passphrase;
1100
1120
  }
1121
+ if (options.https.pfx) {
1122
+ requestOptions.pfx = options.https.pfx;
1123
+ }
1101
1124
  }
1102
1125
  try {
1103
1126
  let requestOrResponse = await fn(url, requestOptions);
@@ -1129,6 +1152,9 @@ class Request extends stream_1.Duplex {
1129
1152
  if (options.https.passphrase) {
1130
1153
  delete requestOptions.passphrase;
1131
1154
  }
1155
+ if (options.https.pfx) {
1156
+ delete requestOptions.pfx;
1157
+ }
1132
1158
  }
1133
1159
  if (isClientRequest(requestOrResponse)) {
1134
1160
  this._onRequest(requestOrResponse);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "11.6.2",
3
+ "version": "11.7.0",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
package/readme.md CHANGED
@@ -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
 
@@ -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`\
@@ -738,6 +740,24 @@ Default: `[]`
738
740
 
739
741
  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.
740
742
 
743
+ **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:
744
+
745
+ ```js
746
+ const got = require('got');
747
+
748
+ got.post({
749
+ json: {payload: 'old'},
750
+ hooks: {
751
+ beforeRequest: [
752
+ options => {
753
+ options.body = JSON.stringify({payload: 'new'});
754
+ options.headers['content-length'] = options.body.length.toString();
755
+ }
756
+ ]
757
+ }
758
+ });
759
+ ```
760
+
741
761
  **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.
742
762
 
743
763
  ###### hooks.beforeRedirect
@@ -1022,7 +1042,24 @@ Type: `string`
1022
1042
 
1023
1043
  The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
1024
1044
 
1025
- ##### Examples for `https.key`, `https.certificate` and `https.passphrase`
1045
+ ##### https.pfx
1046
+
1047
+ Type: `string | Buffer | Array<string | Buffer | object>`
1048
+
1049
+ [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.
1050
+
1051
+ Multiple PFX's can be be provided as an array of unencrypted buffers or an array of objects like:
1052
+
1053
+ ```ts
1054
+ {
1055
+ buffer: string | Buffer,
1056
+ passphrase?: string
1057
+ }
1058
+ ```
1059
+
1060
+ 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.
1061
+
1062
+ ##### Examples for `https.key`, `https.certificate`, `https.passphrase`, and `https.pfx`
1026
1063
 
1027
1064
  ```js
1028
1065
  // Single key with certificate
@@ -1069,6 +1106,45 @@ got('https://example.com', {
1069
1106
  ]
1070
1107
  }
1071
1108
  });
1109
+
1110
+ // Single encrypted PFX with passphrase
1111
+ got('https://example.com', {
1112
+ https: {
1113
+ pfx: fs.readFileSync('./fake.pfx'),
1114
+ passphrase: 'passphrase'
1115
+ }
1116
+ });
1117
+
1118
+ // Multiple encrypted PFX's with different passphrases
1119
+ got('https://example.com', {
1120
+ https: {
1121
+ pfx: [
1122
+ {
1123
+ buffer: fs.readFileSync('./key1.pfx'),
1124
+ passphrase: 'passphrase1'
1125
+ },
1126
+ {
1127
+ buffer: fs.readFileSync('./key2.pfx'),
1128
+ passphrase: 'passphrase2'
1129
+ }
1130
+ ]
1131
+ }
1132
+ });
1133
+
1134
+ // Multiple encrypted PFX's with single passphrase
1135
+ got('https://example.com', {
1136
+ https: {
1137
+ passphrase: 'passphrase',
1138
+ pfx: [
1139
+ {
1140
+ buffer: fs.readFileSync('./key1.pfx')
1141
+ },
1142
+ {
1143
+ buffer: fs.readFileSync('./key2.pfx')
1144
+ }
1145
+ ]
1146
+ }
1147
+ });
1072
1148
  ```
1073
1149
 
1074
1150
  ##### https.rejectUnauthorized