got 11.3.0 → 11.4.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.
@@ -110,6 +110,9 @@ class PromisableRequest extends core_1.default {
110
110
  return mergedOptions;
111
111
  }
112
112
  async _beforeError(error) {
113
+ if (this.destroyed) {
114
+ return;
115
+ }
113
116
  if (!(error instanceof core_1.RequestError)) {
114
117
  error = new core_1.RequestError(error.message, error, this);
115
118
  }
@@ -4,13 +4,13 @@ function __export(m) {
4
4
  }
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const events_1 = require("events");
7
- const getStream = require("get-stream");
8
7
  const PCancelable = require("p-cancelable");
9
8
  const calculate_retry_delay_1 = require("./calculate-retry-delay");
10
9
  const types_1 = require("./types");
11
10
  const core_1 = require("./core");
12
11
  exports.PromisableRequest = core_1.default;
13
12
  const proxy_events_1 = require("../core/utils/proxy-events");
13
+ const get_buffer_1 = require("../core/utils/get-buffer");
14
14
  const proxiedRequestEvents = [
15
15
  'request',
16
16
  'response',
@@ -52,6 +52,7 @@ function asPromise(options) {
52
52
  };
53
53
  globalRequest = request;
54
54
  const onResponse = async (response) => {
55
+ var _a;
55
56
  response.retryCount = retryCount;
56
57
  if (response.request.aborted) {
57
58
  // Canceled while downloading - will throw a `CancelError` or `TimeoutError` error
@@ -65,7 +66,7 @@ function asPromise(options) {
65
66
  // Download body
66
67
  let rawBody;
67
68
  try {
68
- rawBody = await getStream.buffer(request);
69
+ rawBody = await get_buffer_1.default(request);
69
70
  response.rawBody = rawBody;
70
71
  }
71
72
  catch (_) {
@@ -74,16 +75,23 @@ function asPromise(options) {
74
75
  return;
75
76
  }
76
77
  // Parse body
77
- try {
78
- response.body = core_1.parseBody(response, options.responseType, options.parseJson, options.encoding);
79
- }
80
- catch (error) {
81
- // Fallback to `utf8`
82
- response.body = rawBody.toString();
83
- if (isOk()) {
84
- // TODO: Call `request._beforeError`, see https://github.com/nodejs/node/issues/32995
85
- reject(error);
86
- return;
78
+ const contentEncoding = ((_a = response.headers['content-encoding']) !== null && _a !== void 0 ? _a : '').toLowerCase();
79
+ const isCompressed = ['gzip', 'deflate', 'br'].includes(contentEncoding);
80
+ if (isCompressed && !options.decompress) {
81
+ response.body = rawBody;
82
+ }
83
+ else {
84
+ try {
85
+ response.body = core_1.parseBody(response, options.responseType, options.parseJson, options.encoding);
86
+ }
87
+ catch (error) {
88
+ // Fallback to `utf8`
89
+ response.body = rawBody.toString();
90
+ if (isOk()) {
91
+ // TODO: Call `request._beforeError`, see https://github.com/nodejs/node/issues/32995
92
+ reject(error);
93
+ return;
94
+ }
87
95
  }
88
96
  }
89
97
  try {
@@ -127,8 +135,7 @@ function asPromise(options) {
127
135
  globalResponse = response;
128
136
  resolve(options.resolveBodyOnly ? response.body : response);
129
137
  };
130
- request.once('response', onResponse);
131
- request.once('error', async (error) => {
138
+ const onError = async (error) => {
132
139
  if (promise.isCanceled) {
133
140
  return;
134
141
  }
@@ -185,12 +192,18 @@ function asPromise(options) {
185
192
  if (error instanceof types_1.HTTPError) {
186
193
  // The error will be handled by the `response` event
187
194
  onResponse(request._response);
195
+ // Reattach the error handler, because there may be a timeout later.
196
+ process.nextTick(() => {
197
+ request.once('error', onError);
198
+ });
188
199
  return;
189
200
  }
190
201
  // Don't emit the `response` event
191
202
  request.destroy();
192
203
  reject(error);
193
- });
204
+ };
205
+ request.once('response', onResponse);
206
+ request.once('error', onError);
194
207
  proxy_events_1.default(request, emitter, proxiedRequestEvents);
195
208
  };
196
209
  makeRequest();
@@ -90,7 +90,7 @@ export interface Options extends URLOptions {
90
90
  cookieJar?: PromiseCookieJar | ToughCookieJar;
91
91
  ignoreInvalidCookies?: boolean;
92
92
  searchParams?: string | {
93
- [key: string]: string | number | boolean | null;
93
+ [key: string]: string | number | boolean | null | undefined;
94
94
  } | URLSearchParams;
95
95
  dnsCache?: CacheableLookup | boolean;
96
96
  context?: object;
@@ -207,6 +207,7 @@ export interface RequestEvents<T> {
207
207
  on<R extends Response, N extends NormalizedOptions>(name: 'redirect', listener: (response: R, nextOptions: N) => void): T;
208
208
  on(name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void): T;
209
209
  }
210
+ export declare const setNonEnumerableProperties: (sources: (Options | Defaults | undefined)[], to: Options) => void;
210
211
  export declare class RequestError extends Error {
211
212
  code?: string;
212
213
  stack: string;
@@ -14,7 +14,6 @@ const decompressResponse = require("decompress-response");
14
14
  // @ts-ignore Missing types
15
15
  const http2wrapper = require("http2-wrapper");
16
16
  const lowercaseKeys = require("lowercase-keys");
17
- const getStream = require("get-stream");
18
17
  const is_1 = require("@sindresorhus/is");
19
18
  const get_body_size_1 = require("./utils/get-body-size");
20
19
  const is_form_data_1 = require("./utils/is-form-data");
@@ -23,6 +22,7 @@ const timed_out_1 = require("./utils/timed-out");
23
22
  const url_to_options_1 = require("./utils/url-to-options");
24
23
  const options_to_url_1 = require("./utils/options-to-url");
25
24
  const weakable_map_1 = require("./utils/weakable-map");
25
+ const get_buffer_1 = require("./utils/get-buffer");
26
26
  const dns_ip_version_1 = require("./utils/dns-ip-version");
27
27
  const deprecation_warning_1 = require("../utils/deprecation-warning");
28
28
  const kRequest = Symbol('request');
@@ -49,7 +49,7 @@ function validateSearchParameters(searchParameters) {
49
49
  // eslint-disable-next-line guard-for-in
50
50
  for (const key in searchParameters) {
51
51
  const value = searchParameters[key];
52
- if (!is_1.default.string(value) && !is_1.default.number(value) && !is_1.default.boolean(value) && !is_1.default.null_(value)) {
52
+ if (!is_1.default.string(value) && !is_1.default.number(value) && !is_1.default.boolean(value) && !is_1.default.null_(value) && !is_1.default.undefined(value)) {
53
53
  throw new TypeError(`The \`searchParams\` value '${String(value)}' must be a string, number, boolean or null`);
54
54
  }
55
55
  }
@@ -78,7 +78,7 @@ const nonEnumerableProperties = [
78
78
  'json',
79
79
  'form'
80
80
  ];
81
- const setNonEnumerableProperties = (sources, to) => {
81
+ exports.setNonEnumerableProperties = (sources, to) => {
82
82
  // Non enumerable properties shall not be merged
83
83
  const properties = {};
84
84
  for (const source of sources) {
@@ -297,11 +297,11 @@ class Request extends stream_1.Duplex {
297
297
  options = { ...defaults, ...url, ...options };
298
298
  }
299
299
  else {
300
- if (url && options && options.url) {
300
+ if (url && options && options.url !== undefined) {
301
301
  throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
302
302
  }
303
303
  options = { ...defaults, ...options };
304
- if (url) {
304
+ if (url !== undefined) {
305
305
  options.url = url;
306
306
  }
307
307
  if (is_1.default.urlInstance(options.url)) {
@@ -370,10 +370,24 @@ class Request extends stream_1.Duplex {
370
370
  // `options.searchParams`
371
371
  if ('searchParams' in options) {
372
372
  if (options.searchParams && options.searchParams !== (defaults === null || defaults === void 0 ? void 0 : defaults.searchParams)) {
373
- if (!is_1.default.string(options.searchParams) && !(options.searchParams instanceof url_1.URLSearchParams)) {
373
+ let searchParameters;
374
+ if (is_1.default.string(options.searchParams) || (options.searchParams instanceof url_1.URLSearchParams)) {
375
+ searchParameters = new url_1.URLSearchParams(options.searchParams);
376
+ }
377
+ else {
374
378
  validateSearchParameters(options.searchParams);
379
+ searchParameters = new url_1.URLSearchParams();
380
+ // eslint-disable-next-line guard-for-in
381
+ for (const key in options.searchParams) {
382
+ const value = options.searchParams[key];
383
+ if (value === null) {
384
+ searchParameters.append(key, '');
385
+ }
386
+ else if (value !== undefined) {
387
+ searchParameters.append(key, value);
388
+ }
389
+ }
375
390
  }
376
- const searchParameters = new url_1.URLSearchParams(options.searchParams);
377
391
  // `normalizeArguments()` is also used to merge options
378
392
  (_a = defaults === null || defaults === void 0 ? void 0 : defaults.searchParams) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => {
379
393
  // Only use default if one isn't already defined
@@ -564,7 +578,7 @@ class Request extends stream_1.Duplex {
564
578
  }
565
579
  options.maxRedirects = (_d = options.maxRedirects) !== null && _d !== void 0 ? _d : 0;
566
580
  // Set non-enumerable properties
567
- setNonEnumerableProperties([defaults, rawOptions], options);
581
+ exports.setNonEnumerableProperties([defaults, rawOptions], options);
568
582
  return options;
569
583
  }
570
584
  _lockWrite() {
@@ -683,7 +697,8 @@ class Request extends stream_1.Duplex {
683
697
  response.once('aborted', () => {
684
698
  this._beforeError(new ReadError({
685
699
  name: 'Error',
686
- message: 'The server aborted the pending request'
700
+ message: 'The server aborted pending request',
701
+ code: 'ECONNRESET'
687
702
  }, this));
688
703
  });
689
704
  this.emit('downloadProgress', this.downloadProgress);
@@ -1022,7 +1037,7 @@ class Request extends stream_1.Duplex {
1022
1037
  const { response } = error;
1023
1038
  if (response) {
1024
1039
  response.setEncoding(this._readableState.encoding);
1025
- response.rawBody = await getStream.buffer(response);
1040
+ response.rawBody = await get_buffer_1.default(response);
1026
1041
  response.body = response.rawBody.toString();
1027
1042
  }
1028
1043
  }
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ declare const getBuffer: (stream: Readable) => Promise<Buffer>;
4
+ export default getBuffer;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // TODO: Update https://github.com/sindresorhus/get-stream
4
+ const getBuffer = async (stream) => {
5
+ const chunks = [];
6
+ let length = 0;
7
+ for await (const chunk of stream) {
8
+ chunks.push(chunk);
9
+ length += Buffer.byteLength(chunk);
10
+ }
11
+ if (Buffer.isBuffer(chunks[0])) {
12
+ return Buffer.concat(chunks, length);
13
+ }
14
+ return Buffer.from(chunks.join(''));
15
+ };
16
+ exports.default = getBuffer;
@@ -67,11 +67,14 @@ const create = (defaults) => {
67
67
  const iterateHandlers = (newOptions) => {
68
68
  return defaults.handlers[iteration++](newOptions, iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers);
69
69
  };
70
+ // TODO: remove this in Got 12
70
71
  if (is_1.default.plainObject(url)) {
71
- options = {
72
+ const mergedOptions = {
72
73
  ...url,
73
74
  ...options
74
75
  };
76
+ core_1.setNonEnumerableProperties([url, options], mergedOptions);
77
+ options = mergedOptions;
75
78
  url = undefined;
76
79
  }
77
80
  try {
@@ -143,7 +146,7 @@ const create = (defaults) => {
143
146
  while (numberOfRequests < pagination.requestLimit) {
144
147
  // TODO: Throw when result is not an instance of Response
145
148
  // eslint-disable-next-line no-await-in-loop
146
- const result = (await got('', normalizedOptions));
149
+ const result = (await got(normalizedOptions));
147
150
  // eslint-disable-next-line no-await-in-loop
148
151
  const parsed = await pagination.transform(result);
149
152
  const current = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "11.3.0",
3
+ "version": "11.4.0",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -50,7 +50,6 @@
50
50
  "cacheable-lookup": "^5.0.3",
51
51
  "cacheable-request": "^7.0.1",
52
52
  "decompress-response": "^6.0.0",
53
- "get-stream": "^5.1.0",
54
53
  "http2-wrapper": "^1.0.0-beta.4.5",
55
54
  "lowercase-keys": "^2.0.0",
56
55
  "p-cancelable": "^2.0.0",
@@ -76,6 +75,7 @@
76
75
  "delay": "^4.3.0",
77
76
  "express": "^4.17.1",
78
77
  "form-data": "^3.0.0",
78
+ "get-stream": "^5.1.0",
79
79
  "nock": "^12.0.0",
80
80
  "node-fetch": "^2.6.0",
81
81
  "np": "^6.0.0",
package/readme.md CHANGED
@@ -439,7 +439,7 @@ Default: `'utf8'`
439
439
 
440
440
  [Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data.
441
441
 
442
- To get a [`Buffer`](https://nodejs.org/api/buffer.html), you need to set [`responseType`](#responseType) to `buffer` instead.
442
+ To get a [`Buffer`](https://nodejs.org/api/buffer.html), you need to set [`responseType`](#responseType) to `buffer` instead. Don't set this option to `null`.
443
443
 
444
444
  **Note:** This doesn't affect streams! Instead, you need to do `got.stream(...).setEncoding(encoding)`.
445
445
 
@@ -473,6 +473,12 @@ console.log(searchParams.toString());
473
473
  //=> 'key=a&key=b'
474
474
  ```
475
475
 
476
+ There are some exceptions in regards to `URLSearchParams` behavior:
477
+
478
+ **Note #1:** `null` values are not stringified, an empty string is used instead.
479
+
480
+ **Note #2:** `undefined` values are not stringified, the entry is skipped instead.
481
+
476
482
  ###### timeout
477
483
 
478
484
  Type: `number | object`
@@ -1451,7 +1457,7 @@ Options are deeply merged to a new object. The value of each key is determined a
1451
1457
  - If the parent property is a plain `object`, the parent value is deeply cloned.
1452
1458
  - Otherwise, `undefined` is used.
1453
1459
  - If the parent value is an instance of `URLSearchParams`:
1454
- - If the new value is a `string`, an `object` or an instance of `URLSearchParams`, a new `URLSearchParams` instance is created. The values are merged using [`urlSearchParams.append(key, value)`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/append). The keys defined in the new value override the keys defined in the parent value.
1460
+ - If the new value is a `string`, an `object` or an instance of `URLSearchParams`, a new `URLSearchParams` instance is created. The values are merged using [`urlSearchParams.append(key, value)`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/append). The keys defined in the new value override the keys defined in the parent value. Please note that `null` values point to an empty string and `undefined` values will exclude the entry.
1455
1461
  - Otherwise, the only available value is `undefined`.
1456
1462
  - If the new property is a plain `object`:
1457
1463
  - If the parent property is a plain `object` too, both values are merged recursively into a new `object`.
@@ -2107,14 +2113,14 @@ The Electron `net` module is not consistent with the Node.js `http` module. See
2107
2113
  [kbg]: https://badgen.net/github/label-issues/sindresorhus/ky/bug/open?label
2108
2114
  [rbg]: https://badgen.net/github/label-issues/request/request/Needs%20investigation/open?label
2109
2115
  [nbg]: https://badgen.net/github/label-issues/bitinn/node-fetch/bug/open?label
2110
- [abg]: https://badgen.net/github/label-issues/axios/axios/type:bug/open?label
2116
+ [abg]: https://badgen.net/github/label-issues/axios/axios/type:confirmed%20bug/open?label
2111
2117
  [sbg]: https://badgen.net/github/label-issues/visionmedia/superagent/Bug/open?label
2112
2118
 
2113
2119
  [g6]: https://github.com/sindresorhus/got/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Abug
2114
2120
  [k6]: https://github.com/sindresorhus/ky/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Abug
2115
2121
  [r6]: https://github.com/request/request/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A"Needs+investigation"
2116
2122
  [n6]: https://github.com/bitinn/node-fetch/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Abug
2117
- [a6]: https://github.com/axios/axios/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Atype:bug
2123
+ [a6]: https://github.com/axios/axios/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22type%3Aconfirmed+bug%22
2118
2124
  [s6]: https://github.com/visionmedia/superagent/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3ABug
2119
2125
 
2120
2126
  <!-- DEPENDENTS -->