follow-redirects 0.0.2 → 0.0.3

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/index.js +84 -81
  2. package/package.json +1 -1
  3. package/test/index.js +14 -13
package/index.js CHANGED
@@ -19,88 +19,91 @@ for (var protocol in protocols) {
19
19
 
20
20
  module.exports[protocol] = h;
21
21
 
22
- h.request = function (options, callback, redirectOptions) {
23
-
24
- redirectOptions = redirectOptions || {};
25
-
26
- var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects;
27
-
28
- var redirect = _.extend({
29
- count: 0,
30
- max: max,
31
- clientRequest: null,
32
- userCallback: callback
33
- }, redirectOptions);
34
-
35
- //console.log(redirect.count);
36
- //console.log(redirect.max);
37
- /**
38
- * Emit error if too many redirects
39
- */
40
- if (redirect.count > redirect.max) {
41
- var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.');
42
- redirect.clientRequest.emit('error', err);
43
- return redirect.clientRequest;
22
+ h.request = function (h) {
23
+ return function (options, callback, redirectOptions) {
24
+
25
+ redirectOptions = redirectOptions || {};
26
+
27
+ var max = (typeof options === 'object' && 'maxRedirects' in options) ? options.maxRedirects : exports.maxRedirects;
28
+
29
+ var redirect = _.extend({
30
+ count: 0,
31
+ max: max,
32
+ clientRequest: null,
33
+ userCallback: callback
34
+ }, redirectOptions);
35
+
36
+ //console.log(redirect.count);
37
+ //console.log(redirect.max);
38
+ /**
39
+ * Emit error if too many redirects
40
+ */
41
+ if (redirect.count > redirect.max) {
42
+ var err = new Error('Max redirects exceeded. To allow more redirects, pass options.maxRedirects property.');
43
+ redirect.clientRequest.emit('error', err);
44
+ return redirect.clientRequest;
45
+ }
46
+
47
+ redirect.count++;
48
+
49
+ /**
50
+ * Parse URL from options
51
+ */
52
+ var reqUrl;
53
+ if (typeof options === 'string') {
54
+ reqUrl = options;
55
+ }
56
+ else {
57
+ reqUrl = url.format(_.extend({ protocol: protocol }, options));
58
+ }
59
+
60
+ /*
61
+ * Build client request
62
+ */
63
+ var clientRequest = h.__proto__.request(options, redirectCallback(reqUrl, redirect));
64
+
65
+ // Save user's clientRequest so we can emit errors later
66
+ if (!redirect.clientRequest) redirect.clientRequest = clientRequest;
67
+
68
+ /**
69
+ * ClientRequest callback for redirects
70
+ */
71
+ function redirectCallback (reqUrl, redirect) {
72
+ return function (res) {
73
+ // status must be 300-399 for redirects
74
+ if (res.statusCode < 300 || res.statusCode > 399) {
75
+ //console.log('[' + res.statusCode + '] callback user on url ' + reqUrl);
76
+ return redirect.userCallback(res);
77
+ }
78
+
79
+ // no `Location:` header => nowhere to redirect
80
+ if (!('location' in res.headers)) {
81
+ //console.log('[no location header] callback user on url ' + reqUrl);
82
+ return redirect.userCallback(res);
83
+ }
84
+
85
+ // save the original clientRequest to our redirectOptions so we can emit errors later
86
+
87
+ // need to use url.resolve() in case location is a relative URL
88
+ var redirectUrl = url.resolve(reqUrl, res.headers['location']);
89
+ // we need to call the right api (http vs https) depending on protocol
90
+ var proto = url.parse(redirectUrl).protocol;
91
+ proto = proto.substr(0, proto.length - 1);
92
+ //console.log('Redirecting from ' + reqUrl + ' to ' + redirectUrl);
93
+ return module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
94
+ };
95
+ }
96
+
97
+ return clientRequest;
44
98
  }
45
-
46
- redirect.count++;
47
-
48
- /**
49
- * Parse URL from options
50
- */
51
- var reqUrl;
52
- if (typeof options === 'string') {
53
- reqUrl = options;
54
- }
55
- else {
56
- reqUrl = url.format(_.extend({ protocol: protocol }, options));
57
- }
58
-
59
- /*
60
- * Build client request
61
- */
62
- var clientRequest = this.__proto__.request(options, redirectCallback(reqUrl, redirect));
63
-
64
- // Save user's clientRequest so we can emit errors later
65
- if (!redirect.clientRequest) redirect.clientRequest = clientRequest;
66
-
67
- /**
68
- * ClientRequest callback for redirects
69
- */
70
- function redirectCallback (reqUrl, redirect) {
71
- return function (res) {
72
- // status must be 300-399 for redirects
73
- if (res.statusCode < 300 || res.statusCode > 399) {
74
- //console.log('[' + res.statusCode + '] callback user on url ' + reqUrl);
75
- return redirect.userCallback(res);
76
- }
77
-
78
- // no `Location:` header => nowhere to redirect
79
- if (!('location' in res.headers)) {
80
- //console.log('[no location header] callback user on url ' + reqUrl);
81
- return redirect.userCallback(res);
82
- }
83
-
84
- // save the original clientRequest to our redirectOptions so we can emit errors later
85
-
86
- // need to use url.resolve() in case location is a relative URL
87
- var redirectUrl = url.resolve(reqUrl, res.headers['location']);
88
- // we need to call the right api (http vs https) depending on protocol
89
- var proto = url.parse(redirectUrl).protocol;
90
- proto = proto.substr(0, proto.length - 1);
91
- //console.log('Redirecting from ' + reqUrl + ' to ' + redirectUrl);
92
- return module.exports[proto].get(redirectUrl, redirectCallback(reqUrl, redirect), redirect);
93
- };
94
- }
95
-
96
- return clientRequest;
97
- }
99
+ }(h);
98
100
 
99
101
  // see https://github.com/joyent/node/blob/master/lib/http.js#L1623
100
- h.get = function (options, cb, redirectOptions) {
101
- var req = this.request(options, cb, redirectOptions);
102
- req.end();
103
- return req;
104
- };
105
-
102
+ h.get = function (h) {
103
+ return function (options, cb, redirectOptions) {
104
+ var req = h.request(options, cb, redirectOptions);
105
+ req.end();
106
+ return req;
107
+ };
108
+ }(h);
106
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/index.js CHANGED
@@ -62,20 +62,21 @@ urls.forEach(function (url) {
62
62
  /**
63
63
  * Test .request
64
64
  */
65
- //console.log((proto + '.' + 'request(' + url + ')').blue);
66
- //var req = http.request(url, function(res) {
67
- ////console.log('STATUS: ' + res.statusCode);
68
- ////console.log('HEADERS: ' + JSON.stringify(res.headers));
69
- //res.setEncoding('utf8');
70
- //res.on('data', function (chunk) {
71
- //console.log('BODY: ' + chunk);
72
- //});
73
- //});
65
+ console.log((proto + '.' + 'request(' + url + ')').blue);
66
+ var request = http.request;
67
+ var req = request(url, function(res) {
68
+ //console.log('STATUS: ' + res.statusCode);
69
+ //console.log('HEADERS: ' + JSON.stringify(res.headers));
70
+ res.setEncoding('utf8');
71
+ res.on('data', function (chunk) {
72
+ console.log('BODY: ' + chunk);
73
+ });
74
+ });
74
75
 
75
- //req.on('error', function(e) {
76
- //console.log('problem with request: ' + e.message);
77
- //});
76
+ req.on('error', function(e) {
77
+ console.log('problem with request: ' + e.message);
78
+ });
78
79
 
79
- //req.end();
80
+ req.end();
80
81
  };
81
82
  });