follow-redirects 1.4.0 → 1.5.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.

Potentially problematic release.


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

Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/index.js +26 -1
  3. package/package.json +7 -7
package/README.md CHANGED
@@ -79,6 +79,8 @@ the following per-request options are supported:
79
79
 
80
80
  - `agents` (default: `undefined`) – sets the `agent` option per protocol, since HTTP and HTTPS use different agents. Example value: `{ http: new http.Agent(), https: new https.Agent() }`
81
81
 
82
+ - `trackRedirects` (default: `false`) – whether to store the redirected response details into the `redirects` array on the response object.
83
+
82
84
 
83
85
  ### Advanced usage
84
86
  By default, `follow-redirects` will use the Node.js default implementations
package/index.js CHANGED
@@ -24,6 +24,7 @@ function RedirectableRequest(options, responseCallback) {
24
24
  options.headers = options.headers || {};
25
25
  this._options = options;
26
26
  this._redirectCount = 0;
27
+ this._redirects = [];
27
28
  this._requestBodyLength = 0;
28
29
  this._requestBodyBuffers = [];
29
30
 
@@ -57,11 +58,22 @@ RedirectableRequest.prototype = Object.create(Writable.prototype);
57
58
 
58
59
  // Writes buffered data to the current native request
59
60
  RedirectableRequest.prototype.write = function (data, encoding, callback) {
61
+ if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
62
+ throw new Error("data should be a string, Buffer or Uint8Array");
63
+ }
64
+ // Ignore empty buffers, since writing them doesn't invoke the callback
65
+ // https://github.com/nodejs/node/issues/22066
66
+ if (data.length === 0) {
67
+ callback();
68
+ return;
69
+ }
70
+ // Only write when we don't exceed the maximum body length
60
71
  if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
61
72
  this._requestBodyLength += data.length;
62
73
  this._requestBodyBuffers.push({ data: data, encoding: encoding });
63
74
  this._currentRequest.write(data, encoding, callback);
64
75
  }
76
+ // Error when we exceed the maximum body length
65
77
  else {
66
78
  this.emit("error", new Error("Request body larger than maxBodyLength limit"));
67
79
  this.abort();
@@ -106,7 +118,7 @@ RedirectableRequest.prototype.removeHeader = function (name) {
106
118
  // Proxy all public ClientRequest properties
107
119
  ["aborted", "connection", "socket"].forEach(function (property) {
108
120
  Object.defineProperty(RedirectableRequest.prototype, property, {
109
- get() { return this._currentRequest[property]; },
121
+ get: function () { return this._currentRequest[property]; },
110
122
  });
111
123
  });
112
124
 
@@ -156,6 +168,15 @@ RedirectableRequest.prototype._performRequest = function () {
156
168
 
157
169
  // Processes a response from the current native request
158
170
  RedirectableRequest.prototype._processResponse = function (response) {
171
+ // Store the redirected response
172
+ if (this._options.trackRedirects) {
173
+ this._redirects.push({
174
+ url: this._currentUrl,
175
+ headers: response.headers,
176
+ statusCode: response.statusCode,
177
+ });
178
+ }
179
+
159
180
  // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
160
181
  // that further action needs to be taken by the user agent in order to
161
182
  // fulfill the request. If a Location header field is provided,
@@ -207,10 +228,14 @@ RedirectableRequest.prototype._processResponse = function (response) {
207
228
  Object.assign(this._options, url.parse(redirectUrl));
208
229
  this._isRedirect = true;
209
230
  this._performRequest();
231
+
232
+ // Discard the remainder of the response to avoid waiting for data
233
+ response.destroy();
210
234
  }
211
235
  else {
212
236
  // The response is not a redirect; return it as-is
213
237
  response.responseUrl = this._currentUrl;
238
+ response.redirects = this._redirects;
214
239
  this.emit("response", response);
215
240
 
216
241
  // Clean up
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.4.0",
3
+ "version": "1.5.2",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -47,13 +47,13 @@
47
47
  "debug": "^3.1.0"
48
48
  },
49
49
  "devDependencies": {
50
- "bluebird": "^3.4.0",
51
- "concat-stream": "^1.5.2",
50
+ "bluebird": "^3.5.1",
51
+ "concat-stream": "^1.6.0",
52
52
  "coveralls": "^3.0.0",
53
- "eslint": "^4.14.0",
54
- "express": "^4.13.0",
55
- "mocha": "^4.0.1",
56
- "nyc": "^11.3.0"
53
+ "eslint": "^4.19.1",
54
+ "express": "^4.16.2",
55
+ "mocha": "^5.0.0",
56
+ "nyc": "^11.8.0"
57
57
  },
58
58
  "license": "MIT",
59
59
  "nyc": {