follow-redirects 1.15.1 → 1.15.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 (2) hide show
  1. package/index.js +48 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -15,6 +15,11 @@ events.forEach(function (event) {
15
15
  };
16
16
  });
17
17
 
18
+ var InvalidUrlError = createErrorType(
19
+ "ERR_INVALID_URL",
20
+ "Invalid URL",
21
+ TypeError
22
+ );
18
23
  // Error types with codes
19
24
  var RedirectionError = createErrorType(
20
25
  "ERR_FR_REDIRECTION_FAILURE",
@@ -75,10 +80,10 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
75
80
  }
76
81
 
77
82
  // Validate input and shift parameters if necessary
78
- if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
83
+ if (!isString(data) && !isBuffer(data)) {
79
84
  throw new TypeError("data should be a string, Buffer or Uint8Array");
80
85
  }
81
- if (typeof encoding === "function") {
86
+ if (isFunction(encoding)) {
82
87
  callback = encoding;
83
88
  encoding = null;
84
89
  }
@@ -107,11 +112,11 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
107
112
  // Ends the current native request
108
113
  RedirectableRequest.prototype.end = function (data, encoding, callback) {
109
114
  // Shift parameters if necessary
110
- if (typeof data === "function") {
115
+ if (isFunction(data)) {
111
116
  callback = data;
112
117
  data = encoding = null;
113
118
  }
114
- else if (typeof encoding === "function") {
119
+ else if (isFunction(encoding)) {
115
120
  callback = encoding;
116
121
  encoding = null;
117
122
  }
@@ -288,7 +293,7 @@ RedirectableRequest.prototype._performRequest = function () {
288
293
  url.format(this._options) :
289
294
  // When making a request to a proxy, […]
290
295
  // a client MUST send the target URI in absolute-form […].
291
- this._currentUrl = this._options.path;
296
+ this._options.path;
292
297
 
293
298
  // End a redirected request
294
299
  // (The first request must be ended explicitly with RedirectableRequest#end)
@@ -409,7 +414,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
409
414
  redirectUrl = url.resolve(currentUrl, location);
410
415
  }
411
416
  catch (cause) {
412
- this.emit("error", new RedirectionError(cause));
417
+ this.emit("error", new RedirectionError({ cause: cause }));
413
418
  return;
414
419
  }
415
420
 
@@ -429,7 +434,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
429
434
  }
430
435
 
431
436
  // Evaluate the beforeRedirect callback
432
- if (typeof beforeRedirect === "function") {
437
+ if (isFunction(beforeRedirect)) {
433
438
  var responseDetails = {
434
439
  headers: response.headers,
435
440
  statusCode: statusCode,
@@ -454,7 +459,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
454
459
  this._performRequest();
455
460
  }
456
461
  catch (cause) {
457
- this.emit("error", new RedirectionError(cause));
462
+ this.emit("error", new RedirectionError({ cause: cause }));
458
463
  }
459
464
  };
460
465
 
@@ -476,15 +481,19 @@ function wrap(protocols) {
476
481
  // Executes a request, following redirects
477
482
  function request(input, options, callback) {
478
483
  // Parse parameters
479
- if (typeof input === "string") {
480
- var urlStr = input;
484
+ if (isString(input)) {
485
+ var parsed;
481
486
  try {
482
- input = urlToOptions(new URL(urlStr));
487
+ parsed = urlToOptions(new URL(input));
483
488
  }
484
489
  catch (err) {
485
490
  /* istanbul ignore next */
486
- input = url.parse(urlStr);
491
+ parsed = url.parse(input);
492
+ }
493
+ if (!isString(parsed.protocol)) {
494
+ throw new InvalidUrlError({ input });
487
495
  }
496
+ input = parsed;
488
497
  }
489
498
  else if (URL && (input instanceof URL)) {
490
499
  input = urlToOptions(input);
@@ -494,7 +503,7 @@ function wrap(protocols) {
494
503
  options = input;
495
504
  input = { protocol: protocol };
496
505
  }
497
- if (typeof options === "function") {
506
+ if (isFunction(options)) {
498
507
  callback = options;
499
508
  options = null;
500
509
  }
@@ -505,6 +514,9 @@ function wrap(protocols) {
505
514
  maxBodyLength: exports.maxBodyLength,
506
515
  }, input, options);
507
516
  options.nativeProtocols = nativeProtocols;
517
+ if (!isString(options.host) && !isString(options.hostname)) {
518
+ options.hostname = "::1";
519
+ }
508
520
 
509
521
  assert.equal(options.protocol, protocol, "protocol mismatch");
510
522
  debug("options", options);
@@ -562,21 +574,19 @@ function removeMatchingHeaders(regex, headers) {
562
574
  undefined : String(lastValue).trim();
563
575
  }
564
576
 
565
- function createErrorType(code, defaultMessage) {
566
- function CustomError(cause) {
577
+ function createErrorType(code, message, baseClass) {
578
+ // Create constructor
579
+ function CustomError(properties) {
567
580
  Error.captureStackTrace(this, this.constructor);
568
- if (!cause) {
569
- this.message = defaultMessage;
570
- }
571
- else {
572
- this.message = defaultMessage + ": " + cause.message;
573
- this.cause = cause;
574
- }
581
+ Object.assign(this, properties || {});
582
+ this.code = code;
583
+ this.message = this.cause ? message + ": " + this.cause.message : message;
575
584
  }
576
- CustomError.prototype = new Error();
585
+
586
+ // Attach constructor and set default properties
587
+ CustomError.prototype = new (baseClass || Error)();
577
588
  CustomError.prototype.constructor = CustomError;
578
589
  CustomError.prototype.name = "Error [" + code + "]";
579
- CustomError.prototype.code = code;
580
590
  return CustomError;
581
591
  }
582
592
 
@@ -589,10 +599,23 @@ function abortRequest(request) {
589
599
  }
590
600
 
591
601
  function isSubdomain(subdomain, domain) {
592
- const dot = subdomain.length - domain.length - 1;
602
+ assert(isString(subdomain) && isString(domain));
603
+ var dot = subdomain.length - domain.length - 1;
593
604
  return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
594
605
  }
595
606
 
607
+ function isString(value) {
608
+ return typeof value === "string" || value instanceof String;
609
+ }
610
+
611
+ function isFunction(value) {
612
+ return typeof value === "function";
613
+ }
614
+
615
+ function isBuffer(value) {
616
+ return typeof value === "object" && ("length" in value);
617
+ }
618
+
596
619
  // Exports
597
620
  module.exports = wrap({ http: http, https: https });
598
621
  module.exports.wrap = wrap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.15.1",
3
+ "version": "1.15.2",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",