got 11.0.0-beta.1 → 11.0.3

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.
@@ -1,8 +1,5 @@
1
+ /// <reference path="timed-out.d.ts" />
1
2
  /// <reference types="node" />
2
- import { ClientRequestArgs } from 'http';
3
- interface Options {
4
- body?: unknown;
5
- headers: ClientRequestArgs['headers'];
6
- }
7
- declare const _default: (options: Options) => Promise<number | undefined>;
3
+ /// <reference types="node/http" />
4
+ declare const _default: (body: unknown, headers: import("http").OutgoingHttpHeaders | undefined) => Promise<number | undefined>;
8
5
  export default _default;
@@ -5,8 +5,7 @@ const util_1 = require("util");
5
5
  const is_1 = require("@sindresorhus/is");
6
6
  const is_form_data_1 = require("./is-form-data");
7
7
  const statAsync = util_1.promisify(fs_1.stat);
8
- exports.default = async (options) => {
9
- const { body, headers } = options;
8
+ exports.default = async (body, headers) => {
10
9
  if (headers && 'content-length' in headers) {
11
10
  return Number(headers['content-length']);
12
11
  }
@@ -30,7 +30,7 @@ exports.default = (origin, options) => {
30
30
  if (!options.protocol) {
31
31
  throw new TypeError('No URL protocol specified');
32
32
  }
33
- origin = `${options.protocol}//${_b = (_a = options.hostname, (_a !== null && _a !== void 0 ? _a : options.host)), (_b !== null && _b !== void 0 ? _b : '')}`;
33
+ origin = `${options.protocol}//${(_b = (_a = options.hostname) !== null && _a !== void 0 ? _a : options.host) !== null && _b !== void 0 ? _b : ''}`;
34
34
  }
35
35
  const url = new url_1.URL(origin);
36
36
  if (options.path) {
@@ -21,9 +21,9 @@ exports.default = (request, delays, options) => {
21
21
  const cancelers = [];
22
22
  const { once, unhandleAll } = unhandle_1.default();
23
23
  const addTimeout = (delay, callback, event) => {
24
- var _a, _b;
24
+ var _a;
25
25
  const timeout = setTimeout(callback, delay, delay, event);
26
- (_b = (_a = timeout).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
26
+ (_a = timeout.unref) === null || _a === void 0 ? void 0 : _a.call(timeout);
27
27
  const cancel = () => {
28
28
  clearTimeout(timeout);
29
29
  };
@@ -32,9 +32,6 @@ exports.default = (request, delays, options) => {
32
32
  };
33
33
  const { host, hostname } = options;
34
34
  const timeoutHandler = (delay, event) => {
35
- if (request.socket) {
36
- request.socket._hadError = true;
37
- }
38
35
  request.destroy(new TimeoutError(delay, event));
39
36
  };
40
37
  const cancelTimeouts = () => {
@@ -46,6 +43,7 @@ exports.default = (request, delays, options) => {
46
43
  request.once('error', error => {
47
44
  cancelTimeouts();
48
45
  // Save original behavior
46
+ /* istanbul ignore next */
49
47
  if (request.listenerCount('error') === 0) {
50
48
  throw error;
51
49
  }
@@ -74,7 +72,7 @@ exports.default = (request, delays, options) => {
74
72
  const { socketPath } = request;
75
73
  /* istanbul ignore next: hard to test */
76
74
  if (socket.connecting) {
77
- const hasPath = Boolean((socketPath !== null && socketPath !== void 0 ? socketPath : net.isIP((_a = (hostname !== null && hostname !== void 0 ? hostname : host), (_a !== null && _a !== void 0 ? _a : ''))) !== 0));
75
+ const hasPath = Boolean(socketPath !== null && socketPath !== void 0 ? socketPath : net.isIP((_a = hostname !== null && hostname !== void 0 ? hostname : host) !== null && _a !== void 0 ? _a : '') !== 0);
78
76
  if (typeof delays.lookup !== 'undefined' && !hasPath && typeof socket.address().address === 'undefined') {
79
77
  const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
80
78
  once(socket, 'lookup', cancelTimeout);
@@ -0,0 +1,8 @@
1
+ export default class WeakableMap<K, V> {
2
+ weakMap: WeakMap<object, V>;
3
+ map: Map<K, V>;
4
+ constructor();
5
+ set(key: K, value: V): void;
6
+ get(key: K): V | undefined;
7
+ has(key: K): boolean;
8
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class WeakableMap {
4
+ constructor() {
5
+ this.weakMap = new WeakMap();
6
+ this.map = new Map();
7
+ }
8
+ set(key, value) {
9
+ if (typeof key === 'object') {
10
+ this.weakMap.set(key, value);
11
+ }
12
+ else {
13
+ this.map.set(key, value);
14
+ }
15
+ }
16
+ get(key) {
17
+ if (typeof key === 'object') {
18
+ return this.weakMap.get(key);
19
+ }
20
+ return this.map.get(key);
21
+ }
22
+ has(key) {
23
+ if (typeof key === 'object') {
24
+ return this.weakMap.has(key);
25
+ }
26
+ return this.map.has(key);
27
+ }
28
+ }
29
+ exports.default = WeakableMap;
@@ -60,8 +60,8 @@ const create = (defaults) => {
60
60
  }
61
61
  return result;
62
62
  }));
63
- const got = ((url, options = {}) => {
64
- var _a, _b, _c, _d, _e;
63
+ const got = ((url, options) => {
64
+ var _a, _b;
65
65
  let iteration = 0;
66
66
  const iterateHandlers = (newOptions) => {
67
67
  return defaults.handlers[iteration++](newOptions, iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers);
@@ -78,7 +78,7 @@ const create = (defaults) => {
78
78
  let initHookError;
79
79
  try {
80
80
  callInitHooks(defaults.options.hooks.init, options);
81
- callInitHooks((_b = (_a = options) === null || _a === void 0 ? void 0 : _a.hooks) === null || _b === void 0 ? void 0 : _b.init, options);
81
+ callInitHooks((_a = options === null || options === void 0 ? void 0 : options.hooks) === null || _a === void 0 ? void 0 : _a.init, options);
82
82
  }
83
83
  catch (error) {
84
84
  initHookError = error;
@@ -94,13 +94,13 @@ const create = (defaults) => {
94
94
  return iterateHandlers(normalizedOptions);
95
95
  }
96
96
  catch (error) {
97
- if ((_c = options) === null || _c === void 0 ? void 0 : _c.isStream) {
97
+ if (options === null || options === void 0 ? void 0 : options.isStream) {
98
98
  throw error;
99
99
  }
100
100
  else {
101
101
  // A bug.
102
102
  // eslint-disable-next-line @typescript-eslint/return-await
103
- return create_rejection_1.default(error, defaults.options.hooks.beforeError, (_e = (_d = options) === null || _d === void 0 ? void 0 : _d.hooks) === null || _e === void 0 ? void 0 : _e.beforeError);
103
+ 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);
104
104
  }
105
105
  }
106
106
  });
@@ -140,7 +140,8 @@ const create = (defaults) => {
140
140
  throw new TypeError('`options.pagination` must be implemented');
141
141
  }
142
142
  const all = [];
143
- while (true) {
143
+ let numberOfRequests = 0;
144
+ while (numberOfRequests < pagination.requestLimit) {
144
145
  // TODO: Throw when result is not an instance of Response
145
146
  // eslint-disable-next-line no-await-in-loop
146
147
  const result = (await got('', normalizedOptions));
@@ -167,6 +168,7 @@ const create = (defaults) => {
167
168
  if (optionsToMerge !== undefined) {
168
169
  normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
169
170
  }
171
+ numberOfRequests++;
170
172
  }
171
173
  });
172
174
  got.paginate.all = (async (url, options) => {
@@ -104,7 +104,8 @@ const defaults = {
104
104
  },
105
105
  filter: () => true,
106
106
  shouldContinue: () => true,
107
- countLimit: Infinity
107
+ countLimit: Infinity,
108
+ requestLimit: 10000
108
109
  }
109
110
  },
110
111
  handlers: [create_1.defaultHandler],
@@ -17,22 +17,25 @@ export interface ExtendOptions extends Options {
17
17
  handlers?: HandlerFunction[];
18
18
  mutableDefaults?: boolean;
19
19
  }
20
- export declare type OptionsOfTextResponseBody = Options & {
20
+ export declare type OptionsOfTextResponseBody = Merge<Options, {
21
21
  isStream?: false;
22
22
  resolveBodyOnly?: false;
23
23
  responseType?: 'text';
24
- };
25
- export declare type OptionsOfJSONResponseBody = Options & {
24
+ }>;
25
+ export declare type OptionsOfJSONResponseBody = Merge<Options, {
26
26
  isStream?: false;
27
27
  resolveBodyOnly?: false;
28
28
  responseType: 'json';
29
- };
30
- export declare type OptionsOfBufferResponseBody = Options & {
29
+ }>;
30
+ export declare type OptionsOfBufferResponseBody = Merge<Options, {
31
31
  isStream?: false;
32
32
  resolveBodyOnly?: false;
33
33
  responseType: 'buffer';
34
- };
34
+ }>;
35
35
  export declare type StrictOptions = Except<Options, 'isStream' | 'responseType' | 'resolveBodyOnly'>;
36
+ export declare type StreamOptions = Merge<Options, {
37
+ isStream?: true;
38
+ }>;
36
39
  declare type ResponseBodyOnly = {
37
40
  resolveBodyOnly: true;
38
41
  };
@@ -50,29 +53,29 @@ export interface GotRequestFunction {
50
53
  (options: OptionsOfTextResponseBody): CancelableRequest<Response<string>>;
51
54
  <T>(options: OptionsOfJSONResponseBody): CancelableRequest<Response<T>>;
52
55
  (options: OptionsOfBufferResponseBody): CancelableRequest<Response<Buffer>>;
53
- (url: string | URL, options?: (OptionsOfTextResponseBody & ResponseBodyOnly)): CancelableRequest<string>;
54
- <T>(url: string | URL, options?: (OptionsOfJSONResponseBody & ResponseBodyOnly)): CancelableRequest<T>;
55
- (url: string | URL, options?: (OptionsOfBufferResponseBody & ResponseBodyOnly)): CancelableRequest<Buffer>;
56
- (options: (OptionsOfTextResponseBody & ResponseBodyOnly)): CancelableRequest<string>;
57
- <T>(options: (OptionsOfJSONResponseBody & ResponseBodyOnly)): CancelableRequest<T>;
58
- (options: (OptionsOfBufferResponseBody & ResponseBodyOnly)): CancelableRequest<Buffer>;
59
- (url: string | URL, options?: Options & {
56
+ (url: string | URL, options?: (Merge<OptionsOfTextResponseBody, ResponseBodyOnly>)): CancelableRequest<string>;
57
+ <T>(url: string | URL, options?: (Merge<OptionsOfJSONResponseBody, ResponseBodyOnly>)): CancelableRequest<T>;
58
+ (url: string | URL, options?: (Merge<OptionsOfBufferResponseBody, ResponseBodyOnly>)): CancelableRequest<Buffer>;
59
+ (options: (Merge<OptionsOfTextResponseBody, ResponseBodyOnly>)): CancelableRequest<string>;
60
+ <T>(options: (Merge<OptionsOfJSONResponseBody, ResponseBodyOnly>)): CancelableRequest<T>;
61
+ (options: (Merge<OptionsOfBufferResponseBody, ResponseBodyOnly>)): CancelableRequest<Buffer>;
62
+ (url: string | URL, options?: Merge<Options, {
60
63
  isStream: true;
61
- }): Request;
62
- (options: Options & {
64
+ }>): Request;
65
+ (options: Merge<Options, {
63
66
  isStream: true;
64
- }): Request;
67
+ }>): Request;
65
68
  (url: string | URL, options?: Options): CancelableRequest | Request;
66
69
  (options: Options): CancelableRequest | Request;
67
70
  }
68
71
  export declare type HTTPAlias = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
69
72
  interface GotStreamFunction {
70
- (url: string | URL, options?: Options & {
73
+ (url: string | URL, options?: Merge<Options, {
71
74
  isStream?: true;
72
- }): Request;
73
- (options?: Options & {
75
+ }>): Request;
76
+ (options?: Merge<Options, {
74
77
  isStream?: true;
75
- }): Request;
78
+ }>): Request;
76
79
  }
77
80
  export declare type GotStream = GotStreamFunction & Record<HTTPAlias, GotStreamFunction>;
78
81
  export interface Got extends Record<HTTPAlias, GotRequestFunction>, GotRequestFunction {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "11.0.0-beta.1",
3
+ "version": "11.0.3",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -51,7 +51,7 @@
51
51
  "cacheable-request": "^7.0.1",
52
52
  "decompress-response": "^5.0.0",
53
53
  "get-stream": "^5.0.0",
54
- "http2-wrapper": "^1.0.0-beta.4.3",
54
+ "http2-wrapper": "^1.0.0-beta.4.4",
55
55
  "lowercase-keys": "^2.0.0",
56
56
  "p-cancelable": "^2.0.0",
57
57
  "responselike": "^2.0.0"
@@ -66,9 +66,7 @@
66
66
  "@types/node-fetch": "^2.5.5",
67
67
  "@types/request": "^2.48.4",
68
68
  "@types/sinon": "^9.0.0",
69
- "@types/tough-cookie": "^2.3.5",
70
- "@typescript-eslint/eslint-plugin": "^2.27.0",
71
- "@typescript-eslint/parser": "^2.27.0",
69
+ "@types/tough-cookie": "^4.0.0",
72
70
  "ava": "^3.6.0",
73
71
  "axios": "^0.19.2",
74
72
  "benchmark": "^2.1.4",
@@ -76,7 +74,6 @@
76
74
  "create-test-server": "^3.0.1",
77
75
  "del-cli": "^3.0.0",
78
76
  "delay": "^4.3.0",
79
- "eslint-config-xo-typescript": "^0.27.0",
80
77
  "express": "^4.17.1",
81
78
  "form-data": "^3.0.0",
82
79
  "lolex": "^6.0.0",
@@ -89,9 +86,9 @@
89
86
  "slow-stream": "0.0.4",
90
87
  "tempy": "^0.5.0",
91
88
  "to-readable-stream": "^2.1.0",
92
- "tough-cookie": "^3.0.0",
93
- "typescript": "3.7.5",
94
- "xo": "^0.29.0"
89
+ "tough-cookie": "^4.0.0",
90
+ "typescript": "3.8.3",
91
+ "xo": "^0.30.0"
95
92
  },
96
93
  "types": "dist/source",
97
94
  "sideEffects": false,
@@ -115,10 +112,6 @@
115
112
  ]
116
113
  },
117
114
  "xo": {
118
- "extends": "xo-typescript",
119
- "extensions": [
120
- "ts"
121
- ],
122
115
  "ignores": [
123
116
  "documentation/examples/*"
124
117
  ],
@@ -127,11 +120,10 @@
127
120
  "@typescript-eslint/no-base-to-string": "off",
128
121
  "node/prefer-global/url": "off",
129
122
  "node/prefer-global/url-search-params": "off",
130
- "unicorn/string-content": "off",
131
123
  "@typescript-eslint/prefer-readonly-parameter-types": "off",
132
- "@typescript-eslint/no-unsafe-member-access": "off",
133
- "@typescript-eslint/no-unsafe-call": "off",
134
- "@typescript-eslint/no-unsafe-return": "off"
124
+ "@typescript-eslint/no-floating-promises": "off",
125
+ "@typescript-eslint/method-signature-style": "off",
126
+ "unicorn/no-fn-reference-in-iterator": "off"
135
127
  }
136
128
  }
137
129
  }
package/readme.md CHANGED
@@ -56,6 +56,8 @@ $ npm install got
56
56
 
57
57
  ## Usage
58
58
 
59
+ ###### Promise
60
+
59
61
  ```js
60
62
  const got = require('got');
61
63
 
@@ -71,6 +73,26 @@ const got = require('got');
71
73
  })();
72
74
  ```
73
75
 
76
+ ###### JSON
77
+
78
+ ```js
79
+ const got = require('got');
80
+
81
+ (async () => {
82
+ const {body} = await got.post('https://httpbin.org/anything', {
83
+ json: {
84
+ hello: 'world'
85
+ },
86
+ responseType: 'json'
87
+ });
88
+
89
+ console.log(body.data);
90
+ //=> {hello: 'world'}
91
+ })();
92
+ ```
93
+
94
+ See [JSON mode](#json-mode) for more details.
95
+
74
96
  ###### Streams
75
97
 
76
98
  ```js
@@ -452,14 +474,14 @@ If this is disabled, a compressed response is returned as a `Buffer`. This may b
452
474
 
453
475
  ###### cache
454
476
 
455
- Type: `object`\
477
+ Type: `object | false`\
456
478
  Default: `false`
457
479
 
458
480
  [Cache adapter instance](#cache-adapters) for storing cached response data.
459
481
 
460
482
  ###### dnsCache
461
483
 
462
- Type: `object`\
484
+ Type: `object | false`\
463
485
  Default: `new CacheableLookup()`
464
486
 
465
487
  An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
@@ -760,6 +782,15 @@ Default: `Infinity`
760
782
 
761
783
  The maximum amount of items that should be emitted.
762
784
 
785
+ ###### pagination.requestLimit
786
+
787
+ Type: `number`\
788
+ Default: `10000`
789
+
790
+ The maximum amount of request that should be triggered. [Retries on failure](#retry) are not counted towards this limit.
791
+
792
+ For example, it can be helpful during development to avoid an infinite number of requests.
793
+
763
794
  ##### localAddress
764
795
 
765
796
  Type: `string`
@@ -784,6 +815,12 @@ Type: `string | object | Buffer` *(Depending on `options.responseType`)*
784
815
 
785
816
  The result of the request.
786
817
 
818
+ ##### rawBody
819
+
820
+ Type: `Buffer`
821
+
822
+ The raw result of the request.
823
+
787
824
  ##### url
788
825
 
789
826
  Type: `string`
@@ -934,6 +971,10 @@ The same as `response.timings`.
934
971
 
935
972
  The same as `response.isFromCache`.
936
973
 
974
+ ##### .socket
975
+
976
+ The same as `response.socket`.
977
+
937
978
  ##### .on('error', error)
938
979
 
939
980
  The emitted `error` is an instance of [`RequestError`](#got.requesterror).
@@ -962,6 +1003,25 @@ Returns an async iterator:
962
1003
 
963
1004
  See [`options.pagination`](#pagination) for more pagination options.
964
1005
 
1006
+ #### got.paginate.all(url, options?)
1007
+
1008
+ Returns a Promise for an array of all results:
1009
+
1010
+ ```js
1011
+ (async () => {
1012
+ const countLimit = 10;
1013
+
1014
+ const results = await got.paginate.all('https://api.github.com/repos/sindresorhus/got/commits', {
1015
+ pagination: {countLimit}
1016
+ });
1017
+
1018
+ console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
1019
+ console.log(results);
1020
+ })();
1021
+ ```
1022
+
1023
+ See [`options.pagination`](#pagination) for more pagination options.
1024
+
965
1025
  #### got.get(url, options?)
966
1026
  #### got.post(url, options?)
967
1027
  #### got.put(url, options?)
@@ -1039,7 +1099,7 @@ const mergedHandlers = got.extend({
1039
1099
 
1040
1100
  ```js
1041
1101
  const handler = (options, next) => {
1042
- if (options.stream) {
1102
+ if (options.isStream) {
1043
1103
  // It's a Stream
1044
1104
  return next(options);
1045
1105
  }
@@ -1186,10 +1246,10 @@ Got exports some handy TypeScript types and interfaces. See the type definition
1186
1246
  TypeScript will automatically infer types for Got instances, but in case you want to define something like dependencies, you can import the available types directly from Got.
1187
1247
 
1188
1248
  ```ts
1189
- import {GotRequestMethod} from 'got';
1249
+ import {GotRequestFunction} from 'got';
1190
1250
 
1191
1251
  interface Dependencies {
1192
- readonly post: GotRequestMethod
1252
+ readonly post: GotRequestFunction
1193
1253
  }
1194
1254
  ```
1195
1255
 
@@ -1311,7 +1371,7 @@ stream.destroy();
1311
1371
  <a name="cache-adapters"></a>
1312
1372
  ## Cache
1313
1373
 
1314
- Got implements [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).
1374
+ Got implements [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).
1315
1375
 
1316
1376
  You can use the JavaScript `Map` type as an in-memory cache:
1317
1377
 
@@ -1372,11 +1432,13 @@ const got = require('got');
1372
1432
  const tunnel = require('tunnel');
1373
1433
 
1374
1434
  got('https://sindresorhus.com', {
1375
- agent: tunnel.httpOverHttp({
1376
- proxy: {
1377
- host: 'localhost'
1378
- }
1379
- })
1435
+ agent: {
1436
+ https: tunnel.httpOverHttp({
1437
+ proxy: {
1438
+ host: 'localhost'
1439
+ }
1440
+ })
1441
+ }
1380
1442
  });
1381
1443
  ```
1382
1444
 
@@ -1674,7 +1736,7 @@ The Electron `net` module is not consistent with the Node.js `http` module. See
1674
1736
  <!-- GITHUB -->
1675
1737
  [k0]: https://github.com/sindresorhus/ky
1676
1738
  [r0]: https://github.com/request/request
1677
- [n0]: https://github.com/bitinn/node-fetch
1739
+ [n0]: https://github.com/node-fetch/node-fetch
1678
1740
  [a0]: https://github.com/axios/axios
1679
1741
  [s0]: https://github.com/visionmedia/superagent
1680
1742