follow-redirects 1.4.1 → 1.5.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 (3) hide show
  1. package/README.md +2 -0
  2. package/index.js +14 -0
  3. package/package.json +3 -3
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,6 +58,9 @@ 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
+ }
60
64
  if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
61
65
  this._requestBodyLength += data.length;
62
66
  this._requestBodyBuffers.push({ data: data, encoding: encoding });
@@ -156,6 +160,15 @@ RedirectableRequest.prototype._performRequest = function () {
156
160
 
157
161
  // Processes a response from the current native request
158
162
  RedirectableRequest.prototype._processResponse = function (response) {
163
+ // Store the redirected response
164
+ if (this._options.trackRedirects) {
165
+ this._redirects.push({
166
+ url: this._currentUrl,
167
+ headers: response.headers,
168
+ statusCode: response.statusCode,
169
+ });
170
+ }
171
+
159
172
  // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
160
173
  // that further action needs to be taken by the user agent in order to
161
174
  // fulfill the request. If a Location header field is provided,
@@ -211,6 +224,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
211
224
  else {
212
225
  // The response is not a redirect; return it as-is
213
226
  response.responseUrl = this._currentUrl;
227
+ response.redirects = this._redirects;
214
228
  this.emit("response", response);
215
229
 
216
230
  // Clean up
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -50,10 +50,10 @@
50
50
  "bluebird": "^3.5.1",
51
51
  "concat-stream": "^1.6.0",
52
52
  "coveralls": "^3.0.0",
53
- "eslint": "^4.16.0",
53
+ "eslint": "^4.19.1",
54
54
  "express": "^4.16.2",
55
55
  "mocha": "^5.0.0",
56
- "nyc": "^11.4.1"
56
+ "nyc": "^11.8.0"
57
57
  },
58
58
  "license": "MIT",
59
59
  "nyc": {