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

Potentially problematic release.


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

Files changed (2) hide show
  1. package/index.js +24 -14
  2. package/package.json +1 -1
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
  };
@@ -63,9 +64,7 @@ RedirectableRequest.prototype = Object.create(Writable.prototype);
63
64
 
64
65
  RedirectableRequest.prototype.abort = function () {
65
66
  // Abort the internal request
66
- this._currentRequest.removeAllListeners();
67
- this._currentRequest.on("error", noop);
68
- this._currentRequest.abort();
67
+ abortRequest(this._currentRequest);
69
68
 
70
69
  // Abort this request
71
70
  this.emit("abort");
@@ -156,8 +155,14 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
156
155
  this.on("timeout", callback);
157
156
  }
158
157
 
158
+ function destroyOnTimeout(socket) {
159
+ socket.setTimeout(msecs);
160
+ socket.removeListener("timeout", socket.destroy);
161
+ socket.addListener("timeout", socket.destroy);
162
+ }
163
+
159
164
  // Sets up a timer to trigger a timeout event
160
- function startTimer() {
165
+ function startTimer(socket) {
161
166
  if (self._timeout) {
162
167
  clearTimeout(self._timeout);
163
168
  }
@@ -165,6 +170,7 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
165
170
  self.emit("timeout");
166
171
  clearTimer();
167
172
  }, msecs);
173
+ destroyOnTimeout(socket);
168
174
  }
169
175
 
170
176
  // Prevent a timeout from triggering
@@ -180,12 +186,13 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
180
186
 
181
187
  // Start the timer when the socket is opened
182
188
  if (this.socket) {
183
- startTimer();
189
+ startTimer(this.socket);
184
190
  }
185
191
  else {
186
192
  this._currentRequest.once("socket", startTimer);
187
193
  }
188
194
 
195
+ this.on("socket", destroyOnTimeout);
189
196
  this.once("response", clearTimer);
190
197
  this.once("error", clearTimer);
191
198
 
@@ -264,11 +271,8 @@ RedirectableRequest.prototype._performRequest = function () {
264
271
 
265
272
  // Set up event handlers
266
273
  request._redirectable = this;
267
- for (var event in eventHandlers) {
268
- /* istanbul ignore else */
269
- if (event) {
270
- request.on(event, eventHandlers[event]);
271
- }
274
+ for (var e = 0; e < events.length; e++) {
275
+ request.on(events[e], eventHandlers[events[e]]);
272
276
  }
273
277
 
274
278
  // End a redirected request
@@ -326,9 +330,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
326
330
  if (location && this._options.followRedirects !== false &&
327
331
  statusCode >= 300 && statusCode < 400) {
328
332
  // Abort the current request
329
- this._currentRequest.removeAllListeners();
330
- this._currentRequest.on("error", noop);
331
- this._currentRequest.abort();
333
+ abortRequest(this._currentRequest);
332
334
  // Discard the remainder of the response to avoid waiting for data
333
335
  response.destroy();
334
336
 
@@ -520,6 +522,14 @@ function createErrorType(code, defaultMessage) {
520
522
  return CustomError;
521
523
  }
522
524
 
525
+ function abortRequest(request) {
526
+ for (var e = 0; e < events.length; e++) {
527
+ request.removeListener(events[e], eventHandlers[events[e]]);
528
+ }
529
+ request.on("error", noop);
530
+ request.abort();
531
+ }
532
+
523
533
  // Exports
524
534
  module.exports = wrap({ http: http, https: https });
525
535
  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.0",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",