follow-redirects 1.14.9 → 1.15.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 +8 -1
  2. package/index.js +23 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -63,10 +63,17 @@ const { http, https } = require('follow-redirects');
63
63
 
64
64
  const options = url.parse('http://bit.ly/900913');
65
65
  options.maxRedirects = 10;
66
- options.beforeRedirect = (options, { headers }) => {
66
+ options.beforeRedirect = (options, response, request) => {
67
67
  // Use this to adjust the request options upon redirecting,
68
68
  // to inspect the latest response headers,
69
69
  // or to cancel the request by throwing an error
70
+
71
+ // response.headers = the redirect response headers
72
+ // response.statusCode = the redirect response code (eg. 301, 307, etc.)
73
+
74
+ // request.url = the requested URL that resulted in a redirect
75
+ // request.headers = the headers in the request that resulted in a redirect
76
+ // request.method = the method of the request that resulted in a redirect
70
77
  if (options.hostname === "example.com") {
71
78
  options.auth = "user:password";
72
79
  }
package/index.js CHANGED
@@ -270,7 +270,7 @@ RedirectableRequest.prototype._performRequest = function () {
270
270
  // If specified, use the agent corresponding to the protocol
271
271
  // (HTTP and HTTPS use different types of agents)
272
272
  if (this._options.agents) {
273
- var scheme = protocol.substr(0, protocol.length - 1);
273
+ var scheme = protocol.slice(0, -1);
274
274
  this._options.agent = this._options.agents[scheme];
275
275
  }
276
276
 
@@ -362,10 +362,21 @@ RedirectableRequest.prototype._processResponse = function (response) {
362
362
  return;
363
363
  }
364
364
 
365
+ // Store the request headers if applicable
366
+ var requestHeaders;
367
+ var beforeRedirect = this._options.beforeRedirect;
368
+ if (beforeRedirect) {
369
+ requestHeaders = Object.assign({
370
+ // The Host header was set by nativeProtocol.request
371
+ Host: response.req.getHeader("host"),
372
+ }, this._options.headers);
373
+ }
374
+
365
375
  // RFC7231§6.4: Automatic redirection needs to done with
366
376
  // care for methods not known to be safe, […]
367
377
  // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
368
378
  // the request method from POST to GET for the subsequent request.
379
+ var method = this._options.method;
369
380
  if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
370
381
  // RFC7231§6.4.4: The 303 (See Other) status code indicates that
371
382
  // the server is redirecting the user agent to a different resource […]
@@ -413,10 +424,18 @@ RedirectableRequest.prototype._processResponse = function (response) {
413
424
  }
414
425
 
415
426
  // Evaluate the beforeRedirect callback
416
- if (typeof this._options.beforeRedirect === "function") {
417
- var responseDetails = { headers: response.headers };
427
+ if (typeof beforeRedirect === "function") {
428
+ var responseDetails = {
429
+ headers: response.headers,
430
+ statusCode: statusCode,
431
+ };
432
+ var requestDetails = {
433
+ url: currentUrl,
434
+ method: method,
435
+ headers: requestHeaders,
436
+ };
418
437
  try {
419
- this._options.beforeRedirect.call(null, this._options, responseDetails);
438
+ beforeRedirect(this._options, responseDetails, requestDetails);
420
439
  }
421
440
  catch (err) {
422
441
  this.emit("error", err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.14.9",
3
+ "version": "1.15.0",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",