Permalink
Browse files

added basic debugging

  • Loading branch information...
1 parent d470944 commit e9cc0c96aef574a916a6f4ca8184f3fab75174fd @icodeforlove committed Jan 10, 2013
Showing with 34 additions and 12 deletions.
  1. +7 −0 Readme.md
  2. +23 −8 lib/requester.js
  3. +4 −4 package.json
View
7 Readme.md
@@ -77,6 +77,13 @@ they support the following properties
* {Boolean} follow
* {Number} followMax
+## debugging
+
+you can set debug to the following
+* 1 - outgoing requests
+* 2 - outgoing requests and responses with headers
+* 3 - outgoing requests, responses with headers, and response body
+
## proxies
request objects support proxies but you also can add / remove them from the proxy rotation like this
View
31 lib/requester.js
@@ -10,11 +10,13 @@ var http = require('http'),
xml2js = require('xml2js'),
async = require('async'),
zlib = require('zlib'),
- _ = require('underscore');
+ _ = require('underscore'),
+ colors = require('colors');
function RequesterHandler ($config) {
$config = $config || {};
+ this._debug = $config.debug;
this._cookies = $config.cookies || {};
this._headers = $config.headers || {};
this._timeout = $config.timeout || 30000;
@@ -87,6 +89,7 @@ RequesterHandler.prototype = {
_makeRequest: function ($url, $options, callback) {
// prepare options
+ $options.debug = $options.debug || this._debug;
$options.cookies = $options.cookies && this._cookies ? _.extend({}, this._cookies, $options.cookies) : $options.cookies || _.extend({}, this._cookies);
$options.headers = $options.headers && this._headers ? _.extend({}, this._headers, $options.headers) : $options.headers || _.extend({}, this._headers);
$options.timeout = $options.timeout || this._timeout;
@@ -132,6 +135,7 @@ function Requester ($url, $options, callback) {
this._follow = $options.follow;
this._followMax = $options.followMax;
this._proxy = $options.proxy;
+ this._debug = $options.debug;
this._data = $options.data;
this._protocol = $url.protocol === 'https:' && !this._proxy ? https : http;
this._port = $url.protocol === 'https:' && this._proxy ? 443 : 80;
@@ -213,14 +217,19 @@ Requester.prototype = {
_prepareRequestObject: function () {
var self = this;
+ var options = {
+ host: this._proxy ? this._proxy.ip : this._url.hostname,
+ port: this._proxy ? this._proxy.port : this._url.port,
+ path: this._proxy ? url.format(this._url) : this._url.pathname + (this._url.search || ''),
+ method: this._method,
+ headers: this._headers
+ };
+
+ if (this._debug > 0) console.log(this._method.grey + ' ' + url.format(this._url));
+ if (this._debug > 1) console.log(JSON.stringify(this._headers, null, '\t').grey);
+
this._requestObject = this._protocol.request(
- {
- host: this._proxy ? this._proxy.ip : this._url.hostname,
- port: this._proxy ? this._proxy.port : this._url.port,
- path: this._proxy ? url.format(this._url) : this._url.pathname + (this._url.search || ''),
- method: this._method,
- headers: this._headers
- },
+ options,
function (response) {
if (response.headers['content-encoding'] === 'gzip') {
self._gzipResponse(response);
@@ -301,6 +310,12 @@ Requester.prototype = {
_checkResponse: function (response) {
var self = this;
+ if (this._debug > 1) {
+ console.log(this._method.grey + ' RESPONSE '.grey + String(response.statusCode).red + ' ' + url.format(this._url) + ' - '.grey + String((response.connection.bytesRead / 1024).toFixed(2) + 'kb').grey);
+ console.log(JSON.stringify(response.headers, null, '\t').grey);
+ if (this._debug > 2) console.log(response.body.grey);
+ }
+
response.cookies = {};
if (response.headers['set-cookie']) {
response.headers['set-cookie'].forEach(function (value) {
View
8 package.json
@@ -1,6 +1,6 @@
{
"name": "requester",
- "version": "0.1.6",
+ "version": "0.1.7",
"description": "swiss army knife for requests",
"main": "index.js",
"directories": {
@@ -21,13 +21,13 @@
"author": "Chad Scira",
"license": "MIT",
"devDependencies": {
- "async": "0.1.22",
- "colors": "0.6.0-1"
+ "async": "0.1.22"
},
"dependencies": {
"async": "0.1.22",
"form-data": "0.0.4",
"underscore": "1.4.0",
- "xml2js": "0.2.0"
+ "xml2js": "0.2.0",
+ "colors": "0.6.0-1"
}
}

0 comments on commit e9cc0c9

Please sign in to comment.