From 45397d258afa0c217800b09b5c3f25555f5e53ab Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Thu, 26 Jul 2012 23:14:08 -0400 Subject: [PATCH 1/3] added feature detection for http.request, so newer versions of nodejs dont throw a deprecation warning --- .DS_Store | Bin 0 -> 6148 bytes lib/exceptional.js | 56 +++++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..825b77c9403bcc3501ef8f635b29c0733b439db9 GIT binary patch literal 6148 zcmeHK%Sr=55Ue%<1733UIKSW@3?Y7jKcF!=2rDay;Bim+E{BaXhz&7Zr^?5ZM;7B${5;IMyx9(Lm_`}crzuW`U9p74qv z{Bdx?f(2i&%aY^3TVzr|3P=GdAO)nrSqfA)UEQ2jMPw-;1ujtm|2{OjV=tT%#J}6wG l9TTG+^Wg3HHj*;0`JT^v;glG3#)D4O&w%S9lLG&(z&H1*8X5oq literal 0 HcmV?d00001 diff --git a/lib/exceptional.js b/lib/exceptional.js index f9046e0..6f1783b 100644 --- a/lib/exceptional.js +++ b/lib/exceptional.js @@ -59,25 +59,51 @@ var Exceptional = { send_error: function(doc) { gzip(doc, 1, function(err, data) { - var client = HTTP.createClient(Exceptional.Port, Exceptional.Host); + var path = '/api/errors?api_key=' + Exceptional.API_KEY + "&protocol_version=" + Exceptional.PROTOCOL_VERSION; - var headers = { - 'Host' : Exceptional.Host, - 'Content-Length' : data.length - }; + // use feature detection to fallback to deprecated + if (!HTTP.request) + { + // older versions of nodejs use http.createClient + var headers = { + 'Host' : Exceptional.Host, + 'Content-Length' : data.length + }; - var request = client.request('POST', '/api/errors?api_key=' + Exceptional.API_KEY + "&protocol_version=" + Exceptional.PROTOCOL_VERSION, headers); + var client = HTTP.createClient(Exceptional.Port, Exceptional.Host); + var request = client.request('POST', path, headers); - request.write(data); - request.end(); + request.write(data); + request.end(); - request.on('response', function (response) { - if (response.statusCode === 200) { - if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); - } else { - if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); - } - }); + request.on('response', function (response) { + if (response.statusCode === 200) { + if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); + } else { + if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); + } + }); + } + else + { + // newer versions of nodejs use http.request + var options = { + host: Exceptional.Host, + port: Exceptional.Port, + path: path, + method: 'POST' + }; + + HTTP.request(options, function(response) { + if (response.statusCode === 200) { + if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); + } else { + if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); + } + }); + } + + }); } }; From dc93be70d4dc0a85a7f7c0acbdaf1baa46e63789 Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Thu, 26 Jul 2012 23:39:41 -0400 Subject: [PATCH 2/3] updated .gitignore to properly ignore .DS_Store files --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 825b77c9403bcc3501ef8f635b29c0733b439db9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%Sr=55Ue%<1733UIKSW@3?Y7jKcF!=2rDay;Bim+E{BaXhz&7Zr^?5ZM;7B${5;IMyx9(Lm_`}crzuW`U9p74qv z{Bdx?f(2i&%aY^3TVzr|3P=GdAO)nrSqfA)UEQ2jMPw-;1ujtm|2{OjV=tT%#J}6wG l9TTG+^Wg3HHj*;0`JT^v;glG3#)D4O&w%S9lLG&(z&H1*8X5oq diff --git a/.gitignore b/.gitignore index 265f17f..e43b0f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -DS_Store +.DS_Store From 6f0eed48aae0df9412edee56da6e71bc7498edd4 Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Fri, 27 Jul 2012 00:50:49 -0400 Subject: [PATCH 3/3] replaced custom gzip support with zlib. Added HTTP headers. unhardcoded output --- lib/exceptional.js | 42 +++++++++++++++++++++++------------------- lib/gzip/index.js | 41 ----------------------------------------- 2 files changed, 23 insertions(+), 60 deletions(-) delete mode 100644 lib/gzip/index.js diff --git a/lib/exceptional.js b/lib/exceptional.js index 6f1783b..c27c484 100644 --- a/lib/exceptional.js +++ b/lib/exceptional.js @@ -1,5 +1,5 @@ var HTTP = require('http'); -var gzip = require('./gzip').gzip; +var zlib = require('zlib'); var fs = require('fs'); var Exceptional = { @@ -57,30 +57,31 @@ var Exceptional = { }, send_error: function(doc) { - gzip(doc, 1, function(err, data) { - + if(!Buffer.isBuffer(doc)) + doc = new Buffer(doc); + + zlib.gzip(doc, function (err, data) { var path = '/api/errors?api_key=' + Exceptional.API_KEY + "&protocol_version=" + Exceptional.PROTOCOL_VERSION; + var request = null; + + var headers = { + 'Host' : Exceptional.Host, + 'Content-Length' : data.length, + 'Content-Type' : 'application/json; charset=UTF-8' + }; // use feature detection to fallback to deprecated if (!HTTP.request) { // older versions of nodejs use http.createClient - var headers = { - 'Host' : Exceptional.Host, - 'Content-Length' : data.length - }; - var client = HTTP.createClient(Exceptional.Port, Exceptional.Host); - var request = client.request('POST', path, headers); - - request.write(data); - request.end(); + request = client.request('POST', path, headers); request.on('response', function (response) { if (response.statusCode === 200) { - if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); + if (Exceptional.VERBOSE) console.log("Error data successfully sent to " + Exceptional.Host + ':' + Exceptional.Port ); } else { - if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); + if (Exceptional.VERBOSE) console.log("Error sending to " + Exceptional.Host + ":" + Exceptional.Port + " :" + response.statusCode); } }); } @@ -91,19 +92,22 @@ var Exceptional = { host: Exceptional.Host, port: Exceptional.Port, path: path, - method: 'POST' + method: 'POST', + header: headers }; - HTTP.request(options, function(response) { + request = HTTP.request(options, function(response) { if (response.statusCode === 200) { - if (Exceptional.VERBOSE) console.log("Error data successfully sent to exceptional"); + if (Exceptional.VERBOSE) console.log("Error data successfully sent to " + Exceptional.Host + ':' + Exceptional.Port ); } else { - if (Exceptional.VERBOSE) console.log("Error sending to api.getexceptional.com :" + response.statusCode); + if (Exceptional.VERBOSE) console.log("Error sending to " + Exceptional.Host + ":" + Exceptional.Port + " :" + response.statusCode); } }); } - + // post the data + request.write(data); + request.end(); }); } }; diff --git a/lib/gzip/index.js b/lib/gzip/index.js deleted file mode 100644 index bb848c5..0000000 --- a/lib/gzip/index.js +++ /dev/null @@ -1,41 +0,0 @@ -var spawn = require('child_process').spawn, - buffer = require('buffer').Buffer; - -exports.gzip = function(data) { - var rate = 8, - enc = 'utf8', - isBuffer = buffer.isBuffer(data), - args = Array.prototype.slice.call(arguments, 1), - callback; - - if (!isBuffer && typeof args[0] === 'string') { - enc = args.shift(); - } - - if (typeof args[0] === 'number') { - rate = args.shift() - 0; - } - - callback = args[0]; - - if (!callback) return; - - var gzip = spawn('gzip', ['-' + (rate-0),'-c', '-']); - - var output = new buffer(0); - - gzip.stdout.on('data', function(data) { - output = data; - }); - - gzip.on('exit', function(code) { - callback(code, output); - }); - - if (isBuffer) { - gzip.stdin.encoding = 'binary'; - gzip.stdin.end(data.length ? data: ''); - } else { - gzip.stdin.end(data ? data.toString() : '', enc); - } -}; \ No newline at end of file