follow-redirects 1.9.1 → 1.10.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 +36 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -14,6 +14,24 @@ var eventHandlers = Object.create(null);
14
14
  };
15
15
  });
16
16
 
17
+ // Error types with codes
18
+ var RedirectionError = createErrorType(
19
+ "ERR_FR_REDIRECTION_FAILURE",
20
+ ""
21
+ );
22
+ var TooManyRedirectsError = createErrorType(
23
+ "ERR_FR_TOO_MANY_REDIRECTS",
24
+ "Maximum number of redirects exceeded"
25
+ );
26
+ var MaxBodyLengthExceededError = createErrorType(
27
+ "ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
28
+ "Request body larger than maxBodyLength limit"
29
+ );
30
+ var WriteAfterEndError = createErrorType(
31
+ "ERR_STREAM_WRITE_AFTER_END",
32
+ "write after end"
33
+ );
34
+
17
35
  // An HTTP(S) request that can be redirected
18
36
  function RedirectableRequest(options, responseCallback) {
19
37
  // Initialize the request
@@ -47,12 +65,12 @@ RedirectableRequest.prototype = Object.create(Writable.prototype);
47
65
  RedirectableRequest.prototype.write = function (data, encoding, callback) {
48
66
  // Writing is not allowed if end has been called
49
67
  if (this._ending) {
50
- throw new Error("write after end");
68
+ throw new WriteAfterEndError();
51
69
  }
52
70
 
53
71
  // Validate input and shift parameters if necessary
54
72
  if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
55
- throw new Error("data should be a string, Buffer or Uint8Array");
73
+ throw new TypeError("data should be a string, Buffer or Uint8Array");
56
74
  }
57
75
  if (typeof encoding === "function") {
58
76
  callback = encoding;
@@ -75,7 +93,7 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
75
93
  }
76
94
  // Error when we exceed the maximum body length
77
95
  else {
78
- this.emit("error", new Error("Request body larger than maxBodyLength limit"));
96
+ this.emit("error", new MaxBodyLengthExceededError());
79
97
  this.abort();
80
98
  }
81
99
  };
@@ -207,7 +225,7 @@ RedirectableRequest.prototype._performRequest = function () {
207
225
  var protocol = this._options.protocol;
208
226
  var nativeProtocol = this._options.nativeProtocols[protocol];
209
227
  if (!nativeProtocol) {
210
- this.emit("error", new Error("Unsupported protocol " + protocol));
228
+ this.emit("error", new TypeError("Unsupported protocol " + protocol));
211
229
  return;
212
230
  }
213
231
 
@@ -296,7 +314,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
296
314
  // RFC7231§6.4: A client SHOULD detect and intervene
297
315
  // in cyclical redirections (i.e., "infinite" redirection loops).
298
316
  if (++this._redirectCount > this._options.maxRedirects) {
299
- this.emit("error", new Error("Max redirects exceeded."));
317
+ this.emit("error", new TooManyRedirectsError());
300
318
  return;
301
319
  }
302
320
 
@@ -344,7 +362,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
344
362
  this._performRequest();
345
363
  }
346
364
  catch (cause) {
347
- var error = new Error("Cannot redirect: " + cause.message);
365
+ var error = new RedirectionError("Redirected request failed: " + cause.message);
348
366
  error.cause = cause;
349
367
  this.emit("error", error);
350
368
  }
@@ -454,6 +472,18 @@ function removeMatchingHeaders(regex, headers) {
454
472
  }
455
473
  }
456
474
 
475
+ function createErrorType(code, defaultMessage) {
476
+ function CustomError(message) {
477
+ Error.captureStackTrace(this, this.constructor);
478
+ this.message = message || defaultMessage;
479
+ }
480
+ CustomError.prototype = new Error();
481
+ CustomError.prototype.constructor = CustomError;
482
+ CustomError.prototype.name = "Error [" + code + "]";
483
+ CustomError.prototype.code = code;
484
+ return CustomError;
485
+ }
486
+
457
487
  // Exports
458
488
  module.exports = wrap({ http: http, https: https });
459
489
  module.exports.wrap = wrap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",