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.
- package/index.js +48 -25
- 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 (!(
|
83
|
+
if (!isString(data) && !isBuffer(data)) {
|
79
84
|
throw new TypeError("data should be a string, Buffer or Uint8Array");
|
80
85
|
}
|
81
|
-
if (
|
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 (
|
115
|
+
if (isFunction(data)) {
|
111
116
|
callback = data;
|
112
117
|
data = encoding = null;
|
113
118
|
}
|
114
|
-
else if (
|
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.
|
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 (
|
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 (
|
480
|
-
var
|
484
|
+
if (isString(input)) {
|
485
|
+
var parsed;
|
481
486
|
try {
|
482
|
-
|
487
|
+
parsed = urlToOptions(new URL(input));
|
483
488
|
}
|
484
489
|
catch (err) {
|
485
490
|
/* istanbul ignore next */
|
486
|
-
|
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 (
|
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,
|
566
|
-
|
577
|
+
function createErrorType(code, message, baseClass) {
|
578
|
+
// Create constructor
|
579
|
+
function CustomError(properties) {
|
567
580
|
Error.captureStackTrace(this, this.constructor);
|
568
|
-
|
569
|
-
|
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
|
-
|
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
|
-
|
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;
|