follow-redirects 1.13.3 → 1.14.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.

Potentially problematic release.


This version of follow-redirects might be problematic. Click here for more details.

Files changed (3) hide show
  1. package/debug.js +2 -1
  2. package/index.js +25 -19
  3. package/package.json +1 -1
package/debug.js CHANGED
@@ -6,7 +6,8 @@ module.exports = function () {
6
6
  /* eslint global-require: off */
7
7
  debug = require("debug")("follow-redirects");
8
8
  }
9
- catch (error) {
9
+ catch (error) { /* */ }
10
+ if (typeof debug !== "function") {
10
11
  debug = function () { /* */ };
11
12
  }
12
13
  }
package/index.js CHANGED
@@ -7,8 +7,9 @@ var assert = require("assert");
7
7
  var debug = require("./debug");
8
8
 
9
9
  // Create handlers that pass events from native requests
10
+ var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
10
11
  var eventHandlers = Object.create(null);
11
- ["abort", "aborted", "connect", "error", "socket", "timeout"].forEach(function (event) {
12
+ events.forEach(function (event) {
12
13
  eventHandlers[event] = function (arg1, arg2, arg3) {
13
14
  this._redirectable.emit(event, arg1, arg2, arg3);
14
15
  };
@@ -62,14 +63,8 @@ function RedirectableRequest(options, responseCallback) {
62
63
  RedirectableRequest.prototype = Object.create(Writable.prototype);
63
64
 
64
65
  RedirectableRequest.prototype.abort = function () {
65
- // Abort the internal request
66
- this._currentRequest.removeAllListeners();
67
- this._currentRequest.on("error", noop);
68
- this._currentRequest.abort();
69
-
70
- // Abort this request
66
+ abortRequest(this._currentRequest);
71
67
  this.emit("abort");
72
- this.removeAllListeners();
73
68
  };
74
69
 
75
70
  // Writes buffered data to the current native request
@@ -156,8 +151,14 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
156
151
  this.on("timeout", callback);
157
152
  }
158
153
 
154
+ function destroyOnTimeout(socket) {
155
+ socket.setTimeout(msecs);
156
+ socket.removeListener("timeout", socket.destroy);
157
+ socket.addListener("timeout", socket.destroy);
158
+ }
159
+
159
160
  // Sets up a timer to trigger a timeout event
160
- function startTimer() {
161
+ function startTimer(socket) {
161
162
  if (self._timeout) {
162
163
  clearTimeout(self._timeout);
163
164
  }
@@ -165,11 +166,12 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
165
166
  self.emit("timeout");
166
167
  clearTimer();
167
168
  }, msecs);
169
+ destroyOnTimeout(socket);
168
170
  }
169
171
 
170
172
  // Prevent a timeout from triggering
171
173
  function clearTimer() {
172
- clearTimeout(this._timeout);
174
+ clearTimeout(self._timeout);
173
175
  if (callback) {
174
176
  self.removeListener("timeout", callback);
175
177
  }
@@ -180,12 +182,13 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
180
182
 
181
183
  // Start the timer when the socket is opened
182
184
  if (this.socket) {
183
- startTimer();
185
+ startTimer(this.socket);
184
186
  }
185
187
  else {
186
188
  this._currentRequest.once("socket", startTimer);
187
189
  }
188
190
 
191
+ this.on("socket", destroyOnTimeout);
189
192
  this.once("response", clearTimer);
190
193
  this.once("error", clearTimer);
191
194
 
@@ -264,11 +267,8 @@ RedirectableRequest.prototype._performRequest = function () {
264
267
 
265
268
  // Set up event handlers
266
269
  request._redirectable = this;
267
- for (var event in eventHandlers) {
268
- /* istanbul ignore else */
269
- if (event) {
270
- request.on(event, eventHandlers[event]);
271
- }
270
+ for (var e = 0; e < events.length; e++) {
271
+ request.on(events[e], eventHandlers[events[e]]);
272
272
  }
273
273
 
274
274
  // End a redirected request
@@ -326,9 +326,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
326
326
  if (location && this._options.followRedirects !== false &&
327
327
  statusCode >= 300 && statusCode < 400) {
328
328
  // Abort the current request
329
- this._currentRequest.removeAllListeners();
330
- this._currentRequest.on("error", noop);
331
- this._currentRequest.abort();
329
+ abortRequest(this._currentRequest);
332
330
  // Discard the remainder of the response to avoid waiting for data
333
331
  response.destroy();
334
332
 
@@ -520,6 +518,14 @@ function createErrorType(code, defaultMessage) {
520
518
  return CustomError;
521
519
  }
522
520
 
521
+ function abortRequest(request) {
522
+ for (var e = 0; e < events.length; e++) {
523
+ request.removeListener(events[e], eventHandlers[events[e]]);
524
+ }
525
+ request.on("error", noop);
526
+ request.abort();
527
+ }
528
+
523
529
  // Exports
524
530
  module.exports = wrap({ http: http, https: https });
525
531
  module.exports.wrap = wrap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.13.3",
3
+ "version": "1.14.3",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",