@@ -75,10 +75,10 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
75
75
}
76
76
77
77
// Validate input and shift parameters if necessary
78
- if ( ! ( typeof data === "string" || typeof data === "object" && ( "length" in data ) ) ) {
78
+ if ( ! isString ( data ) && ! isBuffer ( data ) ) {
79
79
throw new TypeError ( "data should be a string, Buffer or Uint8Array" ) ;
80
80
}
81
- if ( typeof encoding === "function" ) {
81
+ if ( isFunction ( encoding ) ) {
82
82
callback = encoding ;
83
83
encoding = null ;
84
84
}
@@ -107,11 +107,11 @@ RedirectableRequest.prototype.write = function (data, encoding, callback) {
107
107
// Ends the current native request
108
108
RedirectableRequest . prototype . end = function ( data , encoding , callback ) {
109
109
// Shift parameters if necessary
110
- if ( typeof data === "function" ) {
110
+ if ( isFunction ( data ) ) {
111
111
callback = data ;
112
112
data = encoding = null ;
113
113
}
114
- else if ( typeof encoding === "function" ) {
114
+ else if ( isFunction ( encoding ) ) {
115
115
callback = encoding ;
116
116
encoding = null ;
117
117
}
@@ -429,7 +429,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
429
429
}
430
430
431
431
// Evaluate the beforeRedirect callback
432
- if ( typeof beforeRedirect === "function" ) {
432
+ if ( isFunction ( beforeRedirect ) ) {
433
433
var responseDetails = {
434
434
headers : response . headers ,
435
435
statusCode : statusCode ,
@@ -476,7 +476,7 @@ function wrap(protocols) {
476
476
// Executes a request, following redirects
477
477
function request ( input , options , callback ) {
478
478
// Parse parameters
479
- if ( typeof input === "string" ) {
479
+ if ( isString ( input ) ) {
480
480
var urlStr = input ;
481
481
try {
482
482
input = urlToOptions ( new URL ( urlStr ) ) ;
@@ -494,7 +494,7 @@ function wrap(protocols) {
494
494
options = input ;
495
495
input = { protocol : protocol } ;
496
496
}
497
- if ( typeof options === "function" ) {
497
+ if ( isFunction ( options ) ) {
498
498
callback = options ;
499
499
options = null ;
500
500
}
@@ -593,6 +593,18 @@ function isSubdomain(subdomain, domain) {
593
593
return dot > 0 && subdomain [ dot ] === "." && subdomain . endsWith ( domain ) ;
594
594
}
595
595
596
+ function isString ( value ) {
597
+ return typeof value === "string" || value instanceof String ;
598
+ }
599
+
600
+ function isFunction ( value ) {
601
+ return typeof value === "function" ;
602
+ }
603
+
604
+ function isBuffer ( value ) {
605
+ return typeof value === "object" && ( "length" in value ) ;
606
+ }
607
+
596
608
// Exports
597
609
module . exports = wrap ( { http : http , https : https } ) ;
598
610
module . exports . wrap = wrap ;
0 commit comments