got 9.3.1 → 9.3.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "9.3.1",
3
+ "version": "9.3.2",
4
4
  "description": "Simplified HTTP requests",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
package/readme.md CHANGED
@@ -264,7 +264,7 @@ An object representing `retries`, `methods`, `statusCodes` and `maxRetryAfter` f
264
264
  If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.<br>
265
265
  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.
266
266
 
267
- Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
267
+ Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
268
268
 
269
269
  The `retries` property can be a `number` or a `function` with `retry` and `error` arguments. The function must return a delay in milliseconds (`0` return value cancels retry).
270
270
 
@@ -236,7 +236,7 @@ const normalize = (url, options, defaults) => {
236
236
  }
237
237
 
238
238
  const noise = Math.random() * 100;
239
- return ((1 << iteration) * 1000) + noise;
239
+ return ((2 ** (iteration - 1)) * 1000) + noise;
240
240
  };
241
241
  }
242
242
 
@@ -27,7 +27,6 @@ module.exports = (options, input) => {
27
27
  let redirectString;
28
28
  let uploadBodySize;
29
29
  let retryCount = 0;
30
- let retryTries = 0;
31
30
  let shouldAbort = false;
32
31
 
33
32
  const setCookie = options.cookieJar ? util.promisify(options.cookieJar.setCookie.bind(options.cookieJar)) : null;
@@ -227,7 +226,7 @@ module.exports = (options, input) => {
227
226
  let backoff;
228
227
 
229
228
  try {
230
- backoff = options.retry.retries(++retryTries, error);
229
+ backoff = options.retry.retries(++retryCount, error);
231
230
  } catch (error2) {
232
231
  emitter.emit('error', error2);
233
232
  return;
@@ -241,7 +240,6 @@ module.exports = (options, input) => {
241
240
  await hook(options, error, retryCount);
242
241
  }
243
242
 
244
- retryCount++;
245
243
  await get(options);
246
244
  } catch (error) {
247
245
  emitter.emit('error', error);
@@ -12,31 +12,7 @@ class TimeoutError extends Error {
12
12
 
13
13
  const reentry = Symbol('reentry');
14
14
 
15
- function addTimeout(delay, callback, ...args) {
16
- // Event loop order is timers, poll, immediates.
17
- // The timed event may emit during the current tick poll phase, so
18
- // defer calling the handler until the poll phase completes.
19
- let immediate;
20
- const timeout = setTimeout(() => {
21
- immediate = setImmediate(callback, delay, ...args);
22
- /* istanbul ignore next: added in node v9.7.0 */
23
- if (immediate.unref) {
24
- immediate.unref();
25
- }
26
- }, delay);
27
-
28
- /* istanbul ignore next: in order to support electron renderer */
29
- if (timeout.unref) {
30
- timeout.unref();
31
- }
32
-
33
- const cancel = () => {
34
- clearTimeout(timeout);
35
- clearImmediate(immediate);
36
- };
37
-
38
- return cancel;
39
- }
15
+ const noop = () => {};
40
16
 
41
17
  module.exports = (request, delays, options) => {
42
18
  /* istanbul ignore next: this makes sure timed-out isn't called twice */
@@ -45,6 +21,43 @@ module.exports = (request, delays, options) => {
45
21
  }
46
22
 
47
23
  request[reentry] = true;
24
+
25
+ let stopNewTimeouts = false;
26
+
27
+ const addTimeout = (delay, callback, ...args) => {
28
+ // An error had been thrown before. Going further would result in uncaught errors.
29
+ // See https://github.com/sindresorhus/got/issues/631#issuecomment-435675051
30
+ if (stopNewTimeouts) {
31
+ return noop;
32
+ }
33
+
34
+ // Event loop order is timers, poll, immediates.
35
+ // The timed event may emit during the current tick poll phase, so
36
+ // defer calling the handler until the poll phase completes.
37
+ let immediate;
38
+ const timeout = setTimeout(() => {
39
+ immediate = setImmediate(callback, delay, ...args);
40
+ /* istanbul ignore next: added in node v9.7.0 */
41
+ if (immediate.unref) {
42
+ immediate.unref();
43
+ }
44
+ }, delay);
45
+
46
+ /* istanbul ignore next: in order to support electron renderer */
47
+ if (timeout.unref) {
48
+ timeout.unref();
49
+ }
50
+
51
+ const cancel = () => {
52
+ clearTimeout(timeout);
53
+ clearImmediate(immediate);
54
+ };
55
+
56
+ cancelers.push(cancel);
57
+
58
+ return cancel;
59
+ };
60
+
48
61
  const {host, hostname} = options;
49
62
  const timeoutHandler = (delay, event) => {
50
63
  request.emit('error', new TimeoutError(delay, event));
@@ -55,6 +68,7 @@ module.exports = (request, delays, options) => {
55
68
 
56
69
  const cancelers = [];
57
70
  const cancelTimeouts = () => {
71
+ stopNewTimeouts = true;
58
72
  cancelers.forEach(cancelTimeout => cancelTimeout());
59
73
  };
60
74
 
@@ -64,8 +78,7 @@ module.exports = (request, delays, options) => {
64
78
  });
65
79
 
66
80
  if (delays.request !== undefined) {
67
- const cancelTimeout = addTimeout(delays.request, timeoutHandler, 'request');
68
- cancelers.push(cancelTimeout);
81
+ addTimeout(delays.request, timeoutHandler, 'request');
69
82
  }
70
83
 
71
84
  if (delays.socket !== undefined) {
@@ -79,7 +92,6 @@ module.exports = (request, delays, options) => {
79
92
  /* istanbul ignore next: hard to test */
80
93
  if (socket.connecting) {
81
94
  const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
82
- cancelers.push(cancelTimeout);
83
95
  socket.once('lookup', cancelTimeout);
84
96
  }
85
97
  });
@@ -89,17 +101,15 @@ module.exports = (request, delays, options) => {
89
101
  request.once('socket', socket => {
90
102
  /* istanbul ignore next: hard to test */
91
103
  if (socket.connecting) {
92
- const timeConnect = () => {
93
- const cancelTimeout = addTimeout(delays.connect, timeoutHandler, 'connect');
94
- cancelers.push(cancelTimeout);
95
- return cancelTimeout;
96
- };
104
+ const timeConnect = () => addTimeout(delays.connect, timeoutHandler, 'connect');
97
105
 
98
106
  if (request.socketPath || net.isIP(hostname || host)) {
99
107
  socket.once('connect', timeConnect());
100
108
  } else {
101
- socket.once('lookup', () => {
102
- socket.once('connect', timeConnect());
109
+ socket.once('lookup', error => {
110
+ if (error === null) {
111
+ socket.once('connect', timeConnect());
112
+ }
103
113
  });
104
114
  }
105
115
  }
@@ -112,7 +122,6 @@ module.exports = (request, delays, options) => {
112
122
  if (socket.connecting) {
113
123
  socket.once('connect', () => {
114
124
  const cancelTimeout = addTimeout(delays.secureConnect, timeoutHandler, 'secureConnect');
115
- cancelers.push(cancelTimeout);
116
125
  socket.once('secureConnect', cancelTimeout);
117
126
  });
118
127
  }
@@ -121,11 +130,7 @@ module.exports = (request, delays, options) => {
121
130
 
122
131
  if (delays.send !== undefined) {
123
132
  request.once('socket', socket => {
124
- const timeRequest = () => {
125
- const cancelTimeout = addTimeout(delays.send, timeoutHandler, 'send');
126
- cancelers.push(cancelTimeout);
127
- return cancelTimeout;
128
- };
133
+ const timeRequest = () => addTimeout(delays.send, timeoutHandler, 'send');
129
134
  /* istanbul ignore next: hard to test */
130
135
  if (socket.connecting) {
131
136
  socket.once('connect', () => {
@@ -140,7 +145,6 @@ module.exports = (request, delays, options) => {
140
145
  if (delays.response !== undefined) {
141
146
  request.once('upload-complete', () => {
142
147
  const cancelTimeout = addTimeout(delays.response, timeoutHandler, 'response');
143
- cancelers.push(cancelTimeout);
144
148
  request.once('response', cancelTimeout);
145
149
  });
146
150
  }